package s2;

import C4.f;
import I2.C0317a;
import L3.n;
import M3.h;
import X3.F;
import Z0.o;
import android.content.Context;
import android.content.SharedPreferences;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import m1.AbstractC2464a;
import n.C2579f;

/* JADX INFO: renamed from: s2.b, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class SharedPreferencesC2907b implements SharedPreferences {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final CopyOnWriteArrayList f24194b = new CopyOnWriteArrayList();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final L3.a f24195c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final L3.c f24196d;

    public SharedPreferencesC2907b(SharedPreferences sharedPreferences, L3.a aVar, L3.c cVar) {
        this.f24193a = sharedPreferences;
        this.f24195c = aVar;
        this.f24196d = cVar;
    }

    public static SharedPreferencesC2907b a(Context context, C0317a c0317a) {
        f fVarC;
        f fVarC2;
        String str = c0317a.f2580b;
        int i6 = Q3.a.f5981a;
        n.h(Q3.c.f5986b);
        if (!P3.a.a()) {
            n.f(new h(F.class, new M3.f[]{new M3.f(L3.c.class, 9)}, 8), true);
        }
        M3.a.a();
        Context applicationContext = context.getApplicationContext();
        R3.a aVar = new R3.a();
        aVar.f6077f = L3.b.a("AES256_SIV");
        if (applicationContext == null) {
            throw new IllegalArgumentException("need an Android context");
        }
        aVar.f6072a = applicationContext;
        aVar.f6073b = "__androidx_security_crypto_encrypted_prefs_key_keyset__";
        aVar.f6074c = "trakt_secure_store";
        String strN = A0.a.n("android-keystore://", str);
        if (!strN.startsWith("android-keystore://")) {
            throw new IllegalArgumentException("key URI must start with android-keystore://");
        }
        aVar.f6075d = strN;
        R3.b bVarA = aVar.a();
        synchronized (bVarA) {
            fVarC = bVarA.f6080a.c();
        }
        R3.a aVar2 = new R3.a();
        aVar2.f6077f = L3.b.a("AES256_GCM");
        aVar2.f6072a = applicationContext;
        aVar2.f6073b = "__androidx_security_crypto_encrypted_prefs_value_keyset__";
        aVar2.f6074c = "trakt_secure_store";
        String strN2 = A0.a.n("android-keystore://", str);
        if (!strN2.startsWith("android-keystore://")) {
            throw new IllegalArgumentException("key URI must start with android-keystore://");
        }
        aVar2.f6075d = strN2;
        R3.b bVarA2 = aVar2.a();
        synchronized (bVarA2) {
            fVarC2 = bVarA2.f6080a.c();
        }
        L3.c cVar = (L3.c) fVarC.A(L3.c.class);
        return new SharedPreferencesC2907b(applicationContext.getSharedPreferences("trakt_secure_store", 0), (L3.a) fVarC2.A(L3.a.class), cVar);
    }

    public static boolean d(String str) {
        return "__androidx_security_crypto_encrypted_prefs_key_keyset__".equals(str) || "__androidx_security_crypto_encrypted_prefs_value_keyset__".equals(str);
    }

    public final String b(String str) {
        if (str == null) {
            str = "__NULL__";
        }
        try {
            try {
                return new String(Y3.f.b(this.f24196d.a(str.getBytes(StandardCharsets.UTF_8), "trakt_secure_store".getBytes())), "US-ASCII");
            } catch (UnsupportedEncodingException e6) {
                throw new AssertionError(e6);
            }
        } catch (GeneralSecurityException e7) {
            throw new SecurityException("Could not encrypt key. " + e7.getMessage(), e7);
        }
    }

    public final Object c(String str) {
        String str2;
        if (d(str)) {
            throw new SecurityException(o.n(str, " is a reserved key for the encryption keyset."));
        }
        if (str == null) {
            str = "__NULL__";
        }
        try {
            String strB = b(str);
            String string = this.f24193a.getString(strB, null);
            if (string != null) {
                byte[] bArrA = Y3.f.a(string);
                L3.a aVar = this.f24195c;
                Charset charset = StandardCharsets.UTF_8;
                ByteBuffer byteBufferWrap = ByteBuffer.wrap(aVar.b(bArrA, strB.getBytes(charset)));
                byteBufferWrap.position(0);
                int i6 = byteBufferWrap.getInt();
                int i7 = i6 != 0 ? i6 != 1 ? i6 != 2 ? i6 != 3 ? i6 != 4 ? i6 != 5 ? 0 : 6 : 5 : 4 : 3 : 2 : 1;
                if (i7 == 0) {
                    throw new SecurityException("Unknown type ID for encrypted pref value: " + i6);
                }
                int iC = AbstractC2464a.c(i7);
                if (iC == 0) {
                    int i8 = byteBufferWrap.getInt();
                    ByteBuffer byteBufferSlice = byteBufferWrap.slice();
                    byteBufferWrap.limit(i8);
                    String string2 = charset.decode(byteBufferSlice).toString();
                    if (!string2.equals("__NULL__")) {
                        return string2;
                    }
                } else {
                    if (iC != 1) {
                        if (iC == 2) {
                            return Integer.valueOf(byteBufferWrap.getInt());
                        }
                        if (iC == 3) {
                            return Long.valueOf(byteBufferWrap.getLong());
                        }
                        if (iC == 4) {
                            return Float.valueOf(byteBufferWrap.getFloat());
                        }
                        if (iC == 5) {
                            return Boolean.valueOf(byteBufferWrap.get() != 0);
                        }
                        switch (i7) {
                            case 1:
                                str2 = "STRING";
                                break;
                            case 2:
                                str2 = "STRING_SET";
                                break;
                            case 3:
                                str2 = "INT";
                                break;
                            case 4:
                                str2 = "LONG";
                                break;
                            case 5:
                                str2 = "FLOAT";
                                break;
                            case 6:
                                str2 = "BOOLEAN";
                                break;
                            default:
                                str2 = "null";
                                break;
                        }
                        throw new SecurityException("Unhandled type for encrypted pref value: ".concat(str2));
                    }
                    C2579f c2579f = new C2579f(0);
                    while (byteBufferWrap.hasRemaining()) {
                        int i9 = byteBufferWrap.getInt();
                        ByteBuffer byteBufferSlice2 = byteBufferWrap.slice();
                        byteBufferSlice2.limit(i9);
                        byteBufferWrap.position(byteBufferWrap.position() + i9);
                        c2579f.add(StandardCharsets.UTF_8.decode(byteBufferSlice2).toString());
                    }
                    if (c2579f.f21426c != 1 || !"__NULL__".equals(c2579f.f21425b[0])) {
                        return c2579f;
                    }
                }
            }
            return null;
        } catch (GeneralSecurityException e6) {
            throw new SecurityException("Could not decrypt value. " + e6.getMessage(), e6);
        }
    }

    @Override // android.content.SharedPreferences
    public final boolean contains(String str) {
        if (d(str)) {
            throw new SecurityException(o.n(str, " is a reserved key for the encryption keyset."));
        }
        return this.f24193a.contains(b(str));
    }

    @Override // android.content.SharedPreferences
    public final SharedPreferences.Editor edit() {
        return new SharedPreferencesEditorC2906a(this, this.f24193a.edit());
    }

    @Override // android.content.SharedPreferences
    public final Map getAll() {
        HashMap map = new HashMap();
        for (Map.Entry<String, ?> entry : this.f24193a.getAll().entrySet()) {
            if (!d(entry.getKey())) {
                try {
                    String str = new String(this.f24196d.b(Y3.f.a(entry.getKey()), "trakt_secure_store".getBytes()), StandardCharsets.UTF_8);
                    if (str.equals("__NULL__")) {
                        str = null;
                    }
                    map.put(str, c(str));
                } catch (GeneralSecurityException e6) {
                    throw new SecurityException("Could not decrypt key. " + e6.getMessage(), e6);
                }
            }
        }
        return map;
    }

    @Override // android.content.SharedPreferences
    public final boolean getBoolean(String str, boolean z6) {
        Object objC = c(str);
        return objC instanceof Boolean ? ((Boolean) objC).booleanValue() : z6;
    }

    @Override // android.content.SharedPreferences
    public final float getFloat(String str, float f6) {
        Object objC = c(str);
        return objC instanceof Float ? ((Float) objC).floatValue() : f6;
    }

    @Override // android.content.SharedPreferences
    public final int getInt(String str, int i6) {
        Object objC = c(str);
        return objC instanceof Integer ? ((Integer) objC).intValue() : i6;
    }

    @Override // android.content.SharedPreferences
    public final long getLong(String str, long j4) {
        Object objC = c(str);
        return objC instanceof Long ? ((Long) objC).longValue() : j4;
    }

    @Override // android.content.SharedPreferences
    public final String getString(String str, String str2) {
        Object objC = c(str);
        return objC instanceof String ? (String) objC : str2;
    }

    @Override // android.content.SharedPreferences
    public final Set getStringSet(String str, Set set) {
        Object objC = c(str);
        Set c2579f = objC instanceof Set ? (Set) objC : new C2579f(0);
        return c2579f.size() > 0 ? c2579f : set;
    }

    @Override // android.content.SharedPreferences
    public final void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
        this.f24194b.add(onSharedPreferenceChangeListener);
    }

    @Override // android.content.SharedPreferences
    public final void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
        this.f24194b.remove(onSharedPreferenceChangeListener);
    }
}
