package com.google.firebase.messaging;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import com.google.firebase.messaging.reporting.MessagingClientEvent$Event;
import com.google.firebase.messaging.reporting.MessagingClientEvent$MessageType;
import com.google.firebase.messaging.reporting.MessagingClientEvent$SDKPlatform;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

/* JADX INFO: loaded from: classes2.dex */
public abstract class r {
    public static boolean a() {
        Context context;
        SharedPreferences sharedPreferences;
        ApplicationInfo applicationInfo;
        Bundle bundle;
        try {
            ua.g.b();
            ua.g gVarB = ua.g.b();
            gVarB.a();
            context = gVarB.f33401a;
            sharedPreferences = context.getSharedPreferences("com.google.firebase.messaging", 0);
        } catch (PackageManager.NameNotFoundException | IllegalStateException unused) {
        }
        if (sharedPreferences.contains("export_to_big_query")) {
            return sharedPreferences.getBoolean("export_to_big_query", false);
        }
        PackageManager packageManager = context.getPackageManager();
        if (packageManager != null && (applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 128)) != null && (bundle = applicationInfo.metaData) != null && bundle.containsKey("delivery_metrics_exported_to_big_query_enabled")) {
            return applicationInfo.metaData.getBoolean("delivery_metrics_exported_to_big_query_enabled", false);
        }
        return false;
    }

    public static void b(Intent intent) {
        if (d(intent)) {
            c(intent.getExtras(), "_nr");
        }
        if (intent == null || FirebaseMessagingService.ACTION_DIRECT_BOOT_REMOTE_INTENT.equals(intent.getAction()) || !a()) {
            return;
        }
        logToFirelog(MessagingClientEvent$Event.MESSAGE_DELIVERED, intent, FirebaseMessaging.getTransportFactory());
    }

    public static void c(Bundle bundle, String str) {
        try {
            ua.g.b();
            if (bundle == null) {
                bundle = new Bundle();
            }
            Bundle bundle2 = new Bundle();
            String composerId = getComposerId(bundle);
            if (composerId != null) {
                bundle2.putString("_nmid", composerId);
            }
            String composerLabel = getComposerLabel(bundle);
            if (composerLabel != null) {
                bundle2.putString("_nmn", composerLabel);
            }
            String messageLabel = getMessageLabel(bundle);
            if (!TextUtils.isEmpty(messageLabel)) {
                bundle2.putString("label", messageLabel);
            }
            String messageChannel = getMessageChannel(bundle);
            if (!TextUtils.isEmpty(messageChannel)) {
                bundle2.putString("message_channel", messageChannel);
            }
            String topic = getTopic(bundle);
            if (topic != null) {
                bundle2.putString("_nt", topic);
            }
            String messageTime = getMessageTime(bundle);
            if (messageTime != null) {
                try {
                    bundle2.putInt("_nmt", Integer.parseInt(messageTime));
                } catch (NumberFormatException unused) {
                }
            }
            String useDeviceTime = getUseDeviceTime(bundle);
            if (useDeviceTime != null) {
                try {
                    bundle2.putInt("_ndt", Integer.parseInt(useDeviceTime));
                } catch (NumberFormatException unused2) {
                }
            }
            String str2 = t.d(bundle) ? Constants$ScionAnalytics$MessageType.DISPLAY_NOTIFICATION : "data";
            if ("_nr".equals(str) || "_nf".equals(str)) {
                bundle2.putString("_nmc", str2);
            }
            if (Log.isLoggable("FirebaseMessaging", 3)) {
                bundle2.toString();
            }
            ua.g gVarB = ua.g.b();
            gVarB.a();
            wa.d dVar = (wa.d) gVarB.f33404d.get(wa.d.class);
            if (dVar != null) {
                ((wa.e) dVar).logEvent("fcm", str, bundle2);
            }
        } catch (IllegalStateException unused3) {
        }
    }

    public static boolean d(Intent intent) {
        Bundle extras;
        if (intent == null || FirebaseMessagingService.ACTION_DIRECT_BOOT_REMOTE_INTENT.equals(intent.getAction()) || (extras = intent.getExtras()) == null) {
            return false;
        }
        return "1".equals(extras.getString("google.c.a.e"));
    }

    @Nullable
    public static String getCollapseKey(Bundle bundle) {
        return bundle.getString("collapse_key");
    }

    @Nullable
    public static String getComposerId(Bundle bundle) {
        return bundle.getString("google.c.a.c_id");
    }

    @Nullable
    public static String getComposerLabel(Bundle bundle) {
        return bundle.getString("google.c.a.c_l");
    }

    @Nullable
    public static String getMessageChannel(Bundle bundle) {
        return bundle.getString("google.c.a.m_c");
    }

    @Nullable
    public static String getMessageId(Bundle bundle) {
        String string = bundle.getString("google.message_id");
        return string == null ? bundle.getString("message_id") : string;
    }

    @Nullable
    public static String getMessageLabel(Bundle bundle) {
        return bundle.getString("google.c.a.m_l");
    }

    @Nullable
    public static String getMessageTime(Bundle bundle) {
        return bundle.getString("google.c.a.ts");
    }

    @Nullable
    public static long getProjectNumber(Bundle bundle) {
        if (bundle.containsKey("google.c.sender.id")) {
            try {
                return Long.parseLong(bundle.getString("google.c.sender.id"));
            } catch (NumberFormatException unused) {
            }
        }
        ua.g gVarB = ua.g.b();
        gVarB.a();
        ua.i iVar = gVarB.f33403c;
        String gcmSenderId = iVar.getGcmSenderId();
        if (gcmSenderId != null) {
            try {
                return Long.parseLong(gcmSenderId);
            } catch (NumberFormatException unused2) {
            }
        }
        gVarB.a();
        String str = iVar.f33416b;
        try {
            if (!str.startsWith("1:")) {
                return Long.parseLong(str);
            }
            String[] strArrSplit = str.split(":");
            if (strArrSplit.length < 2) {
                return 0L;
            }
            String str2 = strArrSplit[1];
            if (str2.isEmpty()) {
                return 0L;
            }
            return Long.parseLong(str2);
        } catch (NumberFormatException unused3) {
            return 0L;
        }
    }

    @Nullable
    public static String getTopic(Bundle bundle) {
        String string = bundle.getString("from");
        if (string == null || !string.startsWith("/topics/")) {
            return null;
        }
        return string;
    }

    @Nullable
    public static String getUseDeviceTime(Bundle bundle) {
        if (bundle.containsKey("google.c.a.udt")) {
            return bundle.getString("google.c.a.udt");
        }
        return null;
    }

    /* JADX WARN: Removed duplicated region for block: B:34:0x00b1  */
    /* JADX WARN: Removed duplicated region for block: B:35:0x00b3  */
    /* JADX WARN: Removed duplicated region for block: B:40:0x00bf  */
    /* JADX WARN: Removed duplicated region for block: B:41:0x00c1  */
    /* JADX WARN: Removed duplicated region for block: B:45:0x00cd  */
    /* JADX WARN: Removed duplicated region for block: B:48:0x00d5  */
    /* JADX WARN: Removed duplicated region for block: B:51:0x00dd  */
    /* JADX WARN: Removed duplicated region for block: B:54:0x00e5  */
    /* JADX WARN: Removed duplicated region for block: B:57:0x00ed  */
    /* JADX WARN: Removed duplicated region for block: B:60:0x00f9  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private static void logToFirelog(MessagingClientEvent$Event messagingClientEvent$Event, Intent intent, @Nullable t6.g gVar) {
        int iIntValue;
        char c6;
        String messageId;
        String topic;
        String collapseKey;
        String messageLabel;
        String composerLabel;
        long projectNumber;
        mb.b bVarA;
        if (gVar == null) {
            return;
        }
        if (intent == null) {
            bVarA = null;
        } else {
            Bundle extras = intent.getExtras();
            if (extras == null) {
                extras = Bundle.EMPTY;
            }
            int i10 = mb.b.f30579n;
            mb.a aVar = new mb.a();
            Object obj = extras.get("google.ttl");
            int i11 = 0;
            if (obj instanceof Integer) {
                iIntValue = ((Integer) obj).intValue();
            } else if (obj instanceof String) {
                try {
                    iIntValue = Integer.parseInt((String) obj);
                } catch (NumberFormatException unused) {
                    Objects.toString(obj);
                    iIntValue = 0;
                }
            } else {
                iIntValue = 0;
            }
            aVar.f30574i = iIntValue;
            aVar.f30576k = messagingClientEvent$Event;
            String string = extras.getString("google.to");
            if (TextUtils.isEmpty(string)) {
                try {
                    ua.g gVarB = ua.g.b();
                    Object obj2 = com.google.firebase.installations.a.f24091m;
                    gVarB.a();
                    string = (String) w8.k.a(((com.google.firebase.installations.a) gVarB.f33404d.get(hb.d.class)).c());
                } catch (InterruptedException | ExecutionException e10) {
                    throw new RuntimeException(e10);
                }
            }
            aVar.f30568c = string;
            ua.g gVarB2 = ua.g.b();
            gVarB2.a();
            aVar.f30571f = gVarB2.f33401a.getPackageName();
            aVar.f30570e = MessagingClientEvent$SDKPlatform.ANDROID;
            aVar.f30569d = t.d(extras) ? MessagingClientEvent$MessageType.DISPLAY_NOTIFICATION : MessagingClientEvent$MessageType.DATA_MESSAGE;
            String string2 = extras.getString("google.delivered_priority");
            if (string2 != null) {
                c6 = !"high".equals(string2) ? (char) 1 : "normal".equals(string2) ? (char) 2 : (char) 0;
                if (c6 == 2) {
                    i11 = 5;
                } else if (c6 == 1) {
                    i11 = 10;
                }
                aVar.f30573h = i11;
                messageId = getMessageId(extras);
                if (messageId != null) {
                    aVar.f30567b = messageId;
                }
                topic = getTopic(extras);
                if (topic != null) {
                    aVar.f30575j = topic;
                }
                collapseKey = getCollapseKey(extras);
                if (collapseKey != null) {
                    aVar.f30572g = collapseKey;
                }
                messageLabel = getMessageLabel(extras);
                if (messageLabel != null) {
                    aVar.f30577l = messageLabel;
                }
                composerLabel = getComposerLabel(extras);
                if (composerLabel != null) {
                    aVar.f30578m = composerLabel;
                }
                projectNumber = getProjectNumber(extras);
                if (projectNumber > 0) {
                    aVar.f30566a = projectNumber;
                }
                bVarA = aVar.a();
            } else {
                if (!"1".equals(extras.getString("google.priority_reduced"))) {
                    string2 = extras.getString("google.priority");
                    if (!"high".equals(string2)) {
                    }
                    if (c6 == 2) {
                    }
                    aVar.f30573h = i11;
                    messageId = getMessageId(extras);
                    if (messageId != null) {
                    }
                    topic = getTopic(extras);
                    if (topic != null) {
                    }
                    collapseKey = getCollapseKey(extras);
                    if (collapseKey != null) {
                    }
                    messageLabel = getMessageLabel(extras);
                    if (messageLabel != null) {
                    }
                    composerLabel = getComposerLabel(extras);
                    if (composerLabel != null) {
                    }
                    projectNumber = getProjectNumber(extras);
                    if (projectNumber > 0) {
                    }
                    bVarA = aVar.a();
                }
                if (c6 == 2) {
                }
                aVar.f30573h = i11;
                messageId = getMessageId(extras);
                if (messageId != null) {
                }
                topic = getTopic(extras);
                if (topic != null) {
                }
                collapseKey = getCollapseKey(extras);
                if (collapseKey != null) {
                }
                messageLabel = getMessageLabel(extras);
                if (messageLabel != null) {
                }
                composerLabel = getComposerLabel(extras);
                if (composerLabel != null) {
                }
                projectNumber = getProjectNumber(extras);
                if (projectNumber > 0) {
                }
                bVarA = aVar.a();
            }
        }
        if (bVarA == null) {
            return;
        }
        try {
            t6.e eVarWithProductId = t6.e.withProductId(Integer.valueOf(intent.getIntExtra("google.product_id", 111881503)));
            v6.j jVarA = ((v6.w) gVar).a("FCM_CLIENT_EVENT_LOGGING", new t6.c("proto"), new oa.a(16));
            int i12 = mb.c.f30593b;
            xyz.stream.utils.f0 f0Var = new xyz.stream.utils.f0(20);
            f0Var.f36136b = bVarA;
            jVarA.d(t6.d.ofData(new mb.c((mb.b) f0Var.f36136b), eVarWithProductId));
        } catch (RuntimeException unused2) {
        }
    }
}
