Uniquely identify Slice intents

PendingIntents were being cached because they had the same
targets and would not differentiate on extras - thus all Slice
intents would go to the same destination as the first intent fired.

Adding the data stops the system from caching the intents.

Change-Id: Ifccab72ed482e22750422c5c36aa6d205c20ae3d
Fixes: 77650727
Test: robotests
diff --git a/src/com/android/settings/slices/SliceBuilderUtils.java b/src/com/android/settings/slices/SliceBuilderUtils.java
index 05f0974..a2479a2 100644
--- a/src/com/android/settings/slices/SliceBuilderUtils.java
+++ b/src/com/android/settings/slices/SliceBuilderUtils.java
@@ -51,7 +51,6 @@
 import android.support.v4.graphics.drawable.IconCompat;
 
 import androidx.slice.Slice;
-import androidx.slice.SliceMetadata;
 import androidx.slice.builders.ListBuilder;
 import androidx.slice.builders.SliceAction;
 
@@ -154,7 +153,7 @@
      * @return {@link PendingIntent} for a non-primary {@link SliceAction}.
      */
     public static PendingIntent getActionIntent(Context context, String action, SliceData data) {
-        Intent intent = new Intent(action);
+        final Intent intent = new Intent(action);
         intent.setClass(context, SliceBroadcastReceiver.class);
         intent.putExtra(EXTRA_SLICE_KEY, data.getKey());
         intent.putExtra(EXTRA_SLICE_PLATFORM_DEFINED, data.isPlatformDefined());
@@ -165,11 +164,8 @@
     /**
      * @return {@link PendingIntent} for the primary {@link SliceAction}.
      */
-    public static PendingIntent getContentIntent(Context context, SliceData sliceData) {
-        Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(context,
-                sliceData.getFragmentClassName(), sliceData.getKey(), sliceData.getScreenTitle(),
-                0 /* TODO */);
-        intent.setClassName("com.android.settings", SubSettings.class.getName());
+    public static PendingIntent getContentPendingIntent(Context context, SliceData sliceData) {
+        final Intent intent = getContentIntent(context, sliceData);
         return PendingIntent.getActivity(context, 0 /* requestCode */, intent, 0 /* flags */);
     }
 
@@ -215,9 +211,20 @@
                 .build();
     }
 
+    @VisibleForTesting
+    static Intent getContentIntent(Context context, SliceData sliceData) {
+        final Uri contentUri = new Uri.Builder().appendPath(sliceData.getKey()).build();
+        final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(context,
+                sliceData.getFragmentClassName(), sliceData.getKey(), sliceData.getScreenTitle(),
+                0 /* TODO */);
+        intent.setClassName(context.getPackageName(), SubSettings.class.getName());
+        intent.setData(contentUri);
+        return intent;
+    }
+
     private static Slice buildToggleSlice(Context context, SliceData sliceData,
             BasePreferenceController controller) {
-        final PendingIntent contentIntent = getContentIntent(context, sliceData);
+        final PendingIntent contentIntent = getContentPendingIntent(context, sliceData);
         final Icon icon = Icon.createWithResource(context, sliceData.getIconResource());
         final CharSequence subtitleText = getSubtitleText(context, controller, sliceData);
         final TogglePreferenceController toggleController =
@@ -237,7 +244,7 @@
 
     private static Slice buildIntentSlice(Context context, SliceData sliceData,
             BasePreferenceController controller) {
-        final PendingIntent contentIntent = getContentIntent(context, sliceData);
+        final PendingIntent contentIntent = getContentPendingIntent(context, sliceData);
         final Icon icon = Icon.createWithResource(context, sliceData.getIconResource());
         final CharSequence subtitleText = getSubtitleText(context, controller, sliceData);
 
@@ -314,12 +321,12 @@
                 break;
             case DISABLED_FOR_USER:
                 summary = context.getString(R.string.disabled_for_user_setting_summary);
-                primaryAction = new SliceAction(getContentIntent(context, data),
+                primaryAction = new SliceAction(getContentPendingIntent(context, data),
                         (IconCompat) null /* actionIcon */, null /* actionTitle */);
                 break;
             case DISABLED_DEPENDENT_SETTING:
                 summary = context.getString(R.string.disabled_dependent_setting_summary);
-                primaryAction = new SliceAction(getContentIntent(context, data),
+                primaryAction = new SliceAction(getContentPendingIntent(context, data),
                         (IconCompat) null /* actionIcon */, null /* actionTitle */);
                 break;
             case UNAVAILABLE_UNKNOWN:
diff --git a/tests/robotests/src/com/android/settings/slices/SliceBuilderUtilsTest.java b/tests/robotests/src/com/android/settings/slices/SliceBuilderUtilsTest.java
index 58ee969..59eb7ce 100644
--- a/tests/robotests/src/com/android/settings/slices/SliceBuilderUtilsTest.java
+++ b/tests/robotests/src/com/android/settings/slices/SliceBuilderUtilsTest.java
@@ -24,8 +24,10 @@
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 
+import android.app.PendingIntent;
 import android.content.ContentResolver;
 import android.content.Context;
+import android.content.Intent;
 import android.content.res.Resources;
 import android.net.Uri;
 import android.provider.Settings;
@@ -389,6 +391,17 @@
         SliceTester.testSettingsUnavailableSlice(mContext, slice, data);
     }
 
+    @Test
+    public void testContentIntent_includesUniqueData() {
+        final SliceData sliceData = getDummyData();
+        final Uri expectedUri = new Uri.Builder().appendPath(sliceData.getKey()).build();
+
+        final Intent intent = SliceBuilderUtils.getContentIntent(mContext, sliceData);
+        final Uri intentData = intent.getData();
+
+        assertThat(intentData).isEqualTo(expectedUri);
+    }
+
     private SliceData getDummyData() {
         return getDummyData(TOGGLE_CONTROLLER, SUMMARY, SliceData.SliceType.SWITCH);
     }
diff --git a/tests/robotests/src/com/android/settings/testutils/SliceTester.java b/tests/robotests/src/com/android/settings/testutils/SliceTester.java
index ef62d77..905277e 100644
--- a/tests/robotests/src/com/android/settings/testutils/SliceTester.java
+++ b/tests/robotests/src/com/android/settings/testutils/SliceTester.java
@@ -64,7 +64,7 @@
 
         final PendingIntent primaryPendingIntent = metadata.getPrimaryAction().getAction();
         assertThat(primaryPendingIntent).isEqualTo(
-                SliceBuilderUtils.getContentIntent(context, sliceData));
+                SliceBuilderUtils.getContentPendingIntent(context, sliceData));
 
         final List<SliceItem> sliceItems = slice.getItems();
         assertTitle(sliceItems, sliceData.getTitle());
@@ -97,7 +97,7 @@
         // Check primary intent
         final PendingIntent primaryPendingIntent = metadata.getPrimaryAction().getAction();
         assertThat(primaryPendingIntent).isEqualTo(
-                SliceBuilderUtils.getContentIntent(context, sliceData));
+                SliceBuilderUtils.getContentPendingIntent(context, sliceData));
 
         final List<SliceItem> sliceItems = slice.getItems();
         assertTitle(sliceItems, sliceData.getTitle());
@@ -147,7 +147,7 @@
             case DISABLED_FOR_USER:
             case DISABLED_DEPENDENT_SETTING:
                 assertThat(primaryPendingIntent).isEqualTo(
-                        SliceBuilderUtils.getContentIntent(context, sliceData));
+                        SliceBuilderUtils.getContentPendingIntent(context, sliceData));
                 break;
         }