Move personal account settings to a new screen.

This does not include work account settings.

Bug: 13656853
Change-Id: I105044f8261b1c656ed5053b3c02c02cfaae90d1
This commit is contained in:
Alexandra Gherghina
2014-05-27 11:18:34 +01:00
parent f5ca71751e
commit ba63597d5a
5 changed files with 337 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 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.
-->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/accountTypeIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@null" />

View File

@@ -718,6 +718,8 @@
<string name="profile_info_settings_title">Profile info</string> <string name="profile_info_settings_title">Profile info</string>
<!-- Main Settings screen setting option title for the item to take you to the location screen --> <!-- Main Settings screen setting option title for the item to take you to the location screen -->
<string name="location_settings_title">Location</string> <string name="location_settings_title">Location</string>
<!-- Main Settings screen setting option title for the item to take you to the accounts screen [CHAR LIMIT=22] -->
<string name="account_settings_title">Accounts</string>
<!-- Main Settings screen setting option title for the item to take you to the security screen --> <!-- Main Settings screen setting option title for the item to take you to the security screen -->
<string name="security_settings_title">Security</string> <string name="security_settings_title">Security</string>
<!-- Main Settings screen setting option summary text for the item tot ake you to the security and location screen --> <!-- Main Settings screen setting option summary text for the item tot ake you to the security and location screen -->
@@ -1804,6 +1806,10 @@
<!-- Account settings header. [CHAR LIMIT=30] --> <!-- Account settings header. [CHAR LIMIT=30] -->
<string name="account_settings">Accounts</string> <string name="account_settings">Accounts</string>
<!-- Header for items under the personal user [CHAR LIMIT=30] -->
<string name="category_personal">Personal</string>
<!-- Header for items under the work user [CHAR LIMIT=30] -->
<string name="category_work">Work</string>
<!-- Main Settings screen, setting option name to go into search settings --> <!-- Main Settings screen, setting option name to go into search settings -->
<string name="search_settings">Search</string> <string name="search_settings">Search</string>

View File

@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 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.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="account"
android:title="@string/account_settings_title">
<Preference
android:key="add_account"
android:title="@string/add_account_label"
android:icon="@drawable/ic_menu_add_dark"
android:order="2147483648" />
<PreferenceCategory
android:key="account_personal"
android:title="@string/category_personal" >
<Preference
android:key="add_account_personal"
android:title="@string/add_account_label"
android:icon="@drawable/ic_menu_add_dark"
android:order="2147483648" />
</PreferenceCategory>
<PreferenceCategory
android:key="account_work"
android:title="@string/category_work" >
<Preference
android:key="add_account_work"
android:title="@string/add_account_label"
android:icon="@drawable/ic_menu_add_dark"
android:order="2147483648" />
</PreferenceCategory>
</PreferenceScreen>

View File

@@ -162,6 +162,15 @@
android:icon="@drawable/ic_settings_security" android:icon="@drawable/ic_settings_security"
/> />
<!-- Account -->
<!-- TODO: add accounts icon -->
<dashboard-tile
android:id="@+id/account_settings"
android:title="@string/account_settings_title"
android:fragment="com.android.settings.accounts.AccountSettings"
android:icon="@null"
/>
<!-- Language --> <!-- Language -->
<dashboard-tile <dashboard-tile
android:id="@+id/language_settings" android:id="@+id/language_settings"

View File

@@ -0,0 +1,249 @@
/*
* Copyright (C) 2014 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.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.UserManager;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceGroup;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Settings screen for the account types on the device.
* This shows all account types available for personal and work profiles.
*/
public class AccountSettings extends SettingsPreferenceFragment
implements OnAccountsUpdateListener, OnPreferenceClickListener {
public static final String TAG = "AccountSettings";
private static final String KEY_ACCOUNT = "account";
private static final String KEY_ADD_ACCOUNT = "add_account";
private static final String KEY_CATEGORY_PERSONAL = "account_personal";
private static final String KEY_ADD_ACCOUNT_PERSONAL = "add_account_personal";
private static final String KEY_CATEGORY_WORK = "account_work";
private AuthenticatorHelper mAuthenticatorHelper;
private boolean mListeningToAccountUpdates;
private PreferenceGroup mAccountTypesForUser;
private Preference mAddAccountForUser;
private UserManager mUm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUm = (UserManager) getSystemService(Context.USER_SERVICE);
mAuthenticatorHelper = new AuthenticatorHelper();
mAuthenticatorHelper.updateAuthDescriptions(getActivity());
mAuthenticatorHelper.onAccountsUpdated(getActivity(), null);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.account_settings);
if(mUm.isLinkedUser()) {
// Restricted user or similar
// TODO: Do we disallow modifying accounts for restricted profiles?
mAccountTypesForUser = (PreferenceGroup) findPreference(KEY_ACCOUNT);
if (mUm.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
removePreference(KEY_ADD_ACCOUNT);
} else {
mAddAccountForUser = findPreference(KEY_ADD_ACCOUNT);
mAddAccountForUser.setOnPreferenceClickListener(this);
}
removePreference(KEY_CATEGORY_PERSONAL);
removePreference(KEY_CATEGORY_WORK);
} else {
mAccountTypesForUser = (PreferenceGroup) findPreference(KEY_CATEGORY_PERSONAL);
mAddAccountForUser = findPreference(KEY_ADD_ACCOUNT_PERSONAL);
mAddAccountForUser.setOnPreferenceClickListener(this);
// TODO: Show the work accounts also
// TODO: Handle the case where there is only one account
removePreference(KEY_CATEGORY_WORK);
removePreference(KEY_ADD_ACCOUNT);
}
updateAccountTypes(mAccountTypesForUser);
}
@Override
public void onDestroy() {
super.onDestroy();
stopListeningToAccountUpdates();
}
@Override
public void onAccountsUpdated(Account[] accounts) {
// TODO: watch for package upgrades to invalidate cache; see 7206643
mAuthenticatorHelper.updateAuthDescriptions(getActivity());
mAuthenticatorHelper.onAccountsUpdated(getActivity(), accounts);
listenToAccountUpdates();
updateAccountTypes(mAccountTypesForUser);
}
@Override
public boolean onPreferenceClick(Preference preference) {
// Check the preference
if (preference == mAddAccountForUser) {
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
startActivity(intent);
return true;
}
return false;
}
private void updateAccountTypes(PreferenceGroup preferenceGroup) {
preferenceGroup.removeAll();
preferenceGroup.setOrderingAsAdded(true);
for (AccountPreference preference : getAccountTypePreferences()) {
preferenceGroup.addPreference(preference);
}
if (mAddAccountForUser != null) {
preferenceGroup.addPreference(mAddAccountForUser);
}
}
private List<AccountPreference> getAccountTypePreferences() {
String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
List<AccountPreference> accountTypePreferences =
new ArrayList<AccountPreference>(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<AccountPreference>() {
@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
}