diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 9e066919e..64950a5f2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -46,6 +46,9 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager; import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Locale; @@ -103,7 +106,7 @@ public class GBApplication extends Application { private static SharedPreferences sharedPrefs; private static final String PREFS_VERSION = "shared_preferences_version"; //if preferences have to be migrated, increment the following and add the migration logic in migratePrefs below; see http://stackoverflow.com/questions/16397848/how-can-i-migrate-android-preferences-with-a-new-version - private static final int CURRENT_PREFS_VERSION = 5; + private static final int CURRENT_PREFS_VERSION = 6; private static LimitedQueue mIDSenderLookup = new LimitedQueue(16); private static Prefs prefs; private static GBPrefs gbPrefs; @@ -596,6 +599,58 @@ public class GBApplication extends Application { } } + private void migrateStringPrefToPerDevicePref(String globalPref, String globalPrefDefault, String perDevicePref, ArrayList deviceTypes) { + SharedPreferences.Editor editor = sharedPrefs.edit(); + String globalPrefValue = prefs.getString(globalPref, globalPrefDefault); + try (DBHandler db = acquireDB()) { + DaoSession daoSession = db.getDaoSession(); + List activeDevices = DBHelper.getActiveDevices(daoSession); + for (Device dbDevice : activeDevices) { + SharedPreferences deviceSpecificSharedPrefs = GBApplication.getDeviceSpecificSharedPrefs(dbDevice.getIdentifier()); + if (deviceSpecificSharedPrefs != null) { + SharedPreferences.Editor deviceSharedPrefsEdit = deviceSpecificSharedPrefs.edit(); + DeviceType deviceType = fromKey(dbDevice.getType()); + + if (deviceTypes.contains(deviceType)) { + Log.i(TAG, "migrating global string preference " + globalPref + " for " + deviceType.name() + " " + dbDevice.getIdentifier() ); + deviceSharedPrefsEdit.putString(perDevicePref, globalPrefValue); + } + deviceSharedPrefsEdit.apply(); + } + } + editor.remove(globalPref); + editor.apply(); + } catch (Exception e) { + Log.w(TAG, "error acquiring DB lock"); + } + } + + private void migrateBooleanPrefToPerDevicePref(String globalPref, Boolean globalPrefDefault, String perDevicePref, ArrayList deviceTypes) { + SharedPreferences.Editor editor = sharedPrefs.edit(); + boolean globalPrefValue = prefs.getBoolean(globalPref, globalPrefDefault); + try (DBHandler db = acquireDB()) { + DaoSession daoSession = db.getDaoSession(); + List activeDevices = DBHelper.getActiveDevices(daoSession); + for (Device dbDevice : activeDevices) { + SharedPreferences deviceSpecificSharedPrefs = GBApplication.getDeviceSpecificSharedPrefs(dbDevice.getIdentifier()); + if (deviceSpecificSharedPrefs != null) { + SharedPreferences.Editor deviceSharedPrefsEdit = deviceSpecificSharedPrefs.edit(); + DeviceType deviceType = fromKey(dbDevice.getType()); + + if (deviceTypes.contains(deviceType)) { + Log.i(TAG, "migrating global boolean preference " + globalPref + " for " + deviceType.name() + " " + dbDevice.getIdentifier() ); + deviceSharedPrefsEdit.putBoolean(perDevicePref, globalPrefValue); + } + deviceSharedPrefsEdit.apply(); + } + } + editor.remove(globalPref); + editor.apply(); + } catch (Exception e) { + Log.w(TAG, "error acquiring DB lock"); + } + } + private void migratePrefs(int oldVersion) { SharedPreferences.Editor editor = sharedPrefs.edit(); if (oldVersion == 0) { @@ -684,7 +739,7 @@ public class GBApplication extends Application { switch (deviceType) { case MIBAND: deviceSharedPrefsEdit.putBoolean("low_latency_fw_update", prefs.getBoolean("mi_low_latency_fw_update", true)); - deviceSharedPrefsEdit.putInt("device_time_offset_hours", prefs.getInt("mi_device_time_offset_hours", 0)); + deviceSharedPrefsEdit.putString("device_time_offset_hours", String.valueOf(prefs.getInt("mi_device_time_offset_hours", 0))); break; case AMAZFITCOR: displayItems = prefs.getStringSet("cor_display_items", null); @@ -825,6 +880,14 @@ public class GBApplication extends Application { Log.w(TAG, "error acquiring DB lock"); } } + if (oldVersion < 6) { + migrateBooleanPrefToPerDevicePref("mi2_enable_button_action", false, "button_action_enable", new ArrayList<>(Collections.singletonList(MIBAND2))); + migrateBooleanPrefToPerDevicePref("mi2_button_action_vibrate", false, "button_action_vibrate", new ArrayList<>(Collections.singletonList(MIBAND2))); + migrateStringPrefToPerDevicePref("mi_button_press_count", "6", "button_action_press_count", new ArrayList<>(Collections.singletonList(MIBAND2))); + migrateStringPrefToPerDevicePref("mi_button_press_count_max_delay", "2000", "button_action_press_max_interval", new ArrayList<>(Collections.singletonList(MIBAND2))); + migrateStringPrefToPerDevicePref("mi_button_press_count_match_delay", "0", "button_action_broadcast_delay", new ArrayList<>(Collections.singletonList(MIBAND2))); + migrateStringPrefToPerDevicePref("mi_button_press_broadcast", "nodomain.freeyourgadget.gadgetbridge.ButtonPressed", "button_action_broadcast", new ArrayList<>(Collections.singletonList(MIBAND2))); + } editor.putString(PREFS_VERSION, Integer.toString(CURRENT_PREFS_VERSION)); editor.apply();