package W1;

import A1.y;
import B3.f;
import H3.n;
import I3.h;
import T3.F;
import Z.g;
import android.content.Context;
import android.content.SharedPreferences;
import d5.AbstractC0844a;
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 n.U0;
import u.AbstractC1563h;

/* 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 f7071a;

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

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

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

    public b(SharedPreferences sharedPreferences, H3.a aVar, H3.c cVar) {
        this.f7071a = sharedPreferences;
        this.f7073c = aVar;
        this.f7074d = cVar;
    }

    public static b a(Context context, f fVar) {
        y yVarC;
        String str = fVar.f975b;
        int i = M3.a.f4203a;
        n.g(M3.c.f4208b);
        if (!L3.a.a()) {
            n.e(new h(F.class, new I3.f[]{new I3.f(9, H3.c.class)}, 8), true);
        }
        I3.a.a();
        Context applicationContext = context.getApplicationContext();
        U0 u02 = new U0(1);
        u02.f14798f = H3.b.a("AES256_SIV");
        if (applicationContext == null) {
            throw new IllegalArgumentException("need an Android context");
        }
        u02.f14793a = applicationContext;
        u02.f14794b = "__androidx_security_crypto_encrypted_prefs_key_keyset__";
        u02.f14795c = "com.martsnew.data";
        String strX = AbstractC0844a.x("android-keystore://", str);
        if (!strX.startsWith("android-keystore://")) {
            throw new IllegalArgumentException("key URI must start with android-keystore://");
        }
        u02.f14796d = strX;
        N3.a aVarA = u02.a();
        synchronized (aVarA) {
            yVarC = aVarA.f4737a.c();
        }
        U0 u03 = new U0(1);
        u03.f14798f = H3.b.a("AES256_GCM");
        u03.f14793a = applicationContext;
        u03.f14794b = "__androidx_security_crypto_encrypted_prefs_value_keyset__";
        u03.f14795c = "com.martsnew.data";
        String strX2 = AbstractC0844a.x("android-keystore://", str);
        if (!strX2.startsWith("android-keystore://")) {
            throw new IllegalArgumentException("key URI must start with android-keystore://");
        }
        u03.f14796d = strX2;
        y yVarA = u03.a().a();
        return new b(applicationContext.getSharedPreferences("com.martsnew.data", 0), (H3.a) yVarA.s(H3.a.class), (H3.c) yVarC.s(H3.c.class));
    }

    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(U3.f.b(this.f7074d.a(str.getBytes(StandardCharsets.UTF_8), "com.martsnew.data".getBytes())), "US-ASCII");
            } catch (UnsupportedEncodingException e8) {
                throw new AssertionError(e8);
            }
        } catch (GeneralSecurityException e9) {
            throw new SecurityException("Could not encrypt key. " + e9.getMessage(), e9);
        }
    }

    public final Object c(String str) {
        String str2;
        if (d(str)) {
            throw new SecurityException(AbstractC0844a.l(str, " is a reserved key for the encryption keyset."));
        }
        if (str == null) {
            str = "__NULL__";
        }
        try {
            String strB = b(str);
            String string = this.f7071a.getString(strB, null);
            if (string != null) {
                byte[] bArrA = U3.f.a(string);
                H3.a aVar = this.f7073c;
                Charset charset = StandardCharsets.UTF_8;
                ByteBuffer byteBufferWrap = ByteBuffer.wrap(aVar.b(bArrA, strB.getBytes(charset)));
                byteBufferWrap.position(0);
                int i = byteBufferWrap.getInt();
                int i5 = i != 0 ? i != 1 ? i != 2 ? i != 3 ? i != 4 ? i != 5 ? 0 : 6 : 5 : 4 : 3 : 2 : 1;
                if (i5 == 0) {
                    throw new SecurityException("Unknown type ID for encrypted pref value: " + i);
                }
                int iC = AbstractC1563h.c(i5);
                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 (i5) {
                            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 g.STRING_SET_FIELD_NUMBER /* 6 */:
                                str2 = "BOOLEAN";
                                break;
                            default:
                                str2 = "null";
                                break;
                        }
                        throw new SecurityException("Unhandled type for encrypted pref value: ".concat(str2));
                    }
                    q.f fVar = new q.f(0);
                    while (byteBufferWrap.hasRemaining()) {
                        int i9 = byteBufferWrap.getInt();
                        ByteBuffer byteBufferSlice2 = byteBufferWrap.slice();
                        byteBufferSlice2.limit(i9);
                        byteBufferWrap.position(byteBufferWrap.position() + i9);
                        fVar.add(StandardCharsets.UTF_8.decode(byteBufferSlice2).toString());
                    }
                    if (fVar.f16181s != 1 || !"__NULL__".equals(fVar.f16180r[0])) {
                        return fVar;
                    }
                }
            }
            return null;
        } catch (GeneralSecurityException e8) {
            throw new SecurityException("Could not decrypt value. " + e8.getMessage(), e8);
        }
    }

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

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

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

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

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

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

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

    @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 fVar = objC instanceof Set ? (Set) objC : new q.f(0);
        return fVar.size() > 0 ? fVar : set;
    }

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

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