Merge "Apply admin restriction when network reset is disallowed" into oc-dev
diff --git a/res/drawable/ic_media_stream_on_24dp.xml b/res/drawable/ic_media_stream_on_24dp.xml
new file mode 100644
index 0000000..3db55dd
--- /dev/null
+++ b/res/drawable/ic_media_stream_on_24dp.xml
@@ -0,0 +1,25 @@
+<?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.
+  -->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M18,3h-5c-0.55,0 -1,0.45 -1,1v8.3a3.88,3.88 0,0 0,-2.9 -0.04c-1.79,0.67 -3.11,2.35 -3.1,4.26A4.483,4.483 0,0 0,10.5 21c2.5,0 4.5,-2.3 4.5,-4.5V6h3c0.55,0 1,-0.45 1,-1V4c0,-0.55 -0.45,-1 -1,-1z"/>
+</vector>
diff --git a/res/layout/storage_item.xml b/res/layout/storage_item.xml
index 33f4f9e..0c1f60c 100644
--- a/res/layout/storage_item.xml
+++ b/res/layout/storage_item.xml
@@ -76,6 +76,7 @@
         android:id="@android:id/progress"
         android:layout_width="match_parent"
         android:layout_height="8dp"
+        android:layout_marginStart="60dp"
         android:layout_marginTop="16dp"
         android:layout_marginBottom="8dp"
         android:visibility="gone"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index dea40b6..36643d3 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -4686,9 +4686,9 @@
     <string name="battery_last_full_charge">Last full charge</string>
     <!-- Description for text in battery footer. [CHAR LIMIT=120] -->
     <string name="battery_footer_summary">Remaining battery time is approximate and can change based on usage</string>
-    <!-- Title for battery usage detail in foreground. [CHAR LIMIT=80] -->
+    <!-- Title for text that shows the amount of time an app has been running while in the foreground. [CHAR LIMIT=80] -->
     <string name="battery_detail_foreground">While in active use</string>
-    <!-- Title for battery usage detail in background. [CHAR LIMIT=80] -->
+    <!-- Title for text that shows the amount of time an app has been running while in the background. [CHAR LIMIT=80] -->
     <string name="battery_detail_background">While in background</string>
     <!-- Title for battery usage amount by this app. [CHAR LIMIT=80] -->
     <string name="battery_detail_power_usage">Battery usage</string>
diff --git a/res/xml/app_memory_settings.xml b/res/xml/app_memory_settings.xml
index 53a71ff..308f189 100644
--- a/res/xml/app_memory_settings.xml
+++ b/res/xml/app_memory_settings.xml
@@ -18,26 +18,25 @@
                   android:title="@string/memory_usage">
 
     <PreferenceCategory
-        android:title="@string/average_memory_use"/>
+        android:title="@string/average_memory_use">
 
-    <com.android.settings.SummaryPreference
-        android:key="status_header"
-        android:selectable="false" />
+        <com.android.settings.SummaryPreference
+            android:key="status_header"
+            android:selectable="false" />
 
-    <com.android.settings.applications.SpacePreference
-        android:layout_height="5dp" />
+        <Preference
+            android:key="frequency"
+            android:selectable="false"
+            android:layout="@layout/horizontal_preference"
+            android:title="@string/running_frequency" />
 
-    <Preference
-        android:key="frequency"
-        android:selectable="false"
-        android:layout="@layout/horizontal_preference"
-        android:title="@string/running_frequency" />
+        <Preference
+            android:key="max_usage"
+            android:selectable="false"
+            android:layout="@layout/horizontal_preference"
+            android:title="@string/memory_maximum_usage" />
 
-    <Preference
-        android:key="max_usage"
-        android:selectable="false"
-        android:layout="@layout/horizontal_preference"
-        android:title="@string/memory_maximum_usage" />
+    </PreferenceCategory>
 
     <PreferenceCategory
         android:key="processes"
diff --git a/res/xml/installed_app_details_ia.xml b/res/xml/installed_app_details_ia.xml
index b092753..538205f 100644
--- a/res/xml/installed_app_details_ia.xml
+++ b/res/xml/installed_app_details_ia.xml
@@ -52,6 +52,11 @@
         android:summary="@string/summary_placeholder"
         android:selectable="true"/>
 
+    <com.android.settings.applications.AppDomainsPreference
+        android:key="instant_app_launch_supported_domain_urls"
+        android:title="@string/app_launch_supported_domain_urls_title"
+        android:selectable="true" />
+
     <Preference
         android:key="data_settings"
         android:title="@string/data_usage_summary_title"
diff --git a/res/xml/power_usage_detail_ia.xml b/res/xml/power_usage_detail_ia.xml
index cfaa712..39e6c38 100644
--- a/res/xml/power_usage_detail_ia.xml
+++ b/res/xml/power_usage_detail_ia.xml
@@ -56,7 +56,7 @@
 
         <Preference
             android:key="battery_optimization"
-            android:title="@string/battery_detail_background"
+            android:title="@string/high_power_apps"
             android:summary="@string/high_power_off"
             android:selectable="true"/>
 
diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index 015af17..34990ec 100644
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -83,7 +83,10 @@
 
     // Constants for state save/restore
     private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
-    private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
+    @VisibleForTesting
+    static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
+    @VisibleForTesting
+    static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
 
     /**
      * When starting this activity, the invoking Intent can contain this extra
@@ -192,8 +195,10 @@
 
     private Button mNextButton;
 
-    private boolean mDisplayHomeAsUpEnabled;
-    private boolean mDisplaySearch;
+    @VisibleForTesting
+    boolean mDisplayHomeAsUpEnabled;
+    @VisibleForTesting
+    boolean mDisplaySearch;
 
     private boolean mIsShowingDashboard;
     private boolean mIsShortcut;
@@ -230,7 +235,6 @@
         if (!mDisplaySearch) {
             return false;
         }
-
         mSearchFeatureProvider.setUpSearchMenu(menu, this);
         return true;
     }
@@ -513,12 +517,28 @@
     @Override
     protected void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
+        saveState(outState);
+    }
 
+    /**
+     * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
+     */
+    @VisibleForTesting
+    void saveState(Bundle outState) {
         if (mCategories.size() > 0) {
             outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
         }
 
         outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
+        outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
+    }
+
+    @Override
+    protected void onRestoreInstanceState(Bundle savedInstanceState) {
+        super.onRestoreInstanceState(savedInstanceState);
+
+        mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
+        mDisplaySearch = savedInstanceState.getBoolean(SAVE_KEY_SHOW_SEARCH);
     }
 
     @Override
diff --git a/src/com/android/settings/applications/InstalledAppDetails.java b/src/com/android/settings/applications/InstalledAppDetails.java
index df1cfa3..7510146 100755
--- a/src/com/android/settings/applications/InstalledAppDetails.java
+++ b/src/com/android/settings/applications/InstalledAppDetails.java
@@ -113,6 +113,7 @@
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
 
@@ -159,6 +160,8 @@
     private static final String KEY_BATTERY = "battery";
     private static final String KEY_MEMORY = "memory";
     private static final String KEY_VERSION = "app_version";
+    private static final String KEY_INSTANT_APP_SUPPORTED_LINKS =
+            "instant_app_launch_supported_domain_urls";
 
     private final HashSet<String> mHomePackages = new HashSet<>();
 
@@ -176,6 +179,7 @@
     private Preference mDataPreference;
     private Preference mMemoryPreference;
     private Preference mVersionPreference;
+    private AppDomainsPreference mInstantAppDomainsPreference;
 
     private boolean mDisableAfterUninstall;
 
@@ -433,7 +437,8 @@
         mMemoryPreference = findPreference(KEY_MEMORY);
         mMemoryPreference.setOnPreferenceClickListener(this);
         mVersionPreference = findPreference(KEY_VERSION);
-
+        mInstantAppDomainsPreference =
+                (AppDomainsPreference) findPreference(KEY_INSTANT_APP_SUPPORTED_LINKS);
         mLaunchPreference = findPreference(KEY_LAUNCH);
         if (mAppEntry != null && mAppEntry.info != null) {
             if ((mAppEntry.info.flags&ApplicationInfo.FLAG_INSTALLED) == 0 ||
@@ -551,6 +556,25 @@
     public void onLoaderReset(Loader<AppStorageStats> loader) {
     }
 
+    /**
+     * Utility method to hide and show specific preferences based on whether the app being displayed
+     * is an Instant App or an installed app.
+     */
+    @VisibleForTesting
+    void prepareInstantAppPrefs() {
+        final boolean isInstant = AppUtils.isInstant(mPackageInfo.applicationInfo);
+        if (isInstant) {
+            Set<String> handledDomainSet = Utils.getHandledDomains(mPm, mPackageInfo.packageName);
+            String[] handledDomains = handledDomainSet.toArray(new String[handledDomainSet.size()]);
+            mInstantAppDomainsPreference.setTitles(handledDomains);
+            // Dummy values, unused in the implementation
+            mInstantAppDomainsPreference.setValues(new int[handledDomains.length]);
+            getPreferenceScreen().removePreference(mLaunchPreference);
+        } else {
+            getPreferenceScreen().removePreference(mInstantAppDomainsPreference);
+        }
+    }
+
     // Utility method to set application label and icon.
     private void setAppLabelAndIcon(PackageInfo pkgInfo) {
         final View appSnippet = mHeader.findViewById(R.id.app_snippet);
@@ -642,6 +666,7 @@
         checkForceStop();
         setAppLabelAndIcon(mPackageInfo);
         initUninstallButtons();
+        prepareInstantAppPrefs();
 
         // Update the preference summaries.
         Activity context = getActivity();
diff --git a/src/com/android/settings/applications/MusicViewHolderController.java b/src/com/android/settings/applications/MusicViewHolderController.java
index 4599fcc..69f8958 100644
--- a/src/com/android/settings/applications/MusicViewHolderController.java
+++ b/src/com/android/settings/applications/MusicViewHolderController.java
@@ -61,7 +61,7 @@
 
     @Override
     public void setupView(AppViewHolder holder) {
-        holder.appIcon.setImageDrawable(mContext.getDrawable(R.drawable.empty_icon));
+        holder.appIcon.setImageDrawable(mContext.getDrawable(R.drawable.ic_media_stream_on_24dp));
         holder.appName.setText(mContext.getText(R.string.audio_files_title));
         holder.summary.setText(Formatter.formatFileSize(mContext, mMusicSize));
     }
diff --git a/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceController.java b/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceController.java
index 463d4dc..60a94dd 100644
--- a/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceController.java
@@ -55,16 +55,14 @@
     public void updateState(Preference preference) {
         final int mode = mAppOpsManager
                 .checkOpNoThrow(AppOpsManager.OP_RUN_IN_BACKGROUND, mUid, mTargetPackage);
-
         if (mode == AppOpsManager.MODE_ERRORED) {
             preference.setEnabled(false);
-            preference.setSummary(R.string.background_activity_summary_disabled);
         } else {
             final boolean checked = mode != AppOpsManager.MODE_IGNORED;
             ((SwitchPreference) preference).setChecked(checked);
-            preference.setSummary(checked ? R.string.background_activity_summary_on
-                    : R.string.background_activity_summary_off);
         }
+
+        updateSummary(preference);
     }
 
     @Override
@@ -92,6 +90,8 @@
         boolean switchOn = (Boolean) newValue;
         mAppOpsManager.setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, mUid, mTargetPackage,
                 switchOn ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
+
+        updateSummary(preference);
         return true;
     }
 
@@ -113,4 +113,18 @@
 
         return false;
     }
+
+    @VisibleForTesting
+    void updateSummary(Preference preference) {
+        final int mode = mAppOpsManager
+                .checkOpNoThrow(AppOpsManager.OP_RUN_IN_BACKGROUND, mUid, mTargetPackage);
+
+        if (mode == AppOpsManager.MODE_ERRORED) {
+            preference.setSummary(R.string.background_activity_summary_disabled);
+        } else {
+            final boolean checked = mode != AppOpsManager.MODE_IGNORED;
+            preference.setSummary(checked ? R.string.background_activity_summary_on
+                    : R.string.background_activity_summary_off);
+        }
+    }
 }
diff --git a/src/com/android/settings/fuelgauge/PowerUsageAdvanced.java b/src/com/android/settings/fuelgauge/PowerUsageAdvanced.java
index 3509834..a0ebba8 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageAdvanced.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageAdvanced.java
@@ -172,7 +172,7 @@
         mUsageListGroup.removeAll();
         for (int i = 0, size = dataList.size(); i < size; i++) {
             final PowerUsageData batteryData = dataList.get(i);
-            if (shouldHide(batteryData)) {
+            if (shouldHideCategory(batteryData)) {
                 continue;
             }
             final PowerGaugePreference pref = new PowerGaugePreference(getPrefContext());
@@ -217,7 +217,7 @@
     }
 
     @VisibleForTesting
-    boolean shouldHide(PowerUsageData powerUsageData) {
+    boolean shouldHideCategory(PowerUsageData powerUsageData) {
         if (powerUsageData.usageType == UsageType.UNACCOUNTED
                 || powerUsageData.usageType == UsageType.OVERCOUNTED) {
             return true;
@@ -264,6 +264,9 @@
 
     @VisibleForTesting
     void updateUsageDataSummary(PowerUsageData usageData, double totalPower, int dischargeAmount) {
+        if (shouldHideSummary(usageData)) {
+            return;
+        }
         if (usageData.usageList.size() <= 1) {
             usageData.summary = getString(R.string.battery_used_for,
                     Utils.formatElapsedTime(getContext(), usageData.totalUsageTimeMs, false));
@@ -278,6 +281,13 @@
     }
 
     @VisibleForTesting
+    boolean shouldHideSummary(PowerUsageData powerUsageData) {
+        @UsageType final int usageType = powerUsageData.usageType;
+
+        return usageType == UsageType.CELL;
+    }
+
+    @VisibleForTesting
     BatterySipper findBatterySipperWithMaxBatteryUsage(List<BatterySipper> usageList) {
         BatterySipper sipper = usageList.get(0);
         for (int i = 1, size = usageList.size(); i < size; i++) {
diff --git a/tests/robotests/src/com/android/settings/SettingsActivityTest.java b/tests/robotests/src/com/android/settings/SettingsActivityTest.java
index 9d53bc8..65e9708 100644
--- a/tests/robotests/src/com/android/settings/SettingsActivityTest.java
+++ b/tests/robotests/src/com/android/settings/SettingsActivityTest.java
@@ -16,20 +16,28 @@
 
 package com.android.settings;
 
+import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.FragmentManager;
 import android.app.FragmentTransaction;
+import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
 
+import android.os.Bundle;
+import android.view.Menu;
+import com.android.settings.testutils.FakeFeatureFactory;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Answers;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
+import org.robolectric.util.ReflectionHelpers;
 
+import static com.google.common.truth.Truth.assertThat;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyInt;
 import static org.mockito.Mockito.doReturn;
@@ -42,6 +50,9 @@
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
 public class SettingsActivityTest {
 
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private Context mContext;
+
     @Mock
     private FragmentManager mFragmentManager;
     @Mock
@@ -50,10 +61,15 @@
     private Bitmap mBitmap;
     private SettingsActivity mActivity;
 
+    private FakeFeatureFactory mFeatureFactory;
+
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
+        FakeFeatureFactory.setupForTest(mContext);
+        mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
+
         mActivity = spy(new SettingsActivity());
         doReturn(mBitmap).when(mActivity).getBitmapFromXmlResource(anyInt());
     }
@@ -61,7 +77,6 @@
     @Test
     public void launchSettingFragment_nullExtraShowFragment_shouldNotCrash()
             throws ClassNotFoundException {
-        mActivity = spy(new SettingsActivity());
         when(mActivity.getFragmentManager()).thenReturn(mFragmentManager);
         when(mFragmentManager.beginTransaction()).thenReturn(mock(FragmentTransaction.class));
 
@@ -76,4 +91,51 @@
 
         verify(mTaskDescription).setIcon(any());
     }
+
+    @Test
+    public void testCreateOptionsMenu_setsUpSearch() {
+        ReflectionHelpers.setField(mActivity, "mSearchFeatureProvider",
+                mFeatureFactory.getSearchFeatureProvider());
+        mActivity.mDisplaySearch = true;
+        mActivity.onCreateOptionsMenu(null);
+
+        verify(mFeatureFactory.getSearchFeatureProvider()).setUpSearchMenu(any(Menu.class),
+                any(Activity.class));
+    }
+
+    @Test
+    public void testSaveState_DisplaySearchSaved() {
+        mActivity.mDisplaySearch = true;
+        Bundle bundle = new Bundle();
+        mActivity.saveState(bundle);
+
+        assertThat((boolean) bundle.get(SettingsActivity.SAVE_KEY_SHOW_SEARCH)).isTrue();
+    }
+
+    @Test
+    public void testSaveState_EnabledHomeSaved() {
+        mActivity.mDisplayHomeAsUpEnabled = true;
+        Bundle bundle = new Bundle();
+        mActivity.saveState(bundle);
+
+        assertThat((boolean) bundle.get(SettingsActivity.SAVE_KEY_SHOW_HOME_AS_UP)).isTrue();
+    }
+
+    @Test
+    public void testRestoreState_DisplaySearchRestored() {
+        Bundle bundle = new Bundle();
+        bundle.putBoolean(SettingsActivity.SAVE_KEY_SHOW_SEARCH, true);
+        mActivity.onRestoreInstanceState(bundle);
+
+        assertThat(mActivity.mDisplaySearch).isTrue();
+    }
+
+    @Test
+    public void testRestoreState_EnabledHomeRestored() {
+        Bundle bundle = new Bundle();
+        bundle.putBoolean(SettingsActivity.SAVE_KEY_SHOW_SEARCH, true);
+        mActivity.onRestoreInstanceState(bundle);
+
+        assertThat(mActivity.mDisplaySearch).isTrue();
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java b/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java
index 5b1019e..f372836 100644
--- a/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java
+++ b/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java
@@ -17,21 +17,34 @@
 package com.android.settings.applications;
 
 
-import android.app.Activity;
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
 import android.app.AlertDialog;
 import android.app.admin.DevicePolicyManager;
 import android.content.Context;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
 import android.os.BatteryStats;
 import android.os.UserManager;
 import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceManager;
+import android.support.v7.preference.PreferenceScreen;
 import android.view.View;
 import android.widget.Button;
 
 import com.android.internal.os.BatterySipper;
 import com.android.internal.os.BatteryStatsHelper;
-import com.android.settings.R;
 import com.android.settings.SettingsActivity;
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
@@ -39,8 +52,8 @@
 import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settingslib.applications.AppUtils;
 import com.android.settingslib.applications.ApplicationsState.AppEntry;
-import com.android.settingslib.applications.instantapps.InstantAppDataProvider;
 import com.android.settingslib.applications.StorageStatsSource.AppStorageStats;
+import com.android.settingslib.applications.instantapps.InstantAppDataProvider;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -52,18 +65,6 @@
 import org.robolectric.annotation.Config;
 import org.robolectric.util.ReflectionHelpers;
 
-import static com.google.common.truth.Truth.assertThat;
-
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyInt;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
 
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@@ -315,4 +316,43 @@
         verify(buttonsController).setPackageName(anyString());
         verify(buttonsController).show();
     }
+
+    @Test
+    public void instantApps_removeCorrectPref() {
+        PreferenceScreen mockPreferenceScreen = mock(PreferenceScreen.class);
+        PreferenceManager mockPreferenceManager = mock(PreferenceManager.class);
+        AppDomainsPreference mockAppDomainsPref = mock(AppDomainsPreference.class);
+        Preference mockLaunchPreference = mock(Preference.class);
+        PackageInfo mockPackageInfo = mock(PackageInfo.class);
+        PackageManager mockPackageManager = mock(PackageManager.class);
+        ReflectionHelpers.setField(
+                mAppDetail, "mLaunchPreference", mockLaunchPreference);
+        ReflectionHelpers.setField(
+                mAppDetail, "mInstantAppDomainsPreference", mockAppDomainsPref);
+        ReflectionHelpers.setField(
+                mAppDetail, "mPreferenceManager", mockPreferenceManager);
+        ReflectionHelpers.setField(
+                mAppDetail, "mPackageInfo", mockPackageInfo);
+        ReflectionHelpers.setField(
+                mAppDetail, "mPm", mockPackageManager);
+        when(mockPreferenceManager.getPreferenceScreen()).thenReturn(mockPreferenceScreen);
+
+        ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
+                (InstantAppDataProvider) (i -> false));
+        mAppDetail.prepareInstantAppPrefs();
+
+        // For the non instant case we remove the app domain pref, and leave the launch pref
+        verify(mockPreferenceScreen).removePreference(mockAppDomainsPref);
+        verify(mockPreferenceScreen, never()).removePreference(mockLaunchPreference);
+
+        // For the instant app case we remove the launch preff, and leave the app domain pref
+        ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
+                (InstantAppDataProvider) (i -> true));
+
+        mAppDetail.prepareInstantAppPrefs();
+        verify(mockPreferenceScreen).removePreference(mockLaunchPreference);
+        // Will be 1 still due to above call
+        verify(mockPreferenceScreen, times(1))
+                .removePreference(mockAppDomainsPref);
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceControllerTest.java
index 0813478..4f48945 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceControllerTest.java
@@ -23,7 +23,9 @@
 import android.os.Build;
 import android.support.v14.preference.SwitchPreference;
 
+import com.android.settings.R;
 import com.android.settings.TestConfig;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -31,9 +33,12 @@
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
 
 import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -54,17 +59,18 @@
     @Mock
     private AppOpsManager mAppOpsManager;
     @Mock
-    private SwitchPreference mPreference;
-    @Mock
     private ApplicationInfo mHighApplicationInfo;
     @Mock
     private ApplicationInfo mLowApplicationInfo;
     private BackgroundActivityPreferenceController mController;
+    private SwitchPreference mPreference;
+    private Context mShadowContext;
 
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
 
+        mShadowContext = RuntimeEnvironment.application;
         when(mContext.getPackageManager()).thenReturn(mPackageManager);
         when(mContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mAppOpsManager);
         when(mPackageManager.getPackagesForUid(UID_NORMAL)).thenReturn(PACKAGES_NORMAL);
@@ -77,7 +83,8 @@
         mHighApplicationInfo.targetSdkVersion = Build.VERSION_CODES.O;
         mLowApplicationInfo.targetSdkVersion = Build.VERSION_CODES.L;
 
-        mController = new BackgroundActivityPreferenceController(mContext, UID_NORMAL);
+        mPreference = new SwitchPreference(mShadowContext);
+        mController = spy(new BackgroundActivityPreferenceController(mContext, UID_NORMAL));
         mController.isAvailable();
     }
 
@@ -87,14 +94,16 @@
 
         verify(mAppOpsManager).setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, UID_NORMAL,
                 mController.getTargetPackage(), AppOpsManager.MODE_ALLOWED);
+        verify(mController).updateSummary(mPreference);
     }
 
     @Test
     public void testOnPreferenceChange_TurnOffCheck_MethodInvoked() {
-        mController.onPreferenceChange(null, false);
+        mController.onPreferenceChange(mPreference, false);
 
         verify(mAppOpsManager).setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, UID_NORMAL,
                 mController.getTargetPackage(), AppOpsManager.MODE_IGNORED);
+        verify(mController).updateSummary(mPreference);
     }
 
     @Test
@@ -105,7 +114,8 @@
 
         mController.updateState(mPreference);
 
-        verify(mPreference).setChecked(true);
+        assertThat(mPreference.isChecked()).isTrue();
+        verify(mController).updateSummary(mPreference);
     }
 
     @Test
@@ -116,7 +126,46 @@
 
         mController.updateState(mPreference);
 
-        verify(mPreference).setChecked(false);
+        assertThat(mPreference.isChecked()).isFalse();
+        verify(mController).updateSummary(mPreference);
+    }
+
+    @Test
+    public void testUpdateSummary_modeError_showSummaryDisabled() {
+        when(mAppOpsManager
+                .checkOpNoThrow(AppOpsManager.OP_RUN_IN_BACKGROUND, UID_NORMAL, LOW_SDK_PACKAGE))
+                .thenReturn(AppOpsManager.MODE_ERRORED);
+        final CharSequence expectedSummary = mShadowContext.getText(
+                R.string.background_activity_summary_disabled);
+        mController.updateSummary(mPreference);
+
+        assertThat(mPreference.getSummary()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void testUpdateSummary_modeDefault_showSummaryOn() {
+        when(mAppOpsManager
+                .checkOpNoThrow(AppOpsManager.OP_RUN_IN_BACKGROUND, UID_NORMAL, LOW_SDK_PACKAGE))
+                .thenReturn(AppOpsManager.MODE_DEFAULT);
+        final CharSequence expectedSummary = mShadowContext.getText(
+                R.string.background_activity_summary_on);
+
+        mController.updateSummary(mPreference);
+
+        assertThat(mPreference.getSummary()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void testUpdateSummary_modeIgnored_showSummaryOff() {
+        when(mAppOpsManager
+                .checkOpNoThrow(AppOpsManager.OP_RUN_IN_BACKGROUND, UID_NORMAL, LOW_SDK_PACKAGE))
+                .thenReturn(AppOpsManager.MODE_IGNORED);
+        final CharSequence expectedSummary = mShadowContext.getText(
+                R.string.background_activity_summary_off);
+
+        mController.updateSummary(mPreference);
+
+        assertThat(mPreference.getSummary()).isEqualTo(expectedSummary);
     }
 
     @Test
@@ -144,6 +193,6 @@
 
         assertThat(available).isTrue();
         // Should get status from LOW_SDK_PACKAGE
-        verify(mPreference).setChecked(true);
+        assertThat(mPreference.isChecked()).isTrue();
     }
 }
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageAdvancedTest.java b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageAdvancedTest.java
index 4216d57..3f87bb4 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageAdvancedTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageAdvancedTest.java
@@ -228,22 +228,36 @@
     public void testShouldHide_typeUnAccounted_returnTrue() {
         mPowerUsageData.usageType = UsageType.UNACCOUNTED;
 
-        assertThat(mPowerUsageAdvanced.shouldHide(mPowerUsageData)).isTrue();
+        assertThat(mPowerUsageAdvanced.shouldHideCategory(mPowerUsageData)).isTrue();
     }
 
 
     @Test
-    public void testShouldHide_typeOverCounted_returnTrue() {
+    public void testShouldHideCategory_typeOverCounted_returnTrue() {
         mPowerUsageData.usageType = UsageType.OVERCOUNTED;
 
-        assertThat(mPowerUsageAdvanced.shouldHide(mPowerUsageData)).isTrue();
+        assertThat(mPowerUsageAdvanced.shouldHideCategory(mPowerUsageData)).isTrue();
     }
 
-
     @Test
-    public void testShouldHide_typeNormal_returnFalse() {
+    public void testShouldHideCategory_typeNormal_returnFalse() {
         mPowerUsageData.usageType = UsageType.APP;
 
-        assertThat(mPowerUsageAdvanced.shouldHide(mPowerUsageData)).isFalse();
+        assertThat(mPowerUsageAdvanced.shouldHideCategory(mPowerUsageData)).isFalse();
     }
+
+    @Test
+    public void testShouldHideSummary_typeCell_returnTrue() {
+        mPowerUsageData.usageType = UsageType.CELL;
+
+        assertThat(mPowerUsageAdvanced.shouldHideSummary(mPowerUsageData)).isTrue();
+    }
+
+    @Test
+    public void testShouldHideSummary_typeNormal_returnFalse() {
+        mPowerUsageData.usageType = UsageType.APP;
+
+        assertThat(mPowerUsageAdvanced.shouldHideSummary(mPowerUsageData)).isFalse();
+    }
+
 }