package androidx.localbroadcastmanager.content;

import X.7lW;
import X.C03140Ca;
import X.C03150Cb;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import com.bytedance.covode.number.Covode;
import java.util.ArrayList;
import java.util.HashMap;

public final class LocalBroadcastManager {
    public static LocalBroadcastManager mInstance;
    public static final Object mLock = new Object();
    public final HashMap<String, ArrayList<C03150Cb>> mActions = new HashMap<>();
    public final Context mAppContext;
    public final Handler mHandler;
    public final ArrayList<C03140Ca> mPendingBroadcasts = new ArrayList<>();
    public final HashMap<BroadcastReceiver, ArrayList<C03150Cb>> mReceivers = new HashMap<>();

    static {
        Covode.recordClassIndex(1312);
    }

    /* JADX WARNING: Code restructure failed: missing block: B:10:0x001c, code lost:
        r5 = r7[r6];
        r4 = r5.LIZIZ.size();
        r3 = 0;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:11:0x0025, code lost:
        if (r3 >= r4) goto L_0x003f;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:12:0x0027, code lost:
        r1 = r5.LIZIZ.get(r3);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:13:0x0031, code lost:
        if (r1.LIZLLL != false) goto L_0x003c;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:14:0x0033, code lost:
        r1.LIZIZ.onReceive(r9.mAppContext, r5.LIZ);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:15:0x003c, code lost:
        r3 = r3 + 1;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:16:0x003f, code lost:
        r6 = r6 + 1;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:17:0x0041, code lost:
        if (r6 >= r8) goto L_0x0000;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:9:0x001a, code lost:
        r6 = 0;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final void executePendingBroadcasts() {
        /*
            r9 = this;
        L_0x0000:
            java.util.HashMap<android.content.BroadcastReceiver, java.util.ArrayList<X.0Cb>> r1 = r9.mReceivers
            monitor-enter(r1)
            java.util.ArrayList<X.0Ca> r0 = r9.mPendingBroadcasts     // Catch:{ all -> 0x0045 }
            int r8 = r0.size()     // Catch:{ all -> 0x0045 }
            if (r8 > 0) goto L_0x000d
            monitor-exit(r1)     // Catch:{ all -> 0x0045 }
            goto L_0x0044
        L_0x000d:
            X.0Ca[] r7 = new X.C03140Ca[r8]     // Catch:{ all -> 0x0045 }
            java.util.ArrayList<X.0Ca> r0 = r9.mPendingBroadcasts     // Catch:{ all -> 0x0045 }
            r0.toArray(r7)     // Catch:{ all -> 0x0045 }
            java.util.ArrayList<X.0Ca> r0 = r9.mPendingBroadcasts     // Catch:{ all -> 0x0045 }
            r0.clear()     // Catch:{ all -> 0x0045 }
            monitor-exit(r1)     // Catch:{ all -> 0x0045 }
            r0 = 0
            r6 = 0
        L_0x001c:
            r5 = r7[r6]
            java.util.ArrayList<X.0Cb> r0 = r5.LIZIZ
            int r4 = r0.size()
            r3 = 0
        L_0x0025:
            if (r3 >= r4) goto L_0x003f
            java.util.ArrayList<X.0Cb> r0 = r5.LIZIZ
            java.lang.Object r1 = r0.get(r3)
            X.0Cb r1 = (X.C03150Cb) r1
            boolean r0 = r1.LIZLLL
            if (r0 != 0) goto L_0x003c
            android.content.BroadcastReceiver r2 = r1.LIZIZ
            android.content.Context r1 = r9.mAppContext
            android.content.Intent r0 = r5.LIZ
            r2.onReceive(r1, r0)
        L_0x003c:
            int r3 = r3 + 1
            goto L_0x0025
        L_0x003f:
            int r6 = r6 + 1
            if (r6 >= r8) goto L_0x0000
            goto L_0x001c
        L_0x0044:
            return
        L_0x0045:
            r0 = move-exception
            monitor-exit(r1)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: androidx.localbroadcastmanager.content.LocalBroadcastManager.executePendingBroadcasts():void");
    }

    public static Context INVOKEVIRTUAL_androidx_localbroadcastmanager_content_LocalBroadcastManager_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext(Context context) {
        Context applicationContext = context.getApplicationContext();
        if (7lW.LIZJ && applicationContext == null) {
            return 7lW.LIZ;
        }
        return applicationContext;
    }

    public final void sendBroadcastSync(Intent intent) {
        if (sendBroadcast(intent)) {
            executePendingBroadcasts();
        }
    }

    public static LocalBroadcastManager getInstance(Context context) {
        LocalBroadcastManager localBroadcastManager;
        synchronized (mLock) {
            if (mInstance == null) {
                mInstance = new LocalBroadcastManager(INVOKEVIRTUAL_androidx_localbroadcastmanager_content_LocalBroadcastManager_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext(context));
            }
            localBroadcastManager = mInstance;
        }
        return localBroadcastManager;
    }

    public LocalBroadcastManager(Context context) {
        this.mAppContext = context;
        this.mHandler = new Handler(context.getMainLooper()) {
            static {
                Covode.recordClassIndex(1313);
            }

            public final void handleMessage(Message message) {
                if (message.what != 1) {
                    super.handleMessage(message);
                } else {
                    LocalBroadcastManager.this.executePendingBroadcasts();
                }
            }
        };
    }

    public final void unregisterReceiver(BroadcastReceiver broadcastReceiver) {
        synchronized (this.mReceivers) {
            ArrayList remove = this.mReceivers.remove(broadcastReceiver);
            if (remove != null) {
                for (int size = remove.size() - 1; size >= 0; size--) {
                    C03150Cb r6 = (C03150Cb) remove.get(size);
                    r6.LIZLLL = true;
                    for (int i = 0; i < r6.LIZ.countActions(); i++) {
                        String action = r6.LIZ.getAction(i);
                        ArrayList arrayList = this.mActions.get(action);
                        if (arrayList != null) {
                            for (int size2 = arrayList.size() - 1; size2 >= 0; size2--) {
                                C03150Cb r1 = (C03150Cb) arrayList.get(size2);
                                if (r1.LIZIZ == broadcastReceiver) {
                                    r1.LIZLLL = true;
                                    arrayList.remove(size2);
                                }
                            }
                            if (arrayList.size() <= 0) {
                                this.mActions.remove(action);
                            }
                        }
                    }
                }
            }
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:29:0x009a, code lost:
        return true;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:31:0x009c, code lost:
        return false;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final boolean sendBroadcast(android.content.Intent r17) {
        /*
            r16 = this;
            r3 = r16
            java.util.HashMap<android.content.BroadcastReceiver, java.util.ArrayList<X.0Cb>> r2 = r3.mReceivers
            monitor-enter(r2)
            r5 = r17
            java.lang.String r10 = r5.getAction()     // Catch:{ all -> 0x009e }
            android.content.Context r0 = r3.mAppContext     // Catch:{ all -> 0x009e }
            android.content.ContentResolver r0 = r0.getContentResolver()     // Catch:{ all -> 0x009e }
            java.lang.String r11 = r5.resolveTypeIfNeeded(r0)     // Catch:{ all -> 0x009e }
            android.net.Uri r13 = r5.getData()     // Catch:{ all -> 0x009e }
            java.lang.String r12 = r5.getScheme()     // Catch:{ all -> 0x009e }
            java.util.Set r14 = r5.getCategories()     // Catch:{ all -> 0x009e }
            int r0 = r5.getFlags()     // Catch:{ all -> 0x009e }
            r1 = r0 & 8
            r0 = 1
            java.util.HashMap<java.lang.String, java.util.ArrayList<X.0Cb>> r1 = r3.mActions     // Catch:{ all -> 0x009e }
            java.lang.String r0 = r5.getAction()     // Catch:{ all -> 0x009e }
            java.lang.Object r6 = r1.get(r0)     // Catch:{ all -> 0x009e }
            java.util.ArrayList r6 = (java.util.ArrayList) r6     // Catch:{ all -> 0x009e }
            if (r6 == 0) goto L_0x009b
            r4 = 0
            r1 = 0
        L_0x0038:
            int r0 = r6.size()     // Catch:{ all -> 0x009e }
            if (r1 >= r0) goto L_0x006c
            java.lang.Object r8 = r6.get(r1)     // Catch:{ all -> 0x009e }
            X.0Cb r8 = (X.C03150Cb) r8     // Catch:{ all -> 0x009e }
            boolean r0 = r8.LIZJ     // Catch:{ all -> 0x009e }
            if (r0 == 0) goto L_0x0049
            goto L_0x0063
        L_0x0049:
            android.content.IntentFilter r9 = r8.LIZ     // Catch:{ all -> 0x009e }
            java.lang.String r15 = "LocalBroadcastManager"
            r10 = r10
            r11 = r11
            r7 = 1
            int r0 = r9.match(r10, r11, r12, r13, r14, r15)     // Catch:{ all -> 0x009e }
            if (r0 < 0) goto L_0x0066
            if (r4 != 0) goto L_0x005d
            java.util.ArrayList r4 = new java.util.ArrayList     // Catch:{ all -> 0x009e }
            r4.<init>()     // Catch:{ all -> 0x009e }
        L_0x005d:
            r4.add(r8)     // Catch:{ all -> 0x009e }
            r8.LIZJ = r7     // Catch:{ all -> 0x009e }
            goto L_0x0066
        L_0x0063:
            r10 = r10
            r11 = r11
            r0 = 1
        L_0x0066:
            int r1 = r1 + 1
            r10 = r10
            r11 = r11
            r0 = 1
            goto L_0x0038
        L_0x006c:
            r7 = 1
            if (r4 == 0) goto L_0x009b
            r6 = 0
        L_0x0070:
            int r0 = r4.size()     // Catch:{ all -> 0x009e }
            if (r6 >= r0) goto L_0x0082
            java.lang.Object r1 = r4.get(r6)     // Catch:{ all -> 0x009e }
            X.0Cb r1 = (X.C03150Cb) r1     // Catch:{ all -> 0x009e }
            r0 = 0
            r1.LIZJ = r0     // Catch:{ all -> 0x009e }
            int r6 = r6 + 1
            goto L_0x0070
        L_0x0082:
            java.util.ArrayList<X.0Ca> r1 = r3.mPendingBroadcasts     // Catch:{ all -> 0x009e }
            X.0Ca r0 = new X.0Ca     // Catch:{ all -> 0x009e }
            r0.<init>(r5, r4)     // Catch:{ all -> 0x009e }
            r1.add(r0)     // Catch:{ all -> 0x009e }
            android.os.Handler r0 = r3.mHandler     // Catch:{ all -> 0x009e }
            boolean r0 = r0.hasMessages(r7)     // Catch:{ all -> 0x009e }
            if (r0 != 0) goto L_0x0099
            android.os.Handler r0 = r3.mHandler     // Catch:{ all -> 0x009e }
            r0.sendEmptyMessage(r7)     // Catch:{ all -> 0x009e }
        L_0x0099:
            monitor-exit(r2)     // Catch:{ all -> 0x009e }
            return r7
        L_0x009b:
            monitor-exit(r2)
            r0 = 0
            return r0
        L_0x009e:
            r0 = move-exception
            monitor-exit(r2)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: androidx.localbroadcastmanager.content.LocalBroadcastManager.sendBroadcast(android.content.Intent):boolean");
    }

    public final void registerReceiver(BroadcastReceiver broadcastReceiver, IntentFilter intentFilter) {
        synchronized (this.mReceivers) {
            C03150Cb r5 = new C03150Cb(intentFilter, broadcastReceiver);
            ArrayList arrayList = this.mReceivers.get(broadcastReceiver);
            if (arrayList == null) {
                arrayList = new ArrayList(1);
                this.mReceivers.put(broadcastReceiver, arrayList);
            }
            arrayList.add(r5);
            for (int i = 0; i < intentFilter.countActions(); i++) {
                String action = intentFilter.getAction(i);
                ArrayList arrayList2 = this.mActions.get(action);
                if (arrayList2 == null) {
                    arrayList2 = new ArrayList(1);
                    this.mActions.put(action, arrayList2);
                }
                arrayList2.add(r5);
            }
        }
    }
}
