diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index d310dfc71..ddba06b93 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -226,6 +226,10 @@ public class GBApplication extends Application { migratePrefs(getPrefsFileVersion()); } + // Uncomment the line below to force a device key migration, after you updated + // the devicetype.json file + //migrateDeviceTypes(); + setupExceptionHandler(); Weather.getInstance().setCacheFile(getCacheDir(), prefs.getBoolean("cache_weather", true)); @@ -680,36 +684,40 @@ public class GBApplication extends Application { } } + private void migrateDeviceTypes() { + try (DBHandler db = acquireDB()) { + final InputStream inputStream = getAssets().open("migrations/devicetype.json"); + final byte[] buffer = new byte[inputStream.available()]; + inputStream.read(buffer); + inputStream.close(); + final JSONObject deviceMapping = new JSONObject(new String(buffer)); + final JSONObject deviceIdNameMapping = deviceMapping.getJSONObject("by-id"); + + final DaoSession daoSession = db.getDaoSession(); + final List activeDevices = DBHelper.getActiveDevices(daoSession); + + for (Device dbDevice : activeDevices) { + String deviceTypeName = dbDevice.getTypeName(); + if(deviceTypeName.isEmpty() || deviceTypeName.equals("UNKNOWN")){ + deviceTypeName = deviceIdNameMapping.optString( + String.valueOf(dbDevice.getType()), + "UNKNOWN" + ); + dbDevice.setTypeName(deviceTypeName); + daoSession.getDeviceDao().update(dbDevice); + } + } + } catch (Exception e) { + Log.w(TAG, "error acquiring DB lock"); + } + } + private void migratePrefs(int oldVersion) { SharedPreferences.Editor editor = sharedPrefs.edit(); // this comes before all other migrations since the new column DeviceTypeName was added as non-null if (oldVersion < 25){ - try (DBHandler db = acquireDB()) { - final InputStream inputStream = getAssets().open("migrations/devicetype.json"); - final byte[] buffer = new byte[inputStream.available()]; - inputStream.read(buffer); - inputStream.close(); - final JSONObject deviceMapping = new JSONObject(new String(buffer)); - final JSONObject deviceIdNameMapping = deviceMapping.getJSONObject("by-id"); - - final DaoSession daoSession = db.getDaoSession(); - final List activeDevices = DBHelper.getActiveDevices(daoSession); - - for (Device dbDevice : activeDevices) { - String deviceTypeName = dbDevice.getTypeName(); - if(deviceTypeName.isEmpty()){ - deviceTypeName = deviceIdNameMapping.optString( - String.valueOf(dbDevice.getType()), - "UNKNOWN" - ); - dbDevice.setTypeName(deviceTypeName); - daoSession.getDeviceDao().update(dbDevice); - } - } - } catch (Exception e) { - Log.w(TAG, "error acquiring DB lock"); - } + migrateDeviceTypes(); } if (oldVersion == 0) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceType.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceType.java index bcac25ef4..5f150c75a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceType.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceType.java @@ -21,10 +21,6 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.model; -import androidx.annotation.DrawableRes; -import androidx.annotation.StringRes; - -import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.UnknownDeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.asteroidos.AsteroidOSDeviceCoordinator; @@ -147,9 +143,16 @@ import nodomain.freeyourgadget.gadgetbridge.devices.zetime.ZeTimeCoordinator; /** * For every supported device, a device type constant must exist. - * + *

* Note: they name of the enum is stored in the DB, so it is fixed forever, * and may not be changed. + *

+ * Migration note: As of #3347, + * the numeric device id is not used anymore. If your database has development devices that still used + * the numeric ID, you need to update assets/migrations/devicetype.json before installing Gadgetbridge + * after rebasing, in order for your device to be migrated correctly. If you failed to do this and the + * device is now not being displayed, please update the file and uncomment the call to migrateDeviceTypes + * in GBApplication. */ public enum DeviceType { UNKNOWN(UnknownDeviceCoordinator.class),