Merge "Update package name for PictureAndPictureSettings"
diff --git a/res/color/battery_icon_color_error.xml b/res/color/battery_icon_color_error.xml
index 3a71aae..99c7d7d 100644
--- a/res/color/battery_icon_color_error.xml
+++ b/res/color/battery_icon_color_error.xml
@@ -14,6 +14,6 @@
      limitations under the License.
 -->
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
-    <item android:alpha="@*android:dimen/secondary_content_alpha_material_dark"
+    <item android:alpha="?android:attr/secondaryContentAlpha"
           android:color="?android:attr/colorError"/>
 </selector>
\ No newline at end of file
diff --git a/res/xml/app_info_settings.xml b/res/xml/app_info_settings.xml
index e2fb2e4..664210b 100644
--- a/res/xml/app_info_settings.xml
+++ b/res/xml/app_info_settings.xml
@@ -105,6 +105,33 @@
         android:title="@string/sms_application_title"
         android:summary="@string/summary_placeholder" />
 
+    <!-- Advanced apps settings -->
+    <PreferenceCategory
+        android:key="advanced_app_info"
+        android:title="@string/advanced_apps">
+
+        <Preference
+            android:key="system_alert_window"
+            android:title="@string/draw_overlay"
+            android:summary="@string/summary_placeholder" />
+
+        <Preference
+            android:key="write_settings_apps"
+            android:title="@string/write_settings"
+            android:summary="@string/summary_placeholder" />
+
+        <Preference
+            android:key="picture_in_picture"
+            android:title="@string/picture_in_picture_app_detail_title"
+            android:summary="@string/summary_placeholder" />
+
+        <Preference
+            android:key="install_other_apps"
+            android:title="@string/install_other_apps"
+            android:summary="@string/summary_placeholder" />
+
+    </PreferenceCategory>
+
     <Preference
         android:key="app_version"
         android:selectable="false"
diff --git a/res/xml/draw_overlay_permissions_details.xml b/res/xml/draw_overlay_permissions_details.xml
new file mode 100644
index 0000000..699086f
--- /dev/null
+++ b/res/xml/draw_overlay_permissions_details.xml
@@ -0,0 +1,33 @@
+<?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:key="draw_overlay_permission_detail_settings"
+    android:title="@string/draw_overlay">
+
+    <SwitchPreference
+        android:key="app_ops_settings_switch"
+        android:title="@string/permit_draw_overlay"/>
+
+    <Preference
+        android:key="app_ops_settings_preference"
+        android:title="@string/app_overlay_permission_preference"/>
+
+    <Preference
+        android:summary="@string/allow_overlay_description"
+        android:selectable="false"/>
+
+</PreferenceScreen>
diff --git a/res/xml/external_sources_details.xml b/res/xml/external_sources_details.xml
index 9e79c10..ea2abdc 100644
--- a/res/xml/external_sources_details.xml
+++ b/res/xml/external_sources_details.xml
@@ -15,7 +15,6 @@
 -->
 
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
-        xmlns:settings="http://schemas.android.com/apk/res-auto"
         android:title="@string/install_other_apps">
 
     <com.android.settingslib.RestrictedSwitchPreference
diff --git a/res/xml/picture_in_picture_permissions_details.xml b/res/xml/picture_in_picture_permissions_details.xml
new file mode 100644
index 0000000..c215c9d
--- /dev/null
+++ b/res/xml/picture_in_picture_permissions_details.xml
@@ -0,0 +1,29 @@
+<?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:key="picture_in_picture_permission_detail_settings"
+    android:title="@string/picture_in_picture_app_detail_title">
+
+    <SwitchPreference
+        android:key="app_ops_settings_switch"
+        android:title="@string/picture_in_picture_app_detail_switch"/>
+
+    <Preference
+        android:summary="@string/picture_in_picture_app_detail_summary"
+        android:selectable="false"/>
+
+</PreferenceScreen>
diff --git a/res/xml/write_system_settings_permissions_details.xml b/res/xml/write_system_settings_permissions_details.xml
new file mode 100644
index 0000000..39d69833
--- /dev/null
+++ b/res/xml/write_system_settings_permissions_details.xml
@@ -0,0 +1,33 @@
+<?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:key="write_system_settings_permission_detail_settings"
+    android:title="@string/write_settings">
+
+    <SwitchPreference
+        android:key="app_ops_settings_switch"
+        android:title="@string/permit_write_settings"/>
+
+    <Preference
+        android:key="app_ops_settings_preference"
+        android:title="@string/write_settings_preference"/>
+
+    <Preference
+        android:summary="@string/write_settings_description"
+        android:selectable="false"/>
+
+</PreferenceScreen>
diff --git a/src/com/android/settings/DeviceInfoSettings.java b/src/com/android/settings/DeviceInfoSettings.java
index f99b894..74ce5d0 100644
--- a/src/com/android/settings/DeviceInfoSettings.java
+++ b/src/com/android/settings/DeviceInfoSettings.java
@@ -84,7 +84,7 @@
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         final Bundle arguments = getArguments();
-        if (FeatureFlagUtils.isEnabled(getContext(), DEVICE_INFO_V2) || true) {
+        if (FeatureFlagUtils.isEnabled(getContext(), DEVICE_INFO_V2)) {
             // Do not override initial expand children count if we come from
             // search (EXTRA_FRAGMENT_ARG_KEY is set) - we need to display every if entry point
             // is search.
@@ -119,7 +119,7 @@
 
     @Override
     protected int getPreferenceScreenResId() {
-        return FeatureFlagUtils.isEnabled(getContext(), DEVICE_INFO_V2) || true
+        return FeatureFlagUtils.isEnabled(getContext(), DEVICE_INFO_V2)
                 ? R.xml.device_info_settings_v2 : R.xml.device_info_settings;
     }
 
@@ -156,7 +156,7 @@
 
     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
             Activity activity, Fragment fragment, Lifecycle lifecycle) {
-        if (FeatureFlagUtils.isEnabled(context, DEVICE_INFO_V2) || true) {
+        if (FeatureFlagUtils.isEnabled(context, DEVICE_INFO_V2)) {
             final List<AbstractPreferenceController> controllers = new ArrayList<>();
             // Device name
 
@@ -220,7 +220,7 @@
                 public List<SearchIndexableResource> getXmlResourcesToIndex(
                         Context context, boolean enabled) {
                     final SearchIndexableResource sir = new SearchIndexableResource(context);
-                    sir.xmlResId = FeatureFlagUtils.isEnabled(context, DEVICE_INFO_V2) || true
+                    sir.xmlResId = FeatureFlagUtils.isEnabled(context, DEVICE_INFO_V2)
                             ? R.xml.device_info_settings_v2 : R.xml.device_info_settings;
                     return Arrays.asList(sir);
                 }
diff --git a/src/com/android/settings/applications/AppInfoDashboardFragment.java b/src/com/android/settings/applications/AppInfoDashboardFragment.java
index a725781..d7d91f3 100755
--- a/src/com/android/settings/applications/AppInfoDashboardFragment.java
+++ b/src/com/android/settings/applications/AppInfoDashboardFragment.java
@@ -18,7 +18,6 @@
 
 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
 
-import android.Manifest.permission;
 import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.AlertDialog;
@@ -47,7 +46,6 @@
 import android.os.UserManager;
 import android.support.annotation.VisibleForTesting;
 import android.support.v7.preference.Preference;
-import android.support.v7.preference.Preference.OnPreferenceClickListener;
 import android.support.v7.preference.PreferenceCategory;
 import android.support.v7.preference.PreferenceScreen;
 import android.text.TextUtils;
@@ -58,7 +56,6 @@
 import android.view.View;
 import android.webkit.IWebViewUpdateService;
 
-import com.android.internal.logging.nano.MetricsProto;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.DeviceAdminAdd;
 import com.android.settings.R;
@@ -78,11 +75,10 @@
 import com.android.settings.applications.appinfo.DefaultHomeShortcutPreferenceController;
 import com.android.settings.applications.appinfo.DefaultPhoneShortcutPreferenceController;
 import com.android.settings.applications.appinfo.DefaultSmsShortcutPreferenceController;
-import com.android.settings.applications.defaultapps.DefaultBrowserPreferenceController;
-import com.android.settings.applications.defaultapps.DefaultEmergencyPreferenceController;
-import com.android.settings.applications.defaultapps.DefaultHomePreferenceController;
-import com.android.settings.applications.defaultapps.DefaultPhonePreferenceController;
-import com.android.settings.applications.defaultapps.DefaultSmsPreferenceController;
+import com.android.settings.applications.appinfo.DrawOverlayDetailPreferenceController;
+import com.android.settings.applications.appinfo.ExternalSourceDetailPreferenceController;
+import com.android.settings.applications.appinfo.PictureInPictureDetailPreferenceController;
+import com.android.settings.applications.appinfo.WriteSystemSettingsPreferenceController;
 import com.android.settings.applications.instantapps.InstantAppButtonsController;
 import com.android.settings.applications.manageapplications.ManageApplications;
 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
@@ -90,6 +86,7 @@
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.widget.ActionButtonPreference;
 import com.android.settings.widget.EntityHeaderController;
+import com.android.settings.widget.PreferenceCategoryController;
 import com.android.settings.wrapper.DevicePolicyManagerWrapper;
 import com.android.settingslib.RestrictedLockUtils;
 import com.android.settingslib.applications.AppUtils;
@@ -97,7 +94,6 @@
 import com.android.settingslib.applications.ApplicationsState.AppEntry;
 import com.android.settingslib.core.AbstractPreferenceController;
 import com.android.settingslib.core.lifecycle.Lifecycle;
-import com.android.settingslib.wrapper.PackageManagerWrapper;
 
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
@@ -149,6 +145,7 @@
     public static final String ARG_PACKAGE_UID = "uid";
 
     protected static final boolean localLOGV = false;
+    private static final String KEY_ADVANCED_APP_INFO_CATEGORY = "advanced_app_info";
 
     private EnforcedAdmin mAppsControlDisallowedAdmin;
     private boolean mAppsControlDisallowedBySystem;
@@ -358,11 +355,6 @@
         if (!refreshUi()) {
             setIntentAndFinish(true, true);
         }
-
-        if (mFinishing) {
-            return;
-        }
-        updateDynamicPrefs();
     }
 
     @Override
@@ -404,6 +396,17 @@
         controllers.add(new DefaultEmergencyShortcutPreferenceController(context, packageName));
         controllers.add(new DefaultSmsShortcutPreferenceController(context, packageName));
 
+        final List<AbstractPreferenceController> advancedAppInfoControllers = new ArrayList<>();
+        advancedAppInfoControllers.add(new DrawOverlayDetailPreferenceController(context, this));
+        advancedAppInfoControllers.add(new WriteSystemSettingsPreferenceController(context, this));
+        advancedAppInfoControllers.add(
+                new PictureInPictureDetailPreferenceController(context, this, packageName));
+        advancedAppInfoControllers.add(
+                new ExternalSourceDetailPreferenceController(context, this, packageName));
+        controllers.addAll(advancedAppInfoControllers);
+        controllers.add(new PreferenceCategoryController(
+                context, KEY_ADVANCED_APP_INFO_CATEGORY, advancedAppInfoControllers));
+
         return controllers;
     }
 
@@ -415,6 +418,9 @@
     }
 
     public PackageInfo getPackageInfo() {
+        if (mAppEntry == null) {
+            retrieveAppEntry();
+        }
         return mPackageInfo;
     }
 
@@ -603,7 +609,8 @@
         return false;
     }
 
-    protected boolean refreshUi() {
+    @VisibleForTesting
+    boolean refreshUi() {
         retrieveAppEntry();
         if (mAppEntry == null) {
             return false; // onCreate must have failed, make sure to exit
@@ -782,10 +789,6 @@
         }
     }
 
-    private void startAppInfoFragment(Class<?> fragment, int title) {
-        startAppInfoFragment(fragment, title, this, mAppEntry);
-    }
-
     public static void startAppInfoFragment(Class<?> fragment, int title,
             SettingsPreferenceFragment caller, AppEntry appEntry) {
         // start new fragment to display extended information
@@ -871,100 +874,10 @@
         if (UserManager.get(getContext()).isManagedProfile()) {
             return;
         }
-        final PreferenceScreen screen = getPreferenceScreen();
-        final Context context = getContext();
-
-        // Get the package info with the activities
-        PackageInfo packageInfoWithActivities = null;
-        try {
-            packageInfoWithActivities = mPm.getPackageInfoAsUser(mPackageName,
-                    PackageManager.GET_ACTIVITIES, UserHandle.myUserId());
-        } catch (NameNotFoundException e) {
-            Log.e(TAG, "Exception while retrieving the package info of " + mPackageName, e);
-        }
-
-        boolean hasDrawOverOtherApps = hasPermission(permission.SYSTEM_ALERT_WINDOW);
-        boolean hasWriteSettings = hasPermission(permission.WRITE_SETTINGS);
-        boolean hasPictureInPictureActivities = (packageInfoWithActivities != null) &&
-                PictureInPictureSettings.checkPackageHasPictureInPictureActivities(
-                        packageInfoWithActivities.packageName,
-                        packageInfoWithActivities.activities);
-        boolean isPotentialAppSource = isPotentialAppSource();
-        if (hasDrawOverOtherApps || hasWriteSettings || hasPictureInPictureActivities ||
-                isPotentialAppSource) {
-            PreferenceCategory category = new PreferenceCategory(getPrefContext());
-            category.setTitle(R.string.advanced_apps);
-            screen.addPreference(category);
-
-            if (hasDrawOverOtherApps) {
-                Preference pref = new Preference(getPrefContext());
-                pref.setTitle(R.string.draw_overlay);
-                pref.setKey("system_alert_window");
-                pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
-                    @Override
-                    public boolean onPreferenceClick(Preference preference) {
-                        startAppInfoFragment(DrawOverlayDetails.class, R.string.draw_overlay);
-                        return true;
-                    }
-                });
-                category.addPreference(pref);
-            }
-            if (hasWriteSettings) {
-                Preference pref = new Preference(getPrefContext());
-                pref.setTitle(R.string.write_settings);
-                pref.setKey("write_settings_apps");
-                pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
-                    @Override
-                    public boolean onPreferenceClick(Preference preference) {
-                        startAppInfoFragment(WriteSettingsDetails.class, R.string.write_settings);
-                        return true;
-                    }
-                });
-                category.addPreference(pref);
-            }
-            if (hasPictureInPictureActivities) {
-                Preference pref = new Preference(getPrefContext());
-                pref.setTitle(R.string.picture_in_picture_app_detail_title);
-                pref.setKey("picture_in_picture");
-                pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
-                    @Override
-                    public boolean onPreferenceClick(Preference preference) {
-                        AppInfoBase.startAppInfoFragment(PictureInPictureDetails.class,
-                                R.string.picture_in_picture_app_detail_title, mPackageName,
-                                mPackageInfo.applicationInfo.uid, AppInfoDashboardFragment.this,
-                                -1, getMetricsCategory());
-                        return true;
-                    }
-                });
-                category.addPreference(pref);
-            }
-            if (isPotentialAppSource) {
-                Preference pref = new Preference(getPrefContext());
-                pref.setTitle(R.string.install_other_apps);
-                pref.setKey("install_other_apps");
-                pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
-                    @Override
-                    public boolean onPreferenceClick(Preference preference) {
-                        startAppInfoFragment(ExternalSourcesDetails.class,
-                                R.string.install_other_apps);
-                        return true;
-                    }
-                });
-                category.addPreference(pref);
-            }
-        }
-
-        addAppInstallerInfoPref(screen);
+        addAppInstallerInfoPref(getPreferenceScreen());
         maybeAddInstantAppButtons();
     }
 
-    private boolean isPotentialAppSource() {
-        AppStateInstallAppsBridge.InstallAppsState appState =
-                new AppStateInstallAppsBridge(getContext(), null, null)
-                        .createInstallAppsStateFor(mPackageName, mPackageInfo.applicationInfo.uid);
-        return appState.isPotentialAppSource();
-    }
-
     private void addAppInstallerInfoPref(PreferenceScreen screen) {
         String installerPackageName =
                 AppStoreUtil.getInstallerPackageName(getContext(), mPackageName);
@@ -1008,39 +921,6 @@
         }
     }
 
-    private boolean hasPermission(String permission) {
-        if (mPackageInfo == null || mPackageInfo.requestedPermissions == null) {
-            return false;
-        }
-        for (int i = 0; i < mPackageInfo.requestedPermissions.length; i++) {
-            if (mPackageInfo.requestedPermissions[i].equals(permission)) {
-                return true;
-            }
-        }
-        return false;
-    }
-
-    private void updateDynamicPrefs() {
-        final Context context = getContext();
-        Preference pref = findPreference("system_alert_window");
-        if (pref != null) {
-            pref.setSummary(DrawOverlayDetails.getSummary(getContext(), mAppEntry));
-        }
-        pref = findPreference("picture_in_picture");
-        if (pref != null) {
-            pref.setSummary(PictureInPictureDetails.getPreferenceSummary(getContext(),
-                    mPackageInfo.applicationInfo.uid, mPackageName));
-        }
-        pref = findPreference("write_settings_apps");
-        if (pref != null) {
-            pref.setSummary(WriteSettingsDetails.getSummary(getContext(), mAppEntry));
-        }
-        pref = findPreference("install_other_apps");
-        if (pref != null) {
-            pref.setSummary(ExternalSourcesDetails.getPreferenceSummary(getContext(), mAppEntry));
-        }
-    }
-
     private void onPackageRemoved() {
         getActivity().finishActivity(SUB_INFO_FRAGMENT);
         getActivity().finishAndRemoveTask();
@@ -1112,7 +992,7 @@
             return mPackageName;
         }
         final Bundle args = getArguments();
-        String mPackageName = (args != null) ? args.getString(ARG_PACKAGE_NAME) : null;
+        mPackageName = (args != null) ? args.getString(ARG_PACKAGE_NAME) : null;
         if (mPackageName == null) {
             Intent intent = (args == null) ?
                     getActivity().getIntent() : (Intent) args.getParcelable("intent");
@@ -1229,7 +1109,7 @@
 
         @Override
         public int getMetricsCategory() {
-            return MetricsProto.MetricsEvent.DIALOG_APP_INFO_ACTION;
+            return MetricsEvent.DIALOG_APP_INFO_ACTION;
         }
 
         @Override
diff --git a/src/com/android/settings/applications/AppStateInstallAppsBridge.java b/src/com/android/settings/applications/AppStateInstallAppsBridge.java
index 0c3582e..5b9ded6 100644
--- a/src/com/android/settings/applications/AppStateInstallAppsBridge.java
+++ b/src/com/android/settings/applications/AppStateInstallAppsBridge.java
@@ -90,7 +90,7 @@
         return mAppOpsManager.checkOpNoThrow(appOpCode, uid, packageName);
     }
 
-    InstallAppsState createInstallAppsStateFor(String packageName, int uid) {
+    public InstallAppsState createInstallAppsStateFor(String packageName, int uid) {
         final InstallAppsState appState = new InstallAppsState();
         appState.permissionRequested = hasRequestedAppOpPermission(
                 Manifest.permission.REQUEST_INSTALL_PACKAGES, packageName);
diff --git a/src/com/android/settings/applications/InstalledAppDetails.java b/src/com/android/settings/applications/InstalledAppDetails.java
index 6f94015..2098bd6 100755
--- a/src/com/android/settings/applications/InstalledAppDetails.java
+++ b/src/com/android/settings/applications/InstalledAppDetails.java
@@ -75,6 +75,11 @@
 import com.android.settings.SettingsActivity;
 import com.android.settings.SettingsPreferenceFragment;
 import com.android.settings.Utils;
+import com.android.settings.applications.appinfo.DrawOverlayDetails;
+import com.android.settings.applications.appinfo.ExternalSourcesDetails;
+import com.android.settings.applications.appinfo.PictureInPictureDetails;
+import com.android.settings.applications.appinfo.PictureInPictureSettings;
+import com.android.settings.applications.appinfo.WriteSettingsDetails;
 import com.android.settings.applications.defaultapps.DefaultBrowserPreferenceController;
 import com.android.settings.applications.defaultapps.DefaultEmergencyPreferenceController;
 import com.android.settings.applications.defaultapps.DefaultHomePreferenceController;
diff --git a/src/com/android/settings/applications/appinfo/AppBatteryPreferenceController.java b/src/com/android/settings/applications/appinfo/AppBatteryPreferenceController.java
index d341d53..017afe7 100644
--- a/src/com/android/settings/applications/appinfo/AppBatteryPreferenceController.java
+++ b/src/com/android/settings/applications/appinfo/AppBatteryPreferenceController.java
@@ -17,7 +17,6 @@
 package com.android.settings.applications.appinfo;
 
 import android.app.LoaderManager;
-import android.app.slice.Slice;
 import android.content.Context;
 import android.content.Loader;
 import android.content.pm.PackageInfo;
@@ -82,11 +81,6 @@
     }
 
     @Override
-    public Slice getSettingSlice() {
-        return null;
-    }
-
-    @Override
     public void displayPreference(PreferenceScreen screen) {
         super.displayPreference(screen);
         mPreference = screen.findPreference(getPreferenceKey());
diff --git a/src/com/android/settings/applications/appinfo/AppInfoPreferenceControllerBase.java b/src/com/android/settings/applications/appinfo/AppInfoPreferenceControllerBase.java
index 0d6c038..eac0a0c 100644
--- a/src/com/android/settings/applications/appinfo/AppInfoPreferenceControllerBase.java
+++ b/src/com/android/settings/applications/appinfo/AppInfoPreferenceControllerBase.java
@@ -16,7 +16,6 @@
 
 package com.android.settings.applications.appinfo;
 
-import android.app.slice.Slice;
 import android.content.Context;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceScreen;
@@ -51,11 +50,6 @@
     }
 
     @Override
-    public Slice getSettingSlice() {
-        return null;
-    }
-
-    @Override
     public void displayPreference(PreferenceScreen screen) {
         super.displayPreference(screen);
         mPreference = screen.findPreference(getPreferenceKey());
diff --git a/src/com/android/settings/applications/appinfo/AppMemoryPreferenceController.java b/src/com/android/settings/applications/appinfo/AppMemoryPreferenceController.java
index 2a20f80..3943041 100644
--- a/src/com/android/settings/applications/appinfo/AppMemoryPreferenceController.java
+++ b/src/com/android/settings/applications/appinfo/AppMemoryPreferenceController.java
@@ -17,7 +17,6 @@
 package com.android.settings.applications.appinfo;
 
 import android.app.Activity;
-import android.app.slice.Slice;
 import android.content.Context;
 import android.content.pm.PackageInfo;
 import android.os.AsyncTask;
@@ -111,11 +110,6 @@
     }
 
     @Override
-    public Slice getSettingSlice() {
-        return null;
-    }
-
-    @Override
     public void displayPreference(PreferenceScreen screen) {
         super.displayPreference(screen);
         mPreference = screen.findPreference(getPreferenceKey());
diff --git a/src/com/android/settings/applications/appinfo/AppPermissionPreferenceController.java b/src/com/android/settings/applications/appinfo/AppPermissionPreferenceController.java
index bd309c6..815e8d8 100644
--- a/src/com/android/settings/applications/appinfo/AppPermissionPreferenceController.java
+++ b/src/com/android/settings/applications/appinfo/AppPermissionPreferenceController.java
@@ -23,13 +23,11 @@
 import android.icu.text.ListFormatter;
 import android.support.annotation.VisibleForTesting;
 import android.support.v7.preference.Preference;
-import android.support.v7.preference.PreferenceScreen;
 import android.util.Log;
 
 import com.android.settings.R;
 import com.android.settings.applications.AppInfoDashboardFragment;
 import com.android.settingslib.applications.PermissionsSummaryHelper;
-import com.android.settingslib.core.AbstractPreferenceController;
 
 import java.util.ArrayList;
 import java.util.List;
diff --git a/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBase.java b/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBase.java
index 3311daa..fa67ec8 100644
--- a/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBase.java
+++ b/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBase.java
@@ -51,11 +51,6 @@
     }
 
     @Override
-    public Slice getSettingSlice() {
-        return null;
-    }
-
-    @Override
     public void updateState(Preference preference) {
         preference.setSummary(isDefaultApp() ? R.string.yes : R.string.no);
     }
diff --git a/src/com/android/settings/applications/appinfo/DrawOverlayDetailPreferenceController.java b/src/com/android/settings/applications/appinfo/DrawOverlayDetailPreferenceController.java
new file mode 100644
index 0000000..314d799
--- /dev/null
+++ b/src/com/android/settings/applications/appinfo/DrawOverlayDetailPreferenceController.java
@@ -0,0 +1,70 @@
+/*
+ * 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.appinfo;
+
+import static android.Manifest.permission.SYSTEM_ALERT_WINDOW;
+
+import android.content.Context;
+import android.content.pm.PackageInfo;
+import android.os.UserManager;
+import android.support.annotation.VisibleForTesting;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.SettingsPreferenceFragment;
+import com.android.settings.applications.AppInfoDashboardFragment;
+
+public class DrawOverlayDetailPreferenceController extends AppInfoPreferenceControllerBase {
+
+    private static final String KEY = "system_alert_window";
+
+    public DrawOverlayDetailPreferenceController(Context context, AppInfoDashboardFragment parent) {
+        super(context, parent, KEY);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (UserManager.get(mContext).isManagedProfile()) {
+            return DISABLED_FOR_USER;
+        }
+        final PackageInfo packageInfo = mParent.getPackageInfo();
+        if (packageInfo == null || packageInfo.requestedPermissions == null) {
+            return DISABLED_FOR_USER;
+        }
+        for (int i = 0; i < packageInfo.requestedPermissions.length; i++) {
+            if (packageInfo.requestedPermissions[i].equals(SYSTEM_ALERT_WINDOW)) {
+                return AVAILABLE;
+            }
+        }
+        return DISABLED_FOR_USER;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        preference.setSummary(getSummary());
+    }
+
+    @Override
+    protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() {
+        return DrawOverlayDetails.class;
+    }
+
+    @VisibleForTesting
+    CharSequence getSummary() {
+        return DrawOverlayDetails.getSummary(mContext, mParent.getAppEntry());
+    }
+
+}
diff --git a/src/com/android/settings/applications/DrawOverlayDetails.java b/src/com/android/settings/applications/appinfo/DrawOverlayDetails.java
similarity index 90%
rename from src/com/android/settings/applications/DrawOverlayDetails.java
rename to src/com/android/settings/applications/appinfo/DrawOverlayDetails.java
index 78f1c08..e8400a0 100644
--- a/src/com/android/settings/applications/DrawOverlayDetails.java
+++ b/src/com/android/settings/applications/appinfo/DrawOverlayDetails.java
@@ -13,13 +13,15 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.android.settings.applications;
+package com.android.settings.applications.appinfo;
 
 import android.app.AlertDialog;
 import android.app.AppOpsManager;
 import android.content.ActivityNotFoundException;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.provider.Settings;
@@ -29,12 +31,13 @@
 import android.support.v7.preference.Preference.OnPreferenceClickListener;
 import android.util.Log;
 
-import android.view.Window;
 import android.view.WindowManager;
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.R;
+import com.android.settings.applications.AppInfoWithHeader;
 import com.android.settings.applications.AppStateAppOpsBridge.PermissionState;
+import com.android.settings.applications.AppStateOverlayBridge;
 import com.android.settings.applications.AppStateOverlayBridge.OverlayState;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settingslib.applications.ApplicationsState.AppEntry;
@@ -44,7 +47,6 @@
 
     private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch";
     private static final String KEY_APP_OPS_SETTINGS_PREFS = "app_ops_settings_preference";
-    private static final String KEY_APP_OPS_SETTINGS_DESC = "app_ops_settings_description";
     private static final String LOG_TAG = "DrawOverlayDetails";
 
     private static final int [] APP_OPS_OP_CODE = {
@@ -57,7 +59,6 @@
     private AppOpsManager mAppOpsManager;
     private SwitchPreference mSwitchPref;
     private Preference mOverlayPrefs;
-    private Preference mOverlayDesc;
     private Intent mSettingsIntent;
     private OverlayState mOverlayState;
 
@@ -70,16 +71,9 @@
         mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
 
         // find preferences
-        addPreferencesFromResource(R.xml.app_ops_permissions_details);
+        addPreferencesFromResource(R.xml.draw_overlay_permissions_details);
         mSwitchPref = (SwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH);
         mOverlayPrefs = findPreference(KEY_APP_OPS_SETTINGS_PREFS);
-        mOverlayDesc = findPreference(KEY_APP_OPS_SETTINGS_DESC);
-
-        // set title/summary for all of them
-        getPreferenceScreen().setTitle(R.string.draw_overlay);
-        mSwitchPref.setTitle(R.string.permit_draw_overlay);
-        mOverlayPrefs.setTitle(R.string.app_overlay_permission_preference);
-        mOverlayDesc.setSummary(R.string.allow_overlay_description);
 
         // install event listeners
         mSwitchPref.setOnPreferenceChangeListener(this);
@@ -116,7 +110,8 @@
                 try {
                     getActivity().startActivityAsUser(mSettingsIntent, new UserHandle(mUserId));
                 } catch (ActivityNotFoundException e) {
-                    Log.w(LOG_TAG, "Unable to launch app draw overlay settings " + mSettingsIntent, e);
+                    Log.w(LOG_TAG, "Unable to launch app draw overlay settings " + mSettingsIntent,
+                            e);
                 }
             }
             return true;
@@ -161,7 +156,14 @@
         // you cannot ask a user to grant you a permission you did not have!
         mSwitchPref.setEnabled(mOverlayState.permissionDeclared && mOverlayState.controlEnabled);
         mOverlayPrefs.setEnabled(isAllowed);
-        getPreferenceScreen().removePreference(mOverlayPrefs);
+
+        ResolveInfo resolveInfo = mPm.resolveActivityAsUser(mSettingsIntent,
+                PackageManager.GET_META_DATA, mUserId);
+        if (resolveInfo == null) {
+            if (findPreference(KEY_APP_OPS_SETTINGS_PREFS) != null) {
+                getPreferenceScreen().removePreference(mOverlayPrefs);
+            }
+        }
 
         return true;
     }
diff --git a/src/com/android/settings/applications/appinfo/ExternalSourceDetailPreferenceController.java b/src/com/android/settings/applications/appinfo/ExternalSourceDetailPreferenceController.java
new file mode 100644
index 0000000..4ac67ed
--- /dev/null
+++ b/src/com/android/settings/applications/appinfo/ExternalSourceDetailPreferenceController.java
@@ -0,0 +1,71 @@
+/*
+ * 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.appinfo;
+
+import android.content.Context;
+import android.os.UserManager;
+import android.support.annotation.VisibleForTesting;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.SettingsPreferenceFragment;
+import com.android.settings.applications.AppInfoDashboardFragment;
+import com.android.settings.applications.AppStateInstallAppsBridge;
+
+public class ExternalSourceDetailPreferenceController extends AppInfoPreferenceControllerBase {
+
+    private static final String KEY = "install_other_apps";
+
+    private final String mPackageName;
+
+    public ExternalSourceDetailPreferenceController(Context context,
+            AppInfoDashboardFragment parent, String packageName) {
+        super(context, parent, KEY);
+        mPackageName = packageName;
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (UserManager.get(mContext).isManagedProfile()) {
+            return DISABLED_FOR_USER;
+        }
+        return isPotentialAppSource() ? AVAILABLE : DISABLED_FOR_USER;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        preference.setSummary(getPreferenceSummary());
+    }
+
+    @Override
+    protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() {
+        return ExternalSourcesDetails.class;
+    }
+
+    @VisibleForTesting
+    CharSequence getPreferenceSummary() {
+        return ExternalSourcesDetails.getPreferenceSummary(mContext, mParent.getAppEntry());
+    }
+
+    @VisibleForTesting
+    boolean isPotentialAppSource() {
+        AppStateInstallAppsBridge.InstallAppsState appState =
+                new AppStateInstallAppsBridge(mContext, null, null).createInstallAppsStateFor(
+                        mPackageName, mParent.getPackageInfo().applicationInfo.uid);
+        return appState.isPotentialAppSource();
+    }
+
+}
diff --git a/src/com/android/settings/applications/ExternalSourcesDetails.java b/src/com/android/settings/applications/appinfo/ExternalSourcesDetails.java
similarity index 96%
rename from src/com/android/settings/applications/ExternalSourcesDetails.java
rename to src/com/android/settings/applications/appinfo/ExternalSourcesDetails.java
index 5cd3c44..0400066 100644
--- a/src/com/android/settings/applications/ExternalSourcesDetails.java
+++ b/src/com/android/settings/applications/appinfo/ExternalSourcesDetails.java
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.android.settings.applications;
+package com.android.settings.applications.appinfo;
 
 import static android.app.Activity.RESULT_CANCELED;
 import static android.app.Activity.RESULT_OK;
@@ -30,6 +30,8 @@
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.R;
 import com.android.settings.Settings;
+import com.android.settings.applications.AppInfoWithHeader;
+import com.android.settings.applications.AppStateInstallAppsBridge;
 import com.android.settings.applications.AppStateInstallAppsBridge.InstallAppsState;
 import com.android.settingslib.RestrictedSwitchPreference;
 import com.android.settingslib.applications.ApplicationsState.AppEntry;
diff --git a/src/com/android/settings/applications/appinfo/PictureInPictureDetailPreferenceController.java b/src/com/android/settings/applications/appinfo/PictureInPictureDetailPreferenceController.java
new file mode 100644
index 0000000..aea6bae
--- /dev/null
+++ b/src/com/android/settings/applications/appinfo/PictureInPictureDetailPreferenceController.java
@@ -0,0 +1,86 @@
+/*
+ * 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.appinfo;
+
+import android.content.Context;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.support.annotation.VisibleForTesting;
+import android.support.v7.preference.Preference;
+import android.util.Log;
+
+import com.android.settings.SettingsPreferenceFragment;
+import com.android.settings.applications.AppInfoDashboardFragment;
+
+public class PictureInPictureDetailPreferenceController extends AppInfoPreferenceControllerBase {
+
+    private static final String KEY = "picture_in_picture";
+    private static final String TAG = "PicInPicDetailControl";
+
+    private final PackageManager mPackageManager;
+    private final String mPackageName;
+
+    public PictureInPictureDetailPreferenceController(Context context,
+            AppInfoDashboardFragment parent, String packageName) {
+        super(context, parent, KEY);
+        mPackageManager = context.getPackageManager();
+        mPackageName = packageName;
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (UserManager.get(mContext).isManagedProfile()) {
+            return DISABLED_FOR_USER;
+        }
+        return hasPictureInPictureActivites() ? AVAILABLE : DISABLED_FOR_USER;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        preference.setSummary(getPreferenceSummary());
+    }
+
+    @Override
+    protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() {
+        return PictureInPictureDetails.class;
+    }
+
+    @VisibleForTesting
+    boolean hasPictureInPictureActivites() {
+        // Get the package info with the activities
+        PackageInfo packageInfoWithActivities = null;
+        try {
+            packageInfoWithActivities = mPackageManager.getPackageInfoAsUser(mPackageName,
+                    PackageManager.GET_ACTIVITIES, UserHandle.myUserId());
+        } catch (PackageManager.NameNotFoundException e) {
+            Log.e(TAG, "Exception while retrieving the package info of " + mPackageName, e);
+        }
+
+        return packageInfoWithActivities != null
+                && PictureInPictureSettings.checkPackageHasPictureInPictureActivities(
+                packageInfoWithActivities.packageName,
+                packageInfoWithActivities.activities);
+    }
+
+    @VisibleForTesting
+    int getPreferenceSummary() {
+        return PictureInPictureDetails.getPreferenceSummary(mContext,
+                mParent.getPackageInfo().applicationInfo.uid, mPackageName);
+    }
+}
diff --git a/src/com/android/settings/applications/PictureInPictureDetails.java b/src/com/android/settings/applications/appinfo/PictureInPictureDetails.java
similarity index 81%
rename from src/com/android/settings/applications/PictureInPictureDetails.java
rename to src/com/android/settings/applications/appinfo/PictureInPictureDetails.java
index a886a3d..1d9a544 100644
--- a/src/com/android/settings/applications/PictureInPictureDetails.java
+++ b/src/com/android/settings/applications/appinfo/PictureInPictureDetails.java
@@ -13,14 +13,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.android.settings.applications;
+package com.android.settings.applications.appinfo;
 
 import android.app.AlertDialog;
 import android.app.AppOpsManager;
 import android.content.Context;
-import android.content.Intent;
 import android.os.Bundle;
-import android.provider.Settings;
 import android.support.v14.preference.SwitchPreference;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.Preference.OnPreferenceChangeListener;
@@ -28,6 +26,7 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.R;
+import com.android.settings.applications.AppInfoWithHeader;
 import com.android.settings.overlay.FeatureFactory;
 
 import static android.app.AppOpsManager.MODE_ALLOWED;
@@ -38,42 +37,31 @@
         implements OnPreferenceChangeListener {
 
     private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch";
-    private static final String KEY_APP_OPS_SETTINGS_PREFS = "app_ops_settings_preference";
-    private static final String KEY_APP_OPS_SETTINGS_DESC = "app_ops_settings_description";
     private static final String LOG_TAG = "PictureInPictureDetails";
 
     private SwitchPreference mSwitchPref;
-    private Preference mOverlayDesc;
-    private Intent mSettingsIntent;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
         // find preferences
-        addPreferencesFromResource(R.xml.app_ops_permissions_details);
+        addPreferencesFromResource(R.xml.picture_in_picture_permissions_details);
         mSwitchPref = (SwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH);
-        mOverlayDesc = findPreference(KEY_APP_OPS_SETTINGS_DESC);
-        getPreferenceScreen().removePreference(findPreference(KEY_APP_OPS_SETTINGS_PREFS));
 
         // set title/summary for all of them
-        getPreferenceScreen().setTitle(R.string.picture_in_picture_app_detail_title);
         mSwitchPref.setTitle(R.string.picture_in_picture_app_detail_switch);
-        mOverlayDesc.setSummary(R.string.picture_in_picture_app_detail_summary);
 
         // install event listeners
         mSwitchPref.setOnPreferenceChangeListener(this);
-
-        mSettingsIntent = new Intent(Intent.ACTION_MAIN)
-                .setAction(Settings.ACTION_PICTURE_IN_PICTURE_SETTINGS);
     }
 
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         if (preference == mSwitchPref) {
             logSpecialPermissionChange((Boolean) newValue, mPackageName);
-            setEnterPipStateForPackage(getActivity(), mPackageInfo.applicationInfo.uid, mPackageName,
-                    (Boolean) newValue);
+            setEnterPipStateForPackage(getActivity(), mPackageInfo.applicationInfo.uid,
+                    mPackageName, (Boolean) newValue);
             return true;
         }
         return false;
@@ -121,7 +109,7 @@
      * @return the summary for the current state of whether the app associated with the given
      *         {@param packageName} is allowed to enter picture-in-picture.
      */
-    static int getPreferenceSummary(Context context, int uid, String packageName) {
+    public static int getPreferenceSummary(Context context, int uid, String packageName) {
         final boolean enabled = PictureInPictureDetails.getEnterPipStateForPackage(context, uid,
                 packageName);
         return enabled ? R.string.app_permission_summary_allowed
diff --git a/src/com/android/settings/applications/PictureInPictureSettings.java b/src/com/android/settings/applications/appinfo/PictureInPictureSettings.java
similarity index 97%
rename from src/com/android/settings/applications/PictureInPictureSettings.java
rename to src/com/android/settings/applications/appinfo/PictureInPictureSettings.java
index 01d14f4..28cdf68 100644
--- a/src/com/android/settings/applications/PictureInPictureSettings.java
+++ b/src/com/android/settings/applications/appinfo/PictureInPictureSettings.java
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.android.settings.applications;
+package com.android.settings.applications.appinfo;
 
 import static android.content.pm.PackageManager.GET_ACTIVITIES;
 
@@ -37,6 +37,7 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.R;
+import com.android.settings.applications.AppInfoBase;
 import com.android.settings.notification.EmptyTextSettings;
 import com.android.settings.widget.AppPreference;
 import com.android.settings.wrapper.ActivityInfoWrapper;
@@ -95,7 +96,7 @@
      * @return true if the package has any activities that declare that they support
      *         picture-in-picture.
      */
-    static boolean checkPackageHasPictureInPictureActivities(String packageName,
+    public static boolean checkPackageHasPictureInPictureActivities(String packageName,
             ActivityInfo[] activities) {
         ActivityInfoWrapper[] wrappedActivities = null;
         if (activities != null) {
diff --git a/src/com/android/settings/applications/WriteSettingsDetails.java b/src/com/android/settings/applications/appinfo/WriteSettingsDetails.java
similarity index 90%
rename from src/com/android/settings/applications/WriteSettingsDetails.java
rename to src/com/android/settings/applications/appinfo/WriteSettingsDetails.java
index 50e6948..a65de32 100644
--- a/src/com/android/settings/applications/WriteSettingsDetails.java
+++ b/src/com/android/settings/applications/appinfo/WriteSettingsDetails.java
@@ -13,13 +13,15 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.android.settings.applications;
+package com.android.settings.applications.appinfo;
 
 import android.app.AlertDialog;
 import android.app.AppOpsManager;
 import android.content.ActivityNotFoundException;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.provider.Settings;
@@ -31,7 +33,9 @@
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.R;
+import com.android.settings.applications.AppInfoWithHeader;
 import com.android.settings.applications.AppStateAppOpsBridge.PermissionState;
+import com.android.settings.applications.AppStateWriteSettingsBridge;
 import com.android.settings.applications.AppStateWriteSettingsBridge.WriteSettingsState;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settingslib.applications.ApplicationsState.AppEntry;
@@ -42,7 +46,6 @@
     private static final String KEY_APP_OPS_PREFERENCE_SCREEN = "app_ops_preference_screen";
     private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch";
     private static final String KEY_APP_OPS_SETTINGS_PREFS = "app_ops_settings_preference";
-    private static final String KEY_APP_OPS_SETTINGS_DESC = "app_ops_settings_description";
     private static final String LOG_TAG = "WriteSettingsDetails";
 
     private static final int [] APP_OPS_OP_CODE = {
@@ -55,7 +58,6 @@
     private AppOpsManager mAppOpsManager;
     private SwitchPreference mSwitchPref;
     private Preference mWriteSettingsPrefs;
-    private Preference mWriteSettingsDesc;
     private Intent mSettingsIntent;
     private WriteSettingsState mWriteSettingsState;
 
@@ -67,15 +69,9 @@
         mAppBridge = new AppStateWriteSettingsBridge(context, mState, null);
         mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
 
-        addPreferencesFromResource(R.xml.app_ops_permissions_details);
+        addPreferencesFromResource(R.xml.write_system_settings_permissions_details);
         mSwitchPref = (SwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH);
         mWriteSettingsPrefs = findPreference(KEY_APP_OPS_SETTINGS_PREFS);
-        mWriteSettingsDesc = findPreference(KEY_APP_OPS_SETTINGS_DESC);
-
-        getPreferenceScreen().setTitle(R.string.write_settings);
-        mSwitchPref.setTitle(R.string.permit_write_settings);
-        mWriteSettingsPrefs.setTitle(R.string.write_settings_preference);
-        mWriteSettingsDesc.setSummary(R.string.write_settings_description);
 
         mSwitchPref.setOnPreferenceChangeListener(this);
         mWriteSettingsPrefs.setOnPreferenceClickListener(this);
@@ -147,8 +143,13 @@
         // you can't ask a user for a permission you didn't even declare!
         mSwitchPref.setEnabled(mWriteSettingsState.permissionDeclared);
         mWriteSettingsPrefs.setEnabled(canWrite);
-        if (getPreferenceScreen().findPreference(KEY_APP_OPS_SETTINGS_PREFS) != null) {
-            getPreferenceScreen().removePreference(mWriteSettingsPrefs);
+
+        ResolveInfo resolveInfo = mPm.resolveActivityAsUser(mSettingsIntent,
+                PackageManager.GET_META_DATA, mUserId);
+        if (resolveInfo == null) {
+            if (getPreferenceScreen().findPreference(KEY_APP_OPS_SETTINGS_PREFS) != null) {
+                getPreferenceScreen().removePreference(mWriteSettingsPrefs);
+            }
         }
         return true;
     }
diff --git a/src/com/android/settings/applications/appinfo/WriteSystemSettingsPreferenceController.java b/src/com/android/settings/applications/appinfo/WriteSystemSettingsPreferenceController.java
new file mode 100644
index 0000000..55b181a
--- /dev/null
+++ b/src/com/android/settings/applications/appinfo/WriteSystemSettingsPreferenceController.java
@@ -0,0 +1,71 @@
+/*
+ * 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.appinfo;
+
+import static android.Manifest.permission.WRITE_SETTINGS;
+
+import android.content.Context;
+import android.content.pm.PackageInfo;
+import android.os.UserManager;
+import android.support.annotation.VisibleForTesting;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.SettingsPreferenceFragment;
+import com.android.settings.applications.AppInfoDashboardFragment;
+
+public class WriteSystemSettingsPreferenceController extends AppInfoPreferenceControllerBase {
+
+    private static final String KEY = "write_settings_apps";
+
+    public WriteSystemSettingsPreferenceController(Context context,
+            AppInfoDashboardFragment parent) {
+        super(context, parent, KEY);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (UserManager.get(mContext).isManagedProfile()) {
+            return DISABLED_FOR_USER;
+        }
+        final PackageInfo packageInfo = mParent.getPackageInfo();
+        if (packageInfo == null || packageInfo.requestedPermissions == null) {
+            return DISABLED_FOR_USER;
+        }
+        for (int i = 0; i < packageInfo.requestedPermissions.length; i++) {
+            if (packageInfo.requestedPermissions[i].equals(WRITE_SETTINGS)) {
+                return AVAILABLE;
+            }
+        }
+        return DISABLED_FOR_USER;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        preference.setSummary(getSummary());
+    }
+
+    @Override
+    protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() {
+        return WriteSettingsDetails.class;
+    }
+
+    @VisibleForTesting
+    CharSequence getSummary() {
+        return WriteSettingsDetails.getSummary(mContext, mParent.getAppEntry());
+    }
+
+}
diff --git a/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceController.java b/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceController.java
index a7d65d3..94aa608 100644
--- a/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceController.java
+++ b/src/com/android/settings/applications/defaultapps/DefaultHomePreferenceController.java
@@ -100,7 +100,7 @@
 
         Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
                 .setPackage(packageName)
-                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
         return mPackageManager.queryIntentActivities(intent, 0).size() == 1 ? intent : null;
     }
 
diff --git a/src/com/android/settings/applications/manageapplications/ManageApplications.java b/src/com/android/settings/applications/manageapplications/ManageApplications.java
index e9d105d..067e167 100644
--- a/src/com/android/settings/applications/manageapplications/ManageApplications.java
+++ b/src/com/android/settings/applications/manageapplications/ManageApplications.java
@@ -89,14 +89,14 @@
 import com.android.settings.applications.AppStateWriteSettingsBridge;
 import com.android.settings.applications.AppStorageSettings;
 import com.android.settings.applications.DefaultAppSettings;
-import com.android.settings.applications.DrawOverlayDetails;
-import com.android.settings.applications.ExternalSourcesDetails;
 import com.android.settings.applications.InstalledAppCounter;
 import com.android.settings.applications.InstalledAppDetails;
 import com.android.settings.applications.NotificationApps;
 import com.android.settings.applications.UsageAccessDetails;
-import com.android.settings.applications.WriteSettingsDetails;
 import com.android.settings.applications.AppInfoDashboardFragment;
+import com.android.settings.applications.appinfo.DrawOverlayDetails;
+import com.android.settings.applications.appinfo.ExternalSourcesDetails;
+import com.android.settings.applications.appinfo.WriteSettingsDetails;
 import com.android.settings.core.FeatureFlags;
 import com.android.settings.core.InstrumentedPreferenceFragment;
 import com.android.settings.dashboard.SummaryLoader;
diff --git a/src/com/android/settings/core/BasePreferenceController.java b/src/com/android/settings/core/BasePreferenceController.java
index b3d9878..01d98b8 100644
--- a/src/com/android/settings/core/BasePreferenceController.java
+++ b/src/com/android/settings/core/BasePreferenceController.java
@@ -14,14 +14,10 @@
 package com.android.settings.core;
 
 import android.annotation.IntDef;
-import android.app.slice.Slice;
 import android.content.Context;
-import android.support.v7.preference.Preference;
 import android.text.TextUtils;
 import android.util.Log;
 
-
-import com.android.settings.core.PreferenceControllerMixin;
 import com.android.settings.search.ResultPayload;
 import com.android.settings.search.SearchIndexableRaw;
 import com.android.settingslib.core.AbstractPreferenceController;
@@ -88,11 +84,6 @@
     @AvailabilityStatus
     public abstract int getAvailabilityStatus();
 
-    /**
-     * @return A slice for the corresponding setting.
-     */
-    public abstract Slice getSettingSlice();
-
     @Override
     public String getPreferenceKey() {
         return mPreferenceKey;
@@ -150,10 +141,4 @@
     public ResultPayload getResultPayload() {
         return null;
     }
-
-    // TODO (b/69380366) Add Method to get preference UI
-
-    // TODO (b/69380464) Add method to get intent
-
-    // TODO (b/69380560) Add method to get broadcast intent
 }
\ No newline at end of file
diff --git a/src/com/android/settings/core/FeatureFlags.java b/src/com/android/settings/core/FeatureFlags.java
index 197876f..9ceef47 100644
--- a/src/com/android/settings/core/FeatureFlags.java
+++ b/src/com/android/settings/core/FeatureFlags.java
@@ -26,4 +26,5 @@
     public static final String APP_INFO_V2 = "settings_app_info_v2";
     public static final String CONNECTED_DEVICE_V2 = "settings_connected_device_v2";
     public static final String BATTERY_SETTINGS_V2 = "settings_battery_v2";
+    public static final String BATTERY_DISPLAY_APP_LIST = "settings_battery_display_app_list";
 }
diff --git a/src/com/android/settings/core/TogglePreferenceController.java b/src/com/android/settings/core/TogglePreferenceController.java
index 03106d3..99d2ecc 100644
--- a/src/com/android/settings/core/TogglePreferenceController.java
+++ b/src/com/android/settings/core/TogglePreferenceController.java
@@ -13,7 +13,6 @@
  */
 package com.android.settings.core;
 
-import android.app.slice.Slice;
 import android.content.Context;
 import android.support.v14.preference.SwitchPreference;
 import android.support.v7.preference.Preference;
@@ -55,10 +54,4 @@
         setChecked(auto);
         return true;
     }
-
-    @Override
-    public Slice getSettingSlice() {
-        // TODO
-        return null;
-    }
 }
\ No newline at end of file
diff --git a/src/com/android/settings/core/gateway/SettingsGateway.java b/src/com/android/settings/core/gateway/SettingsGateway.java
index 7720b48..ecef57e 100644
--- a/src/com/android/settings/core/gateway/SettingsGateway.java
+++ b/src/com/android/settings/core/gateway/SettingsGateway.java
@@ -40,19 +40,19 @@
 import com.android.settings.accounts.UserAndAccountDashboardFragment;
 import com.android.settings.applications.AppAndNotificationDashboardFragment;
 import com.android.settings.applications.DefaultAppSettings;
-import com.android.settings.applications.DrawOverlayDetails;
-import com.android.settings.applications.ExternalSourcesDetails;
 import com.android.settings.applications.InstalledAppDetails;
 import com.android.settings.applications.ManageDomainUrls;
 import com.android.settings.applications.NotificationApps;
-import com.android.settings.applications.PictureInPictureDetails;
-import com.android.settings.applications.PictureInPictureSettings;
 import com.android.settings.applications.ProcessStatsSummary;
 import com.android.settings.applications.ProcessStatsUi;
 import com.android.settings.applications.UsageAccessDetails;
 import com.android.settings.applications.VrListenerSettings;
-import com.android.settings.applications.WriteSettingsDetails;
 import com.android.settings.applications.AppInfoDashboardFragment;
+import com.android.settings.applications.appinfo.DrawOverlayDetails;
+import com.android.settings.applications.appinfo.ExternalSourcesDetails;
+import com.android.settings.applications.appinfo.PictureInPictureDetails;
+import com.android.settings.applications.appinfo.PictureInPictureSettings;
+import com.android.settings.applications.appinfo.WriteSettingsDetails;
 import com.android.settings.applications.assist.ManageAssist;
 import com.android.settings.applications.manageapplications.ManageApplications;
 import com.android.settings.bluetooth.BluetoothDeviceDetailsFragment;
diff --git a/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java b/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java
index 4f4753a..3fc2fb2 100644
--- a/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java
+++ b/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java
@@ -92,7 +92,7 @@
     }
 
     private static boolean isV2Enabled(Context context) {
-        return FeatureFlagUtils.isEnabled(context, SUGGESTIONS_V2) || true;
+        return FeatureFlagUtils.isEnabled(context, SUGGESTIONS_V2);
     }
 
     @Override
diff --git a/src/com/android/settings/development/featureflags/FeatureFlagsPreferenceController.java b/src/com/android/settings/development/featureflags/FeatureFlagsPreferenceController.java
index 7c00591..fe565ed 100644
--- a/src/com/android/settings/development/featureflags/FeatureFlagsPreferenceController.java
+++ b/src/com/android/settings/development/featureflags/FeatureFlagsPreferenceController.java
@@ -17,7 +17,6 @@
 package com.android.settings.development.featureflags;
 
 import android.content.Context;
-import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceScreen;
 import android.util.FeatureFlagUtils;
 
@@ -68,14 +67,8 @@
         }
         mScreen.removeAll();
         final Context prefContext = mScreen.getContext();
-        for (String prefixedFeature : featureMap.keySet()) {
-            if (prefixedFeature.startsWith(FeatureFlagUtils.FFLAG_PREFIX)
-                    && !prefixedFeature.startsWith(FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX)) {
-                final String feature = prefixedFeature.substring(
-                        FeatureFlagUtils.FFLAG_PREFIX.length());
-                final Preference pref = new FeatureFlagPreference(prefContext, feature);
-                mScreen.addPreference(pref);
-            }
+        for (String feature : featureMap.keySet()) {
+            mScreen.addPreference(new FeatureFlagPreference(prefContext, feature));
         }
     }
 }
diff --git a/src/com/android/settings/deviceinfo/DeviceModelPreferenceController.java b/src/com/android/settings/deviceinfo/DeviceModelPreferenceController.java
index 89df7cc..7934ad7 100644
--- a/src/com/android/settings/deviceinfo/DeviceModelPreferenceController.java
+++ b/src/com/android/settings/deviceinfo/DeviceModelPreferenceController.java
@@ -51,7 +51,7 @@
         super.displayPreference(screen);
         final Preference pref = screen.findPreference(KEY_DEVICE_MODEL);
         if (pref != null) {
-            if (FeatureFlagUtils.isEnabled(mContext, FeatureFlags.DEVICE_INFO_V2) || true) {
+            if (FeatureFlagUtils.isEnabled(mContext, FeatureFlags.DEVICE_INFO_V2)) {
                 pref.setSummary(mContext.getResources().getString(R.string.model_summary,
                         getDeviceModel()));
             } else {
diff --git a/src/com/android/settings/deviceinfo/HardwareInfoDialogFragment.java b/src/com/android/settings/deviceinfo/HardwareInfoDialogFragment.java
index d1c6447..26f1ac2 100644
--- a/src/com/android/settings/deviceinfo/HardwareInfoDialogFragment.java
+++ b/src/com/android/settings/deviceinfo/HardwareInfoDialogFragment.java
@@ -59,7 +59,7 @@
                 DeviceModelPreferenceController.getDeviceModel());
 
         // Serial number
-        if (FeatureFlagUtils.isEnabled(getContext(), FeatureFlags.DEVICE_INFO_V2) || true) {
+        if (FeatureFlagUtils.isEnabled(getContext(), FeatureFlags.DEVICE_INFO_V2)) {
             setText(content, R.id.serial_number_label, R.id.serial_number_value, getSerialNumber());
         } else {
             content.findViewById(R.id.serial_number_label).setVisibility(View.GONE);
diff --git a/src/com/android/settings/fuelgauge/BatteryAppListPreferenceController.java b/src/com/android/settings/fuelgauge/BatteryAppListPreferenceController.java
new file mode 100644
index 0000000..5d95dd2
--- /dev/null
+++ b/src/com/android/settings/fuelgauge/BatteryAppListPreferenceController.java
@@ -0,0 +1,484 @@
+/*
+ * 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;
+
+import android.app.Activity;
+import android.app.Fragment;
+import android.content.Context;
+import android.graphics.drawable.Drawable;
+import android.os.BatteryStats;
+import android.os.Handler;
+import android.os.Message;
+import android.os.Process;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.support.annotation.VisibleForTesting;
+import android.support.v14.preference.PreferenceFragment;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceGroup;
+import android.support.v7.preference.PreferenceManager;
+import android.support.v7.preference.PreferenceScreen;
+import android.text.TextUtils;
+import android.text.format.DateUtils;
+import android.util.ArrayMap;
+import android.util.FeatureFlagUtils;
+import android.util.Log;
+import android.util.SparseArray;
+
+import com.android.internal.os.BatterySipper;
+import com.android.internal.os.BatterySipper.DrainType;
+import com.android.internal.os.BatteryStatsHelper;
+import com.android.internal.os.PowerProfile;
+import com.android.settings.R;
+import com.android.settings.SettingsActivity;
+import com.android.settings.core.FeatureFlags;
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settings.Utils;
+import com.android.settings.core.instrumentation.MetricsFeatureProvider;
+import com.android.settings.fuelgauge.anomaly.Anomaly;
+import com.android.settings.overlay.FeatureFactory;
+import com.android.settingslib.core.AbstractPreferenceController;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+import com.android.settingslib.core.lifecycle.LifecycleObserver;
+import com.android.settingslib.core.lifecycle.events.OnDestroy;
+import com.android.settingslib.core.lifecycle.events.OnPause;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Controller that update the battery header view
+ */
+public class BatteryAppListPreferenceController extends AbstractPreferenceController
+        implements PreferenceControllerMixin, LifecycleObserver, OnPause, OnDestroy {
+    private static final boolean USE_FAKE_DATA = true;
+    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 STATS_TYPE = BatteryStats.STATS_SINCE_CHARGED;
+
+    private final String mPreferenceKey;
+    @VisibleForTesting
+    PreferenceGroup mAppListGroup;
+    private BatteryStatsHelper mBatteryStatsHelper;
+    private ArrayMap<String, Preference> mPreferenceCache;
+    @VisibleForTesting
+    BatteryUtils mBatteryUtils;
+    private UserManager mUserManager;
+    private SettingsActivity mActivity;
+    private PreferenceFragment mFragment;
+    private Context mPrefContext;
+    SparseArray<List<Anomaly>> mAnomalySparseArray;
+
+    private Handler mHandler = new Handler() {
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case BatteryEntry.MSG_UPDATE_NAME_ICON:
+                    BatteryEntry entry = (BatteryEntry) msg.obj;
+                    PowerGaugePreference pgp =
+                            (PowerGaugePreference) mAppListGroup.findPreference(
+                                    Integer.toString(entry.sipper.uidObj.getUid()));
+                    if (pgp != null) {
+                        final int userId = UserHandle.getUserId(entry.sipper.getUid());
+                        final UserHandle userHandle = new UserHandle(userId);
+                        pgp.setIcon(mUserManager.getBadgedIconForUser(entry.getIcon(), userHandle));
+                        pgp.setTitle(entry.name);
+                        if (entry.sipper.drainType == DrainType.APP) {
+                            pgp.setContentDescription(entry.name);
+                        }
+                    }
+                    break;
+                case BatteryEntry.MSG_REPORT_FULLY_DRAWN:
+                    Activity activity = mActivity;
+                    if (activity != null) {
+                        activity.reportFullyDrawn();
+                    }
+                    break;
+            }
+            super.handleMessage(msg);
+        }
+    };
+
+    public BatteryAppListPreferenceController(Context context, String preferenceKey,
+            Lifecycle lifecycle, SettingsActivity activity, PreferenceFragment fragment) {
+        super(context);
+
+        if (lifecycle != null) {
+            lifecycle.addObserver(this);
+        }
+
+        mPreferenceKey = preferenceKey;
+        mBatteryUtils = BatteryUtils.getInstance(context);
+        mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
+        mActivity = activity;
+        mFragment = fragment;
+    }
+
+    @Override
+    public void onPause() {
+        BatteryEntry.stopRequestQueue();
+        mHandler.removeMessages(BatteryEntry.MSG_UPDATE_NAME_ICON);
+    }
+
+    @Override
+    public void onDestroy() {
+        if (mActivity.isChangingConfigurations()) {
+            BatteryEntry.clearUidCache();
+        }
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mPrefContext = screen.getContext();
+        mAppListGroup = (PreferenceGroup) screen.findPreference(mPreferenceKey);
+    }
+
+    @Override
+    public boolean isAvailable() {
+        return FeatureFlagUtils.isEnabled(mContext, FeatureFlags.BATTERY_DISPLAY_APP_LIST);
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return mPreferenceKey;
+    }
+
+    @Override
+    public boolean handlePreferenceTreeClick(Preference preference) {
+        if (preference instanceof PowerGaugePreference) {
+            PowerGaugePreference pgp = (PowerGaugePreference) preference;
+            BatteryEntry entry = pgp.getInfo();
+            AdvancedPowerUsageDetail.startBatteryDetailPage(mActivity,
+                    mFragment, mBatteryStatsHelper, STATS_TYPE, entry, pgp.getPercent(),
+                    mAnomalySparseArray != null ? mAnomalySparseArray.get(entry.sipper.getUid())
+                            : null);
+            return true;
+        }
+        return false;
+    }
+
+    public void refreshAnomalyIcon(final SparseArray<List<Anomaly>> anomalySparseArray) {
+        if (!isAvailable()) {
+            return;
+        }
+        mAnomalySparseArray = anomalySparseArray;
+        for (int i = 0, size = anomalySparseArray.size(); i < size; i++) {
+            final String key = extractKeyFromUid(anomalySparseArray.keyAt(i));
+            final PowerGaugePreference pref = (PowerGaugePreference) mAppListGroup.findPreference(
+                    key);
+            if (pref != null) {
+                pref.shouldShowAnomalyIcon(true);
+            }
+        }
+    }
+
+    public void refreshAppListGroup(BatteryStatsHelper statsHelper, boolean showAllApps,
+            CharSequence timeSequence) {
+        if (!isAvailable()) {
+            return;
+        }
+        mBatteryStatsHelper = statsHelper;
+        final int resId = showAllApps ? R.string.power_usage_list_summary_device
+                : R.string.power_usage_list_summary;
+        mAppListGroup.setTitle(TextUtils.expandTemplate(mContext.getText(resId), timeSequence));
+
+        final PowerProfile powerProfile = statsHelper.getPowerProfile();
+        final BatteryStats stats = statsHelper.getStats();
+        final double averagePower = powerProfile.getAveragePower(PowerProfile.POWER_SCREEN_FULL);
+        boolean addedSome = false;
+        final int dischargeAmount = USE_FAKE_DATA ? 5000
+                : stats != null ? stats.getDischargeAmount(STATS_TYPE) : 0;
+
+        cacheRemoveAllPrefs(mAppListGroup);
+        mAppListGroup.setOrderingAsAdded(false);
+
+        if (averagePower >= MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP || USE_FAKE_DATA) {
+            final List<BatterySipper> usageList = getCoalescedUsageList(
+                    USE_FAKE_DATA ? getFakeStats() : statsHelper.getUsageList());
+            double hiddenPowerMah = showAllApps ? 0 :
+                    mBatteryUtils.removeHiddenBatterySippers(usageList);
+            mBatteryUtils.sortUsageList(usageList);
+
+            final int numSippers = usageList.size();
+            for (int i = 0; i < numSippers; i++) {
+                final BatterySipper sipper = usageList.get(i);
+                double totalPower = USE_FAKE_DATA ? 4000 : statsHelper.getTotalPower();
+
+                final double percentOfTotal = mBatteryUtils.calculateBatteryPercent(
+                        sipper.totalPowerMah, totalPower, hiddenPowerMah, dischargeAmount);
+
+                if (((int) (percentOfTotal + .5)) < 1) {
+                    continue;
+                }
+                if (shouldHideSipper(sipper)) {
+                    continue;
+                }
+                final UserHandle userHandle = new UserHandle(UserHandle.getUserId(sipper.getUid()));
+                final BatteryEntry entry = new BatteryEntry(mActivity, mHandler, mUserManager,
+                        sipper);
+                final Drawable badgedIcon = mUserManager.getBadgedIconForUser(entry.getIcon(),
+                        userHandle);
+                final CharSequence contentDescription = mUserManager.getBadgedLabelForUser(
+                        entry.getLabel(),
+                        userHandle);
+
+                final String key = extractKeyFromSipper(sipper);
+                PowerGaugePreference pref = (PowerGaugePreference) getCachedPreference(key);
+                if (pref == null) {
+                    pref = new PowerGaugePreference(mPrefContext, badgedIcon,
+                            contentDescription, entry);
+                    pref.setKey(key);
+                }
+                sipper.percent = percentOfTotal;
+                pref.setTitle(entry.getLabel());
+                pref.setOrder(i + 1);
+                pref.setPercent(percentOfTotal);
+                pref.shouldShowAnomalyIcon(false);
+                if (sipper.usageTimeMs == 0 && sipper.drainType == DrainType.APP) {
+                    sipper.usageTimeMs = mBatteryUtils.getProcessTimeMs(
+                            BatteryUtils.StatusType.FOREGROUND, sipper.uidObj, STATS_TYPE);
+                }
+                setUsageSummary(pref, sipper);
+                addedSome = true;
+                mAppListGroup.addPreference(pref);
+                if (mAppListGroup.getPreferenceCount() - getCachedCount()
+                        > (MAX_ITEMS_TO_LIST + 1)) {
+                    break;
+                }
+            }
+        }
+        if (!addedSome) {
+            addNotAvailableMessage();
+        }
+        removeCachedPrefs(mAppListGroup);
+
+        BatteryEntry.startRequestQueue();
+    }
+
+    /**
+     * We want to coalesce some UIDs. For example, dex2oat runs under a shared gid that
+     * exists for all users of the same app. We detect this case and merge the power use
+     * for dex2oat to the device OWNER's use of the app.
+     *
+     * @return A sorted list of apps using power.
+     */
+    private List<BatterySipper> getCoalescedUsageList(final List<BatterySipper> sippers) {
+        final SparseArray<BatterySipper> uidList = new SparseArray<>();
+
+        final ArrayList<BatterySipper> results = new ArrayList<>();
+        final int numSippers = sippers.size();
+        for (int i = 0; i < numSippers; i++) {
+            BatterySipper sipper = sippers.get(i);
+            if (sipper.getUid() > 0) {
+                int realUid = sipper.getUid();
+
+                // Check if this UID is a shared GID. If so, we combine it with the OWNER's
+                // actual app UID.
+                if (isSharedGid(sipper.getUid())) {
+                    realUid = UserHandle.getUid(UserHandle.USER_SYSTEM,
+                            UserHandle.getAppIdFromSharedAppGid(sipper.getUid()));
+                }
+
+                // Check if this UID is a system UID (mediaserver, logd, nfc, drm, etc).
+                if (isSystemUid(realUid)
+                        && !"mediaserver".equals(sipper.packageWithHighestDrain)) {
+                    // Use the system UID for all UIDs running in their own sandbox that
+                    // are not apps. We exclude mediaserver because we already are expected to
+                    // report that as a separate item.
+                    realUid = Process.SYSTEM_UID;
+                }
+
+                if (realUid != sipper.getUid()) {
+                    // Replace the BatterySipper with a new one with the real UID set.
+                    BatterySipper newSipper = new BatterySipper(sipper.drainType,
+                            new FakeUid(realUid), 0.0);
+                    newSipper.add(sipper);
+                    newSipper.packageWithHighestDrain = sipper.packageWithHighestDrain;
+                    newSipper.mPackages = sipper.mPackages;
+                    sipper = newSipper;
+                }
+
+                int index = uidList.indexOfKey(realUid);
+                if (index < 0) {
+                    // New entry.
+                    uidList.put(realUid, sipper);
+                } else {
+                    // Combine BatterySippers if we already have one with this UID.
+                    final BatterySipper existingSipper = uidList.valueAt(index);
+                    existingSipper.add(sipper);
+                    if (existingSipper.packageWithHighestDrain == null
+                            && sipper.packageWithHighestDrain != null) {
+                        existingSipper.packageWithHighestDrain = sipper.packageWithHighestDrain;
+                    }
+
+                    final int existingPackageLen = existingSipper.mPackages != null ?
+                            existingSipper.mPackages.length : 0;
+                    final int newPackageLen = sipper.mPackages != null ?
+                            sipper.mPackages.length : 0;
+                    if (newPackageLen > 0) {
+                        String[] newPackages = new String[existingPackageLen + newPackageLen];
+                        if (existingPackageLen > 0) {
+                            System.arraycopy(existingSipper.mPackages, 0, newPackages, 0,
+                                    existingPackageLen);
+                        }
+                        System.arraycopy(sipper.mPackages, 0, newPackages, existingPackageLen,
+                                newPackageLen);
+                        existingSipper.mPackages = newPackages;
+                    }
+                }
+            } else {
+                results.add(sipper);
+            }
+        }
+
+        final int numUidSippers = uidList.size();
+        for (int i = 0; i < numUidSippers; i++) {
+            results.add(uidList.valueAt(i));
+        }
+
+        // The sort order must have changed, so re-sort based on total power use.
+        mBatteryUtils.sortUsageList(results);
+        return results;
+    }
+
+    @VisibleForTesting
+    void setUsageSummary(Preference preference, BatterySipper sipper) {
+        // Only show summary when usage time is longer than one minute
+        final long usageTimeMs = sipper.usageTimeMs;
+        if (usageTimeMs >= DateUtils.MINUTE_IN_MILLIS) {
+            final CharSequence timeSequence = Utils.formatElapsedTime(mContext, usageTimeMs,
+                    false);
+            preference.setSummary(
+                    (sipper.drainType != DrainType.APP || mBatteryUtils.shouldHideSipper(sipper))
+                            ? timeSequence
+                            : TextUtils.expandTemplate(mContext.getText(R.string.battery_used_for),
+                                    timeSequence));
+        }
+    }
+
+    @VisibleForTesting
+    boolean shouldHideSipper(BatterySipper sipper) {
+        // Don't show over-counted and unaccounted in any condition
+        return sipper.drainType == BatterySipper.DrainType.OVERCOUNTED
+                || sipper.drainType == BatterySipper.DrainType.UNACCOUNTED;
+    }
+
+    @VisibleForTesting
+    String extractKeyFromSipper(BatterySipper sipper) {
+        if (sipper.uidObj != null) {
+            return extractKeyFromUid(sipper.getUid());
+        } else if (sipper.drainType == DrainType.USER) {
+            return sipper.drainType.toString() + sipper.userId;
+        } else if (sipper.drainType != DrainType.APP) {
+            return sipper.drainType.toString();
+        } else if (sipper.getPackages() != null) {
+            return TextUtils.concat(sipper.getPackages()).toString();
+        } else {
+            Log.w(TAG, "Inappropriate BatterySipper without uid and package names: " + sipper);
+            return "-1";
+        }
+    }
+
+    @VisibleForTesting
+    String extractKeyFromUid(int uid) {
+        return Integer.toString(uid);
+    }
+
+    private void cacheRemoveAllPrefs(PreferenceGroup group) {
+        mPreferenceCache = new ArrayMap<>();
+        final int N = group.getPreferenceCount();
+        for (int i = 0; i < N; i++) {
+            Preference p = group.getPreference(i);
+            if (TextUtils.isEmpty(p.getKey())) {
+                continue;
+            }
+            mPreferenceCache.put(p.getKey(), p);
+        }
+    }
+
+    private static boolean isSharedGid(int uid) {
+        return UserHandle.getAppIdFromSharedAppGid(uid) > 0;
+    }
+
+    private static boolean isSystemUid(int uid) {
+        final int appUid = UserHandle.getAppId(uid);
+        return appUid >= Process.SYSTEM_UID && appUid < Process.FIRST_APPLICATION_UID;
+    }
+
+    private static List<BatterySipper> getFakeStats() {
+        ArrayList<BatterySipper> stats = new ArrayList<>();
+        float use = 5;
+        for (DrainType type : DrainType.values()) {
+            if (type == DrainType.APP) {
+                continue;
+            }
+            stats.add(new BatterySipper(type, null, use));
+            use += 5;
+        }
+        for (int i = 0; i < 100; i++) {
+            stats.add(new BatterySipper(DrainType.APP,
+                    new FakeUid(Process.FIRST_APPLICATION_UID + i), use));
+        }
+        stats.add(new BatterySipper(DrainType.APP,
+                new FakeUid(0), use));
+
+        // Simulate dex2oat process.
+        BatterySipper sipper = new BatterySipper(DrainType.APP,
+                new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID)), 10.0f);
+        sipper.packageWithHighestDrain = "dex2oat";
+        stats.add(sipper);
+
+        sipper = new BatterySipper(DrainType.APP,
+                new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID + 1)), 10.0f);
+        sipper.packageWithHighestDrain = "dex2oat";
+        stats.add(sipper);
+
+        sipper = new BatterySipper(DrainType.APP,
+                new FakeUid(UserHandle.getSharedAppGid(Process.LOG_UID)), 9.0f);
+        stats.add(sipper);
+
+        return stats;
+    }
+
+    private Preference getCachedPreference(String key) {
+        return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
+    }
+
+    private void removeCachedPrefs(PreferenceGroup group) {
+        for (Preference p : mPreferenceCache.values()) {
+            group.removePreference(p);
+        }
+        mPreferenceCache = null;
+    }
+
+    private int getCachedCount() {
+        return mPreferenceCache != null ? mPreferenceCache.size() : 0;
+    }
+
+    private void addNotAvailableMessage() {
+        final String NOT_AVAILABLE = "not_available";
+        Preference notAvailable = getCachedPreference(NOT_AVAILABLE);
+        if (notAvailable == null) {
+            notAvailable = new Preference(mPrefContext);
+            notAvailable.setKey(NOT_AVAILABLE);
+            notAvailable.setTitle(R.string.power_usage_not_available);
+            mAppListGroup.addPreference(notAvailable);
+        }
+    }
+}
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index ed5b6f4..e4b70a1 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -21,21 +21,13 @@
 import android.app.LoaderManager.LoaderCallbacks;
 import android.content.Context;
 import android.content.Loader;
-import android.graphics.drawable.Drawable;
 import android.os.BatteryStats;
 import android.os.Bundle;
-import android.os.Handler;
-import android.os.Message;
-import android.os.Process;
-import android.os.UserHandle;
 import android.provider.SearchIndexableResource;
 import android.support.annotation.VisibleForTesting;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceGroup;
-import android.text.TextUtils;
-import android.text.format.DateUtils;
 import android.text.format.Formatter;
-import android.util.Log;
 import android.util.SparseArray;
 import android.view.Menu;
 import android.view.MenuInflater;
@@ -49,7 +41,6 @@
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.internal.os.BatterySipper;
 import com.android.internal.os.BatterySipper.DrainType;
-import com.android.internal.os.PowerProfile;
 import com.android.settings.R;
 import com.android.settings.Settings.HighPowerApplicationsActivity;
 import com.android.settings.SettingsActivity;
@@ -71,6 +62,7 @@
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settingslib.core.AbstractPreferenceController;
+import com.android.settingslib.core.lifecycle.Lifecycle;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -86,12 +78,9 @@
     static final String TAG = "PowerUsageSummary";
 
     private static final boolean DEBUG = false;
-    private static final boolean USE_FAKE_DATA = false;
     private static final String KEY_APP_LIST = "app_list";
     private static final String KEY_BATTERY_HEADER = "battery_header";
     private static final String KEY_SHOW_ALL_APPS = "show_all_apps";
-    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 String KEY_SCREEN_USAGE = "screen_usage";
     private static final String KEY_TIME_SINCE_LAST_FULL_CHARGE = "last_full_charge";
@@ -136,6 +125,7 @@
     PreferenceGroup mAppListGroup;
     @VisibleForTesting
     BatteryHeaderPreferenceController mBatteryHeaderPreferenceController;
+    private BatteryAppListPreferenceController mBatteryAppListPreferenceController;
     private AnomalySummaryPreferenceController mAnomalySummaryPreferenceController;
     private int mStatsType = BatteryStats.STATS_SINCE_CHARGED;
 
@@ -157,7 +147,7 @@
                     mAnomalySummaryPreferenceController.updateAnomalySummaryPreference(data);
 
                     updateAnomalySparseArray(data);
-                    refreshAnomalyIcon();
+                    mBatteryAppListPreferenceController.refreshAnomalyIcon(mAnomalySparseArray);
                 }
 
                 @Override
@@ -235,7 +225,6 @@
         initFeatureProvider();
         mBatteryLayoutPref = (LayoutPreference) findPreference(KEY_BATTERY_HEADER);
 
-        mAppListGroup = (PreferenceGroup) findPreference(KEY_APP_LIST);
         mScreenUsagePref = (PowerGaugePreference) findPreference(KEY_SCREEN_USAGE);
         mLastFullChargePref = (PowerGaugePreference) findPreference(
                 KEY_TIME_SINCE_LAST_FULL_CHARGE);
@@ -255,21 +244,6 @@
     }
 
     @Override
-    public void onPause() {
-        BatteryEntry.stopRequestQueue();
-        mHandler.removeMessages(BatteryEntry.MSG_UPDATE_NAME_ICON);
-        super.onPause();
-    }
-
-    @Override
-    public void onDestroy() {
-        super.onDestroy();
-        if (getActivity().isChangingConfigurations()) {
-            BatteryEntry.clearUidCache();
-        }
-    }
-
-    @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putBoolean(KEY_SHOW_ALL_APPS, mShowAllApps);
@@ -283,14 +257,7 @@
         if (KEY_BATTERY_HEADER.equals(preference.getKey())) {
             performBatteryHeaderClick();
             return true;
-        } else if (!(preference instanceof PowerGaugePreference)) {
-            return super.onPreferenceTreeClick(preference);
         }
-        PowerGaugePreference pgp = (PowerGaugePreference) preference;
-        BatteryEntry entry = pgp.getInfo();
-        AdvancedPowerUsageDetail.startBatteryDetailPage((SettingsActivity) getActivity(),
-                this, mStatsHelper, mStatsType, entry, pgp.getPercent(),
-                mAnomalySparseArray.get(entry.sipper.getUid()));
         return super.onPreferenceTreeClick(preference);
     }
 
@@ -306,10 +273,15 @@
 
     @Override
     protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
+        final Lifecycle lifecycle = getLifecycle();
+        final SettingsActivity activity = (SettingsActivity) getActivity();
         final List<AbstractPreferenceController> controllers = new ArrayList<>();
         mBatteryHeaderPreferenceController = new BatteryHeaderPreferenceController(
-                context, getActivity(), this /* host */, getLifecycle());
+                context, activity, this /* host */, getLifecycle());
         controllers.add(mBatteryHeaderPreferenceController);
+        mBatteryAppListPreferenceController = new BatteryAppListPreferenceController(context,
+                KEY_APP_LIST, lifecycle, activity, this);
+        controllers.add(mBatteryAppListPreferenceController);
         controllers.add(new AutoBrightnessPreferenceController(context, KEY_AUTO_BRIGHTNESS));
         controllers.add(new TimeoutPreferenceController(context, KEY_SCREEN_TIMEOUT));
         controllers.add(new BatterySaverController(context, getLifecycle()));
@@ -388,17 +360,6 @@
         }
     }
 
-    private void addNotAvailableMessage() {
-        final String NOT_AVAILABLE = "not_available";
-        Preference notAvailable = getCachedPreference(NOT_AVAILABLE);
-        if (notAvailable == null) {
-            notAvailable = new Preference(getPrefContext());
-            notAvailable.setKey(NOT_AVAILABLE);
-            notAvailable.setTitle(R.string.power_usage_not_available);
-            mAppListGroup.addPreference(notAvailable);
-        }
-    }
-
     private void performBatteryHeaderClick() {
         if (mPowerFeatureProvider.isAdvancedUiEnabled()) {
             Utils.startWithFragment(getContext(), PowerUsageAdvanced.class.getName(), null,
@@ -415,101 +376,6 @@
         }
     }
 
-    private static boolean isSharedGid(int uid) {
-        return UserHandle.getAppIdFromSharedAppGid(uid) > 0;
-    }
-
-    private static boolean isSystemUid(int uid) {
-        final int appUid = UserHandle.getAppId(uid);
-        return appUid >= Process.SYSTEM_UID && appUid < Process.FIRST_APPLICATION_UID;
-    }
-
-    /**
-     * We want to coalesce some UIDs. For example, dex2oat runs under a shared gid that
-     * exists for all users of the same app. We detect this case and merge the power use
-     * for dex2oat to the device OWNER's use of the app.
-     *
-     * @return A sorted list of apps using power.
-     */
-    private List<BatterySipper> getCoalescedUsageList(final List<BatterySipper> sippers) {
-        final SparseArray<BatterySipper> uidList = new SparseArray<>();
-
-        final ArrayList<BatterySipper> results = new ArrayList<>();
-        final int numSippers = sippers.size();
-        for (int i = 0; i < numSippers; i++) {
-            BatterySipper sipper = sippers.get(i);
-            if (sipper.getUid() > 0) {
-                int realUid = sipper.getUid();
-
-                // Check if this UID is a shared GID. If so, we combine it with the OWNER's
-                // actual app UID.
-                if (isSharedGid(sipper.getUid())) {
-                    realUid = UserHandle.getUid(UserHandle.USER_SYSTEM,
-                            UserHandle.getAppIdFromSharedAppGid(sipper.getUid()));
-                }
-
-                // Check if this UID is a system UID (mediaserver, logd, nfc, drm, etc).
-                if (isSystemUid(realUid)
-                        && !"mediaserver".equals(sipper.packageWithHighestDrain)) {
-                    // Use the system UID for all UIDs running in their own sandbox that
-                    // are not apps. We exclude mediaserver because we already are expected to
-                    // report that as a separate item.
-                    realUid = Process.SYSTEM_UID;
-                }
-
-                if (realUid != sipper.getUid()) {
-                    // Replace the BatterySipper with a new one with the real UID set.
-                    BatterySipper newSipper = new BatterySipper(sipper.drainType,
-                            new FakeUid(realUid), 0.0);
-                    newSipper.add(sipper);
-                    newSipper.packageWithHighestDrain = sipper.packageWithHighestDrain;
-                    newSipper.mPackages = sipper.mPackages;
-                    sipper = newSipper;
-                }
-
-                int index = uidList.indexOfKey(realUid);
-                if (index < 0) {
-                    // New entry.
-                    uidList.put(realUid, sipper);
-                } else {
-                    // Combine BatterySippers if we already have one with this UID.
-                    final BatterySipper existingSipper = uidList.valueAt(index);
-                    existingSipper.add(sipper);
-                    if (existingSipper.packageWithHighestDrain == null
-                            && sipper.packageWithHighestDrain != null) {
-                        existingSipper.packageWithHighestDrain = sipper.packageWithHighestDrain;
-                    }
-
-                    final int existingPackageLen = existingSipper.mPackages != null ?
-                            existingSipper.mPackages.length : 0;
-                    final int newPackageLen = sipper.mPackages != null ?
-                            sipper.mPackages.length : 0;
-                    if (newPackageLen > 0) {
-                        String[] newPackages = new String[existingPackageLen + newPackageLen];
-                        if (existingPackageLen > 0) {
-                            System.arraycopy(existingSipper.mPackages, 0, newPackages, 0,
-                                    existingPackageLen);
-                        }
-                        System.arraycopy(sipper.mPackages, 0, newPackages, existingPackageLen,
-                                newPackageLen);
-                        existingSipper.mPackages = newPackages;
-                    }
-                }
-            } else {
-                results.add(sipper);
-            }
-        }
-
-        final int numUidSippers = uidList.size();
-        for (int i = 0; i < numUidSippers; i++) {
-            results.add(uidList.valueAt(i));
-        }
-
-        // The sort order must have changed, so re-sort based on total power use.
-        mBatteryUtils.sortUsageList(results);
-        return results;
-    }
-
     protected void refreshUi() {
         final Context context = getContext();
         if (context == null) {
@@ -527,102 +393,8 @@
 
         final CharSequence timeSequence = Utils.formatRelativeTime(context, lastFullChargeTime,
                 false);
-        final int resId = mShowAllApps ? R.string.power_usage_list_summary_device
-                : R.string.power_usage_list_summary;
-        mAppListGroup.setTitle(TextUtils.expandTemplate(getText(resId), timeSequence));
-
-        refreshAppListGroup();
-    }
-
-    private void refreshAppListGroup() {
-        final PowerProfile powerProfile = mStatsHelper.getPowerProfile();
-        final BatteryStats stats = mStatsHelper.getStats();
-        final double averagePower = powerProfile.getAveragePower(PowerProfile.POWER_SCREEN_FULL);
-        boolean addedSome = false;
-        final int dischargeAmount = USE_FAKE_DATA ? 5000
-                : stats != null ? stats.getDischargeAmount(mStatsType) : 0;
-
-        cacheRemoveAllPrefs(mAppListGroup);
-        mAppListGroup.setOrderingAsAdded(false);
-
-        if (averagePower >= MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP || USE_FAKE_DATA) {
-            final List<BatterySipper> usageList = getCoalescedUsageList(
-                    USE_FAKE_DATA ? getFakeStats() : mStatsHelper.getUsageList());
-            double hiddenPowerMah = mShowAllApps ? 0 :
-                    mBatteryUtils.removeHiddenBatterySippers(usageList);
-            mBatteryUtils.sortUsageList(usageList);
-
-            final int numSippers = usageList.size();
-            for (int i = 0; i < numSippers; i++) {
-                final BatterySipper sipper = usageList.get(i);
-                double totalPower = USE_FAKE_DATA ? 4000 : mStatsHelper.getTotalPower();
-
-                final double percentOfTotal = mBatteryUtils.calculateBatteryPercent(
-                        sipper.totalPowerMah, totalPower, hiddenPowerMah, dischargeAmount);
-
-                if (((int) (percentOfTotal + .5)) < 1) {
-                    continue;
-                }
-                if (shouldHideSipper(sipper)) {
-                    continue;
-                }
-                final UserHandle userHandle = new UserHandle(UserHandle.getUserId(sipper.getUid()));
-                final BatteryEntry entry = new BatteryEntry(getActivity(), mHandler, mUm, sipper);
-                final Drawable badgedIcon = mUm.getBadgedIconForUser(entry.getIcon(),
-                        userHandle);
-                final CharSequence contentDescription = mUm.getBadgedLabelForUser(entry.getLabel(),
-                        userHandle);
-
-                final String key = extractKeyFromSipper(sipper);
-                PowerGaugePreference pref = (PowerGaugePreference) getCachedPreference(key);
-                if (pref == null) {
-                    pref = new PowerGaugePreference(getPrefContext(), badgedIcon,
-                            contentDescription, entry);
-                    pref.setKey(key);
-                }
-                sipper.percent = percentOfTotal;
-                pref.setTitle(entry.getLabel());
-                pref.setOrder(i + 1);
-                pref.setPercent(percentOfTotal);
-                pref.shouldShowAnomalyIcon(false);
-                if (sipper.usageTimeMs == 0 && sipper.drainType == DrainType.APP) {
-                    sipper.usageTimeMs = mBatteryUtils.getProcessTimeMs(
-                            BatteryUtils.StatusType.FOREGROUND, sipper.uidObj, mStatsType);
-                }
-                setUsageSummary(pref, sipper);
-                addedSome = true;
-                mAppListGroup.addPreference(pref);
-                if (mAppListGroup.getPreferenceCount() - getCachedCount()
-                        > (MAX_ITEMS_TO_LIST + 1)) {
-                    break;
-                }
-            }
-        }
-        if (!addedSome) {
-            addNotAvailableMessage();
-        }
-        removeCachedPrefs(mAppListGroup);
-
-        BatteryEntry.startRequestQueue();
-    }
-
-    @VisibleForTesting
-    boolean shouldHideSipper(BatterySipper sipper) {
-        // Don't show over-counted and unaccounted in any condition
-        return sipper.drainType == BatterySipper.DrainType.OVERCOUNTED
-                || sipper.drainType == BatterySipper.DrainType.UNACCOUNTED;
-    }
-
-    @VisibleForTesting
-    void refreshAnomalyIcon() {
-        for (int i = 0, size = mAnomalySparseArray.size(); i < size; i++) {
-            final String key = extractKeyFromUid(mAnomalySparseArray.keyAt(i));
-            final PowerGaugePreference pref = (PowerGaugePreference) mAppListGroup.findPreference(
-                    key);
-            if (pref != null) {
-                pref.shouldShowAnomalyIcon(true);
-            }
-        }
+        mBatteryAppListPreferenceController.refreshAppListGroup(mStatsHelper, mShowAllApps,
+                timeSequence);
     }
 
     @VisibleForTesting
@@ -633,6 +405,11 @@
     }
 
     @VisibleForTesting
+    void setBatteryLayoutPreference(LayoutPreference layoutPreference) {
+        mBatteryLayoutPref = layoutPreference;
+    }
+
+    @VisibleForTesting
     AnomalyDetectionPolicy getAnomalyDetectionPolicy() {
         return new AnomalyDetectionPolicy(getContext());
     }
@@ -675,54 +452,6 @@
     }
 
     @VisibleForTesting
-    double calculatePercentage(double powerUsage, double dischargeAmount) {
-        final double totalPower = mStatsHelper.getTotalPower();
-        return totalPower == 0 ? 0 :
-                ((powerUsage / totalPower) * dischargeAmount);
-    }
-
-    @VisibleForTesting
-    void setUsageSummary(Preference preference, BatterySipper sipper) {
-        // Only show summary when usage time is longer than one minute
-        final long usageTimeMs = sipper.usageTimeMs;
-        if (usageTimeMs >= DateUtils.MINUTE_IN_MILLIS) {
-            final CharSequence timeSequence = Utils.formatElapsedTime(getContext(), usageTimeMs,
-                    false);
-            preference.setSummary(
-                    (sipper.drainType != DrainType.APP || mBatteryUtils.shouldHideSipper(sipper))
-                            ? timeSequence
-                            : TextUtils.expandTemplate(getText(R.string.battery_used_for),
-                                    timeSequence));
-        }
-    }
-
-    @VisibleForTesting
-    String extractKeyFromSipper(BatterySipper sipper) {
-        if (sipper.uidObj != null) {
-            return extractKeyFromUid(sipper.getUid());
-        } else if (sipper.drainType == DrainType.USER) {
-            return sipper.drainType.toString() + sipper.userId;
-        } else if (sipper.drainType != DrainType.APP) {
-            return sipper.drainType.toString();
-        } else if (sipper.getPackages() != null) {
-            return TextUtils.concat(sipper.getPackages()).toString();
-        } else {
-            Log.w(TAG, "Inappropriate BatterySipper without uid and package names: " + sipper);
-            return "-1";
-        }
-    }
-
-    @VisibleForTesting
-    String extractKeyFromUid(int uid) {
-        return Integer.toString(uid);
-    }
-
-    @VisibleForTesting
-    void setBatteryLayoutPreference(LayoutPreference layoutPreference) {
-        mBatteryLayoutPref = layoutPreference;
-    }
-
-    @VisibleForTesting
     void initFeatureProvider() {
         final Context context = getContext();
         mPowerFeatureProvider = FeatureFactory.getFactory(context)
@@ -755,72 +484,6 @@
         }
     }
 
-    private static List<BatterySipper> getFakeStats() {
-        ArrayList<BatterySipper> stats = new ArrayList<>();
-        float use = 5;
-        for (DrainType type : DrainType.values()) {
-            if (type == DrainType.APP) {
-                continue;
-            }
-            stats.add(new BatterySipper(type, null, use));
-            use += 5;
-        }
-        for (int i = 0; i < 100; i++) {
-            stats.add(new BatterySipper(DrainType.APP,
-                    new FakeUid(Process.FIRST_APPLICATION_UID + i), use));
-        }
-        stats.add(new BatterySipper(DrainType.APP,
-                new FakeUid(0), use));
-
-        // Simulate dex2oat process.
-        BatterySipper sipper = new BatterySipper(DrainType.APP,
-                new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID)), 10.0f);
-        sipper.packageWithHighestDrain = "dex2oat";
-        stats.add(sipper);
-
-        sipper = new BatterySipper(DrainType.APP,
-                new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID + 1)), 10.0f);
-        sipper.packageWithHighestDrain = "dex2oat";
-        stats.add(sipper);
-
-        sipper = new BatterySipper(DrainType.APP,
-                new FakeUid(UserHandle.getSharedAppGid(Process.LOG_UID)), 9.0f);
-        stats.add(sipper);
-
-        return stats;
-    }
-
-    Handler mHandler = new Handler() {
-
-        @Override
-        public void handleMessage(Message msg) {
-            switch (msg.what) {
-                case BatteryEntry.MSG_UPDATE_NAME_ICON:
-                    BatteryEntry entry = (BatteryEntry) msg.obj;
-                    PowerGaugePreference pgp =
-                            (PowerGaugePreference) findPreference(
-                                    Integer.toString(entry.sipper.uidObj.getUid()));
-                    if (pgp != null) {
-                        final int userId = UserHandle.getUserId(entry.sipper.getUid());
-                        final UserHandle userHandle = new UserHandle(userId);
-                        pgp.setIcon(mUm.getBadgedIconForUser(entry.getIcon(), userHandle));
-                        pgp.setTitle(entry.name);
-                        if (entry.sipper.drainType == DrainType.APP) {
-                            pgp.setContentDescription(entry.name);
-                        }
-                    }
-                    break;
-                case BatteryEntry.MSG_REPORT_FULLY_DRAWN:
-                    Activity activity = getActivity();
-                    if (activity != null) {
-                        activity.reportFullyDrawn();
-                    }
-                    break;
-            }
-            super.handleMessage(msg);
-        }
-    };
-
     @Override
     public void onAnomalyHandled(Anomaly anomaly) {
         mAnomalySummaryPreferenceController.hideHighUsagePreference();
diff --git a/tests/robotests/assets/grandfather_not_implementing_indexable b/tests/robotests/assets/grandfather_not_implementing_indexable
index 245d321..eee2add 100644
--- a/tests/robotests/assets/grandfather_not_implementing_indexable
+++ b/tests/robotests/assets/grandfather_not_implementing_indexable
@@ -20,7 +20,7 @@
 com.android.settings.datausage.DataPlanUsageSummary
 com.android.settings.accessibility.FontSizePreferenceFragmentForSetupWizard
 com.android.settings.applications.ManageDomainUrls
-com.android.settings.applications.WriteSettingsDetails
+com.android.settings.applications.appinfo.WriteSettingsDetails
 com.android.settings.applications.ProcessStatsSummary
 com.android.settings.users.RestrictedProfileSettings
 com.android.settings.accounts.ChooseAccountActivity
@@ -36,7 +36,7 @@
 com.android.settings.accounts.AccountSyncSettings
 com.android.settings.notification.RedactionInterstitial$RedactionInterstitialFragment
 com.android.settings.inputmethod.InputMethodAndSubtypeEnabler
-com.android.settings.applications.DrawOverlayDetails
+com.android.settings.applications.appinfo.DrawOverlayDetails
 com.android.settings.backup.ToggleBackupSettingFragment
 com.android.settings.users.UserDetailsSettings
 com.android.settings.datausage.UnrestrictedDataAccess
@@ -59,9 +59,9 @@
 com.android.settings.notification.NotificationAccessSettings
 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment
 com.android.settings.localepicker.LocaleListEditor
-com.android.settings.applications.ExternalSourcesDetails
-com.android.settings.applications.PictureInPictureSettings
-com.android.settings.applications.PictureInPictureDetails
+com.android.settings.applications.appinfo.ExternalSourcesDetails
+com.android.settings.applications.appinfo.PictureInPictureSettings
+com.android.settings.applications.appinfo.PictureInPictureDetails
 com.android.settings.ApnSettings
 com.android.settings.PrivacySettings
 com.android.settings.WifiCallingSettings
diff --git a/tests/robotests/src/com/android/settings/DeviceInfoSettingsTest.java b/tests/robotests/src/com/android/settings/DeviceInfoSettingsTest.java
index 78f5bdf..486993d 100644
--- a/tests/robotests/src/com/android/settings/DeviceInfoSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/DeviceInfoSettingsTest.java
@@ -94,7 +94,12 @@
     }
 
     @Test
+    @Config(shadows = {
+            SettingsShadowSystemProperties.class
+    })
     public void getPrefXml_shouldReturnDeviceInfoXml() {
+        SystemProperties.set(FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + FeatureFlags.DEVICE_INFO_V2,
+                "true");
         assertThat(mSettings.getPreferenceScreenResId()).isEqualTo(R.xml.device_info_settings_v2);
     }
 
diff --git a/tests/robotests/src/com/android/settings/applications/appinfo/DrawOverlayDetailPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/applications/appinfo/DrawOverlayDetailPreferenceControllerTest.java
new file mode 100644
index 0000000..a7468b5
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/applications/appinfo/DrawOverlayDetailPreferenceControllerTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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.appinfo;
+
+import static android.Manifest.permission.SYSTEM_ALERT_WINDOW;
+import static android.Manifest.permission.WRITE_SETTINGS;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.content.pm.PackageInfo;
+import android.os.UserManager;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.TestConfig;
+import com.android.settings.applications.AppInfoDashboardFragment;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+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;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class DrawOverlayDetailPreferenceControllerTest {
+
+    @Mock
+    private UserManager mUserManager;
+    @Mock
+    private AppInfoDashboardFragment mFragment;
+    @Mock
+    private Preference mPreference;
+
+    private Context mContext;
+    private DrawOverlayDetailPreferenceController mController;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = spy(RuntimeEnvironment.application);
+        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
+        mController = spy(new DrawOverlayDetailPreferenceController(mContext, mFragment));
+        final String key = mController.getPreferenceKey();
+        when(mPreference.getKey()).thenReturn(key);
+    }
+
+    @Test
+    public void getAvailabilityStatus_managedProfile_shouldReturnDisabled() {
+        when(mUserManager.isManagedProfile()).thenReturn(true);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_noPermissionRequested_shouldReturnDisabled() {
+        when(mUserManager.isManagedProfile()).thenReturn(false);
+        when(mFragment.getPackageInfo()).thenReturn(new PackageInfo());
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_noSystemAlertWindowPermission_shouldReturnDisabled() {
+        when(mUserManager.isManagedProfile()).thenReturn(false);
+        final PackageInfo info = new PackageInfo();
+        info.requestedPermissions = new String[] {WRITE_SETTINGS};
+        when(mFragment.getPackageInfo()).thenReturn(info);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_hasSystemAlertWindowPermission_shouldReturnAvailable() {
+        when(mUserManager.isManagedProfile()).thenReturn(false);
+        final PackageInfo info = new PackageInfo();
+        info.requestedPermissions = new String[] {SYSTEM_ALERT_WINDOW};
+        when(mFragment.getPackageInfo()).thenReturn(info);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.AVAILABLE);
+    }
+
+    @Test
+    public void getDetailFragmentClass_shouldReturnDrawOverlayDetails() {
+        assertThat(mController.getDetailFragmentClass()).isEqualTo(DrawOverlayDetails.class);
+    }
+
+    @Test
+    public void updateState_shouldSetSummary() {
+        final String summary = "test summary";
+        doReturn(summary).when(mController).getSummary();
+
+        mController.updateState(mPreference);
+
+        verify(mPreference).setSummary(summary);
+    }
+
+}
diff --git a/tests/robotests/src/com/android/settings/applications/DrawOverlayDetailsTest.java b/tests/robotests/src/com/android/settings/applications/appinfo/DrawOverlayDetailsTest.java
similarity index 98%
rename from tests/robotests/src/com/android/settings/applications/DrawOverlayDetailsTest.java
rename to tests/robotests/src/com/android/settings/applications/appinfo/DrawOverlayDetailsTest.java
index 94dc137..a33a6b8 100644
--- a/tests/robotests/src/com/android/settings/applications/DrawOverlayDetailsTest.java
+++ b/tests/robotests/src/com/android/settings/applications/appinfo/DrawOverlayDetailsTest.java
@@ -14,7 +14,7 @@
  * limitations under the License
  */
 
-package com.android.settings.applications;
+package com.android.settings.applications.appinfo;
 
 import static org.mockito.Matchers.eq;
 import static org.mockito.Matchers.nullable;
diff --git a/tests/robotests/src/com/android/settings/applications/appinfo/ExternalSourceDetailPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/applications/appinfo/ExternalSourceDetailPreferenceControllerTest.java
new file mode 100644
index 0000000..d500be9
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/applications/appinfo/ExternalSourceDetailPreferenceControllerTest.java
@@ -0,0 +1,105 @@
+/*
+ * 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.appinfo;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.os.UserManager;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.TestConfig;
+import com.android.settings.applications.AppInfoDashboardFragment;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+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;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class ExternalSourceDetailPreferenceControllerTest {
+
+    @Mock
+    private UserManager mUserManager;
+    @Mock
+    private AppInfoDashboardFragment mFragment;
+    @Mock
+    private Preference mPreference;
+
+    private Context mContext;
+    private ExternalSourceDetailPreferenceController mController;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = spy(RuntimeEnvironment.application);
+        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
+        mController = spy(
+                new ExternalSourceDetailPreferenceController(mContext, mFragment, "Package1"));
+        final String key = mController.getPreferenceKey();
+        when(mPreference.getKey()).thenReturn(key);
+    }
+
+    @Test
+    public void getAvailabilityStatus_managedProfile_shouldReturnDisabled() {
+        when(mUserManager.isManagedProfile()).thenReturn(true);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_notPotentialAppSource_shouldReturnDisabled() {
+        when(mUserManager.isManagedProfile()).thenReturn(false);
+        doReturn(false).when(mController).isPotentialAppSource();
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isPotentialAppSource_shouldReturnAvailable() {
+        when(mUserManager.isManagedProfile()).thenReturn(false);
+        doReturn(true).when(mController).isPotentialAppSource();
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.AVAILABLE);
+    }
+
+    @Test
+    public void getDetailFragmentClass_shouldReturnExternalSourcesDetails() {
+        assertThat(mController.getDetailFragmentClass()).isEqualTo(ExternalSourcesDetails.class);
+    }
+
+    @Test
+    public void updateState_shouldSetSummary() {
+        final String summary = "test summary";
+        doReturn(summary).when(mController).getPreferenceSummary();
+
+        mController.updateState(mPreference);
+
+        verify(mPreference).setSummary(summary);
+    }
+
+}
diff --git a/tests/robotests/src/com/android/settings/applications/appinfo/PictureInPictureDetailPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/applications/appinfo/PictureInPictureDetailPreferenceControllerTest.java
new file mode 100644
index 0000000..7d81168
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/applications/appinfo/PictureInPictureDetailPreferenceControllerTest.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.applications.appinfo;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.os.UserManager;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.R;
+import com.android.settings.TestConfig;
+import com.android.settings.applications.AppInfoDashboardFragment;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+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;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class PictureInPictureDetailPreferenceControllerTest {
+
+    @Mock
+    private UserManager mUserManager;
+    @Mock
+    private AppInfoDashboardFragment mFragment;
+    @Mock
+    private Preference mPreference;
+
+    private Context mContext;
+    private PictureInPictureDetailPreferenceController mController;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = spy(RuntimeEnvironment.application);
+        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
+
+        mController = spy(
+                new PictureInPictureDetailPreferenceController(mContext, mFragment, "Package1"));
+        final String key = mController.getPreferenceKey();
+        when(mPreference.getKey()).thenReturn(key);
+    }
+
+    @Test
+    public void getAvailabilityStatus_managedProfile_shouldReturnDisabled() {
+        when(mUserManager.isManagedProfile()).thenReturn(true);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_noPictureInPictureActivities_shouldReturnDisabled() {
+        when(mUserManager.isManagedProfile()).thenReturn(false);
+        doReturn(false).when(mController).hasPictureInPictureActivites();
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_hasPictureInPictureActivities_shouldReturnAvailable() {
+        when(mUserManager.isManagedProfile()).thenReturn(false);
+        doReturn(true).when(mController).hasPictureInPictureActivites();
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.AVAILABLE);
+    }
+
+    @Test
+    public void getDetailFragmentClass_shouldReturnPictureInPictureDetails() {
+        assertThat(mController.getDetailFragmentClass()).isEqualTo(PictureInPictureDetails.class);
+    }
+
+    @Test
+    public void updateState_shouldSetSummary() {
+        final int summary = R.string.app_permission_summary_allowed;
+        doReturn(summary).when(mController).getPreferenceSummary();
+
+        mController.updateState(mPreference);
+
+        verify(mPreference).setSummary(summary);
+    }
+
+}
diff --git a/tests/robotests/src/com/android/settings/applications/PictureInPictureDetailsTest.java b/tests/robotests/src/com/android/settings/applications/appinfo/PictureInPictureDetailsTest.java
similarity index 98%
rename from tests/robotests/src/com/android/settings/applications/PictureInPictureDetailsTest.java
rename to tests/robotests/src/com/android/settings/applications/appinfo/PictureInPictureDetailsTest.java
index 0369130..da603ca 100644
--- a/tests/robotests/src/com/android/settings/applications/PictureInPictureDetailsTest.java
+++ b/tests/robotests/src/com/android/settings/applications/appinfo/PictureInPictureDetailsTest.java
@@ -14,7 +14,7 @@
  * limitations under the License
  */
 
-package com.android.settings.applications;
+package com.android.settings.applications.appinfo;
 
 import static com.google.common.truth.Truth.assertThat;
 import static org.mockito.ArgumentMatchers.nullable;
diff --git a/tests/robotests/src/com/android/settings/applications/PictureInPictureSettingsTest.java b/tests/robotests/src/com/android/settings/applications/appinfo/PictureInPictureSettingsTest.java
similarity index 98%
rename from tests/robotests/src/com/android/settings/applications/PictureInPictureSettingsTest.java
rename to tests/robotests/src/com/android/settings/applications/appinfo/PictureInPictureSettingsTest.java
index dd3ec85..2ec9c96 100644
--- a/tests/robotests/src/com/android/settings/applications/PictureInPictureSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/applications/appinfo/PictureInPictureSettingsTest.java
@@ -14,7 +14,7 @@
  * limitations under the License
  */
 
-package com.android.settings.applications;
+package com.android.settings.applications.appinfo;
 
 import static com.google.common.truth.Truth.assertThat;
 
diff --git a/tests/robotests/src/com/android/settings/applications/WriteSettingsDetailsTest.java b/tests/robotests/src/com/android/settings/applications/appinfo/WriteSettingsDetailsTest.java
similarity index 97%
rename from tests/robotests/src/com/android/settings/applications/WriteSettingsDetailsTest.java
rename to tests/robotests/src/com/android/settings/applications/appinfo/WriteSettingsDetailsTest.java
index c2abefa..edcf64b 100644
--- a/tests/robotests/src/com/android/settings/applications/WriteSettingsDetailsTest.java
+++ b/tests/robotests/src/com/android/settings/applications/appinfo/WriteSettingsDetailsTest.java
@@ -14,7 +14,7 @@
  * limitations under the License
  */
 
-package com.android.settings.applications;
+package com.android.settings.applications.appinfo;
 
 import static org.mockito.ArgumentMatchers.nullable;
 import static org.mockito.Matchers.eq;
diff --git a/tests/robotests/src/com/android/settings/applications/appinfo/WriteSystemSettingsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/applications/appinfo/WriteSystemSettingsPreferenceControllerTest.java
new file mode 100644
index 0000000..fabcbb2
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/applications/appinfo/WriteSystemSettingsPreferenceControllerTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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.appinfo;
+
+import static android.Manifest.permission.SYSTEM_ALERT_WINDOW;
+import static android.Manifest.permission.WRITE_SETTINGS;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.content.pm.PackageInfo;
+import android.os.UserManager;
+import android.support.v7.preference.Preference;
+
+import com.android.settings.TestConfig;
+import com.android.settings.applications.AppInfoDashboardFragment;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+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;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class WriteSystemSettingsPreferenceControllerTest {
+
+    @Mock
+    private UserManager mUserManager;
+    @Mock
+    private AppInfoDashboardFragment mFragment;
+    @Mock
+    private Preference mPreference;
+
+    private Context mContext;
+    private WriteSystemSettingsPreferenceController mController;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = spy(RuntimeEnvironment.application);
+        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
+        mController = spy(new WriteSystemSettingsPreferenceController(mContext, mFragment));
+        final String key = mController.getPreferenceKey();
+        when(mPreference.getKey()).thenReturn(key);
+    }
+
+    @Test
+    public void getAvailabilityStatus_managedProfile_shouldReturnDisabled() {
+        when(mUserManager.isManagedProfile()).thenReturn(true);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_noPermissionRequested_shouldReturnDisabled() {
+        when(mUserManager.isManagedProfile()).thenReturn(false);
+        when(mFragment.getPackageInfo()).thenReturn(new PackageInfo());
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_noWriteSettingsPermission_shouldReturnDisabled() {
+        when(mUserManager.isManagedProfile()).thenReturn(false);
+        final PackageInfo info = new PackageInfo();
+        info.requestedPermissions = new String[] {SYSTEM_ALERT_WINDOW};
+        when(mFragment.getPackageInfo()).thenReturn(info);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.DISABLED_FOR_USER);
+    }
+
+    @Test
+    public void getAvailabilityStatus_hasWriteSettingsPermission_shouldReturnAvailable() {
+        when(mUserManager.isManagedProfile()).thenReturn(false);
+        final PackageInfo info = new PackageInfo();
+        info.requestedPermissions = new String[] {WRITE_SETTINGS};
+        when(mFragment.getPackageInfo()).thenReturn(info);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(mController.AVAILABLE);
+    }
+
+    @Test
+    public void getDetailFragmentClass_shouldReturnWriteSettingsDetails() {
+        assertThat(mController.getDetailFragmentClass()).isEqualTo(WriteSettingsDetails.class);
+    }
+
+    @Test
+    public void updateState_shouldSetSummary() {
+        final String summary = "test summary";
+        doReturn(summary).when(mController).getSummary();
+
+        mController.updateState(mPreference);
+
+        verify(mPreference).setSummary(summary);
+    }
+
+}
diff --git a/tests/robotests/src/com/android/settings/core/BasePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/core/BasePreferenceControllerTest.java
index 54b58d1..da2197c 100644
--- a/tests/robotests/src/com/android/settings/core/BasePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/core/BasePreferenceControllerTest.java
@@ -117,4 +117,4 @@
 
         assertThat(mPreferenceController.isSupported()).isTrue();
     }
-}
+}
\ No newline at end of file
diff --git a/tests/robotests/src/com/android/settings/development/featureflags/FeatureFlagPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/featureflags/FeatureFlagPreferenceControllerTest.java
index e505d03..f831506 100644
--- a/tests/robotests/src/com/android/settings/development/featureflags/FeatureFlagPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/development/featureflags/FeatureFlagPreferenceControllerTest.java
@@ -21,6 +21,7 @@
 import static com.google.common.truth.Truth.assertThat;
 
 import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.atLeastOnce;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -70,6 +71,6 @@
         mLifecycle.handleLifecycleEvent(ON_START);
 
         verify(mScreen).removeAll();
-        verify(mScreen).addPreference(any(FeatureFlagPreference.class));
+        verify(mScreen, atLeastOnce()).addPreference(any(FeatureFlagPreference.class));
     }
 }
diff --git a/tests/robotests/src/com/android/settings/deviceinfo/DeviceModelPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/DeviceModelPreferenceControllerTest.java
index 1c8bde1..347ca3a 100644
--- a/tests/robotests/src/com/android/settings/deviceinfo/DeviceModelPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/deviceinfo/DeviceModelPreferenceControllerTest.java
@@ -26,12 +26,16 @@
 
 import android.app.Fragment;
 import android.content.Context;
+import android.os.SystemProperties;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceScreen;
+import android.util.FeatureFlagUtils;
 
 import com.android.settings.R;
 import com.android.settings.TestConfig;
+import com.android.settings.core.FeatureFlags;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -72,7 +76,12 @@
     }
 
     @Test
+    @Config(shadows = {
+            SettingsShadowSystemProperties.class
+    })
     public void displayPref_shouldSetSummary() {
+        SystemProperties.set(FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + FeatureFlags.DEVICE_INFO_V2,
+                "true");
         mController.displayPreference(mPreferenceScreen);
 
         verify(mPreference).setSummary(mContext.getResources().getString(R.string.model_summary,
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryAppListPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryAppListPreferenceControllerTest.java
new file mode 100644
index 0000000..a814989
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryAppListPreferenceControllerTest.java
@@ -0,0 +1,223 @@
+/*
+ * 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;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.support.v14.preference.PreferenceFragment;
+import android.support.v7.preference.PreferenceGroup;
+import android.text.TextUtils;
+import android.text.format.DateUtils;
+import android.util.FeatureFlagUtils;
+import android.util.SparseArray;
+
+import com.android.internal.os.BatterySipper;
+import com.android.internal.os.BatteryStatsImpl;
+import com.android.settings.R;
+import com.android.settings.SettingsActivity;
+import com.android.settings.TestConfig;
+import com.android.settings.core.FeatureFlags;
+import com.android.settings.fuelgauge.anomaly.Anomaly;
+import com.android.settings.testutils.FakeFeatureFactory;
+import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+import java.util.List;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, shadows =
+        SettingsShadowSystemProperties.class)
+public class BatteryAppListPreferenceControllerTest {
+    private static final String[] PACKAGE_NAMES = {"com.app1", "com.app2"};
+    private static final String KEY_APP_LIST = "app_list";
+    private static final int UID = 123;
+
+    @Mock
+    private BatterySipper mNormalBatterySipper;
+    @Mock
+    private SettingsActivity mSettingsActivity;
+    @Mock
+    private PreferenceGroup mAppListGroup;
+    @Mock
+    private PreferenceFragment mFragment;
+    @Mock
+    private BatteryUtils mBatteryUtils;
+
+    private Context mContext;
+    private PowerGaugePreference mPreference;
+    private BatteryAppListPreferenceController mPreferenceController;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mContext = spy(RuntimeEnvironment.application);
+        FakeFeatureFactory.setupForTest();
+
+        mPreference = new PowerGaugePreference(mContext);
+        when(mNormalBatterySipper.getPackages()).thenReturn(PACKAGE_NAMES);
+        when(mNormalBatterySipper.getUid()).thenReturn(UID);
+        mNormalBatterySipper.drainType = BatterySipper.DrainType.APP;
+
+        mPreferenceController = new BatteryAppListPreferenceController(mContext, KEY_APP_LIST, null,
+                mSettingsActivity, mFragment);
+        mPreferenceController.mBatteryUtils = mBatteryUtils;
+        mPreferenceController.mAppListGroup = mAppListGroup;
+    }
+
+    @Test
+    public void testExtractKeyFromSipper_typeAPPUidObjectNull_returnPackageNames() {
+        mNormalBatterySipper.uidObj = null;
+        mNormalBatterySipper.drainType = BatterySipper.DrainType.APP;
+
+        final String key = mPreferenceController.extractKeyFromSipper(mNormalBatterySipper);
+        assertThat(key).isEqualTo(TextUtils.concat(mNormalBatterySipper.getPackages()).toString());
+    }
+
+    @Test
+    public void testExtractKeyFromSipper_typeOther_returnDrainType() {
+        mNormalBatterySipper.uidObj = null;
+        mNormalBatterySipper.drainType = BatterySipper.DrainType.BLUETOOTH;
+
+        final String key = mPreferenceController.extractKeyFromSipper(mNormalBatterySipper);
+        assertThat(key).isEqualTo(mNormalBatterySipper.drainType.toString());
+    }
+
+    @Test
+    public void testExtractKeyFromSipper_typeUser_returnDrainTypeWithUserId() {
+        mNormalBatterySipper.uidObj = null;
+        mNormalBatterySipper.drainType = BatterySipper.DrainType.USER;
+        mNormalBatterySipper.userId = 2;
+
+        final String key = mPreferenceController.extractKeyFromSipper(mNormalBatterySipper);
+        assertThat(key).isEqualTo("USER2");
+    }
+
+    @Test
+    public void testExtractKeyFromSipper_typeAPPUidObjectNotNull_returnUid() {
+        mNormalBatterySipper.uidObj = new BatteryStatsImpl.Uid(new BatteryStatsImpl(), UID);
+        mNormalBatterySipper.drainType = BatterySipper.DrainType.APP;
+
+        final String key = mPreferenceController.extractKeyFromSipper(mNormalBatterySipper);
+        assertThat(key).isEqualTo(Integer.toString(mNormalBatterySipper.getUid()));
+    }
+
+    @Test
+    public void testSetUsageSummary_timeLessThanOneMinute_DoNotSetSummary() {
+        mNormalBatterySipper.usageTimeMs = 59 * DateUtils.SECOND_IN_MILLIS;
+
+        mPreferenceController.setUsageSummary(mPreference, mNormalBatterySipper);
+        assertThat(mPreference.getSummary()).isNull();
+    }
+
+    @Test
+    public void testSetUsageSummary_timeMoreThanOneMinute_normalApp_setScreenSummary() {
+        mNormalBatterySipper.usageTimeMs = 2 * DateUtils.MINUTE_IN_MILLIS;
+        doReturn(mContext.getText(R.string.battery_used_for)).when(mFragment).getText(
+                R.string.battery_used_for);
+        doReturn(mContext).when(mFragment).getContext();
+
+        mPreferenceController.setUsageSummary(mPreference, mNormalBatterySipper);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Used for 2m");
+    }
+
+    @Test
+    public void testSetUsageSummary_timeMoreThanOneMinute_hiddenApp_setUsedSummary() {
+        mNormalBatterySipper.usageTimeMs = 2 * DateUtils.MINUTE_IN_MILLIS;
+        doReturn(true).when(mBatteryUtils).shouldHideSipper(mNormalBatterySipper);
+        doReturn(mContext).when(mFragment).getContext();
+
+        mPreferenceController.setUsageSummary(mPreference, mNormalBatterySipper);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("2m");
+    }
+
+    @Test
+    public void testSetUsageSummary_timeMoreThanOneMinute_notApp_setUsedSummary() {
+        mNormalBatterySipper.usageTimeMs = 2 * DateUtils.MINUTE_IN_MILLIS;
+        mNormalBatterySipper.drainType = BatterySipper.DrainType.PHONE;
+        doReturn(mContext).when(mFragment).getContext();
+
+        mPreferenceController.setUsageSummary(mPreference, mNormalBatterySipper);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("2m");
+    }
+
+    @Test
+    public void testRefreshAnomalyIcon_containsAnomaly_showAnomalyIcon() {
+        FeatureFlagUtils.setEnabled(mContext, FeatureFlags.BATTERY_DISPLAY_APP_LIST, true);
+        PowerGaugePreference preference = new PowerGaugePreference(mContext);
+        final String key = mPreferenceController.extractKeyFromUid(UID);
+        final SparseArray<List<Anomaly>> anomalySparseArray = new SparseArray<>();
+        anomalySparseArray.append(UID, null);
+        preference.setKey(key);
+        doReturn(preference).when(mAppListGroup).findPreference(key);
+
+        mPreferenceController.refreshAnomalyIcon(anomalySparseArray);
+
+        assertThat(preference.showAnomalyIcon()).isTrue();
+    }
+
+    @Test
+    public void testShouldHideSipper_typeOvercounted_returnTrue() {
+        mNormalBatterySipper.drainType = BatterySipper.DrainType.OVERCOUNTED;
+
+        assertThat(mPreferenceController.shouldHideSipper(mNormalBatterySipper)).isTrue();
+    }
+
+    @Test
+    public void testShouldHideSipper_typeUnaccounted_returnTrue() {
+        mNormalBatterySipper.drainType = BatterySipper.DrainType.UNACCOUNTED;
+
+        assertThat(mPreferenceController.shouldHideSipper(mNormalBatterySipper)).isTrue();
+    }
+
+    @Test
+    public void testShouldHideSipper_typeNormal_returnFalse() {
+        mNormalBatterySipper.drainType = BatterySipper.DrainType.APP;
+
+        assertThat(mPreferenceController.shouldHideSipper(mNormalBatterySipper)).isFalse();
+    }
+
+    @Test
+    public void testIsAvailable_featureOn_returnTrue() {
+        FeatureFlagUtils.setEnabled(mContext, FeatureFlags.BATTERY_DISPLAY_APP_LIST, true);
+
+        assertThat(mPreferenceController.isAvailable()).isTrue();
+    }
+
+    @Test
+    public void testIsAvailable_featureOff_returnFalse() {
+        FeatureFlagUtils.setEnabled(mContext, FeatureFlags.BATTERY_DISPLAY_APP_LIST, false);
+
+        assertThat(mPreferenceController.isAvailable()).isFalse();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
new file mode 100644
index 0000000..0ca983f
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
@@ -0,0 +1,422 @@
+/*
+ * 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.fuelgauge;
+
+import static com.android.settings.fuelgauge.PowerUsageSummary.MENU_HIGH_POWER_APPS;
+import static com.android.settings.fuelgauge.PowerUsageSummary.MENU_TOGGLE_APPS;
+
+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.eq;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.app.LoaderManager;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.os.PowerManager;
+import android.support.v7.preference.PreferenceScreen;
+import android.util.SparseArray;
+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;
+import com.android.internal.os.BatterySipper;
+import com.android.internal.os.BatteryStatsHelper;
+import com.android.settings.R;
+import com.android.settings.SettingsActivity;
+import com.android.settings.TestConfig;
+import com.android.settings.Utils;
+import com.android.settings.applications.LayoutPreference;
+import com.android.settings.fuelgauge.anomaly.Anomaly;
+import com.android.settings.fuelgauge.anomaly.AnomalyDetectionPolicy;
+import com.android.settings.testutils.FakeFeatureFactory;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.XmlTestUtils;
+import com.android.settings.testutils.shadow.SettingsShadowResources;
+import com.android.settingslib.core.AbstractPreferenceController;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.Robolectric;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Unit tests for {@link PowerUsageSummary}.
+ */
+// TODO: Improve this test class so that it starts up the real activity and fragment.
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH,
+        sdk = TestConfig.SDK_VERSION,
+        shadows = {
+                SettingsShadowResources.class,
+                SettingsShadowResources.SettingsShadowTheme.class,
+        })
+public class PowerUsageSummaryTest {
+    private static final String STUB_STRING = "stub_string";
+    private static final int UID = 123;
+    private static final int UID_2 = 234;
+    private static final int POWER_MAH = 100;
+    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 long USAGE_TIME_MS = 65 * 60 * 1000;
+    private static final double TOTAL_POWER = 200;
+    public static final String NEW_ML_EST_SUFFIX = "(New ML est)";
+    public static final String OLD_EST_SUFFIX = "(Old est)";
+    private static Intent sAdditionalBatteryInfoIntent;
+
+    @BeforeClass
+    public static void beforeClass() {
+        sAdditionalBatteryInfoIntent = new Intent("com.example.app.ADDITIONAL_BATTERY_INFO");
+    }
+
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private Context mContext;
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private Menu mMenu;
+    @Mock
+    private MenuItem mToggleAppsMenu;
+    @Mock
+    private MenuItem mHighPowerMenu;
+    @Mock
+    private MenuInflater mMenuInflater;
+    @Mock
+    private BatterySipper mNormalBatterySipper;
+    @Mock
+    private BatterySipper mScreenBatterySipper;
+    @Mock
+    private BatterySipper mCellBatterySipper;
+    @Mock
+    private LayoutPreference mBatteryLayoutPref;
+    @Mock
+    private TextView mBatteryPercentText;
+    @Mock
+    private TextView mSummary1;
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private BatteryStatsHelper mBatteryHelper;
+    @Mock
+    private PowerManager mPowerManager;
+    @Mock
+    private SettingsActivity mSettingsActivity;
+    @Mock
+    private LoaderManager mLoaderManager;
+    @Mock
+    private PreferenceScreen mPreferenceScreen;
+    @Mock
+    private AnomalyDetectionPolicy mAnomalyDetectionPolicy;
+    @Mock
+    private BatteryHeaderPreferenceController mBatteryHeaderPreferenceController;
+
+    private List<BatterySipper> mUsageList;
+    private Context mRealContext;
+    private TestFragment mFragment;
+    private FakeFeatureFactory mFeatureFactory;
+    private BatteryMeterView mBatteryMeterView;
+    private PowerGaugePreference mScreenUsagePref;
+    private PowerGaugePreference mLastFullChargePref;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mRealContext = RuntimeEnvironment.application;
+        mFeatureFactory = FakeFeatureFactory.setupForTest();
+        when(mContext.getSystemService(Context.POWER_SERVICE)).thenReturn(mPowerManager);
+
+        mScreenUsagePref = new PowerGaugePreference(mRealContext);
+        mLastFullChargePref = new PowerGaugePreference(mRealContext);
+        mFragment = spy(new TestFragment(mContext));
+        mFragment.initFeatureProvider();
+        mBatteryMeterView = new BatteryMeterView(mRealContext);
+        mBatteryMeterView.mDrawable = new BatteryMeterView.BatteryMeterDrawable(mRealContext, 0);
+        doNothing().when(mFragment).restartBatteryStatsLoader();
+        doReturn(mock(LoaderManager.class)).when(mFragment).getLoaderManager();
+
+        when(mFragment.getActivity()).thenReturn(mSettingsActivity);
+        when(mToggleAppsMenu.getItemId()).thenReturn(MENU_TOGGLE_APPS);
+        when(mHighPowerMenu.getItemId()).thenReturn(MENU_HIGH_POWER_APPS);
+        when(mFeatureFactory.powerUsageFeatureProvider.getAdditionalBatteryInfoIntent())
+                .thenReturn(sAdditionalBatteryInfoIntent);
+        when(mBatteryHelper.getTotalPower()).thenReturn(TOTAL_POWER);
+        when(mBatteryHelper.getStats().computeBatteryRealtime(anyLong(), anyInt())).thenReturn(
+                TIME_SINCE_LAST_FULL_CHARGE_US);
+
+        when(mNormalBatterySipper.getUid()).thenReturn(UID);
+        mNormalBatterySipper.totalPowerMah = POWER_MAH;
+        mNormalBatterySipper.drainType = BatterySipper.DrainType.APP;
+
+        mCellBatterySipper.drainType = BatterySipper.DrainType.CELL;
+        mCellBatterySipper.totalPowerMah = POWER_MAH;
+
+        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))
+                .thenReturn(mBatteryMeterView);
+        mFragment.setBatteryLayoutPreference(mBatteryLayoutPref);
+
+        mScreenBatterySipper.drainType = BatterySipper.DrainType.SCREEN;
+        mScreenBatterySipper.usageTimeMs = USAGE_TIME_MS;
+
+        mUsageList = new ArrayList<>();
+        mUsageList.add(mNormalBatterySipper);
+        mUsageList.add(mScreenBatterySipper);
+        mUsageList.add(mCellBatterySipper);
+
+        mFragment.mStatsHelper = mBatteryHelper;
+        when(mBatteryHelper.getUsageList()).thenReturn(mUsageList);
+        mFragment.mScreenUsagePref = mScreenUsagePref;
+        mFragment.mLastFullChargePref = mLastFullChargePref;
+        mFragment.mBatteryUtils = spy(new BatteryUtils(mRealContext));
+    }
+
+    @Test
+    public void testOptionsMenu_menuHighPower_metricEventInvoked() {
+        mFragment.onOptionsItemSelected(mHighPowerMenu);
+
+        verify(mFeatureFactory.metricsFeatureProvider).action(mContext,
+                MetricsProto.MetricsEvent.ACTION_SETTINGS_MENU_BATTERY_OPTIMIZATION);
+    }
+
+    @Test
+    public void testOptionsMenu_menuAppToggle_metricEventInvoked() {
+        mFragment.onOptionsItemSelected(mToggleAppsMenu);
+        mFragment.mShowAllApps = false;
+
+        verify(mFeatureFactory.metricsFeatureProvider).action(mContext,
+                MetricsProto.MetricsEvent.ACTION_SETTINGS_MENU_BATTERY_APPS_TOGGLE, true);
+    }
+
+    @Test
+    public void testOptionsMenu_toggleAppsEnabled() {
+        when(mFeatureFactory.powerUsageFeatureProvider.isPowerAccountingToggleEnabled())
+                .thenReturn(true);
+        mFragment.mShowAllApps = false;
+
+        mFragment.onCreateOptionsMenu(mMenu, mMenuInflater);
+
+        verify(mMenu).add(Menu.NONE, MENU_TOGGLE_APPS, Menu.NONE, R.string.show_all_apps);
+    }
+
+    @Test
+    public void testOptionsMenu_clickToggleAppsMenu_dataChanged() {
+        testToggleAllApps(true);
+        testToggleAllApps(false);
+    }
+
+    private void testToggleAllApps(final boolean isShowApps) {
+        mFragment.mShowAllApps = isShowApps;
+
+        mFragment.onOptionsItemSelected(mToggleAppsMenu);
+        assertThat(mFragment.mShowAllApps).isEqualTo(!isShowApps);
+    }
+
+    @Test
+    public void testFindBatterySipperByType_findTypeScreen() {
+        BatterySipper sipper = mFragment.findBatterySipperByType(mUsageList,
+                BatterySipper.DrainType.SCREEN);
+
+        assertThat(sipper).isSameAs(mScreenBatterySipper);
+    }
+
+    @Test
+    public void testFindBatterySipperByType_findTypeApp() {
+        BatterySipper sipper = mFragment.findBatterySipperByType(mUsageList,
+                BatterySipper.DrainType.APP);
+
+        assertThat(sipper).isSameAs(mNormalBatterySipper);
+    }
+
+    @Test
+    public void testUpdateScreenPreference_showCorrectSummary() {
+        doReturn(mScreenBatterySipper).when(mFragment).findBatterySipperByType(any(), any());
+        doReturn(mRealContext).when(mFragment).getContext();
+        final CharSequence expectedSummary = Utils.formatElapsedTime(mRealContext, USAGE_TIME_MS,
+                false);
+
+        mFragment.updateScreenPreference();
+
+        assertThat(mScreenUsagePref.getSubtitle()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void testUpdateLastFullChargePreference_showCorrectSummary() {
+        doReturn(mRealContext).when(mFragment).getContext();
+
+        mFragment.updateLastFullChargePreference(TIME_SINCE_LAST_FULL_CHARGE_MS);
+
+        assertThat(mLastFullChargePref.getSubtitle()).isEqualTo("2 hr. ago");
+    }
+
+    @Test
+    public void testUpdatePreference_usageListEmpty_shouldNotCrash() {
+        when(mBatteryHelper.getUsageList()).thenReturn(new ArrayList<BatterySipper>());
+        doReturn(STUB_STRING).when(mFragment).getString(anyInt(), any());
+        doReturn(mRealContext).when(mFragment).getContext();
+
+        // Should not crash when update
+        mFragment.updateScreenPreference();
+    }
+
+    @Test
+    public void testNonIndexableKeys_MatchPreferenceKeys() {
+        final Context context = RuntimeEnvironment.application;
+        final List<String> niks = PowerUsageSummary.SEARCH_INDEX_DATA_PROVIDER
+                .getNonIndexableKeys(context);
+
+        final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context,
+                R.xml.power_usage_summary);
+
+        assertThat(keys).containsAllIn(niks);
+    }
+
+    @Test
+    public void testPreferenceControllers_getPreferenceKeys_existInPreferenceScreen() {
+        final Context context = RuntimeEnvironment.application;
+        final PowerUsageSummary fragment = new PowerUsageSummary();
+        final List<String> preferenceScreenKeys = XmlTestUtils.getKeysFromPreferenceXml(context,
+                fragment.getPreferenceScreenResId());
+        final List<String> preferenceKeys = new ArrayList<>();
+
+        for (AbstractPreferenceController controller : fragment.getPreferenceControllers(context)) {
+            preferenceKeys.add(controller.getPreferenceKey());
+        }
+
+        assertThat(preferenceScreenKeys).containsAllIn(preferenceKeys);
+    }
+
+    @Test
+    public void testUpdateAnomalySparseArray() {
+        mFragment.mAnomalySparseArray = new SparseArray<>();
+        final List<Anomaly> anomalies = new ArrayList<>();
+        final Anomaly anomaly1 = new Anomaly.Builder().setUid(UID).build();
+        final Anomaly anomaly2 = new Anomaly.Builder().setUid(UID).build();
+        final Anomaly anomaly3 = new Anomaly.Builder().setUid(UID_2).build();
+        anomalies.add(anomaly1);
+        anomalies.add(anomaly2);
+        anomalies.add(anomaly3);
+
+        mFragment.updateAnomalySparseArray(anomalies);
+
+        assertThat(mFragment.mAnomalySparseArray.get(UID)).containsExactly(anomaly1, anomaly2);
+        assertThat(mFragment.mAnomalySparseArray.get(UID_2)).containsExactly(anomaly3);
+    }
+
+    @Test
+    public void testInitAnomalyDetectionIfPossible_detectionEnabled_init() {
+        doReturn(mLoaderManager).when(mFragment).getLoaderManager();
+        doReturn(mAnomalyDetectionPolicy).when(mFragment).getAnomalyDetectionPolicy();
+        when(mAnomalyDetectionPolicy.isAnomalyDetectionEnabled()).thenReturn(true);
+
+        mFragment.restartAnomalyDetectionIfPossible();
+
+        verify(mLoaderManager).restartLoader(eq(PowerUsageSummary.ANOMALY_LOADER), eq(Bundle.EMPTY),
+                any());
+    }
+
+    @Test
+    public void testShowBothEstimates_summariesAreBothModified() {
+        doReturn(new TextView(mRealContext)).when(mBatteryLayoutPref).findViewById(R.id.summary2);
+        doReturn(new TextView(mRealContext)).when(mBatteryLayoutPref).findViewById(R.id.summary1);
+        mFragment.onLongClick(new View(mRealContext));
+        TextView summary1 = mFragment.mBatteryLayoutPref.findViewById(R.id.summary1);
+        TextView summary2 = mFragment.mBatteryLayoutPref.findViewById(R.id.summary2);
+        Robolectric.flushBackgroundThreadScheduler();
+        assertThat(summary2.getText().toString().contains(NEW_ML_EST_SUFFIX));
+        assertThat(summary1.getText().toString().contains(OLD_EST_SUFFIX));
+    }
+
+    @Test
+    public void testSaveInstanceState_showAllAppsRestored() {
+        Bundle bundle = new Bundle();
+        mFragment.mShowAllApps = true;
+        doReturn(mPreferenceScreen).when(mFragment).getPreferenceScreen();
+
+        mFragment.onSaveInstanceState(bundle);
+        mFragment.restoreSavedInstance(bundle);
+
+        assertThat(mFragment.mShowAllApps).isTrue();
+    }
+
+    @Test
+    public void testDebugMode() {
+        doReturn(true).when(mFeatureFactory.powerUsageFeatureProvider).isEstimateDebugEnabled();
+
+        mFragment.restartBatteryInfoLoader();
+        ArgumentCaptor<View.OnLongClickListener> listener = ArgumentCaptor.forClass(
+                View.OnLongClickListener.class);
+        verify(mSummary1).setOnLongClickListener(listener.capture());
+
+        // Calling the listener should disable it.
+        listener.getValue().onLongClick(mSummary1);
+        verify(mSummary1).setOnLongClickListener(null);
+
+        // Restarting the loader should reset the listener.
+        mFragment.restartBatteryInfoLoader();
+        verify(mSummary1, times(2)).setOnLongClickListener(any(View.OnLongClickListener.class));
+    }
+
+    @Test
+    public void testRestartBatteryStatsLoader_notClearHeader_quickUpdateNotInvoked() {
+        mFragment.mBatteryHeaderPreferenceController = mBatteryHeaderPreferenceController;
+
+        mFragment.restartBatteryStatsLoader(false /* clearHeader */);
+
+        verify(mBatteryHeaderPreferenceController, never()).quickUpdateHeaderPreference();
+    }
+
+    public static class TestFragment extends PowerUsageSummary {
+        private Context mContext;
+
+        public TestFragment(Context context) {
+            mContext = context;
+        }
+
+        @Override
+        public Context getContext() {
+            return mContext;
+        }
+
+
+        @Override
+        protected void refreshUi() {
+            // Leave it empty for toggle apps menu test
+        }
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/notification/VisibilityPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/VisibilityPreferenceControllerTest.java
index cdd1e9e..c18372c 100644
--- a/tests/robotests/src/com/android/settings/notification/VisibilityPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/VisibilityPreferenceControllerTest.java
@@ -205,10 +205,11 @@
         RestrictedDropDownPreference pref = mock(RestrictedDropDownPreference.class);
         mController.updateState(pref);
 
-        ArgumentCaptor<String[]> argumentCaptor = ArgumentCaptor.forClass(String[].class);
+        ArgumentCaptor<CharSequence[]> argumentCaptor =
+            ArgumentCaptor.forClass(CharSequence[].class);
         verify(pref, times(1)).setEntryValues(argumentCaptor.capture());
-        assertFalse(Arrays.asList(argumentCaptor.getValue())
-                .contains(VISIBILITY_NO_OVERRIDE));
+        assertFalse(toStringList(argumentCaptor.getValue())
+                .contains(String.valueOf(VISIBILITY_NO_OVERRIDE)));
     }
 
     @Test
@@ -223,10 +224,11 @@
         RestrictedDropDownPreference pref = mock(RestrictedDropDownPreference.class);
         mController.updateState(pref);
 
-        ArgumentCaptor<String[]> argumentCaptor = ArgumentCaptor.forClass(String[].class);
+        ArgumentCaptor<CharSequence[]> argumentCaptor =
+            ArgumentCaptor.forClass(CharSequence[].class);
         verify(pref, times(1)).setEntryValues(argumentCaptor.capture());
-        assertFalse(Arrays.asList(argumentCaptor.getValue())
-                .contains(VISIBILITY_NO_OVERRIDE));
+        assertFalse(toStringList(argumentCaptor.getValue())
+                .contains(String.valueOf(VISIBILITY_NO_OVERRIDE)));
     }
 
     @Test
@@ -238,15 +240,24 @@
         RestrictedDropDownPreference pref = mock(RestrictedDropDownPreference.class);
         mController.updateState(pref);
 
-        ArgumentCaptor<String[]> argumentCaptor = ArgumentCaptor.forClass(String[].class);
+        ArgumentCaptor<CharSequence[]> argumentCaptor =
+            ArgumentCaptor.forClass(CharSequence[].class);
         verify(pref, times(1)).setEntryValues(argumentCaptor.capture());
-        List<String> values = Arrays.asList(argumentCaptor.getValue());
+        List<String> values = toStringList(argumentCaptor.getValue());
         assertEquals(3, values.size());
         assertTrue(values.contains(String.valueOf(VISIBILITY_NO_OVERRIDE)));
         assertTrue(values.contains(String.valueOf(Notification.VISIBILITY_PRIVATE)));
         assertTrue(values.contains(String.valueOf(Notification.VISIBILITY_SECRET)));
     }
 
+    private static List<String> toStringList(CharSequence[] charSequences) {
+        List<String> result = new ArrayList<>();
+        for (CharSequence charSequence : charSequences) {
+            result.add(charSequence.toString());
+        }
+        return result;
+    }
+
     @Test
     public void testUpdateState_noChannelOverride() throws Exception {
         Settings.Secure.putInt(mContext.getContentResolver(),
diff --git a/tests/unit/Android.mk b/tests/unit/Android.mk
index 58fe7dd..cbf91db 100644
--- a/tests/unit/Android.mk
+++ b/tests/unit/Android.mk
@@ -5,14 +5,19 @@
 LOCAL_MODULE_TAGS := tests
 LOCAL_CERTIFICATE := platform
 
-LOCAL_JAVA_LIBRARIES := android.test.runner telephony-common ims-common
+LOCAL_JAVA_LIBRARIES := \
+    android.test.runner \
+    telephony-common \
+    ims-common \
+    android.test.base \
+    android.test.mock \
+
 
 LOCAL_STATIC_JAVA_LIBRARIES := \
     android-support-test \
     espresso-core \
     espresso-contrib-nodep \
     espresso-intents-nodep \
-    legacy-android-test \
     mockito-target-minus-junit4 \
     platform-test-annotations \
     truth-prebuilt \
diff --git a/tests/unit/src/com/android/settings/applications/ExternalSourcesSettingsTest.java b/tests/unit/src/com/android/settings/applications/ExternalSourcesSettingsTest.java
index 82f0e0a..f7e956b 100644
--- a/tests/unit/src/com/android/settings/applications/ExternalSourcesSettingsTest.java
+++ b/tests/unit/src/com/android/settings/applications/ExternalSourcesSettingsTest.java
@@ -37,6 +37,7 @@
 import android.provider.Settings;
 import android.support.test.InstrumentationRegistry;
 import android.support.test.filters.LargeTest;
+import android.support.test.filters.Suppress;
 import android.support.test.runner.AndroidJUnit4;
 import android.support.test.uiautomator.By;
 import android.support.test.uiautomator.BySelector;
@@ -56,6 +57,7 @@
 
 import java.util.List;
 
+@Suppress
 @RunWith(AndroidJUnit4.class)
 @LargeTest
 public class ExternalSourcesSettingsTest {