Merge "Wi-Fi network details page fixes." into oc-dev
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 781e816..cb20c51 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -2853,7 +2853,7 @@
                 android:label="@string/install_other_apps"
                 android:taskAffinity="">
             <intent-filter android:priority="1">
-                <action android:name="android.settings.action.MANAGE_EXTERNAL_SOURCES" />
+                <action android:name="android.settings.action.MANAGE_UNKNOWN_APP_SOURCES" />
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
             <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
@@ -2864,7 +2864,7 @@
                   android:label="@string/install_other_apps"
                   android:taskAffinity="">
             <intent-filter android:priority="1">
-                <action android:name="android.settings.action.MANAGE_EXTERNAL_SOURCES" />
+                <action android:name="android.settings.action.MANAGE_UNKNOWN_APP_SOURCES" />
                 <category android:name="android.intent.category.DEFAULT" />
                 <data android:scheme="package" />
             </intent-filter>
diff --git a/res/layout/preference_bluetooth.xml b/res/layout/preference_bluetooth.xml
deleted file mode 100644
index c339e67..0000000
--- a/res/layout/preference_bluetooth.xml
+++ /dev/null
@@ -1,33 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2006 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.
--->
-
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
-    android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:gravity="center_vertical">
-
-    <!-- Details button -->
-    <ImageView
-        android:id="@+id/deviceDetails"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_gravity="center_vertical"
-        android:padding="8dip"
-        android:background="?android:attr/selectableItemBackground"
-        android:src="@drawable/ic_settings"
-        android:contentDescription="@string/bluetooth_device_details" />
-
-</LinearLayout>
diff --git a/res/layout/preference_progress_category.xml b/res/layout/preference_progress_category.xml
index 1380698..9f5a8b4 100644
--- a/res/layout/preference_progress_category.xml
+++ b/res/layout/preference_progress_category.xml
@@ -35,7 +35,6 @@
             android:id="@android:id/icon"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:tint="?android:attr/textColorPrimary"
             android:maxWidth="18dp"
             android:maxHeight="18dp"/>
     </LinearLayout>
@@ -47,7 +46,8 @@
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:layout_gravity="start|center"
-        android:textAppearance="@android:style/TextAppearance.Material.Body2"/>
+        android:textAppearance="@android:style/TextAppearance.Material.Body2"
+        android:textColor="?android:attr/colorAccent"/>
 
     <ProgressBar
         android:id="@+id/scanning_progress"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 475fb85..57abe34 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -8421,14 +8421,16 @@
     <string name="automatic_storage_manager_freed_bytes"><xliff:g id="size" example="3.25MB">%1$s</xliff:g> total made available\n\nLast ran on <xliff:g id="date" example="Jan 12">%2$s</xliff:g></string>
 
     <!-- Title text for enabling web actions. [CHAR_LIMIT=60] -->
-    <string name="web_action_enable_title">Open links in apps</string>
+    <string name="web_action_enable_title">Instant apps</string>
 
     <!-- Summary text for enabling web actions. [CHAR_LIMIT=250] -->
-    <string name="web_action_enable_summary">Open links in supported apps, even if the
-      apps aren’t installed on your device</string>
+    <string name="web_action_enable_summary">Open links in apps, even if they’re not installed</string>
 
     <!-- Section title for the Web Action preference [CHAR LIMIT=60] -->
-    <string name="web_action_section_title">Apps not installed</string>
+    <string name="web_action_section_title">Instant apps</string>
+
+    <!-- Preference label for an tappable preference that will open the account chooser for instant apps. [CHAR LIMIT=60] -->
+    <string name="instant_apps_account">Instant apps account</string>
 
     <!-- Section title for the Domain URL app preference list [CHAR LIMIT=60]-->
     <string name="domain_url_section_title">Installed apps</string>
diff --git a/res/xml/empty_settings.xml b/res/xml/empty_settings.xml
new file mode 100644
index 0000000..d63f7d6
--- /dev/null
+++ b/res/xml/empty_settings.xml
@@ -0,0 +1,18 @@
+<?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 />
diff --git a/res/xml/installed_app_details_ia.xml b/res/xml/installed_app_details_ia.xml
index 50d183d..b092753 100644
--- a/res/xml/installed_app_details_ia.xml
+++ b/res/xml/installed_app_details_ia.xml
@@ -43,31 +43,37 @@
     <Preference
         android:key="permission_settings"
         android:title="@string/permissions_label"
+        android:summary="@string/summary_placeholder"
         android:selectable="true"/>
 
     <Preference
         android:key="storage_settings"
         android:title="@string/storage_settings"
+        android:summary="@string/summary_placeholder"
         android:selectable="true"/>
 
     <Preference
         android:key="data_settings"
         android:title="@string/data_usage_summary_title"
+        android:summary="@string/summary_placeholder"
         android:selectable="true"/>
 
     <Preference
         android:key="battery"
         android:title="@string/power_usage_summary_title"
+        android:summary="@string/summary_placeholder"
         android:selectable="true"/>
 
     <Preference
         android:key="preferred_settings"
         android:title="@string/launch_by_default"
+        android:summary="@string/summary_placeholder"
         android:selectable="true"/>
 
     <Preference
         android:key="memory"
         android:title="@string/memory_settings_title"
+        android:summary="@string/summary_placeholder"
         android:enabled="false"
         android:selectable="true"/>
 
diff --git a/src/com/android/settings/ApnSettings.java b/src/com/android/settings/ApnSettings.java
index 5e31a02..b460050 100755
--- a/src/com/android/settings/ApnSettings.java
+++ b/src/com/android/settings/ApnSettings.java
@@ -184,8 +184,7 @@
         mUnavailable = isUiRestricted();
         setHasOptionsMenu(!mUnavailable);
         if (mUnavailable) {
-            setPreferenceScreen(new PreferenceScreen(getPrefContext(), null));
-            getPreferenceScreen().removeAll();
+            addPreferencesFromResource(R.xml.empty_settings);
             return;
         }
 
diff --git a/src/com/android/settings/applications/AppStorageSettings.java b/src/com/android/settings/applications/AppStorageSettings.java
index d5d72bc..8d41558 100644
--- a/src/com/android/settings/applications/AppStorageSettings.java
+++ b/src/com/android/settings/applications/AppStorageSettings.java
@@ -101,6 +101,8 @@
     private static final String KEY_URI_CATEGORY = "uri_category";
     private static final String KEY_CLEAR_URI = "clear_uri_button";
 
+    private static final String KEY_CACHE_CLEARED = "cache_cleared";
+
     // Views related to cache info
     private Preference mCacheSize;
     private Button mClearDataButton;
@@ -115,6 +117,7 @@
     private PreferenceCategory mUri;
 
     private boolean mCanClearData = true;
+    private boolean mCacheCleared;
 
     private AppStorageStats mLastResult;
     private AppStorageSizesController mSizeController;
@@ -133,6 +136,9 @@
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        if (savedInstanceState != null) {
+            mCacheCleared = savedInstanceState.getBoolean(KEY_CACHE_CLEARED, false);
+        }
 
         addPreferencesFromResource(R.xml.app_storage_settings);
         setupViews();
@@ -145,6 +151,12 @@
         updateSize();
     }
 
+    @Override
+    public void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+        outState.putBoolean(KEY_CACHE_CLEARED, mCacheCleared);
+    }
+
     private void setupViews() {
         mComputingStr = getActivity().getText(R.string.computing_size);
         mInvalidSizeStr = getActivity().getText(R.string.invalid_size_value);
@@ -523,6 +535,10 @@
     }
 
     private void updateUiWithSize(AppStorageStats result) {
+        if (mCacheCleared) {
+            mSizeController.setCacheCleared(true);
+        }
+
         mSizeController.updateUi(getContext());
 
         if (result == null) {
@@ -539,7 +555,7 @@
                 mClearDataButton.setEnabled(true);
                 mClearDataButton.setOnClickListener(this);
             }
-            if (cacheSize <= 0) {
+            if (cacheSize <= 0 || mCacheCleared) {
                 mClearCacheButton.setEnabled(false);
             } else {
                 mClearCacheButton.setEnabled(true);
@@ -562,6 +578,7 @@
                     processClearMsg(msg);
                     break;
                 case MSG_CLEAR_CACHE:
+                    mCacheCleared = true;
                     // Refresh size info
                     updateSize();
                     break;
diff --git a/src/com/android/settings/applications/AppStorageSizesController.java b/src/com/android/settings/applications/AppStorageSizesController.java
index bc8f680..94935bd 100644
--- a/src/com/android/settings/applications/AppStorageSizesController.java
+++ b/src/com/android/settings/applications/AppStorageSizesController.java
@@ -39,6 +39,7 @@
     @Nullable
     private StorageStatsSource.AppStorageStats mLastResult;
     private boolean mLastResultFailed;
+    private boolean mCachedCleared;
     private long mLastCodeSize = -1;
     private long mLastDataSize = -1;
     private long mLastCacheSize = -1;
@@ -77,7 +78,7 @@
                 mLastDataSize = dataSize;
                 mDataSize.setSummary(getSizeStr(context, dataSize));
             }
-            long cacheSize = mLastResult.getCacheBytes();
+            long cacheSize = mCachedCleared ? 0 : mLastResult.getCacheBytes();
             if (mLastCacheSize != cacheSize) {
                 mLastCacheSize = cacheSize;
                 mCacheSize.setSummary(getSizeStr(context, cacheSize));
@@ -100,6 +101,15 @@
         mLastResultFailed = result == null;
     }
 
+    /**
+     * Sets if we have cleared the cache and should zero the cache bytes.
+     * When the cache is cleared, the cache directories are recreated. These directories have
+     * some size, but are empty. We zero this out to best match user expectations.
+     */
+    public void setCacheCleared(boolean isCleared) {
+        mCachedCleared = isCleared;
+    }
+
     private String getSizeStr(Context context, long size) {
         return Formatter.formatFileSize(context, size);
     }
diff --git a/src/com/android/settings/applications/ManageDomainUrls.java b/src/com/android/settings/applications/ManageDomainUrls.java
index b09a089..bdc9932 100644
--- a/src/com/android/settings/applications/ManageDomainUrls.java
+++ b/src/com/android/settings/applications/ManageDomainUrls.java
@@ -15,7 +15,9 @@
 package com.android.settings.applications;
 
 import android.app.Application;
+import android.content.ComponentName;
 import android.content.Context;
+import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.os.Bundle;
 import android.os.UserHandle;
@@ -55,6 +57,7 @@
     private ApplicationsState.Session mSession;
     private PreferenceGroup mDomainAppList;
     private SwitchPreference mWebAction;
+    private Preference mInstantAppAccountPreference;
 
     @Override
     public void onCreate(Bundle icicle) {
@@ -126,6 +129,26 @@
                 mWebAction.setOnPreferenceChangeListener(this);
                 webActionCategory.addPreference(mWebAction);
 
+                // Determine whether we should show the instant apps account chooser setting
+                ComponentName instantAppSettingsComponent = getActivity().getPackageManager()
+                        .getInstantAppResolverSettingsComponent();
+                Intent instantAppSettingsIntent = null;
+                if (instantAppSettingsComponent != null) {
+                    instantAppSettingsIntent =
+                            new Intent().setComponent(instantAppSettingsComponent);
+                }
+                if (instantAppSettingsIntent != null) {
+                    final Intent launchIntent = instantAppSettingsIntent;
+                    // TODO: Make this button actually launch the account chooser.
+                    mInstantAppAccountPreference = new Preference(getPrefContext());
+                    mInstantAppAccountPreference.setTitle(R.string.instant_apps_account);
+                    mInstantAppAccountPreference.setOnPreferenceClickListener(pref -> {
+                        startActivity(launchIntent);
+                        return true;
+                    });
+                    webActionCategory.addPreference(mInstantAppAccountPreference);
+                }
+
                 // list to manage link handling per app
                 mDomainAppList = new PreferenceCategory(getPrefContext());
                 mDomainAppList.setTitle(R.string.domain_url_section_title);
@@ -138,9 +161,11 @@
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         if (preference == mWebAction) {
-            final int enabled = (boolean) newValue ? 1 : 0;
+            boolean checked = (boolean) newValue;
             Settings.Secure.putInt(
-                    getContentResolver(), Settings.Secure.WEB_ACTION_ENABLED, enabled);
+                    getContentResolver(),
+                    Settings.Secure.WEB_ACTION_ENABLED, checked ? 1 : 0);
+            mWebAction.setChecked(checked);
             return true;
         }
         return false;
diff --git a/src/com/android/settings/applications/RunningProcessesView.java b/src/com/android/settings/applications/RunningProcessesView.java
index dcb6f9e..b365435 100644
--- a/src/com/android/settings/applications/RunningProcessesView.java
+++ b/src/com/android/settings/applications/RunningProcessesView.java
@@ -411,7 +411,7 @@
             args.putBoolean(RunningServiceDetails.KEY_BACKGROUND, mAdapter.mShowBackground);
 
             SettingsActivity sa = (SettingsActivity) mOwner.getActivity();
-            sa.startPreferencePanel(null, RunningServiceDetails.class.getName(), args,
+            sa.startPreferencePanel(mOwner, RunningServiceDetails.class.getName(), args,
                     R.string.runningservicedetails_settings_title, null, null, 0);
         }
     }
diff --git a/src/com/android/settings/applications/RunningServiceDetails.java b/src/com/android/settings/applications/RunningServiceDetails.java
index 9a173bc..84c2ee5 100644
--- a/src/com/android/settings/applications/RunningServiceDetails.java
+++ b/src/com/android/settings/applications/RunningServiceDetails.java
@@ -33,9 +33,9 @@
 import android.widget.TextView;
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
-import com.android.settings.core.InstrumentedPreferenceFragment;
 import com.android.settings.R;
 import com.android.settings.Utils;
+import com.android.settings.core.InstrumentedPreferenceFragment;
 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
 
 import java.io.File;
@@ -493,19 +493,20 @@
             }
         });
     }
-    
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        
+        setHasOptionsMenu(true);
         mUid = getArguments().getInt(KEY_UID, -1);
         mUserId = getArguments().getInt(KEY_USER_ID, 0);
         mProcessName = getArguments().getString(KEY_PROCESS, null);
         mShowBackground = getArguments().getBoolean(KEY_BACKGROUND, false);
-        
-        mAm = (ActivityManager)getActivity().getSystemService(Context.ACTIVITY_SERVICE);
-        mInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-        
+
+        mAm = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
+        mInflater = (LayoutInflater) getActivity().getSystemService(
+                Context.LAYOUT_INFLATER_SERVICE);
+
         mState = RunningState.getInstance(getActivity());
     }
     
diff --git a/src/com/android/settings/bluetooth/BluetoothDevicePreference.java b/src/com/android/settings/bluetooth/BluetoothDevicePreference.java
index 5bddffa..7be6dd7 100644
--- a/src/com/android/settings/bluetooth/BluetoothDevicePreference.java
+++ b/src/com/android/settings/bluetooth/BluetoothDevicePreference.java
@@ -30,14 +30,13 @@
 import android.util.Log;
 import android.util.Pair;
 import android.util.TypedValue;
-import android.view.View;
-import android.view.View.OnClickListener;
 import android.widget.ImageView;
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.R;
 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
 import com.android.settings.overlay.FeatureFactory;
+import com.android.settings.widget.GearPreference;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
 import com.android.settingslib.bluetooth.HidProfile;
 import com.android.settingslib.bluetooth.LocalBluetoothProfile;
@@ -50,15 +49,14 @@
  * BluetoothDevicePreference is the preference type used to display each remote
  * Bluetooth device in the Bluetooth Settings screen.
  */
-public final class BluetoothDevicePreference extends Preference implements
-        CachedBluetoothDevice.Callback, OnClickListener {
-    private static final String TAG = "BluetoothDevicePreference";
+public final class BluetoothDevicePreference extends GearPreference implements
+        CachedBluetoothDevice.Callback {
+    private static final String TAG = "BluetoothDevicePref";
 
     private static int sDimAlpha = Integer.MIN_VALUE;
 
     private final CachedBluetoothDevice mCachedDevice;
-
-    private OnClickListener mOnSettingsClickListener;
+    private final UserManager mUserManager;
 
     private AlertDialog mDisconnectDialog;
 
@@ -76,7 +74,8 @@
     public final String BLUETOOTH = r.getString(R.string.bluetooth_talkback_bluetooth);
 
     public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice) {
-        super(context);
+        super(context, null);
+        mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
 
         if (sDimAlpha == Integer.MIN_VALUE) {
             TypedValue outValue = new TypedValue();
@@ -85,14 +84,6 @@
         }
 
         mCachedDevice = cachedDevice;
-
-        if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
-            UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
-            if (!um.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH)) {
-                setWidgetLayoutResource(R.layout.preference_bluetooth);
-            }
-        }
-
         mCachedDevice.registerCallback(this);
 
         onDeviceAttributesChanged();
@@ -102,12 +93,20 @@
         notifyChanged();
     }
 
-    CachedBluetoothDevice getCachedDevice() {
-        return mCachedDevice;
+    @Override
+    protected boolean shouldHideSecondTarget() {
+        return mCachedDevice == null
+                || mCachedDevice.getBondState() != BluetoothDevice.BOND_BONDED
+                || mUserManager.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH);
     }
 
-    public void setOnSettingsClickListener(OnClickListener listener) {
-        mOnSettingsClickListener = listener;
+    @Override
+    protected int getSecondTargetResId() {
+        return R.layout.preference_widget_gear;
+    }
+
+    CachedBluetoothDevice getCachedDevice() {
+        return mCachedDevice;
     }
 
     @Override
@@ -120,6 +119,10 @@
         }
     }
 
+    public CachedBluetoothDevice getBluetoothDevice() {
+        return mCachedDevice;
+    }
+
     public void onDeviceAttributesChanged() {
         /*
          * The preference framework takes care of making sure the value has
@@ -157,11 +160,10 @@
         }
 
         if (mCachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
-            ImageView deviceDetails = (ImageView) view.findViewById(R.id.deviceDetails);
+            ImageView deviceDetails = (ImageView) view.findViewById(R.id.settings_button);
 
             if (deviceDetails != null) {
                 deviceDetails.setOnClickListener(this);
-                deviceDetails.setTag(mCachedDevice);
             }
         }
         final ImageView imageView = (ImageView) view.findViewById(android.R.id.icon);
@@ -171,13 +173,6 @@
         super.onBindViewHolder(view);
     }
 
-    public void onClick(View v) {
-        // Should never be null by construction
-        if (mOnSettingsClickListener != null) {
-            mOnSettingsClickListener.onClick(v);
-        }
-    }
-
     @Override
     public boolean equals(Object o) {
         if ((o == null) || !(o instanceof BluetoothDevicePreference)) {
@@ -207,19 +202,19 @@
         int bondState = mCachedDevice.getBondState();
 
         final MetricsFeatureProvider metricsFeatureProvider =
-            FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider();
+                FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider();
 
         if (mCachedDevice.isConnected()) {
             metricsFeatureProvider.action(getContext(),
-                MetricsEvent.ACTION_SETTINGS_BLUETOOTH_DISCONNECT);
+                    MetricsEvent.ACTION_SETTINGS_BLUETOOTH_DISCONNECT);
             askDisconnect();
         } else if (bondState == BluetoothDevice.BOND_BONDED) {
             metricsFeatureProvider.action(getContext(),
-                MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT);
+                    MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT);
             mCachedDevice.connect(true);
         } else if (bondState == BluetoothDevice.BOND_NONE) {
             metricsFeatureProvider.action(getContext(),
-                MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
+                    MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
             pair();
         }
     }
@@ -283,10 +278,10 @@
             }
         }
         if (btClass != null) {
-          if (btClass.doesClassMatch(BluetoothClass.PROFILE_HEADSET)) {
+            if (btClass.doesClassMatch(BluetoothClass.PROFILE_HEADSET)) {
                 return new Pair<Integer, String>(R.drawable.ic_bt_headset_hfp, HEADSET);
             }
-          if (btClass.doesClassMatch(BluetoothClass.PROFILE_A2DP)) {
+            if (btClass.doesClassMatch(BluetoothClass.PROFILE_A2DP)) {
                 return new Pair<Integer, String>(R.drawable.ic_bt_headphones_a2dp, HEADPHONE);
             }
         }
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index 5679f90..317a350 100644
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -45,13 +45,14 @@
 import com.android.settings.LinkifyUtils;
 import com.android.settings.R;
 import com.android.settings.SettingsActivity;
-import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
 import com.android.settings.dashboard.SummaryLoader;
 import com.android.settings.location.ScanningSettings;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Indexable;
 import com.android.settings.search.SearchIndexableRaw;
 import com.android.settings.widget.FooterPreference;
+import com.android.settings.widget.GearPreference;
+import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
 import com.android.settings.widget.SwitchBar;
 import com.android.settings.widget.SwitchBarController;
 import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
@@ -476,24 +477,25 @@
         }
     }
 
-    private final View.OnClickListener mDeviceProfilesListener = new View.OnClickListener() {
-        @Override
-        public void onClick(View v) {
-            // User clicked on advanced options icon for a device in the list
-            if (!(v.getTag() instanceof CachedBluetoothDevice)) {
-                Log.w(TAG, "onClick() called for other View: " + v);
-                return;
-            }
-
-            final CachedBluetoothDevice device = (CachedBluetoothDevice) v.getTag();
-            Bundle args = new Bundle();
-            args.putString(DeviceProfilesSettings.ARG_DEVICE_ADDRESS,
-                    device.getDevice().getAddress());
-            DeviceProfilesSettings profileSettings = new DeviceProfilesSettings();
-            profileSettings.setArguments(args);
-            profileSettings.show(getFragmentManager(),
-                    DeviceProfilesSettings.class.getSimpleName());
+    private final GearPreference.OnGearClickListener mDeviceProfilesListener = pref -> {
+        // User clicked on advanced options icon for a device in the list
+        if (!(pref instanceof BluetoothDevicePreference)) {
+            Log.w(TAG, "onClick() called for other View: " + pref);
+            return;
         }
+        final CachedBluetoothDevice device =
+                ((BluetoothDevicePreference) pref).getBluetoothDevice();
+        if (device == null) {
+            Log.w(TAG, "No BT device attached with this pref: " + pref);
+            return;
+        }
+        final Bundle args = new Bundle();
+        args.putString(DeviceProfilesSettings.ARG_DEVICE_ADDRESS,
+                device.getDevice().getAddress());
+        final DeviceProfilesSettings profileSettings = new DeviceProfilesSettings();
+        profileSettings.setArguments(args);
+        profileSettings.show(getFragmentManager(),
+                DeviceProfilesSettings.class.getSimpleName());
     };
 
     /**
@@ -506,7 +508,7 @@
         CachedBluetoothDevice cachedDevice = preference.getCachedDevice();
         if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
             // Only paired device have an associated advanced settings screen
-            preference.setOnSettingsClickListener(mDeviceProfilesListener);
+            preference.setOnGearClickListener(mDeviceProfilesListener);
         }
     }
 
diff --git a/src/com/android/settings/bluetooth/DevicePickerFragment.java b/src/com/android/settings/bluetooth/DevicePickerFragment.java
index f250c9a..490a3ad 100644
--- a/src/com/android/settings/bluetooth/DevicePickerFragment.java
+++ b/src/com/android/settings/bluetooth/DevicePickerFragment.java
@@ -62,11 +62,6 @@
     }
 
     @Override
-    void initDevicePreference(BluetoothDevicePreference preference) {
-        preference.setWidgetLayoutResource(R.layout.preference_empty_list);
-    }
-
-    @Override
     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
         menu.add(Menu.NONE, MENU_ID_REFRESH, 0, R.string.bluetooth_search_for_devices)
                 .setEnabled(true)
diff --git a/src/com/android/settings/dashboard/ExpandPreference.java b/src/com/android/settings/dashboard/ExpandPreference.java
index 12ca5ac..3939e74 100644
--- a/src/com/android/settings/dashboard/ExpandPreference.java
+++ b/src/com/android/settings/dashboard/ExpandPreference.java
@@ -18,6 +18,7 @@
 
 import android.content.Context;
 import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceViewHolder;
 import android.util.AttributeSet;
 
 import com.android.settings.R;
@@ -51,4 +52,10 @@
         setTitle(R.string.advanced_section_header);
         setOrder(999);
     }
+
+    @Override
+    public void onBindViewHolder(PreferenceViewHolder holder) {
+        super.onBindViewHolder(holder);
+        holder.setDividerAllowedAbove(false);
+    }
 }
diff --git a/src/com/android/settings/development/DevelopmentSettings.java b/src/com/android/settings/development/DevelopmentSettings.java
index 2bfb602..3deedaf 100644
--- a/src/com/android/settings/development/DevelopmentSettings.java
+++ b/src/com/android/settings/development/DevelopmentSettings.java
@@ -396,7 +396,7 @@
             // Block access to developer options if the user is not the owner, if user policy
             // restricts it, or if the device has not been provisioned
             mUnavailable = true;
-            setPreferenceScreen(new PreferenceScreen(getPrefContext(), null));
+            addPreferencesFromResource(R.xml.empty_settings);
             return;
         }
 
diff --git a/src/com/android/settings/vpn2/AppDialogFragment.java b/src/com/android/settings/vpn2/AppDialogFragment.java
index b24db4e..f8e6af0 100644
--- a/src/com/android/settings/vpn2/AppDialogFragment.java
+++ b/src/com/android/settings/vpn2/AppDialogFragment.java
@@ -18,7 +18,6 @@
 
 import android.app.AlertDialog;
 import android.app.Dialog;
-import android.app.DialogFragment;
 import android.app.Fragment;
 import android.content.Context;
 import android.content.DialogInterface;
@@ -67,13 +66,18 @@
 
     public static void show(Fragment parent, PackageInfo packageInfo, String label,
             boolean managing, boolean connected) {
+        if (!managing && !connected) {
+            // We can't display anything useful for this case.
+            return;
+        }
         show(parent, null, packageInfo, label, managing, connected);
     }
 
     public static void show(Fragment parent, Listener listener, PackageInfo packageInfo,
             String label, boolean managing, boolean connected) {
-        if (!parent.isAdded())
+        if (!parent.isAdded()) {
             return;
+        }
 
         Bundle args = new Bundle();
         args.putParcelable(ARG_PACKAGE, packageInfo);
@@ -100,7 +104,7 @@
         final String label = args.getString(ARG_LABEL);
         boolean managing = args.getBoolean(ARG_MANAGING);
         boolean connected = args.getBoolean(ARG_CONNECTED);
-        mPackageInfo = (PackageInfo) args.getParcelable(ARG_PACKAGE);
+        mPackageInfo = args.getParcelable(ARG_PACKAGE);
 
         if (managing) {
             return new AppDialog(getActivity(), this, mPackageInfo, label);
diff --git a/tests/app/src/com/android/settings/applications/ExternalSourcesSettingsTest.java b/tests/app/src/com/android/settings/applications/ExternalSourcesSettingsTest.java
index 9114c6f..82f0e0a 100644
--- a/tests/app/src/com/android/settings/applications/ExternalSourcesSettingsTest.java
+++ b/tests/app/src/com/android/settings/applications/ExternalSourcesSettingsTest.java
@@ -93,7 +93,7 @@
 
     private Intent createManageExternalSourcesListIntent() {
         final Intent manageExternalSourcesIntent = new Intent();
-        manageExternalSourcesIntent.setAction(Settings.ACTION_MANAGE_EXTERNAL_SOURCES);
+        manageExternalSourcesIntent.setAction(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
         return manageExternalSourcesIntent;
     }
 
diff --git a/tests/app/src/com/android/settings/bluetooth/DevicePickerActivityTest.java b/tests/app/src/com/android/settings/bluetooth/DevicePickerActivityTest.java
new file mode 100644
index 0000000..1b855c9
--- /dev/null
+++ b/tests/app/src/com/android/settings/bluetooth/DevicePickerActivityTest.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.bluetooth;
+
+import android.app.Instrumentation;
+import android.content.Intent;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+@SmallTest
+public class DevicePickerActivityTest {
+
+    private Instrumentation mInstrumentation;
+
+    @Before
+    public void setUp() throws Exception {
+        mInstrumentation = InstrumentationRegistry.getInstrumentation();
+    }
+
+    @Test
+    public void startActivityNoCrash() {
+        mInstrumentation.startActivitySync(
+                new Intent("android.bluetooth.devicepicker.action.LAUNCH"));
+        // No crash
+    }
+}
diff --git a/tests/app/src/com/android/settings/fuelgauge/PowerUsageSummaryUiTest.java b/tests/app/src/com/android/settings/fuelgauge/PowerUsageSummaryUiTest.java
index 47e644a..6c372c5 100644
--- a/tests/app/src/com/android/settings/fuelgauge/PowerUsageSummaryUiTest.java
+++ b/tests/app/src/com/android/settings/fuelgauge/PowerUsageSummaryUiTest.java
@@ -16,26 +16,11 @@
 
 package com.android.settings.fuelgauge;
 
-import static android.support.test.InstrumentationRegistry.getTargetContext;
-import static android.support.test.espresso.Espresso.onData;
 import static android.support.test.espresso.Espresso.onView;
 import static android.support.test.espresso.action.ViewActions.click;
-import static android.support.test.espresso.intent.Intents.intended;
-import static android.support.test.espresso.intent.Intents.intending;
-import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;
-import static android.support.test.espresso.matcher.PreferenceMatchers.withKey;
-import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA;
-import static android.support.test.espresso.matcher.ViewMatchers.withId;
 import static android.support.test.espresso.matcher.ViewMatchers.withText;
 
-import static org.hamcrest.Matchers.allOf;
-import static org.hamcrest.core.IsAnything.anything;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import android.app.Activity;
 import android.app.Instrumentation;
-import android.content.Context;
 import android.content.Intent;
 import android.support.test.InstrumentationRegistry;
 import android.support.test.filters.SmallTest;
diff --git a/tests/robotests/src/com/android/settings/applications/AppStorageSizesControllerTest.java b/tests/robotests/src/com/android/settings/applications/AppStorageSizesControllerTest.java
index 7204bd1..127100f 100644
--- a/tests/robotests/src/com/android/settings/applications/AppStorageSizesControllerTest.java
+++ b/tests/robotests/src/com/android/settings/applications/AppStorageSizesControllerTest.java
@@ -92,4 +92,22 @@
         assertThat(mDataPreference.getSummary()).isEqualTo("100B");
         assertThat(mTotalPreference.getSummary()).isEqualTo("111B");
     }
+
+    @Test
+    public void fakeCacheFlagSetsCacheToZero() {
+        AppStorageStats result = mock(AppStorageStats.class);
+        when(result.getCodeBytes()).thenReturn(1L);
+        when(result.getCacheBytes()).thenReturn(10L);
+        when(result.getDataBytes()).thenReturn(100L);
+        when(result.getTotalBytes()).thenReturn(111L);
+
+        mController.setResult(result);
+        mController.setCacheCleared(true);
+        mController.updateUi(mContext);
+
+        assertThat(mAppPreference.getSummary()).isEqualTo("1.00B");
+        assertThat(mCachePreference.getSummary()).isEqualTo("0.00B");
+        assertThat(mDataPreference.getSummary()).isEqualTo("100B");
+        assertThat(mTotalPreference.getSummary()).isEqualTo("101B");
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePreferenceTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePreferenceTest.java
index 5915118..c4c1f8a 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePreferenceTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePreferenceTest.java
@@ -17,8 +17,10 @@
 
 import android.bluetooth.BluetoothDevice;
 import android.content.Context;
+import android.os.UserManager;
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.settings.R;
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
@@ -32,7 +34,10 @@
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
+import org.robolectric.util.ReflectionHelpers;
 
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -66,7 +71,7 @@
         mPreference.onClicked();
 
         verify(mMetricsFeatureProvider).action(
-            mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_DISCONNECT);
+                mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_DISCONNECT);
     }
 
     @Test
@@ -77,7 +82,7 @@
         mPreference.onClicked();
 
         verify(mMetricsFeatureProvider).action(
-            mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT);
+                mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT);
     }
 
     @Test
@@ -89,6 +94,46 @@
         mPreference.onClicked();
 
         verify(mMetricsFeatureProvider).action(
-            mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
+                mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
+    }
+
+    @Test
+    public void getSecondTargetResource_shouldBeGearIconLayout() {
+        assertThat(mPreference.getSecondTargetResId()).isEqualTo(R.layout.preference_widget_gear);
+    }
+
+    @Test
+    public void shouldHideSecondTarget_noDevice_shouldReturnTrue() {
+        ReflectionHelpers.setField(mPreference, "mCachedDevice", null);
+
+        assertThat(mPreference.shouldHideSecondTarget()).isTrue();
+    }
+
+    @Test
+    public void shouldHideSecondTarget_notBond_shouldReturnTrue() {
+        when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
+
+        assertThat(mPreference.shouldHideSecondTarget()).isTrue();
+    }
+
+    @Test
+    public void shouldHideSecondTarget_hasUserRestriction_shouldReturnTrue() {
+        final UserManager um = mock(UserManager.class);
+        ReflectionHelpers.setField(mPreference, "mUserManager", um);
+        when(um.hasUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH))
+                .thenReturn(true);
+
+        assertThat(mPreference.shouldHideSecondTarget()).isTrue();
+    }
+
+    @Test
+    public void shouldHideSecondTarget_hasBoundDeviceAndNoRestriction_shouldReturnFalse() {
+        when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
+        final UserManager um = mock(UserManager.class);
+        ReflectionHelpers.setField(mPreference, "mUserManager", um);
+        when(um.hasUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH))
+                .thenReturn(false);
+
+        assertThat(mPreference.shouldHideSecondTarget()).isFalse();
     }
 }
diff --git a/tests/robotests/src/com/android/settings/vpn2/AppDialogFragmentTest.java b/tests/robotests/src/com/android/settings/vpn2/AppDialogFragmentTest.java
new file mode 100644
index 0000000..088bfae
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/vpn2/AppDialogFragmentTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.vpn2;
+
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+import android.app.Fragment;
+import android.content.pm.PackageInfo;
+
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+
+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 AppDialogFragmentTest {
+
+    @Mock
+    private Fragment mParent;
+
+    private PackageInfo mPackageInfo;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mPackageInfo = new PackageInfo();
+    }
+
+    @Test
+    public void notManagingOrConnected_shouldNotShow() {
+        AppDialogFragment.show(mParent, mPackageInfo, "label", false /* manage */,
+                false /* connected */);
+
+        verify(mParent, never()).isAdded();
+    }
+
+    @Test
+    public void notManagingAndConnected_showShow() {
+        AppDialogFragment.show(mParent, mPackageInfo, "label", false /* manage */,
+                true /* connected */);
+
+        verify(mParent).isAdded();
+    }
+}