| 1 | package tw.nekomimi.nkmr; |
| 2 | |
| 3 | import android.annotation.SuppressLint; |
| 4 | import android.app.Activity; |
| 5 | import android.content.Context; |
| 6 | import android.content.SharedPreferences; |
| 7 | |
| 8 | import org.telegram.messenger.ApplicationLoader; |
| 9 | import org.telegram.messenger.FileLog; |
| 10 | |
| 11 | import java.io.ByteArrayInputStream; |
| 12 | import java.io.ByteArrayOutputStream; |
| 13 | import java.io.ObjectInputStream; |
| 14 | import java.io.ObjectOutputStream; |
| 15 | import java.util.ArrayList; |
| 16 | import android.util.Base64; |
| 17 | import java.util.HashMap; |
| 18 | import java.util.HashSet; |
| 19 | import java.util.Set; |
| 20 | |
| 21 | @SuppressLint("ApplySharedPref") |
| 22 | public class NekomuraConfig { |
| 23 | private static final int configTypeBool = 0; |
| 24 | private static final int configTypeInt = 1; |
| 25 | private static final int configTypeString = 2; |
| 26 | private static final int configTypeSetInt = 3; |
| 27 | private static final int configTypeMapIntInt = 4; |
| 28 | |
| 29 | // Configs |
| 30 | public static ConfigItem largeAvatarInDrawer = addConfig("largeAvatarInDrawer", configTypeInt, 0); // 0:TG Default 1:NekoX Default 2:Large Avatar |
| 31 | public static ConfigItem unreadBadgeOnBackButton = addConfig("unreadBadgeOnBackButton", configTypeBool, false); |
| 32 | |
| 33 | public static class ConfigItem { |
| 34 | private String key; |
| 35 | private int type; |
| 36 | |
| 37 | private Object defaultValue; |
| 38 | private Object value; |
| 39 | private boolean forceDefault;//针对某些人的垃圾选项,应该不允许改变默认值 |
| 40 | |
| 41 | private ConfigItem(String k, int t, Object d) { |
| 42 | key = k; |
| 43 | type = t; |
| 44 | defaultValue = d; |
| 45 | } |
| 46 | |
| 47 | public String getKey() { |
| 48 | return key; |
| 49 | } |
| 50 | |
| 51 | // 读配置 |
| 52 | |
| 53 | public boolean Bool() { |
| 54 | return (boolean) value; |
| 55 | } |
| 56 | |
| 57 | public int Int() { |
| 58 | return (int) value; |
| 59 | } |
| 60 | |
| 61 | public String String() { |
| 62 | return value.toString(); |
| 63 | } |
| 64 | |
| 65 | public HashSet<Integer> SetInt() { |
| 66 | return (HashSet<Integer>) value; |
| 67 | } |
| 68 | |
| 69 | public HashMap<Integer, Integer> MapIntInt() { |
| 70 | return (HashMap<Integer, Integer>) value; |
| 71 | } |
| 72 | |
| 73 | public boolean SetIntContains(Integer v) { |
| 74 | return ((HashSet<Integer>) value).contains(v); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | public void changed(Object o) { |
| 79 | value = o; |
| 80 | } |
| 81 | |
| 82 | //写配置 |
| 83 | //这里没有检查类型哦 |
| 84 | |
| 85 | public boolean toggleConfigBool() { |
| 86 | value = !this.Bool(); |
| 87 | if (forceDefault) { |
| 88 | value = defaultValue; |
| 89 | } |
| 90 | saveConfig(); |
| 91 | return this.Bool();//返回toggle之后的 |
| 92 | } |
| 93 | |
| 94 | public void setConfigBool(boolean v) { |
| 95 | value = v; |
| 96 | if (forceDefault) { |
| 97 | value = defaultValue; |
| 98 | } |
| 99 | saveConfig(); |
| 100 | } |
| 101 | |
| 102 | public void setConfigInt(int v) { |
| 103 | if (forceDefault) { |
| 104 | value = defaultValue; |
| 105 | } |
| 106 | value = v; |
| 107 | saveConfig(); |
| 108 | } |
| 109 | |
| 110 | public void setConfigString(String v) { |
| 111 | value = v; |
| 112 | if (forceDefault) { |
| 113 | value = defaultValue; |
| 114 | } |
| 115 | saveConfig(); |
| 116 | } |
| 117 | |
| 118 | public void setConfigSetInt(HashSet<Integer> v) { |
| 119 | value = v; |
| 120 | if (forceDefault) { |
| 121 | value = defaultValue; |
| 122 | } |
| 123 | saveConfig(); |
| 124 | } |
| 125 | |
| 126 | public void setConfigMapInt(HashMap<Integer, Integer> v) { |
| 127 | value = v; |
| 128 | saveConfig(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private static boolean configLoaded; |
| 133 | private static final Object sync = new Object(); |
| 134 | private static ArrayList<ConfigItem> configs = new ArrayList<>(); |
| 135 | |
| 136 | // Configs |
| 137 | public static ConfigItem largeAvatarInDrawer = addConfig("largeAvatarInDrawer", configTypeInt, 0); // 0:TG Default 1:NekoX Default 2:Large Avatar |
| 138 | //TODO NekoConfig 那个 useAvatar 还没有迁移过来,所以只实现了 largeAvatarInDrawer=2 的情况。。。 |
| 139 | public static ConfigItem unreadBadgeOnBackButton = addConfig("unreadBadgeOnBackButton", configTypeBool, false); |
| 140 | |
| 141 | |
| 142 | static { |
| 143 | loadConfig(false); |
| 144 | } |
| 145 | |
| 146 | public static ConfigItem addConfig(String k, int t, Object d) { |
| 147 | ConfigItem a = new ConfigItem(k, t, d); |
| 148 | configs.add(a); |
| 149 | return a; |
| 150 | } |
| 151 | |
| 152 | public static ConfigItem findOne(String key) { |
| 153 | for (int i = 0; i < configs.size(); i++) { |
| 154 | ConfigItem o = configs.get(i); |
| 155 | if (key.equals(o.key)) { |
| 156 | return o; |
| 157 | } |
| 158 | } |
| 159 | return null; |
| 160 | } |
| 161 | |
| 162 | public static void loadConfig(boolean force) { |
| 163 | synchronized (sync) { |
| 164 | if (configLoaded && !force) { |
| 165 | return; |
| 166 | } |
| 167 | SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("nkmrcfg", Activity.MODE_PRIVATE); |
| 168 | for (int i = 0; i < configs.size(); i++) { |
| 169 | ConfigItem o = configs.get(i); |
| 170 | |
| 171 | if (o.forceDefault) { |
| 172 | o.value = o.defaultValue; |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | if (o.type == configTypeBool) { |
| 177 | o.value = preferences.getBoolean(o.key, (boolean) o.defaultValue); |
| 178 | } |
| 179 | if (o.type == configTypeInt) { |
| 180 | o.value = preferences.getInt(o.key, (int) o.defaultValue); |
| 181 | } |
| 182 | if (o.type == configTypeString) { |
| 183 | o.value = preferences.getString(o.key, (String) o.defaultValue); |
| 184 | } |
| 185 | if (o.type == configTypeSetInt) { |
| 186 | Set<String> ss = preferences.getStringSet(o.key, new HashSet<>()); |
| 187 | HashSet<Integer> si = new HashSet<>(); |
| 188 | for (String s : ss) { |
| 189 | si.add(Integer.parseInt(s)); |
| 190 | } |
| 191 | o.value = si; |
| 192 | } |
| 193 | if (o.type == configTypeMapIntInt) { |
| 194 | String cv = preferences.getString(o.key, ""); |
| 195 | // Log.e("NC", String.format("Getting pref %s val %s", o.key, cv)); |
| 196 | if (cv.length() == 0) { |
| 197 | o.value = new HashMap<Integer, Integer>(); |
| 198 | } else { |
| 199 | try { |
| 200 | byte[] data = Base64.decode(cv, Base64.DEFAULT); |
| 201 | ObjectInputStream ois = new ObjectInputStream( |
| 202 | new ByteArrayInputStream(data)); |
| 203 | o.value = (HashMap<Integer, Integer>) ois.readObject(); |
| 204 | if (o.value == null) { |
| 205 | o.value = new HashMap<Integer, Integer>(); |
| 206 | } |
| 207 | ois.close(); |
| 208 | } catch (Exception e) { |
| 209 | o.value = new HashMap<Integer, Integer>(); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | configLoaded = true; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | public static void saveConfig() { |
| 219 | synchronized (sync) { |
| 220 | try { |
| 221 | SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("nkmrcfg", Context.MODE_PRIVATE); |
| 222 | SharedPreferences.Editor editor = preferences.edit(); |
| 223 | for (int i = 0; i < configs.size(); i++) { |
| 224 | ConfigItem o = configs.get(i); |
| 225 | |
| 226 | if (o.type == configTypeBool) { |
| 227 | editor.putBoolean(o.key, (boolean) o.value); |
| 228 | } |
| 229 | if (o.type == configTypeInt) { |
| 230 | editor.putInt(o.key, (int) o.value); |
| 231 | } |
| 232 | if (o.type == configTypeString) { |
| 233 | editor.putString(o.key, o.value.toString()); |
| 234 | } |
| 235 | if (o.type == configTypeSetInt) { |
| 236 | HashSet<String> ss = new HashSet<>(); |
| 237 | for (Integer n : (Set<Integer>) o.value) { |
| 238 | ss.add(Integer.toString(n)); |
| 239 | } |
| 240 | editor.putStringSet(o.key, ss); |
| 241 | } |
| 242 | if (o.type == configTypeMapIntInt) { |
| 243 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 244 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 245 | oos.writeObject(o.value); |
| 246 | oos.close(); |
| 247 | editor.putString(o.key, Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT)); |
| 248 | } |
| 249 | } |
| 250 | editor.commit(); |
| 251 | } catch (Exception e) { |
| 252 | FileLog.e(e); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |