File indexing completed on 2024-12-22 04:41:43
0001 package org.kde.kdeconnect; 0002 0003 import android.content.SharedPreferences; 0004 0005 import androidx.annotation.Nullable; 0006 0007 import java.util.HashMap; 0008 import java.util.Map; 0009 import java.util.Set; 0010 0011 0012 /** 0013 * Mock implementation of shared preference, which just saves data in memory using map. 0014 * 0015 * It DOES NOT support transactions, changes are immediate 0016 * 0017 * From https://gist.github.com/amardeshbd/354173d00b988574ee5019c4ba0c8a0b 0018 */ 0019 class MockSharedPreference implements SharedPreferences { 0020 0021 private final HashMap<String, Object> preferenceMap; 0022 private final MockSharedPreferenceEditor preferenceEditor; 0023 0024 public MockSharedPreference() { 0025 preferenceMap = new HashMap<>(); 0026 preferenceEditor = new MockSharedPreferenceEditor(preferenceMap); 0027 } 0028 0029 @Override 0030 public Map<String, ?> getAll() { 0031 return preferenceMap; 0032 } 0033 0034 @Nullable 0035 @Override 0036 public String getString(final String s, @Nullable final String def) { 0037 if (!preferenceMap.containsKey(s)) return def; 0038 return (String) preferenceMap.get(s); 0039 } 0040 0041 @Nullable 0042 @Override 0043 public Set<String> getStringSet(final String s, @Nullable final Set<String> def) { 0044 if (!preferenceMap.containsKey(s)) return def; 0045 return (Set<String>) preferenceMap.get(s); 0046 } 0047 0048 @Override 0049 public int getInt(final String s, final int def) { 0050 if (!preferenceMap.containsKey(s)) return def; 0051 return (int) preferenceMap.get(s); 0052 } 0053 0054 @Override 0055 public long getLong(final String s, final long def) { 0056 if (!preferenceMap.containsKey(s)) return def; 0057 return (long) preferenceMap.get(s); 0058 } 0059 0060 @Override 0061 public float getFloat(final String s, final float def) { 0062 if (!preferenceMap.containsKey(s)) return def; 0063 return (float) preferenceMap.get(s); 0064 } 0065 0066 @Override 0067 public boolean getBoolean(final String s, final boolean def) { 0068 if (!preferenceMap.containsKey(s)) return def; 0069 return (boolean) preferenceMap.get(s); 0070 } 0071 0072 @Override 0073 public boolean contains(final String s) { 0074 return preferenceMap.containsKey(s); 0075 } 0076 0077 @Override 0078 public Editor edit() { 0079 return preferenceEditor; 0080 } 0081 0082 @Override 0083 public void registerOnSharedPreferenceChangeListener(final OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) { 0084 0085 } 0086 0087 @Override 0088 public void unregisterOnSharedPreferenceChangeListener(final OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) { 0089 0090 } 0091 0092 static class MockSharedPreferenceEditor implements Editor { 0093 0094 private final HashMap<String, Object> preferenceMap; 0095 0096 MockSharedPreferenceEditor(final HashMap<String, Object> preferenceMap) { 0097 this.preferenceMap = preferenceMap; 0098 } 0099 0100 @Override 0101 public Editor putString(final String s, @Nullable final String s1) { 0102 preferenceMap.put(s, s1); 0103 return this; 0104 } 0105 0106 @Override 0107 public Editor putStringSet(final String s, @Nullable final Set<String> set) { 0108 preferenceMap.put(s, set); 0109 return this; 0110 } 0111 0112 @Override 0113 public Editor putInt(final String s, final int i) { 0114 preferenceMap.put(s, i); 0115 return this; 0116 } 0117 0118 @Override 0119 public Editor putLong(final String s, final long l) { 0120 preferenceMap.put(s, l); 0121 return this; 0122 } 0123 0124 @Override 0125 public Editor putFloat(final String s, final float v) { 0126 preferenceMap.put(s, v); 0127 return this; 0128 } 0129 0130 @Override 0131 public Editor putBoolean(final String s, final boolean b) { 0132 preferenceMap.put(s, b); 0133 return this; 0134 } 0135 0136 @Override 0137 public Editor remove(final String s) { 0138 preferenceMap.remove(s); 0139 return this; 0140 } 0141 0142 @Override 0143 public Editor clear() { 0144 preferenceMap.clear(); 0145 return this; 0146 } 0147 0148 @Override 0149 public boolean commit() { 0150 return true; 0151 } 0152 0153 @Override 0154 public void apply() { 0155 // Nothing to do, everything is saved in memory. 0156 } 0157 } 0158 0159 }