diff options
Diffstat (limited to 'src/com/android/settings/accounts')
6 files changed, 547 insertions, 159 deletions
diff --git a/src/com/android/settings/accounts/AccountPreferenceBase.java b/src/com/android/settings/accounts/AccountPreferenceBase.java index a0d6a7f..2759a8f 100644 --- a/src/com/android/settings/accounts/AccountPreferenceBase.java +++ b/src/com/android/settings/accounts/AccountPreferenceBase.java @@ -17,6 +17,7 @@ package com.android.settings.accounts; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -27,6 +28,7 @@ import android.accounts.Account; import android.accounts.AccountManager; import android.accounts.AuthenticatorDescription; import android.accounts.OnAccountsUpdateListener; +import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.content.SyncAdapterType; @@ -38,6 +40,7 @@ import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; +import android.text.format.DateFormat; import android.util.Log; class AccountPreferenceBase extends SettingsPreferenceFragment @@ -46,12 +49,12 @@ class AccountPreferenceBase extends SettingsPreferenceFragment protected static final String TAG = "AccountSettings"; public static final String AUTHORITIES_FILTER_KEY = "authorities"; public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types"; - private Map<String, AuthenticatorDescription> mTypeToAuthDescription - = new HashMap<String, AuthenticatorDescription>(); - protected AuthenticatorDescription[] mAuthDescs; private final Handler mHandler = new Handler(); private Object mStatusChangeListenerHandle; private HashMap<String, ArrayList<String>> mAccountTypeToAuthorities = null; + private AuthenticatorHelper mAuthenticatorHelper = new AuthenticatorHelper(); + private java.text.DateFormat mDateFormat; + private java.text.DateFormat mTimeFormat; /** * Overload to handle account updates. @@ -75,6 +78,16 @@ class AccountPreferenceBase extends SettingsPreferenceFragment } @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + final Activity activity = getActivity(); + + mDateFormat = DateFormat.getDateFormat(activity); + mTimeFormat = DateFormat.getTimeFormat(activity); + } + + @Override public void onResume() { super.onResume(); mStatusChangeListenerHandle = ContentResolver.addStatusChangeListener( @@ -91,7 +104,6 @@ class AccountPreferenceBase extends SettingsPreferenceFragment ContentResolver.removeStatusChangeListener(mStatusChangeListenerHandle); } - private SyncStatusObserver mSyncStatusObserver = new SyncStatusObserver() { public void onStatusChanged(int which) { mHandler.post(new Runnable() { @@ -124,64 +136,21 @@ class AccountPreferenceBase extends SettingsPreferenceFragment } /** - * Gets an icon associated with a particular account type. If none found, return null. - * @param accountType the type of account - * @return a drawable for the icon or null if one cannot be found. - */ - protected Drawable getDrawableForType(final String accountType) { - Drawable icon = null; - if (mTypeToAuthDescription.containsKey(accountType)) { - try { - AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType); - Context authContext = getActivity().createPackageContext(desc.packageName, 0); - icon = authContext.getResources().getDrawable(desc.iconId); - } catch (PackageManager.NameNotFoundException e) { - // TODO: place holder icon for missing account icons? - Log.w(TAG, "No icon name for account type " + accountType); - } catch (Resources.NotFoundException e) { - // TODO: place holder icon for missing account icons? - Log.w(TAG, "No icon resource for account type " + accountType); - } - } - return icon; - } - - /** - * Gets the label associated with a particular account type. If none found, return null. - * @param accountType the type of account - * @return a CharSequence for the label or null if one cannot be found. - */ - protected CharSequence getLabelForType(final String accountType) { - CharSequence label = null; - if (mTypeToAuthDescription.containsKey(accountType)) { - try { - AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType); - Context authContext = getActivity().createPackageContext(desc.packageName, 0); - label = authContext.getResources().getText(desc.labelId); - } catch (PackageManager.NameNotFoundException e) { - Log.w(TAG, "No label name for account type " + accountType); - } catch (Resources.NotFoundException e) { - Log.w(TAG, "No label icon for account type " + accountType); - } - } - return label; - } - - /** * Gets the preferences.xml file associated with a particular account type. * @param accountType the type of account * @return a PreferenceScreen inflated from accountPreferenceId. */ - protected PreferenceScreen addPreferencesForType(final String accountType) { + public PreferenceScreen addPreferencesForType(final String accountType, + PreferenceScreen parent) { PreferenceScreen prefs = null; - if (mTypeToAuthDescription.containsKey(accountType)) { + if (mAuthenticatorHelper.containsAccountType(accountType)) { AuthenticatorDescription desc = null; try { - desc = mTypeToAuthDescription.get(accountType); + desc = mAuthenticatorHelper.getAccountTypeDescription(accountType); if (desc != null && desc.accountPreferencesId != 0) { Context authContext = getActivity().createPackageContext(desc.packageName, 0); prefs = getPreferenceManager().inflateFromResource(authContext, - desc.accountPreferencesId, getPreferenceScreen()); + desc.accountPreferencesId, parent); } } catch (PackageManager.NameNotFoundException e) { Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName); @@ -192,15 +161,21 @@ class AccountPreferenceBase extends SettingsPreferenceFragment return prefs; } - /** - * Updates provider icons. Subclasses should call this in onCreate() - * and update any UI that depends on AuthenticatorDescriptions in onAuthDescriptionsUpdated(). - */ - protected void updateAuthDescriptions() { - mAuthDescs = AccountManager.get(getActivity()).getAuthenticatorTypes(); - for (int i = 0; i < mAuthDescs.length; i++) { - mTypeToAuthDescription.put(mAuthDescs[i].type, mAuthDescs[i]); - } + public void updateAuthDescriptions() { + mAuthenticatorHelper.updateAuthDescriptions(getActivity()); onAuthDescriptionsUpdated(); } + + protected Drawable getDrawableForType(final String accountType) { + return mAuthenticatorHelper.getDrawableForType(getActivity(), accountType); + } + + protected CharSequence getLabelForType(final String accountType) { + return mAuthenticatorHelper.getLabelForType(getActivity(), accountType); + } + + protected String formatSyncDate(Date date) { + // TODO: Switch to using DateUtils.formatDateTime + return mDateFormat.format(date) + " " + mTimeFormat.format(date); + } } diff --git a/src/com/android/settings/accounts/AccountSyncSettings.java b/src/com/android/settings/accounts/AccountSyncSettings.java index 6847607..196908e 100644 --- a/src/com/android/settings/accounts/AccountSyncSettings.java +++ b/src/com/android/settings/accounts/AccountSyncSettings.java @@ -63,9 +63,9 @@ import java.util.List; public class AccountSyncSettings extends AccountPreferenceBase { public static final String ACCOUNT_KEY = "account"; - protected static final int MENU_REMOVE_ACCOUNT_ID = Menu.FIRST; - private static final int MENU_SYNC_NOW_ID = Menu.FIRST + 1; - private static final int MENU_SYNC_CANCEL_ID = Menu.FIRST + 2; + private static final int MENU_SYNC_NOW_ID = Menu.FIRST; + private static final int MENU_SYNC_CANCEL_ID = Menu.FIRST + 1; + private static final int MENU_REMOVE_ACCOUNT_ID = Menu.FIRST + 2; private static final int REALLY_REMOVE_DIALOG = 100; private static final int FAILED_REMOVAL_DIALOG = 101; private static final int CANT_DO_ONETIME_SYNC_DIALOG = 102; @@ -73,8 +73,6 @@ public class AccountSyncSettings extends AccountPreferenceBase { private TextView mProviderId; private ImageView mProviderIcon; private TextView mErrorInfoView; - private java.text.DateFormat mDateFormat; - private java.text.DateFormat mTimeFormat; private Account mAccount; // 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. @@ -98,6 +96,10 @@ public class AccountSyncSettings extends AccountPreferenceBase { .removeAccount(mAccount, new AccountManagerCallback<Boolean>() { public void run(AccountManagerFuture<Boolean> future) { + // If already out of this screen, don't proceed. + if (!AccountSyncSettings.this.isResumed()) { + return; + } boolean failed = true; try { if (future.getResult() == true) { @@ -168,11 +170,6 @@ public class AccountSyncSettings extends AccountPreferenceBase { public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - final Activity activity = getActivity(); - - mDateFormat = DateFormat.getDateFormat(activity); - mTimeFormat = DateFormat.getTimeFormat(activity); - Bundle arguments = getArguments(); if (arguments == null) { Log.e(TAG, "No arguments provided when starting intent. ACCOUNT_KEY needed."); @@ -208,11 +205,13 @@ public class AccountSyncSettings extends AccountPreferenceBase { new SyncStateCheckBoxPreference(getActivity(), account, authority); item.setPersistent(false); final ProviderInfo providerInfo = getPackageManager().resolveContentProvider(authority, 0); - CharSequence providerLabel = providerInfo != null - ? providerInfo.loadLabel(getPackageManager()) : null; + if (providerInfo == null) { + return; + } + CharSequence providerLabel = providerInfo.loadLabel(getPackageManager()); if (TextUtils.isEmpty(providerLabel)) { Log.e(TAG, "Provider needs a label for authority '" + authority + "'"); - providerLabel = authority; + return; } String title = getString(R.string.sync_item_title, providerLabel); item.setTitle(title); @@ -222,17 +221,16 @@ public class AccountSyncSettings extends AccountPreferenceBase { @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - super.onCreateOptionsMenu(menu, inflater); - MenuItem removeAccount = menu.add(0, MENU_REMOVE_ACCOUNT_ID, 0, - getString(R.string.remove_account_label)) - .setIcon(R.drawable.ic_menu_delete_holo_dark); MenuItem syncNow = menu.add(0, MENU_SYNC_NOW_ID, 0, - getString(R.string.sync_menu_sync_now)) + getString(R.string.sync_menu_sync_now)) .setIcon(R.drawable.ic_menu_refresh_holo_dark); MenuItem syncCancel = menu.add(0, MENU_SYNC_CANCEL_ID, 0, - getString(R.string.sync_menu_sync_cancel)) + getString(R.string.sync_menu_sync_cancel)) .setIcon(com.android.internal.R.drawable.ic_menu_close_clear_cancel); + MenuItem removeAccount = menu.add(0, MENU_REMOVE_ACCOUNT_ID, 0, + getString(R.string.remove_account_label)) + .setIcon(R.drawable.ic_menu_delete_holo_dark); removeAccount.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT); @@ -240,6 +238,8 @@ public class AccountSyncSettings extends AccountPreferenceBase { MenuItem.SHOW_AS_ACTION_WITH_TEXT); syncCancel.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + + super.onCreateOptionsMenu(menu, inflater); } @Override @@ -394,11 +394,14 @@ public class AccountSyncSettings extends AccountPreferenceBase { } final long successEndTime = (status == null) ? 0 : status.lastSuccessTime; - if (successEndTime != 0) { + if (!syncEnabled) { + syncPref.setSummary(R.string.sync_disabled); + } else if (activelySyncing) { + syncPref.setSummary(R.string.sync_in_progress); + } else if (successEndTime != 0) { date.setTime(successEndTime); - final String timeString = mDateFormat.format(date) + " " - + mTimeFormat.format(date); - syncPref.setSummary(timeString); + final String timeString = formatSyncDate(date); + syncPref.setSummary(getResources().getString(R.string.last_synced, timeString)); } else { syncPref.setSummary(""); } @@ -498,25 +501,12 @@ public class AccountSyncSettings extends AccountPreferenceBase { if (mAccount != null) { mProviderIcon.setImageDrawable(getDrawableForType(mAccount.type)); mProviderId.setText(getLabelForType(mAccount.type)); - PreferenceScreen prefs = addPreferencesForType(mAccount.type); - if (prefs != null) { - updatePreferenceIntents(prefs); - } } addPreferencesFromResource(R.xml.account_sync_settings); } - private void updatePreferenceIntents(PreferenceScreen prefs) { - for (int i = 0; i < prefs.getPreferenceCount(); i++) { - Intent intent = prefs.getPreference(i).getIntent(); - if (intent != null) { - intent.putExtra(ACCOUNT_KEY, mAccount); - // This is somewhat of a hack. Since the preference screen we're accessing comes - // from another package, we need to modify the intent to launch it with - // FLAG_ACTIVITY_NEW_TASK. - // TODO: Do something smarter if we ever have PreferenceScreens of our own. - intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); - } - } + @Override + protected int getHelpResource() { + return R.string.help_url_accounts; } } diff --git a/src/com/android/settings/accounts/AuthenticatorHelper.java b/src/com/android/settings/accounts/AuthenticatorHelper.java new file mode 100644 index 0000000..ab2fe74 --- /dev/null +++ b/src/com/android/settings/accounts/AuthenticatorHelper.java @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.accounts; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.accounts.AuthenticatorDescription; +import android.content.Context; +import android.content.pm.PackageManager; +import android.content.res.Resources; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.ScaleDrawable; +import android.util.Log; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class AuthenticatorHelper { + + private static final String TAG = "AccountTypesHelper"; + private Map<String, AuthenticatorDescription> mTypeToAuthDescription + = new HashMap<String, AuthenticatorDescription>(); + private AuthenticatorDescription[] mAuthDescs; + private ArrayList<String> mEnabledAccountTypes = new ArrayList<String>(); + private Map<String, Drawable> mAccTypeIconCache = new HashMap<String, Drawable>(); + + public AuthenticatorHelper() { + } + + public String[] getEnabledAccountTypes() { + return mEnabledAccountTypes.toArray(new String[mEnabledAccountTypes.size()]); + } + + /** + * Gets an icon associated with a particular account type. If none found, return null. + * @param accountType the type of account + * @return a drawable for the icon or null if one cannot be found. + */ + public Drawable getDrawableForType(Context context, final String accountType) { + Drawable icon = null; + if (mAccTypeIconCache.containsKey(accountType)) { + return mAccTypeIconCache.get(accountType); + } + if (mTypeToAuthDescription.containsKey(accountType)) { + try { + AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType); + Context authContext = context.createPackageContext(desc.packageName, 0); + icon = authContext.getResources().getDrawable(desc.iconId); + mAccTypeIconCache.put(accountType, icon); + } catch (PackageManager.NameNotFoundException e) { + } catch (Resources.NotFoundException e) { + } + } + if (icon == null) { + icon = context.getPackageManager().getDefaultActivityIcon(); + } + return icon; + } + + /** + * Gets the label associated with a particular account type. If none found, return null. + * @param accountType the type of account + * @return a CharSequence for the label or null if one cannot be found. + */ + public CharSequence getLabelForType(Context context, final String accountType) { + CharSequence label = null; + if (mTypeToAuthDescription.containsKey(accountType)) { + try { + AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType); + Context authContext = context.createPackageContext(desc.packageName, 0); + label = authContext.getResources().getText(desc.labelId); + } catch (PackageManager.NameNotFoundException e) { + Log.w(TAG, "No label name for account type " + accountType); + } catch (Resources.NotFoundException e) { + Log.w(TAG, "No label icon for account type " + accountType); + } + } + return label; + } + + /** + * Updates provider icons. Subclasses should call this in onCreate() + * and update any UI that depends on AuthenticatorDescriptions in onAuthDescriptionsUpdated(). + */ + public void updateAuthDescriptions(Context context) { + mAuthDescs = AccountManager.get(context).getAuthenticatorTypes(); + for (int i = 0; i < mAuthDescs.length; i++) { + mTypeToAuthDescription.put(mAuthDescs[i].type, mAuthDescs[i]); + } + } + + public void onAccountsUpdated(Context context, Account[] accounts) { + if (accounts == null) { + accounts = AccountManager.get(context).getAccounts(); + } + mEnabledAccountTypes.clear(); + mAccTypeIconCache.clear(); + for (Account account: accounts) { + if (!mEnabledAccountTypes.contains(account.type)) { + mEnabledAccountTypes.add(account.type); + } + } + } + + public boolean containsAccountType(String accountType) { + return mTypeToAuthDescription.containsKey(accountType); + } + + public AuthenticatorDescription getAccountTypeDescription(String accountType) { + return mTypeToAuthDescription.get(accountType); + } + + public boolean hasAccountPreferences(final String accountType) { + if (containsAccountType(accountType)) { + AuthenticatorDescription desc = getAccountTypeDescription(accountType); + if (desc != null && desc.accountPreferencesId != 0) { + return true; + } + } + return false; + } +} diff --git a/src/com/android/settings/accounts/ManageAccountsSettings.java b/src/com/android/settings/accounts/ManageAccountsSettings.java index 0177491..bb1ebdd 100644 --- a/src/com/android/settings/accounts/ManageAccountsSettings.java +++ b/src/com/android/settings/accounts/ManageAccountsSettings.java @@ -21,16 +21,22 @@ import android.accounts.AccountManager; import android.accounts.OnAccountsUpdateListener; import android.app.ActionBar; import android.app.Activity; +import android.app.ActivityManager; +import android.app.ActivityManagerNative; import android.content.ContentResolver; +import android.content.Context; import android.content.Intent; import android.content.SyncAdapterType; import android.content.SyncInfo; import android.content.SyncStatusInfo; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; +import android.text.format.DateFormat; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; @@ -44,16 +50,22 @@ import android.widget.Switch; import android.widget.TextView; import com.android.settings.AccountPreference; -import com.android.settings.DialogCreatable; import com.android.settings.R; +import com.android.settings.Settings; import java.util.ArrayList; +import java.util.Date; import java.util.HashSet; public class ManageAccountsSettings extends AccountPreferenceBase - implements OnAccountsUpdateListener, DialogCreatable { + implements OnAccountsUpdateListener { - private static final int MENU_ADD_ACCOUNT = Menu.FIRST; + private static final String ACCOUNT_KEY = "account"; // to pass to auth settings + public static final String KEY_ACCOUNT_TYPE = "account_type"; + public static final String KEY_ACCOUNT_LABEL = "account_label"; + + private static final int MENU_SYNC_NOW_ID = Menu.FIRST; + private static final int MENU_SYNC_CANCEL_ID = Menu.FIRST + 1; private static final int REQUEST_SHOW_SYNC_SETTINGS = 1; @@ -61,12 +73,19 @@ public class ManageAccountsSettings extends AccountPreferenceBase private TextView mErrorInfoView; private SettingsDialogFragment mDialogFragment; - private Switch mAutoSyncSwitch; + // If an account type is set, then show only accounts of that type + private String mAccountType; + // Temporary hack, to deal with backward compatibility + private Account mFirstAccount; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); + Bundle args = getArguments(); + if (args != null && args.containsKey(KEY_ACCOUNT_TYPE)) { + mAccountType = args.getString(KEY_ACCOUNT_TYPE); + } addPreferencesFromResource(R.xml.manage_accounts_settings); setHasOptionsMenu(true); } @@ -76,12 +95,6 @@ public class ManageAccountsSettings extends AccountPreferenceBase super.onStart(); Activity activity = getActivity(); AccountManager.get(activity).addOnAccountsUpdatedListener(this, null, true); - activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, - ActionBar.DISPLAY_SHOW_CUSTOM); - activity.getActionBar().setCustomView(mAutoSyncSwitch, new ActionBar.LayoutParams( - ActionBar.LayoutParams.WRAP_CONTENT, - ActionBar.LayoutParams.WRAP_CONTENT, - Gravity.CENTER_VERTICAL | Gravity.RIGHT)); } @Override @@ -101,23 +114,12 @@ public class ManageAccountsSettings extends AccountPreferenceBase mErrorInfoView = (TextView)view.findViewById(R.id.sync_settings_error_info); mErrorInfoView.setVisibility(View.GONE); - mAutoSyncSwitch = new Switch(activity); - - // TODO Where to put the switch in tablet multipane layout? - final int padding = activity.getResources().getDimensionPixelSize( - R.dimen.action_bar_switch_padding); - mAutoSyncSwitch.setPadding(0, 0, padding, 0); - mAutoSyncSwitch.setChecked(ContentResolver.getMasterSyncAutomatically()); - mAutoSyncSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - ContentResolver.setMasterSyncAutomatically(isChecked); - onSyncStateUpdated(); - } - }); - mAuthorities = activity.getIntent().getStringArrayExtra(AUTHORITIES_FILTER_KEY); + Bundle args = getArguments(); + if (args != null && args.containsKey(KEY_ACCOUNT_LABEL)) { + getActivity().setTitle(args.getString(KEY_ACCOUNT_LABEL)); + } updateAuthDescriptions(); } @@ -150,29 +152,60 @@ public class ManageAccountsSettings extends AccountPreferenceBase } @Override - public void showDialog(int dialogId) { - if (mDialogFragment != null) { - Log.e(TAG, "Old dialog fragment not null!"); - } - mDialogFragment = new SettingsDialogFragment(this, dialogId); - mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId)); + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + MenuItem syncNow = menu.add(0, MENU_SYNC_NOW_ID, 0, + getString(R.string.sync_menu_sync_now)) + .setIcon(R.drawable.ic_menu_refresh_holo_dark); + MenuItem syncCancel = menu.add(0, MENU_SYNC_CANCEL_ID, 0, + getString(R.string.sync_menu_sync_cancel)) + .setIcon(com.android.internal.R.drawable.ic_menu_close_clear_cancel); + super.onCreateOptionsMenu(menu, inflater); } @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - MenuItem addAccountItem = menu.add(0, MENU_ADD_ACCOUNT, 0, R.string.add_account_label); - addAccountItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM - | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + public void onPrepareOptionsMenu(Menu menu) { + super.onPrepareOptionsMenu(menu); + boolean syncActive = ContentResolver.getCurrentSync() != null; + menu.findItem(MENU_SYNC_NOW_ID).setVisible(!syncActive && mFirstAccount != null); + menu.findItem(MENU_SYNC_CANCEL_ID).setVisible(syncActive && mFirstAccount != null); } @Override public boolean onOptionsItemSelected(MenuItem item) { - final int itemId = item.getItemId(); - if (itemId == MENU_ADD_ACCOUNT) { - onAddAccountClicked(); + switch (item.getItemId()) { + case MENU_SYNC_NOW_ID: + requestOrCancelSyncForAccounts(true); + return true; + case MENU_SYNC_CANCEL_ID: + requestOrCancelSyncForAccounts(false); return true; - } else { - return super.onOptionsItemSelected(item); + } + return super.onOptionsItemSelected(item); + } + + private void requestOrCancelSyncForAccounts(boolean sync) { + SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypes(); + Bundle extras = new Bundle(); + extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); + int count = getPreferenceScreen().getPreferenceCount(); + // For each account + for (int i = 0; i < count; i++) { + Preference pref = getPreferenceScreen().getPreference(i); + if (pref instanceof AccountPreference) { + Account account = ((AccountPreference) pref).getAccount(); + // For all available sync authorities, sync those that are enabled for the account + for (int j = 0; j < syncAdapters.length; j++) { + SyncAdapterType sa = syncAdapters[j]; + if (syncAdapters[j].accountType.equals(mAccountType) + && ContentResolver.getSyncAutomatically(account, sa.authority)) { + if (sync) { + ContentResolver.requestSync(account, sa.authority, extras); + } else { + ContentResolver.cancelSync(account, sa.authority); + } + } + } + } } } @@ -180,15 +213,12 @@ public class ManageAccountsSettings extends AccountPreferenceBase protected void onSyncStateUpdated() { // Catch any delayed delivery of update messages if (getActivity() == null) return; - // Set background connection state - if (mAutoSyncSwitch != null) { - mAutoSyncSwitch.setChecked(ContentResolver.getMasterSyncAutomatically()); - } // iterate over all the preferences, setting the state properly for each SyncInfo currentSync = ContentResolver.getCurrentSync(); boolean anySyncFailed = false; // true if sync on any account failed + Date date = new Date(); // only track userfacing sync adapters when deciding if account is synced or not final SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypes(); @@ -208,8 +238,10 @@ public class ManageAccountsSettings extends AccountPreferenceBase AccountPreference accountPref = (AccountPreference) pref; Account account = accountPref.getAccount(); int syncCount = 0; + long lastSuccessTime = 0; boolean syncIsFailing = false; final ArrayList<String> authorities = accountPref.getAuthorities(); + boolean syncingNow = false; if (authorities != null) { for (String authority : authorities) { SyncStatusInfo status = ContentResolver.getSyncStatus(account, authority); @@ -230,6 +262,10 @@ public class ManageAccountsSettings extends AccountPreferenceBase syncIsFailing = true; anySyncFailed = true; } + syncingNow |= activelySyncing; + if (status != null && lastSuccessTime < status.lastSuccessTime) { + lastSuccessTime = status.lastSuccessTime; + } syncCount += syncEnabled && userFacing.contains(authority) ? 1 : 0; } } else { @@ -237,15 +273,26 @@ public class ManageAccountsSettings extends AccountPreferenceBase Log.v(TAG, "no syncadapters found for " + account); } } - int syncStatus = AccountPreference.SYNC_DISABLED; if (syncIsFailing) { - syncStatus = AccountPreference.SYNC_ERROR; + accountPref.setSyncStatus(AccountPreference.SYNC_ERROR, true); } else if (syncCount == 0) { - syncStatus = AccountPreference.SYNC_DISABLED; + accountPref.setSyncStatus(AccountPreference.SYNC_DISABLED, true); } else if (syncCount > 0) { - syncStatus = AccountPreference.SYNC_ENABLED; + if (syncingNow) { + accountPref.setSyncStatus(AccountPreference.SYNC_IN_PROGRESS, true); + } else { + accountPref.setSyncStatus(AccountPreference.SYNC_ENABLED, true); + if (lastSuccessTime > 0) { + accountPref.setSyncStatus(AccountPreference.SYNC_ENABLED, false); + date.setTime(lastSuccessTime); + final String timeString = formatSyncDate(date); + accountPref.setSummary(getResources().getString( + R.string.last_synced, timeString)); + } + } + } else { + accountPref.setSyncStatus(AccountPreference.SYNC_DISABLED, true); } - accountPref.setSyncStatus(syncStatus); } mErrorInfoView.setVisibility(anySyncFailed ? View.VISIBLE : View.GONE); @@ -255,8 +302,12 @@ public class ManageAccountsSettings extends AccountPreferenceBase public void onAccountsUpdated(Account[] accounts) { if (getActivity() == null) return; getPreferenceScreen().removeAll(); + mFirstAccount = null; + addPreferencesFromResource(R.xml.manage_accounts_settings); for (int i = 0, n = accounts.length; i < n; i++) { final Account account = accounts[i]; + // If an account type is specified for this screen, skip other types + if (mAccountType != null && !account.type.equals(mAccountType)) continue; final ArrayList<String> auths = getAuthoritiesForAccountType(account.type); boolean showAccount = true; @@ -273,26 +324,53 @@ public class ManageAccountsSettings extends AccountPreferenceBase if (showAccount) { final Drawable icon = getDrawableForType(account.type); final AccountPreference preference = - new AccountPreference(getActivity(), account, icon, auths); + new AccountPreference(getActivity(), account, icon, auths, false); getPreferenceScreen().addPreference(preference); + if (mFirstAccount == null) { + mFirstAccount = account; + } } } + if (mAccountType != null && mFirstAccount != null) { + addAuthenticatorSettings(); + } onSyncStateUpdated(); } + private void addAuthenticatorSettings() { + PreferenceScreen prefs = addPreferencesForType(mAccountType, getPreferenceScreen()); + if (prefs != null) { + updatePreferenceIntents(prefs); + } + } + + private void updatePreferenceIntents(PreferenceScreen prefs) { + PackageManager pm = getActivity().getPackageManager(); + for (int i = 0; i < prefs.getPreferenceCount();) { + Intent intent = prefs.getPreference(i).getIntent(); + if (intent != null) { + ResolveInfo ri = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); + if (ri == null) { + prefs.removePreference(prefs.getPreference(i)); + continue; + } else { + intent.putExtra(ACCOUNT_KEY, mFirstAccount); + intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); + } + } + i++; + } + } + @Override protected void onAuthDescriptionsUpdated() { // Update account icons for all account preference items for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) { - AccountPreference pref = (AccountPreference) getPreferenceScreen().getPreference(i); - pref.setProviderIcon(getDrawableForType(pref.getAccount().type)); - pref.setSummary(getLabelForType(pref.getAccount().type)); + Preference pref = getPreferenceScreen().getPreference(i); + if (pref instanceof AccountPreference) { + AccountPreference accPref = (AccountPreference) pref; + accPref.setSummary(getLabelForType(accPref.getAccount().type)); + } } } - - public void onAddAccountClicked() { - Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS"); - intent.putExtra(AUTHORITIES_FILTER_KEY, mAuthorities); - startActivity(intent); - } } diff --git a/src/com/android/settings/accounts/SyncSettings.java b/src/com/android/settings/accounts/SyncSettings.java new file mode 100644 index 0000000..20c296a --- /dev/null +++ b/src/com/android/settings/accounts/SyncSettings.java @@ -0,0 +1,174 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.accounts; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.accounts.OnAccountsUpdateListener; +import android.app.Activity; +import android.content.ContentResolver; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceChangeListener; +import android.preference.PreferenceScreen; +import android.util.Log; + +import com.android.settings.AccountPreference; +import com.android.settings.DialogCreatable; +import com.android.settings.R; + +import java.util.ArrayList; + +public class SyncSettings extends AccountPreferenceBase + implements OnAccountsUpdateListener, DialogCreatable { + + private static final String KEY_SYNC_SWITCH = "sync_switch"; + + private String[] mAuthorities; + + private SettingsDialogFragment mDialogFragment; + private CheckBoxPreference mAutoSyncPreference; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + + addPreferencesFromResource(R.xml.sync_settings); + mAutoSyncPreference = + (CheckBoxPreference) getPreferenceScreen().findPreference(KEY_SYNC_SWITCH); + mAutoSyncPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + ContentResolver.setMasterSyncAutomatically((Boolean) newValue); + return true; + } + }); + + setHasOptionsMenu(true); + } + + @Override + public void onStart() { + super.onStart(); + Activity activity = getActivity(); + AccountManager.get(activity).addOnAccountsUpdatedListener(this, null, true); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + final Activity activity = getActivity(); + mAutoSyncPreference.setChecked(ContentResolver.getMasterSyncAutomatically()); + mAuthorities = activity.getIntent().getStringArrayExtra(AUTHORITIES_FILTER_KEY); + + updateAuthDescriptions(); + } + + @Override + public void onStop() { + super.onStop(); + final Activity activity = getActivity(); + AccountManager.get(activity).removeOnAccountsUpdatedListener(this); + } + + @Override + public boolean onPreferenceTreeClick(PreferenceScreen preferences, Preference preference) { + if (preference instanceof AccountPreference) { + startAccountSettings((AccountPreference) preference); + } else { + return false; + } + return true; + } + + private void startAccountSettings(AccountPreference acctPref) { + Intent intent = new Intent("android.settings.ACCOUNT_SYNC_SETTINGS"); + intent.putExtra(AccountSyncSettings.ACCOUNT_KEY, acctPref.getAccount()); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); + finish(); + } + + @Override + public void showDialog(int dialogId) { + if (mDialogFragment != null) { + Log.e(TAG, "Old dialog fragment not null!"); + } + mDialogFragment = new SettingsDialogFragment(this, dialogId); + mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId)); + } + + private void removeAccountPreferences() { + PreferenceScreen parent = getPreferenceScreen(); + for (int i = 0; i < parent.getPreferenceCount(); ) { + if (parent.getPreference(i) instanceof AccountPreference) { + parent.removePreference(parent.getPreference(i)); + } else { + i++; + } + } + } + + @Override + public void onAccountsUpdated(Account[] accounts) { + if (getActivity() == null) return; + + removeAccountPreferences(); + for (int i = 0, n = accounts.length; i < n; i++) { + final Account account = accounts[i]; + final ArrayList<String> auths = getAuthoritiesForAccountType(account.type); + + boolean showAccount = true; + if (mAuthorities != null && auths != null) { + showAccount = false; + for (String requestedAuthority : mAuthorities) { + if (auths.contains(requestedAuthority)) { + showAccount = true; + break; + } + } + } + + if (showAccount) { + final Drawable icon = getDrawableForType(account.type); + final AccountPreference preference = + new AccountPreference(getActivity(), account, icon, auths, true); + getPreferenceScreen().addPreference(preference); + preference.setSummary(getLabelForType(account.type)); + } + } + onSyncStateUpdated(); + } + + @Override + protected void onAuthDescriptionsUpdated() { + // Update account icons for all account preference items + for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) { + Preference pref = getPreferenceScreen().getPreference(i); + if (pref instanceof AccountPreference) { + AccountPreference accPref = (AccountPreference) + getPreferenceScreen().getPreference(i); + accPref.setIcon(getDrawableForType(accPref.getAccount().type)); + accPref.setSummary(getLabelForType(accPref.getAccount().type)); + } + } + } +} diff --git a/src/com/android/settings/accounts/SyncSettingsActivity.java b/src/com/android/settings/accounts/SyncSettingsActivity.java new file mode 100644 index 0000000..ed0089e --- /dev/null +++ b/src/com/android/settings/accounts/SyncSettingsActivity.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.accounts; + +import android.content.Intent; +import android.preference.PreferenceActivity; + +/** + * Launcher activity for the SyncSettings fragment. + * + */ +public class SyncSettingsActivity extends PreferenceActivity { + @Override + public Intent getIntent() { + Intent modIntent = new Intent(super.getIntent()); + modIntent.putExtra(EXTRA_SHOW_FRAGMENT, SyncSettings.class.getName()); + modIntent.putExtra(EXTRA_NO_HEADERS, true); + return modIntent; + } +}
\ No newline at end of file |