1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-04 01:09:47 +01: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,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<Device> 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<Device> 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) {

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),