Getting closer... db migration almost works.

This commit is contained in:
cpfeiffer 2016-06-18 01:26:36 +02:00
parent 13959677af
commit 41e6833b2d
10 changed files with 87 additions and 55 deletions

View File

@ -37,11 +37,6 @@ public class LockHandler implements DBHandler {
if (session == null) { if (session == null) {
throw new RuntimeException("Unable to create database session"); throw new RuntimeException("Unable to create database session");
} }
if (helper.importOldDbIfNecessary(daoMaster, this)) {
session.clear();
session = daoMaster.newSession();
}
} }
@Override @Override

View File

@ -73,6 +73,7 @@ public class DebugActivity extends GBActivity {
private Button HeartRateButton; private Button HeartRateButton;
private Button exportDBButton; private Button exportDBButton;
private Button importDBButton; private Button importDBButton;
private Button importOldActivityDataButton;
private Button deleteDBButton; private Button deleteDBButton;
private EditText editContent; private EditText editContent;
@ -194,6 +195,14 @@ public class DebugActivity extends GBActivity {
} }
}); });
importOldActivityDataButton = (Button) findViewById(R.id.mergeOldActivityData);
importOldActivityDataButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mergeOldActivityDbContents();
}
});
deleteDBButton = (Button) findViewById(R.id.emptyDBButton); deleteDBButton = (Button) findViewById(R.id.emptyDBButton);
deleteDBButton.setOnClickListener(new View.OnClickListener() { deleteDBButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -300,53 +309,57 @@ public class DebugActivity extends GBActivity {
.show(); .show();
} }
private void insertActivityDbContents() { private void mergeOldActivityDbContents() {
DBHelper helper = new DBHelper(getBaseContext()); final DBHelper helper = new DBHelper(getBaseContext());
ActivityDatabaseHandler oldHandler = helper.getOldActivityDatabaseHandler(); final ActivityDatabaseHandler oldHandler = helper.getOldActivityDatabaseHandler();
if (oldHandler == null) { if (oldHandler == null) {
GB.toast(this, "No old activity database found, nothing to import.", Toast.LENGTH_LONG, GB.ERROR);
return; return;
} }
GBDevice device = getDeviceForMergingActivityDatabaseInto(); selectDeviceForMergingActivityDatabaseInto(new DeviceSelectionCallback() {
if (device == null) { @Override
return; public void invoke(GBDevice device) {
} if (device == null) {
try (DBHandler targetHandler = GBApplication.acquireDB()) { GB.toast(DebugActivity.this, "No device to associate old activity data with.", Toast.LENGTH_LONG, GB.ERROR);
helper.importOldDb(oldHandler, device, targetHandler.getDaoMaster(), targetHandler); return;
} catch (GBException e) { }
e.printStackTrace(); try (DBHandler targetHandler = GBApplication.acquireDB()) {
} catch (Exception e) { helper.importOldDb(oldHandler, device, targetHandler);
e.printStackTrace(); } catch (Exception ex) {
} GB.toast(DebugActivity.this, "Error importing old activity data into new database.", Toast.LENGTH_LONG, GB.ERROR, ex);
}
}
});
} }
private GBDevice getDeviceForMergingActivityDatabaseInto() { private void selectDeviceForMergingActivityDatabaseInto(final DeviceSelectionCallback callback) {
final List<GBDevice> availableDevices = new ArrayList<>(DeviceHelper.getInstance().getAvailableDevices(getBaseContext())); final List<GBDevice> availableDevices = new ArrayList<>(DeviceHelper.getInstance().getAvailableDevices(getBaseContext()));
if (availableDevices.isEmpty()) { if (availableDevices.isEmpty()) {
return null; callback.invoke(null);
return;
} else if (availableDevices.size() == 1) { } else if (availableDevices.size() == 1) {
return availableDevices.get(0); callback.invoke(null);
return;
} }
GBDeviceAdapter adapter = new GBDeviceAdapter(getBaseContext(), availableDevices); GBDeviceAdapter adapter = new GBDeviceAdapter(getBaseContext(), availableDevices);
final GBDevice[] result = new GBDevice[1];
new AlertDialog.Builder(this) new AlertDialog.Builder(this)
.setCancelable(true) .setCancelable(true)
.setTitle("Delete Activity Data?") .setTitle("Associate old Data with Device")
.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() { .setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
GBDevice device = availableDevices.get(which); GBDevice device = availableDevices.get(which);
result[0] = device; callback.invoke(device);
} }
}) })
.setMessage("Select the device to merge the previous activity database data into.")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
callback.invoke(null);
} }
}) })
.show(); .show();
return result[0];
} }
private void deleteActivityDatabase() { private void deleteActivityDatabase() {
@ -424,4 +437,7 @@ public class DebugActivity extends GBActivity {
unregisterReceiver(mReceiver); unregisterReceiver(mReceiver);
} }
public static interface DeviceSelectionCallback {
void invoke(GBDevice device);
}
} }

View File

@ -296,7 +296,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
protected SampleProvider<? extends AbstractActivitySample> getProvider(DBHandler db, GBDevice device) { protected SampleProvider<? extends AbstractActivitySample> getProvider(DBHandler db, GBDevice device) {
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device); DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);
return coordinator.getSampleProvider(db); return coordinator.getSampleProvider(db.getDaoSession());
} }
/** /**

View File

@ -18,6 +18,7 @@ import nodomain.freeyourgadget.gadgetbridge.database.schema.ActivityDBCreationSc
import nodomain.freeyourgadget.gadgetbridge.database.schema.SchemaMigration; import nodomain.freeyourgadget.gadgetbridge.database.schema.SchemaMigration;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.impl.GBActivitySample; import nodomain.freeyourgadget.gadgetbridge.impl.GBActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
@ -295,12 +296,13 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl
public boolean hasContent() { public boolean hasContent() {
try { try {
try (SQLiteDatabase db = this.getReadableDatabase()) { try (SQLiteDatabase db = this.getReadableDatabase()) {
try (Cursor cursor = db.query(TABLE_GBACTIVITYSAMPLES, new String[]{KEY_TIMESTAMP}, null, null, null, KEY_TIMESTAMP + " DESC", "1")) { try (Cursor cursor = db.query(TABLE_GBACTIVITYSAMPLES, new String[]{KEY_TIMESTAMP}, null, null, null, null, null, "1")) {
return cursor.moveToFirst(); return cursor.moveToFirst();
} }
} }
} catch (Exception ex) { } catch (Exception ex) {
// can't expect anything // can't expect anything
GB.log("Error looking for old activity data: " + ex.getMessage(), GB.ERROR, ex);
return false; return false;
} }
} }
@ -309,4 +311,9 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl
public DaoSession getDaoSession() { public DaoSession getDaoSession() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public DaoMaster getDaoMaster() {
throw new UnsupportedOperationException();
}
} }

View File

@ -232,10 +232,10 @@ public class DBHelper {
if (prefsUser.getWeightKg() != attr.getWeightKG()) { if (prefsUser.getWeightKg() != attr.getWeightKG()) {
return false; return false;
} }
if (prefsUser.getSleepDuration() != attr.getSleepGoalHPD()) { if (!Integer.valueOf(prefsUser.getSleepDuration()).equals(attr.getSleepGoalHPD())) {
return false; return false;
} }
if (prefsUser.getStepsGoal() != attr.getStepsGoalSPD()) { if (!Integer.valueOf(prefsUser.getStepsGoal()).equals(attr.getStepsGoalSPD())) {
return false; return false;
} }
return true; return true;
@ -330,10 +330,10 @@ public class DBHelper {
return null; return null;
} }
public void importOldDb(ActivityDatabaseHandler oldDb, GBDevice targetDevice, DaoMaster daoMaster, DBHandler targetDBHandler) { public void importOldDb(ActivityDatabaseHandler oldDb, GBDevice targetDevice, DBHandler targetDBHandler) {
DaoSession tempSession = daoMaster.newSession(); DaoSession tempSession = targetDBHandler.getDaoMaster().newSession();
try { try {
importActivityDatabase(oldDb, targetDevice, tempSession, targetDBHandler); importActivityDatabase(oldDb, targetDevice, tempSession);
} finally { } finally {
tempSession.clear(); tempSession.clear();
} }
@ -345,11 +345,11 @@ public class DBHelper {
return totalSamplesCount == 0; return totalSamplesCount == 0;
} }
private void importActivityDatabase(ActivityDatabaseHandler oldDbHandler, GBDevice targetDevice, DaoSession session, DBHandler targetDBHandler) { private void importActivityDatabase(ActivityDatabaseHandler oldDbHandler, GBDevice targetDevice, DaoSession session) {
try (SQLiteDatabase oldDB = oldDbHandler.getReadableDatabase()) { try (SQLiteDatabase oldDB = oldDbHandler.getReadableDatabase()) {
User user = DBHelper.getUser(session); User user = DBHelper.getUser(session);
for (DeviceCoordinator coordinator : DeviceHelper.getInstance().getAllCoordinators()) { for (DeviceCoordinator coordinator : DeviceHelper.getInstance().getAllCoordinators()) {
AbstractSampleProvider<? extends AbstractActivitySample> sampleProvider = (AbstractSampleProvider<? extends AbstractActivitySample>) coordinator.getSampleProvider(targetDBHandler); AbstractSampleProvider<? extends AbstractActivitySample> sampleProvider = (AbstractSampleProvider<? extends AbstractActivitySample>) coordinator.getSampleProvider(session);
importActivitySamples(oldDB, targetDevice, session, sampleProvider, user); importActivitySamples(oldDB, targetDevice, session, sampleProvider, user);
} }
} }

View File

@ -6,6 +6,7 @@ import android.net.Uri;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
@ -85,7 +86,7 @@ public interface DeviceCoordinator {
* *
* @return * @return
*/ */
SampleProvider<? extends ActivitySample> getSampleProvider(DBHandler db); SampleProvider<? extends ActivitySample> getSampleProvider(DaoSession session);
/** /**
* Finds an install handler for the given uri that can install the given * Finds an install handler for the given uri that can install the given

View File

@ -9,6 +9,7 @@ import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter; import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
@ -110,7 +111,7 @@ public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator {
} }
@Override @Override
public SampleProvider<?> getSampleProvider(DBHandler db) { public SampleProvider<?> getSampleProvider(DaoSession session) {
return new UnknownSampleProvider(); return new UnknownSampleProvider();
} }

View File

@ -15,6 +15,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.AbstractDeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler; import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
@ -55,8 +56,8 @@ public class MiBandCoordinator extends AbstractDeviceCoordinator {
} }
@Override @Override
public SampleProvider<? extends AbstractActivitySample> getSampleProvider(DBHandler db) { public SampleProvider<? extends AbstractActivitySample> getSampleProvider(DaoSession session) {
return new MiBandSampleProvider(db.getDaoSession()); return new MiBandSampleProvider(session);
} }
@Override @Override

View File

@ -49,9 +49,8 @@ public class PebbleCoordinator extends AbstractDeviceCoordinator {
} }
@Override @Override
public SampleProvider<? extends AbstractActivitySample> getSampleProvider(DBHandler db) { public SampleProvider<? extends AbstractActivitySample> getSampleProvider(DaoSession session) {
Prefs prefs = GBApplication.getPrefs(); Prefs prefs = GBApplication.getPrefs();
DaoSession session = db.getDaoSession();
int activityTracker = prefs.getInt("pebble_activitytracker", SampleProvider.PROVIDER_PEBBLE_HEALTH); int activityTracker = prefs.getInt("pebble_activitytracker", SampleProvider.PROVIDER_PEBBLE_HEALTH);
switch (activityTracker) { switch (activityTracker) {
case SampleProvider.PROVIDER_PEBBLE_HEALTH: case SampleProvider.PROVIDER_PEBBLE_HEALTH:

View File

@ -76,16 +76,6 @@
android:layout_row="10" android:layout_row="10"
android:text="create test notification" /> android:text="create test notification" />
<Button
android:id="@+id/setTimeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"
android:layout_row="9"
android:text="set time" />
<Button <Button
android:id="@+id/incomingCallButton" android:id="@+id/incomingCallButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -125,11 +115,21 @@
android:layout_columnSpan="2" /> android:layout_columnSpan="2" />
<Button <Button
android:id="@+id/setMusicInfoButton" android:id="@+id/setTimeButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_column="0" android:layout_column="0"
android:layout_columnSpan="3" android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal"
android:layout_row="5"
android:text="set time" />
<Button
android:id="@+id/setMusicInfoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal" android:layout_gravity="fill_horizontal"
android:layout_row="5" android:layout_row="5"
android:text="set music info" /> android:text="set music info" />
@ -179,6 +179,18 @@
android:id="@+id/emptyDBButton" android:id="@+id/emptyDBButton"
android:layout_row="6" android:layout_row="6"
android:layout_column="2" /> android:layout_column="2" />
<Button
android:id="@+id/mergeOldActivityData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"
android:layout_row="7"
android:text="Merge old activity data" />
</GridLayout> </GridLayout>
</ScrollView> </ScrollView>