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 g implements Iterable<Intent> {

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

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

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

    private g(Context context) {
        this.f613f = context;
    }

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

    public g a(Intent intent) {
        this.f612b.add(intent);
        return this;
    }

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

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

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

    public void f(Bundle bundle) {
        if (this.f612b.isEmpty()) {
            throw new IllegalStateException("No intents added to TaskStackBuilder; cannot startActivities");
        }
        ArrayList<Intent> arrayList = this.f612b;
        Intent[] intentArr = (Intent[]) arrayList.toArray(new Intent[arrayList.size()]);
        intentArr[0] = new Intent(intentArr[0]).addFlags(268484608);
        if (c.f.d.a.e(this.f613f, intentArr, bundle)) {
            return;
        }
        Intent intent = new Intent(intentArr[intentArr.length - 1]);
        intent.addFlags(268435456);
        this.f613f.startActivity(intent);
    }

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