package com.ironsource.sdk.controller;

import android.content.Context;
import com.ironsource.aq;
import com.ironsource.l9;
import com.ironsource.mediationsdk.logger.IronLog;
import com.ironsource.oj;
import com.ironsource.sdk.utils.Logger;
import com.ironsource.z3;
import org.json.JSONException;
import org.json.JSONObject;
/* loaded from: classes4.dex */
public class q {
    private static final String b = "q";
    private static final String c = "getPermissions";
    private static final String d = "isPermissionGranted";
    private static final String e = "permissions";
    private static final String f = "permission";
    private static final String g = "status";
    private static final String h = "functionName";
    private static final String i = "functionParams";
    private static final String j = "success";
    private static final String k = "fail";
    private static final String l = "unhandledPermission";

    /* renamed from: a  reason: collision with root package name */
    private Context f3003a;

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes4.dex */
    public static class b {

        /* renamed from: a  reason: collision with root package name */
        String f3004a;
        JSONObject b;
        String c;
        String d;

        private b() {
        }
    }

    public q(Context context) {
        this.f3003a = context;
    }

    private b a(String str) throws JSONException {
        JSONObject jSONObject = new JSONObject(str);
        b bVar = new b();
        bVar.f3004a = jSONObject.optString("functionName");
        bVar.b = jSONObject.optJSONObject("functionParams");
        bVar.c = jSONObject.optString("success");
        bVar.d = jSONObject.optString("fail");
        return bVar;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void a(String str, oj ojVar) throws Exception {
        b a2 = a(str);
        if (c.equals(a2.f3004a)) {
            a(a2.b, a2, ojVar);
        } else if (d.equals(a2.f3004a)) {
            b(a2.b, a2, ojVar);
        } else {
            Logger.i(b, "PermissionsJSAdapter unhandled API request " + str);
        }
    }

    public void a(JSONObject jSONObject, b bVar, oj ojVar) {
        aq aqVar = new aq();
        try {
            aqVar.a(e, z3.a(this.f3003a, jSONObject.getJSONArray(e)));
            ojVar.a(true, bVar.c, aqVar);
        } catch (Exception e2) {
            l9.d().a(e2);
            IronLog.INTERNAL.error(e2.toString());
            Logger.i(b, "PermissionsJSAdapter getPermissions JSON Exception when getting permissions parameter " + e2.getMessage());
            aqVar.b("errMsg", e2.getMessage());
            ojVar.a(false, bVar.d, aqVar);
        }
    }

    public void b(JSONObject jSONObject, b bVar, oj ojVar) {
        String str;
        boolean z;
        aq aqVar = new aq();
        try {
            String string = jSONObject.getString(f);
            aqVar.b(f, string);
            if (z3.d(this.f3003a, string)) {
                aqVar.b("status", String.valueOf(z3.c(this.f3003a, string)));
                str = bVar.c;
                z = true;
            } else {
                aqVar.b("status", l);
                str = bVar.d;
                z = false;
            }
            ojVar.a(z, str, aqVar);
        } catch (Exception e2) {
            l9.d().a(e2);
            IronLog.INTERNAL.error(e2.toString());
            aqVar.b("errMsg", e2.getMessage());
            ojVar.a(false, bVar.d, aqVar);
        }
    }
}
