package com.bytedance.sdk.openadsdk.core.fm;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.text.TextUtils;
import androidx.browser.customtabs.CustomTabsService;
import androidx.browser.customtabs.r;
import com.bytedance.sdk.component.utils.wey;
import java.util.ArrayList;
import java.util.List;

/* JADX INFO: loaded from: classes2.dex */
public class fm {
    private static String fm;
    private static Boolean ro;

    public static String fm(Context context) {
        String str = fm;
        if (str != null) {
            return str;
        }
        try {
            PackageManager packageManager = context.getPackageManager();
            Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.example.com"));
            ResolveInfo resolveInfoResolveActivity = packageManager.resolveActivity(intent, 0);
            String str2 = resolveInfoResolveActivity != null ? resolveInfoResolveActivity.activityInfo.packageName : null;
            List<ResolveInfo> listQueryIntentActivities = packageManager.queryIntentActivities(intent, 0);
            ArrayList arrayList = new ArrayList();
            for (ResolveInfo resolveInfo : listQueryIntentActivities) {
                Intent intent2 = new Intent();
                intent2.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
                intent2.setPackage(resolveInfo.activityInfo.packageName);
                if (packageManager.resolveService(intent2, 0) != null) {
                    arrayList.add(resolveInfo.activityInfo.packageName);
                }
            }
            if (arrayList.isEmpty()) {
                fm = null;
            } else if (TextUtils.isEmpty(str2) || fm(context, intent) || !arrayList.contains(str2)) {
                fm = (String) arrayList.get(0);
            } else {
                fm = str2;
            }
        } catch (Throwable th) {
            wey.lb("CustomTabsHelper", th.getMessage());
        }
        return fm;
    }

    public static int ro(Context context) {
        return !TextUtils.isEmpty(fm(context)) ? 1 : 0;
    }

    private static boolean fm(Context context, Intent intent) {
        try {
            List<ResolveInfo> listQueryIntentActivities = context.getPackageManager().queryIntentActivities(intent, 64);
            if (listQueryIntentActivities.size() == 0) {
                return false;
            }
            for (ResolveInfo resolveInfo : listQueryIntentActivities) {
                IntentFilter intentFilter = resolveInfo.filter;
                if (intentFilter != null && intentFilter.countDataAuthorities() != 0 && intentFilter.countDataPaths() != 0 && resolveInfo.activityInfo != null) {
                    return true;
                }
            }
        } catch (RuntimeException unused) {
        }
        return false;
    }

    public static int fm() {
        Boolean bool = ro;
        return (bool != null && bool.booleanValue()) ? 1 : 0;
    }

    public static void fm(Context context, String str, r rVar, Uri uri) {
        rVar.f884a.setPackage(str);
        rVar.a(context, uri);
    }
}
