package com.google.firebase.messaging;

import X.C37408xn;
import X.C37418xo;
import X.C37428xp;
import X.C37438xq;
import X.C37448xr;
import X.C37468xt;
import X.C37748yL;
import X.C38168z1;
import X.C38348zJ;
import X.C38478zW;
import X.C38568zf;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import com.bytedance.covode.number.Covode;
import java.util.concurrent.ExecutorService;

public abstract class EnhancedIntentService extends Service {
    public Binder binder;
    public final ExecutorService executor = C37468xt.LIZ.LIZ(new C37748yL("Firebase-Messaging-Intent-Handle"));
    public int lastStartId;
    public final Object lock = new Object();
    public int runningTasks = 0;

    static {
        Covode.recordClassIndex(45321);
    }

    public Intent getStartCommandIntent(Intent intent) {
        return intent;
    }

    public abstract void handleIntent(Intent intent);

    public boolean handleIntentOnMainThread(Intent intent) {
        return false;
    }

    public void onDestroy() {
        this.executor.shutdown();
        super.onDestroy();
    }

    public boolean stopSelfResultHook(int i) {
        return stopSelfResult(i);
    }

    public final synchronized IBinder onBind(Intent intent) {
        if (this.binder == null) {
            this.binder = new C37438xq(new C37408xn() {
                static {
                    Covode.recordClassIndex(45322);
                }

                public final C38348zJ<Void> LIZ(Intent intent) {
                    return EnhancedIntentService.this.processIntent(intent);
                }
            });
        }
        return this.binder;
    }

    public C38348zJ<Void> processIntent(Intent intent) {
        if (handleIntentOnMainThread(intent)) {
            return C38168z1.LIZ(null);
        }
        C38478zW r2 = new C38478zW();
        this.executor.execute(new C37418xo(this, intent, r2));
        return r2.LIZ;
    }

    /* JADX WARNING: Code restructure failed: missing block: B:19:0x003a, code lost:
        if (r3.LJII == 1) goto L_0x003c;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void finishTask(android.content.Intent r18) {
        /*
            r17 = this;
            r2 = 1
            r1 = r18
            if (r1 == 0) goto L_0x0083
            java.lang.Object r6 = X.C37298xc.LIZ
            monitor-enter(r6)
            X.8xb r0 = X.C37298xc.LIZIZ     // Catch:{ all -> 0x0080 }
            if (r0 == 0) goto L_0x007e
            java.lang.String r0 = "com.google.firebase.iid.WakeLockHolder.wakefulintent"
            r5 = 0
            boolean r0 = r1.getBooleanExtra(r0, r5)     // Catch:{ all -> 0x0080 }
            if (r0 == 0) goto L_0x007e
            X.C37298xc.LIZ((android.content.Intent) r1, (boolean) r5)     // Catch:{ all -> 0x0080 }
            X.8xb r3 = X.C37298xc.LIZIZ     // Catch:{ all -> 0x0080 }
            java.util.concurrent.atomic.AtomicInteger r0 = r3.LJIIIIZZ     // Catch:{ all -> 0x0080 }
            r0.decrementAndGet()     // Catch:{ all -> 0x0080 }
            java.lang.String r12 = r3.LIZJ()     // Catch:{ all -> 0x0080 }
            java.lang.Object r1 = r3.LIZ     // Catch:{ all -> 0x0080 }
            monitor-enter(r1)     // Catch:{ all -> 0x0080 }
            boolean r0 = r3.LJFF     // Catch:{ all -> 0x0078 }
            if (r0 == 0) goto L_0x0034
            java.util.Map<java.lang.String, java.lang.Integer[]> r0 = r3.LJI     // Catch:{ all -> 0x0078 }
            java.lang.Object r4 = r0.get(r12)     // Catch:{ all -> 0x0078 }
            java.lang.Integer[] r4 = (java.lang.Integer[]) r4     // Catch:{ all -> 0x0078 }
            if (r4 != 0) goto L_0x005c
        L_0x0034:
            boolean r0 = r3.LJFF     // Catch:{ all -> 0x0078 }
            if (r0 != 0) goto L_0x005a
            int r0 = r3.LJII     // Catch:{ all -> 0x0078 }
            if (r0 != r2) goto L_0x005a
        L_0x003c:
            X.8sd r7 = X.C34208sd.LIZ     // Catch:{ all -> 0x0078 }
            android.content.Context r8 = r3.LJ     // Catch:{ all -> 0x0078 }
            android.os.PowerManager$WakeLock r0 = r3.LIZIZ     // Catch:{ all -> 0x0078 }
            java.lang.String r9 = X.C37148xN.LIZ(r0, r12)     // Catch:{ all -> 0x0078 }
            java.lang.String r11 = r3.LIZLLL     // Catch:{ all -> 0x0078 }
            int r13 = r3.LIZJ     // Catch:{ all -> 0x0078 }
            java.util.List r14 = r3.LIZ()     // Catch:{ all -> 0x0078 }
            r10 = 8
            r15 = 0
            r7.LIZ(r8, r9, r10, r11, r12, r13, r14, r15)     // Catch:{ all -> 0x0078 }
            int r0 = r3.LJII     // Catch:{ all -> 0x0078 }
            int r0 = r0 - r2
            r3.LJII = r0     // Catch:{ all -> 0x0078 }
        L_0x005a:
            monitor-exit(r1)     // Catch:{ all -> 0x0078 }
            goto L_0x007b
        L_0x005c:
            r0 = r4[r5]     // Catch:{ all -> 0x0078 }
            int r0 = r0.intValue()     // Catch:{ all -> 0x0078 }
            if (r0 != r2) goto L_0x006a
            java.util.Map<java.lang.String, java.lang.Integer[]> r0 = r3.LJI     // Catch:{ all -> 0x0078 }
            r0.remove(r12)     // Catch:{ all -> 0x0078 }
            goto L_0x003c
        L_0x006a:
            r0 = r4[r5]     // Catch:{ all -> 0x0078 }
            int r0 = r0.intValue()     // Catch:{ all -> 0x0078 }
            int r0 = r0 - r2
            java.lang.Integer r0 = java.lang.Integer.valueOf(r0)     // Catch:{ all -> 0x0078 }
            r4[r5] = r0     // Catch:{ all -> 0x0078 }
            goto L_0x0034
        L_0x0078:
            r0 = move-exception
            monitor-exit(r1)     // Catch:{ all -> 0x0080 }
            throw r0     // Catch:{ all -> 0x0080 }
        L_0x007b:
            r3.LIZIZ()     // Catch:{ all -> 0x0080 }
        L_0x007e:
            monitor-exit(r6)     // Catch:{ all -> 0x0080 }
            goto L_0x0083
        L_0x0080:
            r0 = move-exception
            monitor-exit(r6)
            throw r0
        L_0x0083:
            r3 = r17
            java.lang.Object r1 = r3.lock
            monitor-enter(r1)
            int r0 = r3.runningTasks     // Catch:{ all -> 0x0096 }
            int r0 = r0 - r2
            r3.runningTasks = r0     // Catch:{ all -> 0x0096 }
            if (r0 != 0) goto L_0x0094
            int r0 = r3.lastStartId     // Catch:{ all -> 0x0096 }
            r3.stopSelfResultHook(r0)     // Catch:{ all -> 0x0096 }
        L_0x0094:
            monitor-exit(r1)     // Catch:{ all -> 0x0096 }
            return
        L_0x0096:
            r0 = move-exception
            monitor-exit(r1)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.google.firebase.messaging.EnhancedIntentService.finishTask(android.content.Intent):void");
    }

    public final /* synthetic */ void lambda$onStartCommand$1$EnhancedIntentService(Intent intent, C38348zJ r2) {
        finishTask(intent);
    }

    public final /* synthetic */ void lambda$processIntent$0$EnhancedIntentService(Intent intent, C38478zW r4) {
        try {
            handleIntent(intent);
        } finally {
            r4.LIZ(null);
        }
    }

    public int onStartCommand(Intent intent, int i, int i2) {
        synchronized (this.lock) {
            this.lastStartId = i2;
            this.runningTasks++;
        }
        Intent startCommandIntent = getStartCommandIntent(intent);
        if (startCommandIntent == null) {
            finishTask(intent);
            return 2;
        }
        C38348zJ<Void> processIntent = processIntent(startCommandIntent);
        if (processIntent.LIZ()) {
            finishTask(intent);
            return 2;
        }
        processIntent.LIZ(C37448xr.LIZ, (C38568zf<Void>) new C37428xp(this, intent));
        return 3;
    }
}
