package com.ss.android.pushmanager.setting;

import X.0HH;
import X.AnonymousClass7lW;
import X.AnonymousClass9EH;
import X.C05117jk;
import X.C148586a;
import X.C30738n2;
import X.C34238sg;
import X.C34738tU;
import X.C34748tV;
import X.C35318uQ;
import android.app.Application;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.UriMatcher;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import com.bytedance.common.utility.Logger;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PushMultiProcessSharedProvider extends ContentProvider {
    public static String LIZ;
    public static Uri LIZIZ;
    public static UriMatcher LIZJ;
    public static C34738tU LJII;
    public SharedPreferences LIZLLL;
    public Map<String, Object> LJ = new ConcurrentHashMap();
    public volatile boolean LJFF = false;
    public final Object LJI = new Object();

    static {
        Covode.recordClassIndex(48809);
    }

    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:9:0x0013 */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static boolean LIZ(android.database.Cursor r1, boolean r2) {
        /*
            if (r1 != 0) goto L_0x0003
            return r2
        L_0x0003:
            boolean r0 = r1.moveToFirst()     // Catch:{ all -> 0x0013 }
            if (r0 == 0) goto L_0x0013
            r0 = 0
            int r0 = r1.getInt(r0)     // Catch:{ all -> 0x0013 }
            if (r0 <= 0) goto L_0x0012
            r2 = 1
            goto L_0x0013
        L_0x0012:
            r2 = 0
        L_0x0013:
            r1.close()     // Catch:{ Exception -> 0x0016 }
        L_0x0016:
            return r2
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.pushmanager.setting.PushMultiProcessSharedProvider.LIZ(android.database.Cursor, boolean):boolean");
    }

    public static boolean LIZIZ() {
        if (!TextUtils.isEmpty(LIZ) && LIZJ != null) {
            return false;
        }
        return true;
    }

    public boolean onCreate() {
        if (!Logger.debug() || C05117jk.LIZ(getContext())) {
            C34238sg.LIZ((Application) LIZIZ(getContext()));
            if (LIZJ != null) {
                return true;
            }
            try {
                Logger.debug();
                LIZJ(getContext());
                return true;
            } catch (Exception e) {
                0HH.LIZ(e);
                return false;
            }
        } else {
            throw new IllegalAccessError("should be create in main process");
        }
    }

    private void LIZJ() {
        MethodCollector.i(9171);
        if (!this.LJFF) {
            synchronized (this.LJI) {
                try {
                    if (!this.LJFF) {
                        SharedPreferences LIZ2 = LIZ();
                        if (LIZ2 != null) {
                            for (Map.Entry next : LIZ2.getAll().entrySet()) {
                                String str = (String) next.getKey();
                                Object value = next.getValue();
                                if (!(str == null || value == null)) {
                                    this.LJ.put(str, value);
                                }
                            }
                        }
                        this.LJFF = true;
                    }
                } finally {
                    MethodCollector.o(9171);
                }
            }
            return;
        }
        MethodCollector.o(9171);
    }

    private synchronized SharedPreferences LIZ() {
        MethodCollector.i(9164);
        SharedPreferences sharedPreferences = this.LIZLLL;
        if (sharedPreferences == null) {
            int i = Build.VERSION.SDK_INT;
            SharedPreferences LIZ2 = C35318uQ.LIZ(LIZIZ(getContext()), "push_multi_process_config", 4);
            this.LIZLLL = LIZ2;
            MethodCollector.o(9164);
            return LIZ2;
        }
        MethodCollector.o(9164);
        return sharedPreferences;
    }

    public static Context LIZIZ(Context context) {
        Context applicationContext = context.getApplicationContext();
        if (AnonymousClass7lW.LIZJ && applicationContext == null) {
            return AnonymousClass7lW.LIZ;
        }
        return applicationContext;
    }

    public String getType(Uri uri) {
        LIZJ();
        return "vnd.android.cursor.item/vnd." + LIZ + ".item";
    }

    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:7:0x0010 */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static long LIZ(android.database.Cursor r3) {
        /*
            r1 = 0
            if (r3 != 0) goto L_0x0005
            return r1
        L_0x0005:
            boolean r0 = r3.moveToFirst()     // Catch:{ all -> 0x0010 }
            if (r0 == 0) goto L_0x0010
            r0 = 0
            long r1 = r3.getLong(r0)     // Catch:{ all -> 0x0010 }
        L_0x0010:
            r3.close()     // Catch:{ Exception -> 0x0013 }
        L_0x0013:
            return r1
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.pushmanager.setting.PushMultiProcessSharedProvider.LIZ(android.database.Cursor):long");
    }

    public static synchronized C34738tU LIZ(Context context) {
        C34738tU r0;
        synchronized (PushMultiProcessSharedProvider.class) {
            MethodCollector.i(9187);
            if (LJII == null) {
                LJII = new C34738tU(context, (byte) 0);
            }
            r0 = LJII;
            MethodCollector.o(9187);
        }
        return r0;
    }

    public static void LIZJ(Context context) {
        if (TextUtils.isEmpty(LIZ)) {
            LIZ = LIZ(context, PushMultiProcessSharedProvider.class.getName());
        }
        if (!TextUtils.isEmpty(LIZ)) {
            Logger.debug();
            UriMatcher uriMatcher = new UriMatcher(-1);
            LIZJ = uriMatcher;
            uriMatcher.addURI(LIZ, "*/*", 65536);
            LIZIZ = Uri.parse("content://" + LIZ);
            return;
        }
        throw new IllegalStateException("Must Set MultiProcessSharedProvider Authority");
    }

    private Runnable LIZ(final String str, final String str2) {
        return new Runnable() {
            static {
                Covode.recordClassIndex(48810);
            }

            public final void run() {
                MethodCollector.i(17529);
                PushMultiProcessSharedProvider pushMultiProcessSharedProvider = PushMultiProcessSharedProvider.this;
                pushMultiProcessSharedProvider.getContext().getContentResolver().notifyChange(PushMultiProcessSharedProvider.LIZ(pushMultiProcessSharedProvider.getContext(), str, str2), (ContentObserver) null);
                MethodCollector.o(17529);
            }
        };
    }

    public void attachInfo(Context context, ProviderInfo providerInfo) {
        if (providerInfo != null) {
            LIZ = providerInfo.authority;
        }
        super.attachInfo(context, providerInfo);
    }

    /* JADX WARNING: Can't wrap try/catch for region: R(6:2|3|(1:5)|6|7|8) */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:6:0x000e */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static int LIZ(android.database.Cursor r1, int r2) {
        /*
            if (r1 != 0) goto L_0x0003
            return r2
        L_0x0003:
            boolean r0 = r1.moveToFirst()     // Catch:{ all -> 0x000e }
            if (r0 == 0) goto L_0x000e
            r0 = 0
            int r2 = r1.getInt(r0)     // Catch:{ all -> 0x000e }
        L_0x000e:
            r1.close()     // Catch:{ Exception -> 0x0011 }
        L_0x0011:
            return r2
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.pushmanager.setting.PushMultiProcessSharedProvider.LIZ(android.database.Cursor, int):int");
    }

    public Uri insert(Uri uri, ContentValues contentValues) {
        Runnable runnable;
        String str;
        LIZJ();
        if (LIZIZ()) {
            return null;
        }
        if (LIZJ.match(uri) == 65536) {
            try {
                ArrayList<Runnable> arrayList = new ArrayList<>();
                SharedPreferences.Editor editor = null;
                for (Map.Entry next : contentValues.valueSet()) {
                    Object value = next.getValue();
                    String str2 = (String) next.getKey();
                    Logger.debug();
                    if (value == null) {
                        this.LJ.remove(str2);
                    } else {
                        Object obj = this.LJ.get(str2);
                        if (obj == null || !obj.equals(value)) {
                            this.LJ.put(str2, value);
                        }
                    }
                    if (Logger.debug()) {
                        if (("MultiProcessShareProvider reallly insert key = " + str2 + " value = " + value) != null) {
                            str = value.toString();
                        } else {
                            str = "null";
                        }
                        Logger.d("PushService", str);
                    }
                    if (editor == null) {
                        editor = LIZ().edit();
                    }
                    if (value == null) {
                        editor.remove(str2);
                    } else {
                        if (value instanceof String) {
                            editor.putString(str2, (String) value);
                            runnable = LIZ(str2, "string");
                        } else if (value instanceof Boolean) {
                            editor.putBoolean(str2, ((Boolean) value).booleanValue());
                            runnable = LIZ(str2, "boolean");
                        } else if (value instanceof Long) {
                            editor.putLong(str2, ((Long) value).longValue());
                            runnable = LIZ(str2, "long");
                        } else if (value instanceof Integer) {
                            editor.putInt(str2, ((Integer) value).intValue());
                            runnable = LIZ(str2, "integer");
                        } else if (value instanceof Float) {
                            editor.putFloat(str2, ((Float) value).floatValue());
                            runnable = LIZ(str2, "float");
                        } else {
                            throw new IllegalArgumentException("Unsupported type ".concat(String.valueOf(uri)));
                        }
                        if (runnable != null) {
                            arrayList.add(runnable);
                        }
                    }
                }
                if (editor == null) {
                    return null;
                }
                editor.apply();
                for (Runnable run : arrayList) {
                    run.run();
                }
                return null;
            } catch (Exception e) {
                0HH.LIZ(e);
            }
        } else {
            throw new IllegalArgumentException("Unsupported uri ".concat(String.valueOf(uri)));
        }
    }

    public static String LIZ(Context context, String str) {
        PackageInfo packageInfo;
        if (context == null || C148586a.LIZ(str)) {
            return null;
        }
        try {
            PackageManager packageManager = context.getPackageManager();
            String packageName = context.getPackageName();
            Context LIZ2 = AnonymousClass9EH.LJJIFFI.LIZ();
            if (C30738n2.LJIIIZ) {
                TextUtils.equals(packageName, LIZ2.getPackageName());
            }
            if (C30738n2.LJIIIZ) {
                TextUtils.equals(packageName, LIZ2.getPackageName());
            }
            if (C30738n2.LJIIIZ) {
                TextUtils.equals(packageName, LIZ2.getPackageName());
            }
            if (!C30738n2.LJIIIZ || !TextUtils.equals(packageName, LIZ2.getPackageName())) {
                packageInfo = packageManager.getPackageInfo(packageName, 8);
            } else {
                if (C30738n2.LJFF == null) {
                    C30738n2.LJFF = packageManager.getPackageInfo(packageName, 8);
                }
                packageInfo = C30738n2.LJFF;
            }
            for (ProviderInfo providerInfo : packageInfo.providers) {
                if (str.equals(providerInfo.name)) {
                    return providerInfo.authority;
                }
            }
        } catch (Exception unused) {
        }
        return context.getPackageName() + ".push.SHARE_PROVIDER_AUTHORITY";
    }

    /* JADX WARNING: Can't wrap try/catch for region: R(6:2|3|(1:5)|6|7|8) */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:6:0x000e */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static java.lang.String LIZ(android.database.Cursor r1, java.lang.String r2) {
        /*
            if (r1 != 0) goto L_0x0003
            return r2
        L_0x0003:
            boolean r0 = r1.moveToFirst()     // Catch:{ all -> 0x000e }
            if (r0 == 0) goto L_0x000e
            r0 = 0
            java.lang.String r2 = r1.getString(r0)     // Catch:{ all -> 0x000e }
        L_0x000e:
            r1.close()     // Catch:{ Exception -> 0x0011 }
        L_0x0011:
            return r2
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.pushmanager.setting.PushMultiProcessSharedProvider.LIZ(android.database.Cursor, java.lang.String):java.lang.String");
    }

    public int delete(Uri uri, String str, String[] strArr) {
        LIZJ();
        if (LIZIZ() || LIZJ.match(uri) == 65536) {
            return 0;
        }
        throw new IllegalArgumentException("Unsupported uri ".concat(String.valueOf(uri)));
    }

    public static final synchronized Uri LIZ(Context context, String str, String str2) {
        Uri build;
        synchronized (PushMultiProcessSharedProvider.class) {
            MethodCollector.i(9388);
            if (LIZIZ == null) {
                try {
                    Logger.debug();
                    LIZJ(context);
                } catch (Exception e) {
                    0HH.LIZ(e);
                    MethodCollector.o(9388);
                    return null;
                }
            }
            build = LIZIZ.buildUpon().appendPath(str).appendPath(str2).build();
            MethodCollector.o(9388);
        }
        return build;
    }

    public int update(Uri uri, ContentValues contentValues, String str, String[] strArr) {
        throw new UnsupportedOperationException();
    }

    public Cursor query(Uri uri, String[] strArr, String str, String[] strArr2, String str2) {
        Object obj;
        boolean z;
        int i;
        LIZJ();
        MatrixCursor matrixCursor = null;
        if (LIZIZ()) {
            return null;
        }
        if (LIZJ.match(uri) == 65536) {
            try {
                int i2 = 1;
                if ("all".equals(uri.getPathSegments().get(1))) {
                    Map<String, ?> all = LIZ().getAll();
                    MatrixCursor matrixCursor2 = new MatrixCursor(new String[]{"key_column", "value_column", "type_column"});
                    try {
                        for (Map.Entry next : all.entrySet()) {
                            String str3 = (String) next.getKey();
                            Object value = next.getValue();
                            MatrixCursor.RowBuilder newRow = matrixCursor2.newRow();
                            String str4 = "string";
                            if (!(value instanceof String)) {
                                if (value instanceof Boolean) {
                                    str4 = "boolean";
                                    if (((Boolean) value).booleanValue()) {
                                        i = 1;
                                    } else {
                                        i = 0;
                                    }
                                    value = Integer.valueOf(i);
                                } else if (value instanceof Integer) {
                                    str4 = "integer";
                                } else if (value instanceof Long) {
                                    str4 = "long";
                                } else if (value instanceof Float) {
                                    str4 = "float";
                                }
                            }
                            newRow.add(str3);
                            newRow.add(value);
                            newRow.add(str4);
                        }
                        return matrixCursor2;
                    } catch (Exception unused) {
                        matrixCursor = matrixCursor2;
                        return matrixCursor;
                    }
                } else {
                    String str5 = uri.getPathSegments().get(0);
                    boolean LIZ2 = C148586a.LIZ(str5, "current_app_state");
                    boolean LIZ3 = C148586a.LIZ(str5, "current_app_foreground");
                    if (!this.LJ.containsKey(str5) && !LIZ2 && !LIZ3) {
                        return null;
                    }
                    MatrixCursor matrixCursor3 = new MatrixCursor(new String[]{str5});
                    if (LIZ2) {
                        obj = Boolean.valueOf(C34748tV.LIZ().LIZ);
                    } else if (LIZ3) {
                        try {
                            C34748tV.LIZ();
                            if (!C34748tV.LIZIZ) {
                                z = true;
                            } else {
                                z = false;
                            }
                            obj = Boolean.valueOf(z);
                        } catch (Exception unused2) {
                            matrixCursor = matrixCursor3;
                            return matrixCursor;
                        }
                    } else {
                        obj = this.LJ.get(str5);
                    }
                    MatrixCursor.RowBuilder newRow2 = matrixCursor3.newRow();
                    if (obj instanceof Boolean) {
                        if (!((Boolean) obj).booleanValue()) {
                            i2 = 0;
                        }
                        obj = Integer.valueOf(i2);
                    }
                    Logger.debug();
                    newRow2.add(obj);
                    return matrixCursor3;
                }
            } catch (Exception unused3) {
                return matrixCursor;
            }
        } else {
            throw new IllegalArgumentException("Unsupported uri ".concat(String.valueOf(uri)));
        }
    }
}
