Merge "Remove duplicates in Security Settings"
diff --git a/res/layout/storage_summary_donut.xml b/res/layout/storage_summary_donut.xml
index 585fbf4..eb26077 100644
--- a/res/layout/storage_summary_donut.xml
+++ b/res/layout/storage_summary_donut.xml
@@ -17,7 +17,7 @@
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
-    android:background="?android:attr/selectableItemBackground"
+    android:background="?android:attr/colorSecondary"
     android:gravity="center_vertical"
     android:orientation="horizontal" >
 
diff --git a/res/values/colors.xml b/res/values/colors.xml
index 449928f..f63d963 100644
--- a/res/values/colors.xml
+++ b/res/values/colors.xml
@@ -104,9 +104,6 @@
 
     <color name="status_bar_color">#3c3c3c</color>
 
-    <!-- Color for the background of the donut graph.-->
-    <color name="donut_background_grey">#ffd7d7d7</color>
-
     <!-- Color for the background of the shortcut icons.-->
     <color name="shortcut_background">#fff5f5f5</color>
 
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 038b184..0ca1628 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -378,6 +378,11 @@
     <!-- Description for bluetooth device name summary [CHAR LIMIT=none] -->
     <string name="bluetooth_device_name_summary">Visible as <xliff:g id="device_name">^1</xliff:g> to other devices</string>
 
+    <!-- Title for paired device group [CHAR LIMIT=none] -->
+    <string name="bluetooth_paired_device_title">Your devices</string>
+    <!-- Title for pairing bluetooth device page [CHAR LIMIT=none] -->
+    <string name="bluetooth_pairing_page_title">Pair bluetooth device</string>
+
     <!-- Date & time settings screen title -->
     <string name="date_and_time">Date &amp; time</string>
     <!-- The title of the activity to pick a time zone. -->
diff --git a/res/xml/bluetooth_pairing_detail.xml b/res/xml/bluetooth_pairing_detail.xml
new file mode 100644
index 0000000..30eaf09
--- /dev/null
+++ b/res/xml/bluetooth_pairing_detail.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 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:title="@string/bluetooth_settings">
+
+    <Preference
+        android:key="device_name"/>
+
+    <com.android.settings.bluetooth.BluetoothProgressCategory
+        android:key="available_devices"
+        android:title="@string/bluetooth_paired_device_title"/>
+
+    <com.android.settingslib.widget.FooterPreference/>
+
+</PreferenceScreen>
diff --git a/res/xml/bluetooth_settings.xml b/res/xml/bluetooth_settings.xml
index 783a860..046295b 100644
--- a/res/xml/bluetooth_settings.xml
+++ b/res/xml/bluetooth_settings.xml
@@ -4,9 +4,9 @@
      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.
@@ -16,6 +16,15 @@
 
 <PreferenceScreen
     xmlns:android="http://schemas.android.com/apk/res/android"
-    android:title="@string/bluetooth_settings" >
+    android:title="@string/bluetooth_settings">
+
+    <Preference
+        android:key="device_name"/>
+
+    <PreferenceCategory
+        android:key="paired_devices"
+        android:title="@string/bluetooth_paired_device_title"/>
+
+    <com.android.settingslib.widget.FooterPreference/>
 
 </PreferenceScreen>
diff --git a/res/xml/bluetooth_settings_obsolete.xml b/res/xml/bluetooth_settings_obsolete.xml
new file mode 100644
index 0000000..55a099c
--- /dev/null
+++ b/res/xml/bluetooth_settings_obsolete.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 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:title="@string/bluetooth_settings" >
+
+</PreferenceScreen>
diff --git a/res/xml/connected_devices.xml b/res/xml/connected_devices.xml
index d70cc33..ecbcbd1 100644
--- a/res/xml/connected_devices.xml
+++ b/res/xml/connected_devices.xml
@@ -19,7 +19,6 @@
     android:title="@string/connected_devices_dashboard_title">
 
     <com.android.settings.widget.MasterSwitchPreference
-      android:fragment="com.android.settings.bluetooth.BluetoothSettings"
       android:key="toggle_bluetooth"
       android:title="@string/bluetooth_settings_title"
       android:icon="@drawable/ic_settings_bluetooth"
diff --git a/res/xml/development_prefs.xml b/res/xml/development_prefs.xml
index 273b80d..d6f9dcc 100644
--- a/res/xml/development_prefs.xml
+++ b/res/xml/development_prefs.xml
@@ -424,6 +424,11 @@
             android:title="@string/show_all_anrs"
             android:summary="@string/show_all_anrs_summary"/>
 
+        <SwitchPreference
+            android:key="show_notification_channel_warnings"
+            android:title="@string/show_notification_channel_warnings"
+            android:summary="@string/show_notification_channel_warnings_summary"/>
+
         <Preference
                 android:key="inactive_apps"
                 android:title="@string/inactive_apps_title"
diff --git a/res/xml/suggestion_ordering.xml b/res/xml/suggestion_ordering.xml
index f0388a3..e21fe5b 100644
--- a/res/xml/suggestion_ordering.xml
+++ b/res/xml/suggestion_ordering.xml
@@ -18,7 +18,7 @@
     <step category="com.android.settings.suggested.category.DEFERRED_SETUP"
           exclusive="true" />
     <step category="com.android.settings.suggested.category.FIRST_IMPRESSION"
-          exclusiveExpireDays="7"
+          exclusiveExpireDays="14"
           exclusive="true"
           multiple="true" />
     <step category="com.android.settings.suggested.category.LOCK_SCREEN" />
diff --git a/src/com/android/settings/accounts/AccountDetailDashboardFragment.java b/src/com/android/settings/accounts/AccountDetailDashboardFragment.java
index 5aee39f..843b7fc 100644
--- a/src/com/android/settings/accounts/AccountDetailDashboardFragment.java
+++ b/src/com/android/settings/accounts/AccountDetailDashboardFragment.java
@@ -109,7 +109,7 @@
         mRemoveAccountController = new RemoveAccountPreferenceController(context, this);
         controllers.add(mRemoveAccountController);
         controllers.add(new AccountHeaderPreferenceController(
-                context, getActivity(), this, getArguments()));
+                context, getLifecycle(), getActivity(), this, getArguments()));
         return controllers;
     }
 
diff --git a/src/com/android/settings/accounts/AccountHeaderPreferenceController.java b/src/com/android/settings/accounts/AccountHeaderPreferenceController.java
index 4730e9e..d0ce58d 100644
--- a/src/com/android/settings/accounts/AccountHeaderPreferenceController.java
+++ b/src/com/android/settings/accounts/AccountHeaderPreferenceController.java
@@ -18,10 +18,10 @@
 
 import android.accounts.Account;
 import android.app.Activity;
-import android.app.Fragment;
 import android.content.Context;
 import android.os.Bundle;
 import android.os.UserHandle;
+import android.support.v14.preference.PreferenceFragment;
 import android.support.v7.preference.PreferenceScreen;
 
 import com.android.settings.R;
@@ -29,6 +29,7 @@
 import com.android.settings.core.PreferenceController;
 import com.android.settings.widget.EntityHeaderController;
 import com.android.settingslib.accounts.AuthenticatorHelper;
+import com.android.settingslib.core.lifecycle.Lifecycle;
 
 import static com.android.settings.accounts.AccountDetailDashboardFragment.KEY_ACCOUNT;
 import static com.android.settings.accounts.AccountDetailDashboardFragment.KEY_USER_HANDLE;
@@ -38,15 +39,17 @@
     private static final String KEY_ACCOUNT_HEADER = "account_header";
 
     private final Activity mActivity;
-    private final Fragment mHost;
+    private final PreferenceFragment mHost;
     private final Account mAccount;
     private final UserHandle mUserHandle;
+    private final Lifecycle mLifecycle;
 
-    public AccountHeaderPreferenceController(Context context, Activity activity, Fragment host,
-            Bundle args) {
+    public AccountHeaderPreferenceController(Context context, Lifecycle lifecycle,
+            Activity activity, PreferenceFragment host, Bundle args) {
         super(context);
         mActivity = activity;
         mHost = host;
+        mLifecycle = lifecycle;
         if (args != null && args.containsKey(KEY_ACCOUNT)) {
             mAccount = args.getParcelable(KEY_ACCOUNT);
         } else {
@@ -80,6 +83,7 @@
 
         EntityHeaderController
                 .newInstance(mActivity, mHost, headerPreference.findViewById(R.id.entity_header))
+                .setRecyclerView(mHost.getListView(), mLifecycle)
                 .setLabel(mAccount.name)
                 .setIcon(helper.getDrawableForType(mContext, mAccount.type))
                 .done(mActivity, true /* rebindButtons */);
diff --git a/src/com/android/settings/applications/AppInfoWithHeader.java b/src/com/android/settings/applications/AppInfoWithHeader.java
index bbcec3c..95877e9 100644
--- a/src/com/android/settings/applications/AppInfoWithHeader.java
+++ b/src/com/android/settings/applications/AppInfoWithHeader.java
@@ -43,6 +43,7 @@
         final Activity activity = getActivity();
         final Preference pref = EntityHeaderController
                 .newInstance(activity, this, null /* header */)
+                .setRecyclerView(getListView(), getLifecycle())
                 .setIcon(IconDrawableFactory.newInstance(activity)
                         .getBadgedIcon(mPackageInfo.applicationInfo))
                 .setLabel(mPackageInfo.applicationInfo.loadLabel(mPm))
diff --git a/src/com/android/settings/applications/InstalledAppDetails.java b/src/com/android/settings/applications/InstalledAppDetails.java
index 3021f75..3052281 100755
--- a/src/com/android/settings/applications/InstalledAppDetails.java
+++ b/src/com/android/settings/applications/InstalledAppDetails.java
@@ -86,6 +86,7 @@
 import com.android.settings.datausage.DataUsageSummary;
 import com.android.settings.fuelgauge.AdvancedPowerUsageDetail;
 import com.android.settings.fuelgauge.BatteryEntry;
+import com.android.settings.fuelgauge.BatteryStatsHelperLoader;
 import com.android.settings.fuelgauge.BatteryUtils;
 import com.android.settings.notification.AppNotificationSettings;
 import com.android.settings.notification.NotificationBackend;
@@ -139,6 +140,7 @@
 
     private static final int LOADER_CHART_DATA = 2;
     private static final int LOADER_STORAGE = 3;
+    private static final int LOADER_BATTERY = 4;
 
     private static final int DLG_FORCE_STOP = DLG_BASE + 1;
     private static final int DLG_DISABLE = DLG_BASE + 2;
@@ -200,6 +202,31 @@
     private String mBatteryPercent;
     private BatteryUtils mBatteryUtils;
 
+    private final LoaderCallbacks<BatteryStatsHelper> mBatteryCallbacks =
+            new LoaderCallbacks<BatteryStatsHelper>() {
+
+                @Override
+                public Loader<BatteryStatsHelper> onCreateLoader(int id, Bundle args) {
+                    return new BatteryStatsHelperLoader(getContext(), args);
+                }
+
+                @Override
+                public void onLoadFinished(Loader<BatteryStatsHelper> loader,
+                        BatteryStatsHelper batteryHelper) {
+                    mBatteryHelper = batteryHelper;
+                    if (mPackageInfo != null) {
+                        mSipper = findTargetSipper(batteryHelper, mPackageInfo.applicationInfo.uid);
+                        if (getActivity() != null) {
+                            updateBattery();
+                        }
+                    }
+                }
+
+                @Override
+                public void onLoaderReset(Loader<BatteryStatsHelper> loader) {
+                }
+            };
+
     private boolean handleDisableable(Button button) {
         boolean disableable = false;
         // Try to prevent the user from bricking their phone
@@ -358,7 +385,6 @@
         } else {
             removePreference(KEY_DATA);
         }
-        mBatteryHelper = new BatteryStatsHelper(getActivity(), true);
         mBatteryUtils = BatteryUtils.getInstance(getContext());
     }
 
@@ -382,7 +408,7 @@
                     mDataCallbacks);
             loaderManager.restartLoader(LOADER_STORAGE, Bundle.EMPTY, this);
         }
-        new BatteryUpdater().execute();
+        getLoaderManager().initLoader(LOADER_BATTERY, Bundle.EMPTY, mBatteryCallbacks);
         new MemoryUpdater().execute();
         updateDynamicPrefs();
     }
@@ -408,6 +434,7 @@
         mHeader = (LayoutPreference) findPreference(KEY_HEADER);
         mActionButtons = (LayoutPreference) findPreference(KEY_ACTION_BUTTONS);
         EntityHeaderController.newInstance(activity, this, mHeader.findViewById(R.id.entity_header))
+                .setRecyclerView(getListView(), getLifecycle())
                 .setPackageName(mPackageName)
                 .setButtonActions(EntityHeaderController.ActionType.ACTION_APP_PREFERENCE,
                         EntityHeaderController.ActionType.ACTION_NONE)
@@ -585,11 +612,11 @@
         final CharSequence summary =
                 isInstantApp ? null : getString(Utils.getInstallationStatus(mAppEntry.info));
         EntityHeaderController.newInstance(activity, this, appSnippet)
-            .setLabel(mAppEntry)
-            .setIcon(mAppEntry)
-            .setSummary(summary)
-            .setIsInstantApp(isInstantApp)
-            .done(activity, false /* rebindActions */);
+                .setLabel(mAppEntry)
+                .setIcon(mAppEntry)
+                .setSummary(summary)
+                .setIsInstantApp(isInstantApp)
+                .done(activity, false /* rebindActions */);
         mVersionPreference.setSummary(getString(R.string.version_text, pkgInfo.versionName));
     }
 
@@ -617,6 +644,19 @@
         return showIt;
     }
 
+    @VisibleForTesting
+    BatterySipper findTargetSipper(BatteryStatsHelper batteryHelper, int uid) {
+        List<BatterySipper> usageList = batteryHelper.getUsageList();
+        for (int i = 0, size = usageList.size(); i < size; i++) {
+            BatterySipper sipper = usageList.get(i);
+            if (sipper.getUid() == uid) {
+                return sipper;
+            }
+        }
+
+        return null;
+    }
+
     private boolean signaturesMatch(String pkg1, String pkg2) {
         if (pkg1 != null && pkg2 != null) {
             try {
@@ -711,7 +751,7 @@
     }
 
     private void updateBattery() {
-        if (mSipper != null) {
+        if (mSipper != null && mBatteryHelper != null) {
             mBatteryPreference.setEnabled(true);
             final int dischargeAmount = mBatteryHelper.getStats().getDischargeAmount(
                     BatteryStats.STATS_SINCE_CHARGED);
@@ -1310,33 +1350,6 @@
 
     }
 
-    private class BatteryUpdater extends AsyncTask<Void, Void, Void> {
-        @Override
-        protected Void doInBackground(Void... params) {
-            mBatteryHelper.create((Bundle) null);
-            mBatteryHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED,
-                    mUserManager.getUserProfiles());
-            List<BatterySipper> usageList = mBatteryHelper.getUsageList();
-            final int N = usageList.size();
-            for (int i = 0; i < N; i++) {
-                BatterySipper sipper = usageList.get(i);
-                if (sipper.getUid() == mPackageInfo.applicationInfo.uid) {
-                    mSipper = sipper;
-                    break;
-                }
-            }
-            return null;
-        }
-
-        @Override
-        protected void onPostExecute(Void result) {
-            if (getActivity() == null) {
-                return;
-            }
-            refreshUi();
-        }
-    }
-
     /**
      * Elicit this class for testing. Test cannot be done in robolectric because it
      * invokes the new API.
diff --git a/src/com/android/settings/applications/NotificationApps.java b/src/com/android/settings/applications/NotificationApps.java
index c54363a..f290d43 100644
--- a/src/com/android/settings/applications/NotificationApps.java
+++ b/src/com/android/settings/applications/NotificationApps.java
@@ -17,7 +17,6 @@
 import android.app.Activity;
 import android.content.Context;
 import android.content.pm.ApplicationInfo;
-import android.content.pm.PackageManager;
 
 import com.android.settings.R;
 import com.android.settings.dashboard.SummaryLoader;
@@ -29,23 +28,24 @@
  */
 public class NotificationApps extends ManageApplications {
 
-    private static class SummaryProvider implements SummaryLoader.SummaryProvider {
+    public static class SummaryProvider implements SummaryLoader.SummaryProvider {
 
         private final Context mContext;
         private final SummaryLoader mLoader;
         private final NotificationBackend mNotificationBackend;
+        private final PackageManagerWrapper mPackageManager;
 
-        private SummaryProvider(Context context, SummaryLoader loader) {
+        public SummaryProvider(Context context, SummaryLoader loader) {
             mContext = context;
             mLoader = loader;
             mNotificationBackend = new NotificationBackend();
+            mPackageManager = new PackageManagerWrapperImpl(mContext.getPackageManager());
         }
 
         @Override
         public void setListening(boolean listening) {
             if (listening) {
-                new AppCounter(mContext,
-                        new PackageManagerWrapperImpl(mContext.getPackageManager())) {
+                new AppCounter(mContext, mPackageManager) {
                     @Override
                     protected void onCountComplete(int num) {
                         updateSummary(num);
diff --git a/src/com/android/settings/applications/ProcessStatsDetail.java b/src/com/android/settings/applications/ProcessStatsDetail.java
index 9f229af..b9c3826 100644
--- a/src/com/android/settings/applications/ProcessStatsDetail.java
+++ b/src/com/android/settings/applications/ProcessStatsDetail.java
@@ -128,16 +128,17 @@
         final Activity activity = getActivity();
         final Preference pref = EntityHeaderController
                 .newInstance(activity, this, null /* appHeader */)
-            .setIcon(mApp.mUiTargetApp != null
-                ? IconDrawableFactory.newInstance(activity).getBadgedIcon(mApp.mUiTargetApp)
-                : new ColorDrawable(0))
-            .setLabel(mApp.mUiLabel)
-            .setPackageName(mApp.mPackage)
-            .setUid(mApp.mUiTargetApp != null
-                ? mApp.mUiTargetApp.uid
-                : UserHandle.USER_NULL)
-            .setButtonActions(ActionType.ACTION_APP_INFO, ActionType.ACTION_NONE)
-            .done(activity, getPrefContext());
+                .setRecyclerView(getListView(), getLifecycle())
+                .setIcon(mApp.mUiTargetApp != null
+                        ? IconDrawableFactory.newInstance(activity).getBadgedIcon(mApp.mUiTargetApp)
+                        : new ColorDrawable(0))
+                .setLabel(mApp.mUiLabel)
+                .setPackageName(mApp.mPackage)
+                .setUid(mApp.mUiTargetApp != null
+                        ? mApp.mUiTargetApp.uid
+                        : UserHandle.USER_NULL)
+                .setButtonActions(ActionType.ACTION_APP_INFO, ActionType.ACTION_NONE)
+                .done(activity, getPrefContext());
         getPreferenceScreen().addPreference(pref);
     }
 
diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceController.java b/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceController.java
index db4c3e0..baab3fc 100644
--- a/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceController.java
+++ b/src/com/android/settings/bluetooth/BluetoothDeviceNamePreferenceController.java
@@ -78,6 +78,11 @@
     }
 
     @Override
+    public void displayPreference(PreferenceScreen screen) {
+        mPreference = screen.findPreference(KEY_DEVICE_NAME);
+    }
+
+    @Override
     public void onStart() {
         mContext.registerReceiver(mReceiver,
                 new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED));
diff --git a/src/com/android/settings/bluetooth/BluetoothMasterSwitchPreferenceController.java b/src/com/android/settings/bluetooth/BluetoothMasterSwitchPreferenceController.java
index f9b7975..6d474ee 100644
--- a/src/com/android/settings/bluetooth/BluetoothMasterSwitchPreferenceController.java
+++ b/src/com/android/settings/bluetooth/BluetoothMasterSwitchPreferenceController.java
@@ -15,11 +15,16 @@
  */
 package com.android.settings.bluetooth;
 
+import android.app.Fragment;
 import android.content.Context;
+import android.os.UserHandle;
+import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceScreen;
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.settings.R;
+import com.android.settings.SettingsActivity;
 import com.android.settings.core.PreferenceController;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.widget.MasterSwitchController;
@@ -43,19 +48,27 @@
     private BluetoothEnabler mBluetoothEnabler;
     private BluetoothSummaryUpdater mSummaryUpdater;
     private RestrictionUtils mRestrictionUtils;
+    private Fragment mFragment;
+    private SettingsActivity mActivity;
+    private BluetoothFeatureProvider mBluetoothFeatureProvider;
 
     public BluetoothMasterSwitchPreferenceController(Context context,
-            LocalBluetoothManager bluetoothManager) {
-        this(context, bluetoothManager, new RestrictionUtils());
+            LocalBluetoothManager bluetoothManager, Fragment fragment, SettingsActivity activity) {
+        this(context, bluetoothManager, new RestrictionUtils(), fragment, activity);
     }
 
     @VisibleForTesting
     public BluetoothMasterSwitchPreferenceController(Context context,
-            LocalBluetoothManager bluetoothManager, RestrictionUtils restrictionUtils) {
+            LocalBluetoothManager bluetoothManager, RestrictionUtils restrictionUtils,
+            Fragment fragment, SettingsActivity activity) {
         super(context);
         mBluetoothManager = bluetoothManager;
         mSummaryUpdater = new BluetoothSummaryUpdater(mContext, this, mBluetoothManager);
         mRestrictionUtils = restrictionUtils;
+        mFragment = fragment;
+        mActivity = activity;
+        mBluetoothFeatureProvider = FeatureFactory.getFactory(
+                mContext).getBluetoothFeatureProvider(mContext);
     }
 
     @Override
@@ -63,10 +76,23 @@
         super.displayPreference(screen);
         mBtPreference = (MasterSwitchPreference) screen.findPreference(KEY_TOGGLE_BLUETOOTH);
         mBluetoothEnabler = new BluetoothEnabler(mContext,
-            new MasterSwitchController(mBtPreference),
-            FeatureFactory.getFactory(mContext).getMetricsFeatureProvider(), mBluetoothManager,
-            MetricsEvent.ACTION_SETTINGS_MASTER_SWITCH_BLUETOOTH_TOGGLE,
-            mRestrictionUtils);
+                new MasterSwitchController(mBtPreference),
+                FeatureFactory.getFactory(mContext).getMetricsFeatureProvider(), mBluetoothManager,
+                MetricsEvent.ACTION_SETTINGS_MASTER_SWITCH_BLUETOOTH_TOGGLE,
+                mRestrictionUtils);
+    }
+
+    @Override
+    public boolean handlePreferenceTreeClick(Preference preference) {
+        if (KEY_TOGGLE_BLUETOOTH.equals(preference.getKey())) {
+            final String fragmentClass = mBluetoothFeatureProvider.isPairingPageEnabled() ?
+                    BluetoothSettings.class.getName() :
+                    BluetoothSettingsObsolete.class.getName();
+            mActivity.startPreferencePanelAsUser(mFragment, fragmentClass, null, R.string.bluetooth,
+                    null, new UserHandle(UserHandle.myUserId()));
+            return true;
+        }
+        return super.handlePreferenceTreeClick(preference);
     }
 
     @Override
@@ -79,6 +105,7 @@
         return KEY_TOGGLE_BLUETOOTH;
     }
 
+    @Override
     public void onResume() {
         mSummaryUpdater.register(true);
     }
diff --git a/src/com/android/settings/bluetooth/BluetoothPairingDetail.java b/src/com/android/settings/bluetooth/BluetoothPairingDetail.java
new file mode 100644
index 0000000..7e2978d
--- /dev/null
+++ b/src/com/android/settings/bluetooth/BluetoothPairingDetail.java
@@ -0,0 +1,194 @@
+/*
+ * Copyright (C) 2017 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.bluetooth;
+
+import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.content.Context;
+import android.os.Bundle;
+import android.support.annotation.VisibleForTesting;
+
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.settings.R;
+import com.android.settings.core.PreferenceController;
+import com.android.settings.search.Indexable;
+import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.widget.FooterPreference;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * BluetoothPairingDetail is a page to scan bluetooth devices and pair them.
+ */
+public class BluetoothPairingDetail extends DeviceListPreferenceFragment implements
+        Indexable {
+    private static final String TAG = "BluetoothPairingDetail";
+
+    @VisibleForTesting
+    static final String KEY_AVAIL_DEVICES = "available_devices";
+    @VisibleForTesting
+    static final String KEY_FOOTER_PREF = "footer_preference";
+
+    @VisibleForTesting
+    BluetoothDeviceNamePreferenceController mDeviceNamePrefController;
+    @VisibleForTesting
+    BluetoothProgressCategory mAvailableDevicesCategory;
+    @VisibleForTesting
+    FooterPreference mFooterPreference;
+
+    private boolean mInitialScanStarted;
+
+    public BluetoothPairingDetail() {
+        super(DISALLOW_CONFIG_BLUETOOTH);
+    }
+
+    @Override
+    public void onActivityCreated(Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+        mInitialScanStarted = false;
+    }
+
+    @Override
+    public void onStart() {
+        super.onStart();
+
+        if (mLocalAdapter != null) {
+            updateContent(mLocalAdapter.getBluetoothState());
+            mAvailableDevicesCategory.setProgress(mLocalAdapter.isDiscovering());
+        }
+    }
+
+    @Override
+    public void onStop() {
+        super.onStop();
+
+        // Make the device only visible to connected devices.
+        mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
+        mLocalAdapter.stopScanning();
+    }
+
+    @Override
+    void initPreferencesFromPreferenceScreen() {
+        mAvailableDevicesCategory = (BluetoothProgressCategory) findPreference(KEY_AVAIL_DEVICES);
+        mFooterPreference = (FooterPreference) findPreference(KEY_FOOTER_PREF);
+        mFooterPreference.setSelectable(false);
+    }
+
+    @Override
+    public int getMetricsCategory() {
+        //TODO(b/38383542): add bluetooth pairing category
+        return MetricsEvent.BLUETOOTH;
+    }
+
+    @VisibleForTesting
+    void startScanning() {
+        if (mAvailableDevicesCategory != null) {
+            removeAllDevices();
+        }
+
+        mLocalManager.getCachedDeviceManager().clearNonBondedDevices();
+        mInitialScanStarted = true;
+        mLocalAdapter.startScanning(true);
+    }
+
+    @Override
+    void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
+        mLocalAdapter.stopScanning();
+        super.onDevicePreferenceClick(btPreference);
+    }
+
+    @Override
+    public void onScanningStateChanged(boolean started) {
+        mAvailableDevicesCategory.setProgress(started);
+    }
+
+    @VisibleForTesting
+    void updateContent(int bluetoothState) {
+        switch (bluetoothState) {
+            case BluetoothAdapter.STATE_ON:
+                mDevicePreferenceMap.clear();
+                mLocalAdapter.setBluetoothEnabled(true);
+
+                addDeviceCategory(mAvailableDevicesCategory,
+                        R.string.bluetooth_preference_found_devices,
+                        BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, mInitialScanStarted);
+                updateFooterPreference(mFooterPreference);
+
+                if (!mInitialScanStarted) {
+                    startScanning();
+                }
+
+                // mLocalAdapter.setScanMode is internally synchronized so it is okay for multiple
+                // threads to execute.
+                mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
+                break;
+
+            case BluetoothAdapter.STATE_OFF:
+                finish();
+                break;
+        }
+    }
+
+    @Override
+    public void onBluetoothStateChanged(int bluetoothState) {
+        super.onBluetoothStateChanged(bluetoothState);
+        updateContent(bluetoothState);
+    }
+
+    @Override
+    public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
+        if (bondState == BluetoothDevice.BOND_BONDED) {
+            // If one device is connected(bonded), then close this fragment.
+            finish();
+        }
+    }
+
+    @Override
+    protected int getHelpResource() {
+        return R.string.help_url_bluetooth;
+    }
+
+    @Override
+    protected String getLogTag() {
+        return TAG;
+    }
+
+    @Override
+    protected int getPreferenceScreenResId() {
+        return R.xml.bluetooth_pairing_detail;
+    }
+
+    @Override
+    protected List<PreferenceController> getPreferenceControllers(Context context) {
+        List<PreferenceController> controllers = new ArrayList<>();
+        mDeviceNamePrefController = new BluetoothDeviceNamePreferenceController(context,
+                this, getLifecycle());
+        controllers.add(mDeviceNamePrefController);
+
+        return controllers;
+    }
+
+    @Override
+    public String getDeviceListKey() {
+        return KEY_AVAIL_DEVICES;
+    }
+
+}
diff --git a/src/com/android/settings/bluetooth/BluetoothPairingPreferenceController.java b/src/com/android/settings/bluetooth/BluetoothPairingPreferenceController.java
index 6409d3b..ab99aad 100644
--- a/src/com/android/settings/bluetooth/BluetoothPairingPreferenceController.java
+++ b/src/com/android/settings/bluetooth/BluetoothPairingPreferenceController.java
@@ -16,12 +16,12 @@
 
 package com.android.settings.bluetooth;
 
-import android.app.Fragment;
 import android.content.Context;
 import android.support.v14.preference.PreferenceFragment;
 import android.support.v7.preference.Preference;
-import android.support.v7.preference.PreferenceScreen;
+import android.os.UserHandle;
 
+import com.android.settings.SettingsActivity;
 import com.android.settings.core.PreferenceController;
 import com.android.settings.R;
 
@@ -34,11 +34,14 @@
 
     public static final String KEY_PAIRING = "pref_bt_pairing";
     private PreferenceFragment mFragment;
+    private SettingsActivity mActivity;
     private Preference mPreference;
 
-    public BluetoothPairingPreferenceController(Context context, PreferenceFragment fragment) {
-       super(context);
+    public BluetoothPairingPreferenceController(Context context, PreferenceFragment fragment,
+            SettingsActivity activity) {
+        super(context);
         mFragment = fragment;
+        mActivity = activity;
     }
 
     @Override
@@ -54,7 +57,9 @@
     @Override
     public boolean handlePreferenceTreeClick(Preference preference) {
         if (KEY_PAIRING.equals(preference.getKey())) {
-            //TODO: open the pairing page
+            mActivity.startPreferencePanelAsUser(mFragment, BluetoothPairingDetail.class.getName(),
+                    null, R.string.bluetooth_pairing_page_title, null,
+                    new UserHandle(UserHandle.myUserId()));
             return true;
         }
 
@@ -66,10 +71,11 @@
      *
      * @return bluetooth preference that created in this method
      */
-    public Preference createBluetoothPairingPreference() {
+    public Preference createBluetoothPairingPreference(int order) {
         mPreference = new Preference(mFragment.getPreferenceScreen().getContext());
         mPreference.setKey(KEY_PAIRING);
         mPreference.setIcon(R.drawable.ic_add);
+        mPreference.setOrder(order);
         mPreference.setTitle(R.string.bluetooth_pairing_pref_title);
 
         return mPreference;
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index ed2629e..b37770a 100644
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -19,7 +19,6 @@
 import android.app.Activity;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
-import android.content.BroadcastReceiver;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
@@ -29,10 +28,8 @@
 import android.provider.Settings;
 import android.support.annotation.VisibleForTesting;
 import android.support.v7.preference.Preference;
-import android.support.v7.preference.PreferenceCategory;
 import android.support.v7.preference.PreferenceGroup;
 import android.support.v7.preference.PreferenceScreen;
-import android.text.BidiFormatter;
 import android.text.Spannable;
 import android.text.style.TextAppearanceSpan;
 import android.util.Log;
@@ -49,6 +46,7 @@
 import com.android.settings.core.PreferenceController;
 import com.android.settings.dashboard.SummaryLoader;
 import com.android.settings.location.ScanningSettings;
+import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Indexable;
 import com.android.settings.search.SearchIndexableRaw;
@@ -64,7 +62,6 @@
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Locale;
 import java.util.Set;
 
 import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
@@ -72,11 +69,11 @@
 /**
  * BluetoothSettings is the Settings screen for Bluetooth configuration and
  * connection management.
+ *
  */
 public class BluetoothSettings extends DeviceListPreferenceFragment implements Indexable {
     private static final String TAG = "BluetoothSettings";
 
-    private static final int MENU_ID_SCAN = Menu.FIRST;
     private static final int MENU_ID_SHOW_RECEIVED = Menu.FIRST + 1;
 
     /* Private intent to show the list of received files */
@@ -85,45 +82,31 @@
     private static final String BTOPP_PACKAGE =
             "com.android.bluetooth";
 
-    private static final String KEY_PAIRED_DEVICES = "paired_devices";
+    private static final int PAIRED_DEVICE_ORDER = 1;
+    private static final int PAIRING_PREF_ORDER = 2;
 
-    private static View mSettingsDialogView = null;
+    @VisibleForTesting
+    static final String KEY_PAIRED_DEVICES = "paired_devices";
+    @VisibleForTesting
+    static final String KEY_FOOTER_PREF = "footer_preference";
 
-    private BluetoothEnabler mBluetoothEnabler;
-
-    private PreferenceGroup mPairedDevicesCategory;
-    private PreferenceGroup mAvailableDevicesCategory;
-    private Preference mDeviceNamePreference;
+    @VisibleForTesting
+    PreferenceGroup mPairedDevicesCategory;
+    @VisibleForTesting
+    FooterPreference mFooterPreference;
     private Preference mPairingPreference;
-    private boolean mAvailableDevicesCategoryIsPresent;
-
-    private boolean mInitialScanStarted;
-    private boolean mInitiateDiscoverable;
+    private BluetoothEnabler mBluetoothEnabler;
 
     private SwitchBar mSwitchBar;
 
     private final IntentFilter mIntentFilter;
     private BluetoothDeviceNamePreferenceController mDeviceNamePrefController;
-    private BluetoothPairingPreferenceController mPairingPrefController;
+    @VisibleForTesting
+    BluetoothPairingPreferenceController mPairingPrefController;
 
     // For Search
-    private static final String DATA_KEY_REFERENCE = "main_toggle_bluetooth";
-
-    // accessed from inner class (not private to avoid thunks)
-    FooterPreference mMyDevicePreference;
-
-    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            final String action = intent.getAction();
-            final int state =
-                    intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
-
-            if (state == BluetoothAdapter.STATE_ON) {
-                mInitiateDiscoverable = true;
-            }
-        }
-    };
+    @VisibleForTesting
+    static final String DATA_KEY_REFERENCE = "main_toggle_bluetooth";
 
     public BluetoothSettings() {
         super(DISALLOW_CONFIG_BLUETOOTH);
@@ -138,15 +121,13 @@
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
-        mInitialScanStarted = false;
-        mInitiateDiscoverable = true;
 
         final SettingsActivity activity = (SettingsActivity) getActivity();
         mSwitchBar = activity.getSwitchBar();
 
         mBluetoothEnabler = new BluetoothEnabler(activity, new SwitchBarController(mSwitchBar),
-            mMetricsFeatureProvider, Utils.getLocalBtManager(activity),
-            MetricsEvent.ACTION_BLUETOOTH_TOGGLE);
+                mMetricsFeatureProvider, Utils.getLocalBtManager(activity),
+                MetricsEvent.ACTION_BLUETOOTH_TOGGLE);
         mBluetoothEnabler.setupSwitchController();
     }
 
@@ -158,28 +139,11 @@
     }
 
     @Override
-    void addPreferencesForActivity() {
-        final Context prefContext = getPrefContext();
-
-        mDeviceNamePreference = mDeviceNamePrefController.createBluetoothDeviceNamePreference(
-                getPreferenceScreen(), 1 /* order */);
-
-        mPairedDevicesCategory = new PreferenceCategory(prefContext);
-        mPairedDevicesCategory.setKey(KEY_PAIRED_DEVICES);
-        mPairedDevicesCategory.setOrder(2);
-        getPreferenceScreen().addPreference(mPairedDevicesCategory);
-
-        mAvailableDevicesCategory = new BluetoothProgressCategory(prefContext);
-        mAvailableDevicesCategory.setSelectable(false);
-        mAvailableDevicesCategory.setOrder(3);
-        getPreferenceScreen().addPreference(mAvailableDevicesCategory);
-
-        mMyDevicePreference = mFooterPreferenceMixin.createFooterPreference();
-        mMyDevicePreference.setSelectable(false);
-
-        mPairingPreference = mPairingPrefController.createBluetoothPairingPreference();
-
-        setHasOptionsMenu(true);
+    void initPreferencesFromPreferenceScreen() {
+        mPairingPreference = mPairingPrefController.createBluetoothPairingPreference(
+                PAIRING_PREF_ORDER);
+        mFooterPreference = (FooterPreference) findPreference(KEY_FOOTER_PREF);
+        mPairedDevicesCategory = (PreferenceGroup) findPreference(KEY_PAIRED_DEVICES);
     }
 
     @Override
@@ -190,19 +154,14 @@
             mBluetoothEnabler.resume(getActivity());
         }
         super.onStart();
-
-        mInitiateDiscoverable = true;
-
         if (isUiRestricted()) {
-            setDeviceListGroup(getPreferenceScreen());
+            getPreferenceScreen().removeAll();
             if (!isUiRestrictedByOnlyAdmin()) {
                 getEmptyTextView().setText(R.string.bluetooth_empty_list_user_restricted);
             }
-            removeAllDevices();
             return;
         }
 
-        getActivity().registerReceiver(mReceiver, mIntentFilter);
         if (mLocalAdapter != null) {
             updateContent(mLocalAdapter.getBluetoothState());
         }
@@ -221,8 +180,6 @@
         if (isUiRestricted()) {
             return;
         }
-
-        getActivity().unregisterReceiver(mReceiver);
     }
 
     @Override
@@ -231,13 +188,6 @@
         // If the user is not allowed to configure bluetooth, do not show the menu.
         if (isUiRestricted()) return;
 
-        boolean bluetoothIsEnabled = mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON;
-        boolean isDiscovering = mLocalAdapter.isDiscovering();
-        int textId = isDiscovering ? R.string.bluetooth_searching_for_devices :
-                R.string.bluetooth_search_for_devices;
-        menu.add(Menu.NONE, MENU_ID_SCAN, 0, textId)
-                .setEnabled(bluetoothIsEnabled && !isDiscovering)
-                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
         menu.add(Menu.NONE, MENU_ID_SHOW_RECEIVED, 0, R.string.bluetooth_show_received_files)
                 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
         super.onCreateOptionsMenu(menu, inflater);
@@ -246,14 +196,6 @@
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId()) {
-            case MENU_ID_SCAN:
-                if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
-                    mMetricsFeatureProvider.action(getActivity(),
-                            MetricsEvent.ACTION_BLUETOOTH_SCAN);
-                    startScanning();
-                }
-                return true;
-
             case MENU_ID_SHOW_RECEIVED:
                 mMetricsFeatureProvider.action(getActivity(),
                         MetricsEvent.ACTION_BLUETOOTH_FILES);
@@ -265,104 +207,37 @@
         return super.onOptionsItemSelected(item);
     }
 
-    private void startScanning() {
-        if (isUiRestricted()) {
-            return;
-        }
-
-        if (!mAvailableDevicesCategoryIsPresent) {
-            getPreferenceScreen().addPreference(mAvailableDevicesCategory);
-            mAvailableDevicesCategoryIsPresent = true;
-        }
-
-        if (mAvailableDevicesCategory != null) {
-            setDeviceListGroup(mAvailableDevicesCategory);
-            removeAllDevices();
-        }
-
-        mLocalManager.getCachedDeviceManager().clearNonBondedDevices();
-        mAvailableDevicesCategory.removeAll();
-        mInitialScanStarted = true;
-        mLocalAdapter.startScanning(true);
-    }
-
     @Override
-    void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
-        mLocalAdapter.stopScanning();
-        super.onDevicePreferenceClick(btPreference);
-    }
-
-    private void addDeviceCategory(PreferenceGroup preferenceGroup, int titleId,
-            BluetoothDeviceFilter.Filter filter, boolean addCachedDevices) {
-        cacheRemoveAllPrefs(preferenceGroup);
-        preferenceGroup.setTitle(titleId);
-        setFilter(filter);
-        setDeviceListGroup(preferenceGroup);
-        if (addCachedDevices) {
-            addCachedDevices();
-        }
-        preferenceGroup.setEnabled(true);
-        removeCachedPrefs(preferenceGroup);
+    public String getDeviceListKey() {
+        return KEY_PAIRED_DEVICES;
     }
 
     private void updateContent(int bluetoothState) {
-        final PreferenceScreen preferenceScreen = getPreferenceScreen();
         int messageId = 0;
 
         switch (bluetoothState) {
             case BluetoothAdapter.STATE_ON:
+                displayEmptyMessage(false);
                 mDevicePreferenceMap.clear();
 
                 if (isUiRestricted()) {
                     messageId = R.string.bluetooth_empty_list_user_restricted;
                     break;
                 }
-                getPreferenceScreen().removeAll();
-                getPreferenceScreen().addPreference(mDeviceNamePreference);
-                getPreferenceScreen().addPreference(mPairedDevicesCategory);
-                getPreferenceScreen().addPreference(mAvailableDevicesCategory);
-                getPreferenceScreen().addPreference(mMyDevicePreference);
 
-                // Paired devices category
                 addDeviceCategory(mPairedDevicesCategory,
                         R.string.bluetooth_preference_paired_devices,
                         BluetoothDeviceFilter.BONDED_DEVICE_FILTER, true);
                 mPairedDevicesCategory.addPreference(mPairingPreference);
-                int numberOfPairedDevices = mPairedDevicesCategory.getPreferenceCount();
+                updateFooterPreference(mFooterPreference);
 
-                if (isUiRestricted() || numberOfPairedDevices <= 0) {
-                    if (preferenceScreen.findPreference(KEY_PAIRED_DEVICES) != null) {
-                        preferenceScreen.removePreference(mPairedDevicesCategory);
-                    }
-                } else {
-                    if (preferenceScreen.findPreference(KEY_PAIRED_DEVICES) == null) {
-                        preferenceScreen.addPreference(mPairedDevicesCategory);
-                    }
-                }
-
-                // Available devices category
-                addDeviceCategory(mAvailableDevicesCategory,
-                        R.string.bluetooth_preference_found_devices,
-                        BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, mInitialScanStarted);
-
-                if (!mInitialScanStarted) {
-                    startScanning();
-                }
-
-                updateMyDevicePreference(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;
-                }
+                mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
                 return; // not break
 
             case BluetoothAdapter.STATE_TURNING_OFF:
                 messageId = R.string.bluetooth_turning_off;
+                mLocalAdapter.stopScanning();
                 break;
 
             case BluetoothAdapter.STATE_OFF:
@@ -374,12 +249,10 @@
 
             case BluetoothAdapter.STATE_TURNING_ON:
                 messageId = R.string.bluetooth_turning_on;
-                mInitialScanStarted = false;
                 break;
         }
 
-        setDeviceListGroup(preferenceScreen);
-        removeAllDevices();
+        displayEmptyMessage(true);
         if (messageId != 0) {
             getEmptyTextView().setText(messageId);
         }
@@ -418,18 +291,21 @@
                 }
             });
         }
-        getPreferenceScreen().removeAll();
         setTextSpan(emptyView.getText(), briefText);
     }
 
+    @VisibleForTesting
+    void displayEmptyMessage(boolean display) {
+        final Activity activity = getActivity();
+        activity.findViewById(android.R.id.list_container).setVisibility(
+                display ? View.INVISIBLE : View.VISIBLE);
+        activity.findViewById(android.R.id.empty).setVisibility(
+                display ? View.VISIBLE : View.GONE);
+    }
+
     @Override
     public void onBluetoothStateChanged(int bluetoothState) {
         super.onBluetoothStateChanged(bluetoothState);
-        // If BT is turned off/on staying in the same BT Settings screen
-        // discoverability to be set again
-        if (BluetoothAdapter.STATE_ON == bluetoothState) {
-            mInitiateDiscoverable = true;
-        }
         updateContent(bluetoothState);
     }
 
@@ -437,15 +313,14 @@
     public void onScanningStateChanged(boolean started) {
         super.onScanningStateChanged(started);
         // Update options' enabled state
-        if (getActivity() != null) {
-            getActivity().invalidateOptionsMenu();
+        final Activity activity = getActivity();
+        if (activity != null) {
+            activity.invalidateOptionsMenu();
         }
     }
 
     @Override
     public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
-        setDeviceListGroup(getPreferenceScreen());
-        removeAllDevices();
         updateContent(mLocalAdapter.getBluetoothState());
     }
 
@@ -454,21 +329,12 @@
         if (text instanceof Spannable) {
             Spannable boldSpan = (Spannable) text;
             boldSpan.setSpan(
-                new TextAppearanceSpan(getActivity(), android.R.style.TextAppearance_Medium), 0,
-                briefText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+                    new TextAppearanceSpan(getActivity(), android.R.style.TextAppearance_Medium), 0,
+                    briefText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
         }
     }
 
     @VisibleForTesting
-    void updateMyDevicePreference(Preference myDevicePreference) {
-        final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
-
-        myDevicePreference.setTitle(getString(
-                R.string.bluetooth_footer_mac_message,
-                bidiFormatter.unicodeWrap(mLocalAdapter.getAddress())));
-    }
-
-    @VisibleForTesting
     void setLocalBluetoothAdapter(LocalBluetoothAdapter localAdapter) {
         mLocalAdapter = localAdapter;
     }
@@ -501,6 +367,7 @@
      */
     @Override
     void initDevicePreference(BluetoothDevicePreference preference) {
+        preference.setOrder(PAIRED_DEVICE_ORDER);
         CachedBluetoothDevice cachedDevice = preference.getCachedDevice();
         if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
             // Only paired device have an associated advanced settings screen
@@ -528,7 +395,8 @@
         List<PreferenceController> controllers = new ArrayList<>();
         mDeviceNamePrefController = new BluetoothDeviceNamePreferenceController(context,
                 this, getLifecycle());
-        mPairingPrefController = new BluetoothPairingPreferenceController(context, this);
+        mPairingPrefController = new BluetoothPairingPreferenceController(context, this,
+                (SettingsActivity) getActivity());
         controllers.add(mDeviceNamePrefController);
         controllers.add(mPairingPrefController);
 
@@ -593,23 +461,18 @@
                     data.key = DATA_KEY_REFERENCE;
                     result.add(data);
 
-                    // Add cached paired BT devices
-                    LocalBluetoothManager lbtm = Utils.getLocalBtManager(context);
-                    // LocalBluetoothManager.getInstance can return null if the device does not
-                    // support bluetooth (e.g. the emulator).
-                    if (lbtm != null) {
-                        Set<BluetoothDevice> bondedDevices =
-                                lbtm.getBluetoothAdapter().getBondedDevices();
-
-                        for (BluetoothDevice device : bondedDevices) {
-                            data = new SearchIndexableRaw(context);
-                            data.title = device.getName();
-                            data.screenTitle = res.getString(R.string.bluetooth_settings);
-                            data.enabled = enabled;
-                            result.add(data);
-                        }
-                    }
+                    // Removed paired bluetooth device indexing. See BluetoothSettingsObsolete.java.
                     return result;
                 }
+
+                @Override
+                public List<String> getNonIndexableKeys(Context context) {
+                    List<String> keys = super.getNonIndexableKeys(context);
+                    if (!FeatureFactory.getFactory(context).getBluetoothFeatureProvider(
+                            context).isPairingPageEnabled()) {
+                        keys.add(DATA_KEY_REFERENCE);
+                    }
+                    return keys;
+                }
             };
 }
diff --git a/src/com/android/settings/bluetooth/BluetoothSettingsObsolete.java b/src/com/android/settings/bluetooth/BluetoothSettingsObsolete.java
new file mode 100644
index 0000000..207d313
--- /dev/null
+++ b/src/com/android/settings/bluetooth/BluetoothSettingsObsolete.java
@@ -0,0 +1,625 @@
+/*
+ * Copyright (C) 2017 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.bluetooth;
+
+import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
+
+import android.app.Activity;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.res.Resources;
+import android.os.Bundle;
+import android.provider.Settings;
+import android.support.annotation.VisibleForTesting;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceCategory;
+import android.support.v7.preference.PreferenceGroup;
+import android.support.v7.preference.PreferenceScreen;
+import android.text.BidiFormatter;
+import android.text.Spannable;
+import android.text.style.TextAppearanceSpan;
+import android.util.Log;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+import android.view.View;
+import android.widget.TextView;
+
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.settings.LinkifyUtils;
+import com.android.settings.R;
+import com.android.settings.SettingsActivity;
+import com.android.settings.core.PreferenceController;
+import com.android.settings.dashboard.SummaryLoader;
+import com.android.settings.location.ScanningSettings;
+import com.android.settings.overlay.FeatureFactory;
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settings.search.Indexable;
+import com.android.settings.search.SearchIndexableRaw;
+import com.android.settings.widget.GearPreference;
+import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
+import com.android.settings.widget.SwitchBar;
+import com.android.settings.widget.SwitchBarController;
+import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.widget.FooterPreference;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * BluetoothSettingsObsolete is the Settings screen for Bluetooth configuration and
+ * connection management.
+ *
+ * This fragment stores old implementation of {@link BluetoothSettings} and is
+ * deprecated, please use {@link BluetoothSettings} instead.
+ */
+@Deprecated
+public class BluetoothSettingsObsolete extends DeviceListPreferenceObsoleteFragment
+        implements Indexable {
+    private static final String TAG = "BluetoothSettingsObsolete";
+
+    private static final int MENU_ID_SCAN = Menu.FIRST;
+    private static final int MENU_ID_SHOW_RECEIVED = Menu.FIRST + 1;
+
+    /* Private intent to show the list of received files */
+    private static final String BTOPP_ACTION_OPEN_RECEIVED_FILES =
+            "android.btopp.intent.action.OPEN_RECEIVED_FILES";
+    private static final String BTOPP_PACKAGE =
+            "com.android.bluetooth";
+
+    private static final String KEY_PAIRED_DEVICES = "paired_devices";
+
+    private static View mSettingsDialogView = null;
+
+    private BluetoothEnabler mBluetoothEnabler;
+
+    private PreferenceGroup mPairedDevicesCategory;
+    private PreferenceGroup mAvailableDevicesCategory;
+    private Preference mDeviceNamePreference;
+    private boolean mAvailableDevicesCategoryIsPresent;
+
+    private boolean mInitialScanStarted;
+    private boolean mInitiateDiscoverable;
+
+    private SwitchBar mSwitchBar;
+
+    private final IntentFilter mIntentFilter;
+    private BluetoothDeviceNamePreferenceController mDeviceNamePrefController;
+
+    // For Search
+    @VisibleForTesting
+    static final String DATA_KEY_REFERENCE = "main_toggle_bluetooth_obsolete";
+
+    // accessed from inner class (not private to avoid thunks)
+    FooterPreference mMyDevicePreference;
+
+    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            final String action = intent.getAction();
+            final int state =
+                    intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
+
+            if (state == BluetoothAdapter.STATE_ON) {
+                mInitiateDiscoverable = true;
+            }
+        }
+    };
+
+    public BluetoothSettingsObsolete() {
+        super(DISALLOW_CONFIG_BLUETOOTH);
+        mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
+    }
+
+    @Override
+    public int getMetricsCategory() {
+        return MetricsEvent.BLUETOOTH;
+    }
+
+    @Override
+    public void onActivityCreated(Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+        mInitialScanStarted = false;
+        mInitiateDiscoverable = true;
+
+        final SettingsActivity activity = (SettingsActivity) getActivity();
+        mSwitchBar = activity.getSwitchBar();
+
+        mBluetoothEnabler = new BluetoothEnabler(activity, new SwitchBarController(mSwitchBar),
+                mMetricsFeatureProvider, Utils.getLocalBtManager(activity),
+                MetricsEvent.ACTION_BLUETOOTH_TOGGLE);
+        mBluetoothEnabler.setupSwitchController();
+    }
+
+    @Override
+    public void onDestroyView() {
+        super.onDestroyView();
+
+        mBluetoothEnabler.teardownSwitchController();
+    }
+
+    @Override
+    void addPreferencesForActivity() {
+        final Context prefContext = getPrefContext();
+
+        mDeviceNamePreference = mDeviceNamePrefController.createBluetoothDeviceNamePreference(
+                getPreferenceScreen(), 1 /* order */);
+
+        mPairedDevicesCategory = new PreferenceCategory(prefContext);
+        mPairedDevicesCategory.setKey(KEY_PAIRED_DEVICES);
+        mPairedDevicesCategory.setOrder(2);
+        getPreferenceScreen().addPreference(mPairedDevicesCategory);
+
+        mAvailableDevicesCategory = new BluetoothProgressCategory(prefContext);
+        mAvailableDevicesCategory.setSelectable(false);
+        mAvailableDevicesCategory.setOrder(3);
+        getPreferenceScreen().addPreference(mAvailableDevicesCategory);
+
+        mMyDevicePreference = mFooterPreferenceMixin.createFooterPreference();
+        mMyDevicePreference.setSelectable(false);
+
+        setHasOptionsMenu(true);
+    }
+
+    @Override
+    public void onStart() {
+        // resume BluetoothEnabler before calling super.onStart() so we don't get
+        // any onDeviceAdded() callbacks before setting up view in updateContent()
+        if (mBluetoothEnabler != null) {
+            mBluetoothEnabler.resume(getActivity());
+        }
+        super.onStart();
+
+        mInitiateDiscoverable = true;
+
+        if (isUiRestricted()) {
+            setDeviceListGroup(getPreferenceScreen());
+            if (!isUiRestrictedByOnlyAdmin()) {
+                getEmptyTextView().setText(R.string.bluetooth_empty_list_user_restricted);
+            }
+            removeAllDevices();
+            return;
+        }
+
+        getActivity().registerReceiver(mReceiver, mIntentFilter);
+        if (mLocalAdapter != null) {
+            updateContent(mLocalAdapter.getBluetoothState());
+        }
+    }
+
+    @Override
+    public void onStop() {
+        super.onStop();
+        if (mBluetoothEnabler != null) {
+            mBluetoothEnabler.pause();
+        }
+
+        // Make the device only visible to connected devices.
+        mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
+
+        if (isUiRestricted()) {
+            return;
+        }
+
+        getActivity().unregisterReceiver(mReceiver);
+    }
+
+    @Override
+    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+        if (mLocalAdapter == null) return;
+        // If the user is not allowed to configure bluetooth, do not show the menu.
+        if (isUiRestricted()) return;
+
+        boolean bluetoothIsEnabled = mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON;
+        boolean isDiscovering = mLocalAdapter.isDiscovering();
+        int textId = isDiscovering ? R.string.bluetooth_searching_for_devices :
+                R.string.bluetooth_search_for_devices;
+        menu.add(Menu.NONE, MENU_ID_SCAN, 0, textId)
+                .setEnabled(bluetoothIsEnabled && !isDiscovering)
+                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
+        menu.add(Menu.NONE, MENU_ID_SHOW_RECEIVED, 0, R.string.bluetooth_show_received_files)
+                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
+        super.onCreateOptionsMenu(menu, inflater);
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        switch (item.getItemId()) {
+            case MENU_ID_SCAN:
+                if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
+                    mMetricsFeatureProvider.action(getActivity(),
+                            MetricsEvent.ACTION_BLUETOOTH_SCAN);
+                    startScanning();
+                }
+                return true;
+
+            case MENU_ID_SHOW_RECEIVED:
+                mMetricsFeatureProvider.action(getActivity(),
+                        MetricsEvent.ACTION_BLUETOOTH_FILES);
+                Intent intent = new Intent(BTOPP_ACTION_OPEN_RECEIVED_FILES);
+                intent.setPackage(BTOPP_PACKAGE);
+                getActivity().sendBroadcast(intent);
+                return true;
+        }
+        return super.onOptionsItemSelected(item);
+    }
+
+    private void startScanning() {
+        if (isUiRestricted()) {
+            return;
+        }
+
+        if (!mAvailableDevicesCategoryIsPresent) {
+            getPreferenceScreen().addPreference(mAvailableDevicesCategory);
+            mAvailableDevicesCategoryIsPresent = true;
+        }
+
+        if (mAvailableDevicesCategory != null) {
+            setDeviceListGroup(mAvailableDevicesCategory);
+            removeAllDevices();
+        }
+
+        mLocalManager.getCachedDeviceManager().clearNonBondedDevices();
+        mAvailableDevicesCategory.removeAll();
+        mInitialScanStarted = true;
+        mLocalAdapter.startScanning(true);
+    }
+
+    @Override
+    void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
+        mLocalAdapter.stopScanning();
+        super.onDevicePreferenceClick(btPreference);
+    }
+
+    private void addDeviceCategory(PreferenceGroup preferenceGroup, int titleId,
+            BluetoothDeviceFilter.Filter filter, boolean addCachedDevices) {
+        cacheRemoveAllPrefs(preferenceGroup);
+        preferenceGroup.setTitle(titleId);
+        setFilter(filter);
+        setDeviceListGroup(preferenceGroup);
+        if (addCachedDevices) {
+            addCachedDevices();
+        }
+        preferenceGroup.setEnabled(true);
+        removeCachedPrefs(preferenceGroup);
+    }
+
+    private void updateContent(int bluetoothState) {
+        final PreferenceScreen preferenceScreen = getPreferenceScreen();
+        int messageId = 0;
+
+        switch (bluetoothState) {
+            case BluetoothAdapter.STATE_ON:
+                mDevicePreferenceMap.clear();
+
+                if (isUiRestricted()) {
+                    messageId = R.string.bluetooth_empty_list_user_restricted;
+                    break;
+                }
+                getPreferenceScreen().removeAll();
+                getPreferenceScreen().addPreference(mDeviceNamePreference);
+                getPreferenceScreen().addPreference(mPairedDevicesCategory);
+                getPreferenceScreen().addPreference(mAvailableDevicesCategory);
+                getPreferenceScreen().addPreference(mMyDevicePreference);
+
+                // Paired devices category
+                addDeviceCategory(mPairedDevicesCategory,
+                        R.string.bluetooth_preference_paired_devices,
+                        BluetoothDeviceFilter.BONDED_DEVICE_FILTER, true);
+                int numberOfPairedDevices = mPairedDevicesCategory.getPreferenceCount();
+
+                if (isUiRestricted() || numberOfPairedDevices <= 0) {
+                    if (preferenceScreen.findPreference(KEY_PAIRED_DEVICES) != null) {
+                        preferenceScreen.removePreference(mPairedDevicesCategory);
+                    }
+                } else {
+                    if (preferenceScreen.findPreference(KEY_PAIRED_DEVICES) == null) {
+                        preferenceScreen.addPreference(mPairedDevicesCategory);
+                    }
+                }
+
+                // Available devices category
+                addDeviceCategory(mAvailableDevicesCategory,
+                        R.string.bluetooth_preference_found_devices,
+                        BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, mInitialScanStarted);
+
+                if (!mInitialScanStarted) {
+                    startScanning();
+                }
+
+                updateMyDevicePreference(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:
+                messageId = R.string.bluetooth_turning_off;
+                break;
+
+            case BluetoothAdapter.STATE_OFF:
+                setOffMessage();
+                if (isUiRestricted()) {
+                    messageId = R.string.bluetooth_empty_list_user_restricted;
+                }
+                break;
+
+            case BluetoothAdapter.STATE_TURNING_ON:
+                messageId = R.string.bluetooth_turning_on;
+                mInitialScanStarted = false;
+                break;
+        }
+
+        setDeviceListGroup(preferenceScreen);
+        removeAllDevices();
+        if (messageId != 0) {
+            getEmptyTextView().setText(messageId);
+        }
+        if (!isUiRestricted()) {
+            getActivity().invalidateOptionsMenu();
+        }
+    }
+
+    private void setOffMessage() {
+        final TextView emptyView = getEmptyTextView();
+        if (emptyView == null) {
+            return;
+        }
+        final CharSequence briefText = getText(R.string.bluetooth_empty_list_bluetooth_off);
+
+        final ContentResolver resolver = getActivity().getContentResolver();
+        final boolean bleScanningMode = Settings.Global.getInt(
+                resolver, Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 0) == 1;
+
+        if (!bleScanningMode) {
+            // Show only the brief text if the scanning mode has been turned off.
+            emptyView.setText(briefText, TextView.BufferType.SPANNABLE);
+        } else {
+            final StringBuilder contentBuilder = new StringBuilder();
+            contentBuilder.append(briefText);
+            contentBuilder.append("\n\n");
+            contentBuilder.append(getText(R.string.ble_scan_notify_text));
+            LinkifyUtils.linkify(emptyView, contentBuilder, new LinkifyUtils.OnClickListener() {
+                @Override
+                public void onClick() {
+                    final SettingsActivity activity =
+                            (SettingsActivity) BluetoothSettingsObsolete.this.getActivity();
+                    activity.startPreferencePanel(BluetoothSettingsObsolete.this,
+                            ScanningSettings.class.getName(), null,
+                            R.string.location_scanning_screen_title, null, null, 0);
+                }
+            });
+        }
+        getPreferenceScreen().removeAll();
+        setTextSpan(emptyView.getText(), briefText);
+    }
+
+    @Override
+    public void onBluetoothStateChanged(int bluetoothState) {
+        super.onBluetoothStateChanged(bluetoothState);
+        // If BT is turned off/on staying in the same BT Settings screen
+        // discoverability to be set again
+        if (BluetoothAdapter.STATE_ON == bluetoothState) {
+            mInitiateDiscoverable = true;
+        }
+        updateContent(bluetoothState);
+    }
+
+    @Override
+    public void onScanningStateChanged(boolean started) {
+        super.onScanningStateChanged(started);
+        // Update options' enabled state
+        if (getActivity() != null) {
+            getActivity().invalidateOptionsMenu();
+        }
+    }
+
+    @Override
+    public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
+        setDeviceListGroup(getPreferenceScreen());
+        removeAllDevices();
+        updateContent(mLocalAdapter.getBluetoothState());
+    }
+
+    @VisibleForTesting
+    void setTextSpan(CharSequence text, CharSequence briefText) {
+        if (text instanceof Spannable) {
+            Spannable boldSpan = (Spannable) text;
+            boldSpan.setSpan(
+                    new TextAppearanceSpan(getActivity(), android.R.style.TextAppearance_Medium), 0,
+                    briefText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+        }
+    }
+
+    @VisibleForTesting
+    void updateMyDevicePreference(Preference myDevicePreference) {
+        final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
+
+        myDevicePreference.setTitle(getString(
+                R.string.bluetooth_footer_mac_message,
+                bidiFormatter.unicodeWrap(mLocalAdapter.getAddress())));
+    }
+
+    @VisibleForTesting
+    void setLocalBluetoothAdapter(LocalBluetoothAdapter localAdapter) {
+        mLocalAdapter = localAdapter;
+    }
+
+    private final GearPreference.OnGearClickListener mDeviceProfilesListener = pref -> {
+        // User clicked on advanced options icon for a device in the list
+        if (!(pref instanceof BluetoothDevicePreference)) {
+            Log.w(TAG, "onClick() called for other View: " + pref);
+            return;
+        }
+        final CachedBluetoothDevice device =
+                ((BluetoothDevicePreference) pref).getBluetoothDevice();
+        if (device == null) {
+            Log.w(TAG, "No BT device attached with this pref: " + pref);
+            return;
+        }
+        final Bundle args = new Bundle();
+        args.putString(DeviceProfilesSettings.ARG_DEVICE_ADDRESS,
+                device.getDevice().getAddress());
+        final DeviceProfilesSettings profileSettings = new DeviceProfilesSettings();
+        profileSettings.setArguments(args);
+        profileSettings.show(getFragmentManager(),
+                DeviceProfilesSettings.class.getSimpleName());
+    };
+
+    /**
+     * Add a listener, which enables the advanced settings icon.
+     *
+     * @param preference the newly added preference
+     */
+    @Override
+    void initDevicePreference(BluetoothDevicePreference preference) {
+        CachedBluetoothDevice cachedDevice = preference.getCachedDevice();
+        if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
+            // Only paired device have an associated advanced settings screen
+            preference.setOnGearClickListener(mDeviceProfilesListener);
+        }
+    }
+
+    @Override
+    protected int getHelpResource() {
+        return R.string.help_url_bluetooth;
+    }
+
+    @Override
+    protected String getLogTag() {
+        return TAG;
+    }
+
+    @Override
+    protected int getPreferenceScreenResId() {
+        return R.xml.bluetooth_settings_obsolete;
+    }
+
+    @Override
+    protected List<PreferenceController> getPreferenceControllers(Context context) {
+        List<PreferenceController> controllers = new ArrayList<>();
+        mDeviceNamePrefController = new BluetoothDeviceNamePreferenceController(context,
+                this, getLifecycle());
+        controllers.add(mDeviceNamePrefController);
+
+        return controllers;
+    }
+
+    @VisibleForTesting
+    static class SummaryProvider implements SummaryLoader.SummaryProvider, OnSummaryChangeListener {
+
+        private final LocalBluetoothManager mBluetoothManager;
+        private final Context mContext;
+        private final SummaryLoader mSummaryLoader;
+
+        @VisibleForTesting
+        BluetoothSummaryUpdater mSummaryUpdater;
+
+        public SummaryProvider(Context context, SummaryLoader summaryLoader,
+                LocalBluetoothManager bluetoothManager) {
+            mBluetoothManager = bluetoothManager;
+            mContext = context;
+            mSummaryLoader = summaryLoader;
+            mSummaryUpdater = new BluetoothSummaryUpdater(mContext, this, mBluetoothManager);
+        }
+
+        @Override
+        public void setListening(boolean listening) {
+            mSummaryUpdater.register(listening);
+        }
+
+        @Override
+        public void onSummaryChanged(String summary) {
+            if (mSummaryLoader != null) {
+                mSummaryLoader.setSummary(this, summary);
+            }
+        }
+    }
+
+    public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
+            = new SummaryLoader.SummaryProviderFactory() {
+        @Override
+        public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
+                SummaryLoader summaryLoader) {
+
+            return new SummaryProvider(activity, summaryLoader, Utils.getLocalBtManager(activity));
+        }
+    };
+
+    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+            new BaseSearchIndexProvider() {
+                @Override
+                public List<SearchIndexableRaw> getRawDataToIndex(Context context,
+                        boolean enabled) {
+
+                    final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
+
+                    final Resources res = context.getResources();
+
+                    // Add fragment title
+                    SearchIndexableRaw data = new SearchIndexableRaw(context);
+                    data.title = res.getString(R.string.bluetooth_settings);
+                    data.screenTitle = res.getString(R.string.bluetooth_settings);
+                    data.key = DATA_KEY_REFERENCE;
+                    result.add(data);
+
+                    // Add cached paired BT devices
+                    LocalBluetoothManager lbtm = Utils.getLocalBtManager(context);
+                    // LocalBluetoothManager.getInstance can return null if the device does not
+                    // support bluetooth (e.g. the emulator).
+                    if (lbtm != null) {
+                        Set<BluetoothDevice> bondedDevices =
+                                lbtm.getBluetoothAdapter().getBondedDevices();
+
+                        for (BluetoothDevice device : bondedDevices) {
+                            data = new SearchIndexableRaw(context);
+                            data.title = device.getName();
+                            data.screenTitle = res.getString(R.string.bluetooth_settings);
+                            data.enabled = enabled;
+                            result.add(data);
+                        }
+                    }
+                    return result;
+                }
+
+                @Override
+                public List<String> getNonIndexableKeys(Context context) {
+                    List<String> keys = super.getNonIndexableKeys(context);
+                    if (FeatureFactory.getFactory(context).getBluetoothFeatureProvider(
+                            context).isPairingPageEnabled()) {
+                        keys.add(DATA_KEY_REFERENCE);
+                    }
+                    return keys;
+                }
+
+            };
+}
diff --git a/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java b/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java
index 4034316..714704e 100644
--- a/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java
+++ b/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java
@@ -19,12 +19,15 @@
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.os.Bundle;
+import android.support.annotation.VisibleForTesting;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceCategory;
 import android.support.v7.preference.PreferenceGroup;
+import android.text.BidiFormatter;
 import android.util.Log;
 
 import com.android.settings.dashboard.RestrictedDashboardFragment;
+import com.android.settings.R;
 import com.android.settingslib.bluetooth.BluetoothCallback;
 import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -41,12 +44,12 @@
  * @see BluetoothSettings
  * @see DevicePickerFragment
  */
+// TODO: Refactor this fragment
 public abstract class DeviceListPreferenceFragment extends
         RestrictedDashboardFragment implements BluetoothCallback {
 
     private static final String TAG = "DeviceListPreferenceFragment";
 
-    private static final String KEY_BT_DEVICE_LIST = "bt_device_list";
     private static final String KEY_BT_SCAN = "bt_scan";
 
     private BluetoothDeviceFilter.Filter mFilter;
@@ -56,7 +59,8 @@
     LocalBluetoothAdapter mLocalAdapter;
     LocalBluetoothManager mLocalManager;
 
-    private PreferenceGroup mDeviceListGroup;
+    @VisibleForTesting
+    PreferenceGroup mDeviceListGroup;
 
     final WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
             new WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference>();
@@ -85,17 +89,13 @@
         }
         mLocalAdapter = mLocalManager.getBluetoothAdapter();
 
-        addPreferencesForActivity();
+        initPreferencesFromPreferenceScreen();
 
-        mDeviceListGroup = (PreferenceCategory) findPreference(KEY_BT_DEVICE_LIST);
+        mDeviceListGroup = (PreferenceCategory) findPreference(getDeviceListKey());
     }
 
-    void setDeviceListGroup(PreferenceGroup preferenceGroup) {
-        mDeviceListGroup = preferenceGroup;
-    }
-
-    /** Add preferences from the subclass. */
-    abstract void addPreferencesForActivity();
+    /** find and update preference that already existed in preference screen */
+    abstract void initPreferencesFromPreferenceScreen();
 
     @Override
     public void onStart() {
@@ -104,8 +104,6 @@
 
         mLocalManager.setForegroundActivity(getActivity());
         mLocalManager.getEventManager().registerCallback(this);
-
-        updateProgressUi(mLocalAdapter.isDiscovering());
     }
 
     @Override
@@ -121,7 +119,6 @@
     }
 
     void removeAllDevices() {
-        mLocalAdapter.stopScanning();
         mDevicePreferenceMap.clear();
         mDeviceListGroup.removeAll();
     }
@@ -156,6 +153,7 @@
         btPreference.onClicked();
     }
 
+    @Override
     public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
         if (mDevicePreferenceMap.get(cachedDevice) != null) {
             return;
@@ -201,6 +199,16 @@
         // Does nothing by default
     }
 
+    @VisibleForTesting
+    void updateFooterPreference(Preference myDevicePreference) {
+        final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
+
+        myDevicePreference.setTitle(getString(
+                R.string.bluetooth_footer_mac_message,
+                bidiFormatter.unicodeWrap(mLocalAdapter.getAddress())));
+    }
+
+    @Override
     public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
         BluetoothDevicePreference preference = mDevicePreferenceMap.remove(cachedDevice);
         if (preference != null) {
@@ -208,21 +216,39 @@
         }
     }
 
-    public void onScanningStateChanged(boolean started) {
-        updateProgressUi(started);
-    }
+    @Override
+    public void onScanningStateChanged(boolean started) {}
 
-    private void updateProgressUi(boolean start) {
-        if (mDeviceListGroup instanceof BluetoothProgressCategory) {
-            ((BluetoothProgressCategory) mDeviceListGroup).setProgress(start);
-        }
-    }
+    @Override
+    public void onBluetoothStateChanged(int bluetoothState) {}
 
-    public void onBluetoothStateChanged(int bluetoothState) {
-        if (bluetoothState == BluetoothAdapter.STATE_OFF) {
-            updateProgressUi(false);
+    /**
+     * Add bluetooth device preferences to {@code preferenceGroup} which satisfy the {@code filter}
+     *
+     * This method will also (1) set the title for {@code preferenceGroup} and (2) change the
+     * default preferenceGroup and filter
+     * @param preferenceGroup
+     * @param titleId
+     * @param filter
+     * @param addCachedDevices
+     */
+    public void addDeviceCategory(PreferenceGroup preferenceGroup, int titleId,
+            BluetoothDeviceFilter.Filter filter, boolean addCachedDevices) {
+        cacheRemoveAllPrefs(preferenceGroup);
+        preferenceGroup.setTitle(titleId);
+        mDeviceListGroup = preferenceGroup;
+        setFilter(filter);
+        if (addCachedDevices) {
+            addCachedDevices();
         }
+        preferenceGroup.setEnabled(true);
+        removeCachedPrefs(preferenceGroup);
     }
 
     public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) { }
+
+    /**
+     * Return the key of the {@link PreferenceGroup} that contains the bluetooth devices
+     */
+    public abstract String getDeviceListKey();
 }
diff --git a/src/com/android/settings/bluetooth/DeviceListPreferenceObsoleteFragment.java b/src/com/android/settings/bluetooth/DeviceListPreferenceObsoleteFragment.java
new file mode 100644
index 0000000..84d8558
--- /dev/null
+++ b/src/com/android/settings/bluetooth/DeviceListPreferenceObsoleteFragment.java
@@ -0,0 +1,232 @@
+/*
+ * Copyright (C) 2017 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.bluetooth;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.os.Bundle;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceCategory;
+import android.support.v7.preference.PreferenceGroup;
+import android.util.Log;
+
+import com.android.settings.dashboard.RestrictedDashboardFragment;
+import com.android.settingslib.bluetooth.BluetoothCallback;
+import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+
+import java.util.Collection;
+import java.util.WeakHashMap;
+
+/**
+ * Parent class for settings fragments that contain a list of Bluetooth
+ * devices.
+ *
+ * This fragment stores old implementation of {@link DeviceListPreferenceFragment} and is
+ * deprecated, please use {@link DeviceListPreferenceFragment} instead.
+ *
+ * @see BluetoothSettingsObsolete
+ * @see DevicePickerFragment
+ */
+@Deprecated
+public abstract class DeviceListPreferenceObsoleteFragment extends
+        RestrictedDashboardFragment implements BluetoothCallback {
+
+    private static final String TAG = "DeviceListPreferenceFragment";
+
+    private static final String KEY_BT_DEVICE_LIST = "bt_device_list";
+    private static final String KEY_BT_SCAN = "bt_scan";
+
+    private BluetoothDeviceFilter.Filter mFilter;
+
+    BluetoothDevice mSelectedDevice;
+
+    LocalBluetoothAdapter mLocalAdapter;
+    LocalBluetoothManager mLocalManager;
+
+    private PreferenceGroup mDeviceListGroup;
+
+    final WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
+            new WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference>();
+
+    DeviceListPreferenceObsoleteFragment(String restrictedKey) {
+        super(restrictedKey);
+        mFilter = BluetoothDeviceFilter.ALL_FILTER;
+    }
+
+    final void setFilter(BluetoothDeviceFilter.Filter filter) {
+        mFilter = filter;
+    }
+
+    final void setFilter(int filterType) {
+        mFilter = BluetoothDeviceFilter.getFilter(filterType);
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        mLocalManager = Utils.getLocalBtManager(getActivity());
+        if (mLocalManager == null) {
+            Log.e(TAG, "Bluetooth is not supported on this device");
+            return;
+        }
+        mLocalAdapter = mLocalManager.getBluetoothAdapter();
+
+        addPreferencesForActivity();
+
+        mDeviceListGroup = (PreferenceCategory) findPreference(KEY_BT_DEVICE_LIST);
+    }
+
+    void setDeviceListGroup(PreferenceGroup preferenceGroup) {
+        mDeviceListGroup = preferenceGroup;
+    }
+
+    /** Add preferences from the subclass. */
+    abstract void addPreferencesForActivity();
+
+    @Override
+    public void onStart() {
+        super.onStart();
+        if (mLocalManager == null || isUiRestricted()) return;
+
+        mLocalManager.setForegroundActivity(getActivity());
+        mLocalManager.getEventManager().registerCallback(this);
+
+        updateProgressUi(mLocalAdapter.isDiscovering());
+    }
+
+    @Override
+    public void onStop() {
+        super.onStop();
+        if (mLocalManager == null || isUiRestricted()) {
+            return;
+        }
+
+        removeAllDevices();
+        mLocalManager.setForegroundActivity(null);
+        mLocalManager.getEventManager().unregisterCallback(this);
+    }
+
+    void removeAllDevices() {
+        mLocalAdapter.stopScanning();
+        mDevicePreferenceMap.clear();
+        mDeviceListGroup.removeAll();
+    }
+
+    void addCachedDevices() {
+        Collection<CachedBluetoothDevice> cachedDevices =
+                mLocalManager.getCachedDeviceManager().getCachedDevicesCopy();
+        for (CachedBluetoothDevice cachedDevice : cachedDevices) {
+            onDeviceAdded(cachedDevice);
+        }
+    }
+
+    @Override
+    public boolean onPreferenceTreeClick(Preference preference) {
+        if (KEY_BT_SCAN.equals(preference.getKey())) {
+            mLocalAdapter.startScanning(true);
+            return true;
+        }
+
+        if (preference instanceof BluetoothDevicePreference) {
+            BluetoothDevicePreference btPreference = (BluetoothDevicePreference) preference;
+            CachedBluetoothDevice device = btPreference.getCachedDevice();
+            mSelectedDevice = device.getDevice();
+            onDevicePreferenceClick(btPreference);
+            return true;
+        }
+
+        return super.onPreferenceTreeClick(preference);
+    }
+
+    void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
+        btPreference.onClicked();
+    }
+
+    public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
+        if (mDevicePreferenceMap.get(cachedDevice) != null) {
+            return;
+        }
+
+        // Prevent updates while the list shows one of the state messages
+        if (mLocalAdapter.getBluetoothState() != BluetoothAdapter.STATE_ON) return;
+
+        if (mFilter.matches(cachedDevice.getDevice())) {
+            createDevicePreference(cachedDevice);
+        }
+    }
+
+    void createDevicePreference(CachedBluetoothDevice cachedDevice) {
+        if (mDeviceListGroup == null) {
+            Log.w(TAG, "Trying to create a device preference before the list group/category "
+                    + "exists!");
+            return;
+        }
+
+        String key = cachedDevice.getDevice().getAddress();
+        BluetoothDevicePreference preference = (BluetoothDevicePreference) getCachedPreference(key);
+
+        if (preference == null) {
+            preference = new BluetoothDevicePreference(getPrefContext(), cachedDevice);
+            preference.setKey(key);
+            mDeviceListGroup.addPreference(preference);
+        } else {
+            // Tell the preference it is being re-used in case there is new info in the
+            // cached device.
+            preference.rebind();
+        }
+
+        initDevicePreference(preference);
+        mDevicePreferenceMap.put(cachedDevice, preference);
+    }
+
+    /**
+     * Overridden in {@link BluetoothSettings} to add a listener.
+     * @param preference the newly added preference
+     */
+    void initDevicePreference(BluetoothDevicePreference preference) {
+        // Does nothing by default
+    }
+
+    public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
+        BluetoothDevicePreference preference = mDevicePreferenceMap.remove(cachedDevice);
+        if (preference != null) {
+            mDeviceListGroup.removePreference(preference);
+        }
+    }
+
+    public void onScanningStateChanged(boolean started) {
+        updateProgressUi(started);
+    }
+
+    private void updateProgressUi(boolean start) {
+        if (mDeviceListGroup instanceof BluetoothProgressCategory) {
+            ((BluetoothProgressCategory) mDeviceListGroup).setProgress(start);
+        }
+    }
+
+    public void onBluetoothStateChanged(int bluetoothState) {
+        if (bluetoothState == BluetoothAdapter.STATE_OFF) {
+            updateProgressUi(false);
+        }
+    }
+
+    public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) { }
+}
diff --git a/src/com/android/settings/bluetooth/DevicePickerFragment.java b/src/com/android/settings/bluetooth/DevicePickerFragment.java
index 8bf8202..0cf13a3 100644
--- a/src/com/android/settings/bluetooth/DevicePickerFragment.java
+++ b/src/com/android/settings/bluetooth/DevicePickerFragment.java
@@ -42,6 +42,7 @@
  */
 public final class DevicePickerFragment extends DeviceListPreferenceFragment {
     private static final int MENU_ID_REFRESH = Menu.FIRST;
+    private static final String KEY_BT_DEVICE_LIST = "bt_device_list";
     private static final String TAG = "DevicePickerFragment";
 
     public DevicePickerFragment() {
@@ -54,7 +55,7 @@
     private boolean mStartScanOnStart;
 
     @Override
-    void addPreferencesForActivity() {
+    void initPreferencesFromPreferenceScreen() {
         Intent intent = getActivity().getIntent();
         mNeedAuth = intent.getBooleanExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
         setFilter(intent.getIntExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
@@ -167,6 +168,11 @@
         return null;
     }
 
+    @Override
+    public String getDeviceListKey() {
+        return KEY_BT_DEVICE_LIST;
+    }
+
     private void sendDevicePickedIntent(BluetoothDevice device) {
         Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
diff --git a/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java b/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
index 6b172c2..27492ce 100644
--- a/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
+++ b/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
@@ -23,6 +23,7 @@
 
 import com.android.internal.logging.nano.MetricsProto;
 import com.android.settings.R;
+import com.android.settings.SettingsActivity;
 import com.android.settings.bluetooth.BluetoothMasterSwitchPreferenceController;
 import com.android.settings.bluetooth.Utils;
 import com.android.settings.core.PreferenceController;
@@ -71,7 +72,8 @@
         controllers.add(mUsbPrefController);
         final BluetoothMasterSwitchPreferenceController bluetoothPreferenceController =
                 new BluetoothMasterSwitchPreferenceController(
-                        context, Utils.getLocalBtManager(context));
+                        context, Utils.getLocalBtManager(context), this,
+                        (SettingsActivity) getActivity());
         lifecycle.addObserver(bluetoothPreferenceController);
         controllers.add(bluetoothPreferenceController);
         return controllers;
diff --git a/src/com/android/settings/core/instrumentation/SharedPreferencesLogger.java b/src/com/android/settings/core/instrumentation/SharedPreferencesLogger.java
index d1c7c7a..51102a6 100644
--- a/src/com/android/settings/core/instrumentation/SharedPreferencesLogger.java
+++ b/src/com/android/settings/core/instrumentation/SharedPreferencesLogger.java
@@ -21,6 +21,7 @@
 import android.content.pm.PackageManager;
 import android.os.AsyncTask;
 import android.text.TextUtils;
+import android.util.Log;
 import android.util.Pair;
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -32,6 +33,8 @@
 
 public class SharedPreferencesLogger implements SharedPreferences {
 
+    private static final String LOG_TAG = "SharedPreferencesLogger";
+
     private final String mTag;
     private final Context mContext;
     private final MetricsFeatureProvider mMetricsFeature;
@@ -99,11 +102,11 @@
             OnSharedPreferenceChangeListener listener) {
     }
 
-    private void logValue(String key, String value) {
+    private void logValue(String key, Object value) {
         logValue(key, value, false /* forceLog */);
     }
 
-    private void logValue(String key, String value, boolean forceLog) {
+    private void logValue(String key, Object value, boolean forceLog) {
         final String prefKey = mTag + "/" + key;
         if (!forceLog && !mPreferenceKeySet.contains(prefKey)) {
             // Pref key doesn't exist in set, this is initial display so we skip metrics but
@@ -114,10 +117,32 @@
         // TODO: Remove count logging to save some resource.
         mMetricsFeature.count(mContext, prefKey + "|" + value, 1);
 
-        // Pref key exists in set, log it's change in metrics.
-        mMetricsFeature.action(mContext, MetricsEvent.ACTION_SETTINGS_PREFERENCE_CHANGE,
-                Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_NAME, prefKey),
-                Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_VALUE, value));
+        final Pair<Integer, Object> valueData;
+        if (value instanceof Long) {
+            valueData = Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE,
+                    value);
+        } else if (value instanceof Integer) {
+            valueData = Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE,
+                    ((Integer) value).longValue());
+        } else if (value instanceof Boolean) {
+            valueData = Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE,
+                    (Boolean) value ? 1L : 0L);
+        } else if (value instanceof Float) {
+            valueData = Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_FLOAT_VALUE,
+                    value);
+        } else if (value instanceof String){
+            valueData = Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_VALUE,
+                    value);
+        } else {
+            Log.w(LOG_TAG, "Tried to log unloggable object"  + value);
+            valueData = null;
+        }
+        if (valueData != null) {
+            // Pref key exists in set, log it's change in metrics.
+            mMetricsFeature.action(mContext, MetricsEvent.ACTION_SETTINGS_PREFERENCE_CHANGE,
+                    Pair.create(MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_NAME, prefKey),
+                    valueData);
+        }
     }
 
     private void logPackageName(String key, String value) {
@@ -173,25 +198,25 @@
 
         @Override
         public Editor putInt(String key, int value) {
-            logValue(key, String.valueOf(value));
+            logValue(key, value);
             return this;
         }
 
         @Override
         public Editor putLong(String key, long value) {
-            logValue(key, String.valueOf(value));
+            logValue(key, value);
             return this;
         }
 
         @Override
         public Editor putFloat(String key, float value) {
-            logValue(key, String.valueOf(value));
+            logValue(key, value);
             return this;
         }
 
         @Override
         public Editor putBoolean(String key, boolean value) {
-            logValue(key, String.valueOf(value));
+            logValue(key, value);
             return this;
         }
 
diff --git a/src/com/android/settings/core/lifecycle/events/OnAttach.java b/src/com/android/settings/core/lifecycle/events/OnAttach.java
deleted file mode 100644
index 74fbe2f..0000000
--- a/src/com/android/settings/core/lifecycle/events/OnAttach.java
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * Copyright (C) 2016 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.core.lifecycle.events;
-
-import android.content.Context;
-
-public interface OnAttach {
-    void onAttach(Context context);
-}
diff --git a/src/com/android/settings/core/lifecycle/events/OnCreate.java b/src/com/android/settings/core/lifecycle/events/OnCreate.java
deleted file mode 100644
index 9c97cf5..0000000
--- a/src/com/android/settings/core/lifecycle/events/OnCreate.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright (C) 2016 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.core.lifecycle.events;
-
-
-import android.os.Bundle;
-
-public interface OnCreate {
-    void onCreate(Bundle savedInstanceState);
-}
diff --git a/src/com/android/settings/core/lifecycle/events/OnCreateOptionsMenu.java b/src/com/android/settings/core/lifecycle/events/OnCreateOptionsMenu.java
deleted file mode 100644
index 4c794ba..0000000
--- a/src/com/android/settings/core/lifecycle/events/OnCreateOptionsMenu.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright (C) 2017 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.core.lifecycle.events;
-
-import android.view.Menu;
-import android.view.MenuInflater;
-
-public interface OnCreateOptionsMenu {
-    void onCreateOptionsMenu(Menu menu, MenuInflater inflater);
-}
diff --git a/src/com/android/settings/core/lifecycle/events/OnDestroy.java b/src/com/android/settings/core/lifecycle/events/OnDestroy.java
deleted file mode 100644
index 5499de8..0000000
--- a/src/com/android/settings/core/lifecycle/events/OnDestroy.java
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (C) 2016 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.core.lifecycle.events;
-
-public interface OnDestroy {
-    void onDestroy();
-}
diff --git a/src/com/android/settings/core/lifecycle/events/OnOptionsItemSelected.java b/src/com/android/settings/core/lifecycle/events/OnOptionsItemSelected.java
deleted file mode 100644
index b34b407..0000000
--- a/src/com/android/settings/core/lifecycle/events/OnOptionsItemSelected.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright (C) 2017 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.core.lifecycle.events;
-
-import android.view.MenuItem;
-
-public interface OnOptionsItemSelected {
-    boolean onOptionsItemSelected(MenuItem menuItem);
-}
diff --git a/src/com/android/settings/core/lifecycle/events/OnPause.java b/src/com/android/settings/core/lifecycle/events/OnPause.java
deleted file mode 100644
index 155af00..0000000
--- a/src/com/android/settings/core/lifecycle/events/OnPause.java
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (C) 2016 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.core.lifecycle.events;
-
-public interface OnPause {
-    void onPause();
-}
diff --git a/src/com/android/settings/core/lifecycle/events/OnPrepareOptionsMenu.java b/src/com/android/settings/core/lifecycle/events/OnPrepareOptionsMenu.java
deleted file mode 100644
index d642807..0000000
--- a/src/com/android/settings/core/lifecycle/events/OnPrepareOptionsMenu.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright (C) 2017 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.core.lifecycle.events;
-
-import android.view.Menu;
-import android.view.MenuInflater;
-
-public interface OnPrepareOptionsMenu {
-    void onPrepareOptionsMenu(Menu menu);
-}
diff --git a/src/com/android/settings/core/lifecycle/events/OnResume.java b/src/com/android/settings/core/lifecycle/events/OnResume.java
deleted file mode 100644
index 30ce42b..0000000
--- a/src/com/android/settings/core/lifecycle/events/OnResume.java
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (C) 2016 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.core.lifecycle.events;
-
-public interface OnResume {
-    void onResume();
-}
diff --git a/src/com/android/settings/core/lifecycle/events/OnSaveInstanceState.java b/src/com/android/settings/core/lifecycle/events/OnSaveInstanceState.java
deleted file mode 100644
index fab4041..0000000
--- a/src/com/android/settings/core/lifecycle/events/OnSaveInstanceState.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright (C) 2016 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.core.lifecycle.events;
-
-import android.os.Bundle;
-
-
-public interface OnSaveInstanceState {
-    void onSaveInstanceState(Bundle outState);
-}
diff --git a/src/com/android/settings/core/lifecycle/events/OnStart.java b/src/com/android/settings/core/lifecycle/events/OnStart.java
deleted file mode 100644
index 3b4e6cc..0000000
--- a/src/com/android/settings/core/lifecycle/events/OnStart.java
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright (C) 2016 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.core.lifecycle.events;
-
-public interface OnStart {
-
-    void onStart();
-}
diff --git a/src/com/android/settings/core/lifecycle/events/OnStop.java b/src/com/android/settings/core/lifecycle/events/OnStop.java
deleted file mode 100644
index 8c19b87..0000000
--- a/src/com/android/settings/core/lifecycle/events/OnStop.java
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * Copyright (C) 2016 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.core.lifecycle.events;
-
-public interface OnStop {
-
-    void onStop();
-}
diff --git a/src/com/android/settings/core/lifecycle/events/SetPreferenceScreen.java b/src/com/android/settings/core/lifecycle/events/SetPreferenceScreen.java
deleted file mode 100644
index d206ed3..0000000
--- a/src/com/android/settings/core/lifecycle/events/SetPreferenceScreen.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright (C) 2016 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.core.lifecycle.events;
-
-import android.support.v7.preference.PreferenceScreen;
-
-public interface SetPreferenceScreen {
-
-    void setPreferenceScreen(PreferenceScreen preferenceScreen);
-}
diff --git a/src/com/android/settings/datausage/AppDataUsage.java b/src/com/android/settings/datausage/AppDataUsage.java
index a47e135..af6306d 100644
--- a/src/com/android/settings/datausage/AppDataUsage.java
+++ b/src/com/android/settings/datausage/AppDataUsage.java
@@ -327,6 +327,7 @@
         final Activity activity = getActivity();
         final Preference pref = EntityHeaderController
                 .newInstance(activity, this, null /* header */)
+                .setRecyclerView(getListView(), getLifecycle())
                 .setButtonActions(showInfoButton
                                 ? EntityHeaderController.ActionType.ACTION_APP_INFO
                                 : EntityHeaderController.ActionType.ACTION_NONE,
diff --git a/src/com/android/settings/development/DevelopmentSettings.java b/src/com/android/settings/development/DevelopmentSettings.java
index b4dda75..6b33a5b 100644
--- a/src/com/android/settings/development/DevelopmentSettings.java
+++ b/src/com/android/settings/development/DevelopmentSettings.java
@@ -226,6 +226,8 @@
 
     private static final String SHOW_ALL_ANRS_KEY = "show_all_anrs";
 
+    private static final String SHOW_NOTIFICATION_CHANNEL_WARNINGS_KEY = "show_notification_channel_warnings";
+
     private static final String TERMINAL_APP_PACKAGE = "com.android.terminal";
 
     private static final String KEY_CONVERT_FBE = "convert_to_file_encryption";
@@ -325,6 +327,8 @@
 
     private SwitchPreference mShowAllANRs;
 
+    private SwitchPreference mShowNotificationChannelWarnings;
+
     private ColorModePreference mColorModePreference;
 
     private SwitchPreference mForceResizable;
@@ -520,6 +524,11 @@
         mAllPrefs.add(mShowAllANRs);
         mResetSwitchPrefs.add(mShowAllANRs);
 
+        mShowNotificationChannelWarnings = (SwitchPreference) findPreference(
+                SHOW_NOTIFICATION_CHANNEL_WARNINGS_KEY);
+        mAllPrefs.add(mShowNotificationChannelWarnings);
+        mResetSwitchPrefs.add(mShowNotificationChannelWarnings);
+
         Preference hdcpChecking = findPreference(HDCP_CHECKING_KEY);
         if (hdcpChecking != null) {
             mAllPrefs.add(hdcpChecking);
@@ -785,6 +794,7 @@
         updateImmediatelyDestroyActivitiesOptions();
         updateAppProcessLimitOptions();
         updateShowAllANRsOptions();
+        updateShowNotificationChannelWarningsOptions();
         mVerifyAppsOverUsbController.updatePreference();
         updateOtaDisableAutomaticUpdateOptions();
         updateBugreportOptions();
@@ -2264,6 +2274,19 @@
                 getActivity().getContentResolver(), Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0);
     }
 
+    private void writeShowNotificationChannelWarningsOptions() {
+        Settings.Global.putInt(getActivity().getContentResolver(),
+                Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS,
+                mShowNotificationChannelWarnings.isChecked() ? 1 : 0);
+    }
+
+    private void updateShowNotificationChannelWarningsOptions() {
+        final int defaultWarningEnabled = Build.IS_DEBUGGABLE ? 1 : 0;
+        updateSwitchPreference(mShowNotificationChannelWarnings, Settings.Global.getInt(
+                getActivity().getContentResolver(),
+                Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, defaultWarningEnabled) != 0);
+    }
+
     private void confirmEnableOemUnlock() {
         DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
             @Override
@@ -2450,6 +2473,8 @@
             writeImmediatelyDestroyActivitiesOptions();
         } else if (preference == mShowAllANRs) {
             writeShowAllANRsOptions();
+        } else if (preference == mShowNotificationChannelWarnings) {
+            writeShowNotificationChannelWarningsOptions();
         } else if (preference == mForceHardwareUi) {
             writeHardwareUiOptions();
         } else if (preference == mForceMsaa) {
diff --git a/src/com/android/settings/display/ThemePreferenceController.java b/src/com/android/settings/display/ThemePreferenceController.java
index 99a19f8..c38ed28 100644
--- a/src/com/android/settings/display/ThemePreferenceController.java
+++ b/src/com/android/settings/display/ThemePreferenceController.java
@@ -90,11 +90,20 @@
         pref.setEntries(labels);
         pref.setEntryValues(pkgs);
         String theme = getCurrentTheme();
-        if (TextUtils.isEmpty(theme)) {
-            theme = mContext.getString(R.string.default_theme);
-            pref.setSummary(theme);
+        CharSequence themeLabel = null;
+
+        for (int i = 0; i < pkgs.length; i++) {
+            if (TextUtils.equals(pkgs[i], theme)) {
+                themeLabel = labels[i];
+                break;
+            }
         }
-        pref.setSummary(theme);
+
+        if (TextUtils.isEmpty(themeLabel)) {
+            themeLabel = mContext.getString(R.string.default_theme);
+        }
+
+        pref.setSummary(themeLabel);
         pref.setValue(theme);
     }
 
@@ -127,7 +136,7 @@
                     UserHandle.myUserId());
             for (int i = 0, size = infos.size(); i < size; i++) {
                 if (infos.get(i).isEnabled() &&
-                         isChangeableOverlay(infos.get(i).packageName)) {
+                        isChangeableOverlay(infos.get(i).packageName)) {
                     return infos.get(i).packageName;
                 }
             }
diff --git a/src/com/android/settings/fingerprint/FingerprintEnrollEnrolling.java b/src/com/android/settings/fingerprint/FingerprintEnrollEnrolling.java
index 3b3e61d..964e046 100644
--- a/src/com/android/settings/fingerprint/FingerprintEnrollEnrolling.java
+++ b/src/com/android/settings/fingerprint/FingerprintEnrollEnrolling.java
@@ -159,6 +159,14 @@
         startIconAnimation();
     }
 
+    @Override
+    protected void onPause() {
+        super.onPause();
+        if (mSidecar != null) {
+            mSidecar.setListener(null);
+        }
+    }
+
     private void startIconAnimation() {
         mIconAnimationDrawable.start();
     }
diff --git a/src/com/android/settings/fingerprint/FingerprintEnrollSidecar.java b/src/com/android/settings/fingerprint/FingerprintEnrollSidecar.java
index 0225f11..9ab561d 100644
--- a/src/com/android/settings/fingerprint/FingerprintEnrollSidecar.java
+++ b/src/com/android/settings/fingerprint/FingerprintEnrollSidecar.java
@@ -32,6 +32,8 @@
 
 import com.android.settings.password.IFingerprintManager;
 
+import java.util.ArrayList;
+
 /**
  * Sidecar fragment to handle the state around fingerprint enrollment.
  */
@@ -47,6 +49,57 @@
     private boolean mDone;
     private int mUserId;
     private IFingerprintManager mFingerprintManager;
+    private ArrayList<QueuedEvent> mQueuedEvents;
+
+    private abstract class QueuedEvent {
+        public abstract void send(Listener listener);
+    }
+
+    private class QueuedEnrollmentProgress extends QueuedEvent {
+        int enrollmentSteps;
+        int remaining;
+        public QueuedEnrollmentProgress(int enrollmentSteps, int remaining) {
+            this.enrollmentSteps = enrollmentSteps;
+            this.remaining = remaining;
+        }
+
+        @Override
+        public void send(Listener listener) {
+            listener.onEnrollmentProgressChange(enrollmentSteps, remaining);
+        }
+    }
+
+    private class QueuedEnrollmentHelp extends QueuedEvent {
+        int helpMsgId;
+        CharSequence helpString;
+        public QueuedEnrollmentHelp(int helpMsgId, CharSequence helpString) {
+            this.helpMsgId = helpMsgId;
+            this.helpString = helpString;
+        }
+
+        @Override
+        public void send(Listener listener) {
+            listener.onEnrollmentHelp(helpString);
+        }
+    }
+
+    private class QueuedEnrollmentError extends QueuedEvent {
+        int errMsgId;
+        CharSequence errString;
+        public QueuedEnrollmentError(int errMsgId, CharSequence errString) {
+            this.errMsgId = errMsgId;
+            this.errString = errString;
+        }
+
+        @Override
+        public void send(Listener listener) {
+            listener.onEnrollmentError(errMsgId, errString);
+        }
+    }
+
+    public FingerprintEnrollSidecar() {
+        mQueuedEvents = new ArrayList<>();
+    }
 
     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
@@ -104,6 +157,13 @@
 
     public void setListener(Listener listener) {
         mListener = listener;
+        if (mListener != null) {
+            for (int i=0; i<mQueuedEvents.size(); i++) {
+                QueuedEvent event = mQueuedEvents.get(i);
+                event.send(mListener);
+            }
+            mQueuedEvents.clear();
+        }
     }
 
     public int getEnrollmentSteps() {
@@ -130,6 +190,8 @@
             mDone = remaining == 0;
             if (mListener != null) {
                 mListener.onEnrollmentProgressChange(mEnrollmentSteps, remaining);
+            } else {
+                mQueuedEvents.add(new QueuedEnrollmentProgress(mEnrollmentSteps, remaining));
             }
         }
 
@@ -137,6 +199,8 @@
         public void onEnrollmentHelp(int helpMsgId, CharSequence helpString) {
             if (mListener != null) {
                 mListener.onEnrollmentHelp(helpString);
+            } else {
+                mQueuedEvents.add(new QueuedEnrollmentHelp(helpMsgId, helpString));
             }
         }
 
@@ -144,6 +208,8 @@
         public void onEnrollmentError(int errMsgId, CharSequence errString) {
             if (mListener != null) {
                 mListener.onEnrollmentError(errMsgId, errString);
+            } else {
+                mQueuedEvents.add(new QueuedEnrollmentError(errMsgId, errString));
             }
             mEnrolling = false;
         }
diff --git a/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java b/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java
index 1822341..9abc7f9 100644
--- a/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java
+++ b/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java
@@ -128,7 +128,8 @@
         args.putInt(EXTRA_POWER_USAGE_AMOUNT, (int) sipper.totalPowerMah);
 
         caller.startPreferencePanelAsUser(fragment, AdvancedPowerUsageDetail.class.getName(), args,
-                R.string.battery_details_title, null, new UserHandle(UserHandle.myUserId()));
+                R.string.battery_details_title, null,
+                new UserHandle(UserHandle.getUserId(sipper.getUid())));
     }
 
     @Override
@@ -184,6 +185,7 @@
         final Bundle bundle = getArguments();
         EntityHeaderController controller = EntityHeaderController
                 .newInstance(context, this, appSnippet)
+                .setRecyclerView(getListView(), getLifecycle())
                 .setButtonActions(EntityHeaderController.ActionType.ACTION_NONE,
                         EntityHeaderController.ActionType.ACTION_NONE);
 
diff --git a/src/com/android/settings/fuelgauge/BatteryMeterView.java b/src/com/android/settings/fuelgauge/BatteryMeterView.java
index 969f886..b9ed1e4 100644
--- a/src/com/android/settings/fuelgauge/BatteryMeterView.java
+++ b/src/com/android/settings/fuelgauge/BatteryMeterView.java
@@ -38,8 +38,6 @@
     @VisibleForTesting
     ColorFilter mAccentColorFilter;
 
-    private int mLevel;
-
     public BatteryMeterView(Context context) {
         this(context, null, 0);
     }
@@ -51,7 +49,7 @@
     public BatteryMeterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
 
-        final int frameColor = context.getColor(R.color.batterymeter_frame_color);
+        final int frameColor = context.getColor(R.color.meter_background_color);
         mAccentColorFilter = new PorterDuffColorFilter(
                 Utils.getColorAttr(context, android.R.attr.colorAccent), PorterDuff.Mode.SRC_IN);
         mErrorColorFilter = new PorterDuffColorFilter(
@@ -66,7 +64,6 @@
     }
 
     public void setBatteryLevel(int level) {
-        mLevel = level;
         mDrawable.setBatteryLevel(level);
         if (level < mDrawable.getCriticalLevel()) {
             mDrawable.setBatteryColorFilter(mErrorColorFilter);
@@ -75,10 +72,6 @@
         }
     }
 
-    public int getBatteryLevel() {
-        return mLevel;
-    }
-
     public void setCharging(boolean charging) {
         mDrawable.setCharging(charging);
     }
diff --git a/src/com/android/settings/fuelgauge/BatteryUtils.java b/src/com/android/settings/fuelgauge/BatteryUtils.java
index 849144a..327d3a4 100644
--- a/src/com/android/settings/fuelgauge/BatteryUtils.java
+++ b/src/com/android/settings/fuelgauge/BatteryUtils.java
@@ -27,6 +27,8 @@
 import android.util.SparseLongArray;
 
 import com.android.internal.os.BatterySipper;
+import com.android.internal.os.BatteryStatsHelper;
+import com.android.internal.util.ArrayUtils;
 import com.android.settings.overlay.FeatureFactory;
 
 import java.lang.annotation.Retention;
@@ -213,11 +215,43 @@
         return (powerUsageMah / (totalPowerMah - hiddenPowerMah)) * dischargeAmount;
     }
 
+    /**
+     * Calculate the whole running time in the state {@code statsType}
+     *
+     * @param batteryStatsHelper utility class that contains the data
+     * @param statsType state that we want to calculate the time for
+     * @return the running time in millis
+     */
+    public long calculateRunningTimeBasedOnStatsType(BatteryStatsHelper batteryStatsHelper,
+            int statsType) {
+        final long elapsedRealtimeUs = convertMsToUs(SystemClock.elapsedRealtime());
+        // Return the battery time (millisecond) on status mStatsType
+        return convertUsToMs(
+                batteryStatsHelper.getStats().computeBatteryRealtime(elapsedRealtimeUs, statsType));
+
+    }
+
+    /**
+     * Find the package name for a {@link android.os.BatteryStats.Uid}
+     *
+     * @param uid id to get the package name
+     * @return the package name. If there are multiple packages related to
+     * given id, return the first one. Or return null if there are no known
+     * packages with the given id
+     *
+     * @see PackageManager#getPackagesForUid(int)
+     */
+    public String getPackageName(int uid) {
+        final String[] packageNames = mPackageManager.getPackagesForUid(uid);
+
+        return ArrayUtils.isEmpty(packageNames) ? null : packageNames[0];
+    }
+
     private long convertUsToMs(long timeUs) {
         return timeUs / 1000;
     }
 
-    private long convertMsToUs(long timeMs) {
+    public long convertMsToUs(long timeMs) {
         return timeMs * 1000;
     }
 
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index c27ea22..512dc17 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -16,8 +16,6 @@
 
 package com.android.settings.fuelgauge;
 
-import android.animation.Animator;
-import android.animation.ValueAnimator;
 import android.app.Activity;
 import android.app.LoaderManager;
 import android.content.Context;
@@ -50,7 +48,6 @@
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
-import android.view.animation.AnimationUtils;
 import android.widget.TextView;
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -99,10 +96,6 @@
     private static final String KEY_BATTERY_HEADER = "battery_header";
     private static final int MAX_ITEMS_TO_LIST = USE_FAKE_DATA ? 30 : 10;
     private static final int MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP = 10;
-    private static final int BATTERY_ANIMATION_DURATION_MS_PER_LEVEL = 30;
-
-    @VisibleForTesting
-    static final String ARG_BATTERY_LEVEL = "key_battery_level";
 
     private static final String KEY_SCREEN_USAGE = "screen_usage";
     private static final String KEY_TIME_SINCE_LAST_FULL_CHARGE = "last_full_charge";
@@ -128,8 +121,6 @@
             new FooterPreferenceMixin(this, getLifecycle());
 
     @VisibleForTesting
-    int mBatteryLevel;
-    @VisibleForTesting
     boolean mShowAllApps = false;
     @VisibleForTesting
     PowerGaugePreference mScreenUsagePref;
@@ -196,12 +187,11 @@
                         mEnhancedEstimate =
                                 mPowerFeatureProvider.getTimeRemainingEstimate(cursor);
                     }
-                    final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
+                    final long elapsedRealtimeUs =
+                            mBatteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
                     Intent batteryBroadcast = getContext().registerReceiver(null,
                             new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
-                    BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(getContext(),
-                            batteryBroadcast, mStatsHelper.getStats(), elapsedRealtimeUs, false);
-                    useEnhancedEstimateIfAvailable(getContext(), batteryInfo);
+                    BatteryInfo batteryInfo = getBatteryInfo(elapsedRealtimeUs, batteryBroadcast);
                     updateHeaderPreference(batteryInfo);
                 }
 
@@ -216,8 +206,6 @@
         super.onCreate(icicle);
         setAnimationAllowed(true);
 
-        mBatteryLevel = getContext().getResources().getInteger(
-                com.android.internal.R.integer.config_criticalBatteryWarningLevel) + 1;
         mBatteryLayoutPref = (LayoutPreference) findPreference(KEY_BATTERY_HEADER);
         mAppListGroup = (PreferenceGroup) findPreference(KEY_APP_LIST);
         mScreenUsagePref = (PowerGaugePreference) findPreference(KEY_SCREEN_USAGE);
@@ -234,14 +222,6 @@
     }
 
     @Override
-    public void onActivityCreated(Bundle savedInstanceState) {
-        super.onActivityCreated(savedInstanceState);
-        if (savedInstanceState != null) {
-            mBatteryLevel = savedInstanceState.getInt(ARG_BATTERY_LEVEL);
-        }
-    }
-
-    @Override
     public int getMetricsCategory() {
         return MetricsEvent.FUELGAUGE_POWER_USAGE_SUMMARY;
     }
@@ -249,8 +229,6 @@
     @Override
     public void onResume() {
         super.onResume();
-
-        initHeaderPreference();
     }
 
     @Override
@@ -269,12 +247,6 @@
     }
 
     @Override
-    public void onSaveInstanceState(Bundle outState) {
-        super.onSaveInstanceState(outState);
-        outState.putInt(ARG_BATTERY_LEVEL, mBatteryLevel);
-    }
-
-    @Override
     public boolean onPreferenceTreeClick(Preference preference) {
         if (mAnomalySummaryPreferenceController.onPreferenceTreeClick(preference)) {
             return true;
@@ -521,15 +493,14 @@
 
         initAnomalyDetectionIfPossible();
 
-        final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
+        final long elapsedRealtimeUs = mBatteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
         Intent batteryBroadcast = context.registerReceiver(null,
                 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
-        BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(context, batteryBroadcast,
-                mStatsHelper.getStats(), elapsedRealtimeUs, false);
-        useEnhancedEstimateIfAvailable(context, batteryInfo);
+        BatteryInfo batteryInfo = getBatteryInfo(elapsedRealtimeUs, batteryBroadcast);
         updateHeaderPreference(batteryInfo);
 
-        final long runningTime = calculateRunningTimeBasedOnStatsType();
+        final long runningTime = mBatteryUtils.calculateRunningTimeBasedOnStatsType(mStatsHelper,
+                mStatsType);
         updateScreenPreference();
         updateLastFullChargePreference(runningTime);
 
@@ -686,14 +657,6 @@
     }
 
     @VisibleForTesting
-    long calculateRunningTimeBasedOnStatsType() {
-        final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
-        // Return the battery time (millisecond) on status mStatsType
-        return mStatsHelper.getStats().computeBatteryRealtime(elapsedRealtimeUs,
-                mStatsType /* STATS_SINCE_CHARGED */) / 1000;
-    }
-
-    @VisibleForTesting
     void updateHeaderPreference(BatteryInfo info) {
         final Context context = getContext();
         if (context == null) {
@@ -703,46 +666,15 @@
                 .findViewById(R.id.battery_header_icon);
         final TextView timeText = (TextView) mBatteryLayoutPref.findViewById(R.id.battery_percent);
         final TextView summary1 = (TextView) mBatteryLayoutPref.findViewById(R.id.summary1);
+        timeText.setText(Utils.formatPercentage(info.batteryLevel));
         if (info.remainingLabel == null ) {
             summary1.setText(info.statusLabel);
         } else {
             summary1.setText(info.remainingLabel);
         }
+
+        batteryView.setBatteryLevel(info.batteryLevel);
         batteryView.setCharging(!info.discharging);
-        startBatteryHeaderAnimationIfNecessary(batteryView, timeText, mBatteryLevel,
-                info.batteryLevel);
-    }
-
-    @VisibleForTesting
-    void initHeaderPreference() {
-        final BatteryMeterView batteryView = (BatteryMeterView) mBatteryLayoutPref
-                .findViewById(R.id.battery_header_icon);
-        final TextView timeText = (TextView) mBatteryLayoutPref.findViewById(R.id.battery_percent);
-
-        batteryView.setBatteryLevel(mBatteryLevel);
-        timeText.setText(Utils.formatPercentage(mBatteryLevel));
-    }
-
-    @VisibleForTesting
-    void startBatteryHeaderAnimationIfNecessary(BatteryMeterView batteryView, TextView timeTextView,
-            int prevLevel, int currentLevel) {
-        mBatteryLevel = currentLevel;
-        final int diff = Math.abs(prevLevel - currentLevel);
-        if (diff != 0) {
-            final ValueAnimator animator = ValueAnimator.ofInt(prevLevel, currentLevel);
-            animator.setDuration(BATTERY_ANIMATION_DURATION_MS_PER_LEVEL * diff);
-            animator.setInterpolator(AnimationUtils.loadInterpolator(getContext(),
-                    android.R.interpolator.fast_out_slow_in));
-            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
-                @Override
-                public void onAnimationUpdate(ValueAnimator animation) {
-                    final Integer level = (Integer) animation.getAnimatedValue();
-                    batteryView.setBatteryLevel(level);
-                    timeTextView.setText(Utils.formatPercentage(level));
-                }
-            });
-            animator.start();
-        }
     }
 
     @VisibleForTesting
@@ -801,17 +733,20 @@
         }
     }
 
-    @VisibleForTesting
-    void useEnhancedEstimateIfAvailable(Context context, BatteryInfo batteryInfo) {
-        if (mEnhancedEstimate > 0
-                && mPowerFeatureProvider.isEnhancedBatteryPredictionEnabled(context)) {
-            final Resources resources = context.getResources();
-            batteryInfo.remainingTimeUs = mEnhancedEstimate;
-            String timeString = Formatter.formatShortElapsedTime(context, mEnhancedEstimate);
-            batteryInfo.remainingLabel = resources.getString(
-                    com.android.settingslib.R.string.power_remaining_duration_only,
-                    timeString);
+    private BatteryInfo getBatteryInfo(long elapsedRealtimeUs, Intent batteryBroadcast) {
+        BatteryInfo batteryInfo;
+        if (mEnhancedEstimate > 0 &&
+                mPowerFeatureProvider.isEnhancedBatteryPredictionEnabled(
+                        getContext())) {
+            // Drain time is in micro-seconds so we have to multiply by 1000
+            batteryInfo = BatteryInfo.getBatteryInfo(getContext(), batteryBroadcast,
+                    mStatsHelper.getStats(), elapsedRealtimeUs, false,
+                    mBatteryUtils.convertMsToUs(mEnhancedEstimate), true);
+        } else {
+            batteryInfo = BatteryInfo.getBatteryInfo(getContext(), batteryBroadcast,
+                    mStatsHelper.getStats(), elapsedRealtimeUs, false);
         }
+        return batteryInfo;
     }
 
     @VisibleForTesting
diff --git a/src/com/android/settings/fuelgauge/anomaly/Anomaly.java b/src/com/android/settings/fuelgauge/anomaly/Anomaly.java
index 8aff861..90fc852 100644
--- a/src/com/android/settings/fuelgauge/anomaly/Anomaly.java
+++ b/src/com/android/settings/fuelgauge/anomaly/Anomaly.java
@@ -34,9 +34,11 @@
  */
 public class Anomaly implements Parcelable {
     @Retention(RetentionPolicy.SOURCE)
-    @IntDef({AnomalyType.WAKE_LOCK})
+    @IntDef({AnomalyType.WAKE_LOCK,
+            AnomalyType.WAKEUP_ALARM})
     public @interface AnomalyType {
         int WAKE_LOCK = 0;
+        int WAKEUP_ALARM = 1;
     }
 
     @Retention(RetentionPolicy.SOURCE)
@@ -46,7 +48,9 @@
     }
 
     @AnomalyType
-    public static final int[] ANOMALY_TYPE_LIST = {AnomalyType.WAKE_LOCK};
+    public static final int[] ANOMALY_TYPE_LIST =
+            {AnomalyType.WAKE_LOCK,
+            AnomalyType.WAKEUP_ALARM};
 
     /**
      * Type of this this anomaly
diff --git a/src/com/android/settings/fuelgauge/anomaly/checker/WakeLockAnomalyDetector.java b/src/com/android/settings/fuelgauge/anomaly/checker/WakeLockAnomalyDetector.java
index 61293c6..12f5b8c 100644
--- a/src/com/android/settings/fuelgauge/anomaly/checker/WakeLockAnomalyDetector.java
+++ b/src/com/android/settings/fuelgauge/anomaly/checker/WakeLockAnomalyDetector.java
@@ -31,6 +31,7 @@
 import com.android.settings.Utils;
 import com.android.settings.fuelgauge.BatteryUtils;
 import com.android.settings.fuelgauge.anomaly.Anomaly;
+import com.android.settings.fuelgauge.anomaly.AnomalyDetectionPolicy;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -40,17 +41,24 @@
  */
 public class WakeLockAnomalyDetector implements AnomalyDetector {
     private static final String TAG = "WakeLockAnomalyChecker";
-    // TODO: get threshold form server side
-    private static final long WAKE_LOCK_THRESHOLD_MS = 2 * DateUtils.MINUTE_IN_MILLIS;
     private PackageManager mPackageManager;
     private Context mContext;
     @VisibleForTesting
     BatteryUtils mBatteryUtils;
+    @VisibleForTesting
+    long mWakeLockThresholdMs;
 
     public WakeLockAnomalyDetector(Context context) {
+        this(context, new AnomalyDetectionPolicy(context));
+    }
+
+    @VisibleForTesting
+    WakeLockAnomalyDetector(Context context, AnomalyDetectionPolicy policy) {
         mContext = context;
         mPackageManager = context.getPackageManager();
         mBatteryUtils = BatteryUtils.getInstance(context);
+
+        mWakeLockThresholdMs = policy.wakeLockThreshold;
     }
 
     @Override
@@ -82,9 +90,9 @@
 
             // Report it if wakelock time is too long and it is not a hidden batterysipper
             // TODO: add more attributes to detect wakelock anomaly
-            if (maxPartialWakeLockMs > WAKE_LOCK_THRESHOLD_MS
+            if (maxPartialWakeLockMs > mWakeLockThresholdMs
                     && !mBatteryUtils.shouldHideSipper(sipper)) {
-                final String packageName = getPackageName(uid.getUid());
+                final String packageName = mBatteryUtils.getPackageName(uid.getUid());
                 final CharSequence displayName = Utils.getApplicationLabel(mContext,
                         packageName);
 
@@ -101,12 +109,6 @@
         return anomalies;
     }
 
-    private String getPackageName(int uid) {
-        final String[] packageNames = mPackageManager.getPackagesForUid(uid);
-
-        return packageNames == null ? null : packageNames[0];
-    }
-
     @VisibleForTesting
     long getTotalDurationMs(BatteryStats.Timer timer, long rawRealtime) {
         if (timer == null) {
diff --git a/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetector.java b/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetector.java
new file mode 100644
index 0000000..82c009e
--- /dev/null
+++ b/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetector.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2017 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.fuelgauge.anomaly.checker;
+
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.os.BatteryStats;
+import android.os.SystemClock;
+import android.support.annotation.VisibleForTesting;
+import android.text.format.DateUtils;
+import android.util.ArrayMap;
+
+import com.android.internal.os.BatterySipper;
+import com.android.internal.os.BatteryStatsHelper;
+import com.android.settings.Utils;
+import com.android.settings.fuelgauge.BatteryUtils;
+import com.android.settings.fuelgauge.anomaly.Anomaly;
+import com.android.settings.fuelgauge.anomaly.AnomalyDetectionPolicy;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Check whether apps has too many wakeup alarms
+ */
+public class WakeupAlarmAnomalyDetector implements AnomalyDetector {
+    private static final String TAG = "WakeupAlarmAnomalyDetector";
+    //TODO: add this threshold into AnomalyDetectionPolicy
+    private static final int WAKEUP_ALARM_THRESHOLD = 60;
+    private Context mContext;
+    @VisibleForTesting
+    BatteryUtils mBatteryUtils;
+
+    public WakeupAlarmAnomalyDetector(Context context) {
+        mContext = context;
+        mBatteryUtils = BatteryUtils.getInstance(context);
+    }
+
+    @Override
+    public List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper) {
+        final List<BatterySipper> batterySippers = batteryStatsHelper.getUsageList();
+        final List<Anomaly> anomalies = new ArrayList<>();
+        final long totalRunningHours = mBatteryUtils.calculateRunningTimeBasedOnStatsType(
+                batteryStatsHelper, BatteryStats.STATS_SINCE_CHARGED) / DateUtils.HOUR_IN_MILLIS;
+
+        if (totalRunningHours != 0) {
+            for (int i = 0, size = batterySippers.size(); i < size; i++) {
+                final BatterySipper sipper = batterySippers.get(i);
+                final BatteryStats.Uid uid = sipper.uidObj;
+                if (uid == null || mBatteryUtils.shouldHideSipper(sipper)) {
+                    continue;
+                }
+
+                final int wakeups = getWakeupAlarmCountFromUid(uid);
+                if ((wakeups / totalRunningHours) > WAKEUP_ALARM_THRESHOLD) {
+                    final String packageName = mBatteryUtils.getPackageName(uid.getUid());
+                    final CharSequence displayName = Utils.getApplicationLabel(mContext,
+                            packageName);
+
+                    Anomaly anomaly = new Anomaly.Builder()
+                            .setUid(uid.getUid())
+                            .setType(Anomaly.AnomalyType.WAKEUP_ALARM)
+                            .setDisplayName(displayName)
+                            .setPackageName(packageName)
+                            .build();
+                    anomalies.add(anomaly);
+                }
+            }
+        }
+
+        return anomalies;
+    }
+
+    @VisibleForTesting
+    int getWakeupAlarmCountFromUid(BatteryStats.Uid uid) {
+        int wakeups = 0;
+        final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
+                = uid.getPackageStats();
+        for (int ipkg = packageStats.size() - 1; ipkg >= 0; ipkg--) {
+            final BatteryStats.Uid.Pkg ps = packageStats.valueAt(ipkg);
+            final ArrayMap<String, ? extends BatteryStats.Counter> alarms =
+                    ps.getWakeupAlarmStats();
+            for (int iwa = alarms.size() - 1; iwa >= 0; iwa--) {
+                int count = alarms.valueAt(iwa).getCountLocked(BatteryStats.STATS_SINCE_CHARGED);
+                wakeups += count;
+            }
+
+        }
+
+        return wakeups;
+    }
+
+}
diff --git a/src/com/android/settings/notification/AppNotificationSettings.java b/src/com/android/settings/notification/AppNotificationSettings.java
index 92ad3f1..e7be62f 100644
--- a/src/com/android/settings/notification/AppNotificationSettings.java
+++ b/src/com/android/settings/notification/AppNotificationSettings.java
@@ -132,6 +132,7 @@
         final Activity activity = getActivity();
         final Preference pref = EntityHeaderController
                 .newInstance(activity, this /* fragment */, null /* header */)
+                .setRecyclerView(getListView(), getLifecycle())
                 .setIcon(mAppRow.icon)
                 .setLabel(mAppRow.label)
                 .setPackageName(mAppRow.pkg)
diff --git a/src/com/android/settings/notification/ChannelNotificationSettings.java b/src/com/android/settings/notification/ChannelNotificationSettings.java
index 85a56ba..f7bf1ca 100644
--- a/src/com/android/settings/notification/ChannelNotificationSettings.java
+++ b/src/com/android/settings/notification/ChannelNotificationSettings.java
@@ -112,6 +112,7 @@
         final Activity activity = getActivity();
         final Preference pref = EntityHeaderController
                 .newInstance(activity, this /* fragment */, null /* header */)
+                .setRecyclerView(getListView(), getLifecycle())
                 .setIcon(mAppRow.icon)
                 .setLabel(mChannel.getName())
                 .setSummary(mAppRow.label)
diff --git a/src/com/android/settings/notification/ConfigureNotificationSettings.java b/src/com/android/settings/notification/ConfigureNotificationSettings.java
index ad71e83..3e363d7 100644
--- a/src/com/android/settings/notification/ConfigureNotificationSettings.java
+++ b/src/com/android/settings/notification/ConfigureNotificationSettings.java
@@ -16,13 +16,17 @@
 
 package com.android.settings.notification;
 
+import android.app.Activity;
 import android.content.Context;
 import android.provider.SearchIndexableResource;
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.R;
+import com.android.settings.applications.NotificationApps;
+import com.android.settings.applications.NotificationApps.SummaryProvider;
 import com.android.settings.core.PreferenceController;
 import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.dashboard.SummaryLoader;
 import com.android.settings.gestures.SwipeToNotificationPreferenceController;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Indexable;
@@ -78,6 +82,15 @@
         return controllers;
     }
 
+    public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
+        = new SummaryLoader.SummaryProviderFactory() {
+            @Override
+            public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
+                    SummaryLoader summaryLoader) {
+                return new NotificationApps.SummaryProvider(activity, summaryLoader);
+            }
+    };
+
     /**
      * For Search.
      */
diff --git a/src/com/android/settings/search/SearchIndexableResources.java b/src/com/android/settings/search/SearchIndexableResources.java
index ce8135c..ab1af49 100644
--- a/src/com/android/settings/search/SearchIndexableResources.java
+++ b/src/com/android/settings/search/SearchIndexableResources.java
@@ -40,6 +40,7 @@
 import com.android.settings.backup.BackupSettingsActivity;
 import com.android.settings.backup.BackupSettingsFragment;
 import com.android.settings.bluetooth.BluetoothSettings;
+import com.android.settings.bluetooth.BluetoothSettingsObsolete;
 import com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment;
 import com.android.settings.datausage.DataUsageMeteredSettings;
 import com.android.settings.datausage.DataUsageSummary;
@@ -110,6 +111,7 @@
         addIndex(SavedAccessPointsWifiSettings.class, NO_DATA_RES_ID,
                 R.drawable.ic_settings_wireless);
         addIndex(BluetoothSettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_bluetooth);
+        addIndex(BluetoothSettingsObsolete.class, NO_DATA_RES_ID, R.drawable.ic_settings_bluetooth);
         addIndex(SimSettings.class, NO_DATA_RES_ID, R.drawable.ic_sim_sd);
         addIndex(DataUsageSummary.class, NO_DATA_RES_ID, R.drawable.ic_settings_data_usage);
         addIndex(DataUsageMeteredSettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_data_usage);
diff --git a/src/com/android/settings/search2/CursorToSearchResultConverter.java b/src/com/android/settings/search2/CursorToSearchResultConverter.java
index 706f33b..30afbd0 100644
--- a/src/com/android/settings/search2/CursorToSearchResultConverter.java
+++ b/src/com/android/settings/search2/CursorToSearchResultConverter.java
@@ -74,6 +74,7 @@
     private static final String[] whiteList = {
             "main_toggle_wifi",
             "main_toggle_bluetooth",
+            "main_toggle_bluetooth_obsolete",
             "toggle_airplane",
             "tether_settings",
             "battery_saver",
diff --git a/src/com/android/settings/widget/ActionBarShadowController.java b/src/com/android/settings/widget/ActionBarShadowController.java
new file mode 100644
index 0000000..75bdf0e
--- /dev/null
+++ b/src/com/android/settings/widget/ActionBarShadowController.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2017 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.widget;
+
+import android.app.ActionBar;
+import android.app.Activity;
+import android.support.v7.widget.RecyclerView;
+import android.view.View;
+
+import com.android.settingslib.core.lifecycle.Lifecycle;
+import com.android.settingslib.core.lifecycle.LifecycleObserver;
+import com.android.settingslib.core.lifecycle.events.OnStart;
+import com.android.settingslib.core.lifecycle.events.OnStop;
+
+public class ActionBarShadowController implements LifecycleObserver, OnStart, OnStop {
+
+    private ScrollChangeWatcher mScrollChangeWatcher;
+    private RecyclerView mRecyclerView;
+    private boolean isScrollWatcherAttached;
+
+    public static ActionBarShadowController attachToRecyclerView(Activity activity,
+            Lifecycle lifecycle, RecyclerView recyclerView) {
+        return new ActionBarShadowController(activity, lifecycle, recyclerView);
+    }
+
+    private ActionBarShadowController(Activity activity, Lifecycle lifecycle,
+            RecyclerView recyclerView) {
+        mScrollChangeWatcher = new ScrollChangeWatcher(activity);
+        mRecyclerView = recyclerView;
+        attachScrollWatcher();
+        lifecycle.addObserver(this);
+    }
+
+    @Override
+    public void onStop() {
+        detachScrollWatcher();
+    }
+
+    private void detachScrollWatcher() {
+        mRecyclerView.removeOnScrollListener(mScrollChangeWatcher);
+        isScrollWatcherAttached = false;
+    }
+
+    @Override
+    public void onStart() {
+        attachScrollWatcher();
+    }
+
+    private void attachScrollWatcher() {
+        if (!isScrollWatcherAttached) {
+            isScrollWatcherAttached = true;
+            mRecyclerView.addOnScrollListener(mScrollChangeWatcher);
+            mScrollChangeWatcher.updateDropShadow(mRecyclerView);
+        }
+    }
+
+    /**
+     * Update the drop shadow as the scrollable entity is scrolled.
+     */
+    private final class ScrollChangeWatcher extends RecyclerView.OnScrollListener {
+
+        private Activity mActivity;
+
+        public ScrollChangeWatcher(Activity activity) {
+            mActivity = activity;
+        }
+
+        // RecyclerView scrolled.
+        @Override
+        public void onScrolled(RecyclerView view, int dx, int dy) {
+            updateDropShadow(view);
+        }
+
+        public void updateDropShadow(View view) {
+            final boolean shouldShowShadow = view.canScrollVertically(-1);
+            final ActionBar actionBar = mActivity.getActionBar();
+            if (actionBar != null) {
+                actionBar.setElevation(shouldShowShadow ? 8 : 0);
+            }
+        }
+    }
+
+}
diff --git a/src/com/android/settings/widget/DonutView.java b/src/com/android/settings/widget/DonutView.java
index 0feaa07..7a13a0e 100644
--- a/src/com/android/settings/widget/DonutView.java
+++ b/src/com/android/settings/widget/DonutView.java
@@ -17,12 +17,14 @@
 
 import android.content.Context;
 import android.graphics.Canvas;
+import android.graphics.ColorFilter;
 import android.graphics.Paint;
+import android.graphics.PorterDuff;
+import android.graphics.PorterDuffColorFilter;
 import android.text.TextPaint;
 import android.util.AttributeSet;
 import android.view.View;
 
-import com.android.internal.util.Preconditions;
 import com.android.settings.R;
 import com.android.settings.Utils;
 
@@ -50,20 +52,26 @@
         super(context, attrs);
         mDeviceDensity = getResources().getDisplayMetrics().density;
         mStrokeWidth = 6f * mDeviceDensity;
+        final ColorFilter mAccentColorFilter =
+                new PorterDuffColorFilter(
+                        Utils.getColorAttr(context, android.R.attr.colorAccent),
+                        PorterDuff.Mode.SRC_IN);
 
         mBackgroundCircle = new Paint();
         mBackgroundCircle.setAntiAlias(true);
         mBackgroundCircle.setStrokeCap(Paint.Cap.BUTT);
         mBackgroundCircle.setStyle(Paint.Style.STROKE);
         mBackgroundCircle.setStrokeWidth(mStrokeWidth);
-        mBackgroundCircle.setColor(getResources().getColor(R.color.donut_background_grey));
+        mBackgroundCircle.setColorFilter(mAccentColorFilter);
+        mBackgroundCircle.setColor(context.getColor(R.color.meter_background_color));
 
         mFilledArc = new Paint();
         mFilledArc.setAntiAlias(true);
         mFilledArc.setStrokeCap(Paint.Cap.BUTT);
         mFilledArc.setStyle(Paint.Style.STROKE);
         mFilledArc.setStrokeWidth(mStrokeWidth);
-        mFilledArc.setColor(Utils.getColorAccent(getContext()));
+        mFilledArc.setColor(Utils.getDefaultColor(mContext, R.color.meter_consumed_color));
+        mFilledArc.setColorFilter(mAccentColorFilter);
 
         mTextPaint = new TextPaint();
         mTextPaint.setColor(Utils.getColorAccent(getContext()));
@@ -86,11 +94,25 @@
     }
 
     private void drawDonut(Canvas canvas) {
-        canvas.drawArc(0 + mStrokeWidth, 0 + mStrokeWidth, getWidth() - mStrokeWidth,
-                getHeight() - mStrokeWidth, TOP, 360, false, mBackgroundCircle);
+        canvas.drawArc(
+                0 + mStrokeWidth,
+                0 + mStrokeWidth,
+                getWidth() - mStrokeWidth,
+                getHeight() - mStrokeWidth,
+                TOP,
+                360,
+                false,
+                mBackgroundCircle);
 
-        canvas.drawArc(0 + mStrokeWidth, 0 + mStrokeWidth, getWidth() - mStrokeWidth,
-                getHeight() - mStrokeWidth, TOP, (360 * mPercent / 100), false, mFilledArc);
+        canvas.drawArc(
+                0 + mStrokeWidth,
+                0 + mStrokeWidth,
+                getWidth() - mStrokeWidth,
+                getHeight() - mStrokeWidth,
+                TOP,
+                (360 * mPercent / 100),
+                false,
+                mFilledArc);
     }
 
     private void drawInnerText(Canvas canvas) {
diff --git a/src/com/android/settings/widget/EntityHeaderController.java b/src/com/android/settings/widget/EntityHeaderController.java
index d7ba35a..70d3ce5 100644
--- a/src/com/android/settings/widget/EntityHeaderController.java
+++ b/src/com/android/settings/widget/EntityHeaderController.java
@@ -30,6 +30,7 @@
 import android.os.UserHandle;
 import android.support.annotation.IntDef;
 import android.support.annotation.VisibleForTesting;
+import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.LayoutInflater;
@@ -46,6 +47,7 @@
 import com.android.settings.applications.LayoutPreference;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settingslib.applications.ApplicationsState;
+import com.android.settingslib.core.lifecycle.Lifecycle;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -68,11 +70,13 @@
 
     private static final String TAG = "AppDetailFeature";
 
-    private final Context mContext;
+    private final Context mAppContext;
+    private final Activity mActivity;
     private final Fragment mFragment;
     private final int mMetricsCategory;
     private final View mHeader;
-
+    private Lifecycle mLifecycle;
+    private RecyclerView mRecyclerView;
     private Drawable mIcon;
     private CharSequence mLabel;
     private CharSequence mSummary;
@@ -93,15 +97,16 @@
      * @param fragment The fragment that header will be placed in.
      * @param header   Optional: header view if it's already created.
      */
-    public static EntityHeaderController newInstance(Context context, Fragment fragment,
+    public static EntityHeaderController newInstance(Activity activity, Fragment fragment,
             View header) {
-        return new EntityHeaderController(context.getApplicationContext(), fragment, header);
+        return new EntityHeaderController(activity, fragment, header);
     }
 
-    private EntityHeaderController(Context context, Fragment fragment, View header) {
-        mContext = context;
+    private EntityHeaderController(Activity activity, Fragment fragment, View header) {
+        mActivity = activity;
+        mAppContext = activity.getApplicationContext();
         mFragment = fragment;
-        mMetricsCategory = FeatureFactory.getFactory(context).getMetricsFeatureProvider()
+        mMetricsCategory = FeatureFactory.getFactory(mAppContext).getMetricsFeatureProvider()
                 .getMetricsCategory(fragment);
         if (header != null) {
             mHeader = header;
@@ -111,16 +116,22 @@
         }
     }
 
+    public EntityHeaderController setRecyclerView(RecyclerView recyclerView, Lifecycle lifecycle) {
+        mRecyclerView = recyclerView;
+        mLifecycle = lifecycle;
+        return this;
+    }
+
     public EntityHeaderController setIcon(Drawable icon) {
         if (icon != null) {
-            mIcon = icon.getConstantState().newDrawable(mContext.getResources());
+            mIcon = icon.getConstantState().newDrawable(mAppContext.getResources());
         }
         return this;
     }
 
     public EntityHeaderController setIcon(ApplicationsState.AppEntry appEntry) {
         if (appEntry.icon != null) {
-            mIcon = appEntry.icon.getConstantState().newDrawable(mContext.getResources());
+            mIcon = appEntry.icon.getConstantState().newDrawable(mAppContext.getResources());
         }
         return this;
     }
@@ -233,6 +244,9 @@
         actionBar.setBackgroundDrawable(
                 new ColorDrawable(Utils.getColorAttr(activity, android.R.attr.colorSecondary)));
         actionBar.setElevation(0);
+        if (mRecyclerView != null && mLifecycle != null) {
+            ActionBarShadowController.attachToRecyclerView(mActivity, mLifecycle, mRecyclerView);
+        }
 
         return this;
     }
@@ -257,7 +271,7 @@
                     button.setVisibility(View.GONE);
                 } else {
                     button.setContentDescription(
-                            mContext.getString(R.string.application_info_label));
+                            mAppContext.getString(R.string.application_info_label));
                     button.setImageResource(com.android.settings.R.drawable.ic_info);
                     button.setOnClickListener(new View.OnClickListener() {
                         @Override
@@ -311,7 +325,7 @@
     }
 
     private Intent resolveIntent(Intent i) {
-        ResolveInfo result = mContext.getPackageManager().resolveActivity(i, 0);
+        ResolveInfo result = mAppContext.getPackageManager().resolveActivity(i, 0);
         if (result != null) {
             return new Intent(i.getAction())
                     .setClassName(result.activityInfo.packageName, result.activityInfo.name);
diff --git a/tests/robotests/assets/grandfather_not_implementing_index_provider b/tests/robotests/assets/grandfather_not_implementing_index_provider
index e21b76c..c668779 100644
--- a/tests/robotests/assets/grandfather_not_implementing_index_provider
+++ b/tests/robotests/assets/grandfather_not_implementing_index_provider
@@ -1,4 +1,5 @@
 com.android.settings.bluetooth.DevicePickerFragment
+com.android.settings.bluetooth.BluetoothPairingDetail
 com.android.settings.notification.ZenModePrioritySettings
 com.android.settings.accounts.AccountDetailDashboardFragment
 com.android.settings.fuelgauge.PowerUsageAnomalyDetails
diff --git a/tests/robotests/src/com/android/settings/accounts/AccountHeaderPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accounts/AccountHeaderPreferenceControllerTest.java
index 0668379..64c2e9e 100644
--- a/tests/robotests/src/com/android/settings/accounts/AccountHeaderPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/accounts/AccountHeaderPreferenceControllerTest.java
@@ -18,10 +18,10 @@
 
 import android.accounts.Account;
 import android.app.Activity;
-import android.app.Fragment;
 import android.content.Context;
 import android.os.Bundle;
 import android.os.UserHandle;
+import android.support.v14.preference.PreferenceFragment;
 import android.support.v7.preference.PreferenceScreen;
 import android.widget.TextView;
 
@@ -31,6 +31,7 @@
 import com.android.settings.applications.LayoutPreference;
 import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settingslib.accounts.AuthenticatorHelper;
+import com.android.settingslib.core.lifecycle.Lifecycle;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -57,7 +58,7 @@
     @Mock
     private Activity mActivity;
     @Mock
-    private Fragment mFragment;
+    private PreferenceFragment mFragment;
     @Mock
     private PreferenceScreen mScreen;
 
@@ -76,7 +77,7 @@
     @Test
     public void isAvailable_noArgs_shouldReturnNull() {
         mController = new AccountHeaderPreferenceController(RuntimeEnvironment.application,
-                mActivity, mFragment, null /* args */);
+                new Lifecycle(), mActivity, mFragment, null /* args */);
 
         assertThat(mController.isAvailable()).isFalse();
     }
@@ -89,7 +90,7 @@
         args.putParcelable(AccountDetailDashboardFragment.KEY_ACCOUNT, account);
         args.putParcelable(AccountDetailDashboardFragment.KEY_USER_HANDLE, UserHandle.CURRENT);
         mController = new AccountHeaderPreferenceController(RuntimeEnvironment.application,
-                mActivity, mFragment, args);
+                new Lifecycle(), mActivity, mFragment, args);
 
         assertThat(mController.isAvailable()).isTrue();
 
diff --git a/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java b/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java
index 17910bf..a09aeec 100644
--- a/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java
+++ b/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java
@@ -18,6 +18,7 @@
 
 
 import static com.google.common.truth.Truth.assertThat;
+
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.doReturn;
@@ -65,12 +66,17 @@
 import org.robolectric.annotation.Config;
 import org.robolectric.util.ReflectionHelpers;
 
+import java.util.ArrayList;
+import java.util.List;
+
 
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
 public final class InstalledAppDetailsTest {
 
     private static final String PACKAGE_NAME = "test_package_name";
+    private static final int TARGET_UID = 111;
+    private static final int OTHER_UID = 222;
 
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private Context mContext;
@@ -87,6 +93,8 @@
     @Mock
     private BatterySipper mBatterySipper;
     @Mock
+    private BatterySipper mOtherBatterySipper;
+    @Mock
     private BatteryStatsHelper mBatteryStatsHelper;
     @Mock
     private BatteryStats.Uid mUid;
@@ -105,6 +113,8 @@
 
         mBatterySipper.drainType = BatterySipper.DrainType.IDLE;
         mBatterySipper.uidObj = mUid;
+        doReturn(TARGET_UID).when(mBatterySipper).getUid();
+        doReturn(OTHER_UID).when(mOtherBatterySipper).getUid();
         doReturn(mActivity).when(mAppDetail).getActivity();
         doReturn(mShadowContext).when(mAppDetail).getContext();
         doReturn(mPackageManager).when(mActivity).getPackageManager();
@@ -388,4 +398,15 @@
 
         verify(mActivity).invalidateOptionsMenu();
     }
+
+    @Test
+    public void findTargetSipper_findCorrectSipper() {
+        List<BatterySipper> usageList = new ArrayList<>();
+        usageList.add(mBatterySipper);
+        usageList.add(mOtherBatterySipper);
+        doReturn(usageList).when(mBatteryStatsHelper).getUsageList();
+
+        assertThat(mAppDetail.findTargetSipper(mBatteryStatsHelper, TARGET_UID)).isEqualTo(
+                mBatterySipper);
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/applications/NotificationAppsTest.java b/tests/robotests/src/com/android/settings/applications/NotificationAppsTest.java
new file mode 100644
index 0000000..f84e7a8
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/applications/NotificationAppsTest.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2017 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.applications;
+
+import android.content.Context;
+
+import android.content.pm.ApplicationInfo;
+import android.content.pm.UserInfo;
+import android.os.UserManager;
+
+import com.android.settings.R;
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import com.android.settings.dashboard.SummaryLoader;
+import com.android.settings.notification.NotificationBackend;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowApplication;
+import org.robolectric.util.ReflectionHelpers;
+
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class NotificationAppsTest {
+
+    @Mock
+    private PackageManagerWrapper mPackageManager;
+    @Mock
+    private UserManager mUserManager;
+    @Mock
+    private SummaryLoader mSummaryLoader;
+    @Mock
+    private NotificationBackend mBackend;
+
+    private Context mContext;
+    private NotificationApps.SummaryProvider mSummaryProvider;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        ShadowApplication shadowApplication = ShadowApplication.getInstance();
+        shadowApplication.setSystemService(Context.USER_SERVICE, mUserManager);
+        mContext = shadowApplication.getApplicationContext();
+        mSummaryProvider = spy(new NotificationApps.SummaryProvider(mContext, mSummaryLoader));
+        ReflectionHelpers.setField(mSummaryProvider, "mNotificationBackend", mBackend);
+        ReflectionHelpers.setField(mSummaryProvider, "mPackageManager", mPackageManager);
+    }
+
+    @Test
+    public void setListening_shouldSetSummary() {
+        List<UserInfo> userInfos = new ArrayList<>();
+        userInfos.add(new UserInfo(1, "user1", 0));
+        when(mUserManager.getProfiles(anyInt())).thenReturn(userInfos);
+        List<ApplicationInfo> appInfos = new ArrayList<>();
+        ApplicationInfo info1 = new ApplicationInfo();
+        info1.packageName = "package1";
+        appInfos.add(info1);
+        ApplicationInfo info2 = new ApplicationInfo();
+        info2.packageName = "package2";
+        appInfos.add(info2);
+        when(mPackageManager.getInstalledApplicationsAsUser(anyInt(), anyInt()))
+            .thenReturn(appInfos);
+
+        // no notification off
+        when(mBackend.getNotificationsBanned(anyString(), anyInt())).thenReturn(false);
+        mSummaryProvider.setListening(true);
+        ShadowApplication.runBackgroundTasks();
+        verify(mSummaryLoader).setSummary(mSummaryProvider,
+            mContext.getString(R.string.notification_summary_none));
+
+        // some notification off
+        when(mBackend.getNotificationsBanned(eq("package1"), anyInt())).thenReturn(true);
+        mSummaryProvider.setListening(true);
+        ShadowApplication.runBackgroundTasks();
+        verify(mSummaryLoader).setSummary(mSummaryProvider,
+            mContext.getResources().getQuantityString(R.plurals.notification_summary, 1, 1));
+
+        when(mBackend.getNotificationsBanned(eq("package2"), anyInt())).thenReturn(true);
+        mSummaryProvider.setListening(true);
+        ShadowApplication.runBackgroundTasks();
+        verify(mSummaryLoader).setSummary(mSummaryProvider,
+            mContext.getResources().getQuantityString(R.plurals.notification_summary, 2, 2));
+    }
+
+}
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothMasterSwitchPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothMasterSwitchPreferenceControllerTest.java
index 177130e..f0fb91c 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothMasterSwitchPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothMasterSwitchPreferenceControllerTest.java
@@ -16,14 +16,18 @@
 
 package com.android.settings.bluetooth;
 
+import android.app.Fragment;
 import android.content.Context;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.Preference.OnPreferenceChangeListener;
 import android.support.v7.preference.PreferenceScreen;
 
 import com.android.settings.R;
+import com.android.settings.SettingsActivity;
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
+import com.android.settings.overlay.FeatureFactory;
+import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settings.widget.MasterSwitchPreference;
 import com.android.settingslib.bluetooth.BluetoothCallback;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -38,7 +42,10 @@
 import org.robolectric.annotation.Config;
 
 import static com.google.common.truth.Truth.assertThat;
+
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -54,17 +61,26 @@
     private MasterSwitchPreference mPreference;
     @Mock
     private RestrictionUtils mRestrictionUtils;
+    @Mock
+    private Fragment mFragment;
+    @Mock
+    private SettingsActivity mActivity;
 
     private Context mContext;
     private BluetoothMasterSwitchPreferenceController mController;
+    private FakeFeatureFactory mFeatureFactory;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        mContext = RuntimeEnvironment.application.getApplicationContext();
+        mContext = spy(RuntimeEnvironment.application.getApplicationContext());
+        FakeFeatureFactory.setupForTest(mContext);
+        mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
+
         mController = new BluetoothMasterSwitchPreferenceController(
-                mContext, mBluetoothManager, mRestrictionUtils);
+                mContext, mBluetoothManager, mRestrictionUtils, mFragment, mActivity);
         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        when(mPreference.getKey()).thenReturn(mController.getPreferenceKey());
     }
 
     @Test
@@ -84,7 +100,7 @@
         mController.onPause();
 
         verify(mBluetoothManager.getEventManager()).unregisterCallback(
-            any(BluetoothCallback.class));
+                any(BluetoothCallback.class));
     }
 
     @Test
@@ -114,4 +130,22 @@
         verify(mPreference).setSummary("test summary");
     }
 
+    @Test
+    public void testHandlePreferenceTreeClick_pairPageEnabled_showNewPage() {
+        when(mFeatureFactory.bluetoothFeatureProvider.isPairingPageEnabled()).thenReturn(true);
+
+        mController.handlePreferenceTreeClick(mPreference);
+
+        verify(mActivity).startPreferencePanelAsUser(eq(mFragment),
+                eq(BluetoothSettings.class.getName()), any(), eq(R.string.bluetooth), any(), any());
+    }
+
+    @Test
+    public void testHandlePreferenceTreeClick_pairPageDisabled_showOldPage() {
+        mController.handlePreferenceTreeClick(mPreference);
+
+        verify(mActivity).startPreferencePanelAsUser(eq(mFragment),
+                eq(BluetoothSettingsObsolete.class.getName()), any(), eq(R.string.bluetooth), any(),
+                any());
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingDetailTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingDetailTest.java
new file mode 100644
index 0000000..6774ba9
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingDetailTest.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2017 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.bluetooth;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyBoolean;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import android.bluetooth.BluetoothAdapter;
+import android.content.Context;
+import android.content.res.Resources;
+import android.os.UserManager;
+import android.support.v7.preference.PreferenceGroup;
+
+import com.android.settings.R;
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
+import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.widget.FooterPreference;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class BluetoothPairingDetailTest {
+
+    @Mock
+    private UserManager mUserManager;
+    @Mock
+    private Resources mResource;
+    @Mock
+    private LocalBluetoothAdapter mLocalAdapter;
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private LocalBluetoothManager mLocalManager;
+    @Mock
+    private PreferenceGroup mPreferenceGroup;
+    private BluetoothPairingDetail mFragment;
+    private Context mContext;
+    private BluetoothProgressCategory mAvailableDevicesCategory;
+    private FooterPreference mFooterPreference;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mContext = RuntimeEnvironment.application;
+        mFragment = spy(new BluetoothPairingDetail());
+        doReturn(mContext).when(mFragment).getContext();
+        doReturn(mResource).when(mFragment).getResources();
+
+        mAvailableDevicesCategory = spy(new BluetoothProgressCategory(mContext));
+        mFooterPreference = new FooterPreference(mContext);
+
+        mFragment.mLocalAdapter = mLocalAdapter;
+        mFragment.mLocalManager = mLocalManager;
+        mFragment.mDeviceListGroup = mPreferenceGroup;
+    }
+
+    @Test
+    public void testInitPreferencesFromPreferenceScreen_findPreferences() {
+        doReturn(mAvailableDevicesCategory).when(mFragment).findPreference(
+                BluetoothPairingDetail.KEY_AVAIL_DEVICES);
+        doReturn(mFooterPreference).when(mFragment).findPreference(
+                BluetoothPairingDetail.KEY_FOOTER_PREF);
+
+        mFragment.initPreferencesFromPreferenceScreen();
+
+        assertThat(mFragment.mAvailableDevicesCategory).isEqualTo(mAvailableDevicesCategory);
+        assertThat(mFragment.mFooterPreference).isEqualTo(mFooterPreference);
+    }
+
+    @Test
+    public void testStartScanning_startScanAndRemoveDevices() {
+        mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory;
+        mFragment.mDeviceListGroup = mAvailableDevicesCategory;
+
+        mFragment.startScanning();
+
+        verify(mLocalAdapter).startScanning(true);
+        verify(mAvailableDevicesCategory).removeAll();
+    }
+
+    @Test
+    public void testUpdateContent_stateOn_addDevices() {
+        mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory;
+        mFragment.mFooterPreference = mFooterPreference;
+        doNothing().when(mFragment).addDeviceCategory(any(), anyInt(), any(), anyBoolean());
+
+        mFragment.updateContent(BluetoothAdapter.STATE_ON);
+
+        verify(mFragment).addDeviceCategory(mAvailableDevicesCategory,
+                R.string.bluetooth_preference_found_devices,
+                BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, false);
+        verify(mLocalAdapter).setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
+    }
+
+    @Test
+    public void testUpdateContent_stateOff_finish() {
+        mFragment.updateContent(BluetoothAdapter.STATE_OFF);
+
+        verify(mFragment).finish();
+    }
+
+}
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingPreferenceControllerTest.java
index 24325b3..937b31f 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingPreferenceControllerTest.java
@@ -18,6 +18,11 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import android.app.FragmentManager;
@@ -27,6 +32,7 @@
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceScreen;
 
+import com.android.settings.SettingsActivity;
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
 import com.android.settings.R;
@@ -45,7 +51,7 @@
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
 public class BluetoothPairingPreferenceControllerTest {
-
+    private static final int ORDER = 1;
     private Context mContext;
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private PreferenceFragment mFragment;
@@ -59,6 +65,8 @@
     private FragmentTransaction mFragmentTransaction;
     @Mock
     private PreferenceScreen mPreferenceScreen;
+    @Mock
+    private SettingsActivity mSettingsActivity;
     private Preference mPreference;
 
     private BluetoothPairingPreferenceController mController;
@@ -70,16 +78,29 @@
         mContext = RuntimeEnvironment.application;
         when(mFragment.getPreferenceScreen().getContext()).thenReturn(mContext);
 
-        mController = new BluetoothPairingPreferenceController(mContext, mFragment);
+        mPreference = new Preference(mContext);
+        mPreference.setKey(BluetoothPairingPreferenceController.KEY_PAIRING);
+
+        mController = new BluetoothPairingPreferenceController(mContext, mFragment,
+                mSettingsActivity);
     }
 
     @Test
     public void testCreateBluetoothPairingPreference() {
-        Preference pref = mController.createBluetoothPairingPreference();
+        Preference pref = mController.createBluetoothPairingPreference(ORDER);
 
         assertThat(pref.getKey()).isEqualTo(BluetoothPairingPreferenceController.KEY_PAIRING);
         assertThat(pref.getIcon()).isEqualTo(mContext.getDrawable(R.drawable.ic_add));
+        assertThat(pref.getOrder()).isEqualTo(ORDER);
         assertThat(pref.getTitle()).isEqualTo(
                 mContext.getString(R.string.bluetooth_pairing_pref_title));
     }
+
+    @Test
+    public void testHandlePreferenceTreeClick_startFragment() {
+        mController.handlePreferenceTreeClick(mPreference);
+
+        verify(mSettingsActivity).startPreferencePanelAsUser(eq(mFragment), anyString(), any(),
+                anyInt(), any(), any());
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsObsoleteTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsObsoleteTest.java
new file mode 100644
index 0000000..549eeb7
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsObsoleteTest.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2017 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.bluetooth;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
+import android.content.Context;
+
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import com.android.settings.testutils.FakeFeatureFactory;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.annotation.Config;
+
+import java.util.List;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class BluetoothSettingsObsoleteTest {
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private Context mContext;
+    private BluetoothSettingsObsolete mFragment;
+    private FakeFeatureFactory mFeatureFactory;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        FakeFeatureFactory.setupForTest(mContext);
+        mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
+
+        mFragment = spy(new BluetoothSettingsObsolete());
+        doReturn(mContext).when(mFragment).getContext();
+    }
+
+    @Test
+    public void testSearchIndexProvider_pairPageEnabled_keyAdded() {
+        doReturn(true).when(mFeatureFactory.bluetoothFeatureProvider).isPairingPageEnabled();
+
+        final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
+                mContext);
+
+        assertThat(keys).contains(BluetoothSettingsObsolete.DATA_KEY_REFERENCE);
+    }
+
+    @Test
+    public void testSearchIndexProvider_pairPageDisabled_keyNotAdded() {
+        final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
+                mContext);
+
+        assertThat(keys).doesNotContain(BluetoothSettingsObsolete.DATA_KEY_REFERENCE);
+    }
+
+}
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsTest.java
index 35207f5..6ceca41 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsTest.java
@@ -23,24 +23,33 @@
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.spy;
 
+import android.app.Activity;
 import android.content.Context;
 import android.content.res.Resources;
 import android.os.UserManager;
 import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceGroup;
+import android.view.View;
+import android.widget.TextView;
 
 import com.android.settings.R;
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
+import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
+import com.android.settingslib.widget.FooterPreference;
 
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Answers;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
 
+import java.util.List;
+
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
 public class BluetoothSettingsTest {
@@ -51,23 +60,42 @@
     @Mock
     private Resources mResource;
     @Mock
-    private Context mContext;
-    @Mock
     private LocalBluetoothAdapter mLocalAdapter;
+    @Mock
+    private Activity mActivity;
+    @Mock
+    private PreferenceGroup mPairedDevicesCategory;
+    @Mock
+    private BluetoothPairingPreferenceController mPairingPreferenceController;
+    private Context mContext;
     private BluetoothSettings mFragment;
-    private Preference mMyDevicePreference;
+    private FakeFeatureFactory mFeatureFactory;
+    private Preference mFooterPreference;
+    private TextView mEmptyMessage;
+    private View mContainer;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
+        mContext = spy(RuntimeEnvironment.application);
+        FakeFeatureFactory.setupForTest(mContext);
+        mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
+
         mFragment = spy(new BluetoothSettings());
+
         doReturn(mContext).when(mFragment).getContext();
         doReturn(mResource).when(mFragment).getResources();
+        doReturn(mActivity).when(mFragment).getActivity();
 
-        mMyDevicePreference = new Preference(RuntimeEnvironment.application);
+        mContainer = new View(mContext);
+        mEmptyMessage = new TextView(mContext);
+        doReturn(mContainer).when(mActivity).findViewById(android.R.id.list_container);
+        doReturn(mEmptyMessage).when(mActivity).findViewById(android.R.id.empty);
 
+        mFooterPreference = new FooterPreference(RuntimeEnvironment.application);
         mFragment.setLocalBluetoothAdapter(mLocalAdapter);
+        mFragment.mPairingPrefController = mPairingPreferenceController;
     }
 
     @Test
@@ -81,9 +109,56 @@
         doReturn(FOOTAGE_MAC_STRING).when(mFragment).getString(
                 eq(R.string.bluetooth_footer_mac_message), any());
 
-        mFragment.updateMyDevicePreference(mMyDevicePreference);
+        mFragment.updateFooterPreference(mFooterPreference);
 
-        assertThat(mMyDevicePreference.getTitle()).isEqualTo(FOOTAGE_MAC_STRING);
+        assertThat(mFooterPreference.getTitle()).isEqualTo(FOOTAGE_MAC_STRING);
+    }
+
+    @Test
+    public void testDisplayEmptyMessage_showEmptyMessage() {
+        mFragment.displayEmptyMessage(true);
+
+        assertThat(mContainer.getVisibility()).isEqualTo(View.INVISIBLE);
+        assertThat(mEmptyMessage.getVisibility()).isEqualTo(View.VISIBLE);
+    }
+
+    @Test
+    public void testDisplayEmptyMessage_hideEmptyMessage() {
+        mFragment.displayEmptyMessage(false);
+
+        assertThat(mContainer.getVisibility()).isEqualTo(View.VISIBLE);
+        assertThat(mEmptyMessage.getVisibility()).isEqualTo(View.GONE);
+    }
+
+    @Test
+    public void testInitPreferencesFromPreferenceScreen() {
+        doReturn(mPairedDevicesCategory).when(mFragment).findPreference(
+                BluetoothSettings.KEY_PAIRED_DEVICES);
+        doReturn(mFooterPreference).when(mFragment).findPreference(
+                BluetoothSettings.KEY_FOOTER_PREF);
+
+        mFragment.initPreferencesFromPreferenceScreen();
+
+        assertThat(mFragment.mPairedDevicesCategory).isEqualTo(mPairedDevicesCategory);
+        assertThat(mFragment.mFooterPreference).isEqualTo(mFooterPreference);
+    }
+
+    @Test
+    public void testSearchIndexProvider_pairPageEnabled_keyNotAdded() {
+        doReturn(true).when(mFeatureFactory.bluetoothFeatureProvider).isPairingPageEnabled();
+
+        final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
+                mContext);
+
+        assertThat(keys).doesNotContain(BluetoothSettings.DATA_KEY_REFERENCE);
+    }
+
+    @Test
+    public void testSearchIndexProvider_pairPageDisabled_keyAdded() {
+        final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
+                mContext);
+
+        assertThat(keys).contains(BluetoothSettings.DATA_KEY_REFERENCE);
     }
 
 }
diff --git a/tests/robotests/src/com/android/settings/bluetooth/DeviceListPreferenceFragmentTest.java b/tests/robotests/src/com/android/settings/bluetooth/DeviceListPreferenceFragmentTest.java
new file mode 100644
index 0000000..bbb7359
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/bluetooth/DeviceListPreferenceFragmentTest.java
@@ -0,0 +1,131 @@
+/*
+ * Copyright (C) 2017 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.bluetooth;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.os.UserManager;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.R;
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import com.android.settings.core.PreferenceController;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+import java.util.List;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class DeviceListPreferenceFragmentTest {
+    private static final String FOOTAGE_MAC_STRING = "Bluetooth mac: xxxx";
+
+    @Mock
+    private UserManager mUserManager;
+    @Mock
+    private Resources mResource;
+    @Mock
+    private Context mContext;
+    @Mock
+    private LocalBluetoothAdapter mLocalAdapter;
+    private TestFragment mFragment;
+    private Preference mMyDevicePreference;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mFragment = spy(new TestFragment());
+        doReturn(mContext).when(mFragment).getContext();
+        doReturn(mResource).when(mFragment).getResources();
+        mFragment.mLocalAdapter = mLocalAdapter;
+
+        mMyDevicePreference = new Preference(RuntimeEnvironment.application);
+    }
+
+    @Test
+    public void setUpdateMyDevicePreference_setTitleCorrectly() {
+        doReturn(FOOTAGE_MAC_STRING).when(mFragment).getString(
+                eq(R.string.bluetooth_footer_mac_message), any());
+
+        mFragment.updateFooterPreference(mMyDevicePreference);
+
+        assertThat(mMyDevicePreference.getTitle()).isEqualTo(FOOTAGE_MAC_STRING);
+    }
+
+    /**
+     * Fragment to test since {@code DeviceListPreferenceFragment} is abstract
+     */
+    public static class TestFragment extends DeviceListPreferenceFragment {
+
+        public TestFragment() {
+            super("");
+        }
+
+        @Override
+        public int getMetricsCategory() {
+            return 0;
+        }
+
+        @Override
+        public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
+
+        }
+
+        @Override
+        void initPreferencesFromPreferenceScreen() {
+
+        }
+
+        @Override
+        public String getDeviceListKey() {
+            return null;
+        }
+
+        @Override
+        protected String getLogTag() {
+            return null;
+        }
+
+        @Override
+        protected int getPreferenceScreenResId() {
+            return 0;
+        }
+
+        @Override
+        protected List<PreferenceController> getPreferenceControllers(Context context) {
+            return null;
+        }
+    }
+
+}
diff --git a/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java b/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java
index 7421570..72cbf4e 100644
--- a/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java
@@ -22,10 +22,10 @@
 import android.provider.SearchIndexableResource;
 
 import com.android.settings.R;
+import com.android.settings.nfc.NfcPreferenceController;
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
 import com.android.settings.dashboard.SummaryLoader;
-import com.android.settings.nfc.NfcPreferenceController;
 import com.android.settings.testutils.XmlTestUtils;
 import com.android.settingslib.drawer.CategoryKey;
 
diff --git a/tests/robotests/src/com/android/settings/core/instrumentation/SharedPreferenceLoggerTest.java b/tests/robotests/src/com/android/settings/core/instrumentation/SharedPreferenceLoggerTest.java
index 763d6e3..3e24fcf 100644
--- a/tests/robotests/src/com/android/settings/core/instrumentation/SharedPreferenceLoggerTest.java
+++ b/tests/robotests/src/com/android/settings/core/instrumentation/SharedPreferenceLoggerTest.java
@@ -23,16 +23,26 @@
 import com.android.settings.TestConfig;
 import com.android.settings.testutils.FakeFeatureFactory;
 
+import com.google.common.truth.Platform;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Answers;
+import org.mockito.ArgumentMatcher;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.annotation.Config;
 
+import static com.android.internal.logging.nano.MetricsProto.MetricsEvent
+        .FIELD_SETTINGS_PREFERENCE_CHANGE_FLOAT_VALUE;
+import static com.android.internal.logging.nano.MetricsProto.MetricsEvent
+        .FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE;
+import static com.android.internal.logging.nano.MetricsProto.MetricsEvent
+        .FIELD_SETTINGS_PREFERENCE_CHANGE_NAME;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.argThat;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 
@@ -46,6 +56,7 @@
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private Context mContext;
 
+    private PairMatcher mNamePairMatcher;
     private FakeFeatureFactory mFactory;
     private MetricsFeatureProvider mMetricsFeature;
     private SharedPreferencesLogger mSharedPrefLogger;
@@ -58,6 +69,7 @@
         mMetricsFeature = mFactory.metricsFeatureProvider;
 
         mSharedPrefLogger = new SharedPreferencesLogger(mContext, TEST_TAG);
+        mNamePairMatcher = new PairMatcher(FIELD_SETTINGS_PREFERENCE_CHANGE_NAME, String.class);
     }
 
     @Test
@@ -71,8 +83,11 @@
         editor.putInt(TEST_KEY, 2);
         editor.putInt(TEST_KEY, 2);
 
+        final PairMatcher longMatcher =
+                new PairMatcher(FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE, Long.class);
+
         verify(mMetricsFeature, times(6)).action(any(Context.class), anyInt(),
-                any(Pair.class), any(Pair.class));
+                argThat(mNamePairMatcher), argThat(longMatcher));
     }
 
     @Test
@@ -84,8 +99,16 @@
         editor.putBoolean(TEST_KEY, false);
         editor.putBoolean(TEST_KEY, false);
 
-        verify(mMetricsFeature, times(4)).action(any(Context.class), anyInt(),
-                any(Pair.class), any(Pair.class));
+
+        final PairMatcher trueMatcher =
+                new PairMatcher(FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE, true);
+        final PairMatcher falseMatcher =
+                new PairMatcher(FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE, false);
+
+        verify(mMetricsFeature).action(any(Context.class), anyInt(),
+                argThat(mNamePairMatcher), argThat(trueMatcher));
+        verify(mMetricsFeature, times(3)).action(any(Context.class), anyInt(),
+                argThat(mNamePairMatcher), argThat(falseMatcher));
     }
 
     @Test
@@ -97,8 +120,11 @@
         editor.putLong(TEST_KEY, 1);
         editor.putLong(TEST_KEY, 2);
 
+        final PairMatcher longMatcher =
+                new PairMatcher(FIELD_SETTINGS_PREFERENCE_CHANGE_LONG_VALUE, Long.class);
+
         verify(mMetricsFeature, times(4)).action(any(Context.class), anyInt(),
-                any(Pair.class), any(Pair.class));
+                argThat(mNamePairMatcher), argThat(longMatcher));
     }
 
     @Test
@@ -110,8 +136,40 @@
         editor.putFloat(TEST_KEY, 1);
         editor.putFloat(TEST_KEY, 2);
 
+        final PairMatcher floatMatcher =
+                new PairMatcher(FIELD_SETTINGS_PREFERENCE_CHANGE_FLOAT_VALUE, Float.class);
+
         verify(mMetricsFeature, times(4)).action(any(Context.class), anyInt(),
-                any(Pair.class), any(Pair.class));
+                argThat(mNamePairMatcher), argThat(floatMatcher));
     }
 
+    private static class PairMatcher extends ArgumentMatcher<Pair<Integer, Object>> {
+
+        private final int mExpectedTag;
+        private final Class mExpectedClass;
+        private final Long mExpectedBoolean;
+
+
+        public PairMatcher(int tag, Class clazz) {
+            mExpectedTag = tag;
+            mExpectedClass = clazz;
+            mExpectedBoolean = null;
+        }
+
+        public PairMatcher(int tag, boolean bool) {
+            mExpectedTag = tag;
+            mExpectedClass = Long.class;
+            mExpectedBoolean = bool ? 1L : 0L;
+        }
+
+        @Override
+        public boolean matches(Object arg) {
+            final Pair<Integer, Object> pair = (Pair) arg;
+            boolean booleanMatch = mExpectedBoolean == null
+                    || mExpectedBoolean == pair.second;
+            return pair.first == mExpectedTag
+                    && Platform.isInstanceOfType(pair.second, mExpectedClass)
+                    && booleanMatch;
+        }
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java b/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java
index 2d0f031..8cbe947 100644
--- a/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java
+++ b/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java
@@ -42,11 +42,13 @@
 import org.robolectric.annotation.Config;
 import org.robolectric.util.ReflectionHelpers;
 
+import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
@@ -74,6 +76,8 @@
     @Test
     public void bindAppHeader_allWorkApps_shouldNotShowAppInfoLink() {
         ShadowEntityHeaderController.setUseMock(mHeaderController);
+        when(mHeaderController.setRecyclerView(any(), any())).thenReturn(mHeaderController);
+
         mFragment = spy(new AppDataUsage());
 
         doReturn(mock(PreferenceManager.class, RETURNS_DEEP_STUBS))
diff --git a/tests/robotests/src/com/android/settings/display/ThemePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/ThemePreferenceControllerTest.java
index 2c0f4a7..4e9cede 100644
--- a/tests/robotests/src/com/android/settings/display/ThemePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/ThemePreferenceControllerTest.java
@@ -21,20 +21,25 @@
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.support.v7.preference.ListPreference;
+
 import com.android.settings.R;
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
 import com.android.settings.display.ThemePreferenceController.OverlayManager;
+import com.android.settings.testutils.FakeFeatureFactory;
 
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Answers;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
 
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
@@ -45,33 +50,65 @@
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
 public class ThemePreferenceControllerTest {
 
-    @Mock
-    private ListPreference mPreference;
-    @Mock
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private Context mContext;
     @Mock
     private PackageManager mPackageManager;
     @Mock
     private ApplicationInfo mApplicationInfo;
+    @Mock
+    private ListPreference mPreference;
 
     private ThemePreferenceController mController;
 
     @Before
     public void setUp() throws NameNotFoundException {
         MockitoAnnotations.initMocks(this);
+        FakeFeatureFactory.setupForTest(mContext);
         when(mPackageManager.getApplicationInfo(any(), anyInt())).thenReturn(mApplicationInfo);
         when(mContext.getPackageManager()).thenReturn(mPackageManager);
+        when(mContext.getString(R.string.default_theme))
+                .thenReturn(RuntimeEnvironment.application.getString(R.string.default_theme));
+
         mController = spy(new ThemePreferenceController(mContext, mock(OverlayManager.class)));
     }
 
     @Test
-    public void updateState_themeSet_shouldSetPreferenceValue() {
-        final String[] themes = {"Theme1", "Theme2"};
-        doReturn("Theme1").when(mController).getCurrentTheme();
+    public void updateState_themeSet_shouldSetPreferenceValue() throws NameNotFoundException {
+        final String pkg1 = "pkg1.theme1";
+        final String pkg2 = "pkg2.theme2";
+        final String themeLabel1 = "Theme1";
+        final String themeLabel2 = "Theme2";
+        final String[] themes = {pkg1, pkg2};
+        doReturn("pkg1.theme1").when(mController).getCurrentTheme();
         doReturn(themes).when(mController).getAvailableThemes();
+        when(mPackageManager.getApplicationInfo(anyString(), anyInt()).loadLabel(mPackageManager))
+                .thenReturn(themeLabel1)
+                .thenReturn(themeLabel2);
 
         mController.updateState(mPreference);
 
-        verify(mPreference).setValue("Theme1");
+        verify(mPreference).setSummary(themeLabel1);
+        verify(mPreference).setValue(pkg1);
+    }
+
+    @Test
+    public void updateState_themeNull_shouldSetDefaultSummary() throws NameNotFoundException {
+        final String pkg1 = "pkg1.theme1";
+        final String pkg2 = "pkg2.theme2";
+        final String themeLabel1 = "Theme1";
+        final String themeLabel2 = "Theme2";
+        final String[] themes = {pkg1, pkg2};
+        doReturn(null).when(mController).getCurrentTheme();
+        doReturn(themes).when(mController).getAvailableThemes();
+        when(mPackageManager.getApplicationInfo(anyString(), anyInt()).loadLabel(mPackageManager))
+                .thenReturn(themeLabel1)
+                .thenReturn(themeLabel2);
+
+        mController.updateState(mPreference);
+
+        verify(mPreference)
+                .setSummary(RuntimeEnvironment.application.getString(R.string.default_theme));
+        verify(mPreference).setValue(null);
     }
 }
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetailTest.java b/tests/robotests/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetailTest.java
index 7b2d8cc..6fd626b 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetailTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetailTest.java
@@ -16,6 +16,19 @@
 
 package com.android.settings.fuelgauge;
 
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
 import android.app.Activity;
 import android.content.Context;
 import android.content.pm.ApplicationInfo;
@@ -23,6 +36,8 @@
 import android.graphics.drawable.Drawable;
 import android.os.BatteryStats;
 import android.os.Bundle;
+import android.os.UserHandle;
+import android.support.v7.widget.RecyclerView;
 
 import com.android.internal.os.BatterySipper;
 import com.android.internal.os.BatteryStatsHelper;
@@ -36,6 +51,7 @@
 import com.android.settingslib.applications.AppUtils;
 import com.android.settingslib.applications.ApplicationsState;
 import com.android.settingslib.applications.instantapps.InstantAppDataProvider;
+import com.android.settingslib.core.lifecycle.Lifecycle;
 
 import org.junit.After;
 import org.junit.Before;
@@ -51,18 +67,6 @@
 import org.robolectric.annotation.Config;
 import org.robolectric.util.ReflectionHelpers;
 
-import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyInt;
-import static org.mockito.Matchers.anyLong;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
-
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
         shadows = ShadowEntityHeaderController.class)
@@ -123,6 +127,8 @@
 
         ShadowEntityHeaderController.setUseMock(mEntityHeaderController);
         doReturn(mEntityHeaderController).when(mEntityHeaderController)
+                .setRecyclerView(any(RecyclerView.class), any(Lifecycle.class));
+        doReturn(mEntityHeaderController).when(mEntityHeaderController)
                 .setButtonActions(anyInt(), anyInt());
         doReturn(mEntityHeaderController).when(mEntityHeaderController)
                 .setIcon(any(Drawable.class));
@@ -265,4 +271,16 @@
         assertThat(mBundle.getInt(AdvancedPowerUsageDetail.EXTRA_ICON_ID)).isEqualTo(ICON_ID);
         assertThat(mBundle.getString(AdvancedPowerUsageDetail.EXTRA_PACKAGE_NAME)).isEqualTo(null);
     }
+
+    @Test
+    public void testStartBatteryDetailPage_WorkApp() {
+        final int appUid = 1010019;
+        mBatterySipper.mPackages = PACKAGE_NAME;
+        doReturn(appUid).when(mBatterySipper).getUid();
+        AdvancedPowerUsageDetail.startBatteryDetailPage(mTestActivity, null, mBatteryStatsHelper, 0,
+                mBatteryEntry, USAGE_PERCENT);
+
+        verify(mTestActivity).startPreferencePanelAsUser(
+                any(), anyString(), any(), anyInt(), any(), eq(new UserHandle(10)));
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java
index 03759ec..395d36d 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java
@@ -21,6 +21,7 @@
 import android.text.format.DateUtils;
 
 import com.android.internal.os.BatterySipper;
+import com.android.internal.os.BatteryStatsHelper;
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
 import com.android.settings.testutils.FakeFeatureFactory;
@@ -68,6 +69,9 @@
     private static final long TIME_STATE_BACKGROUND = 6000 * UNIT;
     private static final long TIME_FOREGROUND_ACTIVITY_ZERO = 0;
     private static final long TIME_FOREGROUND_ACTIVITY = 100 * DateUtils.MINUTE_IN_MILLIS;
+    private static final long TIME_SINCE_LAST_FULL_CHARGE_MS = 120 * 60 * 1000;
+    private static final long TIME_SINCE_LAST_FULL_CHARGE_US =
+            TIME_SINCE_LAST_FULL_CHARGE_MS * 1000;
 
     private static final int UID = 123;
     private static final long TIME_EXPECTED_FOREGROUND = 1500;
@@ -100,6 +104,8 @@
     private BatterySipper mCellBatterySipper;
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private Context mContext;
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private BatteryStatsHelper mBatteryStatsHelper;
     private BatteryUtils mBatteryUtils;
     private FakeFeatureFactory mFeatureFactory;
     private PowerUsageFeatureProvider mProvider;
@@ -122,6 +128,8 @@
                 anyLong(), anyInt());
         doReturn(TIME_STATE_BACKGROUND).when(mUid).getProcessStateTime(eq(PROCESS_STATE_BACKGROUND),
                 anyLong(), anyInt());
+        when(mBatteryStatsHelper.getStats().computeBatteryRealtime(anyLong(), anyInt())).thenReturn(
+                TIME_SINCE_LAST_FULL_CHARGE_US);
 
         mNormalBatterySipper.drainType = BatterySipper.DrainType.APP;
         mNormalBatterySipper.totalPowerMah = TOTAL_BATTERY_USAGE;
@@ -278,6 +286,12 @@
                 BATTERY_APP_USAGE + BATTERY_SCREEN_USAGE);
     }
 
+    @Test
+    public void testCalculateRunningTimeBasedOnStatsType() {
+        assertThat(mBatteryUtils.calculateRunningTimeBasedOnStatsType(mBatteryStatsHelper,
+                BatteryStats.STATS_SINCE_CHARGED)).isEqualTo(TIME_SINCE_LAST_FULL_CHARGE_MS);
+    }
+
     private BatterySipper createTestSmearBatterySipper(long activityTime, double totalPowerMah,
             int uidCode, boolean isUidNull) {
         final BatterySipper sipper = mock(BatterySipper.class);
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
index efbfbda..56dfbcd 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
@@ -132,6 +132,8 @@
     @Mock
     private LayoutPreference mBatteryLayoutPref;
     @Mock
+    private TextView mBatteryPercentText;
+    @Mock
     private TextView mSummary1;
     @Mock
     private BatteryInfo mBatteryInfo;
@@ -146,7 +148,6 @@
     @Mock
     private ContentResolver mContentResolver;
 
-    private TextView mBatteryPercentText;
     private List<BatterySipper> mUsageList;
     private Context mRealContext;
     private TestFragment mFragment;
@@ -171,7 +172,7 @@
         mLastFullChargePref = new PowerGaugePreference(mRealContext);
         mFragment = spy(new TestFragment(mContext));
         mFragment.initFeatureProvider();
-        mBatteryMeterView = spy(new BatteryMeterView(mRealContext));
+        mBatteryMeterView = new BatteryMeterView(mRealContext);
         mBatteryMeterView.mDrawable = new BatteryMeterView.BatteryMeterDrawable(mRealContext, 0);
         doNothing().when(mFragment).restartBatteryStatsLoader();
 
@@ -194,7 +195,6 @@
         mCellBatterySipper.drainType = BatterySipper.DrainType.CELL;
         mCellBatterySipper.totalPowerMah = POWER_MAH;
 
-        mBatteryPercentText = new TextView(mRealContext);
         when(mBatteryLayoutPref.findViewById(R.id.summary1)).thenReturn(mSummary1);
         when(mBatteryLayoutPref.findViewById(R.id.battery_percent)).thenReturn(mBatteryPercentText);
         when(mBatteryLayoutPref.findViewById(R.id.battery_header_icon))
@@ -288,50 +288,6 @@
     }
 
     @Test
-    public void testInitHeaderPreference_initCorrectly() {
-        mFragment.mBatteryLevel = 100;
-
-        mFragment.initHeaderPreference();
-
-        assertThat(mBatteryMeterView.getBatteryLevel()).isEqualTo(100);
-        assertThat(mBatteryPercentText.getText().toString()).isEqualTo("100%");
-    }
-
-    @Test
-    public void testStartBatteryHeaderAnimationIfNecessary_batteryLevelChanged_animationStarted() {
-        final int prevLevel = 100;
-        final int curLevel = 80;
-
-        mFragment.startBatteryHeaderAnimationIfNecessary(mBatteryMeterView, mBatteryPercentText,
-                prevLevel, curLevel);
-
-        assertThat(mBatteryMeterView.getBatteryLevel()).isEqualTo(curLevel);
-        assertThat(mBatteryPercentText.getText().toString()).isEqualTo("80%");
-    }
-
-    @Test
-    public void testOnSaveInstanceState_saveBatteryLevel() {
-        Bundle bundle = new Bundle();
-        mFragment.mBatteryLevel = BATTERY_LEVEL;
-        // mock it to stop crash in getPreferenceScreen
-        doReturn(null).when(mFragment).getPreferenceScreen();
-
-        mFragment.onSaveInstanceState(bundle);
-
-        assertThat(bundle.getInt(PowerUsageSummary.ARG_BATTERY_LEVEL)).isEqualTo(BATTERY_LEVEL);
-    }
-
-    @Test
-    public void testOnActivityCreated_setBatteryLevel() {
-        Bundle bundle = new Bundle();
-        bundle.putInt(PowerUsageSummary.ARG_BATTERY_LEVEL, BATTERY_LEVEL);
-
-        mFragment.onActivityCreated(bundle);
-
-        assertThat(mFragment.mBatteryLevel).isEqualTo(BATTERY_LEVEL);
-    }
-
-    @Test
     public void testExtractKeyFromSipper_typeAPPUidObjectNull_returnPackageNames() {
         mNormalBatterySipper.uidObj = null;
         mNormalBatterySipper.drainType = BatterySipper.DrainType.APP;
@@ -486,12 +442,6 @@
     }
 
     @Test
-    public void testCalculateRunningTimeBasedOnStatsType() {
-        assertThat(mFragment.calculateRunningTimeBasedOnStatsType()).isEqualTo(
-                TIME_SINCE_LAST_FULL_CHARGE_MS);
-    }
-
-    @Test
     public void testNonIndexableKeys_MatchPreferenceKeys() {
         final Context context = RuntimeEnvironment.application;
         final List<String> niks = PowerUsageSummary.SEARCH_INDEX_DATA_PROVIDER
@@ -536,41 +486,6 @@
     }
 
     @Test
-    public void testUseEnhancedEstimateIfAvailable() {
-        // mock out the provider
-        final long time = 60 * 1000 * 1000;
-        PowerUsageFeatureProvider provider = mFeatureFactory.getPowerUsageFeatureProvider(mContext);
-        when(provider.isEnhancedBatteryPredictionEnabled(any())).thenReturn(true);
-        mFragment.mPowerFeatureProvider = provider;
-        mFragment.mEnhancedEstimate = time;
-
-        mFragment.useEnhancedEstimateIfAvailable(mRealContext, mBatteryInfo);
-
-        // The string that gets returned always has weird whitespacing to make it fit
-        // so we're just going to check that it contains the correct value we care about.
-        assertThat(mBatteryInfo.remainingTimeUs).isEqualTo(time);
-        assertThat(mBatteryInfo.remainingLabel).contains("About 17 hrs");
-    }
-
-    @Test
-    public void testUseEnhancedEstimateIfAvailable_noOpsOnDisabled() {
-        // mock out the provider
-        final long time = 60 * 1000 * 1000;
-        PowerUsageFeatureProvider provider = mFeatureFactory.getPowerUsageFeatureProvider(mContext);
-        when(provider.isEnhancedBatteryPredictionEnabled(any())).thenReturn(false);
-        mFragment.mPowerFeatureProvider = provider;
-        mFragment.mEnhancedEstimate = time;
-        mBatteryInfo.remainingTimeUs = TIME_SINCE_LAST_FULL_CHARGE_US;
-        mBatteryInfo.remainingLabel = TIME_LEFT;
-
-        mFragment.useEnhancedEstimateIfAvailable(mRealContext, mBatteryInfo);
-
-        // check to make sure the values did not change
-        assertThat(mBatteryInfo.remainingTimeUs).isEqualTo(TIME_SINCE_LAST_FULL_CHARGE_US);
-        assertThat(mBatteryInfo.remainingLabel).contains(TIME_LEFT);
-    }
-
-    @Test
     public void testBatteryPredictionLoaderCallbacks_DoesNotCrashOnNull() {
         // Sanity test to check for crash
         mFragment.mBatteryPredictionLoaderCallbacks.onLoadFinished(null, null);
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyLoaderTest.java b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyLoaderTest.java
index 843b747..e6c9955 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyLoaderTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyLoaderTest.java
@@ -49,6 +49,7 @@
     private Context mContext;
     @Mock
     private BatteryStatsHelper mBatteryStatsHelper;
+    @Mock
     private WakeLockAnomalyDetector mWakeLockAnomalyDetector;
     private Anomaly mWakeLockAnomaly;
     private List<Anomaly> mWakeLockAnomalies;
@@ -62,7 +63,6 @@
         FakeFeatureFactory.setupForTest(mContext);
         mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
 
-        mWakeLockAnomalyDetector = spy(new WakeLockAnomalyDetector(mContext));
         mWakeLockAnomalies = new ArrayList<>();
         mWakeLockAnomaly = new Anomaly.Builder()
                 .setType(Anomaly.AnomalyType.WAKE_LOCK)
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyUtilsTest.java b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyUtilsTest.java
index 6303b18..d8652e5 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyUtilsTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyUtilsTest.java
@@ -22,6 +22,7 @@
 import com.android.settings.TestConfig;
 import com.android.settings.fuelgauge.anomaly.action.ForceStopAction;
 import com.android.settings.fuelgauge.anomaly.checker.WakeLockAnomalyDetector;
+import com.android.settings.testutils.shadow.ShadowKeyValueListParserWrapperImpl;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -30,7 +31,8 @@
 import org.robolectric.annotation.Config;
 
 @RunWith(SettingsRobolectricTestRunner.class)
-@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, shadows = {
+        ShadowKeyValueListParserWrapperImpl.class})
 public class AnomalyUtilsTest {
     private AnomalyUtils mAnomalyUtils;
 
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/checker/WakeLockAnomalyDetectorTest.java b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/checker/WakeLockAnomalyDetectorTest.java
index 7010783..b5a4def 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/checker/WakeLockAnomalyDetectorTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/checker/WakeLockAnomalyDetectorTest.java
@@ -39,6 +39,7 @@
 import com.android.settings.TestConfig;
 import com.android.settings.fuelgauge.BatteryUtils;
 import com.android.settings.fuelgauge.anomaly.Anomaly;
+import com.android.settings.fuelgauge.anomaly.AnomalyDetectionPolicy;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -47,6 +48,7 @@
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
+import org.robolectric.util.ReflectionHelpers;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -54,8 +56,9 @@
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
 public class WakeLockAnomalyDetectorTest {
-    private static final long ANOMALY_WAKELOCK_TIME_MS = DateUtils.HOUR_IN_MILLIS;
+    private static final long ANOMALY_WAKELOCK_TIME_MS = 2 * DateUtils.HOUR_IN_MILLIS;
     private static final long NORMAL_WAKELOCK_TIME_MS = DateUtils.SECOND_IN_MILLIS;
+    private static final long WAKELOCK_THRESHOLD_MS = DateUtils.HOUR_IN_MILLIS;
     private static final int ANOMALY_UID = 111;
     private static final int NORMAL_UID = 222;
     @Mock
@@ -82,6 +85,8 @@
     private PackageManager mPackageManager;
     @Mock
     private ApplicationInfo mApplicationInfo;
+    @Mock
+    private AnomalyDetectionPolicy mPolicy;
 
     private ArrayMap<String, BatteryStats.Uid.Wakelock> mAnomalyWakelocks;
     private ArrayMap<String, BatteryStats.Uid.Wakelock> mNormalWakelocks;
@@ -94,6 +99,7 @@
         MockitoAnnotations.initMocks(this);
 
         mContext = spy(RuntimeEnvironment.application);
+        ReflectionHelpers.setField(mPolicy, "wakeLockThreshold", WAKELOCK_THRESHOLD_MS);
 
         doReturn(false).when(mBatteryUtils).shouldHideSipper(any());
         doReturn(mPackageManager).when(mContext).getPackageManager();
@@ -118,7 +124,7 @@
         mUsageList.add(mNormalSipper);
         doReturn(mUsageList).when(mBatteryStatsHelper).getUsageList();
 
-        mWakelockAnomalyDetector = spy(new WakeLockAnomalyDetector(mContext));
+        mWakelockAnomalyDetector = spy(new WakeLockAnomalyDetector(mContext, mPolicy));
         mWakelockAnomalyDetector.mBatteryUtils = mBatteryUtils;
         doReturn(ANOMALY_WAKELOCK_TIME_MS).when(mWakelockAnomalyDetector).getTotalDurationMs(
                 eq(mAnomalyTimer), anyLong());
@@ -137,4 +143,9 @@
 
         assertThat(mAnomalies).containsExactly(anomaly);
     }
+
+    @Test
+    public void testContainsThresholdFromPolicy() {
+        assertThat(mWakelockAnomalyDetector.mWakeLockThresholdMs).isEqualTo(WAKELOCK_THRESHOLD_MS);
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetectorTest.java b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetectorTest.java
new file mode 100644
index 0000000..826694d
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetectorTest.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright (C) 2017 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.fuelgauge.anomaly.checker;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.os.BatteryStats;
+import android.text.format.DateUtils;
+import android.util.ArrayMap;
+
+import com.android.internal.os.BatterySipper;
+import com.android.internal.os.BatteryStatsHelper;
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import com.android.settings.fuelgauge.BatteryUtils;
+import com.android.settings.fuelgauge.anomaly.Anomaly;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class WakeupAlarmAnomalyDetectorTest {
+    private static final int ANOMALY_UID = 111;
+    private static final int NORMAL_UID = 222;
+    private static final long RUNNING_TIME_MS = 2 * DateUtils.HOUR_IN_MILLIS;
+    private static final int ANOMALY_WAKEUP_COUNT = 500;
+    private static final int NORMAL_WAKEUP_COUNT = 50;
+    @Mock
+    private BatteryStatsHelper mBatteryStatsHelper;
+    @Mock
+    private BatterySipper mAnomalySipper;
+    @Mock
+    private BatterySipper mNormalSipper;
+    @Mock
+    private BatteryStats.Uid mAnomalyUid;
+    @Mock
+    private BatteryStats.Uid mNormalUid;
+    @Mock
+    private BatteryUtils mBatteryUtils;
+    @Mock
+    private ApplicationInfo mApplicationInfo;
+    @Mock
+    private BatteryStats.Uid.Pkg mPkg;
+    @Mock
+    private BatteryStats.Counter mCounter;
+
+    private WakeupAlarmAnomalyDetector mWakeupAlarmAnomalyDetector;
+    private Context mContext;
+    private List<BatterySipper> mUsageList;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        mContext = spy(RuntimeEnvironment.application);
+
+        doReturn(false).when(mBatteryUtils).shouldHideSipper(any());
+        doReturn(RUNNING_TIME_MS).when(mBatteryUtils).calculateRunningTimeBasedOnStatsType(any(),
+                anyInt());
+
+        mAnomalySipper.uidObj = mAnomalyUid;
+        doReturn(ANOMALY_UID).when(mAnomalyUid).getUid();
+        mNormalSipper.uidObj = mNormalUid;
+        doReturn(NORMAL_UID).when(mNormalUid).getUid();
+
+        mUsageList = new ArrayList<>();
+        mUsageList.add(mAnomalySipper);
+        mUsageList.add(mNormalSipper);
+        doReturn(mUsageList).when(mBatteryStatsHelper).getUsageList();
+
+        mWakeupAlarmAnomalyDetector = spy(new WakeupAlarmAnomalyDetector(mContext));
+        mWakeupAlarmAnomalyDetector.mBatteryUtils = mBatteryUtils;
+    }
+
+    @Test
+    public void testDetectAnomalies_containsAnomaly_detectIt() {
+        doReturn(ANOMALY_WAKEUP_COUNT).when(mWakeupAlarmAnomalyDetector).getWakeupAlarmCountFromUid(
+                mAnomalyUid);
+        doReturn(NORMAL_WAKEUP_COUNT).when(mWakeupAlarmAnomalyDetector).getWakeupAlarmCountFromUid(
+                mNormalUid);
+        final Anomaly anomaly = new Anomaly.Builder()
+                .setUid(ANOMALY_UID)
+                .setType(Anomaly.AnomalyType.WAKEUP_ALARM)
+                .build();
+
+        List<Anomaly> mAnomalies = mWakeupAlarmAnomalyDetector.detectAnomalies(mBatteryStatsHelper);
+
+        assertThat(mAnomalies).containsExactly(anomaly);
+    }
+
+    @Test
+    public void testGetWakeupAlarmCountFromUid_countCorrect() {
+        final ArrayMap<String, BatteryStats.Uid.Pkg> packageStats = new ArrayMap<>();
+        final ArrayMap<String, BatteryStats.Counter> alarms = new ArrayMap<>();
+        doReturn(alarms).when(mPkg).getWakeupAlarmStats();
+        doReturn(NORMAL_WAKEUP_COUNT).when(mCounter).getCountLocked(anyInt());
+        doReturn(packageStats).when(mAnomalyUid).getPackageStats();
+        packageStats.put("", mPkg);
+        alarms.put("1", mCounter);
+        alarms.put("2", mCounter);
+
+        assertThat(mWakeupAlarmAnomalyDetector.getWakeupAlarmCountFromUid(mAnomalyUid)).isEqualTo(
+                2 * NORMAL_WAKEUP_COUNT);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/language/LanguageAndInputSettingsTest.java b/tests/robotests/src/com/android/settings/language/LanguageAndInputSettingsTest.java
index 195e007..873b408 100644
--- a/tests/robotests/src/com/android/settings/language/LanguageAndInputSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/language/LanguageAndInputSettingsTest.java
@@ -37,6 +37,7 @@
 import com.android.settings.dashboard.SummaryLoader;
 import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settings.testutils.XmlTestUtils;
+import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settings.testutils.shadow.ShadowSecureSettings;
 import com.android.settingslib.core.lifecycle.Lifecycle;
 import com.android.settingslib.core.lifecycle.LifecycleObserver;
diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowEntityHeaderController.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowEntityHeaderController.java
index bccb297..4ec48d3 100644
--- a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowEntityHeaderController.java
+++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowEntityHeaderController.java
@@ -16,8 +16,8 @@
 
 package com.android.settings.testutils.shadow;
 
+import android.app.Activity;
 import android.app.Fragment;
-import android.content.Context;
 import android.view.View;
 
 import com.android.settings.widget.EntityHeaderController;
@@ -41,7 +41,7 @@
     }
 
     @Implementation
-    public static EntityHeaderController newInstance(Context context, Fragment fragment,
+    public static EntityHeaderController newInstance(Activity activity, Fragment fragment,
             View header) {
         return sMockController;
     }
diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowKeyValueListParserWrapperImpl.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowKeyValueListParserWrapperImpl.java
new file mode 100644
index 0000000..0af9c30
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowKeyValueListParserWrapperImpl.java
@@ -0,0 +1,20 @@
+package com.android.settings.testutils.shadow;
+
+import com.android.settings.fuelgauge.anomaly.KeyValueListParserWrapperImpl;
+
+import org.robolectric.annotation.Implementation;
+import org.robolectric.annotation.Implements;
+
+/**
+ * Shadow for {@link KeyValueListParserWrapperImpl} so we could implement
+ * {@link #getBoolean(String, boolean)} that doesn't support in the current
+ * robolectric
+ */
+@Implements(KeyValueListParserWrapperImpl.class)
+public class ShadowKeyValueListParserWrapperImpl {
+
+    @Implementation
+    public boolean getBoolean(String key, boolean defaultValue) {
+        return defaultValue;
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/widget/ActionBarShadowControllerTest.java b/tests/robotests/src/com/android/settings/widget/ActionBarShadowControllerTest.java
new file mode 100644
index 0000000..b8f7820
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/widget/ActionBarShadowControllerTest.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2017 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.widget;
+
+
+import android.app.ActionBar;
+import android.app.Activity;
+import android.support.v7.widget.RecyclerView;
+
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+import com.android.settingslib.core.lifecycle.LifecycleObserver;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.annotation.Config;
+import org.robolectric.util.ReflectionHelpers;
+
+import java.util.List;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class ActionBarShadowControllerTest {
+
+    @Mock
+    private RecyclerView mRecyclerView;
+    @Mock
+    private Activity mActivity;
+    @Mock
+    private ActionBar mActionBar;
+    private Lifecycle mLifecycle;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        when(mActivity.getActionBar()).thenReturn(mActionBar);
+        mLifecycle = new Lifecycle();
+    }
+
+    @Test
+    public void attachToRecyclerView_shouldAddScrollWatcherAndUpdateActionBar() {
+        when(mRecyclerView.canScrollVertically(-1)).thenReturn(false);
+
+        ActionBarShadowController.attachToRecyclerView(mActivity, mLifecycle, mRecyclerView);
+
+        verify(mActionBar).setElevation(0);
+    }
+
+
+    @Test
+    public void attachToRecyclerView_lifecycleChange_shouldAttachDetach() {
+        ActionBarShadowController.attachToRecyclerView(mActivity, mLifecycle, mRecyclerView);
+
+        List<LifecycleObserver> observers = ReflectionHelpers.getField(mLifecycle, "mObservers");
+        assertThat(observers).hasSize(1);
+        verify(mRecyclerView).addOnScrollListener(any());
+
+        mLifecycle.onStop();
+        verify(mRecyclerView).removeOnScrollListener(any());
+
+        mLifecycle.onStart();
+        verify(mRecyclerView, times(2)).addOnScrollListener(any());
+    }
+
+}
diff --git a/tests/robotests/src/com/android/settings/widget/EntityHeaderControllerTest.java b/tests/robotests/src/com/android/settings/widget/EntityHeaderControllerTest.java
index e6c742a..e386282 100644
--- a/tests/robotests/src/com/android/settings/widget/EntityHeaderControllerTest.java
+++ b/tests/robotests/src/com/android/settings/widget/EntityHeaderControllerTest.java
@@ -66,7 +66,6 @@
     @Mock
     private Fragment mFragment;
 
-    private FakeFeatureFactory mFeatureFactory;
     private Context mShadowContext;
     private LayoutInflater mLayoutInflater;
     private PackageInfo mInfo;
@@ -76,8 +75,8 @@
     public void setUp() {
         MockitoAnnotations.initMocks(this);
         FakeFeatureFactory.setupForTest(mContext);
-        mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
         mShadowContext = RuntimeEnvironment.application;
+        when(mActivity.getApplicationContext()).thenReturn(mShadowContext);
         when(mContext.getApplicationContext()).thenReturn(mContext);
         when(mFragment.getContext()).thenReturn(mShadowContext);
         mLayoutInflater = LayoutInflater.from(mShadowContext);
@@ -87,7 +86,7 @@
 
     @Test
     public void testBuildView_constructedWithoutView_shouldCreateNewView() {
-        mController = EntityHeaderController.newInstance(mShadowContext, mFragment, null);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, null);
         View view = mController.done(mActivity);
 
         assertThat(view).isNotNull();
@@ -95,7 +94,7 @@
 
     @Test
     public void testBuildView_withContext_shouldBuildPreference() {
-        mController = EntityHeaderController.newInstance(mShadowContext, mFragment, null);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, null);
         Preference preference = mController.done(mActivity, mShadowContext);
 
         assertThat(preference instanceof LayoutPreference).isTrue();
@@ -104,7 +103,7 @@
     @Test
     public void testBuildView_constructedWithView_shouldReturnSameView() {
         View inputView = mLayoutInflater.inflate(R.layout.settings_entity_header, null /* root */);
-        mController = EntityHeaderController.newInstance(mShadowContext, mFragment, inputView);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, inputView);
         View view = mController.done(mActivity);
 
         assertThat(view).isSameAs(inputView);
@@ -118,7 +117,7 @@
         final TextView label = header.findViewById(R.id.entity_header_title);
         final TextView version = header.findViewById(R.id.entity_header_summary);
 
-        mController = EntityHeaderController.newInstance(mShadowContext, mFragment, header);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, header);
         mController.setLabel(testString);
         mController.setSummary(testString);
         mController.setIcon(mShadowContext.getDrawable(R.drawable.ic_add));
@@ -136,10 +135,11 @@
         info.activityInfo.name = "321";
         final View appLinks = mLayoutInflater
                 .inflate(R.layout.settings_entity_header, null /* root */);
+        when(mActivity.getApplicationContext()).thenReturn(mContext);
         when(mContext.getPackageManager().resolveActivity(any(Intent.class), anyInt()))
                 .thenReturn(info);
 
-        mController = EntityHeaderController.newInstance(mContext, mFragment, appLinks);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, appLinks);
         mController.setButtonActions(
                 EntityHeaderController.ActionType.ACTION_APP_PREFERENCE,
                 EntityHeaderController.ActionType.ACTION_NONE);
@@ -164,7 +164,7 @@
         when(mContext.getPackageManager().resolveActivity(any(Intent.class), anyInt()))
                 .thenReturn(null);
 
-        mController = EntityHeaderController.newInstance(mContext, mFragment, appLinks);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, appLinks);
         mController.setButtonActions(
                 EntityHeaderController.ActionType.ACTION_APP_PREFERENCE,
                 EntityHeaderController.ActionType.ACTION_NONE);
@@ -181,7 +181,7 @@
         final View appLinks = mLayoutInflater
                 .inflate(R.layout.settings_entity_header, null /* root */);
 
-        mController = EntityHeaderController.newInstance(mContext, mFragment, appLinks);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, appLinks);
         mController.setPackageName(null)
                 .setButtonActions(
                         EntityHeaderController.ActionType.ACTION_APP_INFO,
@@ -200,7 +200,7 @@
                 .inflate(R.layout.settings_entity_header, null /* root */);
         when(mFragment.getActivity()).thenReturn(mock(Activity.class));
 
-        mController = EntityHeaderController.newInstance(mContext, mFragment, appLinks);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, appLinks);
         mController.setPackageName("123")
                 .setUid(UserHandle.USER_SYSTEM)
                 .setButtonActions(
@@ -221,7 +221,7 @@
         when(mFragment.getActivity()).thenReturn(mock(Activity.class));
         when(mContext.getString(eq(R.string.application_info_label))).thenReturn("App Info");
 
-        mController = EntityHeaderController.newInstance(mContext, mFragment, appLinks);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, appLinks);
         mController.setPackageName("123")
                 .setUid(UserHandle.USER_SYSTEM)
                 .setButtonActions(
@@ -229,8 +229,8 @@
                         EntityHeaderController.ActionType.ACTION_NOTIF_PREFERENCE);
         mController.done(mActivity);
 
-        assertThat(appLinks.findViewById(android.R.id.button1).getContentDescription())
-                .isEqualTo("App Info");
+        assertThat(appLinks.findViewById(android.R.id.button1).getContentDescription().toString())
+                .isEqualTo("App info");
     }
 
     @Test
@@ -238,7 +238,7 @@
         final View appLinks = mLayoutInflater
                 .inflate(R.layout.settings_entity_header, null /* root */);
 
-        mController = EntityHeaderController.newInstance(mContext, mFragment, appLinks);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, appLinks);
         mController.setAppNotifPrefIntent(new Intent())
                 .setButtonActions(
                         EntityHeaderController.ActionType.ACTION_NOTIF_PREFERENCE,
@@ -257,7 +257,7 @@
     public void instantApps_normalAppsDontGetLabel() {
         final View header = mLayoutInflater.inflate(
                 R.layout.settings_entity_header, null /* root */);
-        mController = EntityHeaderController.newInstance(mContext, mFragment, header);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, header);
         mController.done(mActivity);
 
         assertThat(header.findViewById(R.id.install_type).getVisibility())
@@ -269,7 +269,7 @@
     public void instantApps_expectedHeaderItem() {
         final View header = mLayoutInflater.inflate(
                 R.layout.settings_entity_header, null /* root */);
-        mController = EntityHeaderController.newInstance(mContext, mFragment, header);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, header);
         mController.setIsInstantApp(true);
         mController.done(mActivity);
         TextView label = header.findViewById(R.id.install_type);
@@ -283,7 +283,7 @@
 
     @Test
     public void styleActionBar_invalidObjects_shouldNotCrash() {
-        mController = EntityHeaderController.newInstance(mShadowContext, mFragment, null);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, null);
         mController.styleActionBar(null);
 
         when(mActivity.getActionBar()).thenReturn(null);
@@ -296,7 +296,7 @@
     public void styleActionBar_setElevationAndBackground() {
         final ActionBar actionBar = mActivity.getActionBar();
 
-        mController = EntityHeaderController.newInstance(mShadowContext, mFragment, null);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, null);
         mController.styleActionBar(mActivity);
 
         verify(actionBar).setElevation(0);
@@ -307,7 +307,7 @@
 
     @Test
     public void initAppHeaderController_appHeaderNull_useFragmentContext() {
-        mController = EntityHeaderController.newInstance(mContext, mFragment, null);
+        mController = EntityHeaderController.newInstance(mActivity, mFragment, null);
 
         // Fragment.getContext() is invoked to inflate the view
         verify(mFragment).getContext();