Clear sync state when reset settings.

This commit is contained in:
Jimmy Shih
2013-02-25 11:03:43 -08:00
parent 6810a0d048
commit 10b327f533
4 changed files with 40 additions and 43 deletions
@@ -52,13 +52,8 @@ public class DeleteAllTrackDialogFragment extends DialogFragment {
PreferencesUtils.setBoolean(
fragmentActivity, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
SyncUtils.disableSync(fragmentActivity);
SyncUtils.clearSyncState(fragmentActivity);
MyTracksProviderUtils.Factory.get(fragmentActivity).deleteAllTracks();
PreferencesUtils.setLong(fragmentActivity, R.string.drive_largest_change_id_key,
PreferencesUtils.DRIVE_LARGEST_CHANGE_ID_DEFAULT);
// Clear the drive_deleted_list_key last
PreferencesUtils.setString(fragmentActivity, R.string.drive_deleted_list_key,
PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT);
}
}).start();
}
@@ -41,6 +41,7 @@ import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
@@ -137,6 +138,38 @@ public class SyncUtils {
ContentResolver.requestSync(account, SYNC_AUTHORITY, new Bundle());
}
/**
* Clears the sync state. Assumes sync is turned off. Do not want clearing the
* state to cause sync activities.
*/
public static void clearSyncState(Context context) {
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
Cursor cursor = null;
try {
cursor = myTracksProviderUtils.getTrackCursor(SyncUtils.DRIVE_ID_TRACKS_QUERY, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
Track track = myTracksProviderUtils.createTrack(cursor);
if (track.isSharedWithMe()) {
myTracksProviderUtils.deleteTrack(track.getId());
} else {
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, null);
}
} while (cursor.moveToNext());
}
} finally {
if (cursor != null) {
cursor.close();
}
}
PreferencesUtils.setLong(context, R.string.drive_largest_change_id_key,
PreferencesUtils.DRIVE_LARGEST_CHANGE_ID_DEFAULT);
// Clear the drive_deleted_list_key last
PreferencesUtils.setString(
context, R.string.drive_deleted_list_key, PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT);
}
/**
* Gets the drive service.
*
@@ -19,6 +19,7 @@ package com.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.io.backup.BackupActivity;
import com.google.android.apps.mytracks.io.backup.RestoreChooserActivity;
import com.google.android.apps.mytracks.io.sync.SyncUtils;
import com.google.android.apps.mytracks.util.DialogUtils;
import com.google.android.apps.mytracks.util.IntentUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
@@ -189,6 +190,9 @@ public class BackupResetSettingsActivity extends AbstractSettingsActivity {
// Actually wipe preferences and save synchronously
sharedPreferences.edit().clear().commit();
SyncUtils.disableSync(BackupResetSettingsActivity.this);
SyncUtils.clearSyncState(BackupResetSettingsActivity.this);
// Give UI feedback in the UI thread
runOnUiThread(new Runnable() {
@Override
@@ -17,8 +17,6 @@
package com.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.io.sendtogoogle.PermissionCallback;
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
import com.google.android.apps.mytracks.io.sync.SyncUtils;
@@ -34,7 +32,6 @@ import android.app.Dialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
@@ -122,7 +119,6 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
showDialog(DIALOG_CONFIRM_DRIVE_SYNC_ON);
} else {
handleSync(false);
clearSyncState();
}
return false;
}
@@ -202,7 +198,6 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
public void onClick(DialogInterface d, int button) {
googleAccountPreference.setValue(newValue);
handleSync(false);
clearSyncState();
updateUiByAccountName(newValue);
}
});
@@ -217,38 +212,6 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
super.onPrepareDialog(id, dialog, bundle);
}
/**
* Clears the sync state. Assumes sync is turned off. Do not want clearing the
* state to cause sync activities.
*/
private void clearSyncState() {
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(this);
Cursor cursor = null;
try {
cursor = myTracksProviderUtils.getTrackCursor(SyncUtils.DRIVE_ID_TRACKS_QUERY, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
Track track = myTracksProviderUtils.createTrack(cursor);
if (track.isSharedWithMe()) {
myTracksProviderUtils.deleteTrack(track.getId());
} else {
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, null);
}
} while (cursor.moveToNext());
}
} finally {
if (cursor != null) {
cursor.close();
}
}
PreferencesUtils.setLong(this, R.string.drive_largest_change_id_key,
PreferencesUtils.DRIVE_LARGEST_CHANGE_ID_DEFAULT);
// Clear the drive_deleted_list_key last
PreferencesUtils.setString(
this, R.string.drive_deleted_list_key, PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT);
}
/**
* Handles sync.
*
@@ -275,6 +238,8 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
break;
}
}
} else {
SyncUtils.clearSyncState(this);
}
}