package defpackage;

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;

/* renamed from: ːᵺ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class SharedPreferencesC1998 implements SharedPreferences {

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public final InterfaceC4799 f8019;

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final String f8020;

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final SharedPreferences f8021;

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final CopyOnWriteArrayList f8022 = new CopyOnWriteArrayList();

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public final InterfaceC2556 f8023;

    public SharedPreferencesC1998(String str, SharedPreferences sharedPreferences, InterfaceC2556 interfaceC2556, InterfaceC4799 interfaceC4799) {
        this.f8020 = str;
        this.f8021 = sharedPreferences;
        this.f8023 = interfaceC2556;
        this.f8019 = interfaceC4799;
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public static SharedPreferencesC1998 m5517(Context context, String str, C5217 c5217) throws GeneralSecurityException {
        ᵧˉʙ r3;
        String str2 = c5217.f19472;
        AbstractC0104.m1182();
        AbstractC3823.m8466();
        Context applicationContext = context.getApplicationContext();
        C1818 c1818 = new C1818();
        c1818.f7384 = ˊˉʤﹳ.ˌʾˀ("AES256_SIV");
        if (applicationContext == null) {
            ᵹˉᵞ.ˌᵜᵹᵩ("need an Android context");
            return null;
        }
        c1818.f7391 = applicationContext;
        c1818.f7387 = "__androidx_security_crypto_encrypted_prefs_key_keyset__";
        c1818.f7390 = str;
        String strM9562 = AbstractC4486.m9562("android-keystore://", str2);
        if (!strM9562.startsWith("android-keystore://")) {
            ᵹˉᵞ.ˌᵜᵹᵩ("key URI must start with android-keystore://");
            return null;
        }
        c1818.f7388 = strM9562;
        C2931 c2931M5166 = c1818.m5166();
        synchronized (c2931M5166) {
            r3 = ((ˊʘ) c2931M5166.f11366).ᵧـˌᵎ();
        }
        C1818 c18182 = new C1818();
        c18182.f7384 = ˊˉʤﹳ.ˌʾˀ("AES256_GCM");
        c18182.f7391 = applicationContext;
        c18182.f7387 = "__androidx_security_crypto_encrypted_prefs_value_keyset__";
        c18182.f7390 = str;
        String strM95622 = AbstractC4486.m9562("android-keystore://", str2);
        if (!strM95622.startsWith("android-keystore://")) {
            ᵹˉᵞ.ˌᵜᵹᵩ("key URI must start with android-keystore://");
            return null;
        }
        c18182.f7388 = strM95622;
        ᵧˉʙ r7 = c18182.m5166().m7134();
        InterfaceC4799 interfaceC4799 = (InterfaceC4799) r3.ˑᵥˁ(InterfaceC4799.class);
        return new SharedPreferencesC1998(str, applicationContext.getSharedPreferences(str, 0), (InterfaceC2556) r7.ˑᵥˁ(InterfaceC2556.class), interfaceC4799);
    }

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public static boolean m5518(String str) {
        return "__androidx_security_crypto_encrypted_prefs_key_keyset__".equals(str) || "__androidx_security_crypto_encrypted_prefs_value_keyset__".equals(str);
    }

    @Override // android.content.SharedPreferences
    public final boolean contains(String str) {
        if (m5518(str)) {
            throw new SecurityException(AbstractC3915.m8598(str, " is a reserved key for the encryption keyset."));
        }
        return this.f8021.contains(m5520(str));
    }

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

    @Override // android.content.SharedPreferences
    public final Map getAll() {
        HashMap map = new HashMap();
        for (Map.Entry<String, ?> entry : this.f8021.getAll().entrySet()) {
            if (!m5518(entry.getKey())) {
                try {
                    String str = new String(this.f8019.mo4281(AbstractC1870.m5275(entry.getKey()), this.f8020.getBytes()), StandardCharsets.UTF_8);
                    String str2 = str.equals("__NULL__") ? null : str;
                    map.put(str2, m5519(str2));
                } catch (GeneralSecurityException e) {
                    C4069.m8937("Could not decrypt key. ", e.getMessage(), e);
                    return null;
                }
            }
        }
        return map;
    }

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

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

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

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

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

    @Override // android.content.SharedPreferences
    public final Set getStringSet(String str, Set set) {
        Object objM5519 = m5519(str);
        Set c0277 = objM5519 instanceof Set ? (Set) objM5519 : new C0277(0);
        return c0277.size() > 0 ? c0277 : set;
    }

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

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

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final Object m5519(String str) {
        String str2;
        if (m5518(str)) {
            throw new SecurityException(AbstractC3915.m8598(str, " is a reserved key for the encryption keyset."));
        }
        if (str == null) {
            str = "__NULL__";
        }
        try {
            String strM5520 = m5520(str);
            String string = this.f8021.getString(strM5520, null);
            if (string != null) {
                byte[] bArrM5275 = AbstractC1870.m5275(string);
                InterfaceC2556 interfaceC2556 = this.f8023;
                Charset charset = StandardCharsets.UTF_8;
                ByteBuffer byteBufferWrap = ByteBuffer.wrap(interfaceC2556.mo2412(bArrM5275, strM5520.getBytes(charset)));
                byteBufferWrap.position(0);
                int i = byteBufferWrap.getInt();
                int i2 = i != 0 ? i != 1 ? i != 2 ? i != 3 ? i != 4 ? i != 5 ? 0 : 6 : 5 : 4 : 3 : 2 : 1;
                if (i2 == 0) {
                    throw new SecurityException("Unknown type ID for encrypted pref value: " + i);
                }
                int iM4088 = AbstractC1327.m4088(i2);
                if (iM4088 == 0) {
                    int i3 = byteBufferWrap.getInt();
                    ByteBuffer byteBufferSlice = byteBufferWrap.slice();
                    byteBufferWrap.limit(i3);
                    String string2 = charset.decode(byteBufferSlice).toString();
                    if (!string2.equals("__NULL__")) {
                        return string2;
                    }
                } else {
                    if (iM4088 != 1) {
                        if (iM4088 == 2) {
                            return Integer.valueOf(byteBufferWrap.getInt());
                        }
                        if (iM4088 == 3) {
                            return Long.valueOf(byteBufferWrap.getLong());
                        }
                        if (iM4088 == 4) {
                            return Float.valueOf(byteBufferWrap.getFloat());
                        }
                        if (iM4088 == 5) {
                            return Boolean.valueOf(byteBufferWrap.get() != 0);
                        }
                        switch (i2) {
                            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 C1339.STRING_SET_FIELD_NUMBER /* 6 */:
                                str2 = "BOOLEAN";
                                break;
                            default:
                                str2 = "null";
                                break;
                        }
                        throw new SecurityException("Unhandled type for encrypted pref value: ".concat(str2));
                    }
                    C0277 c0277 = new C0277(0);
                    while (byteBufferWrap.hasRemaining()) {
                        int i4 = byteBufferWrap.getInt();
                        ByteBuffer byteBufferSlice2 = byteBufferWrap.slice();
                        byteBufferSlice2.limit(i4);
                        byteBufferWrap.position(byteBufferWrap.position() + i4);
                        c0277.add(StandardCharsets.UTF_8.decode(byteBufferSlice2).toString());
                    }
                    if (c0277.f1875 != 1 || !"__NULL__".equals(c0277.f1873[0])) {
                        return c0277;
                    }
                }
            }
            return null;
        } catch (GeneralSecurityException e) {
            C4069.m8937("Could not decrypt value. ", e.getMessage(), e);
            return null;
        }
    }

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final String m5520(String str) {
        if (str == null) {
            str = "__NULL__";
        }
        try {
            try {
                return new String(AbstractC1870.m5276(this.f8019.mo4280(str.getBytes(StandardCharsets.UTF_8), this.f8020.getBytes())), "US-ASCII");
            } catch (UnsupportedEncodingException e) {
                throw new AssertionError(e);
            }
        } catch (GeneralSecurityException e2) {
            C4069.m8937("Could not encrypt key. ", e2.getMessage(), e2);
            return null;
        }
    }
}
