Merge "Update feature flag dashboard UI to display all features"
diff --git a/Android.mk b/Android.mk
index c59d938..b9121ec 100644
--- a/Android.mk
+++ b/Android.mk
@@ -20,13 +20,16 @@
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
 LOCAL_STATIC_ANDROID_LIBRARIES := \
+    android-slices-builders \
+    android-slices-core \
+    android-slices-view \
     android-support-v4 \
     android-support-v13 \
     android-support-v7-appcompat \
     android-support-v7-cardview \
     android-support-v7-preference \
     android-support-v7-recyclerview \
-    android-support-v14-preference
+    android-support-v14-preference \
 
 LOCAL_JAVA_LIBRARIES := \
     bouncycastle \
@@ -34,8 +37,10 @@
     ims-common
 
 LOCAL_STATIC_JAVA_LIBRARIES := \
+    apptoolkit-arch-core-runtime \
+    apptoolkit-lifecycle-extensions \
     jsr305 \
-    settings-logtags
+    settings-logtags \
 
 LOCAL_PROGUARD_FLAG_FILES := proguard.flags
 
diff --git a/proguard.flags b/proguard.flags
index d644f47..091211d 100644
--- a/proguard.flags
+++ b/proguard.flags
@@ -27,6 +27,9 @@
 -keepclasseswithmembers class * {
     public <init>(android.content.Context, android.util.AttributeSet, int);
 }
+-keepclasseswithmembers class * {
+    public <init>(android.content.Context, android.util.AttributeSet, int, int);
+}
 
 # Keep annotated classes or class members.
 -keep @android.support.annotation.Keep class *
diff --git a/res/layout/preference_dropdown_material_settings.xml b/res/layout/preference_dropdown_material_settings.xml
new file mode 100644
index 0000000..a3f5ab9
--- /dev/null
+++ b/res/layout/preference_dropdown_material_settings.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  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.
+  -->
+
+
+<!-- Based off frameworks/base/core/res/res/layout/preference_dropdown_material.xml
+     except that icon space in this layout is always reserved -->
+<FrameLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content">
+
+    <Spinner
+        android:id="@+id/spinner"
+        android:layout_width="0dp"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="@dimen/preference_no_icon_padding_start"
+        android:visibility="invisible" />
+
+    <include layout="@layout/preference_material"/>
+
+</FrameLayout>
\ No newline at end of file
diff --git a/res/values/styles_preference.xml b/res/values/styles_preference.xml
index b8b9eb3..056d55a 100644
--- a/res/values/styles_preference.xml
+++ b/res/values/styles_preference.xml
@@ -20,19 +20,19 @@
 <resources>
 
     <!-- Fragment style -->
-    <style name="SettingsPreferenceFragmentStyle" parent="@style/PreferenceFragment.Material">
+    <style name="SettingsPreferenceFragmentStyle" parent="@style/PreferenceFragmentStyle.SettingsBase">
         <item name="android:layout">@layout/preference_list_fragment</item>
     </style>
 
-    <style name="ApnPreference" parent="@style/Preference.Material">
+    <style name="ApnPreference" parent="Preference.SettingsBase">
         <item name="android:layout">@layout/apn_preference_layout</item>
     </style>
 
-    <style name="SettingsSeekBarPreference" parent="@style/Preference.Material">
+    <style name="SettingsSeekBarPreference" parent="Preference.SettingsBase">
         <item name="android:layout">@layout/preference_widget_seekbar_settings</item>
     </style>
 
-    <style name="SyncSwitchPreference" parent="@style/Preference.Material">
+    <style name="SyncSwitchPreference" parent="Preference.SettingsBase">
         <item name="android:widgetLayout">@layout/preference_widget_sync_toggle</item>
     </style>
 
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/LicenseHtmlGeneratorFromXml.java b/src/com/android/settings/LicenseHtmlGeneratorFromXml.java
deleted file mode 100644
index 7025c5a..0000000
--- a/src/com/android/settings/LicenseHtmlGeneratorFromXml.java
+++ /dev/null
@@ -1,292 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.settings;
-
-import android.support.annotation.VisibleForTesting;
-import android.text.TextUtils;
-import android.util.Log;
-import android.util.Xml;
-
-import org.xmlpull.v1.XmlPullParser;
-import org.xmlpull.v1.XmlPullParserException;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.zip.GZIPInputStream;
-
-/**
- * The utility class that generate a license html file from xml files.
- * All the HTML snippets and logic are copied from build/make/tools/generate-notice-files.py.
- *
- * TODO: Remove duplicate codes once backward support ends.
- */
-class LicenseHtmlGeneratorFromXml {
-    private static final String TAG = "LicenseHtmlGeneratorFromXml";
-
-    private static final String TAG_ROOT = "licenses";
-    private static final String TAG_FILE_NAME = "file-name";
-    private static final String TAG_FILE_CONTENT = "file-content";
-    private static final String ATTR_CONTENT_ID = "contentId";
-
-    private static final String HTML_HEAD_STRING =
-            "<html><head>\n" +
-            "<style type=\"text/css\">\n" +
-            "body { padding: 0; font-family: sans-serif; }\n" +
-            ".same-license { background-color: #eeeeee;\n" +
-            "                border-top: 20px solid white;\n" +
-            "                padding: 10px; }\n" +
-            ".label { font-weight: bold; }\n" +
-            ".file-list { margin-left: 1em; color: blue; }\n" +
-            "</style>\n" +
-            "</head>" +
-            "<body topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\">\n" +
-            "<div class=\"toc\">\n" +
-            "<ul>";
-
-    private static final String HTML_MIDDLE_STRING =
-            "</ul>\n" +
-            "</div><!-- table of contents -->\n" +
-            "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
-
-    private static final String HTML_REAR_STRING =
-            "</table></body></html>";
-
-    private final List<File> mXmlFiles;
-
-    /*
-     * A map from a file name to a content id (MD5 sum of file content) for its license.
-     * For example, "/system/priv-app/TeleService/TeleService.apk" maps to
-     * "9645f39e9db895a4aa6e02cb57294595". Here "9645f39e9db895a4aa6e02cb57294595" is a MD5 sum
-     * of the content of packages/services/Telephony/MODULE_LICENSE_APACHE2.
-     */
-    private final Map<String, String> mFileNameToContentIdMap = new HashMap();
-
-    /*
-     * A map from a content id (MD5 sum of file content) to a license file content.
-     * For example, "9645f39e9db895a4aa6e02cb57294595" maps to the content string of
-     * packages/services/Telephony/MODULE_LICENSE_APACHE2. Here "9645f39e9db895a4aa6e02cb57294595"
-     * is a MD5 sum of the file content.
-     */
-    private final Map<String, String> mContentIdToFileContentMap = new HashMap();
-
-    static class ContentIdAndFileNames {
-        final String mContentId;
-        final List<String> mFileNameList = new ArrayList();
-
-        ContentIdAndFileNames(String contentId) {
-            mContentId = contentId;
-        }
-    }
-
-    private LicenseHtmlGeneratorFromXml(List<File> xmlFiles) {
-        mXmlFiles = xmlFiles;
-    }
-
-    public static boolean generateHtml(List<File> xmlFiles, File outputFile) {
-        LicenseHtmlGeneratorFromXml genertor = new LicenseHtmlGeneratorFromXml(xmlFiles);
-        return genertor.generateHtml(outputFile);
-    }
-
-    private boolean generateHtml(File outputFile) {
-        for (File xmlFile : mXmlFiles) {
-            parse(xmlFile);
-        }
-
-        if (mFileNameToContentIdMap.isEmpty() || mContentIdToFileContentMap.isEmpty()) {
-            return false;
-        }
-
-        PrintWriter writer = null;
-        try {
-            writer = new PrintWriter(outputFile);
-
-            generateHtml(mFileNameToContentIdMap, mContentIdToFileContentMap, writer);
-
-            writer.flush();
-            writer.close();
-            return true;
-        } catch (FileNotFoundException | SecurityException e) {
-            Log.e(TAG, "Failed to generate " + outputFile, e);
-
-            if (writer != null) {
-                writer.close();
-            }
-            return false;
-        }
-    }
-
-    private void parse(File xmlFile) {
-        if (xmlFile == null || !xmlFile.exists() || xmlFile.length() == 0) {
-            return;
-        }
-
-        InputStreamReader in = null;
-        try {
-            if (xmlFile.getName().endsWith(".gz")) {
-                in = new InputStreamReader(new GZIPInputStream(new FileInputStream(xmlFile)));
-            } else {
-                in = new FileReader(xmlFile);
-            }
-
-            parse(in, mFileNameToContentIdMap, mContentIdToFileContentMap);
-
-            in.close();
-        } catch (XmlPullParserException | IOException e) {
-            Log.e(TAG, "Failed to parse " + xmlFile, e);
-            if (in != null) {
-                try {
-                    in.close();
-                } catch (IOException ie) {
-                    Log.w(TAG, "Failed to close " + xmlFile);
-                }
-            }
-        }
-    }
-
-    /*
-     * Parses an input stream and fills a map from a file name to a content id for its license
-     * and a map from a content id to a license file content.
-     *
-     * Following xml format is expected from the input stream.
-     *
-     *     <licenses>
-     *     <file-name contentId="content_id_of_license1">file1</file-name>
-     *     <file-name contentId="content_id_of_license2">file2</file-name>
-     *     ...
-     *     <file-content contentId="content_id_of_license1">license1 file contents</file-content>
-     *     <file-content contentId="content_id_of_license2">license2 file contents</file-content>
-     *     ...
-     *     </licenses>
-     */
-    @VisibleForTesting
-    static void parse(InputStreamReader in, Map<String, String> outFileNameToContentIdMap,
-            Map<String, String> outContentIdToFileContentMap)
-                    throws XmlPullParserException, IOException {
-        Map<String, String> fileNameToContentIdMap = new HashMap<String, String>();
-        Map<String, String> contentIdToFileContentMap = new HashMap<String, String>();
-
-        XmlPullParser parser = Xml.newPullParser();
-        parser.setInput(in);
-        parser.nextTag();
-
-        parser.require(XmlPullParser.START_TAG, "", TAG_ROOT);
-
-        int state = parser.getEventType();
-        while (state != XmlPullParser.END_DOCUMENT) {
-            if (state == XmlPullParser.START_TAG) {
-                if (TAG_FILE_NAME.equals(parser.getName())) {
-                    String contentId = parser.getAttributeValue("", ATTR_CONTENT_ID);
-                        if (!TextUtils.isEmpty(contentId)) {
-                        String fileName = readText(parser).trim();
-                        if (!TextUtils.isEmpty(fileName)) {
-                            fileNameToContentIdMap.put(fileName, contentId);
-                        }
-                    }
-                } else if (TAG_FILE_CONTENT.equals(parser.getName())) {
-                    String contentId = parser.getAttributeValue("", ATTR_CONTENT_ID);
-                    if (!TextUtils.isEmpty(contentId) &&
-                            !outContentIdToFileContentMap.containsKey(contentId) &&
-                            !contentIdToFileContentMap.containsKey(contentId)) {
-                        String fileContent = readText(parser);
-                        if (!TextUtils.isEmpty(fileContent)) {
-                            contentIdToFileContentMap.put(contentId, fileContent);
-                        }
-                    }
-                }
-            }
-
-            state = parser.next();
-        }
-        outFileNameToContentIdMap.putAll(fileNameToContentIdMap);
-        outContentIdToFileContentMap.putAll(contentIdToFileContentMap);
-    }
-
-    private static String readText(XmlPullParser parser)
-            throws IOException, XmlPullParserException {
-        StringBuffer result = new StringBuffer();
-        int state = parser.next();
-        while (state == XmlPullParser.TEXT) {
-            result.append(parser.getText());
-            state = parser.next();
-        }
-        return result.toString();
-    }
-
-    @VisibleForTesting
-    static void generateHtml(Map<String, String> fileNameToContentIdMap,
-            Map<String, String> contentIdToFileContentMap, PrintWriter writer) {
-        List<String> fileNameList = new ArrayList();
-        fileNameList.addAll(fileNameToContentIdMap.keySet());
-        Collections.sort(fileNameList);
-
-        writer.println(HTML_HEAD_STRING);
-
-        int count = 0;
-        Map<String, Integer> contentIdToOrderMap = new HashMap();
-        List<ContentIdAndFileNames> contentIdAndFileNamesList = new ArrayList();
-
-        // Prints all the file list with a link to its license file content.
-        for (String fileName : fileNameList) {
-            String contentId = fileNameToContentIdMap.get(fileName);
-            // Assigns an id to a newly referred license file content.
-            if (!contentIdToOrderMap.containsKey(contentId)) {
-                contentIdToOrderMap.put(contentId, count);
-
-                // An index in contentIdAndFileNamesList is the order of each element.
-                contentIdAndFileNamesList.add(new ContentIdAndFileNames(contentId));
-                count++;
-            }
-
-            int id = contentIdToOrderMap.get(contentId);
-            contentIdAndFileNamesList.get(id).mFileNameList.add(fileName);
-            writer.format("<li><a href=\"#id%d\">%s</a></li>\n", id, fileName);
-        }
-
-        writer.println(HTML_MIDDLE_STRING);
-
-        count = 0;
-        // Prints all contents of the license files in order of id.
-        for (ContentIdAndFileNames contentIdAndFileNames : contentIdAndFileNamesList) {
-            writer.format("<tr id=\"id%d\"><td class=\"same-license\">\n", count);
-            writer.println("<div class=\"label\">Notices for file(s):</div>");
-            writer.println("<div class=\"file-list\">");
-            for (String fileName : contentIdAndFileNames.mFileNameList) {
-                writer.format("%s <br/>\n", fileName);
-            }
-            writer.println("</div><!-- file-list -->");
-            writer.println("<pre class=\"license-text\">");
-            writer.println(contentIdToFileContentMap.get(
-                    contentIdAndFileNames.mContentId));
-            writer.println("</pre><!-- license-text -->");
-            writer.println("</td></tr><!-- same-license -->");
-
-            count++;
-        }
-
-        writer.println(HTML_REAR_STRING);
-    }
-}
diff --git a/src/com/android/settings/LicenseHtmlLoader.java b/src/com/android/settings/LicenseHtmlLoader.java
deleted file mode 100644
index 9717926..0000000
--- a/src/com/android/settings/LicenseHtmlLoader.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.settings;
-
-import android.content.Context;
-import android.support.annotation.VisibleForTesting;
-import android.util.Log;
-
-import com.android.settings.utils.AsyncLoader;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * LicenseHtmlLoader is a loader which loads a license html file from default license xml files.
- */
-class LicenseHtmlLoader extends AsyncLoader<File> {
-    private static final String TAG = "LicenseHtmlLoader";
-
-    private static final String[] DEFAULT_LICENSE_XML_PATHS = {
-                "/system/etc/NOTICE.xml.gz",
-                "/vendor/etc/NOTICE.xml.gz",
-                "/odm/etc/NOTICE.xml.gz",
-                "/oem/etc/NOTICE.xml.gz"};
-    private static final String NOTICE_HTML_FILE_NAME = "NOTICE.html";
-
-    private Context mContext;
-
-    public LicenseHtmlLoader(Context context) {
-        super(context);
-        mContext = context;
-    }
-
-    @Override
-    public File loadInBackground() {
-        return generateHtmlFromDefaultXmlFiles();
-    }
-
-    @Override
-    protected void onDiscardResult(File f) {
-    }
-
-    private File generateHtmlFromDefaultXmlFiles() {
-        final List<File> xmlFiles = getVaildXmlFiles();
-        if (xmlFiles.isEmpty()) {
-            Log.e(TAG, "No notice file exists.");
-            return null;
-        }
-
-        File cachedHtmlFile = getCachedHtmlFile();
-        if(!isCachedHtmlFileOutdated(xmlFiles, cachedHtmlFile) ||
-                generateHtmlFile(xmlFiles, cachedHtmlFile)) {
-            return cachedHtmlFile;
-        }
-
-        return null;
-    }
-
-    @VisibleForTesting
-    List<File> getVaildXmlFiles() {
-        final List<File> xmlFiles = new ArrayList();
-        for (final String xmlPath : DEFAULT_LICENSE_XML_PATHS) {
-            File file = new File(xmlPath);
-            if (file.exists() && file.length() != 0) {
-                xmlFiles.add(file);
-            }
-        }
-        return xmlFiles;
-    }
-
-    @VisibleForTesting
-    File getCachedHtmlFile() {
-        return new File(mContext.getCacheDir(), NOTICE_HTML_FILE_NAME);
-    }
-
-    @VisibleForTesting
-    boolean isCachedHtmlFileOutdated(List<File> xmlFiles, File cachedHtmlFile) {
-        boolean outdated = true;
-        if (cachedHtmlFile.exists() && cachedHtmlFile.length() != 0) {
-            outdated = false;
-            for (File file : xmlFiles) {
-                if (cachedHtmlFile.lastModified() < file.lastModified()) {
-                    outdated = true;
-                    break;
-                }
-            }
-        }
-        return outdated;
-    }
-
-    @VisibleForTesting
-    boolean generateHtmlFile(List<File> xmlFiles, File htmlFile) {
-        return LicenseHtmlGeneratorFromXml.generateHtml(xmlFiles, htmlFile);
-    }
-}
diff --git a/src/com/android/settings/SettingsLicenseActivity.java b/src/com/android/settings/SettingsLicenseActivity.java
index 5b23a68..ebb1ae1 100644
--- a/src/com/android/settings/SettingsLicenseActivity.java
+++ b/src/com/android/settings/SettingsLicenseActivity.java
@@ -20,12 +20,10 @@
 import android.app.LoaderManager;
 import android.content.ActivityNotFoundException;
 import android.content.ContentResolver;
-import android.content.Context;
 import android.content.Intent;
 import android.content.Loader;
 import android.net.Uri;
 import android.os.Bundle;
-import android.os.StrictMode;
 import android.os.SystemProperties;
 import android.support.annotation.VisibleForTesting;
 import android.support.v4.content.FileProvider;
@@ -34,10 +32,9 @@
 import android.widget.Toast;
 
 import com.android.settings.users.RestrictedProfileSettings;
+import com.android.settingslib.license.LicenseHtmlLoader;
 
 import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
 
 /**
  * The "dialog" that shows from "License" in the Settings app.
@@ -111,9 +108,9 @@
             return;
         }
         showHtmlFromUri(Uri.fromFile(file));
-     }
+    }
 
-     private void showHtmlFromUri(Uri uri) {
+    private void showHtmlFromUri(Uri uri) {
         // Kick off external viewer due to WebView security restrictions; we
         // carefully point it at HTMLViewer, since it offers to decompress
         // before viewing.
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/FetchPackageStorageAsyncLoader.java b/src/com/android/settings/applications/FetchPackageStorageAsyncLoader.java
index b39ec3b..9ff96c1 100644
--- a/src/com/android/settings/applications/FetchPackageStorageAsyncLoader.java
+++ b/src/com/android/settings/applications/FetchPackageStorageAsyncLoader.java
@@ -24,9 +24,9 @@
 import android.util.Log;
 
 import com.android.internal.util.Preconditions;
-import com.android.settings.utils.AsyncLoader;
 import com.android.settingslib.applications.StorageStatsSource;
 import com.android.settingslib.applications.StorageStatsSource.AppStorageStats;
+import com.android.settingslib.utils.AsyncLoader;
 
 import java.io.IOException;
 
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/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/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/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/SuggestionLoader.java b/src/com/android/settings/dashboard/suggestions/SuggestionLoader.java
index b9d51ce..8c5b46d 100644
--- a/src/com/android/settings/dashboard/suggestions/SuggestionLoader.java
+++ b/src/com/android/settings/dashboard/suggestions/SuggestionLoader.java
@@ -20,7 +20,7 @@
 import android.service.settings.suggestions.Suggestion;
 import android.util.Log;
 
-import com.android.settings.utils.AsyncLoader;
+import com.android.settingslib.utils.AsyncLoader;
 
 import java.util.List;
 
diff --git a/src/com/android/settings/datausage/AppPrefLoader.java b/src/com/android/settings/datausage/AppPrefLoader.java
index 30e30eb..b684813 100644
--- a/src/com/android/settings/datausage/AppPrefLoader.java
+++ b/src/com/android/settings/datausage/AppPrefLoader.java
@@ -21,7 +21,7 @@
 import android.content.pm.PackageManager;
 import android.support.v7.preference.Preference;
 import android.util.ArraySet;
-import com.android.settings.utils.AsyncLoader;
+import com.android.settingslib.utils.AsyncLoader;
 
 public class AppPrefLoader extends AsyncLoader<ArraySet<Preference>> {
     private ArraySet<String> mPackages;
diff --git a/src/com/android/settings/deviceinfo/StorageWizardInit.java b/src/com/android/settings/deviceinfo/StorageWizardInit.java
index ffc07e5..05c7b15 100644
--- a/src/com/android/settings/deviceinfo/StorageWizardInit.java
+++ b/src/com/android/settings/deviceinfo/StorageWizardInit.java
@@ -16,18 +16,26 @@
 
 package com.android.settings.deviceinfo;
 
+import static com.android.settings.deviceinfo.StorageSettings.TAG;
+
 import android.app.ActivityManager;
 import android.content.Intent;
+import android.os.AsyncTask;
 import android.os.Bundle;
+import android.os.Environment;
 import android.os.UserManager;
 import android.os.storage.DiskInfo;
 import android.os.storage.VolumeInfo;
+import android.util.DebugUtils;
+import android.util.Log;
 import android.widget.CompoundButton;
 import android.widget.CompoundButton.OnCheckedChangeListener;
 import android.widget.RadioButton;
 
 import com.android.settings.R;
 
+import java.io.File;
+
 public class StorageWizardInit extends StorageWizardBase {
     private RadioButton mRadioExternal;
     private RadioButton mRadioInternal;
@@ -69,12 +77,15 @@
             mRadioExternal.setChecked(true);
             onNavigateNext();
             finish();
-        }
-
-        // TODO: Show a message about why this is disabled for guest and that only an admin user
-        // can adopt an sd card.
-        if (!mIsPermittedToAdopt) {
+        } else if (!mIsPermittedToAdopt) {
+            // TODO: Show a message about why this is disabled for guest and
+            // that only an admin user can adopt an sd card.
             mRadioInternal.setEnabled(false);
+        } else if (mVolume != null && mVolume.getType() == VolumeInfo.TYPE_PUBLIC
+                && mVolume.isMountedReadable()) {
+            // Device is mounted, so classify contents to possibly pick a
+            // recommended default operation.
+            new ClassifyTask().execute(mVolume.getPath());
         }
     }
 
@@ -121,4 +132,29 @@
             startActivity(intent);
         }
     }
+
+    /**
+     * Task that classifies the contents of a mounted storage device, and sets a
+     * recommended default operation based on result.
+     */
+    public class ClassifyTask extends AsyncTask<File, Void, Integer> {
+        @Override
+        protected Integer doInBackground(File... params) {
+            int classes = Environment.classifyExternalStorageDirectory(params[0]);
+            Log.v(TAG, "Classified " + params[0] + " as "
+                    + DebugUtils.flagsToString(Environment.class, "HAS_", classes));
+            return classes;
+        }
+
+        @Override
+        protected void onPostExecute(Integer classes) {
+            if (classes == 0) {
+                // Empty is strong signal for adopt
+                mRadioInternal.setChecked(true);
+            } else if ((classes & (Environment.HAS_PICTURES | Environment.HAS_DCIM)) != 0) {
+                // Photos is strong signal for portable
+                mRadioExternal.setChecked(true);
+            }
+        }
+    }
 }
diff --git a/src/com/android/settings/deviceinfo/storage/StorageAsyncLoader.java b/src/com/android/settings/deviceinfo/storage/StorageAsyncLoader.java
index 2ce53f6..0b9b697 100644
--- a/src/com/android/settings/deviceinfo/storage/StorageAsyncLoader.java
+++ b/src/com/android/settings/deviceinfo/storage/StorageAsyncLoader.java
@@ -30,9 +30,9 @@
 import android.util.Log;
 import android.util.SparseArray;
 
-import com.android.settings.utils.AsyncLoader;
 import com.android.settings.wrapper.UserManagerWrapper;
 import com.android.settingslib.applications.StorageStatsSource;
+import com.android.settingslib.utils.AsyncLoader;
 import com.android.settingslib.wrapper.PackageManagerWrapper;
 
 import java.io.IOException;
diff --git a/src/com/android/settings/deviceinfo/storage/UserIconLoader.java b/src/com/android/settings/deviceinfo/storage/UserIconLoader.java
index 4f00c3c..d1c29df 100644
--- a/src/com/android/settings/deviceinfo/storage/UserIconLoader.java
+++ b/src/com/android/settings/deviceinfo/storage/UserIconLoader.java
@@ -25,7 +25,7 @@
 
 import com.android.internal.util.Preconditions;
 import com.android.settings.Utils;
-import com.android.settings.utils.AsyncLoader;
+import com.android.settingslib.utils.AsyncLoader;
 
 /**
  * Fetches a user icon as a loader using a given icon loading lambda.
diff --git a/src/com/android/settings/deviceinfo/storage/VolumeSizesLoader.java b/src/com/android/settings/deviceinfo/storage/VolumeSizesLoader.java
index 720f151..236f55f 100644
--- a/src/com/android/settings/deviceinfo/storage/VolumeSizesLoader.java
+++ b/src/com/android/settings/deviceinfo/storage/VolumeSizesLoader.java
@@ -21,9 +21,9 @@
 import android.os.storage.VolumeInfo;
 import android.support.annotation.VisibleForTesting;
 
-import com.android.settings.utils.AsyncLoader;
 import com.android.settingslib.deviceinfo.PrivateStorageInfo;
 import com.android.settingslib.deviceinfo.StorageVolumeProvider;
+import com.android.settingslib.utils.AsyncLoader;
 
 import java.io.IOException;
 
diff --git a/src/com/android/settings/fuelgauge/BatteryInfoLoader.java b/src/com/android/settings/fuelgauge/BatteryInfoLoader.java
index ce22a8c..614eb80 100644
--- a/src/com/android/settings/fuelgauge/BatteryInfoLoader.java
+++ b/src/com/android/settings/fuelgauge/BatteryInfoLoader.java
@@ -25,7 +25,7 @@
 import android.os.SystemClock;
 import com.android.internal.os.BatteryStatsHelper;
 import com.android.settings.overlay.FeatureFactory;
-import com.android.settings.utils.AsyncLoader;
+import com.android.settingslib.utils.AsyncLoader;
 
 /**
  * Loader that can be used by classes to load BatteryInfo in a background thread. This loader will
diff --git a/src/com/android/settings/fuelgauge/BatteryStatsHelperLoader.java b/src/com/android/settings/fuelgauge/BatteryStatsHelperLoader.java
index b81f282..28585ae 100644
--- a/src/com/android/settings/fuelgauge/BatteryStatsHelperLoader.java
+++ b/src/com/android/settings/fuelgauge/BatteryStatsHelperLoader.java
@@ -23,7 +23,7 @@
 import android.support.annotation.VisibleForTesting;
 
 import com.android.internal.os.BatteryStatsHelper;
-import com.android.settings.utils.AsyncLoader;
+import com.android.settingslib.utils.AsyncLoader;
 
 /**
  * Loader to get new {@link BatteryStatsHelper} in the background
diff --git a/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java b/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java
index 5f4758a..19aa639 100644
--- a/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java
+++ b/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java
@@ -22,7 +22,7 @@
 import android.os.SystemClock;
 import com.android.internal.os.BatteryStatsHelper;
 import com.android.settings.overlay.FeatureFactory;
-import com.android.settings.utils.AsyncLoader;
+import com.android.settingslib.utils.AsyncLoader;
 import java.util.ArrayList;
 import java.util.List;
 
diff --git a/src/com/android/settings/fuelgauge/PowerUsageBase.java b/src/com/android/settings/fuelgauge/PowerUsageBase.java
index 5f46b07..b811f20 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageBase.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageBase.java
@@ -26,7 +26,7 @@
 
 import com.android.internal.os.BatteryStatsHelper;
 import com.android.settings.dashboard.DashboardFragment;
-import com.android.settings.utils.AsyncLoader;
+import com.android.settingslib.utils.AsyncLoader;
 
 /**
  * Common base class for things that need to show the battery usage graph.
diff --git a/src/com/android/settings/fuelgauge/anomaly/AnomalyLoader.java b/src/com/android/settings/fuelgauge/anomaly/AnomalyLoader.java
index 596eaf5..f26b742 100644
--- a/src/com/android/settings/fuelgauge/anomaly/AnomalyLoader.java
+++ b/src/com/android/settings/fuelgauge/anomaly/AnomalyLoader.java
@@ -26,7 +26,7 @@
 
 import com.android.internal.os.BatteryStatsHelper;
 import com.android.internal.util.ArrayUtils;
-import com.android.settings.utils.AsyncLoader;
+import com.android.settingslib.utils.AsyncLoader;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
diff --git a/src/com/android/settings/location/LocationEnabler.java b/src/com/android/settings/location/LocationEnabler.java
index 0bec6ba..5c5399c 100644
--- a/src/com/android/settings/location/LocationEnabler.java
+++ b/src/com/android/settings/location/LocationEnabler.java
@@ -13,6 +13,7 @@
  */
 package com.android.settings.location;
 
+import android.app.ActivityManager;
 import android.Manifest.permission;
 import android.content.BroadcastReceiver;
 import android.content.Context;
@@ -32,6 +33,8 @@
 import com.android.settingslib.core.lifecycle.events.OnPause;
 import com.android.settingslib.core.lifecycle.events.OnResume;
 
+import static com.android.settingslib.Utils.updateLocationMode;
+
 /**
  * A class that listens to location settings change and modifies location settings
  * settings.
@@ -40,11 +43,6 @@
 
     private static final String TAG = "LocationEnabler";
     @VisibleForTesting
-    static final String MODE_CHANGING_ACTION =
-            "com.android.settings.location.MODE_CHANGING";
-    private static final String CURRENT_MODE_KEY = "CURRENT_MODE";
-    private static final String NEW_MODE_KEY = "NEW_MODE";
-    @VisibleForTesting
     static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED =
             new IntentFilter(LocationManager.MODE_CHANGED_ACTION);
 
@@ -122,7 +120,7 @@
             return;
         }
 
-        updateLocationMode(currentMode, mode);
+        updateLocationMode(mContext, currentMode, mode, ActivityManager.getCurrentUser());
         refreshLocationMode();
     }
 
@@ -154,13 +152,4 @@
     private boolean isRestricted() {
         return mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION);
     }
-
-    private boolean updateLocationMode(int oldMode, int newMode) {
-        final Intent intent = new Intent(MODE_CHANGING_ACTION);
-        intent.putExtra(CURRENT_MODE_KEY, oldMode);
-        intent.putExtra(NEW_MODE_KEY, newMode);
-        mContext.sendBroadcast(intent, permission.WRITE_SECURE_SETTINGS);
-        return Settings.Secure.putInt(
-                mContext.getContentResolver(), Settings.Secure.LOCATION_MODE, newMode);
-    }
 }
diff --git a/src/com/android/settings/notification/AbstractZenModeAutomaticRulePreferenceController.java b/src/com/android/settings/notification/AbstractZenModeAutomaticRulePreferenceController.java
index cc70a6f..668b06c 100644
--- a/src/com/android/settings/notification/AbstractZenModeAutomaticRulePreferenceController.java
+++ b/src/com/android/settings/notification/AbstractZenModeAutomaticRulePreferenceController.java
@@ -35,6 +35,7 @@
 
 import java.util.Arrays;
 import java.util.Comparator;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -45,6 +46,7 @@
     protected Fragment mParent;
     protected Set<Map.Entry<String, AutomaticZenRule>> mRules;
     protected PackageManager mPm;
+    private static List<String> mDefaultRuleIds;
 
     public AbstractZenModeAutomaticRulePreferenceController(Context context, String key, Fragment
             parent, Lifecycle lifecycle) {
@@ -60,6 +62,13 @@
         mRules = getZenModeRules();
     }
 
+    private static List<String> getDefaultRuleIds() {
+        if (mDefaultRuleIds == null) {
+            mDefaultRuleIds = ZenModeConfig.DEFAULT_RULE_IDS;
+        }
+        return mDefaultRuleIds;
+    }
+
     private Set<Map.Entry<String, AutomaticZenRule>> getZenModeRules() {
         Map<String, AutomaticZenRule> ruleMap =
                 NotificationManager.from(mContext).getAutomaticZenRules();
@@ -99,6 +108,13 @@
                 @Override
                 public int compare(Map.Entry<String, AutomaticZenRule> lhs,
                         Map.Entry<String, AutomaticZenRule> rhs) {
+                    // if it's a default rule, should be at the top of automatic rules
+                    boolean lhsIsDefaultRule = getDefaultRuleIds().contains(lhs.getKey());
+                    boolean rhsIsDefaultRule = getDefaultRuleIds().contains(rhs.getKey());
+                    if (lhsIsDefaultRule != rhsIsDefaultRule) {
+                        return lhsIsDefaultRule ? -1 : 1;
+                    }
+
                     int byDate = Long.compare(lhs.getValue().getCreationTime(),
                             rhs.getValue().getCreationTime());
                     if (byDate != 0) {
diff --git a/src/com/android/settings/notification/ZenRulePreference.java b/src/com/android/settings/notification/ZenRulePreference.java
index 90f6a94..7193873 100644
--- a/src/com/android/settings/notification/ZenRulePreference.java
+++ b/src/com/android/settings/notification/ZenRulePreference.java
@@ -73,6 +73,10 @@
 
     @Override
     protected int getSecondTargetResId() {
+        if (mId != null && ZenModeConfig.DEFAULT_RULE_IDS.contains(mId)) {
+            return 0;
+        }
+
         return R.layout.zen_rule_widget;
     }
 
diff --git a/src/com/android/settings/search/SavedQueryLoader.java b/src/com/android/settings/search/SavedQueryLoader.java
index e8efe85..5df3610 100644
--- a/src/com/android/settings/search/SavedQueryLoader.java
+++ b/src/com/android/settings/search/SavedQueryLoader.java
@@ -23,7 +23,7 @@
 import android.support.annotation.VisibleForTesting;
 
 import com.android.settings.search.IndexDatabaseHelper.SavedQueriesColumns;
-import com.android.settings.utils.AsyncLoader;
+import com.android.settingslib.utils.AsyncLoader;
 
 import java.util.ArrayList;
 import java.util.List;
diff --git a/src/com/android/settings/search/SavedQueryRecorder.java b/src/com/android/settings/search/SavedQueryRecorder.java
index 466af0b..b3b0bb8 100644
--- a/src/com/android/settings/search/SavedQueryRecorder.java
+++ b/src/com/android/settings/search/SavedQueryRecorder.java
@@ -24,7 +24,7 @@
 import android.util.Log;
 
 import com.android.settings.search.IndexDatabaseHelper;
-import com.android.settings.utils.AsyncLoader;
+import com.android.settingslib.utils.AsyncLoader;
 
 import static com.android.settings.search.IndexDatabaseHelper.Tables.TABLE_SAVED_QUERIES;
 
diff --git a/src/com/android/settings/search/SavedQueryRemover.java b/src/com/android/settings/search/SavedQueryRemover.java
index 77334a5..c6abe97 100644
--- a/src/com/android/settings/search/SavedQueryRemover.java
+++ b/src/com/android/settings/search/SavedQueryRemover.java
@@ -24,7 +24,7 @@
 import android.database.sqlite.SQLiteException;
 import android.util.Log;
 
-import com.android.settings.utils.AsyncLoader;
+import com.android.settingslib.utils.AsyncLoader;
 
 public class SavedQueryRemover extends AsyncLoader<Void> {
 
diff --git a/src/com/android/settings/search/SearchResultLoader.java b/src/com/android/settings/search/SearchResultLoader.java
index 7ec3146..f4abd8e 100644
--- a/src/com/android/settings/search/SearchResultLoader.java
+++ b/src/com/android/settings/search/SearchResultLoader.java
@@ -1,6 +1,6 @@
 package com.android.settings.search;
 
-import com.android.settings.utils.AsyncLoader;
+import com.android.settingslib.utils.AsyncLoader;
 
 import android.content.Context;
 
diff --git a/src/com/android/settings/utils/AsyncLoader.java b/src/com/android/settings/utils/AsyncLoader.java
deleted file mode 100644
index 76c99fa..0000000
--- a/src/com/android/settings/utils/AsyncLoader.java
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Copyright (C) 2016 Google Inc.
- * Licensed to 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.utils;
-
-import android.content.AsyncTaskLoader;
-import android.content.Context;
-
-/**
- * This class fills in some boilerplate for AsyncTaskLoader to actually load things.
- *
- * Subclasses need to implement {@link AsyncLoader#loadInBackground()} to perform the actual
- * background task, and {@link AsyncLoader#onDiscardResult(T)} to clean up previously loaded
- * results.
- *
- * This loader is based on the MailAsyncTaskLoader from the AOSP EmailUnified repo.
- */
-public abstract class AsyncLoader<T> extends AsyncTaskLoader<T> {
-    private T mResult;
-
-    public AsyncLoader(final Context context) {
-        super(context);
-    }
-
-    @Override
-    protected void onStartLoading() {
-        if (mResult != null) {
-            deliverResult(mResult);
-        }
-
-        if (takeContentChanged() || mResult == null) {
-            forceLoad();
-        }
-    }
-
-    @Override
-    protected void onStopLoading() {
-        cancelLoad();
-    }
-
-    @Override
-    public void deliverResult(final T data) {
-        if (isReset()) {
-            if (data != null) {
-                onDiscardResult(data);
-            }
-            return;
-        }
-
-        final T oldResult = mResult;
-        mResult = data;
-
-        if (isStarted()) {
-            super.deliverResult(data);
-        }
-
-        if (oldResult != null && oldResult != mResult) {
-            onDiscardResult(oldResult);
-        }
-    }
-
-    @Override
-    protected void onReset() {
-        super.onReset();
-
-        onStopLoading();
-
-        if (mResult != null) {
-            onDiscardResult(mResult);
-        }
-        mResult = null;
-    }
-
-    @Override
-    public void onCanceled(final T data) {
-        super.onCanceled(data);
-
-        if (data != null) {
-            onDiscardResult(data);
-        }
-    }
-
-    /**
-     * Called when discarding the load results so subclasses can take care of clean-up or
-     * recycling tasks. This is not called if the same result (by way of pointer equality) is
-     * returned again by a subsequent call to loadInBackground, or if result is null.
-     *
-     * Note that this may be called concurrently with loadInBackground(), and in some circumstances
-     * may be called more than once for a given object.
-     *
-     * @param result The value returned from {@link AsyncLoader#loadInBackground()} which
-     *               is to be discarded.
-     */
-    protected abstract void onDiscardResult(final T result);
-}
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/LicenseHtmlGeneratorFromXmlTest.java b/tests/robotests/src/com/android/settings/LicenseHtmlGeneratorFromXmlTest.java
deleted file mode 100644
index ea4b272..0000000
--- a/tests/robotests/src/com/android/settings/LicenseHtmlGeneratorFromXmlTest.java
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.settings;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import com.android.settings.testutils.SettingsRobolectricTestRunner;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.robolectric.annotation.Config;
-import org.xmlpull.v1.XmlPullParserException;
-
-@RunWith(SettingsRobolectricTestRunner.class)
-@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
-public class LicenseHtmlGeneratorFromXmlTest {
-    private static final String VAILD_XML_STRING =
-            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
-            "<licenses>\n" +
-            "<file-name contentId=\"0\">/file0</file-name>\n" +
-            "<file-name contentId=\"0\">/file1</file-name>\n" +
-            "<file-content contentId=\"0\"><![CDATA[license content #0]]></file-content>\n" +
-            "</licenses>";
-
-    private static final String INVAILD_XML_STRING =
-            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
-            "<licenses2>\n" +
-            "<file-name contentId=\"0\">/file0</file-name>\n" +
-            "<file-name contentId=\"0\">/file1</file-name>\n" +
-            "<file-content contentId=\"0\"><![CDATA[license content #0]]></file-content>\n" +
-            "</licenses2>";
-
-    private static final String EXPECTED_HTML_STRING =
-            "<html><head>\n" +
-            "<style type=\"text/css\">\n" +
-            "body { padding: 0; font-family: sans-serif; }\n" +
-            ".same-license { background-color: #eeeeee;\n" +
-            "                border-top: 20px solid white;\n" +
-            "                padding: 10px; }\n" +
-            ".label { font-weight: bold; }\n" +
-            ".file-list { margin-left: 1em; color: blue; }\n" +
-            "</style>\n" +
-            "</head>" +
-            "<body topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\">\n" +
-            "<div class=\"toc\">\n" +
-            "<ul>\n" +
-            "<li><a href=\"#id0\">/file0</a></li>\n" +
-            "<li><a href=\"#id0\">/file1</a></li>\n" +
-            "</ul>\n" +
-            "</div><!-- table of contents -->\n" +
-            "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n" +
-            "<tr id=\"id0\"><td class=\"same-license\">\n" +
-            "<div class=\"label\">Notices for file(s):</div>\n" +
-            "<div class=\"file-list\">\n" +
-            "/file0 <br/>\n" +
-            "/file1 <br/>\n" +
-            "</div><!-- file-list -->\n" +
-            "<pre class=\"license-text\">\n" +
-            "license content #0\n" +
-            "</pre><!-- license-text -->\n" +
-            "</td></tr><!-- same-license -->\n" +
-            "</table></body></html>\n";
-
-    @Test
-    public void testParseValidXmlStream() throws XmlPullParserException, IOException {
-        Map<String, String> fileNameToContentIdMap = new HashMap<String, String>();
-        Map<String, String> contentIdToFileContentMap = new HashMap<String, String>();
-
-        LicenseHtmlGeneratorFromXml.parse(
-                new InputStreamReader(new ByteArrayInputStream(VAILD_XML_STRING.getBytes())),
-                fileNameToContentIdMap, contentIdToFileContentMap);
-        assertThat(fileNameToContentIdMap.size()).isEqualTo(2);
-        assertThat(fileNameToContentIdMap.get("/file0")).isEqualTo("0");
-        assertThat(fileNameToContentIdMap.get("/file1")).isEqualTo("0");
-        assertThat(contentIdToFileContentMap.size()).isEqualTo(1);
-        assertThat(contentIdToFileContentMap.get("0")).isEqualTo("license content #0");
-    }
-
-    @Test(expected = XmlPullParserException.class)
-    public void testParseInvalidXmlStream() throws XmlPullParserException, IOException {
-        Map<String, String> fileNameToContentIdMap = new HashMap<String, String>();
-        Map<String, String> contentIdToFileContentMap = new HashMap<String, String>();
-
-        LicenseHtmlGeneratorFromXml.parse(
-                new InputStreamReader(new ByteArrayInputStream(INVAILD_XML_STRING.getBytes())),
-                fileNameToContentIdMap, contentIdToFileContentMap);
-    }
-
-    @Test
-    public void testGenerateHtml() {
-        Map<String, String> fileNameToContentIdMap = new HashMap<String, String>();
-        Map<String, String> contentIdToFileContentMap = new HashMap<String, String>();
-
-        fileNameToContentIdMap.put("/file0", "0");
-        fileNameToContentIdMap.put("/file1", "0");
-        contentIdToFileContentMap.put("0", "license content #0");
-
-        StringWriter output = new StringWriter();
-        LicenseHtmlGeneratorFromXml.generateHtml(
-                fileNameToContentIdMap, contentIdToFileContentMap, new PrintWriter(output));
-        assertThat(output.toString()).isEqualTo(EXPECTED_HTML_STRING);
-    }
-}
diff --git a/tests/robotests/src/com/android/settings/LicenseHtmlLoaderTest.java b/tests/robotests/src/com/android/settings/LicenseHtmlLoaderTest.java
deleted file mode 100644
index b16d315..0000000
--- a/tests/robotests/src/com/android/settings/LicenseHtmlLoaderTest.java
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.settings;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
-
-import android.content.Context;
-
-import com.android.settings.testutils.SettingsRobolectricTestRunner;
-
-import java.io.File;
-import java.util.ArrayList;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.robolectric.annotation.Config;
-
-@RunWith(SettingsRobolectricTestRunner.class)
-@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
-public class LicenseHtmlLoaderTest {
-    @Mock
-    private Context mContext;
-
-    LicenseHtmlLoader newLicenseHtmlLoader(ArrayList<File> xmlFiles,
-            File cachedHtmlFile, boolean isCachedHtmlFileOutdated,
-            boolean generateHtmlFileSucceeded) {
-        LicenseHtmlLoader loader = spy(new LicenseHtmlLoader(mContext));
-        doReturn(xmlFiles).when(loader).getVaildXmlFiles();
-        doReturn(cachedHtmlFile).when(loader).getCachedHtmlFile();
-        doReturn(isCachedHtmlFileOutdated).when(loader).isCachedHtmlFileOutdated(any(), any());
-        doReturn(generateHtmlFileSucceeded).when(loader).generateHtmlFile(any(), any());
-        return loader;
-    }
-
-    @Before
-    public void setUp() {
-        MockitoAnnotations.initMocks(this);
-    }
-
-    @Test
-    public void testLoadInBackground() {
-        ArrayList<File> xmlFiles = new ArrayList();
-        xmlFiles.add(new File("test.xml"));
-        File cachedHtmlFile = new File("test.html");
-
-        LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, true);
-
-        assertThat(loader.loadInBackground()).isEqualTo(cachedHtmlFile);
-        verify(loader).generateHtmlFile(any(), any());
-    }
-
-    @Test
-    public void testLoadInBackgroundWithNoVaildXmlFiles() {
-        ArrayList<File> xmlFiles = new ArrayList();
-        File cachedHtmlFile = new File("test.html");
-
-        LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, true);
-
-        assertThat(loader.loadInBackground()).isNull();
-        verify(loader, never()).generateHtmlFile(any(), any());
-    }
-
-    @Test
-    public void testLoadInBackgroundWithNonOutdatedCachedHtmlFile() {
-        ArrayList<File> xmlFiles = new ArrayList();
-        xmlFiles.add(new File("test.xml"));
-        File cachedHtmlFile = new File("test.html");
-
-        LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, false, true);
-
-        assertThat(loader.loadInBackground()).isEqualTo(cachedHtmlFile);
-        verify(loader, never()).generateHtmlFile(any(), any());
-    }
-
-    @Test
-    public void testLoadInBackgroundWithGenerateHtmlFileFailed() {
-        ArrayList<File> xmlFiles = new ArrayList();
-        xmlFiles.add(new File("test.xml"));
-        File cachedHtmlFile = new File("test.html");
-
-        LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, false);
-
-        assertThat(loader.loadInBackground()).isNull();
-        verify(loader).generateHtmlFile(any(), any());
-    }
-}
diff --git a/tests/robotests/src/com/android/settings/SettingsLicenseActivityTest.java b/tests/robotests/src/com/android/settings/SettingsLicenseActivityTest.java
index ecda97e..aceb671 100644
--- a/tests/robotests/src/com/android/settings/SettingsLicenseActivityTest.java
+++ b/tests/robotests/src/com/android/settings/SettingsLicenseActivityTest.java
@@ -18,21 +18,19 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
-import static org.mockito.Matchers.anyInt;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.spy;
 import static org.robolectric.Shadows.shadowOf;
 
 import android.app.Application;
-import android.os.SystemProperties;
 import android.content.Intent;
 import android.net.Uri;
+import android.os.SystemProperties;
 
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 
-import java.io.File;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -42,6 +40,8 @@
 import org.robolectric.android.controller.ActivityController;
 import org.robolectric.annotation.Config;
 
+import java.io.File;
+
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
 public class SettingsLicenseActivityTest {
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/location/LocationEnablerTest.java b/tests/robotests/src/com/android/settings/location/LocationEnablerTest.java
index 25e491a..1e50770 100644
--- a/tests/robotests/src/com/android/settings/location/LocationEnablerTest.java
+++ b/tests/robotests/src/com/android/settings/location/LocationEnablerTest.java
@@ -16,9 +16,7 @@
 package com.android.settings.location;
 
 import static android.Manifest.permission.WRITE_SECURE_SETTINGS;
-
 import static com.google.common.truth.Truth.assertThat;
-
 import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.anyString;
@@ -31,18 +29,21 @@
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import android.app.ActivityManager;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.UserInfo;
+import android.location.LocationManager;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.provider.Settings;
 import android.text.TextUtils;
-
 import com.android.settings.TestConfig;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.shadow.ShadowSecureSettings;
 import com.android.settingslib.core.lifecycle.Lifecycle;
-
+import java.util.ArrayList;
+import java.util.List;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -52,11 +53,10 @@
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
 
-import java.util.ArrayList;
-import java.util.List;
-
 @RunWith(SettingsRobolectricTestRunner.class)
-@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+@Config(manifest = TestConfig.MANIFEST_PATH,
+        sdk = TestConfig.SDK_VERSION,
+        shadows = {ShadowSecureSettings.class})
 public class LocationEnablerTest {
 
     @Mock
@@ -178,8 +178,11 @@
 
         mEnabler.setLocationMode(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
 
-        verify(mContext).sendBroadcast(argThat(actionMatches(mEnabler.MODE_CHANGING_ACTION)),
+        verify(mContext).sendBroadcastAsUser(
+                argThat(actionMatches(LocationManager.MODE_CHANGING_ACTION)),
+                eq(UserHandle.of(ActivityManager.getCurrentUser())),
                 eq(WRITE_SECURE_SETTINGS));
+
     }
 
     @Test
diff --git a/tests/robotests/src/com/android/settings/location/LocationModeRadioButtonPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/location/LocationModeRadioButtonPreferenceControllerTest.java
index 0c7ac41..ae98acc 100644
--- a/tests/robotests/src/com/android/settings/location/LocationModeRadioButtonPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/location/LocationModeRadioButtonPreferenceControllerTest.java
@@ -16,29 +16,30 @@
 package com.android.settings.location;
 
 import static com.google.common.truth.Truth.assertThat;
-
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import android.content.Context;
 import android.provider.Settings;
 import android.support.v7.preference.PreferenceScreen;
-
 import com.android.settings.TestConfig;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.shadow.ShadowSecureSettings;
 import com.android.settings.widget.RadioButtonPreference;
 import com.android.settingslib.core.lifecycle.Lifecycle;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
-import org.robolectric.annotation.Config;
 import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
 
 @RunWith(SettingsRobolectricTestRunner.class)
-@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+@Config(
+        manifest = TestConfig.MANIFEST_PATH,
+        sdk = TestConfig.SDK_VERSION,
+        shadows = {ShadowSecureSettings.class})
 public class LocationModeRadioButtonPreferenceControllerTest {
 
     @Mock
diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeAutomaticRulesPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeAutomaticRulesPreferenceControllerTest.java
index 0dae923..20f9e62 100644
--- a/tests/robotests/src/com/android/settings/notification/ZenModeAutomaticRulesPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/ZenModeAutomaticRulesPreferenceControllerTest.java
@@ -17,7 +17,6 @@
 package com.android.settings.notification;
 
 import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertTrue;
 
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -25,7 +24,6 @@
 import android.app.AutomaticZenRule;
 import android.app.Fragment;
 import android.app.NotificationManager;
-import android.content.ContentResolver;
 import android.content.Context;
 import android.provider.Settings;
 import android.support.v7.preference.PreferenceCategory;
@@ -45,7 +43,9 @@
 import org.robolectric.shadows.ShadowApplication;
 import org.robolectric.util.ReflectionHelpers;
 
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 @RunWith(SettingsRobolectricTestRunner.class)
@@ -53,6 +53,9 @@
 public class ZenModeAutomaticRulesPreferenceControllerTest {
     private ZenModeAutomaticRulesPreferenceController mController;
     private final String GENERIC_RULE_NAME = "test";
+    final String DEFAULT_ID_1 = "DEFAULT_1";
+    final String DEFAULT_ID_2 = "DEFAULT_2";
+    private final List<String> mDefaultIds = Arrays.asList(DEFAULT_ID_1, DEFAULT_ID_2);
 
     @Mock
     private ZenModeBackend mBackend;
@@ -66,7 +69,6 @@
     private PreferenceScreen mPreferenceScreen;
 
     private Context mContext;
-    private ContentResolver mContentResolver;
 
     @Before
     public void setup() {
@@ -75,11 +77,12 @@
         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
 
         mContext = shadowApplication.getApplicationContext();
-        mContentResolver = RuntimeEnvironment.application.getContentResolver();
         when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
         mController = new ZenModeAutomaticRulesPreferenceController(mContext, mock(Fragment.class),
                 mock(Lifecycle.class));
+
         ReflectionHelpers.setField(mController, "mBackend", mBackend);
+        ReflectionHelpers.setField(mController, "mDefaultRuleIds", mDefaultIds);
 
         when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
                 mockPref);
@@ -97,7 +100,7 @@
 
         // check ordering, most recent should be at the bottom/end (ie higher creation time)
         for (int i = 0; i < NUM_RULES; i++) {
-            assertEquals(rules[i].getKey(), GENERIC_RULE_NAME + (NUM_RULES - 1 - i));
+            assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i].getKey());
         }
     }
 
@@ -112,7 +115,29 @@
 
         // check ordering, most recent should be at the bottom/end (ie higher creation time)
         for (int i = 0; i < NUM_RULES; i++) {
-            assertEquals(rules[i].getKey(), GENERIC_RULE_NAME + i);
+            assertEquals(GENERIC_RULE_NAME + i, rules[i].getKey());
+        }
+    }
+
+    @Test
+    public void updateState_checkRuleOrderingDescending_withDefaultRules() {
+        final int NUM_RULES = 4;
+
+        Map<String, AutomaticZenRule> ruleMap = mockAutoZenRulesDecreasingCreationTime(NUM_RULES);
+        ruleMap.put(DEFAULT_ID_2, new AutomaticZenRule("DEFAULT_1_NAME", null,
+                null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20));
+        ruleMap.put(DEFAULT_ID_1, new AutomaticZenRule("DEFAULT_1_NAME", null,
+                null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10));
+        when(mNotificationManager.getAutomaticZenRules()).thenReturn(ruleMap);
+
+        Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
+        assertEquals(NUM_RULES + 2, rules.length);
+
+        assertEquals(rules[0].getKey(), DEFAULT_ID_1);
+        assertEquals(rules[1].getKey(), DEFAULT_ID_2);
+        // NON-DEFAULT RULES check ordering, most recent at the bottom/end
+        for (int i = 0; i < NUM_RULES; i++) {
+            assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i + 2].getKey());
         }
     }
 
@@ -138,8 +163,8 @@
         assertEquals(NUM_RULES + 2, rules.length); // inserted 2 rules
 
         // check ordering of inserted rules
-        assertEquals(rules[4].getKey(), insertedRule1);
-        assertEquals(rules[2].getKey(), insertedRule2);
+        assertEquals(insertedRule1, rules[4].getKey());
+        assertEquals(insertedRule2, rules[2].getKey());
     }
 
     private Map<String, AutomaticZenRule> mockAutoZenRulesAscendingCreationTime(int numRules) {
@@ -163,4 +188,4 @@
 
         return ruleMap;
     }
-}
\ No newline at end of file
+}
diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeScheduleRuleSettingsTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeScheduleRuleSettingsTest.java
index f8cc767..070aa38 100644
--- a/tests/robotests/src/com/android/settings/notification/ZenModeScheduleRuleSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/notification/ZenModeScheduleRuleSettingsTest.java
@@ -17,10 +17,10 @@
 package com.android.settings.notification;
 
 import android.app.Activity;
+import android.app.NotificationManager;
 import android.content.Context;
 import android.content.res.Resources;
 import android.content.Intent;
-import android.os.UserManager;
 
 import com.android.settings.R;
 import com.android.settings.TestConfig;
@@ -34,6 +34,8 @@
 import org.mockito.MockitoAnnotations;
 import org.robolectric.annotation.Config;
 import org.robolectric.shadows.ShadowToast;
+import org.robolectric.shadows.ShadowApplication;
+import org.robolectric.RuntimeEnvironment;
 
 import static com.google.common.truth.Truth.assertThat;
 import static org.mockito.Mockito.doReturn;
@@ -58,14 +60,19 @@
     private Intent mIntent;
 
     @Mock
-    private UserManager mUserManager;
+    private NotificationManager mNotificationManager;
 
     private TestFragment mFragment;
+    private Context mContext;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
+        ShadowApplication shadowApplication = ShadowApplication.getInstance();
+        shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
+        mContext = shadowApplication.getApplicationContext();
+
         mFragment = spy(new TestFragment());
         mFragment.onAttach(application);
 
@@ -77,13 +84,13 @@
         when(mActivity.getTheme()).thenReturn(res.newTheme());
         when(mActivity.getIntent()).thenReturn(mIntent);
         when(mActivity.getResources()).thenReturn(res);
-        when(mFragment.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
+        when(mFragment.getContext()).thenReturn(mContext);
     }
 
     @Test
     public void onCreate_noRuleId_shouldToastAndFinishAndNoCrash() {
-        final Context ctx = application.getApplicationContext();
-        final String expected = ctx.getResources().getString(R.string.zen_mode_rule_not_found_text);
+        final String expected = mContext.getResources().getString(
+                R.string.zen_mode_rule_not_found_text);
 
         mFragment.onCreate(null);
 
@@ -93,7 +100,7 @@
         // verify the finish
         verify(mActivity).finish();
 
-        //shoud not crash
+        //should not crash
     }
 
     public static class TestFragment extends ZenModeScheduleRuleSettings {