package com.google.firebase.messaging;

import android.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import androidx.core.app.s0;
import androidx.media3.common.C;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: loaded from: classes2.dex */
public abstract class f {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final AtomicInteger f24168a = new AtomicInteger((int) SystemClock.elapsedRealtime());

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:10:0x0019  */
    /* JADX WARN: Removed duplicated region for block: B:13:0x0029  */
    /* JADX WARN: Removed duplicated region for block: B:161:0x01a1 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:97:0x01ac  */
    /* JADX WARN: Type inference failed for: r10v21 */
    /* JADX WARN: Type inference failed for: r10v22, types: [int] */
    /* JADX WARN: Type inference failed for: r10v46 */
    /* JADX WARN: Type inference failed for: r10v47 */
    /* JADX WARN: Type inference failed for: r10v48 */
    /* JADX WARN: Type inference failed for: r10v49 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static x1.a a(Context context, t tVar) {
        Bundle bundle;
        int identifier;
        Uri defaultUri;
        Integer numValueOf;
        int i10;
        ApplicationInfo applicationInfo;
        try {
            applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 128);
        } catch (PackageManager.NameNotFoundException e10) {
            e10.toString();
        }
        if (applicationInfo != null) {
            bundle = applicationInfo.metaData;
            if (bundle == null) {
                bundle = Bundle.EMPTY;
            }
        }
        String strC = tVar.c("gcm.n.android_channel_id");
        Long lValueOf = null;
        if (Build.VERSION.SDK_INT < 26) {
            strC = null;
        } else {
            try {
                if (context.getPackageManager().getApplicationInfo(context.getPackageName(), 0).targetSdkVersion >= 26) {
                    NotificationManager notificationManager = (NotificationManager) context.getSystemService(NotificationManager.class);
                    if (TextUtils.isEmpty(strC) || notificationManager.getNotificationChannel(strC) == null) {
                        strC = bundle.getString("com.google.firebase.messaging.default_notification_channel_id");
                        if (TextUtils.isEmpty(strC) || notificationManager.getNotificationChannel(strC) == null) {
                            if (notificationManager.getNotificationChannel("fcm_fallback_notification_channel") == null) {
                                int identifier2 = context.getResources().getIdentifier("fcm_fallback_notification_channel_label", "string", context.getPackageName());
                                notificationManager.createNotificationChannel(com.google.android.material.textfield.w.d(identifier2 == 0 ? "Misc" : context.getString(identifier2)));
                            }
                            strC = "fcm_fallback_notification_channel";
                        }
                    }
                }
            } catch (PackageManager.NameNotFoundException unused) {
            }
        }
        String packageName = context.getPackageName();
        Resources resources = context.getResources();
        PackageManager packageManager = context.getPackageManager();
        s0 s0Var = new s0(context, strC);
        String strC2 = tVar.c("gcm.n.title");
        if (TextUtils.isEmpty(strC2)) {
            strC2 = tVar.getLocalizedString(resources, packageName, "gcm.n.title");
        }
        if (!TextUtils.isEmpty(strC2)) {
            s0Var.setContentTitle(strC2);
        }
        String strC3 = tVar.c("gcm.n.body");
        if (TextUtils.isEmpty(strC3)) {
            strC3 = tVar.getLocalizedString(resources, packageName, "gcm.n.body");
        }
        if (!TextUtils.isEmpty(strC3)) {
            s0Var.setContentText(strC3);
            s0Var.setStyle(new androidx.core.app.l0().bigText(strC3));
        }
        String strC4 = tVar.c("gcm.n.icon");
        if (TextUtils.isEmpty(strC4) || (((identifier = resources.getIdentifier(strC4, "drawable", packageName)) == 0 || !b(resources, identifier)) && ((identifier = resources.getIdentifier(strC4, "mipmap", packageName)) == 0 || !b(resources, identifier)))) {
            int i11 = bundle.getInt("com.google.firebase.messaging.default_notification_icon", 0);
            if (i11 == 0 || !b(resources, i11)) {
                try {
                    i11 = packageManager.getApplicationInfo(packageName, 0).icon;
                } catch (PackageManager.NameNotFoundException e11) {
                    e11.toString();
                }
            }
            if (i11 == 0 || !b(resources, i11)) {
                i11 = R.drawable.sym_def_app_icon;
            }
            identifier = i11;
        }
        s0Var.Q.icon = identifier;
        String soundResourceName = tVar.getSoundResourceName();
        if (TextUtils.isEmpty(soundResourceName)) {
            defaultUri = null;
        } else if ("default".equals(soundResourceName) || resources.getIdentifier(soundResourceName, "raw", packageName) == 0) {
            defaultUri = RingtoneManager.getDefaultUri(2);
        } else {
            defaultUri = Uri.parse("android.resource://" + packageName + "/raw/" + soundResourceName);
        }
        if (defaultUri != null) {
            s0Var.setSound(defaultUri);
        }
        s0Var.setContentIntent(createContentIntent(context, tVar, packageName, packageManager));
        PendingIntent pendingIntentCreateDeleteIntent = createDeleteIntent(context, context, tVar);
        if (pendingIntentCreateDeleteIntent != null) {
            s0Var.setDeleteIntent(pendingIntentCreateDeleteIntent);
        }
        String strC5 = tVar.c("gcm.n.color");
        if (TextUtils.isEmpty(strC5)) {
            i10 = bundle.getInt("com.google.firebase.messaging.default_notification_color", 0);
            if (i10 == 0) {
                try {
                    Object obj = g0.d.f26125a;
                    numValueOf = Integer.valueOf(context.getColor(i10));
                } catch (Resources.NotFoundException unused2) {
                    numValueOf = null;
                }
            } else {
                numValueOf = null;
            }
        } else {
            try {
                numValueOf = Integer.valueOf(Color.parseColor(strC5));
            } catch (IllegalArgumentException unused3) {
                i10 = bundle.getInt("com.google.firebase.messaging.default_notification_color", 0);
                if (i10 == 0) {
                }
            }
        }
        if (numValueOf != null) {
            s0Var.D = numValueOf.intValue();
        }
        s0Var.b(16, !tVar.a("gcm.n.sticky"));
        s0Var.f1670y = tVar.a("gcm.n.local_only");
        String strC6 = tVar.c("gcm.n.ticker");
        if (strC6 != null) {
            s0Var.setTicker(strC6);
        }
        Integer notificationPriority = tVar.getNotificationPriority();
        if (notificationPriority != null) {
            s0Var.f1658m = notificationPriority.intValue();
        }
        Integer numB = tVar.b("gcm.n.visibility");
        if (numB == null || numB.intValue() < -1 || numB.intValue() > 1) {
            numB = null;
        }
        if (numB != null) {
            s0Var.E = numB.intValue();
        }
        Integer notificationCount = tVar.getNotificationCount();
        if (notificationCount != null) {
            s0Var.f1657l = notificationCount.intValue();
        }
        String strC7 = tVar.c("gcm.n.event_time");
        if (!TextUtils.isEmpty(strC7)) {
            try {
                lValueOf = Long.valueOf(Long.parseLong(strC7));
            } catch (NumberFormatException unused4) {
                t.f("gcm.n.event_time");
            }
        }
        if (lValueOf != null) {
            s0Var.f1659n = true;
            s0Var.Q.when = lValueOf.longValue();
        }
        long[] vibrateTimings = tVar.getVibrateTimings();
        if (vibrateTimings != null) {
            s0Var.setVibrate(vibrateTimings);
        }
        int[] lightSettings = tVar.getLightSettings();
        if (lightSettings != null) {
            int i12 = lightSettings[0];
            int i13 = lightSettings[1];
            int i14 = lightSettings[2];
            Notification notification = s0Var.Q;
            notification.ledARGB = i12;
            notification.ledOnMS = i13;
            notification.ledOffMS = i14;
            notification.flags = ((i13 == 0 || i14 == 0) ? 0 : 1) | (notification.flags & (-2));
        }
        boolean zA = tVar.a("gcm.n.default_sound");
        ?? r10 = zA;
        if (tVar.a("gcm.n.default_vibrate_timings")) {
            r10 = (zA ? 1 : 0) | 2;
        }
        ?? r102 = r10;
        if (tVar.a("gcm.n.default_light_settings")) {
            r102 = (r10 == true ? 1 : 0) | 4;
        }
        Notification notification2 = s0Var.Q;
        notification2.defaults = r102;
        if ((r102 & 4) != 0) {
            notification2.flags |= 1;
        }
        String strC8 = tVar.c("gcm.n.tag");
        if (TextUtils.isEmpty(strC8)) {
            strC8 = "FCM-Notification:" + SystemClock.uptimeMillis();
        }
        return new x1.a(s0Var, strC8, 0);
    }

    public static boolean b(Resources resources, int i10) {
        if (Build.VERSION.SDK_INT != 26) {
            return true;
        }
        try {
            return !com.google.android.material.textfield.w.w(resources.getDrawable(i10, null));
        } catch (Resources.NotFoundException unused) {
            return false;
        }
    }

    @Nullable
    private static PendingIntent createContentIntent(Context context, t tVar, String str, PackageManager packageManager) {
        Intent launchIntentForPackage;
        String strC = tVar.c("gcm.n.click_action");
        if (TextUtils.isEmpty(strC)) {
            Uri link = tVar.getLink();
            if (link != null) {
                launchIntentForPackage = new Intent("android.intent.action.VIEW");
                launchIntentForPackage.setPackage(str);
                launchIntentForPackage.setData(link);
            } else {
                launchIntentForPackage = packageManager.getLaunchIntentForPackage(str);
            }
        } else {
            launchIntentForPackage = new Intent(strC);
            launchIntentForPackage.setPackage(str);
            launchIntentForPackage.setFlags(268435456);
        }
        if (launchIntentForPackage == null) {
            return null;
        }
        launchIntentForPackage.addFlags(C.BUFFER_FLAG_NOT_DEPENDED_ON);
        Bundle bundle = tVar.f24259a;
        Bundle bundle2 = new Bundle(bundle);
        for (String str2 : bundle.keySet()) {
            if (str2.startsWith("google.c.") || str2.startsWith("gcm.n.") || str2.startsWith("gcm.notification.")) {
                bundle2.remove(str2);
            }
        }
        launchIntentForPackage.putExtras(bundle2);
        if (tVar.a("google.c.a.e")) {
            launchIntentForPackage.putExtra("gcm.n.analytics_data", tVar.e());
        }
        return PendingIntent.getActivity(context, f24168a.incrementAndGet(), launchIntentForPackage, 1140850688);
    }

    @Nullable
    private static PendingIntent createDeleteIntent(Context context, Context context2, t tVar) {
        if (!tVar.a("google.c.a.e")) {
            return null;
        }
        return PendingIntent.getBroadcast(context, f24168a.incrementAndGet(), new Intent("com.google.android.c2dm.intent.RECEIVE").setPackage(context2.getPackageName()).putExtra("wrapped_intent", new Intent("com.google.firebase.messaging.NOTIFICATION_DISMISS").putExtras(tVar.e())), 1140850688);
    }
}
