diff --git a/res/layout/account_type_preference.xml b/res/layout/account_type_preference.xml new file mode 100644 index 00000000000..f7ba859fe33 --- /dev/null +++ b/res/layout/account_type_preference.xml @@ -0,0 +1,22 @@ + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index fe487bb152a..26950439f8f 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -718,6 +718,8 @@ Profile info Location + + Accounts Security @@ -1804,6 +1806,10 @@ Accounts + + Personal + + Work Search diff --git a/res/xml/account_settings.xml b/res/xml/account_settings.xml new file mode 100644 index 00000000000..236a26d8642 --- /dev/null +++ b/res/xml/account_settings.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/res/xml/dashboard_categories.xml b/res/xml/dashboard_categories.xml index 44ec9606bc1..ab669d1b6e0 100644 --- a/res/xml/dashboard_categories.xml +++ b/res/xml/dashboard_categories.xml @@ -162,6 +162,15 @@ android:icon="@drawable/ic_settings_security" /> + + + + getAccountTypePreferences() { + String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes(); + List accountTypePreferences = + new ArrayList(accountTypes.length); + for (String accountType : accountTypes) { + CharSequence label = mAuthenticatorHelper.getLabelForType(getActivity(), accountType); + if (label == null) { + continue; + } + + Account[] accounts = AccountManager.get(getActivity()).getAccountsByType(accountType); + boolean skipToAccount = accounts.length == 1 + && !mAuthenticatorHelper.hasAccountPreferences(accountType); + + if (skipToAccount) { + Bundle fragmentArguments = new Bundle(); + fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY, + accounts[0]); + + accountTypePreferences.add(new AccountPreference( + getActivity(), + label, + accountType, + AccountSyncSettings.class.getName(), + fragmentArguments)); + } else { + Bundle fragmentArguments = new Bundle(); + fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType); + fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL, + label.toString()); + + accountTypePreferences.add(new AccountPreference( + getActivity(), + label, + accountType, + ManageAccountsSettings.class.getName(), + fragmentArguments)); + } + mAuthenticatorHelper.preloadDrawableForType(getActivity(), accountType); + } + // Sort by label + Collections.sort(accountTypePreferences, new Comparator() { + @Override + public int compare(AccountPreference t1, AccountPreference t2) { + return t1.mTitle.toString().compareTo(t2.mTitle.toString()); + } + }); + return accountTypePreferences; + } + + private void listenToAccountUpdates() { + if (!mListeningToAccountUpdates) { + AccountManager.get(getActivity()).addOnAccountsUpdatedListener(this, null, true); + mListeningToAccountUpdates = true; + } + } + + private void stopListeningToAccountUpdates() { + if (mListeningToAccountUpdates) { + AccountManager.get(getActivity()).removeOnAccountsUpdatedListener(this); + mListeningToAccountUpdates = false; + } + } + + private class AccountPreference extends Preference implements OnPreferenceClickListener { + /** + * Title of the tile that is shown to the user. + * @attr ref android.R.styleable#PreferenceHeader_title + */ + private final CharSequence mTitle; + + /** + * Full class name of the fragment to display when this tile is + * selected. + * @attr ref android.R.styleable#PreferenceHeader_fragment + */ + private final String mFragment; + + /** + * Optional arguments to supply to the fragment when it is + * instantiated. + */ + private final Bundle mFragmentArguments; + + + public AccountPreference(Context context, CharSequence title, + String accountType, String fragment, Bundle fragmentArguments) { + super(context); + mTitle = title; + mFragment = fragment; + mFragmentArguments = fragmentArguments; + setWidgetLayoutResource(R.layout.account_type_preference); + + Drawable drawable = mAuthenticatorHelper.getDrawableForType(context, accountType); + setTitle(title); + setIcon(drawable); + + setOnPreferenceClickListener(this); + } + + @Override + public boolean onPreferenceClick(Preference preference) { + if (mFragment != null) { + Utils.startWithFragment( + getContext(), mFragment, mFragmentArguments, null, 0, mTitle); + return true; + } + return false; + } + } + // TODO Implement a {@link SearchIndexProvider} to allow Indexing and Search of account types + // See http://b/15403806 +}