package android.support.v4.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 d0 implements Iterable<Intent> {

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

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

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

    private d0(Context context) {
        this.f513c = context;
    }

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

    public d0 a(Intent intent) {
        this.f512b.add(intent);
        return this;
    }

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

    public d0 c(ComponentName componentName) {
        int size = this.f512b.size();
        try {
            Intent intentB = t.b(this.f513c, componentName);
            while (intentB != null) {
                this.f512b.add(size, intentB);
                intentB = t.b(this.f513c, 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.f512b.isEmpty()) {
            throw new IllegalStateException("No intents added to TaskStackBuilder; cannot startActivities");
        }
        ArrayList<Intent> arrayList = this.f512b;
        Intent[] intentArr = (Intent[]) arrayList.toArray(new Intent[arrayList.size()]);
        intentArr[0] = new Intent(intentArr[0]).addFlags(268484608);
        if (a.b.c.a.a.c(this.f513c, intentArr, bundle)) {
            return;
        }
        Intent intent = new Intent(intentArr[intentArr.length - 1]);
        intent.addFlags(268435456);
        this.f513c.startActivity(intent);
    }

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