Add entry point to launch media output slice

Add media output panel type to launch media output slice.

Bug: 121083246
Test: make -j RunSettingsRoboTests
Change-Id: Ibf706146430e309fef6cbf0e1e86c2d5b78b50d5
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index a8101f1..08c4985 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -2983,6 +2983,17 @@
                 </intent-filter>
         </activity>
 
+        <activity-alias
+            android:name="MediaOutputSlice"
+            android:label="@string/media_output_panel_title"
+            android:permission="android.permission.BLUETOOTH_PRIVILEGED"
+            android:targetActivity=".panel.SettingsPanelActivity">
+                <intent-filter>
+                    <action android:name="com.android.settings.panel.action.MEDIA_OUTPUT" />
+                    <category android:name="android.intent.category.DEFAULT" />
+                </intent-filter>
+        </activity-alias>
+
         <provider android:name=".slices.SettingsSliceProvider"
                   android:authorities="com.android.settings.slices;android.settings.slices"
                   android:exported="true"
diff --git a/src/com/android/settings/panel/MediaOutputPanel.java b/src/com/android/settings/panel/MediaOutputPanel.java
new file mode 100644
index 0000000..f7639d9
--- /dev/null
+++ b/src/com/android/settings/panel/MediaOutputPanel.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2019 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.panel;
+
+import static com.android.settings.media.MediaOutputSlice.MEDIA_PACKAGE_NAME;
+import static com.android.settings.slices.CustomSliceRegistry.MEDIA_OUTPUT_SLICE_URI;
+
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+
+import com.android.settings.R;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Represents the Media output Panel.
+ *
+ * <p>
+ * Displays Media output item
+ * </p>
+ */
+public class MediaOutputPanel implements PanelContent {
+
+    private final Context mContext;
+    private final String mPackageName;
+
+    public static MediaOutputPanel create(Context context, String packageName) {
+        return new MediaOutputPanel(context, packageName);
+    }
+
+    private MediaOutputPanel(Context context, String packageName) {
+        mContext = context.getApplicationContext();
+        mPackageName = packageName;
+    }
+
+    @Override
+    public CharSequence getTitle() {
+        return mContext.getText(R.string.media_output_panel_title);
+    }
+
+    @Override
+    public List<Uri> getSlices() {
+        final List<Uri> uris = new ArrayList<>();
+        MEDIA_OUTPUT_SLICE_URI =
+                MEDIA_OUTPUT_SLICE_URI
+                        .buildUpon()
+                        .clearQuery()
+                        .appendQueryParameter(MEDIA_PACKAGE_NAME, mPackageName)
+                        .build();
+        uris.add(MEDIA_OUTPUT_SLICE_URI);
+        return uris;
+    }
+
+    @Override
+    public Intent getSeeMoreIntent() {
+        return null;
+    }
+}
diff --git a/src/com/android/settings/panel/PanelFeatureProvider.java b/src/com/android/settings/panel/PanelFeatureProvider.java
index 7d6c558..5af5ac8 100644
--- a/src/com/android/settings/panel/PanelFeatureProvider.java
+++ b/src/com/android/settings/panel/PanelFeatureProvider.java
@@ -21,7 +21,7 @@
 public interface PanelFeatureProvider {
 
     /**
-     * Returns {@link PanelContent} as specified by the {@param panelType}.
+     * Returns {@link PanelContent} as specified by the {@code panelType} and {@code packageName}.
      */
-    PanelContent getPanel(Context context, String panelType);
+    PanelContent getPanel(Context context, String panelType, String packageName);
 }
diff --git a/src/com/android/settings/panel/PanelFeatureProviderImpl.java b/src/com/android/settings/panel/PanelFeatureProviderImpl.java
index b4c37bf..c3d611d 100644
--- a/src/com/android/settings/panel/PanelFeatureProviderImpl.java
+++ b/src/com/android/settings/panel/PanelFeatureProviderImpl.java
@@ -16,13 +16,15 @@
 
 package com.android.settings.panel;
 
+import static com.android.settingslib.media.MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT;
+
 import android.content.Context;
 import android.provider.Settings;
 
 public class PanelFeatureProviderImpl implements PanelFeatureProvider {
 
     @Override
-    public PanelContent getPanel(Context context, String panelType) {
+    public PanelContent getPanel(Context context, String panelType, String packageName) {
         switch (panelType) {
             case Settings.Panel.ACTION_INTERNET_CONNECTIVITY:
                 return InternetConnectivityPanel.create(context);
@@ -30,6 +32,8 @@
                 return VolumePanel.create(context);
             case Settings.Panel.ACTION_NFC:
                 return NfcPanel.create(context);
+            case ACTION_MEDIA_OUTPUT:
+                return MediaOutputPanel.create(context, packageName);
         }
 
         throw new IllegalStateException("No matching panel for: "  + panelType);
diff --git a/src/com/android/settings/panel/PanelFragment.java b/src/com/android/settings/panel/PanelFragment.java
index 3d302fc..db0bf0e 100644
--- a/src/com/android/settings/panel/PanelFragment.java
+++ b/src/com/android/settings/panel/PanelFragment.java
@@ -70,10 +70,12 @@
 
         final Bundle arguments = getArguments();
         final String panelType = arguments.getString(SettingsPanelActivity.KEY_PANEL_TYPE_ARGUMENT);
+        final String packageName =
+                arguments.getString(SettingsPanelActivity.KEY_PANEL_PACKAGE_NAME);
 
         final PanelContent panel = FeatureFactory.getFactory(activity)
                 .getPanelFeatureProvider()
-                .getPanel(activity, panelType);
+                .getPanel(activity, panelType, packageName);
 
         mAdapter = new PanelSlicesAdapter(this, panel.getSlices());
 
@@ -86,6 +88,11 @@
         mSeeMoreButton.setOnClickListener(getSeeMoreListener(panel.getSeeMoreIntent()));
         mDoneButton.setOnClickListener(mDoneButtonListener);
 
+        //If getSeeMoreIntent() is null, hide the mSeeMoreButton.
+        if (panel.getSeeMoreIntent() == null) {
+            mSeeMoreButton.setVisibility(View.GONE);
+        }
+
         return view;
     }
 
diff --git a/src/com/android/settings/panel/SettingsPanelActivity.java b/src/com/android/settings/panel/SettingsPanelActivity.java
index 02e14e8..4cf535e 100644
--- a/src/com/android/settings/panel/SettingsPanelActivity.java
+++ b/src/com/android/settings/panel/SettingsPanelActivity.java
@@ -16,8 +16,12 @@
 
 package com.android.settings.panel;
 
+import static com.android.settingslib.media.MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT;
+import static com.android.settingslib.media.MediaOutputSliceConstants.EXTRA_PACKAGE_NAME;
+
 import android.content.Intent;
 import android.os.Bundle;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.Gravity;
 import android.view.Window;
@@ -28,6 +32,7 @@
 import androidx.fragment.app.FragmentActivity;
 import androidx.fragment.app.FragmentManager;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.settings.R;
 
 /**
@@ -37,10 +42,14 @@
 
     private final String TAG = "panel_activity";
 
+    @VisibleForTesting
+    final Bundle mBundle = new Bundle();
+
     /**
      * Key specifying which Panel the app is requesting.
      */
     public static final String KEY_PANEL_TYPE_ARGUMENT = "PANEL_TYPE_ARGUMENT";
+    public static final String KEY_PANEL_PACKAGE_NAME = "PANEL_PACKAGE_NAME";
 
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -53,6 +62,16 @@
             return;
         }
 
+        final String packageName =
+                callingIntent.getStringExtra(EXTRA_PACKAGE_NAME);
+
+        if (TextUtils.equals(ACTION_MEDIA_OUTPUT, callingIntent.getAction())
+                && TextUtils.isEmpty(packageName)) {
+            Log.e(TAG, "Null package name, closing Panel Activity");
+            finish();
+            return;
+        }
+
         setContentView(R.layout.settings_panel);
 
         // Move the window to the bottom of screen, and make it take up the entire screen width.
@@ -61,11 +80,11 @@
         window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                 WindowManager.LayoutParams.WRAP_CONTENT);
 
-        final Bundle bundle = new Bundle();
-        bundle.putString(KEY_PANEL_TYPE_ARGUMENT, callingIntent.getAction());
+        mBundle.putString(KEY_PANEL_TYPE_ARGUMENT, callingIntent.getAction());
+        mBundle.putString(KEY_PANEL_PACKAGE_NAME, packageName);
 
         final PanelFragment panelFragment = new PanelFragment();
-        panelFragment.setArguments(bundle);
+        panelFragment.setArguments(mBundle);
 
         final FragmentManager fragmentManager = getSupportFragmentManager();
         final Fragment fragment = fragmentManager.findFragmentById(R.id.main_content);
diff --git a/src/com/android/settings/slices/CustomSliceManager.java b/src/com/android/settings/slices/CustomSliceManager.java
index 8d07276..3786c5c 100644
--- a/src/com/android/settings/slices/CustomSliceManager.java
+++ b/src/com/android/settings/slices/CustomSliceManager.java
@@ -18,6 +18,7 @@
 
 import android.content.Context;
 import android.net.Uri;
+import android.text.TextUtils;
 import android.util.ArrayMap;
 
 import androidx.annotation.VisibleForTesting;
@@ -33,6 +34,7 @@
 import com.android.settings.homepage.contextualcards.slices.LowStorageSlice;
 import com.android.settings.homepage.contextualcards.slices.NotificationChannelSlice;
 import com.android.settings.location.LocationSlice;
+import com.android.settings.media.MediaOutputSlice;
 import com.android.settings.wifi.slice.ContextualWifiSlice;
 import com.android.settings.wifi.slice.WifiSlice;
 
@@ -65,20 +67,25 @@
      * the only thing that should be needed to create the object.
      */
     public CustomSliceable getSliceableFromUri(Uri uri) {
-        if (mSliceableCache.containsKey(uri)) {
-            return mSliceableCache.get(uri);
+        final Uri newUri = removeParameterFromUri(uri);
+        if (mSliceableCache.containsKey(newUri)) {
+            return mSliceableCache.get(newUri);
         }
 
-        final Class clazz = mUriMap.get(uri);
+        final Class clazz = mUriMap.get(newUri);
         if (clazz == null) {
             throw new IllegalArgumentException("No Slice found for uri: " + uri);
         }
 
         final CustomSliceable sliceable = CustomSliceable.createInstance(mContext, clazz);
-        mSliceableCache.put(uri, sliceable);
+        mSliceableCache.put(newUri, sliceable);
         return sliceable;
     }
 
+    private Uri removeParameterFromUri(Uri uri) {
+        return uri != null ? uri.buildUpon().clearQuery().build() : null;
+    }
+
     /**
      * Return a {@link CustomSliceable} associated to the Action.
      * <p>
@@ -94,7 +101,7 @@
      * {@link CustomSliceManager}.
      */
     public boolean isValidUri(Uri uri) {
-        return mUriMap.containsKey(uri);
+        return mUriMap.containsKey(removeParameterFromUri(uri));
     }
 
     /**
@@ -120,5 +127,6 @@
                 NotificationChannelSlice.class);
         mUriMap.put(CustomSliceRegistry.STORAGE_SLICE_URI, StorageSlice.class);
         mUriMap.put(CustomSliceRegistry.WIFI_SLICE_URI, WifiSlice.class);
+        mUriMap.put(CustomSliceRegistry.MEDIA_OUTPUT_SLICE_URI, MediaOutputSlice.class);
     }
 }
diff --git a/src/com/android/settings/slices/CustomSliceRegistry.java b/src/com/android/settings/slices/CustomSliceRegistry.java
index 3a213df..66e85c0 100644
--- a/src/com/android/settings/slices/CustomSliceRegistry.java
+++ b/src/com/android/settings/slices/CustomSliceRegistry.java
@@ -27,6 +27,7 @@
 
 import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController;
 import com.android.settings.wifi.calling.WifiCallingSliceHelper;
+import com.android.settingslib.media.MediaOutputSliceConstants;
 
 /**
  * A registry of custom slice Uris.
@@ -255,4 +256,14 @@
             .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
             .appendPath(ZEN_MODE_KEY)
             .build();
+
+    /**
+     * Backing Uri for the Media output Slice.
+     */
+    public static Uri MEDIA_OUTPUT_SLICE_URI = new Uri.Builder()
+            .scheme(ContentResolver.SCHEME_CONTENT)
+            .authority(SettingsSliceProvider.SLICE_AUTHORITY)
+            .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
+            .appendPath(MediaOutputSliceConstants.KEY_MEDIA_OUTPUT)
+            .build();
 }
diff --git a/tests/robotests/src/com/android/settings/panel/MediaOutputPanelTest.java b/tests/robotests/src/com/android/settings/panel/MediaOutputPanelTest.java
new file mode 100644
index 0000000..b411037
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/panel/MediaOutputPanelTest.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2019 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.panel;
+
+import static com.android.settings.media.MediaOutputSlice.MEDIA_PACKAGE_NAME;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.net.Uri;
+
+import com.android.settings.slices.CustomSliceRegistry;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+import java.util.List;
+
+@RunWith(RobolectricTestRunner.class)
+public class MediaOutputPanelTest {
+
+    private static final String TEST_PACKAGENAME = "com.test.packagename";
+
+    private MediaOutputPanel mPanel;
+
+    @Before
+    public void setUp() {
+        mPanel = MediaOutputPanel.create(RuntimeEnvironment.application, TEST_PACKAGENAME);
+    }
+
+    @Test
+    public void getSlices_containsNecessarySlices() {
+        final List<Uri> uris = mPanel.getSlices();
+
+        assertThat(uris).containsExactly(CustomSliceRegistry.MEDIA_OUTPUT_SLICE_URI);
+    }
+
+    @Test
+    public void getSlices_verifyPackageName_isEqual() {
+        final List<Uri> uris = mPanel.getSlices();
+
+        assertThat(uris.get(0).getQueryParameter(MEDIA_PACKAGE_NAME)).isEqualTo(TEST_PACKAGENAME);
+    }
+
+    @Test
+    public void getSeeMoreIntent_isNull() {
+        assertThat(mPanel.getSeeMoreIntent()).isNull();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/panel/PanelFeatureProviderImplTest.java b/tests/robotests/src/com/android/settings/panel/PanelFeatureProviderImplTest.java
index 99d5d6c..ae57a77 100644
--- a/tests/robotests/src/com/android/settings/panel/PanelFeatureProviderImplTest.java
+++ b/tests/robotests/src/com/android/settings/panel/PanelFeatureProviderImplTest.java
@@ -17,6 +17,8 @@
 
 package com.android.settings.panel;
 
+import static com.android.settingslib.media.MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT;
+
 import static com.google.common.truth.Truth.assertThat;
 
 import android.content.Context;
@@ -32,6 +34,8 @@
 @RunWith(RobolectricTestRunner.class)
 public class PanelFeatureProviderImplTest {
 
+    private static final String TEST_PACKAGENAME = "com.test.packagename";
+
     private Context mContext;
     private PanelFeatureProviderImpl mProvider;
 
@@ -44,7 +48,7 @@
     @Test
     public void getPanel_internetConnectivityKey_returnsCorrectPanel() {
         final PanelContent panel = mProvider.getPanel(mContext,
-                Settings.Panel.ACTION_INTERNET_CONNECTIVITY);
+                Settings.Panel.ACTION_INTERNET_CONNECTIVITY, TEST_PACKAGENAME);
 
         assertThat(panel).isInstanceOf(InternetConnectivityPanel.class);
     }
@@ -52,8 +56,16 @@
     @Test
     public void getPanel_volume_returnsCorrectPanel() {
         final PanelContent panel = mProvider.getPanel(mContext,
-                Settings.Panel.ACTION_VOLUME);
+                Settings.Panel.ACTION_VOLUME, TEST_PACKAGENAME);
 
         assertThat(panel).isInstanceOf(VolumePanel.class);
     }
+
+    @Test
+    public void getPanel_mediaOutputKey_returnsCorrectPanel() {
+        final PanelContent panel = mProvider.getPanel(mContext,
+                ACTION_MEDIA_OUTPUT, TEST_PACKAGENAME);
+
+        assertThat(panel).isInstanceOf(MediaOutputPanel.class);
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/panel/PanelFragmentTest.java b/tests/robotests/src/com/android/settings/panel/PanelFragmentTest.java
index 73318f2..389c31e 100644
--- a/tests/robotests/src/com/android/settings/panel/PanelFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/panel/PanelFragmentTest.java
@@ -59,7 +59,7 @@
         mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
         mFakeFeatureFactory.panelFeatureProvider = mPanelFeatureProvider;
         mFakePanelContent = new FakePanelContent();
-        doReturn(mFakePanelContent).when(mPanelFeatureProvider).getPanel(any(), any());
+        doReturn(mFakePanelContent).when(mPanelFeatureProvider).getPanel(any(), any(), any());
 
         ActivityController<FakeSettingsPanelActivity> activityController =
                 Robolectric.buildActivity(FakeSettingsPanelActivity.class);
diff --git a/tests/robotests/src/com/android/settings/panel/PanelSlicesAdapterTest.java b/tests/robotests/src/com/android/settings/panel/PanelSlicesAdapterTest.java
index 7648d23..abefa67 100644
--- a/tests/robotests/src/com/android/settings/panel/PanelSlicesAdapterTest.java
+++ b/tests/robotests/src/com/android/settings/panel/PanelSlicesAdapterTest.java
@@ -58,7 +58,7 @@
         mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
         mFakeFeatureFactory.panelFeatureProvider = mPanelFeatureProvider;
         mFakePanelContent = new FakePanelContent();
-        doReturn(mFakePanelContent).when(mPanelFeatureProvider).getPanel(any(), any());
+        doReturn(mFakePanelContent).when(mPanelFeatureProvider).getPanel(any(), any(), any());
 
         ActivityController<FakeSettingsPanelActivity> activityController =
                 Robolectric.buildActivity(FakeSettingsPanelActivity.class);
diff --git a/tests/robotests/src/com/android/settings/panel/SettingsPanelActivityTest.java b/tests/robotests/src/com/android/settings/panel/SettingsPanelActivityTest.java
new file mode 100644
index 0000000..359cf5d
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/panel/SettingsPanelActivityTest.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2019 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.panel;
+
+import static com.android.settings.panel.SettingsPanelActivity.KEY_PANEL_PACKAGE_NAME;
+import static com.android.settings.panel.SettingsPanelActivity.KEY_PANEL_TYPE_ARGUMENT;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Intent;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+
+@RunWith(RobolectricTestRunner.class)
+public class SettingsPanelActivityTest {
+
+    @Test
+    public void startMediaOutputSlice_withPackageName_bundleShouldHaveValue() {
+        final Intent intent = new Intent()
+                .setAction("com.android.settings.panel.action.MEDIA_OUTPUT")
+                .putExtra("com.android.settings.panel.extra.PACKAGE_NAME",
+                        "com.google.android.music");
+
+        final SettingsPanelActivity activity =
+                Robolectric.buildActivity(SettingsPanelActivity.class, intent).create().get();
+
+        assertThat(activity.mBundle.getString(KEY_PANEL_PACKAGE_NAME))
+                .isEqualTo("com.google.android.music");
+        assertThat(activity.mBundle.getString(KEY_PANEL_TYPE_ARGUMENT))
+                .isEqualTo("com.android.settings.panel.action.MEDIA_OUTPUT");
+    }
+
+    @Test
+    public void startMediaOutputSlice_withoutPackageName_bundleShouldNotHaveValue() {
+        final Intent intent = new Intent()
+                .setAction("com.android.settings.panel.action.MEDIA_OUTPUT");
+
+        final SettingsPanelActivity activity =
+                Robolectric.buildActivity(SettingsPanelActivity.class, intent).create().get();
+
+        assertThat(activity.mBundle.containsKey(KEY_PANEL_PACKAGE_NAME)).isFalse();
+        assertThat(activity.mBundle.containsKey(KEY_PANEL_TYPE_ARGUMENT)).isFalse();
+    }
+}