package com.snapchat.kit.sdk.creative.internal;

import X.0HH;
import X.3GY;
import X.43u;
import X.8n2;
import X.9EH;
import X.C0372REi;
import X.C0373REj;
import X.LxD;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Pair;
import com.bytedance.covode.number.Covode;
import com.snapchat.kit.sdk.core.metrics.model.KitPluginType;
import java.util.ArrayList;
import java.util.List;

public final class a {
    public final C0372REi LIZ;
    public final String LIZIZ;

    static {
        Covode.recordClassIndex(47660);
    }

    public static String LIZ(Context context) {
        List<ResolveInfo> queryIntentActivities;
        ApplicationInfo applicationInfo;
        CharSequence applicationLabel;
        String packageName = context.getPackageName();
        PackageManager packageManager = context.getPackageManager();
        Intent intent = new Intent("android.intent.action.MAIN", (Uri) null);
        intent.setPackage(packageName);
        intent.addCategory("android.intent.category.LAUNCHER");
        3GY.LIZ(101311);
        Pair LIZ2 = 3GY.LIZ((Object) null, packageManager, new Object[]{intent, 0}, 101311, "java.util.List", false, -2103477774, 2);
        if (((Boolean) LIZ2.first).booleanValue()) {
            queryIntentActivities = (List) LIZ2.second;
        } else {
            queryIntentActivities = packageManager.queryIntentActivities(intent, 0);
        }
        for (ResolveInfo next : queryIntentActivities) {
            if (next.activityInfo != null && !TextUtils.isEmpty(next.activityInfo.nonLocalizedLabel)) {
                return next.activityInfo.nonLocalizedLabel.toString();
            }
        }
        try {
            Context LIZ3 = 9EH.LJJIFFI.LIZ();
            if (8n2.LJIIIZ) {
                TextUtils.equals(packageName, LIZ3.getPackageName());
            }
            if (!8n2.LJIIIZ || !TextUtils.equals(packageName, LIZ3.getPackageName())) {
                applicationInfo = packageManager.getApplicationInfo(packageName, 0);
            } else {
                if (8n2.LIZJ == null) {
                    8n2.LIZJ = packageManager.getApplicationInfo(packageName, 0);
                }
                applicationInfo = 8n2.LIZJ;
            }
            if (!(applicationInfo == null || (applicationLabel = packageManager.getApplicationLabel(applicationInfo)) == null)) {
                return applicationLabel.toString();
            }
        } catch (PackageManager.NameNotFoundException unused) {
        }
        return null;
    }

    public a(String str, C0372REi rEi) {
        this.LIZIZ = str;
        this.LIZ = rEi;
    }

    public final Intent LIZ(Context context, KitPluginType kitPluginType, boolean z) {
        Intent intent = new Intent("android.intent.action.SEND");
        intent.setDataAndType(Uri.parse(0HH.LIZ("snapchat://%s?link=%s", new Object[]{this.LIZ.LIZ(), this.LIZIZ})), this.LIZ.LIZIZ());
        Uri LIZ2 = LxD.LIZ(context, this.LIZ.LIZJ());
        43u r1 = this.LIZ.LIZ;
        if (r1 != null) {
            Uri LIZ3 = LxD.LIZ(context, r1.LIZ);
            intent.putExtra("sticker", r1.LIZ(LIZ3, context).toString());
            ArrayList arrayList = new ArrayList();
            if (LIZ2 != null) {
                arrayList.add(LIZ2);
            }
            arrayList.add(LIZ3);
            if (arrayList.size() > 1) {
                intent.putParcelableArrayListExtra("android.intent.extra.STREAM", arrayList);
                intent.setAction("android.intent.action.SEND_MULTIPLE");
            } else if (!arrayList.isEmpty()) {
                intent.putExtra("android.intent.extra.STREAM", (Parcelable) arrayList.get(0));
            }
        } else if (LIZ2 != null) {
            intent.putExtra("android.intent.extra.STREAM", LIZ2);
        }
        String str = this.LIZ.LIZIZ;
        if (!TextUtils.isEmpty(str)) {
            intent.putExtra("attachmentUrl", str);
        }
        String str2 = this.LIZ.LIZJ;
        if (!TextUtils.isEmpty(str2)) {
            intent.putExtra("captionText", str2);
        }
        C0372REi rEi = this.LIZ;
        if (rEi instanceof C0373REj) {
            C0373REj rEj = (C0373REj) rEi;
            String str3 = rEj.LIZLLL;
            String str4 = rEj.LJ;
            if (!TextUtils.isEmpty(str3)) {
                intent.putExtra("lensUUID", str3);
            } else if (!TextUtils.isEmpty(str4)) {
                intent.putExtra("lensId", str4);
            }
            if (!TextUtils.isEmpty(str3) || !TextUtils.isEmpty(str4)) {
                String LIZLLL = rEj.LIZLLL();
                if (!TextUtils.isEmpty(LIZLLL)) {
                    intent.putExtra("lensLaunchData", LIZLLL);
                }
            }
        }
        if (kitPluginType != KitPluginType.NO_PLUGIN) {
            intent.putExtra("kitPluginType", kitPluginType.toString());
        }
        intent.putExtra("sdk_is_from_react_native_plugin", z);
        String LIZ4 = LIZ(context);
        if (!TextUtils.isEmpty(LIZ4)) {
            intent.putExtra("CLIENT_APP_NAME", LIZ4);
        }
        return intent;
    }
}
