package com.google.firebase.messaging;

import X.2n6;
import X.C25148e1;
import X.C25158e2;
import X.C25298eG;
import X.C25348eL;
import X.C34658tM;
import X.C37748yL;
import X.C38168z1;
import X.C38348zJ;
import X.C38638zm;
import X.C387890b;
import X.C387990c;
import X.C388090d;
import X.C388290f;
import X.C388490h;
import X.C388690j;
import X.C389490r;
import X.C394992u;
import X.C395192w;
import X.R2h;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import com.bytedance.covode.number.Covode;
import com.bytedance.helios.sdk.utils.ServiceLifecycle;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class FirebaseMessagingService extends EnhancedIntentService {
    public static final Queue<String> recentlyReceivedMessageIds = new ArrayDeque(10);

    public void onDeletedMessages() {
    }

    public void onMessageReceived(RemoteMessage remoteMessage) {
    }

    public void onMessageSent(String str) {
    }

    public void onNewToken(String str) {
    }

    public void onSendError(String str, Exception exc) {
    }

    public int onStartCommand(Intent intent, int i, int i2) {
        ServiceLifecycle.onStartCommand(this, intent, i, i2);
        return super.onStartCommand(intent, i, i2);
    }

    static {
        Covode.recordClassIndex(45327);
    }

    public long getAckTimeoutMillis() {
        return TimeUnit.SECONDS.toMillis(1);
    }

    public static Bundle INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras(Intent intent) {
        try {
            return intent.getExtras();
        } catch (Exception unused) {
            return null;
        }
    }

    private String getMessageId(Intent intent) {
        String INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra = INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra(intent, "google.message_id");
        if (INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra == null) {
            return INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra(intent, "message_id");
        }
        return INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra;
    }

    public Intent getStartCommandIntent(Intent intent) {
        return C34658tM.LIZ().LIZJ.poll();
    }

    private void handleMessageIntent(Intent intent) {
        String INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra = INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra(intent, "google.message_id");
        C38348zJ<Void> ackMessage = ackMessage(INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra);
        if (!alreadyReceivedMessage(INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra)) {
            passMessageIntentToSdk(intent);
        }
        try {
            C38168z1.LIZ(ackMessage, getAckTimeoutMillis(), TimeUnit.MILLISECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException unused) {
        }
    }

    public boolean handleIntentOnMainThread(Intent intent) {
        if (!"com.google.firebase.messaging.NOTIFICATION_OPEN".equals(intent.getAction())) {
            return false;
        }
        handleNotificationOpen(intent);
        return true;
    }

    private C38348zJ<Void> ackMessage(String str) {
        if (TextUtils.isEmpty(str)) {
            return C38168z1.LIZ(null);
        }
        Bundle bundle = new Bundle();
        bundle.putString("google.message_id", str);
        C387890b LIZ = C387890b.LIZ((Context) this);
        return LIZ.LIZ(new C389490r(LIZ.LIZ(), bundle));
    }

    private boolean alreadyReceivedMessage(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        Queue<String> queue = recentlyReceivedMessageIds;
        if (!queue.contains(str)) {
            if (queue.size() >= 10) {
                queue.remove();
            }
            queue.add(str);
            return false;
        } else if (!Log.isLoggable("FirebaseMessaging", 3)) {
            return true;
        } else {
            String valueOf = String.valueOf(str);
            if (valueOf.length() != 0) {
                "Received duplicate message: ".concat(valueOf);
                return true;
            }
            new String("Received duplicate message: ");
            return true;
        }
    }

    private void dispatchMessage(Intent intent) {
        Bundle INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras = INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras(intent);
        if (INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras == null) {
            INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras = new Bundle();
        }
        INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras.remove("androidx.content.wakelockid");
        if (C395192w.LIZ(INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras)) {
            C395192w r3 = new C395192w(INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras);
            C37748yL r2 = new C37748yL("Firebase-Messaging-Network-Io");
            C25148e1 LIZ = C25158e2.LIZ(C25298eG.FIXED);
            LIZ.LIZJ = 1;
            LIZ.LJI = r2;
            ExecutorService LIZ2 = C25348eL.LIZ(LIZ.LIZ());
            try {
                if (!new C394992u(this, r3, LIZ2).LIZ()) {
                    LIZ2.shutdown();
                    if (C388690j.LIZ(intent)) {
                        C388690j.LIZ("_nf", intent);
                    }
                } else {
                    return;
                }
            } finally {
                LIZ2.shutdown();
            }
        }
        onMessageReceived(new RemoteMessage(INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras));
    }

    private void handleNotificationOpen(Intent intent) {
        C388090d r5;
        PendingIntent pendingIntent = (PendingIntent) intent.getParcelableExtra("pending_intent");
        if (pendingIntent != null) {
            try {
                pendingIntent.send();
            } catch (PendingIntent.CanceledException unused) {
            }
        }
        if (C388690j.LIZ(intent)) {
            if ("1".equals(C388690j.LIZ(intent, "google.c.a.tc")) && (r5 = (C388090d) C38638zm.LIZLLL().LIZ(C388090d.class)) != null) {
                String LIZ = C388690j.LIZ(intent, "google.c.a.c_id");
                r5.LIZ("fcm", "_ln", (Object) LIZ);
                Bundle bundle = new Bundle();
                bundle.putString("source", "Firebase");
                bundle.putString("medium", "notification");
                bundle.putString("campaign", LIZ);
                r5.LIZ("fcm", "_cmp", bundle);
            }
            C388690j.LIZ("_no", intent);
        }
    }

    public void handleIntent(Intent intent) {
        String action = intent.getAction();
        if ("com.google.android.c2dm.intent.RECEIVE".equals(action) || "com.google.firebase.messaging.RECEIVE_DIRECT_BOOT".equals(action)) {
            handleMessageIntent(intent);
        } else if ("com.google.firebase.messaging.NOTIFICATION_DISMISS".equals(action)) {
            if (C388690j.LIZ(intent)) {
                C388690j.LIZ("_nd", intent);
            }
        } else if ("com.google.firebase.messaging.NEW_TOKEN".equals(action)) {
            onNewToken(INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra(intent, "token"));
        } else {
            String valueOf = String.valueOf(intent.getAction());
            if (valueOf.length() != 0) {
                "Unknown intent action: ".concat(valueOf);
            } else {
                new String("Unknown intent action: ");
            }
        }
    }

    private void passMessageIntentToSdk(Intent intent) {
        C388290f r3;
        String INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra = INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra(intent, "message_type");
        if (INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra == null) {
            INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra = "gcm";
        }
        INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra.hashCode();
        switch (INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra.hashCode()) {
            case -2062414158:
                if (INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra.equals("deleted_messages")) {
                    onDeletedMessages();
                    return;
                }
                break;
            case 102161:
                if (INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra.equals("gcm")) {
                    if (C388690j.LIZ(intent)) {
                        C388690j.LIZ(intent, (C388490h<String>) null);
                    }
                    if (intent != null && !C388690j.LIZIZ(intent) && C388690j.LIZ() && (r3 = FirebaseMessaging.LIZIZ) != null) {
                        C388690j.LIZ(intent, (C388490h<String>) r3.LIZ("FCM_CLIENT_EVENT_LOGGING", R2h.LIZ("json"), C387990c.LIZ));
                    }
                    dispatchMessage(intent);
                    return;
                }
                break;
            case 814694033:
                if (INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra.equals("send_error")) {
                    onSendError(getMessageId(intent), new 2n6(INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra(intent, "error")));
                    return;
                }
                break;
            case 814800675:
                if (INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra.equals("send_event")) {
                    onMessageSent(INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra(intent, "google.message_id"));
                    return;
                }
                break;
        }
        String valueOf = String.valueOf(INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra);
        if (valueOf.length() != 0) {
            "Received message with unknown type: ".concat(valueOf);
        } else {
            new String("Received message with unknown type: ");
        }
    }

    public static String INVOKEVIRTUAL_com_google_firebase_messaging_FirebaseMessagingService_com_ss_android_ugc_aweme_lancet_IntentLancet_getStringExtra(Intent intent, String str) {
        try {
            return intent.getStringExtra(str);
        } catch (Exception unused) {
            return null;
        }
    }
}
