package com.pichillilorenzo.flutter_inappwebview_android.chrome_custom_tabs;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import androidx.annotation.Nullable;
import androidx.browser.customtabs.d;
import androidx.browser.customtabs.l;
import androidx.browser.customtabs.r;
import androidx.browser.customtabs.v;
import androidx.browser.customtabs.z;
import androidx.browser.trusted.h;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class CustomTabActivityHelper implements ServiceConnectionCallback {
    private l mClient;
    private v mConnection;
    private ConnectionCallback mConnectionCallback;
    private d mCustomTabsCallback;
    private z mCustomTabsSession;

    public interface ConnectionCallback {
        void onCustomTabsConnected();

        void onCustomTabsDisconnected();
    }

    public static boolean isAvailable(Activity activity) {
        return CustomTabsHelper.getPackageNameToUse(activity) != null;
    }

    public static void openCustomTab(Activity activity, Intent intent, Uri uri, @Nullable Map<String, String> map, @Nullable Uri uri2, int i10) {
        intent.setData(uri);
        if (map != null) {
            Bundle bundle = new Bundle();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                bundle.putString(entry.getKey(), entry.getValue());
            }
            intent.putExtra("com.android.browser.headers", bundle);
        }
        if (uri2 != null) {
            intent.putExtra("android.intent.extra.REFERRER", uri2);
        }
        activity.startActivityForResult(intent, i10);
    }

    public static void openTrustedWebActivity(Activity activity, h hVar, Uri uri, @Nullable Map<String, String> map, @Nullable Uri uri2, int i10) {
        openCustomTab(activity, hVar.f903a, uri, map, uri2, i10);
    }

    public boolean bindCustomTabsService(Activity activity) {
        if (this.mClient != null) {
            return true;
        }
        String packageNameToUse = CustomTabsHelper.getPackageNameToUse(activity);
        if (packageNameToUse == null) {
            return false;
        }
        ServiceConnection serviceConnection = new ServiceConnection(this);
        this.mConnection = serviceConnection;
        return l.bindCustomTabsService(activity, packageNameToUse, serviceConnection);
    }

    @Nullable
    public z getSession() {
        l lVar = this.mClient;
        if (lVar == null) {
            this.mCustomTabsSession = null;
        } else if (this.mCustomTabsSession == null) {
            this.mCustomTabsSession = lVar.newSession(this.mCustomTabsCallback);
        }
        return this.mCustomTabsSession;
    }

    public boolean mayLaunchUrl(Uri uri, Bundle bundle, List<Bundle> list) {
        z session;
        if (this.mClient == null || (session = getSession()) == null) {
            return false;
        }
        return session.mayLaunchUrl(uri, bundle, list);
    }

    @Override // com.pichillilorenzo.flutter_inappwebview_android.chrome_custom_tabs.ServiceConnectionCallback
    public void onServiceConnected(l lVar) {
        this.mClient = lVar;
        lVar.getClass();
        try {
            lVar.f877a.warmup(0L);
        } catch (RemoteException unused) {
        }
        ConnectionCallback connectionCallback = this.mConnectionCallback;
        if (connectionCallback != null) {
            connectionCallback.onCustomTabsConnected();
        }
    }

    @Override // com.pichillilorenzo.flutter_inappwebview_android.chrome_custom_tabs.ServiceConnectionCallback
    public void onServiceDisconnected() {
        this.mClient = null;
        this.mCustomTabsSession = null;
        ConnectionCallback connectionCallback = this.mConnectionCallback;
        if (connectionCallback != null) {
            connectionCallback.onCustomTabsDisconnected();
        }
    }

    public void setConnectionCallback(ConnectionCallback connectionCallback) {
        this.mConnectionCallback = connectionCallback;
    }

    public void setCustomTabsCallback(d dVar) {
        this.mCustomTabsCallback = dVar;
    }

    public void unbindCustomTabsService(Activity activity) {
        v vVar = this.mConnection;
        if (vVar == null) {
            return;
        }
        activity.unbindService(vVar);
        this.mClient = null;
        this.mCustomTabsSession = null;
        this.mConnection = null;
    }

    public static void openCustomTab(Activity activity, r rVar, Uri uri, @Nullable Map<String, String> map, @Nullable Uri uri2, int i10) {
        openCustomTab(activity, rVar.f884a, uri, map, uri2, i10);
    }
}
