package androidx.browser.customtabs;

import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.customtabs.ICustomTabsCallback;
import android.support.customtabs.ICustomTabsService;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.media3.common.C;
import java.util.ArrayList;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public abstract class l {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final ICustomTabsService f877a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ComponentName f878b;

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

    public l(ICustomTabsService iCustomTabsService, ComponentName componentName, Context context) {
        this.f877a = iCustomTabsService;
        this.f878b = componentName;
        this.f879c = context;
    }

    public static boolean bindCustomTabsService(@NonNull Context context, @Nullable String str, @NonNull v vVar) {
        vVar.setApplicationContext(context.getApplicationContext());
        Intent intent = new Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        if (!TextUtils.isEmpty(str)) {
            intent.setPackage(str);
        }
        return context.bindService(intent, vVar, 33);
    }

    public static boolean bindCustomTabsServicePreservePriority(@NonNull Context context, @Nullable String str, @NonNull v vVar) {
        vVar.setApplicationContext(context.getApplicationContext());
        Intent intent = new Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        if (!TextUtils.isEmpty(str)) {
            intent.setPackage(str);
        }
        return context.bindService(intent, vVar, 1);
    }

    private ICustomTabsCallback.Stub createCallbackWrapper(@Nullable d dVar) {
        return new k(dVar);
    }

    @Nullable
    public static String getPackageName(@NonNull Context context, @Nullable List<String> list) {
        return getPackageName(context, list, false);
    }

    @NonNull
    @RestrictTo({RestrictTo.Scope.f555a})
    public static y newPendingSession(@NonNull Context context, @Nullable d dVar, int i10) {
        return new y(dVar, PendingIntent.getActivity(context, i10, new Intent(), C.BUFFER_FLAG_NOT_DEPENDED_ON));
    }

    @Nullable
    private z newSessionInternal(@Nullable d dVar, @Nullable PendingIntent pendingIntent) {
        boolean zNewSession;
        ICustomTabsCallback.Stub stubCreateCallbackWrapper = createCallbackWrapper(dVar);
        ICustomTabsService iCustomTabsService = this.f877a;
        try {
            if (pendingIntent != null) {
                Bundle bundle = new Bundle();
                bundle.putParcelable("android.support.customtabs.extra.SESSION_ID", pendingIntent);
                zNewSession = iCustomTabsService.newSessionWithExtras(stubCreateCallbackWrapper, bundle);
            } else {
                zNewSession = iCustomTabsService.newSession(stubCreateCallbackWrapper);
            }
            if (zNewSession) {
                return new z(iCustomTabsService, stubCreateCallbackWrapper, this.f878b, pendingIntent);
            }
            return null;
        } catch (RemoteException unused) {
            return null;
        }
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f555a})
    public z attachSession(@NonNull y yVar) {
        return newSessionInternal(yVar.getCallback(), yVar.getId());
    }

    @Nullable
    public Bundle extraCommand(@NonNull String str, @Nullable Bundle bundle) {
        try {
            return this.f877a.extraCommand(str, bundle);
        } catch (RemoteException unused) {
            return null;
        }
    }

    @Nullable
    public z newSession(@Nullable d dVar, int i10) {
        return newSessionInternal(dVar, PendingIntent.getActivity(this.f879c, i10, new Intent(), C.BUFFER_FLAG_NOT_DEPENDED_ON));
    }

    @Nullable
    public static String getPackageName(@NonNull Context context, @Nullable List<String> list, boolean z10) {
        ResolveInfo resolveInfoResolveActivity;
        PackageManager packageManager = context.getPackageManager();
        List<String> arrayList = list == null ? new ArrayList() : list;
        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://"));
        if (!z10 && (resolveInfoResolveActivity = packageManager.resolveActivity(intent, 0)) != null) {
            String str = resolveInfoResolveActivity.activityInfo.packageName;
            ArrayList arrayList2 = new ArrayList(arrayList.size() + 1);
            arrayList2.add(str);
            if (list != null) {
                arrayList2.addAll(list);
            }
            arrayList = arrayList2;
        }
        Intent intent2 = new Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        for (String str2 : arrayList) {
            intent2.setPackage(str2);
            if (packageManager.resolveService(intent2, 0) != null) {
                return str2;
            }
        }
        return null;
    }

    @Nullable
    public z newSession(@Nullable d dVar) {
        return newSessionInternal(dVar, null);
    }
}
