package androidx.security.crypto;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.ArraySet;
import android.util.Pair;
import fk.g;
import fk.i;
import gk.f;
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 jk.a;

/* loaded from: classes.dex */
public final class a implements SharedPreferences {

    /* renamed from: a, reason: collision with root package name */
    final SharedPreferences f5581a;

    /* renamed from: b, reason: collision with root package name */
    final List f5582b = new ArrayList();

    /* renamed from: c, reason: collision with root package name */
    final String f5583c;

    /* renamed from: d, reason: collision with root package name */
    final String f5584d;

    /* renamed from: e, reason: collision with root package name */
    final fk.a f5585e;

    /* renamed from: f, reason: collision with root package name */
    final fk.d f5586f;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: androidx.security.crypto.a$a, reason: collision with other inner class name */
    /* loaded from: classes.dex */
    public static /* synthetic */ class C0141a {

        /* renamed from: a, reason: collision with root package name */
        static final /* synthetic */ int[] f5587a;

        static {
            int[] iArr = new int[c.values().length];
            f5587a = iArr;
            try {
                iArr[c.STRING.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                f5587a[c.INT.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                f5587a[c.LONG.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                f5587a[c.FLOAT.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                f5587a[c.BOOLEAN.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                f5587a[c.STRING_SET.ordinal()] = 6;
            } catch (NoSuchFieldError unused6) {
            }
        }
    }

    /* loaded from: classes.dex */
    private static final class b implements SharedPreferences.Editor {

        /* renamed from: a, reason: collision with root package name */
        private final a f5588a;

        /* renamed from: b, reason: collision with root package name */
        private final SharedPreferences.Editor f5589b;

        /* renamed from: d, reason: collision with root package name */
        private AtomicBoolean f5591d = new AtomicBoolean(false);

        /* renamed from: c, reason: collision with root package name */
        private final List f5590c = new CopyOnWriteArrayList();

        b(a aVar, SharedPreferences.Editor editor) {
            this.f5588a = aVar;
            this.f5589b = editor;
        }

        private void a() {
            if (this.f5591d.getAndSet(false)) {
                for (String str : this.f5588a.getAll().keySet()) {
                    if (!this.f5590c.contains(str) && !this.f5588a.f(str)) {
                        this.f5589b.remove(this.f5588a.c(str));
                    }
                }
            }
        }

        private void b() {
            for (SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener : this.f5588a.f5582b) {
                Iterator it = this.f5590c.iterator();
                while (it.hasNext()) {
                    onSharedPreferenceChangeListener.onSharedPreferenceChanged(this.f5588a, (String) it.next());
                }
            }
        }

        private void c(String str, byte[] bArr) {
            if (this.f5588a.f(str)) {
                throw new SecurityException(str + " is a reserved key for the encryption keyset.");
            }
            this.f5590c.add(str);
            if (str == null) {
                str = "__NULL__";
            }
            try {
                Pair d10 = this.f5588a.d(str, bArr);
                this.f5589b.putString((String) d10.first, (String) d10.second);
            } catch (GeneralSecurityException e10) {
                throw new SecurityException("Could not encrypt data: " + e10.getMessage(), e10);
            }
        }

        @Override // android.content.SharedPreferences.Editor
        public void apply() {
            a();
            this.f5589b.apply();
            b();
            this.f5590c.clear();
        }

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

        @Override // android.content.SharedPreferences.Editor
        public boolean commit() {
            a();
            try {
                return this.f5589b.commit();
            } finally {
                b();
                this.f5590c.clear();
            }
        }

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

        @Override // android.content.SharedPreferences.Editor
        public SharedPreferences.Editor putFloat(String str, float f10) {
            ByteBuffer allocate = ByteBuffer.allocate(8);
            allocate.putInt(c.FLOAT.getId());
            allocate.putFloat(f10);
            c(str, allocate.array());
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        public SharedPreferences.Editor putInt(String str, int i10) {
            ByteBuffer allocate = ByteBuffer.allocate(8);
            allocate.putInt(c.INT.getId());
            allocate.putInt(i10);
            c(str, allocate.array());
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        public SharedPreferences.Editor putLong(String str, long j10) {
            ByteBuffer allocate = ByteBuffer.allocate(12);
            allocate.putInt(c.LONG.getId());
            allocate.putLong(j10);
            c(str, allocate.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 allocate = ByteBuffer.allocate(length + 8);
            allocate.putInt(c.STRING.getId());
            allocate.putInt(length);
            allocate.put(bytes);
            c(str, allocate.array());
            return this;
        }

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

        @Override // android.content.SharedPreferences.Editor
        public SharedPreferences.Editor remove(String str) {
            if (!this.f5588a.f(str)) {
                this.f5589b.remove(this.f5588a.c(str));
                this.f5590c.remove(str);
                return this;
            }
            throw new SecurityException(str + " is a reserved key for the encryption keyset.");
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public enum c {
        STRING(0),
        STRING_SET(1),
        INT(2),
        LONG(3),
        FLOAT(4),
        BOOLEAN(5);

        private final int mId;

        c(int i10) {
            this.mId = i10;
        }

        public static c fromId(int i10) {
            if (i10 == 0) {
                return STRING;
            }
            if (i10 == 1) {
                return STRING_SET;
            }
            if (i10 == 2) {
                return INT;
            }
            if (i10 == 3) {
                return LONG;
            }
            if (i10 == 4) {
                return FLOAT;
            }
            if (i10 != 5) {
                return null;
            }
            return BOOLEAN;
        }

        public int getId() {
            return this.mId;
        }
    }

    /* loaded from: classes.dex */
    public enum d {
        AES256_SIV(ik.a.j());

        private final g mDeterministicAeadKeyTemplate;

        d(g gVar) {
            this.mDeterministicAeadKeyTemplate = gVar;
        }

        g getKeyTemplate() {
            return this.mDeterministicAeadKeyTemplate;
        }
    }

    /* loaded from: classes.dex */
    public enum e {
        AES256_GCM(f.j());

        private final g mAeadKeyTemplate;

        e(g gVar) {
            this.mAeadKeyTemplate = gVar;
        }

        g getKeyTemplate() {
            return this.mAeadKeyTemplate;
        }
    }

    a(String str, String str2, SharedPreferences sharedPreferences, fk.a aVar, fk.d dVar) {
        this.f5583c = str;
        this.f5581a = sharedPreferences;
        this.f5584d = str2;
        this.f5585e = aVar;
        this.f5586f = dVar;
    }

    public static SharedPreferences a(String str, String str2, Context context, d dVar, e eVar) {
        ik.b.b();
        gk.a.b();
        i c10 = new a.b().h(dVar.getKeyTemplate()).j(context, "__androidx_security_crypto_encrypted_prefs_key_keyset__", str).i("android-keystore://" + str2).d().c();
        i c11 = new a.b().h(eVar.getKeyTemplate()).j(context, "__androidx_security_crypto_encrypted_prefs_value_keyset__", str).i("android-keystore://" + str2).d().c();
        return new a(str, str2, context.getSharedPreferences(str, 0), (fk.a) c11.h(fk.a.class), (fk.d) c10.h(fk.d.class));
    }

    private Object e(String str) {
        if (f(str)) {
            throw new SecurityException(str + " is a reserved key for the encryption keyset.");
        }
        if (str == null) {
            str = "__NULL__";
        }
        try {
            String c10 = c(str);
            String string = this.f5581a.getString(c10, null);
            if (string == null) {
                return null;
            }
            ByteBuffer wrap = ByteBuffer.wrap(this.f5585e.b(nk.f.a(string, 0), c10.getBytes(StandardCharsets.UTF_8)));
            wrap.position(0);
            switch (C0141a.f5587a[c.fromId(wrap.getInt()).ordinal()]) {
                case 1:
                    int i10 = wrap.getInt();
                    ByteBuffer slice = wrap.slice();
                    wrap.limit(i10);
                    String charBuffer = StandardCharsets.UTF_8.decode(slice).toString();
                    if (charBuffer.equals("__NULL__")) {
                        return null;
                    }
                    return charBuffer;
                case 2:
                    return Integer.valueOf(wrap.getInt());
                case 3:
                    return Long.valueOf(wrap.getLong());
                case 4:
                    return Float.valueOf(wrap.getFloat());
                case 5:
                    return Boolean.valueOf(wrap.get() != 0);
                case 6:
                    ArraySet arraySet = new ArraySet();
                    while (wrap.hasRemaining()) {
                        int i11 = wrap.getInt();
                        ByteBuffer slice2 = wrap.slice();
                        slice2.limit(i11);
                        wrap.position(wrap.position() + i11);
                        arraySet.add(StandardCharsets.UTF_8.decode(slice2).toString());
                    }
                    if (arraySet.size() == 1 && "__NULL__".equals(arraySet.valueAt(0))) {
                        return null;
                    }
                    return arraySet;
                default:
                    return null;
            }
        } catch (GeneralSecurityException e10) {
            throw new SecurityException("Could not decrypt value. " + e10.getMessage(), e10);
        }
    }

    String b(String str) {
        try {
            String str2 = new String(this.f5586f.b(nk.f.a(str, 0), this.f5583c.getBytes()), StandardCharsets.UTF_8);
            if (str2.equals("__NULL__")) {
                return null;
            }
            return str2;
        } catch (GeneralSecurityException e10) {
            throw new SecurityException("Could not decrypt key. " + e10.getMessage(), e10);
        }
    }

    String c(String str) {
        if (str == null) {
            str = "__NULL__";
        }
        try {
            return nk.f.d(this.f5586f.a(str.getBytes(StandardCharsets.UTF_8), this.f5583c.getBytes()));
        } catch (GeneralSecurityException e10) {
            throw new SecurityException("Could not encrypt key. " + e10.getMessage(), e10);
        }
    }

    @Override // android.content.SharedPreferences
    public boolean contains(String str) {
        if (!f(str)) {
            return this.f5581a.contains(c(str));
        }
        throw new SecurityException(str + " is a reserved key for the encryption keyset.");
    }

    Pair d(String str, byte[] bArr) {
        String c10 = c(str);
        return new Pair(c10, nk.f.d(this.f5585e.a(bArr, c10.getBytes(StandardCharsets.UTF_8))));
    }

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

    boolean f(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 Map getAll() {
        HashMap hashMap = new HashMap();
        for (Map.Entry<String, ?> entry : this.f5581a.getAll().entrySet()) {
            if (!f(entry.getKey())) {
                String b10 = b(entry.getKey());
                hashMap.put(b10, e(b10));
            }
        }
        return hashMap;
    }

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

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

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

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

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

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

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

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