package p037.p113.p114;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.ArraySet;
import android.util.Pair;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import p138.p139.p143.p144.C2803;
import p138.p176.p278.p279.C5184;
import p138.p176.p278.p279.InterfaceC5176;
import p138.p176.p278.p279.InterfaceC5179;
import p138.p176.p278.p279.p280.C5203;
import p138.p176.p278.p279.p281.C5216;
import p138.p176.p278.p279.p282.C5220;
import p138.p176.p278.p279.p284.p285.C5233;
import p138.p176.p278.p279.p287.C5441;
import p138.p176.p278.p279.p290.C5506;

/* JADX INFO: renamed from: މ.ޒ.֏.֏, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public final class SharedPreferencesC2435 implements SharedPreferences {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final SharedPreferences f7709;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final List<SharedPreferences.OnSharedPreferenceChangeListener> f7710 = new ArrayList();

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final String f7711;

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final InterfaceC5176 f7712;

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public final InterfaceC5179 f7713;

    /* JADX INFO: renamed from: މ.ޒ.֏.֏$֏, reason: contains not printable characters */
    public static final class SharedPreferencesEditorC2436 implements SharedPreferences.Editor {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final SharedPreferencesC2435 f7714;

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        public final SharedPreferences.Editor f7715;

        /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
        public AtomicBoolean f7717 = new AtomicBoolean(false);

        /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
        public final List<String> f7716 = new CopyOnWriteArrayList();

        public SharedPreferencesEditorC2436(SharedPreferencesC2435 sharedPreferencesC2435, SharedPreferences.Editor editor) {
            this.f7714 = sharedPreferencesC2435;
            this.f7715 = editor;
        }

        @Override // android.content.SharedPreferences.Editor
        public void apply() {
            this.f7715.apply();
            m4520();
        }

        @Override // android.content.SharedPreferences.Editor
        public SharedPreferences.Editor clear() {
            this.f7717.set(true);
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        public boolean commit() {
            if (this.f7717.getAndSet(false)) {
                for (String str : ((HashMap) this.f7714.getAll()).keySet()) {
                    if (!this.f7716.contains(str) && !this.f7714.m4519(str)) {
                        this.f7715.remove(this.f7714.m4517(str));
                    }
                }
            }
            try {
                return this.f7715.commit();
            } finally {
                m4520();
                this.f7716.clear();
            }
        }

        @Override // android.content.SharedPreferences.Editor
        public SharedPreferences.Editor putBoolean(String str, boolean z) {
            ByteBuffer byteBufferAllocate = ByteBuffer.allocate(5);
            byteBufferAllocate.putInt(EnumC2437.BOOLEAN.f7725);
            byteBufferAllocate.put(z ? (byte) 1 : (byte) 0);
            m4521(str, byteBufferAllocate.array());
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        public SharedPreferences.Editor putFloat(String str, float f) {
            ByteBuffer byteBufferAllocate = ByteBuffer.allocate(8);
            byteBufferAllocate.putInt(EnumC2437.FLOAT.f7725);
            byteBufferAllocate.putFloat(f);
            m4521(str, byteBufferAllocate.array());
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        public SharedPreferences.Editor putInt(String str, int i) {
            ByteBuffer byteBufferAllocate = ByteBuffer.allocate(8);
            byteBufferAllocate.putInt(EnumC2437.INT.f7725);
            byteBufferAllocate.putInt(i);
            m4521(str, byteBufferAllocate.array());
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        public SharedPreferences.Editor putLong(String str, long j) {
            ByteBuffer byteBufferAllocate = ByteBuffer.allocate(12);
            byteBufferAllocate.putInt(EnumC2437.LONG.f7725);
            byteBufferAllocate.putLong(j);
            m4521(str, byteBufferAllocate.array());
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        public SharedPreferences.Editor putString(String str, String str2) {
            if (str2 == null) {
                str2 = "__NULL__";
            }
            byte[] bytes = str2.getBytes(StandardCharsets.UTF_8);
            int length = bytes.length;
            ByteBuffer byteBufferAllocate = ByteBuffer.allocate(length + 8);
            byteBufferAllocate.putInt(EnumC2437.STRING.f7725);
            byteBufferAllocate.putInt(length);
            byteBufferAllocate.put(bytes);
            m4521(str, byteBufferAllocate.array());
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        public SharedPreferences.Editor putStringSet(String str, Set<String> set) {
            Set set2 = set;
            if (set == null) {
                ArraySet arraySet = new ArraySet();
                arraySet.add("__NULL__");
                set2 = arraySet;
            }
            ArrayList<byte[]> arrayList = new ArrayList(set2.size());
            int size = set2.size() * 4;
            Iterator it = set2.iterator();
            while (it.hasNext()) {
                byte[] bytes = ((String) it.next()).getBytes(StandardCharsets.UTF_8);
                arrayList.add(bytes);
                size += bytes.length;
            }
            ByteBuffer byteBufferAllocate = ByteBuffer.allocate(size + 4);
            byteBufferAllocate.putInt(EnumC2437.STRING_SET.f7725);
            for (byte[] bArr : arrayList) {
                byteBufferAllocate.putInt(bArr.length);
                byteBufferAllocate.put(bArr);
            }
            m4521(str, byteBufferAllocate.array());
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        public SharedPreferences.Editor remove(String str) {
            if (this.f7714.m4519(str)) {
                throw new SecurityException(C2803.m4879(str, " is a reserved key for the encryption keyset."));
            }
            this.f7715.remove(this.f7714.m4517(str));
            this.f7716.remove(str);
            return this;
        }

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final void m4520() {
            for (SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener : this.f7714.f7710) {
                Iterator<String> it = this.f7716.iterator();
                while (it.hasNext()) {
                    onSharedPreferenceChangeListener.onSharedPreferenceChanged(this.f7714, it.next());
                }
            }
        }

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final void m4521(String str, byte[] bArr) {
            if (this.f7714.m4519(str)) {
                throw new SecurityException(C2803.m4879(str, " is a reserved key for the encryption keyset."));
            }
            this.f7716.add(str);
            if (str == null) {
                str = "__NULL__";
            }
            try {
                SharedPreferencesC2435 sharedPreferencesC2435 = this.f7714;
                String strM4517 = sharedPreferencesC2435.m4517(str);
                Pair pair = new Pair(strM4517, C5506.m8874(sharedPreferencesC2435.f7712.mo8647(bArr, strM4517.getBytes(StandardCharsets.UTF_8))));
                this.f7715.putString((String) pair.first, (String) pair.second);
            } catch (GeneralSecurityException e) {
                StringBuilder sbM4889 = C2803.m4889("Could not encrypt data: ");
                sbM4889.append(e.getMessage());
                throw new SecurityException(sbM4889.toString(), e);
            }
        }
    }

    /* JADX INFO: renamed from: މ.ޒ.֏.֏$ؠ, reason: contains not printable characters */
    public enum EnumC2437 {
        STRING(0),
        STRING_SET(1),
        INT(2),
        LONG(3),
        FLOAT(4),
        BOOLEAN(5);


        /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
        public final int f7725;

        EnumC2437(int i) {
            this.f7725 = i;
        }

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public static EnumC2437 m4522(int i) {
            if (i == 0) {
                return STRING;
            }
            if (i == 1) {
                return STRING_SET;
            }
            if (i == 2) {
                return INT;
            }
            if (i == 3) {
                return LONG;
            }
            if (i == 4) {
                return FLOAT;
            }
            if (i != 5) {
                return null;
            }
            return BOOLEAN;
        }
    }

    /* JADX INFO: renamed from: މ.ޒ.֏.֏$ހ, reason: contains not printable characters */
    public enum EnumC2438 {
        AES256_SIV(C5220.f15444);


        /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
        public final C5441 f7728;

        EnumC2438(C5441 c5441) {
            this.f7728 = c5441;
        }
    }

    /* JADX INFO: renamed from: މ.ޒ.֏.֏$ށ, reason: contains not printable characters */
    public enum EnumC2439 {
        AES256_GCM(C5203.f15436);


        /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
        public final C5441 f7731;

        EnumC2439(C5441 c5441) {
            this.f7731 = c5441;
        }
    }

    public SharedPreferencesC2435(String str, String str2, SharedPreferences sharedPreferences, InterfaceC5176 interfaceC5176, InterfaceC5179 interfaceC5179) {
        this.f7711 = str;
        this.f7709 = sharedPreferences;
        this.f7712 = interfaceC5176;
        this.f7713 = interfaceC5179;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static SharedPreferences m4516(String str, String str2, Context context, EnumC2438 enumC2438, EnumC2439 enumC2439) throws GeneralSecurityException {
        C5216.m8710();
        C5233.C5235 c5235 = new C5233.C5235();
        c5235.f15462 = enumC2438.f7728;
        c5235.m8716(context, "__androidx_security_crypto_encrypted_prefs_key_keyset__", str);
        String str3 = "android-keystore://" + str2;
        if (!str3.startsWith("android-keystore://")) {
            throw new IllegalArgumentException("key URI must start with android-keystore://");
        }
        c5235.f15460 = str3;
        C5184 c5184M8713 = new C5233(c5235, null).m8713();
        C5233.C5235 c52352 = new C5233.C5235();
        c52352.f15462 = enumC2439.f7731;
        c52352.m8716(context, "__androidx_security_crypto_encrypted_prefs_value_keyset__", str);
        String str4 = "android-keystore://" + str2;
        if (!str4.startsWith("android-keystore://")) {
            throw new IllegalArgumentException("key URI must start with android-keystore://");
        }
        c52352.f15460 = str4;
        C5184 c5184M87132 = new C5233(c52352, null).m8713();
        return new SharedPreferencesC2435(str, str2, context.getSharedPreferences(str, 0), (InterfaceC5176) c5184M87132.m8668(InterfaceC5176.class), (InterfaceC5179) c5184M8713.m8668(InterfaceC5179.class));
    }

    @Override // android.content.SharedPreferences
    public boolean contains(String str) {
        if (m4519(str)) {
            throw new SecurityException(C2803.m4879(str, " is a reserved key for the encryption keyset."));
        }
        return this.f7709.contains(m4517(str));
    }

    @Override // android.content.SharedPreferences
    public SharedPreferences.Editor edit() {
        return new SharedPreferencesEditorC2436(this, this.f7709.edit());
    }

    @Override // android.content.SharedPreferences
    public Map<String, ?> getAll() {
        HashMap map = new HashMap();
        for (Map.Entry<String, ?> entry : this.f7709.getAll().entrySet()) {
            if (!m4519(entry.getKey())) {
                try {
                    String str = new String(this.f7713.mo8652(C5506.m8875(entry.getKey(), 0), this.f7711.getBytes()), StandardCharsets.UTF_8);
                    if (str.equals("__NULL__")) {
                        str = null;
                    }
                    map.put(str, m4518(str));
                } catch (GeneralSecurityException e) {
                    StringBuilder sbM4889 = C2803.m4889("Could not decrypt key. ");
                    sbM4889.append(e.getMessage());
                    throw new SecurityException(sbM4889.toString(), e);
                }
            }
        }
        return map;
    }

    @Override // android.content.SharedPreferences
    public boolean getBoolean(String str, boolean z) {
        Object objM4518 = m4518(str);
        return (objM4518 == null || !(objM4518 instanceof Boolean)) ? z : ((Boolean) objM4518).booleanValue();
    }

    @Override // android.content.SharedPreferences
    public float getFloat(String str, float f) {
        Object objM4518 = m4518(str);
        return (objM4518 == null || !(objM4518 instanceof Float)) ? f : ((Float) objM4518).floatValue();
    }

    @Override // android.content.SharedPreferences
    public int getInt(String str, int i) {
        Object objM4518 = m4518(str);
        return (objM4518 == null || !(objM4518 instanceof Integer)) ? i : ((Integer) objM4518).intValue();
    }

    @Override // android.content.SharedPreferences
    public long getLong(String str, long j) {
        Object objM4518 = m4518(str);
        return (objM4518 == null || !(objM4518 instanceof Long)) ? j : ((Long) objM4518).longValue();
    }

    @Override // android.content.SharedPreferences
    public String getString(String str, String str2) {
        Object objM4518 = m4518(str);
        return (objM4518 == null || !(objM4518 instanceof String)) ? str2 : (String) objM4518;
    }

    @Override // android.content.SharedPreferences
    public Set<String> getStringSet(String str, Set<String> set) {
        Object objM4518 = m4518(str);
        Set<String> arraySet = objM4518 instanceof Set ? (Set) objM4518 : new ArraySet<>();
        return arraySet.size() > 0 ? arraySet : set;
    }

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

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

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public String m4517(String str) {
        if (str == null) {
            str = "__NULL__";
        }
        try {
            return C5506.m8874(this.f7713.mo8651(str.getBytes(StandardCharsets.UTF_8), this.f7711.getBytes()));
        } catch (GeneralSecurityException e) {
            StringBuilder sbM4889 = C2803.m4889("Could not encrypt key. ");
            sbM4889.append(e.getMessage());
            throw new SecurityException(sbM4889.toString(), e);
        }
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final Object m4518(String str) {
        if (m4519(str)) {
            throw new SecurityException(C2803.m4879(str, " is a reserved key for the encryption keyset."));
        }
        if (str == null) {
            str = "__NULL__";
        }
        try {
            String strM4517 = m4517(str);
            String string = this.f7709.getString(strM4517, null);
            if (string == null) {
                return null;
            }
            ByteBuffer byteBufferWrap = ByteBuffer.wrap(this.f7712.mo8648(C5506.m8875(string, 0), strM4517.getBytes(StandardCharsets.UTF_8)));
            byteBufferWrap.position(0);
            int iOrdinal = EnumC2437.m4522(byteBufferWrap.getInt()).ordinal();
            if (iOrdinal == 0) {
                int i = byteBufferWrap.getInt();
                ByteBuffer byteBufferSlice = byteBufferWrap.slice();
                byteBufferWrap.limit(i);
                String string2 = StandardCharsets.UTF_8.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 null;
                }
                return Boolean.valueOf(byteBufferWrap.get() != 0);
            }
            ArraySet arraySet = new ArraySet();
            while (byteBufferWrap.hasRemaining()) {
                int i2 = byteBufferWrap.getInt();
                ByteBuffer byteBufferSlice2 = byteBufferWrap.slice();
                byteBufferSlice2.limit(i2);
                byteBufferWrap.position(byteBufferWrap.position() + i2);
                arraySet.add(StandardCharsets.UTF_8.decode(byteBufferSlice2).toString());
            }
            if (arraySet.size() == 1 && "__NULL__".equals(arraySet.valueAt(0))) {
                return null;
            }
            return arraySet;
        } catch (GeneralSecurityException e) {
            StringBuilder sbM4889 = C2803.m4889("Could not decrypt value. ");
            sbM4889.append(e.getMessage());
            throw new SecurityException(sbM4889.toString(), e);
        }
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public boolean m4519(String str) {
        return "__androidx_security_crypto_encrypted_prefs_key_keyset__".equals(str) || "__androidx_security_crypto_encrypted_prefs_value_keyset__".equals(str);
    }
}
