package com.baseflow.permissionhandler;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.PowerManager;
import android.provider.Settings;
import androidx.annotation.Nullable;
import androidx.core.app.r1;
import ed.w;
import ed.x;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public final class e implements w, x {

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

    @Nullable
    private Activity activity;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public int f5543b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public HashMap f5544c;

    @Nullable
    private d successCallback;

    public e(Context context) {
        this.f5542a = context;
    }

    @Override // ed.x
    public final boolean a(int i10, String[] strArr, int[] iArr) {
        int i11;
        if (i10 != 24) {
            this.f5543b = 0;
            return false;
        }
        if (this.f5544c == null) {
            return false;
        }
        if (strArr.length == 0 && iArr.length == 0) {
            this.f5543b = 0;
            return false;
        }
        List listAsList = Arrays.asList(strArr);
        int iIndexOf = listAsList.indexOf("android.permission.WRITE_CALENDAR");
        if (iIndexOf >= 0) {
            int permissionStatus = f.toPermissionStatus(this.activity, "android.permission.WRITE_CALENDAR", iArr[iIndexOf]);
            this.f5544c.put(36, Integer.valueOf(permissionStatus));
            int iIndexOf2 = listAsList.indexOf("android.permission.READ_CALENDAR");
            if (iIndexOf2 >= 0) {
                int iIntValue = f.strictestStatus(Integer.valueOf(permissionStatus), Integer.valueOf(f.toPermissionStatus(this.activity, "android.permission.READ_CALENDAR", iArr[iIndexOf2]))).intValue();
                this.f5544c.put(37, Integer.valueOf(iIntValue));
                this.f5544c.put(0, Integer.valueOf(iIntValue));
            }
        }
        for (int i12 = 0; i12 < strArr.length; i12++) {
            String str = strArr[i12];
            if (!str.equals("android.permission.WRITE_CALENDAR") && !str.equals("android.permission.READ_CALENDAR")) {
                switch (str) {
                    case "android.permission.READ_SMS":
                    case "android.permission.RECEIVE_WAP_PUSH":
                    case "android.permission.RECEIVE_MMS":
                    case "android.permission.RECEIVE_SMS":
                    case "android.permission.SEND_SMS":
                        i11 = 13;
                        break;
                    case "android.permission.READ_CALENDAR":
                    case "android.permission.WRITE_CALENDAR":
                        i11 = 0;
                        break;
                    case "android.permission.POST_NOTIFICATIONS":
                        i11 = 17;
                        break;
                    case "android.permission.READ_CALL_LOG":
                    case "android.permission.READ_PHONE_NUMBERS":
                    case "android.permission.READ_PHONE_STATE":
                    case "android.permission.CALL_PHONE":
                    case "android.permission.WRITE_CALL_LOG":
                    case "android.permission.USE_SIP":
                    case "com.android.voicemail.permission.ADD_VOICEMAIL":
                        i11 = 8;
                        break;
                    case "android.permission.ACCESS_FINE_LOCATION":
                    case "android.permission.ACCESS_COARSE_LOCATION":
                        i11 = 3;
                        break;
                    case "android.permission.MANAGE_EXTERNAL_STORAGE":
                        i11 = 22;
                        break;
                    case "android.permission.ACCESS_NOTIFICATION_POLICY":
                        i11 = 27;
                        break;
                    case "android.permission.SYSTEM_ALERT_WINDOW":
                        i11 = 23;
                        break;
                    case "android.permission.BODY_SENSORS":
                        i11 = 12;
                        break;
                    case "android.permission.NEARBY_WIFI_DEVICES":
                        i11 = 31;
                        break;
                    case "android.permission.BLUETOOTH_CONNECT":
                        i11 = 30;
                        break;
                    case "android.permission.READ_EXTERNAL_STORAGE":
                    case "android.permission.WRITE_EXTERNAL_STORAGE":
                        i11 = 15;
                        break;
                    case "android.permission.READ_MEDIA_IMAGES":
                        i11 = 9;
                        break;
                    case "android.permission.WRITE_CONTACTS":
                    case "android.permission.GET_ACCOUNTS":
                    case "android.permission.READ_CONTACTS":
                        i11 = 2;
                        break;
                    case "android.permission.BODY_SENSORS_BACKGROUND":
                        i11 = 35;
                        break;
                    case "android.permission.CAMERA":
                        i11 = 1;
                        break;
                    case "android.permission.READ_MEDIA_AUDIO":
                        i11 = 33;
                        break;
                    case "android.permission.READ_MEDIA_VIDEO":
                        i11 = 32;
                        break;
                    case "android.permission.SCHEDULE_EXACT_ALARM":
                        i11 = 34;
                        break;
                    case "android.permission.BLUETOOTH_ADVERTISE":
                        i11 = 29;
                        break;
                    case "android.permission.REQUEST_INSTALL_PACKAGES":
                        i11 = 24;
                        break;
                    case "android.permission.ACTIVITY_RECOGNITION":
                        i11 = 19;
                        break;
                    case "android.permission.RECORD_AUDIO":
                        i11 = 7;
                        break;
                    case "android.permission.ACCESS_BACKGROUND_LOCATION":
                        i11 = 4;
                        break;
                    case "android.permission.BLUETOOTH_SCAN":
                        i11 = 28;
                        break;
                    case "android.permission.ACCESS_MEDIA_LOCATION":
                        i11 = 18;
                        break;
                    default:
                        i11 = 20;
                        break;
                }
                if (i11 != 20) {
                    int i13 = iArr[i12];
                    if (i11 == 8) {
                        this.f5544c.put(8, f.strictestStatus((Integer) this.f5544c.get(8), Integer.valueOf(f.toPermissionStatus(this.activity, str, i13))));
                    } else if (i11 == 7) {
                        if (!this.f5544c.containsKey(7)) {
                            this.f5544c.put(7, Integer.valueOf(f.toPermissionStatus(this.activity, str, i13)));
                        }
                        if (!this.f5544c.containsKey(14)) {
                            this.f5544c.put(14, Integer.valueOf(f.toPermissionStatus(this.activity, str, i13)));
                        }
                    } else if (i11 == 4) {
                        int permissionStatus2 = f.toPermissionStatus(this.activity, str, i13);
                        if (!this.f5544c.containsKey(4)) {
                            this.f5544c.put(4, Integer.valueOf(permissionStatus2));
                        }
                    } else if (i11 == 3) {
                        int permissionStatus3 = f.toPermissionStatus(this.activity, str, i13);
                        if (Build.VERSION.SDK_INT < 29 && !this.f5544c.containsKey(4)) {
                            this.f5544c.put(4, Integer.valueOf(permissionStatus3));
                        }
                        if (!this.f5544c.containsKey(5)) {
                            this.f5544c.put(5, Integer.valueOf(permissionStatus3));
                        }
                        this.f5544c.put(Integer.valueOf(i11), Integer.valueOf(permissionStatus3));
                    } else if (i11 == 9 || i11 == 32) {
                        this.f5544c.put(Integer.valueOf(i11), Integer.valueOf(b(i11)));
                    } else if (!this.f5544c.containsKey(Integer.valueOf(i11))) {
                        this.f5544c.put(Integer.valueOf(i11), Integer.valueOf(f.toPermissionStatus(this.activity, str, i13)));
                    }
                }
            }
        }
        int length = this.f5543b - iArr.length;
        this.f5543b = length;
        d dVar = this.successCallback;
        if (dVar == null || length != 0) {
            return true;
        }
        ((a) dVar).f5535b.success(this.f5544c);
        return true;
    }

    public final int b(int i10) {
        int i11 = 0;
        Context context = this.f5542a;
        if (i10 == 17) {
            if (Build.VERSION.SDK_INT < 33) {
                return new r1(context).f1645b.areNotificationsEnabled() ? 1 : 0;
            }
            if (context.checkSelfPermission("android.permission.POST_NOTIFICATIONS") == 0) {
                return 1;
            }
            return f.determineDeniedVariant(this.activity, "android.permission.POST_NOTIFICATIONS");
        }
        if (i10 == 21) {
            ArrayList arrayListB = f.b(context, 21);
            return (arrayListB == null || arrayListB.isEmpty()) ? 0 : 1;
        }
        if ((i10 == 30 || i10 == 28 || i10 == 29) && Build.VERSION.SDK_INT < 31) {
            ArrayList arrayListB2 = f.b(context, 21);
            return (arrayListB2 == null || arrayListB2.isEmpty()) ? 0 : 1;
        }
        if ((i10 == 37 || i10 == 0) && !c()) {
            return 0;
        }
        ArrayList<String> arrayListB3 = f.b(context, i10);
        if (arrayListB3 == null) {
            return 1;
        }
        if (arrayListB3.size() == 0) {
            arrayListB3.toString();
            return (i10 != 22 || Build.VERSION.SDK_INT >= 30) ? 0 : 2;
        }
        if (context.getApplicationInfo().targetSdkVersion >= 23) {
            HashSet hashSet = new HashSet();
            for (String str : arrayListB3) {
                if (i10 == 16) {
                    String packageName = context.getPackageName();
                    PowerManager powerManager = (PowerManager) context.getSystemService("power");
                    if (powerManager == null || !powerManager.isIgnoringBatteryOptimizations(packageName)) {
                        hashSet.add(Integer.valueOf(i11));
                    } else {
                        hashSet.add(1);
                    }
                } else if (i10 == 22) {
                    if (Build.VERSION.SDK_INT < 30) {
                        hashSet.add(2);
                    }
                    hashSet.add(Integer.valueOf(Environment.isExternalStorageManager() ? 1 : 0));
                } else if (i10 == 23) {
                    hashSet.add(Integer.valueOf(Settings.canDrawOverlays(context) ? 1 : 0));
                } else if (i10 == 24) {
                    if (Build.VERSION.SDK_INT >= 26) {
                        hashSet.add(Integer.valueOf(context.getPackageManager().canRequestPackageInstalls() ? 1 : 0));
                    }
                } else if (i10 == 27) {
                    hashSet.add(Integer.valueOf(((NotificationManager) context.getSystemService("notification")).isNotificationPolicyAccessGranted() ? 1 : 0));
                } else if (i10 == 34) {
                    if (Build.VERSION.SDK_INT >= 31) {
                        hashSet.add(Integer.valueOf(((AlarmManager) context.getSystemService("alarm")).canScheduleExactAlarms() ? 1 : 0));
                    } else {
                        hashSet.add(1);
                    }
                } else if (i10 == 9 || i10 == 32) {
                    int iA = g0.d.a(context, str);
                    if ((Build.VERSION.SDK_INT >= 34 ? g0.d.a(context, "android.permission.READ_MEDIA_VISUAL_USER_SELECTED") : iA) == 0 && iA == -1) {
                        hashSet.add(3);
                    } else if (iA == 0) {
                        hashSet.add(1);
                    } else {
                        hashSet.add(Integer.valueOf(f.determineDeniedVariant(this.activity, str)));
                    }
                } else if (g0.d.a(context, str) != 0) {
                    hashSet.add(Integer.valueOf(f.determineDeniedVariant(this.activity, str)));
                }
                i11 = 0;
            }
            if (!hashSet.isEmpty()) {
                return f.d(hashSet).intValue();
            }
        }
        return 1;
    }

    public final boolean c() {
        ArrayList arrayListB = f.b(this.f5542a, 37);
        return (arrayListB != null && arrayListB.contains("android.permission.WRITE_CALENDAR")) && (arrayListB != null && arrayListB.contains("android.permission.READ_CALENDAR"));
    }

    public final void d(int i10, String str) {
        if (this.activity == null) {
            return;
        }
        Intent intent = new Intent(str);
        if (!str.equals("android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS")) {
            intent.setData(Uri.parse("package:" + this.activity.getPackageName()));
        }
        this.activity.startActivityForResult(intent, i10);
        this.f5543b++;
    }

    public final void e(List list, a aVar, a aVar2) {
        if (this.f5543b > 0) {
            aVar2.a("PermissionHandler.PermissionManager", "A request for permissions is already running, please wait for it to finish before doing another request (note that you can request multiple permissions at the same time).");
            return;
        }
        if (this.activity == null) {
            aVar2.a("PermissionHandler.PermissionManager", "Unable to detect current Android Activity.");
            return;
        }
        this.successCallback = aVar;
        this.f5544c = new HashMap();
        this.f5543b = 0;
        ArrayList arrayList = new ArrayList();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Integer num = (Integer) it.next();
            if (b(num.intValue()) != 1) {
                ArrayList arrayListB = f.b(this.activity, num.intValue());
                if (arrayListB != null && !arrayListB.isEmpty()) {
                    int i10 = Build.VERSION.SDK_INT;
                    if (num.intValue() == 16) {
                        d(209, "android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS");
                    } else if (i10 >= 30 && num.intValue() == 22) {
                        d(210, "android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION");
                    } else if (num.intValue() == 23) {
                        d(211, "android.settings.action.MANAGE_OVERLAY_PERMISSION");
                    } else if (i10 >= 26 && num.intValue() == 24) {
                        d(212, "android.settings.MANAGE_UNKNOWN_APP_SOURCES");
                    } else if (num.intValue() == 27) {
                        d(213, "android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS");
                    } else if (i10 >= 31 && num.intValue() == 34) {
                        d(214, "android.settings.REQUEST_SCHEDULE_EXACT_ALARM");
                    } else if (num.intValue() != 37 && num.intValue() != 0) {
                        arrayList.addAll(arrayListB);
                        this.f5543b = arrayListB.size() + this.f5543b;
                    } else if (c()) {
                        arrayList.add("android.permission.WRITE_CALENDAR");
                        arrayList.add("android.permission.READ_CALENDAR");
                        this.f5543b += 2;
                    } else {
                        this.f5544c.put(num, 0);
                    }
                } else if (!this.f5544c.containsKey(num)) {
                    num.intValue();
                    this.f5544c.put(num, 0);
                    if (num.intValue() != 22 || Build.VERSION.SDK_INT >= 30) {
                        this.f5544c.put(num, 0);
                    } else {
                        this.f5544c.put(num, 2);
                    }
                }
            } else if (!this.f5544c.containsKey(num)) {
                this.f5544c.put(num, 1);
            }
        }
        if (arrayList.size() > 0) {
            androidx.core.app.f.c(this.activity, (String[]) arrayList.toArray(new String[0]), 24);
        }
        d dVar = this.successCallback;
        if (dVar == null || this.f5543b != 0) {
            return;
        }
        ((a) dVar).f5535b.success(this.f5544c);
    }

    public final void f(int i10, a aVar, a aVar2) {
        Activity activity = this.activity;
        if (activity == null) {
            aVar2.a("PermissionHandler.PermissionManager", "Unable to detect current Android Activity.");
            return;
        }
        ArrayList arrayListB = f.b(activity, i10);
        if (arrayListB == null) {
            aVar.c(false);
        } else if (arrayListB.isEmpty()) {
            aVar.c(false);
        } else {
            aVar.c(androidx.core.app.f.d(this.activity, (String) arrayListB.get(0)));
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // ed.w
    public final boolean onActivityResult(int i10, int i11, Intent intent) {
        int i12;
        int iCanScheduleExactAlarms;
        Activity activity = this.activity;
        boolean z10 = false;
        z10 = false;
        if (activity == null) {
            return false;
        }
        if (this.f5544c == null) {
            this.f5543b = 0;
            return false;
        }
        if (i10 == 209) {
            Context context = this.f5542a;
            String packageName = context.getPackageName();
            PowerManager powerManager = (PowerManager) context.getSystemService("power");
            if (powerManager != null && powerManager.isIgnoringBatteryOptimizations(packageName)) {
                z10 = true;
            }
            i12 = 16;
            iCanScheduleExactAlarms = z10;
        } else if (i10 == 210) {
            if (Build.VERSION.SDK_INT < 30) {
                return false;
            }
            i12 = 22;
            iCanScheduleExactAlarms = Environment.isExternalStorageManager();
        } else if (i10 == 211) {
            i12 = 23;
            iCanScheduleExactAlarms = Settings.canDrawOverlays(activity);
        } else if (i10 == 212) {
            if (Build.VERSION.SDK_INT < 26) {
                return false;
            }
            i12 = 24;
            iCanScheduleExactAlarms = activity.getPackageManager().canRequestPackageInstalls();
        } else if (i10 == 213) {
            i12 = 27;
            iCanScheduleExactAlarms = ((NotificationManager) activity.getSystemService("notification")).isNotificationPolicyAccessGranted();
        } else {
            if (i10 != 214) {
                return false;
            }
            i12 = 34;
            iCanScheduleExactAlarms = Build.VERSION.SDK_INT >= 31 ? ((AlarmManager) activity.getSystemService("alarm")).canScheduleExactAlarms() : true;
        }
        this.f5544c.put(Integer.valueOf(i12), Integer.valueOf(iCanScheduleExactAlarms));
        int i13 = this.f5543b - 1;
        this.f5543b = i13;
        d dVar = this.successCallback;
        if (dVar != null && i13 == 0) {
            ((a) dVar).f5535b.success(this.f5544c);
        }
        return true;
    }

    public void setActivity(@Nullable Activity activity) {
        this.activity = activity;
    }
}
