From c0e48b95aabe6a86649f42d4ca22deae9ee534fd Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 21 Nov 2015 10:53:03 +0100 Subject: [PATCH] Upgrade before displaying, prevent synchronization issues --- .../eu/faircode/netguard/ActivityMain.java | 8 +-- .../java/eu/faircode/netguard/Receiver.java | 64 ++++++++++--------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/ActivityMain.java b/app/src/main/java/eu/faircode/netguard/ActivityMain.java index 556a37b4..f85db3ac 100644 --- a/app/src/main/java/eu/faircode/netguard/ActivityMain.java +++ b/app/src/main/java/eu/faircode/netguard/ActivityMain.java @@ -83,6 +83,10 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences running = true; boolean enabled = prefs.getBoolean("enabled", false); + boolean initialized = prefs.getBoolean("initialized", false); + + // Upgrade + Receiver.upgrade(initialized, this); if (enabled) SinkholeService.start(this); @@ -180,7 +184,6 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences registerReceiver(packageChangedReceiver, intentFilter); // First use - boolean initialized = prefs.getBoolean("initialized", false); if (!initialized) { // Create view LayoutInflater inflater = LayoutInflater.from(this); @@ -215,9 +218,6 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences dialogFirst.show(); } - // Upgrade - Receiver.upgrade(initialized, this); - // Fill application list updateApplicationList(); } diff --git a/app/src/main/java/eu/faircode/netguard/Receiver.java b/app/src/main/java/eu/faircode/netguard/Receiver.java index f626bca9..ca57d7e8 100644 --- a/app/src/main/java/eu/faircode/netguard/Receiver.java +++ b/app/src/main/java/eu/faircode/netguard/Receiver.java @@ -62,42 +62,44 @@ public class Receiver extends BroadcastReceiver { } public static void upgrade(boolean initialized, Context context) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - int oldVersion = prefs.getInt("version", -1); - int newVersion = Util.getSelfVersionCode(context); - Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion); + synchronized (context.getApplicationContext()) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + int oldVersion = prefs.getInt("version", -1); + int newVersion = Util.getSelfVersionCode(context); + Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion); - SharedPreferences.Editor editor = prefs.edit(); - if (initialized) { - if (oldVersion < 38) { - Log.i(TAG, "Converting screen wifi/mobile"); - SharedPreferences.Editor edit = prefs.edit(); - edit.putBoolean("screen_wifi", prefs.getBoolean("unused", false)); - edit.putBoolean("screen_other", prefs.getBoolean("unused", false)); - edit.remove("unused"); - edit.apply(); + SharedPreferences.Editor editor = prefs.edit(); + if (initialized) { + if (oldVersion < 38) { + Log.i(TAG, "Converting screen wifi/mobile"); + SharedPreferences.Editor edit = prefs.edit(); + edit.putBoolean("screen_wifi", prefs.getBoolean("unused", false)); + edit.putBoolean("screen_other", prefs.getBoolean("unused", false)); + edit.remove("unused"); + edit.apply(); - SharedPreferences unused = context.getSharedPreferences("unused", Context.MODE_PRIVATE); - SharedPreferences screen_wifi = context.getSharedPreferences("screen_wifi", Context.MODE_PRIVATE); - SharedPreferences screen_other = context.getSharedPreferences("screen_other", Context.MODE_PRIVATE); + SharedPreferences unused = context.getSharedPreferences("unused", Context.MODE_PRIVATE); + SharedPreferences screen_wifi = context.getSharedPreferences("screen_wifi", Context.MODE_PRIVATE); + SharedPreferences screen_other = context.getSharedPreferences("screen_other", Context.MODE_PRIVATE); - Map punused = unused.getAll(); - SharedPreferences.Editor edit_screen_wifi = screen_wifi.edit(); - SharedPreferences.Editor edit_screen_other = screen_other.edit(); - for (String key : punused.keySet()) { - edit_screen_wifi.putBoolean(key, (Boolean) punused.get(key)); - edit_screen_other.putBoolean(key, (Boolean) punused.get(key)); + Map punused = unused.getAll(); + SharedPreferences.Editor edit_screen_wifi = screen_wifi.edit(); + SharedPreferences.Editor edit_screen_other = screen_other.edit(); + for (String key : punused.keySet()) { + edit_screen_wifi.putBoolean(key, (Boolean) punused.get(key)); + edit_screen_other.putBoolean(key, (Boolean) punused.get(key)); + } + edit_screen_wifi.apply(); + edit_screen_other.apply(); + + // TODO: delete unused } - edit_screen_wifi.apply(); - edit_screen_other.apply(); - - // TODO: delete unused + } else { + editor.putBoolean("whitelist_wifi", false); + editor.putBoolean("whitelist_other", false); } - } else { - editor.putBoolean("whitelist_wifi", false); - editor.putBoolean("whitelist_other", false); + editor.putInt("version", newVersion); + editor.apply(); } - editor.putInt("version", newVersion); - editor.apply(); } } \ No newline at end of file