From 1826417c77f01e6ae69d711a89471a76c7496e66 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Fri, 15 Mar 2013 14:16:01 -0700 Subject: [PATCH] Improvements to logout logic --- .../astrid/actfm/ActFmLoginActivity.java | 65 ++++++++++--------- .../actfm/sync/ActFmPreferenceService.java | 10 +++ .../actfm/sync/ActFmSyncV2Provider.java | 20 +++++- astrid/res/values/strings-actfm.xml | 9 ++- .../todoroo/astrid/dao/RemoteModelDao.java | 2 +- 5 files changed, 72 insertions(+), 34 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/ActFmLoginActivity.java b/astrid/plugin-src/com/todoroo/astrid/actfm/ActFmLoginActivity.java index 1cb960163..26924c30d 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/ActFmLoginActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/ActFmLoginActivity.java @@ -599,9 +599,8 @@ public class ActFmLoginActivity extends FragmentActivity implements AuthListener } // Successful login, create outstanding entries long lastId = Preferences.getLong(ActFmPreferenceService.PREF_USER_ID, 0); - long newUser = result.optLong("id"); - if (!TextUtils.isEmpty(token) && (lastId == 0 || lastId == newUser)) { + if (!TextUtils.isEmpty(token) && lastId == 0) { constructOutstandingTables(); } runOnUiThread(new Runnable() { @@ -636,7 +635,10 @@ public class ActFmLoginActivity extends FragmentActivity implements AuthListener @SuppressWarnings("nls") private void postAuthenticate(final JSONObject result, final String token) { long lastLoggedInUser = Preferences.getLong(ActFmPreferenceService.PREF_USER_ID, 0); - if (lastLoggedInUser > 0) { + boolean clearedOnLastLogOut = Preferences.getBoolean(ActFmPreferenceService.PREF_CLEARED_TASKS_ON_LOGOUT, false); + Preferences.setBoolean(ActFmPreferenceService.PREF_CLEARED_TASKS_ON_LOGOUT, false); + + if (lastLoggedInUser > 0 && !clearedOnLastLogOut) { long newUserId = result.optLong("id"); if (lastLoggedInUser != newUserId) { // In this case, we need to either make all data private or clear all data @@ -650,38 +652,12 @@ public class ActFmLoginActivity extends FragmentActivity implements AuthListener new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - // TODO: Make all data private final ProgressDialog pd = DialogUtilities.progressDialog(ActFmLoginActivity.this, getString(R.string.actfm_logged_in_different_user_processing)); new Thread(new Runnable() { @Override public void run() { - // Delete all tasks not assigned to self - taskService.deleteWhere(Criterion.or(Task.USER_ID.neq(0), Task.DELETION_DATE.gt(0))); - // Delete user table - userDao.deleteWhere(Criterion.all); - // Delete attachments table - taskAttachmentDao.deleteWhere(Criterion.all); - // Delete deleted tags - tagDataDao.deleteWhere(TagData.DELETION_DATE.gt(0)); - // Delete deleted metadata - metadataDao.deleteWhere(Metadata.DELETION_DATE.gt(0)); - - // Clear all outstanding tables - taskOutstandingDao.deleteWhere(Criterion.all); - tagOutstandingDao.deleteWhere(Criterion.all); - userActivityOutstandingDao.deleteWhere(Criterion.all); - taskListMetadataOutstandingDao.deleteWhere(Criterion.all); - taskAttachmentOutstandingDao.deleteWhere(Criterion.all); - - // Make all tags private - tagMetadataDao.deleteWhere(Criterion.all); - - // Generate new uuids for all tasks/tags/user activity/task list metadata and update links - generateNewUuids(); - clearTablePushedAtValues(); - - constructOutstandingTables(); + rebuildAllSyncData(); runOnUiThread(new Runnable() { @Override public void run() { @@ -708,6 +684,35 @@ public class ActFmLoginActivity extends FragmentActivity implements AuthListener } } + private void rebuildAllSyncData() { + // Delete all tasks not assigned to self + taskService.deleteWhere(Criterion.or(Task.USER_ID.neq(0), Task.DELETION_DATE.gt(0))); + // Delete user table + userDao.deleteWhere(Criterion.all); + // Delete attachments table + taskAttachmentDao.deleteWhere(Criterion.all); + // Delete deleted tags + tagDataDao.deleteWhere(TagData.DELETION_DATE.gt(0)); + // Delete deleted metadata + metadataDao.deleteWhere(Metadata.DELETION_DATE.gt(0)); + + // Clear all outstanding tables + taskOutstandingDao.deleteWhere(Criterion.all); + tagOutstandingDao.deleteWhere(Criterion.all); + userActivityOutstandingDao.deleteWhere(Criterion.all); + taskListMetadataOutstandingDao.deleteWhere(Criterion.all); + taskAttachmentOutstandingDao.deleteWhere(Criterion.all); + + // Make all tags private + tagMetadataDao.deleteWhere(Criterion.all); + + // Generate new uuids for all tasks/tags/user activity/task list metadata and update links + generateNewUuids(); + clearTablePushedAtValues(); + + constructOutstandingTables(); + } + private void generateNewUuids() { final HashMap uuidTaskMap = new HashMap(); HashMap uuidTagMap = new HashMap(); diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmPreferenceService.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmPreferenceService.java index c32104f63..ddaa95509 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmPreferenceService.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmPreferenceService.java @@ -64,6 +64,13 @@ public class ActFmPreferenceService extends SyncProviderUtilities { RemoteModelDao.outstandingEntryFlag = 1; } + /** + * @return true if the user is now or has ever been logged in + */ + public boolean wasLoggedIn() { + return Preferences.getLong(PREF_USER_ID, 0) > 0; + } + /** * @return get user id */ @@ -105,6 +112,9 @@ public class ActFmPreferenceService extends SyncProviderUtilities { /** Act.fm last sync server time */ public static final String PREF_SERVER_TIME = IDENTIFIER + "_time"; //$NON-NLS-1$ + /** Whether the user kept existing tasks after last logout */ + public static final String PREF_CLEARED_TASKS_ON_LOGOUT = IDENTIFIER + "_logout_cleared_tasks"; //$NON-NLS-1$ + private static JSONObject user = null; @Override diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java index 50db43448..9c9089f8d 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java @@ -10,11 +10,13 @@ import java.io.IOException; import org.json.JSONObject; import android.app.Activity; +import android.content.DialogInterface; import com.timsu.astrid.GCMIntentService; import com.timsu.astrid.R; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.ContextManager; +import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.billing.BillingConstants; import com.todoroo.astrid.dao.Database; @@ -61,11 +63,27 @@ public class ActFmSyncV2Provider extends SyncV2Provider { } @Override - public void signOut(Activity activity) { + public void signOut(final Activity activity) { actFmPreferenceService.setToken(null); actFmPreferenceService.clearLastSyncDate(); ActFmPreferenceService.premiumLogout(); GCMIntentService.unregister(ContextManager.getContext()); + + DialogUtilities.okCancelCustomDialog(activity, + activity.getString(R.string.actfm_logout_clear_tasks_title), + activity.getString(R.string.actfm_logout_clear_tasks_body), + R.string.actfm_logout_clear_tasks_yes, + R.string.actfm_logout_clear_tasks_no, + android.R.drawable.ic_dialog_alert, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + activity.deleteDatabase(database.getName()); + Preferences.setBoolean(ActFmPreferenceService.PREF_CLEARED_TASKS_ON_LOGOUT, true); + System.exit(0); + } + }, + null); } @Override diff --git a/astrid/res/values/strings-actfm.xml b/astrid/res/values/strings-actfm.xml index a36e0737a..a0b6efe05 100644 --- a/astrid/res/values/strings-actfm.xml +++ b/astrid/res/values/strings-actfm.xml @@ -358,8 +358,13 @@ You have been logged out of Astrid - Warning! Logging in as different user - This device was previously signed in under a different user account. Do you want to keep existing data or clear everything? (This cannot be undone, and Astrid will restart when you make your choice) + Clear data? + Would you like to clear your tasks and lists? + Clear data + Keep data + + Logging in as different user + Would you like to delete existing phone tasks before syncing? Keep data Clear all data Processing existing data... diff --git a/astrid/src/com/todoroo/astrid/dao/RemoteModelDao.java b/astrid/src/com/todoroo/astrid/dao/RemoteModelDao.java index f197999cc..190eef7eb 100644 --- a/astrid/src/com/todoroo/astrid/dao/RemoteModelDao.java +++ b/astrid/src/com/todoroo/astrid/dao/RemoteModelDao.java @@ -37,7 +37,7 @@ public class RemoteModelDao extends DatabaseDao