package com.ss.android.token;

import X.0HH;
import X.AnonymousClass7lW;
import X.AnonymousClass9EH;
import X.AnonymousClass9I5;
import X.AnonymousClass9IF;
import X.AnonymousClass9IR;
import X.C11887xj;
import X.C148586a;
import X.C30738n2;
import X.C34768tX;
import X.C35208uF;
import X.C35318uQ;
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 android.util.Log;
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;
import org.json.JSONObject;

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

    public final void LIZ(Uri uri) {
        MethodCollector.i(7049);
        getContext().getContentResolver().notifyChange(uri, (ContentObserver) null);
        MethodCollector.o(7049);
    }

    static {
        Covode.recordClassIndex(49271);
    }

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

    private synchronized SharedPreferences LIZ() {
        MethodCollector.i(4645);
        SharedPreferences sharedPreferences = this.LJ;
        if (sharedPreferences == null) {
            int i = Build.VERSION.SDK_INT;
            Context applicationContext = getContext().getApplicationContext();
            if (AnonymousClass7lW.LIZJ) {
                if (applicationContext == null) {
                    applicationContext = AnonymousClass7lW.LIZ;
                }
            }
            SharedPreferences LIZ2 = C35318uQ.LIZ(applicationContext, LIZJ, 4);
            this.LJ = LIZ2;
            MethodCollector.o(4645);
            return LIZ2;
        }
        MethodCollector.o(4645);
        return sharedPreferences;
    }

    public boolean onCreate() {
        if (Logger.debug() && !C11887xj.LIZ(getContext())) {
            throw new IllegalAccessError("should be create in main process");
        } else if (LIZLLL != null) {
            return true;
        } else {
            try {
                Logger.debug();
                LIZ(getContext());
                return true;
            } catch (Exception e) {
                C35208uF.LIZ((Throwable) e);
                0HH.LIZ(e);
                return false;
            }
        }
    }

    private void LIZJ() {
        MethodCollector.i(4707);
        if (!this.LJI) {
            synchronized (this.LJII) {
                try {
                    if (!this.LJI) {
                        SharedPreferences LIZ2 = LIZ();
                        if (LIZ2 != null) {
                            for (Map.Entry next : LIZ2.getAll().entrySet()) {
                                String str = (String) next.getKey();
                                Object value = next.getValue();
                                if (value == null || str == null) {
                                    AnonymousClass9IF.LIZ("AuthTokenMultiProcessShared", "MultiProcessShareProvider loadValues key_ = " + str + " value_ = " + value);
                                    JSONObject jSONObject = new JSONObject();
                                    jSONObject.put("key_", str);
                                    jSONObject.put("value_", value);
                                    AnonymousClass9IR.LIZ("token_sp_error", jSONObject);
                                } else {
                                    this.LJFF.put(str, value);
                                }
                            }
                        }
                        this.LJI = true;
                    }
                } catch (Exception e) {
                    C35208uF.LIZ((Throwable) e);
                } catch (Throwable th) {
                    MethodCollector.o(4707);
                    throw th;
                }
            }
            MethodCollector.o(4707);
            return;
        }
        MethodCollector.o(4707);
    }

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

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

    public static boolean LIZ(Cursor cursor) {
        boolean z = true;
        if (cursor == null) {
            return true;
        }
        try {
            if (cursor.moveToFirst()) {
                if (cursor.getInt(0) <= 0) {
                    z = false;
                }
            }
            try {
                cursor.close();
            } catch (Exception e) {
                C35208uF.LIZ((Throwable) e);
            }
        } catch (Throwable th) {
            try {
                cursor.close();
            } catch (Exception e2) {
                C35208uF.LIZ((Throwable) e2);
            }
            throw th;
        }
        return z;
    }

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

            public final void run() {
                AuthTokenMultiProcessSharedProvider authTokenMultiProcessSharedProvider = AuthTokenMultiProcessSharedProvider.this;
                authTokenMultiProcessSharedProvider.LIZ(AuthTokenMultiProcessSharedProvider.LIZ(authTokenMultiProcessSharedProvider.getContext(), str, str2));
            }
        };
    }

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

    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 e) {
            C35208uF.LIZ((Throwable) e);
        }
        return context.getPackageName() + ".auth_token.SHARE_PROVIDER_AUTHORITY";
    }

    public static String LIZ(Cursor cursor, String str) {
        if (cursor == null) {
            return str;
        }
        try {
            if (cursor.moveToFirst()) {
                str = cursor.getString(0);
            }
            try {
                cursor.close();
            } catch (Exception e) {
                C35208uF.LIZ((Throwable) e);
            }
        } catch (Throwable th) {
            try {
                cursor.close();
            } catch (Exception e2) {
                C35208uF.LIZ((Throwable) e2);
            }
            throw th;
        }
        return str;
    }

    public Uri insert(Uri uri, ContentValues contentValues) {
        Runnable runnable;
        LIZJ();
        if (LIZIZ()) {
            return null;
        }
        if (LIZLLL.match(uri) == 65536) {
            try {
                ArrayList<Runnable> arrayList = new ArrayList<>();
                SharedPreferences.Editor editor = null;
                for (Map.Entry next : contentValues.valueSet()) {
                    Object value = next.getValue();
                    String str = (String) next.getKey();
                    AnonymousClass9IF.LIZ("AuthTokenMultiProcessShared", "MultiProcessShareProvider reallly insert key = " + str + " value = " + AnonymousClass9I5.LIZ(value));
                    if (str != null) {
                        if (value == null) {
                            this.LJFF.remove(str);
                        } else {
                            Object obj = this.LJFF.get(str);
                            if (obj == null || !obj.equals(value)) {
                                this.LJFF.put(str, value);
                            }
                        }
                        AnonymousClass9IF.LIZ("AuthTokenMultiProcessShared", "MultiProcessShareProvider reallly insert key = " + str + " value = " + AnonymousClass9I5.LIZ(value));
                        if (editor == null) {
                            editor = LIZ().edit();
                        }
                        if (value == null) {
                            editor.remove(str);
                        } else {
                            if (value instanceof String) {
                                editor.putString(str, (String) value);
                                runnable = LIZ(str, "string");
                            } else if (value instanceof Boolean) {
                                editor.putBoolean(str, ((Boolean) value).booleanValue());
                                runnable = LIZ(str, "boolean");
                            } else if (value instanceof Long) {
                                editor.putLong(str, ((Long) value).longValue());
                                runnable = LIZ(str, "long");
                            } else if (value instanceof Integer) {
                                editor.putInt(str, ((Integer) value).intValue());
                                runnable = LIZ(str, "integer");
                            } else if (value instanceof Float) {
                                editor.putFloat(str, ((Float) value).floatValue());
                                runnable = LIZ(str, "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) {
                C35208uF.LIZ((Throwable) e);
            }
        } else {
            throw new IllegalArgumentException("Unsupported uri ".concat(String.valueOf(uri)));
        }
    }

    public static synchronized C34768tX LIZ(Context context, String str, boolean z) {
        C34768tX r0;
        synchronized (AuthTokenMultiProcessSharedProvider.class) {
            MethodCollector.i(4927);
            if (LJIIIIZZ == null) {
                LJIIIIZZ = new C34768tX(context, str, z, (byte) 0);
            }
            r0 = LJIIIIZZ;
            MethodCollector.o(4927);
        }
        return r0;
    }

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

    public int delete(Uri uri, String str, String[] strArr) {
        LIZJ();
        if (LIZIZ()) {
            return 0;
        }
        if (LIZLLL.match(uri) == 65536) {
            try {
                this.LJFF.clear();
                LIZ().edit().clear().commit();
                LIZ(LIZ(getContext(), "key", "type"));
            } catch (Exception e) {
                C35208uF.LIZ((Throwable) e);
            }
            return 0;
        }
        throw new IllegalArgumentException("Unsupported uri ".concat(String.valueOf(uri)));
    }

    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) {
        MatrixCursor matrixCursor;
        int i;
        LIZJ();
        MatrixCursor matrixCursor2 = null;
        if (LIZIZ()) {
            return null;
        }
        if (LIZLLL.match(uri) == 65536) {
            try {
                int i2 = 1;
                if ("all".equals(uri.getPathSegments().get(1))) {
                    Map<String, ?> all = LIZ().getAll();
                    matrixCursor = 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 = matrixCursor.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 matrixCursor;
                    } catch (Exception e) {
                        e = e;
                        matrixCursor2 = matrixCursor;
                        C35208uF.LIZ((Throwable) e);
                        return matrixCursor2;
                    }
                } else {
                    String str5 = uri.getPathSegments().get(0);
                    if (!this.LJFF.containsKey(str5)) {
                        return null;
                    }
                    matrixCursor = new MatrixCursor(new String[]{str5});
                    Object obj = this.LJFF.get(str5);
                    MatrixCursor.RowBuilder newRow2 = matrixCursor.newRow();
                    if (obj instanceof Boolean) {
                        if (!((Boolean) obj).booleanValue()) {
                            i2 = 0;
                        }
                        obj = Integer.valueOf(i2);
                    }
                    AnonymousClass9IF.LIZ("AuthTokenMultiProcessShared", "MultiProcessShareProvider  get key = " + str5 + " value = " + AnonymousClass9I5.LIZ(obj) + " " + Log.getStackTraceString(new Exception()));
                    newRow2.add(obj);
                    return matrixCursor;
                }
            } catch (Exception e2) {
                e = e2;
                C35208uF.LIZ((Throwable) e);
                return matrixCursor2;
            }
        } else {
            throw new IllegalArgumentException("Unsupported uri ".concat(String.valueOf(uri)));
        }
    }
}
