Merge "Add PrefController in XML support"
diff --git a/res/xml/bluetooth_pairing_detail.xml b/res/xml/bluetooth_pairing_detail.xml
index e60da75..e654a3c 100644
--- a/res/xml/bluetooth_pairing_detail.xml
+++ b/res/xml/bluetooth_pairing_detail.xml
@@ -19,7 +19,9 @@
     android:title="@string/bluetooth_pairing_pref_title">
 
     <Preference
-        android:key="device_name"/>
+        android:key="bt_pair_rename_devices"
+        android:title="@string/bluetooth_device_name"
+        android:summary="@string/summary_placeholder" />
 
     <com.android.settings.bluetooth.BluetoothProgressCategory
         android:key="available_devices"
diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceController.java b/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceController.java
index 8b07bcb..2d0ce60 100644
--- a/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceController.java
+++ b/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceController.java
@@ -29,10 +29,9 @@
 import android.util.Log;
 
 import com.android.settings.R;
-import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settings.core.BasePreferenceController;
 import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
-import com.android.settingslib.core.AbstractPreferenceController;
 import com.android.settingslib.core.lifecycle.Lifecycle;
 import com.android.settingslib.core.lifecycle.LifecycleObserver;
 import com.android.settingslib.core.lifecycle.events.OnStart;
@@ -41,8 +40,8 @@
 /**
  * Controller that shows and updates the bluetooth device name
  */
-public class BluetoothDeviceNamePreferenceController extends AbstractPreferenceController
-        implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop {
+public class BluetoothDeviceNamePreferenceController extends BasePreferenceController implements
+        LifecycleObserver, OnStart, OnStop {
     private static final String TAG = "BluetoothNamePrefCtrl";
 
     public static final String KEY_DEVICE_NAME = "device_name";
@@ -62,12 +61,22 @@
             return;
         }
         mLocalAdapter = mLocalManager.getBluetoothAdapter();
-        lifecycle.addObserver(this);
+
+        if (lifecycle != null) {
+            lifecycle.addObserver(this);
+        }
+    }
+
+    /**
+     * Constructor exclusively used for Slice.
+     */
+    public BluetoothDeviceNamePreferenceController(Context context) {
+        this(context, (Lifecycle) null);
     }
 
     @VisibleForTesting
     BluetoothDeviceNamePreferenceController(Context context, LocalBluetoothAdapter localAdapter) {
-        super(context);
+        super(context, KEY_DEVICE_NAME);
         mLocalAdapter = localAdapter;
     }
 
@@ -89,8 +98,8 @@
     }
 
     @Override
-    public boolean isAvailable() {
-        return mLocalAdapter != null;
+    public int getAvailabilityStatus() {
+        return mLocalAdapter != null ? AVAILABLE : DISABLED_UNSUPPORTED;
     }
 
     @Override
diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceController.java b/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceController.java
index b64da26..69eefcf 100644
--- a/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceController.java
+++ b/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceController.java
@@ -20,6 +20,7 @@
 import android.content.Context;
 import android.support.annotation.VisibleForTesting;
 import android.support.v7.preference.Preference;
+import android.text.TextUtils;
 
 import com.android.internal.logging.nano.MetricsProto;
 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
@@ -30,29 +31,39 @@
 public class BluetoothDeviceRenamePreferenceController extends
         BluetoothDeviceNamePreferenceController {
 
-    public static final String PREF_KEY = "bt_rename_device";
-
     private final Fragment mFragment;
+    private String mPrefKey;
     private MetricsFeatureProvider mMetricsFeatureProvider;
 
-    public BluetoothDeviceRenamePreferenceController(Context context, Fragment fragment,
-            Lifecycle lifecycle) {
+    public BluetoothDeviceRenamePreferenceController(Context context, String prefKey,
+            Fragment fragment, Lifecycle lifecycle) {
         super(context, lifecycle);
+        mPrefKey = prefKey;
         mFragment = fragment;
         mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
     }
 
+    /**
+     * Constructor exclusively used for Slice.
+     */
+    public BluetoothDeviceRenamePreferenceController(Context context, String prefKey) {
+        super(context, (Lifecycle) null);
+        mPrefKey = prefKey;
+        mFragment = null;
+    }
+
     @VisibleForTesting
-    BluetoothDeviceRenamePreferenceController(Context context, Fragment fragment,
+    BluetoothDeviceRenamePreferenceController(Context context, String prefKey, Fragment fragment,
             LocalBluetoothAdapter localAdapter) {
         super(context, localAdapter);
+        mPrefKey = prefKey;
         mFragment = fragment;
         mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
     }
 
     @Override
     public String getPreferenceKey() {
-        return PREF_KEY;
+        return mPrefKey;
     }
 
     @Override
@@ -62,7 +73,7 @@
 
     @Override
     public boolean handlePreferenceTreeClick(Preference preference) {
-        if (PREF_KEY.equals(preference.getKey())) {
+        if (TextUtils.equals(mPrefKey, preference.getKey()) && mFragment != null) {
             mMetricsFeatureProvider.action(mContext,
                     MetricsProto.MetricsEvent.ACTION_BLUETOOTH_RENAME);
             LocalDeviceNameDialogFragment.newInstance()
diff --git a/src/com/android/settings/bluetooth/BluetoothPairingDetail.java b/src/com/android/settings/bluetooth/BluetoothPairingDetail.java
index a9756a6..5e003fe 100644
--- a/src/com/android/settings/bluetooth/BluetoothPairingDetail.java
+++ b/src/com/android/settings/bluetooth/BluetoothPairingDetail.java
@@ -46,10 +46,9 @@
     static final String KEY_AVAIL_DEVICES = "available_devices";
     @VisibleForTesting
     static final String KEY_FOOTER_PREF = "footer_preference";
+    private static final String KEY_RENAME_DEVICES = "bt_pair_rename_devices";
 
     @VisibleForTesting
-    BluetoothDeviceNamePreferenceController mDeviceNamePrefController;
-    @VisibleForTesting
     BluetoothProgressCategory mAvailableDevicesCategory;
     @VisibleForTesting
     FooterPreference mFooterPreference;
@@ -195,10 +194,10 @@
 
     @Override
     protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
-        List<AbstractPreferenceController> controllers = new ArrayList<>();
-        mDeviceNamePrefController = new BluetoothDeviceNamePreferenceController(context,
-                getLifecycle());
-        controllers.add(mDeviceNamePrefController);
+        final List<AbstractPreferenceController> controllers = new ArrayList<>();
+        controllers.add(
+                new BluetoothDeviceRenamePreferenceController(context, KEY_RENAME_DEVICES, this,
+                        getLifecycle()));
 
         return controllers;
     }
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index 72d8023..3acd477 100644
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -73,6 +73,7 @@
     static final String KEY_PAIRED_DEVICES = "paired_devices";
     @VisibleForTesting
     static final String KEY_FOOTER_PREF = "footer_preference";
+    private static final String KEY_RENAME_DEVICES = "bt_rename_device";
 
     @VisibleForTesting
     PreferenceGroup mPairedDevicesCategory;
@@ -369,7 +370,9 @@
         controllers.add(mDeviceNamePrefController);
         controllers.add(mPairingPrefController);
         controllers.add(new BluetoothFilesPreferenceController(context));
-        controllers.add(new BluetoothDeviceRenamePreferenceController(context, this, lifecycle));
+        controllers.add(
+                new BluetoothDeviceRenamePreferenceController(context, KEY_RENAME_DEVICES, this,
+                        lifecycle));
 
         return controllers;
     }
diff --git a/src/com/android/settings/system/SystemDashboardFragment.java b/src/com/android/settings/system/SystemDashboardFragment.java
index 94e4d80..323a2d4 100644
--- a/src/com/android/settings/system/SystemDashboardFragment.java
+++ b/src/com/android/settings/system/SystemDashboardFragment.java
@@ -16,8 +16,12 @@
 package com.android.settings.system;
 
 import android.content.Context;
+import android.os.Bundle;
 import android.os.UserManager;
 import android.provider.SearchIndexableResource;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceGroup;
+import android.support.v7.preference.PreferenceScreen;
 
 import com.android.internal.logging.nano.MetricsProto;
 import com.android.settings.R;
@@ -41,6 +45,17 @@
     private static final String KEY_RESET = "reset_dashboard";
 
     @Override
+    public void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+
+        final PreferenceScreen screen = getPreferenceScreen();
+        // We do not want to display an advanced button if only one setting is hidden
+        if (getVisiblePreferenceCount(screen) == screen.getInitialExpandedChildrenCount() + 1) {
+            screen.setInitialExpandedChildrenCount(Integer.MAX_VALUE);
+        }
+    }
+
+    @Override
     public int getMetricsCategory() {
         return MetricsProto.MetricsEvent.SETTINGS_SYSTEM_CATEGORY;
     }
@@ -74,6 +89,19 @@
         return controllers;
     }
 
+    private int getVisiblePreferenceCount(PreferenceGroup group) {
+        int visibleCount = 0;
+        for (int i = 0; i < group.getPreferenceCount(); i++) {
+            final Preference preference = group.getPreference(i);
+            if (preference instanceof PreferenceGroup) {
+                visibleCount += getVisiblePreferenceCount((PreferenceGroup) preference);
+            } else if (preference.isVisible()) {
+                visibleCount++;
+            }
+        }
+        return visibleCount;
+    }
+
     /**
      * For Search.
      */
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceControllerTest.java
index cde95cd..62a0d42 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceRenamePreferenceControllerTest.java
@@ -47,6 +47,7 @@
 public class BluetoothDeviceRenamePreferenceControllerTest {
 
     private static final String DEVICE_NAME = "Nightshade";
+    private static final String PREF_KEY = "bt_rename_devices";
 
     @Mock
     private LocalBluetoothAdapter mLocalAdapter;
@@ -66,10 +67,10 @@
 
         mContext = spy(RuntimeEnvironment.application);
         mPreference = new Preference(mContext);
-        mPreference.setKey(BluetoothDeviceRenamePreferenceController.PREF_KEY);
+        mPreference.setKey(PREF_KEY);
 
         mController = new BluetoothDeviceRenamePreferenceController(
-                mContext, mFragment, mLocalAdapter);
+                mContext, PREF_KEY, mFragment, mLocalAdapter);
     }
 
     @Test