forked from upstream-mirrors/OpenTracks
Code cleanup for enable and disable google sync
This commit is contained in:
@@ -50,7 +50,6 @@ import android.accounts.AccountManagerFuture;
|
|||||||
import android.accounts.AuthenticatorException;
|
import android.accounts.AuthenticatorException;
|
||||||
import android.accounts.OperationCanceledException;
|
import android.accounts.OperationCanceledException;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
@@ -343,16 +342,7 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv
|
|||||||
Class<?> next;
|
Class<?> next;
|
||||||
if (sendRequest.isSendDrive()) {
|
if (sendRequest.isSendDrive()) {
|
||||||
if (sendRequest.isDriveSync()) {
|
if (sendRequest.isDriveSync()) {
|
||||||
PreferencesUtils.setBoolean(this, R.string.drive_sync_key, true);
|
SyncUtils.enableSync(this);
|
||||||
|
|
||||||
// Turn off everything
|
|
||||||
SyncUtils.disableSync(this);
|
|
||||||
|
|
||||||
// Turn on sync
|
|
||||||
ContentResolver.setMasterSyncAutomatically(true);
|
|
||||||
|
|
||||||
// Enable sync for account
|
|
||||||
SyncUtils.enableSync(sendRequest.getAccount());
|
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
next = SendDriveActivity.class;
|
next = SendDriveActivity.class;
|
||||||
|
|||||||
@@ -91,10 +91,7 @@ public class DeleteAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
PreferencesUtils.setBoolean(
|
|
||||||
context, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
|
|
||||||
SyncUtils.disableSync(context);
|
SyncUtils.disableSync(context);
|
||||||
SyncUtils.clearSyncState(context);
|
|
||||||
myTracksProviderUtils.deleteAllTracks(context);
|
myTracksProviderUtils.deleteAllTracks(context);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -36,9 +36,7 @@ import com.google.android.apps.mytracks.util.TrackRecordingServiceConnectionUtil
|
|||||||
import com.google.android.apps.mytracks.util.TrackUtils;
|
import com.google.android.apps.mytracks.util.TrackUtils;
|
||||||
import com.google.android.maps.mytracks.R;
|
import com.google.android.maps.mytracks.R;
|
||||||
|
|
||||||
import android.accounts.Account;
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -307,19 +305,7 @@ public class TrackEditActivity extends AbstractMyTracksActivity implements Choos
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onDrivePermissionSuccess() {
|
private void onDrivePermissionSuccess() {
|
||||||
PreferencesUtils.setBoolean(this, R.string.drive_sync_key, true);
|
SyncUtils.enableSync(this);
|
||||||
|
|
||||||
// Turn off everything
|
|
||||||
SyncUtils.disableSync(this);
|
|
||||||
|
|
||||||
// Turn on sync
|
|
||||||
ContentResolver.setMasterSyncAutomatically(true);
|
|
||||||
|
|
||||||
// Enable sync for account
|
|
||||||
String googleAccount = PreferencesUtils.getString(
|
|
||||||
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
|
||||||
Account account = new Account(googleAccount, Constants.ACCOUNT_TYPE);
|
|
||||||
SyncUtils.enableSync(account);
|
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utilites for Google Drive sync.
|
* Utilities for Google Drive sync.
|
||||||
*
|
*
|
||||||
* @author Jimmy Shih
|
* @author Jimmy Shih
|
||||||
*/
|
*/
|
||||||
@@ -124,6 +124,23 @@ public class SyncUtils {
|
|||||||
* @param context the context
|
* @param context the context
|
||||||
*/
|
*/
|
||||||
public static void disableSync(Context context) {
|
public static void disableSync(Context context) {
|
||||||
|
// Set preference
|
||||||
|
PreferencesUtils.setBoolean(
|
||||||
|
context, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
|
||||||
|
|
||||||
|
// Disable sync for all accounts
|
||||||
|
disableSyncForAll(context);
|
||||||
|
|
||||||
|
// Clear sync state
|
||||||
|
clearSyncState(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disables sync for all accounts.
|
||||||
|
*
|
||||||
|
* @param context the context
|
||||||
|
*/
|
||||||
|
private static void disableSyncForAll(Context context) {
|
||||||
Account[] accounts = AccountManager.get(context).getAccountsByType(Constants.ACCOUNT_TYPE);
|
Account[] accounts = AccountManager.get(context).getAccountsByType(Constants.ACCOUNT_TYPE);
|
||||||
for (Account account : accounts) {
|
for (Account account : accounts) {
|
||||||
ContentResolver.cancelSync(account, SYNC_AUTHORITY);
|
ContentResolver.cancelSync(account, SYNC_AUTHORITY);
|
||||||
@@ -131,7 +148,7 @@ public class SyncUtils {
|
|||||||
ContentResolver.setSyncAutomatically(account, SYNC_AUTHORITY, false);
|
ContentResolver.setSyncAutomatically(account, SYNC_AUTHORITY, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if sync is active.
|
* Returns true if sync is active.
|
||||||
*
|
*
|
||||||
@@ -146,13 +163,35 @@ public class SyncUtils {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables sync.
|
* Enables sync.
|
||||||
*
|
*
|
||||||
|
* @param context the context
|
||||||
|
*/
|
||||||
|
public static void enableSync(Context context) {
|
||||||
|
// Set preference
|
||||||
|
PreferencesUtils.setBoolean(context, R.string.drive_sync_key, true);
|
||||||
|
|
||||||
|
// Disable sync for all accounts
|
||||||
|
disableSyncForAll(context);
|
||||||
|
|
||||||
|
// Turn on sync
|
||||||
|
ContentResolver.setMasterSyncAutomatically(true);
|
||||||
|
|
||||||
|
// Enable sync for account
|
||||||
|
String googleAccount = PreferencesUtils.getString(
|
||||||
|
context, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
||||||
|
enableSyncForAccount(new Account(googleAccount, Constants.ACCOUNT_TYPE));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables sync for an account.
|
||||||
|
*
|
||||||
* @param account the account
|
* @param account the account
|
||||||
*/
|
*/
|
||||||
public static void enableSync(Account account) {
|
private static void enableSyncForAccount(Account account) {
|
||||||
ContentResolver.setIsSyncable(account, SYNC_AUTHORITY, 1);
|
ContentResolver.setIsSyncable(account, SYNC_AUTHORITY, 1);
|
||||||
ContentResolver.setSyncAutomatically(account, SYNC_AUTHORITY, true);
|
ContentResolver.setSyncAutomatically(account, SYNC_AUTHORITY, true);
|
||||||
ContentResolver.requestSync(account, SYNC_AUTHORITY, new Bundle());
|
ContentResolver.requestSync(account, SYNC_AUTHORITY, new Bundle());
|
||||||
@@ -162,7 +201,7 @@ public class SyncUtils {
|
|||||||
* Clears the sync state. Assumes sync is turned off. Do not want clearing the
|
* Clears the sync state. Assumes sync is turned off. Do not want clearing the
|
||||||
* sync state to cause sync activities.
|
* sync state to cause sync activities.
|
||||||
*/
|
*/
|
||||||
public static void clearSyncState(Context context) {
|
private static void clearSyncState(Context context) {
|
||||||
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
|
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
|
||||||
Cursor cursor = null;
|
Cursor cursor = null;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -199,7 +199,6 @@ public class AdvancedSettingsActivity extends AbstractSettingsActivity {
|
|||||||
sharedPreferences.edit().clear().commit();
|
sharedPreferences.edit().clear().commit();
|
||||||
|
|
||||||
SyncUtils.disableSync(AdvancedSettingsActivity.this);
|
SyncUtils.disableSync(AdvancedSettingsActivity.this);
|
||||||
SyncUtils.clearSyncState(AdvancedSettingsActivity.this);
|
|
||||||
|
|
||||||
// Give UI feedback in the UI thread
|
// Give UI feedback in the UI thread
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ import android.accounts.AccountManager;
|
|||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.DialogInterface.OnClickListener;
|
import android.content.DialogInterface.OnClickListener;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -244,26 +243,10 @@ public class SettingsActivity extends AbstractSettingsActivity implements CheckP
|
|||||||
syncDrivePreference.setChecked(value);
|
syncDrivePreference.setChecked(value);
|
||||||
updateSyncDriveSummary();
|
updateSyncDriveSummary();
|
||||||
|
|
||||||
// Turn off everything
|
|
||||||
SyncUtils.disableSync(this);
|
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
|
SyncUtils.enableSync(this);
|
||||||
// Turn on sync
|
} else {
|
||||||
ContentResolver.setMasterSyncAutomatically(true);
|
SyncUtils.disableSync(this);
|
||||||
|
|
||||||
// Enable sync for account
|
|
||||||
String googleAccount = PreferencesUtils.getString(
|
|
||||||
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
|
||||||
Account[] accounts = AccountManager.get(this).getAccountsByType(Constants.ACCOUNT_TYPE);
|
|
||||||
for (Account account : accounts) {
|
|
||||||
if (account.name.equals(googleAccount)) {
|
|
||||||
SyncUtils.enableSync(account);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
SyncUtils.clearSyncState(this);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user