package io.flutter.plugins.sharedpreferences; import android.content.Context; import android.content.SharedPreferences; import android.os.Handler; import android.os.Looper; import android.util.Base64; import com.google.android.gms.analytics.ecommerce.ProductAction; import com.google.android.gms.measurement.api.AppMeasurementSdk; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.math.BigInteger; 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.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /* loaded from: classes6.dex */ class MethodCallHandlerImpl implements MethodChannel.MethodCallHandler { private static final String BIG_INTEGER_PREFIX = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBCaWdJbnRlZ2Vy"; private static final String DOUBLE_PREFIX = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBEb3VibGUu"; private static final String LIST_IDENTIFIER = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBhIGxpc3Qu"; private static final String SHARED_PREFERENCES_NAME = "FlutterSharedPreferences"; private final ExecutorService executor = new ThreadPoolExecutor(0, 1, 30, TimeUnit.SECONDS, new LinkedBlockingQueue()); private final Handler handler = new Handler(Looper.getMainLooper()); private final SharedPreferences preferences; /* JADX INFO: Access modifiers changed from: package-private */ public MethodCallHandlerImpl(Context context) { this.preferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0); } @Override // io.flutter.plugin.common.MethodChannel.MethodCallHandler public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { char c; String str = (String) methodCall.argument("key"); try { String str2 = methodCall.method; switch (str2.hashCode()) { case -1354815177: if (str2.equals("commit")) { c = 5; break; } c = 65535; break; case -1249367445: if (str2.equals("getAll")) { c = 6; break; } c = 65535; break; case -1096934831: if (str2.equals("setStringList")) { c = 4; break; } c = 65535; break; case -934610812: if (str2.equals(ProductAction.ACTION_REMOVE)) { c = 7; break; } c = 65535; break; case -905809875: if (str2.equals("setInt")) { c = 2; break; } c = 65535; break; case 94746189: if (str2.equals("clear")) { c = '\b'; break; } c = 65535; break; case 155439827: if (str2.equals("setDouble")) { c = 1; break; } c = 65535; break; case 589412115: if (str2.equals("setString")) { c = 3; break; } c = 65535; break; case 1984457324: if (str2.equals("setBool")) { c = 0; break; } c = 65535; break; default: c = 65535; break; } switch (c) { case 0: commitAsync(this.preferences.edit().putBoolean(str, ((Boolean) methodCall.argument(AppMeasurementSdk.ConditionalUserProperty.VALUE)).booleanValue()), result); return; case 1: String d = Double.toString(((Number) methodCall.argument(AppMeasurementSdk.ConditionalUserProperty.VALUE)).doubleValue()); SharedPreferences.Editor edit = this.preferences.edit(); StringBuilder sb = new StringBuilder(DOUBLE_PREFIX); sb.append(d); commitAsync(edit.putString(str, sb.toString()), result); return; case 2: Number number = (Number) methodCall.argument(AppMeasurementSdk.ConditionalUserProperty.VALUE); if (number instanceof BigInteger) { SharedPreferences.Editor edit2 = this.preferences.edit(); StringBuilder sb2 = new StringBuilder(BIG_INTEGER_PREFIX); sb2.append(((BigInteger) number).toString(36)); commitAsync(edit2.putString(str, sb2.toString()), result); return; } commitAsync(this.preferences.edit().putLong(str, number.longValue()), result); return; case 3: String str3 = (String) methodCall.argument(AppMeasurementSdk.ConditionalUserProperty.VALUE); if (!str3.startsWith(LIST_IDENTIFIER) && !str3.startsWith(BIG_INTEGER_PREFIX) && !str3.startsWith(DOUBLE_PREFIX)) { commitAsync(this.preferences.edit().putString(str, str3), result); return; } result.error("StorageError", "This string cannot be stored as it clashes with special identifier prefixes.", null); return; case 4: List list = (List) methodCall.argument(AppMeasurementSdk.ConditionalUserProperty.VALUE); SharedPreferences.Editor edit3 = this.preferences.edit(); StringBuilder sb3 = new StringBuilder(LIST_IDENTIFIER); sb3.append(encodeList(list)); commitAsync(edit3.putString(str, sb3.toString()), result); return; case 5: result.success(Boolean.TRUE); return; case 6: result.success(getAllPrefs()); return; case 7: commitAsync(this.preferences.edit().remove(str), result); return; case '\b': Set keySet = getAllPrefs().keySet(); SharedPreferences.Editor edit4 = this.preferences.edit(); Iterator it = keySet.iterator(); while (it.hasNext()) { edit4.remove(it.next()); } commitAsync(edit4, result); return; default: result.notImplemented(); return; } } catch (IOException e) { result.error("IOException encountered", methodCall.method, e); } } public void teardown() { this.handler.removeCallbacksAndMessages(null); this.executor.shutdown(); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: io.flutter.plugins.sharedpreferences.MethodCallHandlerImpl$1, reason: invalid class name */ /* loaded from: classes6.dex */ public class AnonymousClass1 implements Runnable { final MethodCallHandlerImpl this$0; final SharedPreferences.Editor val$editor; final MethodChannel.Result val$result; AnonymousClass1(MethodCallHandlerImpl methodCallHandlerImpl, SharedPreferences.Editor editor, MethodChannel.Result result) { this.this$0 = methodCallHandlerImpl; this.val$editor = editor; this.val$result = result; } @Override // java.lang.Runnable public void run() { this.this$0.handler.post(new Runnable(this, this.val$editor.commit()) { // from class: io.flutter.plugins.sharedpreferences.MethodCallHandlerImpl.1.1 final AnonymousClass1 this$1; final boolean val$response; { this.this$1 = this; this.val$response = r2; } @Override // java.lang.Runnable public void run() { this.this$1.val$result.success(Boolean.valueOf(this.val$response)); } }); } } private void commitAsync(SharedPreferences.Editor editor, MethodChannel.Result result) { this.executor.execute(new AnonymousClass1(this, editor, result)); } private List decodeList(String str) throws IOException { ObjectInputStream objectInputStream; ObjectInputStream objectInputStream2 = null; try { try { objectInputStream = new ObjectInputStream(new ByteArrayInputStream(Base64.decode(str, 0))); } catch (ClassNotFoundException e) { e = e; } } catch (Throwable th) { th = th; objectInputStream = objectInputStream2; } try { List list = (List) objectInputStream.readObject(); objectInputStream.close(); return list; } catch (ClassNotFoundException e2) { e = e2; objectInputStream2 = objectInputStream; throw new IOException(e); } catch (Throwable th2) { th = th2; if (objectInputStream != null) { objectInputStream.close(); } throw th; } } private String encodeList(List list) throws IOException { ObjectOutputStream objectOutputStream; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); try { objectOutputStream.writeObject(list); objectOutputStream.flush(); String encodeToString = Base64.encodeToString(byteArrayOutputStream.toByteArray(), 0); objectOutputStream.close(); return encodeToString; } catch (Throwable th) { th = th; if (objectOutputStream != null) { objectOutputStream.close(); } throw th; } } catch (Throwable th2) { th = th2; objectOutputStream = null; } } private Map getAllPrefs() throws IOException { Object obj; Map all = this.preferences.getAll(); HashMap hashMap = new HashMap(); for (String str : all.keySet()) { if (str.startsWith("flutter.")) { Object obj2 = all.get(str); if (obj2 instanceof String) { String str2 = (String) obj2; if (str2.startsWith(LIST_IDENTIFIER)) { obj2 = decodeList(str2.substring(40)); } else if (str2.startsWith(BIG_INTEGER_PREFIX)) { obj = new BigInteger(str2.substring(44), 36); obj2 = obj; } else if (str2.startsWith(DOUBLE_PREFIX)) { obj2 = Double.valueOf(str2.substring(40)); } hashMap.put(str, obj2); } else { if (obj2 instanceof Set) { ArrayList arrayList = new ArrayList((Set) obj2); SharedPreferences.Editor remove = this.preferences.edit().remove(str); StringBuilder sb = new StringBuilder(LIST_IDENTIFIER); sb.append(encodeList(arrayList)); obj = arrayList; if (!remove.putString(str, sb.toString()).commit()) { throw new IOException("Could not migrate set to list"); } obj2 = obj; } hashMap.put(str, obj2); } } } return hashMap; } }