package defpackage;

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;

/* renamed from: ᵩʾ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class SharedPreferencesEditorC3786 implements SharedPreferences.Editor {

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final SharedPreferencesC1998 f14133;

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final SharedPreferences.Editor f14134;

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public final AtomicBoolean f14135 = new AtomicBoolean(false);

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final CopyOnWriteArrayList f14132 = new CopyOnWriteArrayList();

    public SharedPreferencesEditorC3786(SharedPreferencesC1998 sharedPreferencesC1998, SharedPreferences.Editor editor) {
        this.f14133 = sharedPreferencesC1998;
        this.f14134 = editor;
    }

    @Override // android.content.SharedPreferences.Editor
    public final void apply() {
        m8442();
        this.f14134.apply();
        m8443();
        this.f14132.clear();
    }

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

    @Override // android.content.SharedPreferences.Editor
    public final boolean commit() {
        CopyOnWriteArrayList copyOnWriteArrayList = this.f14132;
        m8442();
        try {
            return this.f14134.commit();
        } finally {
            m8443();
            copyOnWriteArrayList.clear();
        }
    }

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

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

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

    @Override // android.content.SharedPreferences.Editor
    public final SharedPreferences.Editor putLong(String str, long j) {
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(12);
        byteBufferAllocate.putInt(3);
        byteBufferAllocate.putLong(j);
        m8441(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);
        m8441(str, byteBufferAllocate.array());
        return this;
    }

    @Override // android.content.SharedPreferences.Editor
    public final SharedPreferences.Editor putStringSet(String str, Set set) {
        int i = 0;
        if (set == null) {
            set = new C0277(0);
            set.add("__NULL__");
        }
        ArrayList 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);
        int size2 = arrayList.size();
        while (i < size2) {
            Object obj = arrayList.get(i);
            i++;
            byte[] bArr = (byte[]) obj;
            byteBufferAllocate.putInt(bArr.length);
            byteBufferAllocate.put(bArr);
        }
        m8441(str, byteBufferAllocate.array());
        return this;
    }

    @Override // android.content.SharedPreferences.Editor
    public final SharedPreferences.Editor remove(String str) {
        SharedPreferencesC1998 sharedPreferencesC1998 = this.f14133;
        sharedPreferencesC1998.getClass();
        if (SharedPreferencesC1998.m5518(str)) {
            throw new SecurityException(AbstractC3915.m8598(str, " is a reserved key for the encryption keyset."));
        }
        this.f14134.remove(sharedPreferencesC1998.m5520(str));
        this.f14132.add(str);
        return this;
    }

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final void m8441(String str, byte[] bArr) {
        SharedPreferencesC1998 sharedPreferencesC1998 = this.f14133;
        sharedPreferencesC1998.getClass();
        if (SharedPreferencesC1998.m5518(str)) {
            throw new SecurityException(AbstractC3915.m8598(str, " is a reserved key for the encryption keyset."));
        }
        this.f14132.add(str);
        if (str == null) {
            str = "__NULL__";
        }
        try {
            String strM5520 = sharedPreferencesC1998.m5520(str);
            try {
                Pair pair = new Pair(strM5520, new String(AbstractC1870.m5276(sharedPreferencesC1998.f8023.mo2411(bArr, strM5520.getBytes(StandardCharsets.UTF_8))), "US-ASCII"));
                this.f14134.putString((String) pair.first, (String) pair.second);
            } catch (UnsupportedEncodingException e) {
                throw new AssertionError(e);
            }
        } catch (GeneralSecurityException e2) {
            C4069.m8937("Could not encrypt data: ", e2.getMessage(), e2);
        }
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final void m8442() {
        if (this.f14135.getAndSet(false)) {
            SharedPreferencesC1998 sharedPreferencesC1998 = this.f14133;
            for (String str : ((HashMap) sharedPreferencesC1998.getAll()).keySet()) {
                if (!this.f14132.contains(str) && !SharedPreferencesC1998.m5518(str)) {
                    this.f14134.remove(sharedPreferencesC1998.m5520(str));
                }
            }
        }
    }

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final void m8443() {
        SharedPreferencesC1998 sharedPreferencesC1998 = this.f14133;
        Iterator it = sharedPreferencesC1998.f8022.iterator();
        while (it.hasNext()) {
            SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener = (SharedPreferences.OnSharedPreferenceChangeListener) it.next();
            Iterator it2 = this.f14132.iterator();
            while (it2.hasNext()) {
                onSharedPreferenceChangeListener.onSharedPreferenceChanged(sharedPreferencesC1998, (String) it2.next());
            }
        }
    }
}
