|
29 | 29 | import android.app.NotificationChannel; |
30 | 30 | import android.app.NotificationManager; |
31 | 31 | import android.content.Context; |
| 32 | +import android.content.SharedPreferences; |
32 | 33 | import android.graphics.Color; |
33 | 34 | import android.graphics.drawable.ColorDrawable; |
34 | 35 | import android.os.Build; |
@@ -119,34 +120,8 @@ public void onCreate() { |
119 | 120 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) |
120 | 121 | createNotificationChannels(); |
121 | 122 |
|
122 | | - // Migrate onboarding_complete boolean to onboarding_version int |
123 | | - android.content.SharedPreferences prefs = androidx.preference.PreferenceManager.getDefaultSharedPreferences(this); |
124 | | - if (prefs.contains("onboarding_complete") && !prefs.contains("onboarding_version")) { |
125 | | - boolean completed = prefs.getBoolean("onboarding_complete", false); |
126 | | - prefs.edit() |
127 | | - .remove("onboarding_complete") |
128 | | - .putInt("onboarding_version", completed ? 1 : 0) |
129 | | - .apply(); |
130 | | - Log.i(TAG, "Migrated onboarding_complete=" + completed + " -> onboarding_version=" + (completed ? 1 : 0)); |
131 | | - } |
132 | | - |
133 | | - // Migrate old strict_blocking pref to blocking_mode |
134 | | - if (!prefs.contains(BlockingMode.PREF_BLOCKING_MODE)) { |
135 | | - String migratedMode = BlockingMode.getDefaultMode(); |
136 | | - |
137 | | - if (prefs.contains("strict_blocking")) { |
138 | | - boolean oldStrict = prefs.getBoolean("strict_blocking", false); |
139 | | - migratedMode = oldStrict ? BlockingMode.MODE_STRICT : BlockingMode.MODE_STANDARD; |
140 | | - |
141 | | - prefs.edit() |
142 | | - .remove("strict_blocking") |
143 | | - .putString(BlockingMode.PREF_BLOCKING_MODE, migratedMode) |
144 | | - .apply(); |
145 | | - Log.i(TAG, "Migrated strict_blocking=" + oldStrict + " -> mode=" + migratedMode); |
146 | | - } else { |
147 | | - prefs.edit().putString(BlockingMode.PREF_BLOCKING_MODE, migratedMode).apply(); |
148 | | - } |
149 | | - } |
| 123 | + SharedPreferences prefs = androidx.preference.PreferenceManager.getDefaultSharedPreferences(this); |
| 124 | + migratePreferences(prefs); |
150 | 125 |
|
151 | 126 | // Keep VPN exclusions aligned with the selected blocking mode on startup. |
152 | 127 | BlockingMode.syncModeExclusions(this); |
@@ -231,6 +206,51 @@ public void onActivityDestroyed(@NonNull Activity activity) { |
231 | 206 | }); |
232 | 207 | } |
233 | 208 |
|
| 209 | + static void migratePreferences(SharedPreferences prefs) { |
| 210 | + if (prefs.contains("onboarding_complete") && !prefs.contains("onboarding_version")) { |
| 211 | + boolean completed = prefs.getBoolean("onboarding_complete", false); |
| 212 | + prefs.edit() |
| 213 | + .remove("onboarding_complete") |
| 214 | + .putInt("onboarding_version", completed ? 1 : 0) |
| 215 | + .apply(); |
| 216 | + Log.i(TAG, "Migrated onboarding_complete=" + completed + " -> onboarding_version=" + (completed ? 1 : 0)); |
| 217 | + } |
| 218 | + |
| 219 | + if (!prefs.contains(BlockingMode.PREF_BLOCKING_MODE)) { |
| 220 | + Boolean oldStrict = prefs.contains("strict_blocking") |
| 221 | + ? prefs.getBoolean("strict_blocking", false) |
| 222 | + : null; |
| 223 | + int installedVersion = prefs.getInt("version", -1); |
| 224 | + String migratedMode = resolveBlockingModeMigration(null, oldStrict, installedVersion); |
| 225 | + |
| 226 | + if (oldStrict != null) { |
| 227 | + prefs.edit() |
| 228 | + .remove("strict_blocking") |
| 229 | + .putString(BlockingMode.PREF_BLOCKING_MODE, migratedMode) |
| 230 | + .apply(); |
| 231 | + Log.i(TAG, "Migrated strict_blocking=" + oldStrict + " -> mode=" + migratedMode); |
| 232 | + } else { |
| 233 | + prefs.edit().putString(BlockingMode.PREF_BLOCKING_MODE, migratedMode).apply(); |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + static String resolveBlockingModeMigration(@Nullable String existingMode, @Nullable Boolean legacyStrict) { |
| 239 | + return resolveBlockingModeMigration(existingMode, legacyStrict, -1); |
| 240 | + } |
| 241 | + |
| 242 | + static String resolveBlockingModeMigration(@Nullable String existingMode, |
| 243 | + @Nullable Boolean legacyStrict, |
| 244 | + int installedVersion) { |
| 245 | + if (existingMode != null) |
| 246 | + return existingMode; |
| 247 | + if (legacyStrict != null) |
| 248 | + return legacyStrict ? BlockingMode.MODE_STRICT : BlockingMode.MODE_STANDARD; |
| 249 | + if (installedVersion >= 0) |
| 250 | + return BlockingMode.MODE_STANDARD; |
| 251 | + return BlockingMode.getDefaultMode(); |
| 252 | + } |
| 253 | + |
234 | 254 | @TargetApi(Build.VERSION_CODES.O) |
235 | 255 | private void createNotificationChannels() { |
236 | 256 | NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
|
0 commit comments