1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-07-03 11:33:19 +02:00

Extract device type migration to standalone function

This commit is contained in:
José Rebelo 2023-10-27 20:14:41 +01:00
parent c2a9f5d805
commit dc825c87e7
2 changed files with 41 additions and 30 deletions

View File

@ -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,11 +684,7 @@ public class GBApplication extends Application {
}
}
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){
private void migrateDeviceTypes() {
try (DBHandler db = acquireDB()) {
final InputStream inputStream = getAssets().open("migrations/devicetype.json");
final byte[] buffer = new byte[inputStream.available()];
@ -698,7 +698,7 @@ public class GBApplication extends Application {
for (Device dbDevice : activeDevices) {
String deviceTypeName = dbDevice.getTypeName();
if(deviceTypeName.isEmpty()){
if(deviceTypeName.isEmpty() || deviceTypeName.equals("UNKNOWN")){
deviceTypeName = deviceIdNameMapping.optString(
String.valueOf(dbDevice.getType()),
"UNKNOWN"
@ -712,6 +712,14 @@ public class GBApplication extends Application {
}
}
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){
migrateDeviceTypes();
}
if (oldVersion == 0) {
String legacyGender = sharedPrefs.getString("mi_user_gender", null);
String legacyHeight = sharedPrefs.getString("mi_user_height_cm", null);

View File

@ -21,10 +21,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
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.
*
* <p>
* Note: they name of the enum is stored in the DB, so it is fixed forever,
* and may not be changed.
* <p>
* Migration note: As of <a href="https://codeberg.org/Freeyourgadget/Gadgetbridge/pulls/3347">#3347</a>,
* 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),