package androidx.core.app;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
import java.util.Iterator;

/* JADX INFO: compiled from: TaskStackBuilder.java */
/* JADX INFO: loaded from: classes.dex */
public final class j implements Iterable<Intent> {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final ArrayList<Intent> f1626e = new ArrayList<>();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final Context f1627f;

    /* JADX INFO: compiled from: TaskStackBuilder.java */
    public interface a {
        Intent g();
    }

    private j(Context context) {
        this.f1627f = context;
    }

    public static j d(Context context) {
        return new j(context);
    }

    public j a(Intent intent) {
        this.f1626e.add(intent);
        return this;
    }

    /* JADX WARN: Multi-variable type inference failed */
    public j b(Activity activity) {
        Intent intentG = activity instanceof a ? ((a) activity).g() : null;
        if (intentG == null) {
            intentG = g.a(activity);
        }
        if (intentG != null) {
            ComponentName component = intentG.getComponent();
            if (component == null) {
                component = intentG.resolveActivity(this.f1627f.getPackageManager());
            }
            c(component);
            a(intentG);
        }
        return this;
    }

    public j c(ComponentName componentName) {
        int size = this.f1626e.size();
        try {
            Intent intentB = g.b(this.f1627f, componentName);
            while (intentB != null) {
                this.f1626e.add(size, intentB);
                intentB = g.b(this.f1627f, intentB.getComponent());
            }
            return this;
        } catch (PackageManager.NameNotFoundException e3) {
            Log.e("TaskStackBuilder", "Bad ComponentName while traversing activity parent metadata");
            throw new IllegalArgumentException(e3);
        }
    }

    public void e() {
        f(null);
    }

    public void f(Bundle bundle) {
        if (this.f1626e.isEmpty()) {
            throw new IllegalStateException("No intents added to TaskStackBuilder; cannot startActivities");
        }
        Intent[] intentArr = (Intent[]) this.f1626e.toArray(new Intent[0]);
        intentArr[0] = new Intent(intentArr[0]).addFlags(268484608);
        if (androidx.core.content.a.e(this.f1627f, intentArr, bundle)) {
            return;
        }
        Intent intent = new Intent(intentArr[intentArr.length - 1]);
        intent.addFlags(268435456);
        this.f1627f.startActivity(intent);
    }

    @Override // java.lang.Iterable
    @Deprecated
    public Iterator<Intent> iterator() {
        return this.f1626e.iterator();
    }
}
