Merge "Fix problem where rotating the device doesn't launch TrustAgent pref" into lmp-dev
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 1790226..fe704a4 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -138,18 +138,6 @@
                 android:value="true" />
         </activity>
 
-        <activity android:name="AirplaneModeVoiceActivity"
-                android:label="@string/wireless_networks_settings_title"
-                android:theme="@android:style/Theme.Material.Light.Voice"
-                android:exported="true"
-                android:taskAffinity="">
-            <intent-filter>
-                <action android:name="android.settings.VOICE_CONTROL_AIRPLANE_MODE" />
-                <category android:name="android.intent.category.DEFAULT" />
-                <category android:name="android.intent.category.VOICE" />
-            </intent-filter>
-        </activity>
-
         <!-- Top-level settings -->
 
         <activity android:name="Settings$WifiSettingsActivity"
diff --git a/res/layout/bluetooth_device_settings.xml b/res/layout/bluetooth_device_settings.xml
index cc2cc09..b87551a 100644
--- a/res/layout/bluetooth_device_settings.xml
+++ b/res/layout/bluetooth_device_settings.xml
@@ -31,8 +31,9 @@
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="@string/bluetooth_preference_paired_dialog_name_label"
+                    android:textAppearance="@android:style/TextAppearance.Material.Body1"
+                    android:textColor="?android:attr/textColorSecondary"
                     android:textDirection="locale"
-                    android:textColor="?android:attr/colorAccent"
                     style="@style/bt_item_label" />
 
             <EditText android:id="@+id/name"
diff --git a/res/layout/bluetooth_preference_category.xml b/res/layout/bluetooth_preference_category.xml
new file mode 100644
index 0000000..59a3e87
--- /dev/null
+++ b/res/layout/bluetooth_preference_category.xml
@@ -0,0 +1,25 @@
+<?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.
+-->
+
+<!-- Layout used for Bluetooth PreferenceCategory in a DeviceProfilesSettings. -->
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+android:id/title"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:textAppearance="@android:style/TextAppearance.Material.Body1"
+    android:textColor="?android:attr/textColorSecondary"
+    android:paddingTop="16dip"
+    android:paddingBottom="16dip" />
diff --git a/src/com/android/settings/DeviceAdminSettings.java b/src/com/android/settings/DeviceAdminSettings.java
index a70984d..6ac0974 100644
--- a/src/com/android/settings/DeviceAdminSettings.java
+++ b/src/com/android/settings/DeviceAdminSettings.java
@@ -304,8 +304,8 @@
             final Activity activity = getActivity();
             ViewHolder vh = (ViewHolder) view.getTag();
             Drawable activityIcon = item.loadIcon(activity.getPackageManager());
-            Drawable badgedIcon = activity.getPackageManager().getUserBadgedDrawableForDensity(
-                    activityIcon, new UserHandle(getUserId(item)), null, 0);
+            Drawable badgedIcon = activity.getPackageManager().getUserBadgedIcon(
+                    activityIcon, new UserHandle(getUserId(item)));
             vh.icon.setImageDrawable(badgedIcon);
             vh.name.setText(item.loadLabel(activity.getPackageManager()));
             vh.checkbox.setChecked(isActiveAdmin(item));
diff --git a/src/com/android/settings/RestrictedSettingsFragment.java b/src/com/android/settings/RestrictedSettingsFragment.java
index 142a42c..7d7599f 100644
--- a/src/com/android/settings/RestrictedSettingsFragment.java
+++ b/src/com/android/settings/RestrictedSettingsFragment.java
@@ -141,7 +141,7 @@
     private void ensurePin() {
         if (!mChallengeSucceeded && !mChallengeRequested
                 && mRestrictionsManager.hasRestrictionsProvider()) {
-            Intent intent = mRestrictionsManager.getLocalApprovalIntent();
+            Intent intent = mRestrictionsManager.createLocalApprovalIntent();
             if (intent != null) {
                 mChallengeRequested = true;
                 mChallengeSucceeded = false;
diff --git a/src/com/android/settings/accounts/AccountPreferenceBase.java b/src/com/android/settings/accounts/AccountPreferenceBase.java
index 28da591..bb60871 100644
--- a/src/com/android/settings/accounts/AccountPreferenceBase.java
+++ b/src/com/android/settings/accounts/AccountPreferenceBase.java
@@ -17,15 +17,10 @@
 
 package com.android.settings.accounts;
 
-import com.google.android.collect.Maps;
-
 import android.accounts.AuthenticatorDescription;
 import android.app.Activity;
-import android.app.ActivityManagerNative;
-import android.app.IActivityManager;
 import android.content.ContentResolver;
 import android.content.Context;
-import android.content.SyncAdapterType;
 import android.content.SyncStatusObserver;
 import android.content.pm.PackageManager;
 import android.content.res.Resources;
@@ -45,7 +40,6 @@
 
 import java.util.ArrayList;
 import java.util.Date;
-import java.util.HashMap;
 
 class AccountPreferenceBase extends SettingsPreferenceFragment
         implements AuthenticatorHelper.OnAccountsUpdateListener {
@@ -59,7 +53,6 @@
 
     private UserManager mUm;
     private Object mStatusChangeListenerHandle;
-    private HashMap<String, ArrayList<String>> mAccountTypeToAuthorities = null;
     protected AuthenticatorHelper mAuthenticatorHelper;
     protected UserHandle mUserHandle;
 
@@ -136,25 +129,7 @@
     };
 
     public ArrayList<String> getAuthoritiesForAccountType(String type) {
-        if (mAccountTypeToAuthorities == null) {
-            mAccountTypeToAuthorities = Maps.newHashMap();
-            SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(
-                    mUserHandle.getIdentifier());
-            for (int i = 0, n = syncAdapters.length; i < n; i++) {
-                final SyncAdapterType sa = syncAdapters[i];
-                ArrayList<String> authorities = mAccountTypeToAuthorities.get(sa.accountType);
-                if (authorities == null) {
-                    authorities = new ArrayList<String>();
-                    mAccountTypeToAuthorities.put(sa.accountType, authorities);
-                }
-                if (Log.isLoggable(TAG, Log.VERBOSE)) {
-                    Log.d(TAG, "added authority " + sa.authority + " to accountType "
-                            + sa.accountType);
-                }
-                authorities.add(sa.authority);
-            }
-        }
-        return mAccountTypeToAuthorities.get(type);
+        return mAuthenticatorHelper.getAuthoritiesForAccountType(type);
     }
 
     /**
diff --git a/src/com/android/settings/accounts/AccountSettings.java b/src/com/android/settings/accounts/AccountSettings.java
index 9bcef13..f057cbf 100644
--- a/src/com/android/settings/accounts/AccountSettings.java
+++ b/src/com/android/settings/accounts/AccountSettings.java
@@ -57,6 +57,7 @@
 
 import static android.content.Intent.EXTRA_USER;
 import static android.os.UserManager.DISALLOW_MODIFY_ACCOUNTS;
+import static android.provider.Settings.EXTRA_AUTHORITIES;
 
 /**
  * Settings screen for the account types on the device.
@@ -83,6 +84,8 @@
     private ManagedProfileBroadcastReceiver mManagedProfileBroadcastReceiver
                 = new ManagedProfileBroadcastReceiver();
     private Preference mProfileNotAvailablePreference;
+    private String[] mAuthorities;
+    private int mAuthoritiesCount = 0;
 
     /**
      * Holds data related to the accounts belonging to one profile.
@@ -114,7 +117,10 @@
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         mUm = (UserManager) getSystemService(Context.USER_SERVICE);
-        mProfileNotAvailablePreference = new Preference(getActivity());
+        mAuthorities = getActivity().getIntent().getStringArrayExtra(EXTRA_AUTHORITIES);
+        if (mAuthorities != null) {
+            mAuthoritiesCount = mAuthorities.length;
+        }
         setHasOptionsMenu(true);
     }
 
@@ -190,6 +196,7 @@
             if (preference == profileData.addAccountPreference) {
                 Intent intent = new Intent(ADD_ACCOUNT_ACTION);
                 intent.putExtra(EXTRA_USER, profileData.userInfo.getUserHandle());
+                intent.putExtra(EXTRA_AUTHORITIES, mAuthorities);
                 startActivity(intent);
                 return true;
             }
@@ -353,6 +360,21 @@
 
         for (int i = 0; i < accountTypes.length; i++) {
             final String accountType = accountTypes[i];
+            if (mAuthoritiesCount > 0) {
+                // Skip showing any account that does not have any of the requested authorities
+                final ArrayList<String> authoritiesForType = helper.getAuthoritiesForAccountType(
+                        accountType);
+                boolean show = false;
+                for (int j = 0; j < mAuthoritiesCount; j++) {
+                    if (authoritiesForType.contains(mAuthorities[j])) {
+                        show = true;
+                        break;
+                    }
+                }
+                if (!show) {
+                    continue;
+                }
+            }
             final CharSequence label = helper.getLabelForType(getActivity(), accountType);
             if (label == null) {
                 continue;
diff --git a/src/com/android/settings/accounts/AuthenticatorHelper.java b/src/com/android/settings/accounts/AuthenticatorHelper.java
index c9c6ac4..6cebf5f 100644
--- a/src/com/android/settings/accounts/AuthenticatorHelper.java
+++ b/src/com/android/settings/accounts/AuthenticatorHelper.java
@@ -16,13 +16,17 @@
 
 package com.android.settings.accounts;
 
+import com.google.android.collect.Maps;
+
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.accounts.AuthenticatorDescription;
 import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.SyncAdapterType;
 import android.content.pm.PackageManager;
 import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
@@ -52,6 +56,7 @@
     private AuthenticatorDescription[] mAuthDescs;
     private ArrayList<String> mEnabledAccountTypes = new ArrayList<String>();
     private Map<String, Drawable> mAccTypeIconCache = new HashMap<String, Drawable>();
+    private HashMap<String, ArrayList<String>> mAccountTypeToAuthorities = Maps.newHashMap();
 
     private final UserHandle mUserHandle;
     private final UserManager mUm;
@@ -69,7 +74,8 @@
         mUm = userManager;
         mUserHandle = userHandle;
         mListener = listener;
-        // This guarantees that the helper is ready to use once constructed
+        // This guarantees that the helper is ready to use once constructed: the account types and
+        // authorities are initialized
         onAccountsUpdated(null);
     }
 
@@ -104,8 +110,8 @@
                 AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType);
                 Context authContext = context.createPackageContextAsUser(desc.packageName, 0,
                         mUserHandle);
-                icon = mContext.getPackageManager().getUserBadgedDrawableForDensity(
-                        authContext.getResources().getDrawable(desc.iconId), mUserHandle, null, 0);
+                icon = mContext.getPackageManager().getUserBadgedIcon(
+                        authContext.getResources().getDrawable(desc.iconId), mUserHandle);
                 synchronized (mAccTypeIconCache) {
                     mAccTypeIconCache.put(accountType, icon);
                 }
@@ -187,6 +193,7 @@
         if (mListeningToAccountUpdates) {
             mListener.onAccountsUpdate(mUserHandle);
         }
+        buildAccountTypeToAuthoritiesMap();
     }
 
     @Override
@@ -215,4 +222,27 @@
             mListeningToAccountUpdates = false;
         }
     }
+
+    public ArrayList<String> getAuthoritiesForAccountType(String type) {
+        return mAccountTypeToAuthorities.get(type);
+    }
+
+    private void buildAccountTypeToAuthoritiesMap() {
+        mAccountTypeToAuthorities.clear();
+        SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(
+                mUserHandle.getIdentifier());
+        for (int i = 0, n = syncAdapters.length; i < n; i++) {
+            final SyncAdapterType sa = syncAdapters[i];
+            ArrayList<String> authorities = mAccountTypeToAuthorities.get(sa.accountType);
+            if (authorities == null) {
+                authorities = new ArrayList<String>();
+                mAccountTypeToAuthorities.put(sa.accountType, authorities);
+            }
+            if (Log.isLoggable(TAG, Log.VERBOSE)) {
+                Log.d(TAG, "Added authority " + sa.authority + " to accountType "
+                        + sa.accountType);
+            }
+            authorities.add(sa.authority);
+        }
+    }
 }
diff --git a/src/com/android/settings/accounts/ChooseAccountActivity.java b/src/com/android/settings/accounts/ChooseAccountActivity.java
index 27edca1..e52d640 100644
--- a/src/com/android/settings/accounts/ChooseAccountActivity.java
+++ b/src/com/android/settings/accounts/ChooseAccountActivity.java
@@ -215,8 +215,8 @@
             try {
                 AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType);
                 Context authContext = createPackageContextAsUser(desc.packageName, 0, mUserHandle);
-                icon = getPackageManager().getUserBadgedDrawableForDensity(
-                        authContext.getResources().getDrawable(desc.iconId), mUserHandle, null, 0);
+                icon = getPackageManager().getUserBadgedIcon(
+                        authContext.getResources().getDrawable(desc.iconId), mUserHandle);
             } catch (PackageManager.NameNotFoundException e) {
                 // TODO: place holder icon for missing account icons?
                 Log.w(TAG, "No icon name for account type " + accountType);
diff --git a/src/com/android/settings/applications/ManageApplications.java b/src/com/android/settings/applications/ManageApplications.java
index ac5a78a..e64e56e 100644
--- a/src/com/android/settings/applications/ManageApplications.java
+++ b/src/com/android/settings/applications/ManageApplications.java
@@ -1041,10 +1041,10 @@
         if (selectedUser.getIdentifier() != UserHandle.myUserId()) {
             Intent intent = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
             int currentTab = mViewPager.getCurrentItem();
             intent.putExtra(EXTRA_LIST_TYPE, mTabs.get(currentTab).mListType);
             mContext.startActivityAsUser(intent, selectedUser);
-            getActivity().finish();
         }
     }
 
diff --git a/src/com/android/settings/bluetooth/DeviceProfilesSettings.java b/src/com/android/settings/bluetooth/DeviceProfilesSettings.java
index 64c807f..110432b 100755
--- a/src/com/android/settings/bluetooth/DeviceProfilesSettings.java
+++ b/src/com/android/settings/bluetooth/DeviceProfilesSettings.java
@@ -78,6 +78,7 @@
         addPreferencesFromResource(R.xml.bluetooth_device_advanced);
         getPreferenceScreen().setOrderingAsAdded(false);
         mProfileContainer = (PreferenceGroup) findPreference(KEY_PROFILE_CONTAINER);
+        mProfileContainer.setLayoutResource(R.layout.bluetooth_preference_category);
 
         mManager = LocalBluetoothManager.getInstance(getActivity());
         CachedBluetoothDeviceManager deviceManager =
diff --git a/src/com/android/settings/location/RecentLocationApps.java b/src/com/android/settings/location/RecentLocationApps.java
index 6a40d66..7e99725 100644
--- a/src/com/android/settings/location/RecentLocationApps.java
+++ b/src/com/android/settings/location/RecentLocationApps.java
@@ -212,10 +212,9 @@
 
             final UserHandle userHandle = new UserHandle(userId);
             Drawable appIcon = mPackageManager.getApplicationIcon(appInfo);
-            Drawable icon = mPackageManager.getUserBadgedDrawableForDensity(appIcon, userHandle,
-                    null, 0);
+            Drawable icon = mPackageManager.getUserBadgedIcon(appIcon, userHandle);
             CharSequence appLabel = mPackageManager.getApplicationLabel(appInfo);
-            CharSequence badgedAppLabel = um.getBadgedLabelForUser(appLabel.toString(), userHandle);
+            CharSequence badgedAppLabel = mPackageManager.getUserBadgedLabel(appLabel, userHandle);
             preference = createRecentLocationEntry(icon,
                     appLabel, highBattery, badgedAppLabel,
                     new PackageEntryClickedListener(packageName));
diff --git a/src/com/android/settings/notification/NotificationAppList.java b/src/com/android/settings/notification/NotificationAppList.java
index 1f63084..e8b68f6 100644
--- a/src/com/android/settings/notification/NotificationAppList.java
+++ b/src/com/android/settings/notification/NotificationAppList.java
@@ -156,8 +156,8 @@
         if (selectedUser.getIdentifier() != UserHandle.myUserId()) {
             Intent intent = new Intent(getActivity(), NotificationAppListActivity.class);
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
             mContext.startActivityAsUser(intent, selectedUser);
-            getActivity().finish();
         }
     }
 
diff --git a/src/com/android/settings/print/PrintSettingsFragment.java b/src/com/android/settings/print/PrintSettingsFragment.java
index 2acc733..3e2cd8b 100644
--- a/src/com/android/settings/print/PrintSettingsFragment.java
+++ b/src/com/android/settings/print/PrintSettingsFragment.java
@@ -298,8 +298,8 @@
         if (selectedUser.getIdentifier() != UserHandle.myUserId()) {
             Intent intent = new Intent(Settings.ACTION_PRINT_SETTINGS);
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
             getActivity().startActivityAsUser(intent, selectedUser);
-            getActivity().finish();
         }
     }
 
diff --git a/src/com/android/settings/wifi/WifiEnabler.java b/src/com/android/settings/wifi/WifiEnabler.java
index dc3974e..ffc10a0 100644
--- a/src/com/android/settings/wifi/WifiEnabler.java
+++ b/src/com/android/settings/wifi/WifiEnabler.java
@@ -27,8 +27,6 @@
 import android.os.Handler;
 import android.os.Message;
 import android.provider.Settings;
-import android.view.View;
-import android.widget.CompoundButton;
 import android.widget.Switch;
 import android.widget.Toast;
 
@@ -41,7 +39,6 @@
 
 public class WifiEnabler implements SwitchBar.OnSwitchChangeListener  {
     private Context mContext;
-    private Switch mSwitch;
     private SwitchBar mSwitchBar;
     private AtomicBoolean mConnected = new AtomicBoolean(false);
 
@@ -88,22 +85,20 @@
     public WifiEnabler(Context context, SwitchBar switchBar) {
         mContext = context;
         mSwitchBar = switchBar;
-        mSwitch = switchBar.getSwitch();
-        // This is a trick: as the Wi-Fi initial state is asynchronously coming from the
-        // BroadcastReceiver we cannot have the Switch visible at first otherwise you will notice
-        // its state change later on. So start it as VIEW.GONE and make it View.VISIBLE later
-        // when its state is defined.
-        mSwitch.setVisibility(View.GONE);
 
         mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
+
         mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
         // The order matters! We really should not depend on this. :(
         mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
         mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
+
         setupSwitchBar();
     }
 
     public void setupSwitchBar() {
+        final int state = mWifiManager.getWifiState();
+        handleWifiStateChanged(state);
         mSwitchBar.addOnSwitchChangeListener(this);
         mSwitchBar.show();
     }
@@ -126,27 +121,26 @@
     private void handleWifiStateChanged(int state) {
         switch (state) {
             case WifiManager.WIFI_STATE_ENABLING:
-                mSwitch.setEnabled(false);
+                mSwitchBar.setEnabled(false);
                 break;
             case WifiManager.WIFI_STATE_ENABLED:
-                setSwitchChecked(true);
-                mSwitch.setEnabled(true);
+                setSwitchBarChecked(true);
+                mSwitchBar.setEnabled(true);
                 updateSearchIndex(true);
                 break;
             case WifiManager.WIFI_STATE_DISABLING:
-                mSwitch.setEnabled(false);
+                mSwitchBar.setEnabled(false);
                 break;
             case WifiManager.WIFI_STATE_DISABLED:
-                setSwitchChecked(false);
-                mSwitch.setEnabled(true);
+                setSwitchBarChecked(false);
+                mSwitchBar.setEnabled(true);
                 updateSearchIndex(false);
                 break;
             default:
-                setSwitchChecked(false);
-                mSwitch.setEnabled(true);
+                setSwitchBarChecked(false);
+                mSwitchBar.setEnabled(true);
                 updateSearchIndex(false);
         }
-        mSwitch.setVisibility(View.VISIBLE);
     }
 
     private void updateSearchIndex(boolean isWiFiOn) {
@@ -158,12 +152,10 @@
         mHandler.sendMessage(msg);
     }
 
-    private void setSwitchChecked(boolean checked) {
-        if (checked != mSwitch.isChecked()) {
-            mStateMachineEvent = true;
-            mSwitch.setChecked(checked);
-            mStateMachineEvent = false;
-        }
+    private void setSwitchBarChecked(boolean checked) {
+        mStateMachineEvent = true;
+        mSwitchBar.setChecked(checked);
+        mStateMachineEvent = false;
     }
 
     private void handleStateChanged(@SuppressWarnings("unused") NetworkInfo.DetailedState state) {
@@ -192,7 +184,7 @@
         if (isChecked && !WirelessSettings.isRadioAllowed(mContext, Settings.Global.RADIO_WIFI)) {
             Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
             // Reset switch to off. No infinite check/listenenr loop.
-            switchView.setChecked(false);
+            mSwitchBar.setChecked(false);
             return;
         }
 
@@ -203,10 +195,9 @@
             mWifiManager.setWifiApEnabled(null, false);
         }
 
-        mSwitch.setEnabled(false);
         if (!mWifiManager.setWifiEnabled(isChecked)) {
             // Error
-            mSwitch.setEnabled(true);
+            mSwitchBar.setEnabled(true);
             Toast.makeText(mContext, R.string.wifi_error, Toast.LENGTH_SHORT).show();
         }
     }