package s2;

import Y3.f;
import Z0.o;
import android.content.SharedPreferences;
import android.util.Pair;
import java.io.UnsupportedEncodingException;
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.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import n.C2579f;

/* JADX INFO: renamed from: s2.a, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class SharedPreferencesEditorC2906a implements SharedPreferences.Editor {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final SharedPreferences.Editor f24190b;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final AtomicBoolean f24192d = new AtomicBoolean(false);

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

    public SharedPreferencesEditorC2906a(SharedPreferencesC2907b sharedPreferencesC2907b, SharedPreferences.Editor editor) {
        this.f24189a = sharedPreferencesC2907b;
        this.f24190b = editor;
    }

    public final void a() {
        if (this.f24192d.getAndSet(false)) {
            SharedPreferencesC2907b sharedPreferencesC2907b = this.f24189a;
            for (String str : ((HashMap) sharedPreferencesC2907b.getAll()).keySet()) {
                if (!this.f24191c.contains(str) && !SharedPreferencesC2907b.d(str)) {
                    this.f24190b.remove(sharedPreferencesC2907b.b(str));
                }
            }
        }
    }

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

    public final void b() {
        SharedPreferencesC2907b sharedPreferencesC2907b = this.f24189a;
        for (SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener : sharedPreferencesC2907b.f24194b) {
            Iterator it = this.f24191c.iterator();
            while (it.hasNext()) {
                onSharedPreferenceChangeListener.onSharedPreferenceChanged(sharedPreferencesC2907b, (String) it.next());
            }
        }
    }

    public final void c(String str, byte[] bArr) {
        SharedPreferencesC2907b sharedPreferencesC2907b = this.f24189a;
        sharedPreferencesC2907b.getClass();
        if (SharedPreferencesC2907b.d(str)) {
            throw new SecurityException(o.n(str, " is a reserved key for the encryption keyset."));
        }
        this.f24191c.add(str);
        if (str == null) {
            str = "__NULL__";
        }
        try {
            String strB = sharedPreferencesC2907b.b(str);
            try {
                Pair pair = new Pair(strB, new String(f.b(sharedPreferencesC2907b.f24195c.a(bArr, strB.getBytes(StandardCharsets.UTF_8))), "US-ASCII"));
                this.f24190b.putString((String) pair.first, (String) pair.second);
            } catch (UnsupportedEncodingException e6) {
                throw new AssertionError(e6);
            }
        } catch (GeneralSecurityException e7) {
            throw new SecurityException("Could not encrypt data: " + e7.getMessage(), e7);
        }
    }

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

    @Override // android.content.SharedPreferences.Editor
    public final boolean commit() {
        CopyOnWriteArrayList copyOnWriteArrayList = this.f24191c;
        a();
        try {
            return this.f24190b.commit();
        } finally {
            b();
            copyOnWriteArrayList.clear();
        }
    }

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

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

    @Override // android.content.SharedPreferences.Editor
    public final SharedPreferences.Editor putInt(String str, int i6) {
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(8);
        byteBufferAllocate.putInt(2);
        byteBufferAllocate.putInt(i6);
        c(str, byteBufferAllocate.array());
        return this;
    }

    @Override // android.content.SharedPreferences.Editor
    public final SharedPreferences.Editor putLong(String str, long j4) {
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(12);
        byteBufferAllocate.putInt(3);
        byteBufferAllocate.putLong(j4);
        c(str, byteBufferAllocate.array());
        return this;
    }

    @Override // android.content.SharedPreferences.Editor
    public final 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(0);
        byteBufferAllocate.putInt(length);
        byteBufferAllocate.put(bytes);
        c(str, byteBufferAllocate.array());
        return this;
    }

    @Override // android.content.SharedPreferences.Editor
    public final SharedPreferences.Editor putStringSet(String str, Set set) {
        if (set == null) {
            set = new C2579f(0);
            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 byteBufferAllocate = ByteBuffer.allocate(size + 4);
        byteBufferAllocate.putInt(1);
        for (byte[] bArr : arrayList) {
            byteBufferAllocate.putInt(bArr.length);
            byteBufferAllocate.put(bArr);
        }
        c(str, byteBufferAllocate.array());
        return this;
    }

    @Override // android.content.SharedPreferences.Editor
    public final SharedPreferences.Editor remove(String str) {
        SharedPreferencesC2907b sharedPreferencesC2907b = this.f24189a;
        sharedPreferencesC2907b.getClass();
        if (SharedPreferencesC2907b.d(str)) {
            throw new SecurityException(o.n(str, " is a reserved key for the encryption keyset."));
        }
        this.f24190b.remove(sharedPreferencesC2907b.b(str));
        this.f24191c.add(str);
        return this;
    }
}
