package androidx.security.crypto;

import android.content.Context;
import android.content.SharedPreferences;
import com.google.android.gms.measurement.internal.u1;
import fe.h;
import ja.e;
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 sa.g;

/* JADX INFO: loaded from: classes.dex */
public final class b implements SharedPreferences {

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final String f4235c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final da.a f4236d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final da.b f4237e;

    public b(String str, SharedPreferences sharedPreferences, da.a aVar, da.b bVar) {
        this.f4235c = str;
        this.f4233a = sharedPreferences;
        this.f4236d = aVar;
        this.f4237e = bVar;
    }

    public static b a(Context context, String str, h hVar) throws GeneralSecurityException {
        EncryptedSharedPreferences$PrefKeyEncryptionScheme encryptedSharedPreferences$PrefKeyEncryptionScheme = EncryptedSharedPreferences$PrefKeyEncryptionScheme.f4223a;
        EncryptedSharedPreferences$PrefValueEncryptionScheme encryptedSharedPreferences$PrefValueEncryptionScheme = EncryptedSharedPreferences$PrefValueEncryptionScheme.f4225a;
        String str2 = (String) hVar.f26080b;
        e.a();
        ea.a.a();
        Context applicationContext = context.getApplicationContext();
        la.a aVar = new la.a();
        aVar.f30191f = encryptedSharedPreferences$PrefKeyEncryptionScheme.a();
        if (applicationContext == null) {
            throw new IllegalArgumentException("need an Android context");
        }
        aVar.f30186a = applicationContext;
        aVar.f30187b = "__androidx_security_crypto_encrypted_prefs_key_keyset__";
        aVar.f30188c = str;
        String strI = defpackage.e.i("android-keystore://", str2);
        if (!strI.startsWith("android-keystore://")) {
            throw new IllegalArgumentException("key URI must start with android-keystore://");
        }
        aVar.f30189d = strI;
        da.h hVarA = aVar.a().a();
        la.a aVar2 = new la.a();
        aVar2.f30191f = encryptedSharedPreferences$PrefValueEncryptionScheme.a();
        aVar2.f30186a = applicationContext;
        aVar2.f30187b = "__androidx_security_crypto_encrypted_prefs_value_keyset__";
        aVar2.f30188c = str;
        String strI2 = defpackage.e.i("android-keystore://", str2);
        if (!strI2.startsWith("android-keystore://")) {
            throw new IllegalArgumentException("key URI must start with android-keystore://");
        }
        aVar2.f30189d = strI2;
        da.h hVarA2 = aVar2.a().a();
        da.b bVar = (da.b) hVarA.f(da.b.class);
        return new b(str, applicationContext.getSharedPreferences(str, 0), (da.a) hVarA2.f(da.a.class), bVar);
    }

    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(g.b(this.f4237e.a(str.getBytes(StandardCharsets.UTF_8), this.f4235c.getBytes())), "US-ASCII");
            } catch (UnsupportedEncodingException e10) {
                throw new AssertionError(e10);
            }
        } catch (GeneralSecurityException e11) {
            throw new SecurityException("Could not encrypt key. " + e11.getMessage(), e11);
        }
    }

    public final Object c(String str) {
        if (d(str)) {
            throw new SecurityException(u1.j(str, " is a reserved key for the encryption keyset."));
        }
        if (str == null) {
            str = "__NULL__";
        }
        try {
            String strB = b(str);
            String string = this.f4233a.getString(strB, null);
            if (string == null) {
                return null;
            }
            byte[] bArrA = g.a(string);
            da.a aVar = this.f4236d;
            Charset charset = StandardCharsets.UTF_8;
            ByteBuffer byteBufferWrap = ByteBuffer.wrap(aVar.b(bArrA, strB.getBytes(charset)));
            byteBufferWrap.position(0);
            int i10 = byteBufferWrap.getInt();
            EncryptedSharedPreferences$EncryptedType encryptedSharedPreferences$EncryptedType = i10 != 0 ? i10 != 1 ? i10 != 2 ? i10 != 3 ? i10 != 4 ? i10 != 5 ? null : EncryptedSharedPreferences$EncryptedType.BOOLEAN : EncryptedSharedPreferences$EncryptedType.FLOAT : EncryptedSharedPreferences$EncryptedType.LONG : EncryptedSharedPreferences$EncryptedType.INT : EncryptedSharedPreferences$EncryptedType.STRING_SET : EncryptedSharedPreferences$EncryptedType.STRING;
            if (encryptedSharedPreferences$EncryptedType == null) {
                throw new SecurityException("Unknown type ID for encrypted pref value: " + i10);
            }
            int iOrdinal = encryptedSharedPreferences$EncryptedType.ordinal();
            if (iOrdinal == 0) {
                int i11 = byteBufferWrap.getInt();
                ByteBuffer byteBufferSlice = byteBufferWrap.slice();
                byteBufferWrap.limit(i11);
                String string2 = charset.decode(byteBufferSlice).toString();
                if (string2.equals("__NULL__")) {
                    return null;
                }
                return string2;
            }
            if (iOrdinal != 1) {
                if (iOrdinal == 2) {
                    return Integer.valueOf(byteBufferWrap.getInt());
                }
                if (iOrdinal == 3) {
                    return Long.valueOf(byteBufferWrap.getLong());
                }
                if (iOrdinal == 4) {
                    return Float.valueOf(byteBufferWrap.getFloat());
                }
                if (iOrdinal == 5) {
                    return Boolean.valueOf(byteBufferWrap.get() != 0);
                }
                throw new SecurityException("Unhandled type for encrypted pref value: " + encryptedSharedPreferences$EncryptedType);
            }
            androidx.collection.g gVar = new androidx.collection.g(0);
            while (byteBufferWrap.hasRemaining()) {
                int i12 = byteBufferWrap.getInt();
                ByteBuffer byteBufferSlice2 = byteBufferWrap.slice();
                byteBufferSlice2.limit(i12);
                byteBufferWrap.position(byteBufferWrap.position() + i12);
                gVar.add(StandardCharsets.UTF_8.decode(byteBufferSlice2).toString());
            }
            if (gVar.f935c == 1 && "__NULL__".equals(gVar.f934b[0])) {
                return null;
            }
            return gVar;
        } catch (GeneralSecurityException e10) {
            throw new SecurityException("Could not decrypt value. " + e10.getMessage(), e10);
        }
    }

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

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

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

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

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

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

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

    @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 gVar = objC instanceof Set ? (Set) objC : new androidx.collection.g(0);
        return gVar.size() > 0 ? gVar : set;
    }

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

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