Merge "Fixed scrolling on the Wi-Fi Settings Dialog." into lmp-dev
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index fe704a4..11665b0 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -86,15 +86,26 @@
                 android:label="@string/settings_label_launcher"
                 android:launchMode="singleTask">
             <intent-filter>
-                <action android:name="android.intent.action.MAIN" />
                 <action android:name="android.settings.SETTINGS" />
                 <category android:name="android.intent.category.DEFAULT" />
-                <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
             <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
                 android:value="true" />
         </activity>
 
+        <!-- Alias for launcher activity only, as this belongs to each profile. -->
+        <activity-alias android:name="Settings"
+                android:taskAffinity="com.android.settings"
+                android:label="@string/settings_label_launcher"
+                android:launchMode="singleTask"
+                android:targetActivity="Settings">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity-alias>
+
         <receiver android:name="ManagedProfileSetup">
             <intent-filter>
                 <action android:name="android.intent.action.USER_INITIALIZE"/>
diff --git a/res/layout/wifi_dialog.xml b/res/layout/wifi_dialog.xml
index 9dc71a4..b7b20c7 100644
--- a/res/layout/wifi_dialog.xml
+++ b/res/layout/wifi_dialog.xml
@@ -50,7 +50,7 @@
              <LinearLayout
                        style="@style/wifi_item" >
                  <TextView
-                         style="@style/wifi_item_content"
+                         style="@style/wifi_item_label"
                          android:text="@string/wifi_security" />
 
                  <Spinner android:id="@+id/security"
@@ -149,8 +149,6 @@
                        style="@style/wifi_item" >
                 <TextView
                         style="@style/wifi_item_label"
-                        android:textAppearance="@android:style/TextAppearance.Material.Body2"
-                        android:textColor="?android:attr/colorAccent"
                         android:text="@string/wifi_password" />
 
                 <EditText android:id="@+id/password"
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index 03b2e90..e586e29 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -293,6 +293,7 @@
         <item>@string/wifi_security_psk_generic</item>
         <!-- Do not translate. -->
         <item>@string/wifi_security_eap</item>
+
     </string-array>
 
     <!-- Match this with the constants in AccessPoint. --> <skip />
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 465bd45..20ff544 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -234,6 +234,8 @@
         <item name="android:layout_height">wrap_content</item>
         <item name="android:textSize">14sp</item>
         <item name="android:textAlignment">viewStart</item>
+        <item name="android:textAppearance">@android:style/TextAppearance.Material.Body1</item>
+        <item name="android:textColor">@*android:color/secondary_text_default_material_light</item>
     </style>
 
     <style name="wifi_item_content">
@@ -241,6 +243,8 @@
         <item name="android:layout_height">wrap_content</item>
         <item name="android:textSize">18sp</item>
         <item name="android:textAlignment">viewStart</item>
+        <item name="android:textAppearance">@android:style/TextAppearance.Material.Subhead</item>
+        <item name="android:textColor">@*android:color/primary_text_default_material_light</item>
     </style>
 
     <style name="wifi_item_edit_content">
diff --git a/src/com/android/settings/MasterClearConfirm.java b/src/com/android/settings/MasterClearConfirm.java
index 0455d74..40c2324 100644
--- a/src/com/android/settings/MasterClearConfirm.java
+++ b/src/com/android/settings/MasterClearConfirm.java
@@ -62,31 +62,29 @@
             final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
                     getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
 
-            if (pdbManager != null) {
+            if (pdbManager != null && !pdbManager.getOemUnlockEnabled()) {
                 // if OEM unlock is enabled, this will be wiped during FR process.
-                if (!pdbManager.getOemUnlockEnabled()) {
-                    final ProgressDialog progressDialog = getProgressDialog();
-                    progressDialog.show();
+                final ProgressDialog progressDialog = getProgressDialog();
+                progressDialog.show();
 
-                    // need to prevent orientation changes as we're about to go into
-                    // a long IO request, so we won't be able to access inflate resources on flash
-                    final int oldOrientation = getActivity().getRequestedOrientation();
-                    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
-                    new AsyncTask<Void, Void, Void>() {
-                        @Override
-                        protected Void doInBackground(Void... params) {
-                            pdbManager.wipe();
-                            return null;
-                        }
+                // need to prevent orientation changes as we're about to go into
+                // a long IO request, so we won't be able to access inflate resources on flash
+                final int oldOrientation = getActivity().getRequestedOrientation();
+                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
+                new AsyncTask<Void, Void, Void>() {
+                    @Override
+                    protected Void doInBackground(Void... params) {
+                        pdbManager.wipe();
+                        return null;
+                    }
 
-                        @Override
-                        protected void onPostExecute(Void aVoid) {
-                            progressDialog.hide();
-                            getActivity().setRequestedOrientation(oldOrientation);
-                            doMasterClear();
-                        }
-                    }.execute();
-                }
+                    @Override
+                    protected void onPostExecute(Void aVoid) {
+                        progressDialog.hide();
+                        getActivity().setRequestedOrientation(oldOrientation);
+                        doMasterClear();
+                    }
+                }.execute();
             } else {
                 doMasterClear();
             }
diff --git a/src/com/android/settings/SettingsPreferenceFragment.java b/src/com/android/settings/SettingsPreferenceFragment.java
index 6cf3330..e87f676 100644
--- a/src/com/android/settings/SettingsPreferenceFragment.java
+++ b/src/com/android/settings/SettingsPreferenceFragment.java
@@ -230,12 +230,15 @@
                     listView.postDelayed(new Runnable() {
                         @Override
                         public void run() {
-                            final View v = listView.getChildAt(0);
-                            final int centerX = v.getWidth() / 2;
-                            final int centerY = v.getHeight() / 2;
-                            highlight.setHotspot(centerX, centerY);
-                            v.setPressed(true);
-                            v.setPressed(false);
+                            final int index = position - listView.getFirstVisiblePosition();
+                            if (index >= 0 && index < listView.getChildCount()) {
+                                final View v = listView.getChildAt(index);
+                                final int centerX = v.getWidth() / 2;
+                                final int centerY = v.getHeight() / 2;
+                                highlight.setHotspot(centerX, centerY);
+                                v.setPressed(true);
+                                v.setPressed(false);
+                            }
                         }
                     }, DELAY_HIGHLIGHT_DURATION_MILLIS);
                 }
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java
index 47ff6af..b238eb9 100644
--- a/src/com/android/settings/Utils.java
+++ b/src/com/android/settings/Utils.java
@@ -839,7 +839,9 @@
         final TelephonyManager tm =
                 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
 
-        return tm.getSimCount() > 0;
+        // TODO: Uncomment to re-enable SimSettings.
+        // return tm.getSimCount() > 0;
+        return false;
     }
 
     /**
diff --git a/src/com/android/settings/accounts/AddAccountSettings.java b/src/com/android/settings/accounts/AddAccountSettings.java
index 4e97c05..3af28b2 100644
--- a/src/com/android/settings/accounts/AddAccountSettings.java
+++ b/src/com/android/settings/accounts/AddAccountSettings.java
@@ -22,8 +22,8 @@
 import android.accounts.AuthenticatorException;
 import android.accounts.OperationCanceledException;
 import android.app.Activity;
-import android.app.ActivityManagerNative;
 import android.app.PendingIntent;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
@@ -69,6 +69,7 @@
      * application.
      */
     private static final String KEY_CALLER_IDENTITY = "pendingIntent";
+    private static final String SHOULD_NOT_RESOLVE = "SHOULDN'T RESOLVE!";
 
     private static final String TAG = "AccountSettings";
 
@@ -196,7 +197,21 @@
 
     private void addAccount(String accountType) {
         Bundle addAccountOptions = new Bundle();
-        mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(), 0);
+        /*
+         * The identityIntent is for the purposes of establishing the identity
+         * of the caller and isn't intended for launching activities, services
+         * or broadcasts.
+         *
+         * Unfortunately for legacy reasons we still need to support this. But
+         * we can cripple the intent so that 3rd party authenticators can't
+         * fill in addressing information and launch arbitrary actions.
+         */
+        Intent identityIntent = new Intent();
+        identityIntent.setComponent(new ComponentName(SHOULD_NOT_RESOLVE, SHOULD_NOT_RESOLVE));
+        identityIntent.setAction(SHOULD_NOT_RESOLVE);
+        identityIntent.addCategory(SHOULD_NOT_RESOLVE);
+
+        mPendingIntent = PendingIntent.getBroadcast(this, 0, identityIntent, 0);
         addAccountOptions.putParcelable(KEY_CALLER_IDENTITY, mPendingIntent);
         addAccountOptions.putBoolean(EXTRA_HAS_MULTIPLE_USERS, Utils.hasMultipleUsers(this));
         AccountManager.get(this).addAccountAsUser(
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index e29ccc9..a752fd9 100755
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -83,6 +83,7 @@
     private boolean mAvailableDevicesCategoryIsPresent;
 
     private boolean mInitialScanStarted;
+    private boolean mInitiateDiscoverable;
 
     private TextView mEmptyView;
     private SwitchBar mSwitchBar;
@@ -119,6 +120,7 @@
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
         mInitialScanStarted = (savedInstanceState != null);    // don't auto start scan after rotation
+        mInitiateDiscoverable = true;
 
         mEmptyView = (TextView) getView().findViewById(android.R.id.empty);
         getListView().setEmptyView(mEmptyView);
@@ -153,8 +155,7 @@
         }
         super.onResume();
 
-        // Make the device visible to other devices.
-        mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
+        mInitiateDiscoverable = true;
 
         if (isUiRestricted()) {
             setDeviceListGroup(getPreferenceScreen());
@@ -325,6 +326,14 @@
                 preferenceScreen.addPreference(mMyDevicePreference);
 
                 getActivity().invalidateOptionsMenu();
+
+                // mLocalAdapter.setScanMode is internally synchronized so it is okay for multiple
+                // threads to execute.
+                if (mInitiateDiscoverable) {
+                    // Make the device visible to other devices.
+                    mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
+                    mInitiateDiscoverable = false;
+                }
                 return; // not break
 
             case BluetoothAdapter.STATE_TURNING_OFF:
diff --git a/src/com/android/settings/wifi/AccessPoint.java b/src/com/android/settings/wifi/AccessPoint.java
index ac818a7..dda8d9e 100644
--- a/src/com/android/settings/wifi/AccessPoint.java
+++ b/src/com/android/settings/wifi/AccessPoint.java
@@ -433,6 +433,8 @@
      */
     private String getVisibilityStatus() {
         StringBuilder visibility = new StringBuilder();
+        StringBuilder scans24GHz = null;
+        StringBuilder scans5GHz = null;
 
         long now = System.currentTimeMillis();
 
@@ -441,7 +443,7 @@
             if (bssid != null) {
                 visibility.append(" ").append(bssid);
             }
-            visibility.append(" sc=").append(Integer.toString(mInfo.score));
+            visibility.append(" score=").append(mInfo.score);
             visibility.append(" ");
             visibility.append(String.format("tx=%.1f,", mInfo.txSuccessRate));
             visibility.append(String.format("%.1f,", mInfo.txRetriesRate));
@@ -455,63 +457,96 @@
             int num5 = 0;
             int num24 = 0;
             int numBlackListed = 0;
+            int n24 = 0; // Number scan results we included in the string
+            int n5 = 0; // Number scan results we included in the string
             Map<String, ScanResult> list = mScanResultCache.snapshot();
+            // TODO: sort list by RSSI or age
             for (ScanResult result : list.values()) {
                 if (result.seen == 0)
                     continue;
 
-                if (result.autoJoinStatus != ScanResult.ENABLED)
-                    numBlackListed++;
+                if (result.autoJoinStatus != ScanResult.ENABLED) numBlackListed++;
 
-                if (result.frequency > LOWER_FREQ_5GHZ
-                        && result.frequency < HIGHER_FREQ_5GHZ) {
-                    //strictly speaking: [4915, 5825]
-                    //number of known BSSID on 5GHz band
+                if (result.frequency >= LOWER_FREQ_5GHZ
+                        && result.frequency <= HIGHER_FREQ_5GHZ) {
+                    // Strictly speaking: [4915, 5825]
+                    // number of known BSSID on 5GHz band
                     num5 = num5 + 1;
-                } else if (result.frequency > LOWER_FREQ_24GHZ
-                        && result.frequency < HIGHER_FREQ_24GHZ) {
-                    //strictly speaking: [2412, 2482]
-                    //number of known BSSID on 2.4Ghz band
+                } else if (result.frequency >= LOWER_FREQ_24GHZ
+                        && result.frequency <= HIGHER_FREQ_24GHZ) {
+                    // Strictly speaking: [2412, 2482]
+                    // number of known BSSID on 2.4Ghz band
                     num24 = num24 + 1;
                 }
 
-                //ignore results seen, older than 20 seconds
+                // Ignore results seen, older than 20 seconds
                 if (now - result.seen > VISIBILITY_OUTDATED_AGE_IN_MILLI) continue;
 
-                if (result.frequency > LOWER_FREQ_5GHZ
-                        &&result.frequency < HIGHER_FREQ_5GHZ) {
+                if (result.frequency >= LOWER_FREQ_5GHZ
+                        && result.frequency <= HIGHER_FREQ_5GHZ) {
                     if (result.level > rssi5) {
                         rssi5 = result.level;
                     }
-                } else if (result.frequency > LOWER_FREQ_24GHZ
-                        && result.frequency < HIGHER_FREQ_24GHZ) {
+                    if (n5 < 4) {
+                        if (scans5GHz == null) scans5GHz = new StringBuilder();
+
+                        scans5GHz.append(" {").append(result.BSSID);
+                        scans5GHz.append("=").append(result.frequency);
+                        scans5GHz.append(",").append(result.level).append("}");
+                        n5++;
+                    }
+                } else if (result.frequency >= LOWER_FREQ_24GHZ
+                        && result.frequency <= HIGHER_FREQ_24GHZ) {
                     if (result.level > rssi24) {
                         rssi24 = result.level;
                     }
+                    if (n24 < 4) {
+                        if (scans24GHz == null) scans24GHz = new StringBuilder();
+                        scans24GHz.append(" {").append(result.BSSID);
+                        scans24GHz.append("=").append(result.frequency);
+                        scans24GHz.append(",").append(result.level).append("}");
+                        n24++;
+                    }
                 }
             }
             visibility.append(" [");
-            if (num24 > 0 || rssi24 > WifiConfiguration.INVALID_RSSI) {
-                visibility.append(Integer.toString(rssi24));
-                visibility.append(",");
-                visibility.append(Integer.toString(num24));
+            if (num24 > 0) {
+                visibility.append("(").append(num24).append(")");
+                if (n24 <= 4) {
+                    if (scans24GHz != null) {
+                        visibility.append(scans24GHz.toString());
+                    }
+                } else {
+                    visibility.append("max=").append(rssi24);
+                    if (scans24GHz != null) {
+                        visibility.append(",").append(scans24GHz.toString());
+                    }
+                }
             }
             visibility.append(";");
-            if (num5 > 0 || rssi5 > WifiConfiguration.INVALID_RSSI) {
-                visibility.append(Integer.toString(rssi5));
-                visibility.append(",");
-                visibility.append(Integer.toString(num5));
+            if (num5 > 0) {
+                visibility.append("(").append(num5).append(")");
+                if (n5 <= 4) {
+                    if (scans5GHz != null) {
+                        visibility.append(scans5GHz.toString());
+                    }
+                } else {
+                    visibility.append("max=").append(rssi5);
+                    if (scans5GHz != null) {
+                        visibility.append(",").append(scans5GHz.toString());
+                    }
+                }
             }
             if (numBlackListed > 0)
-                visibility.append("!");
+                visibility.append("!").append(numBlackListed);
             visibility.append("]");
         } else {
             if (mRssi != Integer.MAX_VALUE) {
-                visibility.append(", ss=");
-                visibility.append(Integer.toString(mRssi));
+                visibility.append(" rssi=");
+                visibility.append(mRssi);
                 if (mScanResult != null) {
-                    visibility.append(", ");
-                    visibility.append(Integer.toString(mScanResult.frequency));
+                    visibility.append(", f=");
+                    visibility.append(mScanResult.frequency);
                 }
             }
         }