From 330edc80b0890f34b4f8ec601ebc665fcbb30572 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Fri, 12 Apr 2013 15:40:05 -0700 Subject: [PATCH] Implemented UserMigrated message, update all reference to local user id to be a string --- .../astrid/actfm/ActFmLoginActivity.java | 16 ++++++------- .../actfm/sync/ActFmPreferenceService.java | 3 ++- .../actfm/sync/ActFmSyncV2Provider.java | 13 ++-------- .../sync/messages/ServerToClientMessage.java | 5 +++- .../actfm/sync/messages/UserMigrated.java | 24 +++++++++++++++++++ .../sync/repeats/RepeatTestsActFmSync.java | 4 ++-- 6 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/UserMigrated.java diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/ActFmLoginActivity.java b/astrid/plugin-src/com/todoroo/astrid/actfm/ActFmLoginActivity.java index 6c0b23ced..4f38024d9 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/ActFmLoginActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/ActFmLoginActivity.java @@ -584,9 +584,9 @@ public class ActFmLoginActivity extends SherlockFragmentActivity { StatisticsService.reportEvent(StatisticsConstants.ACTFM_NEW_USER, "provider", provider); } // Successful login, create outstanding entries - long lastId = Preferences.getLong(ActFmPreferenceService.PREF_USER_ID, 0); + String lastId = ActFmPreferenceService.userId(); //Preferences.getLong(ActFmPreferenceService.PREF_USER_ID, 0); - if (!TextUtils.isEmpty(token) && lastId == 0) { + if (!TextUtils.isEmpty(token) && RemoteModel.isUuidEmpty(lastId)) { constructOutstandingTables(); } runOnUiThread(new Runnable() { @@ -620,11 +620,11 @@ public class ActFmLoginActivity extends SherlockFragmentActivity { @SuppressWarnings("nls") private void postAuthenticate(final JSONObject result, final String token) { - long lastLoggedInUser = Preferences.getLong(ActFmPreferenceService.PREF_USER_ID, 0); + String lastLoggedInUser = ActFmPreferenceService.userId(); - if (lastLoggedInUser > 0) { - long newUserId = result.optLong("id"); - if (lastLoggedInUser != newUserId) { + if (RemoteModel.isValidUuid(lastLoggedInUser)) { + String newUserId = Long.toString(result.optLong("id")); + if (!lastLoggedInUser.equals(newUserId)) { // In this case, we need to either make all data private or clear all data // Prompt for choice DialogUtilities.okCancelCustomDialog(this, @@ -831,8 +831,8 @@ public class ActFmLoginActivity extends SherlockFragmentActivity { private void finishSignIn(JSONObject result, String token, boolean restart) { actFmPreferenceService.setToken(token); - Preferences.setLong(ActFmPreferenceService.PREF_USER_ID, - result.optLong("id")); + Preferences.setString(ActFmPreferenceService.PREF_USER_ID, + Long.toString(result.optLong("id"))); Preferences.setString(ActFmPreferenceService.PREF_NAME, result.optString("name")); Preferences.setString(ActFmPreferenceService.PREF_FIRST_NAME, 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 2bc379b6a..10da26b16 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmPreferenceService.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmPreferenceService.java @@ -14,6 +14,7 @@ import com.timsu.astrid.R; import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.billing.BillingConstants; import com.todoroo.astrid.dao.RemoteModelDao; +import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.service.StatisticsConstants; import com.todoroo.astrid.service.StatisticsService; import com.todoroo.astrid.sync.SyncProviderUtilities; @@ -68,7 +69,7 @@ public class ActFmPreferenceService extends SyncProviderUtilities { * @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 RemoteModel.isValidUuid(userId()); } /** 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 578adbd86..8bbf4a91e 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java @@ -19,7 +19,6 @@ 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.actfm.sync.messages.ConvertSelfUserIdsToZero; import com.todoroo.astrid.billing.BillingConstants; import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.RemoteModelDao; @@ -122,16 +121,8 @@ public class ActFmSyncV2Provider extends SyncV2Provider { try { JSONObject status = actFmSyncService.invoke("user_status"); //$NON-NLS-1$ - long oldId = Preferences.getLong(ActFmPreferenceService.PREF_USER_ID, -1); - if (status.has("id")) { - long newId = status.optLong("id"); - Preferences.setLong(ActFmPreferenceService.PREF_USER_ID, newId); - if (oldId > 0 && oldId != newId) { - // Migrate all db userIds that = newId to 0 - new ConvertSelfUserIdsToZero().execute(); - ActFmSyncThread.clearTablePushedAtValues(); - } - } + if (status.has("id")) + Preferences.setString(ActFmPreferenceService.PREF_USER_ID, Long.toString(status.optLong("id"))); if (status.has("name")) Preferences.setString(ActFmPreferenceService.PREF_NAME, status.optString("name")); if (status.has("first_name")) diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ServerToClientMessage.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ServerToClientMessage.java index 6203737e2..f31fe6eee 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ServerToClientMessage.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ServerToClientMessage.java @@ -21,6 +21,7 @@ public abstract class ServerToClientMessage { public static final String TYPE_ACKNOWLEDGE_CHANGE = "AcknowledgeChange"; public static final String TYPE_USER_DATA = "UserData"; public static final String TYPE_DOUBLE_CHECK = "DoubleCheck"; + public static final String TYPE_USER_MIGRATED = "UserMigrated"; public static final String TYPE_DEBUG = "Debug"; protected final JSONObject json; @@ -39,8 +40,10 @@ public abstract class ServerToClientMessage { return new AcknowledgeChange(json); else if (TYPE_USER_DATA.equals(type)) return new UserData(json); - else if (TYPE_DOUBLE_CHECK.equals(json)) + else if (TYPE_DOUBLE_CHECK.equals(type)) return new DoubleCheck(json); + else if (TYPE_USER_MIGRATED.equals(type)) + return new UserMigrated(json); else if (TYPE_DEBUG.equals(type)) return new Debug(json); diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/UserMigrated.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/UserMigrated.java new file mode 100644 index 000000000..e60c98e51 --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/UserMigrated.java @@ -0,0 +1,24 @@ +package com.todoroo.astrid.actfm.sync.messages; + +import org.json.JSONObject; + +import com.todoroo.andlib.utility.Preferences; +import com.todoroo.astrid.actfm.sync.ActFmPreferenceService; +import com.todoroo.astrid.data.RemoteModel; + +public class UserMigrated extends ServerToClientMessage { + + public UserMigrated(JSONObject json) { + super(json); + } + + @Override + public void processMessage(String serverTime) { + String newUuid = json.optString("new_user_id"); //$NON-NLS-1$ + if (RemoteModel.isValidUuid(newUuid)) { + Preferences.setString(ActFmPreferenceService.PREF_USER_ID, newUuid); + new ConvertSelfUserIdsToZero(); + } + } + +} diff --git a/tests-sync/src/com/todoroo/astrid/sync/repeats/RepeatTestsActFmSync.java b/tests-sync/src/com/todoroo/astrid/sync/repeats/RepeatTestsActFmSync.java index 11c6f1959..2bb199190 100644 --- a/tests-sync/src/com/todoroo/astrid/sync/repeats/RepeatTestsActFmSync.java +++ b/tests-sync/src/com/todoroo/astrid/sync/repeats/RepeatTestsActFmSync.java @@ -85,8 +85,8 @@ public class RepeatTestsActFmSync extends AbstractSyncRepeatTests { private void postAuthenticate(JSONObject result, String token) { actFmPreferenceService.setToken(token); - Preferences.setLong(ActFmPreferenceService.PREF_USER_ID, - result.optLong("id")); + Preferences.setString(ActFmPreferenceService.PREF_USER_ID, + Long.toString(result.optLong("id"))); Preferences.setString(ActFmPreferenceService.PREF_NAME, result.optString("name")); Preferences.setString(ActFmPreferenceService.PREF_EMAIL, result.optString("email")); Preferences.setString(ActFmPreferenceService.PREF_PICTURE, result.optString("picture"));