Fix bug #3248308 (On account set up, turn on sync for all apps by default; delay sync until after user selects Finish)

- reuse code by making AccountSyncSettings work in two modes: either use SyncManager or not

Change-Id: I29fc6820aa41ad4689745ed97d38248474033e7e
This commit is contained in:
Fabrice Di Meglio
2010-12-14 19:45:11 -08:00
parent 7553ebea45
commit 31ffa0439d
2 changed files with 71 additions and 47 deletions

View File

@@ -50,8 +50,6 @@ import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
@@ -74,17 +72,17 @@ public class AccountSyncSettings extends AccountPreferenceBase {
private TextView mProviderId; private TextView mProviderId;
private ImageView mProviderIcon; private ImageView mProviderIcon;
private TextView mErrorInfoView; private TextView mErrorInfoView;
protected View mRemoveAccountArea;
private java.text.DateFormat mDateFormat; private java.text.DateFormat mDateFormat;
private java.text.DateFormat mTimeFormat; private java.text.DateFormat mTimeFormat;
private Account mAccount; private Account mAccount;
// List of all accounts, updated when accounts are added/removed // List of all accounts, updated when accounts are added/removed
// We need to re-scan the accounts on sync events, in case sync state changes. // We need to re-scan the accounts on sync events, in case sync state changes.
private Account[] mAccounts; private Account[] mAccounts;
private Button mRemoveAccountButton;
private ArrayList<SyncStateCheckBoxPreference> mCheckBoxes = private ArrayList<SyncStateCheckBoxPreference> mCheckBoxes =
new ArrayList<SyncStateCheckBoxPreference>(); new ArrayList<SyncStateCheckBoxPreference>();
private ArrayList<String> mInvisibleAdapters = Lists.newArrayList(); private ArrayList<String> mInvisibleAdapters = Lists.newArrayList();
// Tell if we will use the current SyncManager data or just the User input
protected boolean mUseSyncManagerFeedsState = true;
@Override @Override
public Dialog onCreateDialog(final int id) { public Dialog onCreateDialog(final int id) {
@@ -266,7 +264,7 @@ public class AccountSyncSettings extends AccountPreferenceBase {
@Override @Override
public boolean onPreferenceTreeClick(PreferenceScreen preferences, Preference preference) { public boolean onPreferenceTreeClick(PreferenceScreen preferences, Preference preference) {
if (preference instanceof SyncStateCheckBoxPreference) { if (preference instanceof SyncStateCheckBoxPreference && mUseSyncManagerFeedsState) {
SyncStateCheckBoxPreference syncPref = (SyncStateCheckBoxPreference) preference; SyncStateCheckBoxPreference syncPref = (SyncStateCheckBoxPreference) preference;
String authority = syncPref.getAuthority(); String authority = syncPref.getAuthority();
Account account = syncPref.getAccount(); Account account = syncPref.getAccount();
@@ -347,6 +345,10 @@ public class AccountSyncSettings extends AccountPreferenceBase {
@Override @Override
protected void onSyncStateUpdated() { protected void onSyncStateUpdated() {
if (!isResumed()) return; if (!isResumed()) return;
setFeedsState(mUseSyncManagerFeedsState);
}
private void setFeedsState(boolean useSyncManager) {
// iterate over all the preferences, setting the state properly for each // iterate over all the preferences, setting the state properly for each
Date date = new Date(); Date date = new Date();
List<SyncInfo> currentSyncs = ContentResolver.getCurrentSyncs(); List<SyncInfo> currentSyncs = ContentResolver.getCurrentSyncs();
@@ -365,49 +367,53 @@ public class AccountSyncSettings extends AccountPreferenceBase {
String authority = syncPref.getAuthority(); String authority = syncPref.getAuthority();
Account account = syncPref.getAccount(); Account account = syncPref.getAccount();
SyncStatusInfo status = ContentResolver.getSyncStatus(account, authority); if (useSyncManager) {
boolean syncEnabled = ContentResolver.getSyncAutomatically(account, authority); SyncStatusInfo status = ContentResolver.getSyncStatus(account, authority);
boolean authorityIsPending = status == null ? false : status.pending; boolean syncEnabled = ContentResolver.getSyncAutomatically(account, authority);
boolean initialSync = status == null ? false : status.initialize; boolean authorityIsPending = status == null ? false : status.pending;
boolean initialSync = status == null ? false : status.initialize;
boolean activelySyncing = isSyncing(currentSyncs, account, authority); boolean activelySyncing = isSyncing(currentSyncs, account, authority);
boolean lastSyncFailed = status != null boolean lastSyncFailed = status != null
&& status.lastFailureTime != 0 && status.lastFailureTime != 0
&& status.getLastFailureMesgAsInt(0) && status.getLastFailureMesgAsInt(0)
!= ContentResolver.SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS; != ContentResolver.SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS;
if (!syncEnabled) lastSyncFailed = false; if (!syncEnabled) lastSyncFailed = false;
if (lastSyncFailed && !activelySyncing && !authorityIsPending) { if (lastSyncFailed && !activelySyncing && !authorityIsPending) {
syncIsFailing = true; syncIsFailing = true;
} }
if (Log.isLoggable(TAG, Log.VERBOSE)) { if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.d(TAG, "Update sync status: " + account + " " + authority + Log.d(TAG, "Update sync status: " + account + " " + authority +
" active = " + activelySyncing + " pend =" + authorityIsPending); " active = " + activelySyncing + " pend =" + authorityIsPending);
} }
final long successEndTime = (status == null) ? 0 : status.lastSuccessTime; final long successEndTime = (status == null) ? 0 : status.lastSuccessTime;
if (successEndTime != 0) { if (successEndTime != 0) {
date.setTime(successEndTime); date.setTime(successEndTime);
final String timeString = mDateFormat.format(date) + " " final String timeString = mDateFormat.format(date) + " "
+ mTimeFormat.format(date); + mTimeFormat.format(date);
syncPref.setSummary(timeString); syncPref.setSummary(timeString);
} else {
syncPref.setSummary("");
}
int syncState = ContentResolver.getIsSyncable(account, authority);
syncPref.setActive(activelySyncing && (syncState >= 0) &&
!initialSync);
syncPref.setPending(authorityIsPending && (syncState >= 0) &&
!initialSync);
syncPref.setFailed(lastSyncFailed);
ConnectivityManager connManager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final boolean masterSyncAutomatically = ContentResolver.getMasterSyncAutomatically();
final boolean backgroundDataEnabled = connManager.getBackgroundDataSetting();
final boolean oneTimeSyncMode = !masterSyncAutomatically || !backgroundDataEnabled;
syncPref.setOneTimeSyncMode(oneTimeSyncMode);
syncPref.setChecked(oneTimeSyncMode || syncEnabled);
} else { } else {
syncPref.setSummary(""); syncPref.setChecked(true);
} }
int syncState = ContentResolver.getIsSyncable(account, authority);
syncPref.setActive(activelySyncing && (syncState >= 0) &&
!initialSync);
syncPref.setPending(authorityIsPending && (syncState >= 0) &&
!initialSync);
syncPref.setFailed(lastSyncFailed);
ConnectivityManager connManager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final boolean masterSyncAutomatically = ContentResolver.getMasterSyncAutomatically();
final boolean backgroundDataEnabled = connManager.getBackgroundDataSetting();
final boolean oneTimeSyncMode = !masterSyncAutomatically || !backgroundDataEnabled;
syncPref.setOneTimeSyncMode(oneTimeSyncMode);
syncPref.setChecked(oneTimeSyncMode || syncEnabled);
} }
mErrorInfoView.setVisibility(syncIsFailing ? View.VISIBLE : View.GONE); mErrorInfoView.setVisibility(syncIsFailing ? View.VISIBLE : View.GONE);
getActivity().invalidateOptionsMenu(); getActivity().invalidateOptionsMenu();

View File

@@ -1,11 +1,11 @@
package com.android.settings.accounts; package com.android.settings.accounts;
import android.accounts.Account;
import android.content.ContentResolver;
import android.preference.Preference;
import com.android.settings.R; import com.android.settings.R;
import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
@@ -27,6 +27,8 @@ public class AccountSyncSettingsInAddAccount extends AccountSyncSettings
mFinishArea.setVisibility(View.VISIBLE); mFinishArea.setVisibility(View.VISIBLE);
mFinishButton = (View) rootView.findViewById(R.id.finish_button); mFinishButton = (View) rootView.findViewById(R.id.finish_button);
mFinishButton.setOnClickListener(this); mFinishButton.setOnClickListener(this);
mUseSyncManagerFeedsState = false;
} }
@Override @Override
@@ -37,6 +39,22 @@ public class AccountSyncSettingsInAddAccount extends AccountSyncSettings
} }
public void onClick(View v) { public void onClick(View v) {
applySyncSettingsToSyncManager();
finish(); finish();
} }
private void applySyncSettingsToSyncManager() {
for (int i = 0, count = getPreferenceScreen().getPreferenceCount(); i < count; i++) {
Preference pref = getPreferenceScreen().getPreference(i);
if (! (pref instanceof SyncStateCheckBoxPreference)) {
continue;
}
SyncStateCheckBoxPreference syncPref = (SyncStateCheckBoxPreference) pref;
String authority = syncPref.getAuthority();
Account account = syncPref.getAccount();
ContentResolver.setSyncAutomatically(account, authority, syncPref.isChecked());
}
}
} }