Merge "Cutout emulation: string changes and ordering" into pi-dev
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index fb74bf9..2c5f846 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -3314,10 +3314,15 @@
         </provider>
 
         <receiver
-            android:name=".slices.SliceBroadcastReceiver" >
-            <intent-filter>
-                <action android:name="com.android.settings.slice.action.WIFI_CHANGED"/>
-            </intent-filter>
+            android:name=".slices.SliceBroadcastReceiver"
+            android:exported="false">
+        </receiver>
+
+        <receiver
+            android:name=".slices.SliceRelayReceiver"
+            android:permission="android.permission.MANAGE_SLICE_PERMISSIONS"
+            android:exported="true">
+
         </receiver>
 
         <!-- Couldn't be triggered from outside of settings. Statsd can trigger it because we send
diff --git a/src/com/android/settings/fuelgauge/BatteryUtils.java b/src/com/android/settings/fuelgauge/BatteryUtils.java
index c62af8f..2dc3ed2 100644
--- a/src/com/android/settings/fuelgauge/BatteryUtils.java
+++ b/src/com/android/settings/fuelgauge/BatteryUtils.java
@@ -24,8 +24,8 @@
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.os.BatteryStats;
-import android.os.Bundle;
 import android.os.Build;
+import android.os.Bundle;
 import android.os.Process;
 import android.os.SystemClock;
 import android.os.UserHandle;
@@ -35,7 +35,6 @@
 import android.support.annotation.StringRes;
 import android.support.annotation.VisibleForTesting;
 import android.support.annotation.WorkerThread;
-import android.text.TextUtils;
 import android.text.format.DateUtils;
 import android.util.Log;
 import android.util.SparseLongArray;
@@ -48,7 +47,6 @@
 import com.android.settings.fuelgauge.batterytip.AnomalyInfo;
 import com.android.settings.fuelgauge.batterytip.StatsManagerConfig;
 import com.android.settings.overlay.FeatureFactory;
-
 import com.android.settingslib.fuelgauge.PowerWhitelistBackend;
 import com.android.settingslib.utils.PowerUtil;
 
@@ -93,14 +91,14 @@
 
     public static BatteryUtils getInstance(Context context) {
         if (sInstance == null || sInstance.isDataCorrupted()) {
-            sInstance = new BatteryUtils(context);
+            sInstance = new BatteryUtils(context.getApplicationContext());
         }
         return sInstance;
     }
 
     @VisibleForTesting
     BatteryUtils(Context context) {
-        mContext = context.getApplicationContext();
+        mContext = context;
         mPackageManager = context.getPackageManager();
         mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
         mPowerUsageFeatureProvider = FeatureFactory.getFactory(
diff --git a/src/com/android/settings/overlay/FeatureFactoryImpl.java b/src/com/android/settings/overlay/FeatureFactoryImpl.java
index d058b3b..0f7d58c 100644
--- a/src/com/android/settings/overlay/FeatureFactoryImpl.java
+++ b/src/com/android/settings/overlay/FeatureFactoryImpl.java
@@ -94,7 +94,8 @@
     @Override
     public PowerUsageFeatureProvider getPowerUsageFeatureProvider(Context context) {
         if (mPowerUsageFeatureProvider == null) {
-            mPowerUsageFeatureProvider = new PowerUsageFeatureProviderImpl(context);
+            mPowerUsageFeatureProvider = new PowerUsageFeatureProviderImpl(
+                    context.getApplicationContext());
         }
         return mPowerUsageFeatureProvider;
     }
@@ -102,7 +103,8 @@
     @Override
     public DashboardFeatureProvider getDashboardFeatureProvider(Context context) {
         if (mDashboardFeatureProvider == null) {
-            mDashboardFeatureProvider = new DashboardFeatureProviderImpl(context);
+            mDashboardFeatureProvider = new DashboardFeatureProviderImpl(
+                    context.getApplicationContext());
         }
         return mDashboardFeatureProvider;
     }
@@ -118,10 +120,11 @@
     @Override
     public ApplicationFeatureProvider getApplicationFeatureProvider(Context context) {
         if (mApplicationFeatureProvider == null) {
-            mApplicationFeatureProvider = new ApplicationFeatureProviderImpl(context,
-                    new PackageManagerWrapper(context.getPackageManager()),
+            final Context appContext = context.getApplicationContext();
+            mApplicationFeatureProvider = new ApplicationFeatureProviderImpl(appContext,
+                    new PackageManagerWrapper(appContext.getPackageManager()),
                     AppGlobals.getPackageManager(),
-                    (DevicePolicyManager) context
+                    (DevicePolicyManager) appContext
                             .getSystemService(Context.DEVICE_POLICY_SERVICE));
         }
         return mApplicationFeatureProvider;
@@ -138,12 +141,14 @@
     @Override
     public EnterprisePrivacyFeatureProvider getEnterprisePrivacyFeatureProvider(Context context) {
         if (mEnterprisePrivacyFeatureProvider == null) {
-            mEnterprisePrivacyFeatureProvider = new EnterprisePrivacyFeatureProviderImpl(context,
-                    (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE),
-                    new PackageManagerWrapper(context.getPackageManager()),
-                    UserManager.get(context),
-                    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE),
-                    context.getResources());
+            final Context appContext = context.getApplicationContext();
+            mEnterprisePrivacyFeatureProvider = new EnterprisePrivacyFeatureProviderImpl(appContext,
+                    (DevicePolicyManager) appContext
+                            .getSystemService(Context.DEVICE_POLICY_SERVICE),
+                    new PackageManagerWrapper(appContext.getPackageManager()),
+                    UserManager.get(appContext),
+                    (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE),
+                    appContext.getResources());
         }
         return mEnterprisePrivacyFeatureProvider;
     }
@@ -172,7 +177,8 @@
     @Override
     public SuggestionFeatureProvider getSuggestionFeatureProvider(Context context) {
         if (mSuggestionFeatureProvider == null) {
-            mSuggestionFeatureProvider = new SuggestionFeatureProviderImpl(context);
+            mSuggestionFeatureProvider = new SuggestionFeatureProviderImpl(
+                    context.getApplicationContext());
         }
         return mSuggestionFeatureProvider;
     }
@@ -180,7 +186,7 @@
     @Override
     public UserFeatureProvider getUserFeatureProvider(Context context) {
         if (mUserFeatureProvider == null) {
-            mUserFeatureProvider = new UserFeatureProviderImpl(context);
+            mUserFeatureProvider = new UserFeatureProviderImpl(context.getApplicationContext());
         }
         return mUserFeatureProvider;
     }
diff --git a/src/com/android/settings/slices/SettingsSliceProvider.java b/src/com/android/settings/slices/SettingsSliceProvider.java
index 841247b..3b5e72e 100644
--- a/src/com/android/settings/slices/SettingsSliceProvider.java
+++ b/src/com/android/settings/slices/SettingsSliceProvider.java
@@ -382,7 +382,7 @@
     void registerIntentToUri(IntentFilter intentFilter, Uri sliceUri) {
         Log.d(TAG, "Registering Uri for broadcast relay: " + sliceUri);
         mRegisteredUris.add(sliceUri);
-        SliceBroadcastRelay.registerReceiver(getContext(), sliceUri, SliceBroadcastReceiver.class,
+        SliceBroadcastRelay.registerReceiver(getContext(), sliceUri, SliceRelayReceiver.class,
                 intentFilter);
     }
 
diff --git a/src/com/android/settings/slices/SliceBroadcastReceiver.java b/src/com/android/settings/slices/SliceBroadcastReceiver.java
index d81734a..53fa4be 100644
--- a/src/com/android/settings/slices/SliceBroadcastReceiver.java
+++ b/src/com/android/settings/slices/SliceBroadcastReceiver.java
@@ -44,7 +44,6 @@
 import com.android.settings.notification.ZenModeSliceBuilder;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.wifi.WifiSliceBuilder;
-import com.android.settingslib.SliceBroadcastRelay;
 
 /**
  * Responds to actions performed on slices and notifies slices of updates in state changes.
@@ -84,12 +83,6 @@
             case ACTION_ZEN_MODE_SLICE_CHANGED:
                 ZenModeSliceBuilder.handleUriChange(context, intent);
                 break;
-            default:
-                final String uriString = intent.getStringExtra(SliceBroadcastRelay.EXTRA_URI);
-                if (!TextUtils.isEmpty(uriString)) {
-                    final Uri uri = Uri.parse(uriString);
-                    context.getContentResolver().notifyChange(uri, null /* observer */);
-                }
         }
     }
 
diff --git a/src/com/android/settings/slices/SliceRelayReceiver.java b/src/com/android/settings/slices/SliceRelayReceiver.java
new file mode 100644
index 0000000..c8ec12e
--- /dev/null
+++ b/src/com/android/settings/slices/SliceRelayReceiver.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2018 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.slices;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.text.TextUtils;
+import com.android.settingslib.SliceBroadcastRelay;
+
+/**
+ * Receives broadcasts to notify that Settings Slices are potentially stale.
+ */
+public class SliceRelayReceiver extends BroadcastReceiver {
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        final String uriString = intent.getStringExtra(SliceBroadcastRelay.EXTRA_URI);
+        if (!TextUtils.isEmpty(uriString)) {
+            final Uri uri = Uri.parse(uriString);
+            context.getContentResolver().notifyChange(uri, null /* observer */);
+        }
+    }
+}
\ No newline at end of file
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceControllerTest.java
index 389b714..8bbb334 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BackgroundActivityPreferenceControllerTest.java
@@ -17,6 +17,7 @@
 package com.android.settings.fuelgauge;
 
 import static com.google.common.truth.Truth.assertThat;
+
 import static org.mockito.Matchers.anyInt;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.doNothing;
@@ -89,6 +90,7 @@
 
         mShadowContext = RuntimeEnvironment.application;
         FakeFeatureFactory.setupForTest();
+        when(mContext.getApplicationContext()).thenReturn(mContext);
         when(mContext.getPackageManager()).thenReturn(mPackageManager);
         when(mContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mAppOpsManager);
         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
@@ -118,7 +120,7 @@
     @Test
     public void testHandlePreferenceTreeClick_restrictApp_showDialog() {
         doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager)
-            .checkOpNoThrow(anyInt(), anyInt(), anyString());
+                .checkOpNoThrow(anyInt(), anyInt(), anyString());
 
         mController.handlePreferenceTreeClick(mPreference);
 
@@ -128,7 +130,7 @@
     @Test
     public void testHandlePreferenceTreeClick_unRestrictApp_showDialog() {
         doReturn(AppOpsManager.MODE_IGNORED).when(mAppOpsManager)
-            .checkOpNoThrow(anyInt(), anyInt(), anyString());
+                .checkOpNoThrow(anyInt(), anyInt(), anyString());
 
         mController.handlePreferenceTreeClick(mPreference);
 
diff --git a/tests/robotests/src/com/android/settings/slices/SliceBroadcastReceiverTest.java b/tests/robotests/src/com/android/settings/slices/SliceBroadcastReceiverTest.java
index 25f0d0c..f8172e8 100644
--- a/tests/robotests/src/com/android/settings/slices/SliceBroadcastReceiverTest.java
+++ b/tests/robotests/src/com/android/settings/slices/SliceBroadcastReceiverTest.java
@@ -159,8 +159,12 @@
 
         assertThat(fakeToggleController.isChecked()).isFalse();
 
-        final Uri expectedUri = SliceBuilderUtils.getUri(
-                SettingsSlicesContract.PATH_SETTING_ACTION + "/" + key, false);
+        final Uri expectedUri = new Uri.Builder()
+                .scheme(ContentResolver.SCHEME_CONTENT)
+                .authority(SettingsSliceProvider.SLICE_AUTHORITY)
+                .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
+                .appendPath(key)
+                .build();
         verify(resolver).notifyChange(eq(expectedUri), eq(null));
     }
 
diff --git a/tests/robotests/src/com/android/settings/slices/SliceRelayReceiverTest.java b/tests/robotests/src/com/android/settings/slices/SliceRelayReceiverTest.java
new file mode 100644
index 0000000..3388a16
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/slices/SliceRelayReceiverTest.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2018 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.slices;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import android.content.ContentResolver;
+import android.content.Context;
+
+import android.content.Intent;
+import android.net.Uri;
+import android.provider.SettingsSlicesContract;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settingslib.SliceBroadcastRelay;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+public class SliceRelayReceiverTest {
+
+  private Context mContext;
+  private SliceRelayReceiver mSliceRelayReceiver;
+
+  @Before
+  public void setUp() {
+      mContext = spy(RuntimeEnvironment.application);
+      mSliceRelayReceiver = new SliceRelayReceiver();
+  }
+
+
+  @Test
+  public void onReceive_extraUri_notifiesChangeOnUri() {
+      // Monitor the ContentResolver
+      final ContentResolver resolver = spy(mContext.getContentResolver());
+      doReturn(resolver).when(mContext).getContentResolver();
+
+      final Uri uri = new Uri.Builder()
+          .scheme(ContentResolver.SCHEME_CONTENT)
+          .authority(SettingsSlicesContract.AUTHORITY)
+          .appendPath("path")
+          .build();
+
+      final Intent intent = new Intent();
+    intent.putExtra(SliceBroadcastRelay.EXTRA_URI, uri.toString());
+
+    mSliceRelayReceiver.onReceive(mContext, intent);
+
+    verify(resolver).notifyChange(eq(uri), any());
+
+  }
+}