package androidx.core.app;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.DeadObjectException;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.provider.Settings;
import android.support.v4.app.INotificationSideChannel;
import android.util.Log;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public final class p1 implements Handler.Callback, ServiceConnection {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f1627a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Handler f1628b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final HashMap f1629c = new HashMap();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public HashSet f1630d = new HashSet();

    public p1(Context context) {
        this.f1627a = context;
        HandlerThread handlerThread = new HandlerThread("NotificationManagerCompat");
        handlerThread.start();
        this.f1628b = new Handler(handlerThread.getLooper(), this);
    }

    /* JADX WARN: Removed duplicated region for block: B:17:0x0046  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a(o1 o1Var) {
        ArrayDeque arrayDeque;
        boolean zIsLoggable = Log.isLoggable("NotifManCompat", 3);
        ComponentName componentName = o1Var.f1615a;
        if (zIsLoggable) {
            Objects.toString(componentName);
            o1Var.f1618d.size();
        }
        if (o1Var.f1618d.isEmpty()) {
            return;
        }
        if (!o1Var.f1616b) {
            Intent component = new Intent("android.support.BIND_NOTIFICATION_SIDE_CHANNEL").setComponent(componentName);
            Context context = this.f1627a;
            boolean zBindService = context.bindService(component, this, 33);
            o1Var.f1616b = zBindService;
            if (zBindService) {
                o1Var.f1619e = 0;
            } else {
                Objects.toString(componentName);
                context.unbindService(this);
            }
            if (o1Var.f1616b) {
            }
        } else if (o1Var.f1617c != null) {
            while (true) {
                arrayDeque = o1Var.f1618d;
                q1 q1Var = (q1) arrayDeque.peek();
                if (q1Var == null) {
                    break;
                }
                try {
                    if (Log.isLoggable("NotifManCompat", 3)) {
                        q1Var.toString();
                    }
                    ((m1) q1Var).a(o1Var.f1617c);
                    arrayDeque.remove();
                } catch (DeadObjectException unused) {
                    if (Log.isLoggable("NotifManCompat", 3)) {
                        Objects.toString(componentName);
                    }
                } catch (RemoteException unused2) {
                    Objects.toString(componentName);
                }
            }
            if (arrayDeque.isEmpty()) {
                return;
            }
            b(o1Var);
            return;
        }
        b(o1Var);
    }

    public final void b(o1 o1Var) {
        Handler handler = this.f1628b;
        ComponentName componentName = o1Var.f1615a;
        if (handler.hasMessages(3, componentName)) {
            return;
        }
        int i10 = o1Var.f1619e + 1;
        o1Var.f1619e = i10;
        if (i10 <= 6) {
            handler.sendMessageDelayed(handler.obtainMessage(3, componentName), (1 << r3) * 1000);
            return;
        }
        ArrayDeque arrayDeque = o1Var.f1618d;
        arrayDeque.size();
        Objects.toString(componentName);
        arrayDeque.clear();
    }

    @Override // android.os.Handler.Callback
    public final boolean handleMessage(Message message) {
        HashSet hashSet;
        int i10 = message.what;
        if (i10 != 0) {
            if (i10 == 1) {
                n1 n1Var = (n1) message.obj;
                ComponentName componentName = n1Var.f1605a;
                IBinder iBinder = n1Var.f1606b;
                o1 o1Var = (o1) this.f1629c.get(componentName);
                if (o1Var != null) {
                    o1Var.f1617c = INotificationSideChannel.Stub.asInterface(iBinder);
                    o1Var.f1619e = 0;
                    a(o1Var);
                }
                return true;
            }
            if (i10 != 2) {
                if (i10 != 3) {
                    return false;
                }
                o1 o1Var2 = (o1) this.f1629c.get((ComponentName) message.obj);
                if (o1Var2 != null) {
                    a(o1Var2);
                }
                return true;
            }
            o1 o1Var3 = (o1) this.f1629c.get((ComponentName) message.obj);
            if (o1Var3 != null) {
                if (o1Var3.f1616b) {
                    this.f1627a.unbindService(this);
                    o1Var3.f1616b = false;
                }
                o1Var3.f1617c = null;
            }
            return true;
        }
        q1 q1Var = (q1) message.obj;
        String string = Settings.Secure.getString(this.f1627a.getContentResolver(), "enabled_notification_listeners");
        synchronized (r1.f1639c) {
            if (string != null) {
                try {
                    if (!string.equals(r1.f1640d)) {
                        String[] strArrSplit = string.split(":", -1);
                        HashSet hashSet2 = new HashSet(strArrSplit.length);
                        for (String str : strArrSplit) {
                            ComponentName componentNameUnflattenFromString = ComponentName.unflattenFromString(str);
                            if (componentNameUnflattenFromString != null) {
                                hashSet2.add(componentNameUnflattenFromString.getPackageName());
                            }
                        }
                        r1.f1641e = hashSet2;
                        r1.f1640d = string;
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            hashSet = r1.f1641e;
        }
        if (!hashSet.equals(this.f1630d)) {
            this.f1630d = hashSet;
            List<ResolveInfo> listQueryIntentServices = this.f1627a.getPackageManager().queryIntentServices(new Intent().setAction("android.support.BIND_NOTIFICATION_SIDE_CHANNEL"), 0);
            HashSet<ComponentName> hashSet3 = new HashSet();
            for (ResolveInfo resolveInfo : listQueryIntentServices) {
                if (hashSet.contains(resolveInfo.serviceInfo.packageName)) {
                    ServiceInfo serviceInfo = resolveInfo.serviceInfo;
                    ComponentName componentName2 = new ComponentName(serviceInfo.packageName, serviceInfo.name);
                    if (resolveInfo.serviceInfo.permission != null) {
                        componentName2.toString();
                    } else {
                        hashSet3.add(componentName2);
                    }
                }
            }
            for (ComponentName componentName3 : hashSet3) {
                if (!this.f1629c.containsKey(componentName3)) {
                    if (Log.isLoggable("NotifManCompat", 3)) {
                        Objects.toString(componentName3);
                    }
                    this.f1629c.put(componentName3, new o1(componentName3));
                }
            }
            Iterator it = this.f1629c.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                if (!hashSet3.contains(entry.getKey())) {
                    if (Log.isLoggable("NotifManCompat", 3)) {
                        Objects.toString(entry.getKey());
                    }
                    o1 o1Var4 = (o1) entry.getValue();
                    if (o1Var4.f1616b) {
                        this.f1627a.unbindService(this);
                        o1Var4.f1616b = false;
                    }
                    o1Var4.f1617c = null;
                    it.remove();
                }
            }
        }
        for (o1 o1Var5 : this.f1629c.values()) {
            o1Var5.f1618d.add(q1Var);
            a(o1Var5);
        }
        return true;
    }

    @Override // android.content.ServiceConnection
    public final void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        if (Log.isLoggable("NotifManCompat", 3)) {
            Objects.toString(componentName);
        }
        this.f1628b.obtainMessage(1, new n1(componentName, iBinder)).sendToTarget();
    }

    @Override // android.content.ServiceConnection
    public final void onServiceDisconnected(ComponentName componentName) {
        if (Log.isLoggable("NotifManCompat", 3)) {
            Objects.toString(componentName);
        }
        this.f1628b.obtainMessage(2, componentName).sendToTarget();
    }
}
