mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 09:33:06 +02:00
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.OperationCanceledException;
|
||||
import android.app.Activity;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
@@ -343,16 +342,7 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv
|
||||
Class<?> next;
|
||||
if (sendRequest.isSendDrive()) {
|
||||
if (sendRequest.isDriveSync()) {
|
||||
PreferencesUtils.setBoolean(this, R.string.drive_sync_key, true);
|
||||
|
||||
// Turn off everything
|
||||
SyncUtils.disableSync(this);
|
||||
|
||||
// Turn on sync
|
||||
ContentResolver.setMasterSyncAutomatically(true);
|
||||
|
||||
// Enable sync for account
|
||||
SyncUtils.enableSync(sendRequest.getAccount());
|
||||
SyncUtils.enableSync(this);
|
||||
return;
|
||||
} else {
|
||||
next = SendDriveActivity.class;
|
||||
|
||||
@@ -91,10 +91,7 @@ public class DeleteAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
} catch (InterruptedException e) {
|
||||
return false;
|
||||
}
|
||||
PreferencesUtils.setBoolean(
|
||||
context, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
|
||||
SyncUtils.disableSync(context);
|
||||
SyncUtils.clearSyncState(context);
|
||||
myTracksProviderUtils.deleteAllTracks(context);
|
||||
return true;
|
||||
} 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.maps.mytracks.R;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.app.Activity;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
@@ -307,19 +305,7 @@ public class TrackEditActivity extends AbstractMyTracksActivity implements Choos
|
||||
}
|
||||
|
||||
private void onDrivePermissionSuccess() {
|
||||
PreferencesUtils.setBoolean(this, R.string.drive_sync_key, true);
|
||||
|
||||
// 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);
|
||||
SyncUtils.enableSync(this);
|
||||
finish();
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ import java.util.ArrayList;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Utilites for Google Drive sync.
|
||||
* Utilities for Google Drive sync.
|
||||
*
|
||||
* @author Jimmy Shih
|
||||
*/
|
||||
@@ -124,6 +124,23 @@ public class SyncUtils {
|
||||
* @param context the 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);
|
||||
for (Account account : accounts) {
|
||||
ContentResolver.cancelSync(account, SYNC_AUTHORITY);
|
||||
@@ -131,7 +148,7 @@ public class SyncUtils {
|
||||
ContentResolver.setSyncAutomatically(account, SYNC_AUTHORITY, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if sync is active.
|
||||
*
|
||||
@@ -146,13 +163,35 @@ public class SyncUtils {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public static void enableSync(Account account) {
|
||||
private static void enableSyncForAccount(Account account) {
|
||||
ContentResolver.setIsSyncable(account, SYNC_AUTHORITY, 1);
|
||||
ContentResolver.setSyncAutomatically(account, SYNC_AUTHORITY, true);
|
||||
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
|
||||
* sync state to cause sync activities.
|
||||
*/
|
||||
public static void clearSyncState(Context context) {
|
||||
private static void clearSyncState(Context context) {
|
||||
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
|
||||
@@ -199,7 +199,6 @@ public class AdvancedSettingsActivity extends AbstractSettingsActivity {
|
||||
sharedPreferences.edit().clear().commit();
|
||||
|
||||
SyncUtils.disableSync(AdvancedSettingsActivity.this);
|
||||
SyncUtils.clearSyncState(AdvancedSettingsActivity.this);
|
||||
|
||||
// Give UI feedback in the UI thread
|
||||
runOnUiThread(new Runnable() {
|
||||
|
||||
@@ -31,7 +31,6 @@ import android.accounts.AccountManager;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnClickListener;
|
||||
import android.content.Intent;
|
||||
@@ -244,26 +243,10 @@ public class SettingsActivity extends AbstractSettingsActivity implements CheckP
|
||||
syncDrivePreference.setChecked(value);
|
||||
updateSyncDriveSummary();
|
||||
|
||||
// Turn off everything
|
||||
SyncUtils.disableSync(this);
|
||||
|
||||
if (value) {
|
||||
|
||||
// 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[] 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);
|
||||
SyncUtils.enableSync(this);
|
||||
} else {
|
||||
SyncUtils.disableSync(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user