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) {
throw new RuntimeException("Unable to create database session");
}
if (helper.importOldDbIfNecessary(daoMaster, this)) {
session.clear();
session = daoMaster.newSession();
}
}
@Override

View File

@ -73,6 +73,7 @@ public class DebugActivity extends GBActivity {
private Button HeartRateButton;
private Button exportDBButton;
private Button importDBButton;
private Button importOldActivityDataButton;
private Button deleteDBButton;
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.setOnClickListener(new View.OnClickListener() {
@Override
@ -300,53 +309,57 @@ public class DebugActivity extends GBActivity {
.show();
}
private void insertActivityDbContents() {
DBHelper helper = new DBHelper(getBaseContext());
ActivityDatabaseHandler oldHandler = helper.getOldActivityDatabaseHandler();
private void mergeOldActivityDbContents() {
final DBHelper helper = new DBHelper(getBaseContext());
final ActivityDatabaseHandler oldHandler = helper.getOldActivityDatabaseHandler();
if (oldHandler == null) {
GB.toast(this, "No old activity database found, nothing to import.", Toast.LENGTH_LONG, GB.ERROR);
return;
}
GBDevice device = getDeviceForMergingActivityDatabaseInto();
if (device == null) {
return;
}
try (DBHandler targetHandler = GBApplication.acquireDB()) {
helper.importOldDb(oldHandler, device, targetHandler.getDaoMaster(), targetHandler);
} catch (GBException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
selectDeviceForMergingActivityDatabaseInto(new DeviceSelectionCallback() {
@Override
public void invoke(GBDevice device) {
if (device == null) {
GB.toast(DebugActivity.this, "No device to associate old activity data with.", Toast.LENGTH_LONG, GB.ERROR);
return;
}
try (DBHandler targetHandler = GBApplication.acquireDB()) {
helper.importOldDb(oldHandler, device, targetHandler);
} 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()));
if (availableDevices.isEmpty()) {
return null;
callback.invoke(null);
return;
} else if (availableDevices.size() == 1) {
return availableDevices.get(0);
callback.invoke(null);
return;
}
GBDeviceAdapter adapter = new GBDeviceAdapter(getBaseContext(), availableDevices);
final GBDevice[] result = new GBDevice[1];
new AlertDialog.Builder(this)
.setCancelable(true)
.setTitle("Delete Activity Data?")
.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() {
.setTitle("Associate old Data with Device")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int 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() {
@Override
public void onClick(DialogInterface dialog, int which) {
callback.invoke(null);
}
})
.show();
return result[0];
}
private void deleteActivityDatabase() {
@ -424,4 +437,7 @@ public class DebugActivity extends GBActivity {
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) {
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.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.impl.GBActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
@ -295,12 +296,13 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl
public boolean hasContent() {
try {
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();
}
}
} catch (Exception ex) {
// can't expect anything
GB.log("Error looking for old activity data: " + ex.getMessage(), GB.ERROR, ex);
return false;
}
}
@ -309,4 +311,9 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl
public DaoSession getDaoSession() {
throw new UnsupportedOperationException();
}
@Override
public DaoMaster getDaoMaster() {
throw new UnsupportedOperationException();
}
}

View File

@ -232,10 +232,10 @@ public class DBHelper {
if (prefsUser.getWeightKg() != attr.getWeightKG()) {
return false;
}
if (prefsUser.getSleepDuration() != attr.getSleepGoalHPD()) {
if (!Integer.valueOf(prefsUser.getSleepDuration()).equals(attr.getSleepGoalHPD())) {
return false;
}
if (prefsUser.getStepsGoal() != attr.getStepsGoalSPD()) {
if (!Integer.valueOf(prefsUser.getStepsGoal()).equals(attr.getStepsGoalSPD())) {
return false;
}
return true;
@ -330,10 +330,10 @@ public class DBHelper {
return null;
}
public void importOldDb(ActivityDatabaseHandler oldDb, GBDevice targetDevice, DaoMaster daoMaster, DBHandler targetDBHandler) {
DaoSession tempSession = daoMaster.newSession();
public void importOldDb(ActivityDatabaseHandler oldDb, GBDevice targetDevice, DBHandler targetDBHandler) {
DaoSession tempSession = targetDBHandler.getDaoMaster().newSession();
try {
importActivityDatabase(oldDb, targetDevice, tempSession, targetDBHandler);
importActivityDatabase(oldDb, targetDevice, tempSession);
} finally {
tempSession.clear();
}
@ -345,11 +345,11 @@ public class DBHelper {
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()) {
User user = DBHelper.getUser(session);
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);
}
}

View File

@ -6,6 +6,7 @@ import android.net.Uri;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
@ -85,7 +86,7 @@ public interface DeviceCoordinator {
*
* @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

View File

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

View File

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

View File

@ -76,16 +76,6 @@
android:layout_row="10"
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
android:id="@+id/incomingCallButton"
android:layout_width="wrap_content"
@ -125,11 +115,21 @@
android:layout_columnSpan="2" />
<Button
android:id="@+id/setMusicInfoButton"
android:id="@+id/setTimeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_row="5"
android:text="set music info" />
@ -179,6 +179,18 @@
android:id="@+id/emptyDBButton"
android:layout_row="6"
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>
</ScrollView>