1
0
mirror of https://github.com/etesync/android synced 2025-02-02 19:01:06 +00:00

Account upgrade: raise an error when account migration fails.

Account migration works in most cases, though while testing I managed to
get it to fail in some rare occasions. This commit adds a check to
verify the number of contacts we thought we migrated is equal to the
number of contacts we have after migration.

If the check fails, it presents the user with a notification that opens
the relevant FAQ entry on the EteSync website.
This commit is contained in:
Tom Hacohen 2017-05-03 17:53:38 +01:00
parent 20568c850a
commit a9eba1af4e
3 changed files with 21 additions and 2 deletions

View File

@ -253,6 +253,8 @@ public class AccountSettings {
private void updateInner(int fromVersion) throws ContactsStorageException {
if (fromVersion < 2) {
long affected = -1;
long newCount = -1;
@Cleanup("release") ContentProviderClient provider = context.getContentResolver().acquireContentProviderClient(ContactsContract.AUTHORITY);
if (provider == null)
// no access to contacts provider
@ -288,14 +290,14 @@ public class AccountSettings {
throw new ContactsStorageException("Couldn't create address book account");
LocalAddressBook.setUserData(accountManager, addressBookAccount, account, info.uid);
LocalAddressBook addressBook = new LocalAddressBook(context, addressBookAccount, provider);
LocalAddressBook newAddressBook = new LocalAddressBook(context, addressBookAccount, provider);
// move contacts to new address book
App.log.info("Moving contacts from " + account + " to " + addressBookAccount);
ContentValues newAccount = new ContentValues(2);
newAccount.put(ContactsContract.RawContacts.ACCOUNT_NAME, addressBookAccount.name);
newAccount.put(ContactsContract.RawContacts.ACCOUNT_TYPE, addressBookAccount.type);
int affected = provider.update(ContactsContract.RawContacts.CONTENT_URI.buildUpon()
affected = provider.update(ContactsContract.RawContacts.CONTENT_URI.buildUpon()
.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, account.name)
.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type)
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(),
@ -303,6 +305,7 @@ public class AccountSettings {
ContactsContract.RawContacts.ACCOUNT_NAME + "=? AND " + ContactsContract.RawContacts.ACCOUNT_TYPE + "=?",
new String[]{account.name, account.type});
App.log.info(affected + " contacts moved to new address book");
newCount = newAddressBook.count();
}
ContactsContract.SyncState.set(provider, account, null);
@ -314,6 +317,13 @@ public class AccountSettings {
// request sync of new address book account
ContentResolver.setIsSyncable(account, App.getAddressBooksAuthority(), 1);
setSyncInterval(App.getAddressBooksAuthority(), Constants.DEFAULT_SYNC_INTERVAL);
// Error handling
if ((affected != -1) && (affected != newCount)) {
NotificationHelper notificationHelper = new NotificationHelper(context, "account-migration", Constants.NOTIFICATION_ACCOUNT_UPDATE);
notificationHelper.setThrowable(new AccountMigrationException("Failed to upgrade account"));
notificationHelper.notify("Account upgrade failed", "upgrading account");
}
}
}
@ -337,4 +347,9 @@ public class AccountSettings {
}
public static class AccountMigrationException extends Exception {
public AccountMigrationException(String msg) {
super(msg);
}
}
}

View File

@ -21,6 +21,7 @@ public class Constants {
NOTIFICATION_CONTACTS_SYNC = 10,
NOTIFICATION_CALENDAR_SYNC = 11,
NOTIFICATION_TASK_SYNC = 12,
NOTIFICATION_ACCOUNT_UPDATE = 13,
NOTIFICATION_PERMISSIONS = 20;
public static final Uri webUri = Uri.parse((DEBUG_REMOTE_URL == null) ? "https://www.etesync.com/" : DEBUG_REMOTE_URL);

View File

@ -98,6 +98,9 @@ public class NotificationHelper {
} else if (e instanceof Exceptions.UserInactiveException) {
WebViewActivity.openUrl(this, Constants.dashboard);
return;
} else if (e instanceof AccountSettings.AccountMigrationException) {
WebViewActivity.openUrl(this, Constants.faqUri.buildUpon().encodedFragment("account-migration-error").build());
return;
} else {
detailsIntent = new Intent(this, DebugInfoActivity.class);
}