Add long press menu to remove media player

Screenshots: https://drive.google.com/drive/folders/1oaE6oFqShIhmcnG6P7CJFhgVenFgYNgm?usp=sharing

Fixes: 150742919
Test: manual
Change-Id: I36b61c62443bfee3ce6f13cccea9564460563918
diff --git a/packages/SystemUI/res/drawable/ic_clear.xml b/packages/SystemUI/res/drawable/ic_clear.xml
new file mode 100644
index 0000000..5ee50d8
--- /dev/null
+++ b/packages/SystemUI/res/drawable/ic_clear.xml
@@ -0,0 +1,24 @@
+<!--
+  ~ Copyright (C) 2020 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="24.0dp"
+        android:height="24.0dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12 19,6.41z"
+        android:fillColor="#FF000000"/>
+</vector>
diff --git a/packages/SystemUI/res/layout/qs_media_panel.xml b/packages/SystemUI/res/layout/qs_media_panel.xml
index 34bd703..9ef8c1d 100644
--- a/packages/SystemUI/res/layout/qs_media_panel.xml
+++ b/packages/SystemUI/res/layout/qs_media_panel.xml
@@ -27,163 +27,174 @@
     android:background="@drawable/qs_media_background"
     >
 
-    <!-- Header section -->
+    <!-- Buttons to remove this view when no longer needed -->
+    <include
+        layout="@layout/qs_media_panel_options"
+        android:visibility="gone"/>
+
     <LinearLayout
-        android:orientation="horizontal"
+        android:id="@+id/media_guts"
+        android:orientation="vertical"
         android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:layout_marginBottom="16dp"
-    >
-
-        <ImageView
-            android:id="@+id/album_art"
-            android:layout_width="@dimen/qs_media_album_size"
-            android:layout_height="@dimen/qs_media_album_size"
-            android:layout_marginRight="16dp"
-            android:layout_weight="0"
-        />
-
+        android:layout_height="match_parent">
+        <!-- Header section -->
         <LinearLayout
-            android:orientation="vertical"
-            android:layout_width="0dp"
-            android:layout_height="@dimen/qs_media_album_size"
-            android:layout_weight="1"
+            android:orientation="horizontal"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginBottom="16dp"
         >
+
+            <ImageView
+                android:id="@+id/album_art"
+                android:layout_width="@dimen/qs_media_album_size"
+                android:layout_height="@dimen/qs_media_album_size"
+                android:layout_marginRight="16dp"
+                android:layout_weight="0"
+            />
+
             <LinearLayout
-                android:orientation="horizontal"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:gravity="center"
+                android:orientation="vertical"
+                android:layout_width="0dp"
+                android:layout_height="@dimen/qs_media_album_size"
+                android:layout_weight="1"
             >
-                <com.android.internal.widget.CachingIconView
-                    android:id="@+id/icon"
-                    android:layout_width="16dp"
-                    android:layout_height="16dp"
-                    android:layout_marginEnd="5dp"
-                />
-                <TextView
-                    android:id="@+id/app_name"
+                <LinearLayout
+                    android:orientation="horizontal"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
+                    android:gravity="center"
+                >
+                    <com.android.internal.widget.CachingIconView
+                        android:id="@+id/icon"
+                        android:layout_width="16dp"
+                        android:layout_height="16dp"
+                        android:layout_marginEnd="5dp"
+                    />
+                    <TextView
+                        android:id="@+id/app_name"
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:textSize="14sp"
+                        android:singleLine="true"
+                    />
+                </LinearLayout>
+
+                <!-- Song name -->
+                <TextView
+                    android:id="@+id/header_title"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:singleLine="true"
+                    android:fontFamily="@*android:string/config_headlineFontFamilyMedium"
+                    android:textSize="18sp"
+                    android:paddingBottom="6dp"
+                    android:gravity="center"/>
+
+                <!-- Artist name -->
+                <TextView
+                    android:id="@+id/header_artist"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:fontFamily="@*android:string/config_bodyFontFamily"
                     android:textSize="14sp"
                     android:singleLine="true"
                 />
             </LinearLayout>
 
-            <!-- Song name -->
-            <TextView
-                android:id="@+id/header_title"
-                android:layout_width="wrap_content"
+            <!-- Output chip -->
+            <LinearLayout
+                android:layout_width="0dp"
                 android:layout_height="wrap_content"
-                android:singleLine="true"
-                android:fontFamily="@*android:string/config_headlineFontFamilyMedium"
-                android:textSize="18sp"
+                android:orientation="horizontal"
+                android:visibility="gone"
+                android:paddingTop="6dp"
                 android:paddingBottom="6dp"
-                android:gravity="center"/>
-
-            <!-- Artist name -->
-            <TextView
-                android:id="@+id/header_artist"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:fontFamily="@*android:string/config_bodyFontFamily"
-                android:textSize="14sp"
-                android:singleLine="true"
-            />
+                android:paddingLeft="12dp"
+                android:paddingRight="12dp"
+                android:gravity="center"
+                android:id="@+id/media_seamless"
+                android:background="@*android:drawable/media_seamless_background"
+                android:layout_weight="1"
+            >
+                <ImageView
+                    android:layout_width="@dimen/qs_seamless_icon_size"
+                    android:layout_height="@dimen/qs_seamless_icon_size"
+                    android:src="@*android:drawable/ic_media_seamless"
+                    android:layout_marginRight="8dp"
+                    android:id="@+id/media_seamless_image"
+                />
+                <TextView
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:fontFamily="@*android:string/config_bodyFontFamily"
+                    android:text="@*android:string/ext_media_seamless_action"
+                    android:textSize="14sp"
+                    android:id="@+id/media_seamless_text"
+                    android:singleLine="true"
+                />
+            </LinearLayout>
         </LinearLayout>
 
-        <!-- Output chip -->
+        <!-- Controls -->
         <LinearLayout
-            android:layout_width="0dp"
-            android:layout_height="wrap_content"
+            android:id="@+id/media_actions"
             android:orientation="horizontal"
-            android:visibility="gone"
-            android:paddingTop="6dp"
-            android:paddingBottom="6dp"
-            android:paddingLeft="12dp"
-            android:paddingRight="12dp"
+            android:layoutDirection="ltr"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
             android:gravity="center"
-            android:id="@+id/media_seamless"
-            android:background="@*android:drawable/media_seamless_background"
-            android:layout_weight="1"
-        >
-            <ImageView
-                android:layout_width="@dimen/qs_seamless_icon_size"
-                android:layout_height="@dimen/qs_seamless_icon_size"
-                android:src="@*android:drawable/ic_media_seamless"
-                android:layout_marginRight="8dp"
-                android:id="@+id/media_seamless_image"
+            >
+            <ImageButton
+                style="@android:style/Widget.Material.Button.Borderless.Small"
+                android:layout_width="48dp"
+                android:layout_height="48dp"
+                android:layout_marginStart="8dp"
+                android:layout_marginEnd="8dp"
+                android:gravity="center"
+                android:visibility="gone"
+                android:id="@+id/action0"
             />
-            <TextView
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:fontFamily="@*android:string/config_bodyFontFamily"
-                android:text="@*android:string/ext_media_seamless_action"
-                android:textSize="14sp"
-                android:id="@+id/media_seamless_text"
-                android:singleLine="true"
+            <ImageButton
+                style="@android:style/Widget.Material.Button.Borderless.Small"
+                android:layout_width="48dp"
+                android:layout_height="48dp"
+                android:layout_marginStart="8dp"
+                android:layout_marginEnd="8dp"
+                android:gravity="center"
+                android:visibility="gone"
+                android:id="@+id/action1"
+            />
+            <ImageButton
+                style="@android:style/Widget.Material.Button.Borderless.Small"
+                android:layout_width="52dp"
+                android:layout_height="52dp"
+                android:layout_marginStart="8dp"
+                android:layout_marginEnd="8dp"
+                android:gravity="center"
+                android:visibility="gone"
+                android:id="@+id/action2"
+            />
+            <ImageButton
+                style="@android:style/Widget.Material.Button.Borderless.Small"
+                android:layout_width="48dp"
+                android:layout_height="48dp"
+                android:layout_marginStart="8dp"
+                android:layout_marginEnd="8dp"
+                android:gravity="center"
+                android:visibility="gone"
+                android:id="@+id/action3"
+            />
+            <ImageButton
+                style="@android:style/Widget.Material.Button.Borderless.Small"
+                android:layout_width="48dp"
+                android:layout_height="48dp"
+                android:layout_marginStart="8dp"
+                android:layout_marginEnd="8dp"
+                android:gravity="center"
+                android:visibility="gone"
+                android:id="@+id/action4"
             />
         </LinearLayout>
     </LinearLayout>
-
-    <!-- Controls -->
-    <LinearLayout
-        android:id="@+id/media_actions"
-        android:orientation="horizontal"
-        android:layoutDirection="ltr"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:gravity="center"
-        >
-        <ImageButton
-            style="@android:style/Widget.Material.Button.Borderless.Small"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_marginStart="8dp"
-            android:layout_marginEnd="8dp"
-            android:gravity="center"
-            android:visibility="gone"
-            android:id="@+id/action0"
-        />
-        <ImageButton
-            style="@android:style/Widget.Material.Button.Borderless.Small"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_marginStart="8dp"
-            android:layout_marginEnd="8dp"
-            android:gravity="center"
-            android:visibility="gone"
-            android:id="@+id/action1"
-        />
-        <ImageButton
-            style="@android:style/Widget.Material.Button.Borderless.Small"
-            android:layout_width="52dp"
-            android:layout_height="52dp"
-            android:layout_marginStart="8dp"
-            android:layout_marginEnd="8dp"
-            android:gravity="center"
-            android:visibility="gone"
-            android:id="@+id/action2"
-        />
-        <ImageButton
-            style="@android:style/Widget.Material.Button.Borderless.Small"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_marginStart="8dp"
-            android:layout_marginEnd="8dp"
-            android:gravity="center"
-            android:visibility="gone"
-            android:id="@+id/action3"
-        />
-        <ImageButton
-            style="@android:style/Widget.Material.Button.Borderless.Small"
-            android:layout_width="48dp"
-            android:layout_height="48dp"
-            android:layout_marginStart="8dp"
-            android:layout_marginEnd="8dp"
-            android:gravity="center"
-            android:visibility="gone"
-            android:id="@+id/action4"
-        />
-    </LinearLayout>
 </LinearLayout>
diff --git a/packages/SystemUI/res/layout/qs_media_panel_options.xml b/packages/SystemUI/res/layout/qs_media_panel_options.xml
index 0669357..46655e7 100644
--- a/packages/SystemUI/res/layout/qs_media_panel_options.xml
+++ b/packages/SystemUI/res/layout/qs_media_panel_options.xml
@@ -19,23 +19,40 @@
     android:id="@+id/qs_media_controls_options"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    android:orientation="horizontal"
     android:gravity="center"
-    android:layout_gravity="center"
-    android:padding="10dp"
-    >
-    <ImageButton
+    android:padding="16dp"
+    android:orientation="vertical">
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="48dp"
+        android:layout_weight="1"
+        android:minWidth="48dp"
+        android:layout_gravity="start|bottom"
+        android:gravity="bottom"
         android:id="@+id/remove"
-        android:layout_width="48dp"
-        android:layout_height="48dp"
-        android:src="@android:drawable/ic_menu_delete"
-        android:padding="8dp"
-    />
-    <ImageButton
+        android:orientation="horizontal">
+        <ImageView
+            android:layout_width="18dp"
+            android:layout_height="18dp"
+            android:id="@+id/remove_icon"
+            android:layout_marginEnd="16dp"
+            android:src="@drawable/ic_clear"/>
+        <TextView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:id="@+id/remove_text"
+            android:fontFamily="@*android:string/config_headlineFontFamilyMedium"
+            android:singleLine="true"
+            android:text="@string/controls_media_close_session" />
+    </LinearLayout>
+    <TextView
         android:id="@+id/cancel"
-        android:layout_width="48dp"
+        android:layout_width="wrap_content"
         android:layout_height="48dp"
-        android:src="@android:drawable/ic_menu_revert"
-        android:padding="8dp"
-    />
+        android:layout_weight="1"
+        android:minWidth="48dp"
+        android:layout_gravity="end|bottom"
+        android:gravity="bottom"
+        android:fontFamily="@*android:string/config_headlineFontFamilyMedium"
+        android:text="@string/cancel" />
 </LinearLayout>
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml
index cb08840..179033a 100644
--- a/packages/SystemUI/res/values/strings.xml
+++ b/packages/SystemUI/res/values/strings.xml
@@ -2661,4 +2661,7 @@
     <!-- Message to tell the user to wait while systemui attempts to load a set of
          recommended controls [CHAR_LIMIT=30] -->
     <string name="controls_seeding_in_progress">Loading recommendations</string>
+
+    <!-- Close the controls associated with a specific media session [CHAR_LIMIT=NONE] -->
+    <string name="controls_media_close_session">Close this media session</string>
 </resources>
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSMediaPlayer.java b/packages/SystemUI/src/com/android/systemui/qs/QSMediaPlayer.java
index d5e5b10..8922e14 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSMediaPlayer.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSMediaPlayer.java
@@ -18,6 +18,7 @@
 
 import android.app.Notification;
 import android.content.Context;
+import android.content.res.ColorStateList;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.Icon;
 import android.media.session.MediaSession;
@@ -25,7 +26,9 @@
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageButton;
+import android.widget.ImageView;
 import android.widget.LinearLayout;
+import android.widget.TextView;
 
 import com.android.settingslib.media.MediaDevice;
 import com.android.systemui.R;
@@ -50,6 +53,8 @@
             R.id.action4
     };
 
+    private final QSPanel mParent;
+
     /**
      * Initialize quick shade version of player
      * @param context
@@ -62,6 +67,7 @@
             Executor foregroundExecutor, Executor backgroundExecutor) {
         super(context, parent, manager, R.layout.qs_media_panel, QS_ACTION_IDS, foregroundExecutor,
                 backgroundExecutor);
+        mParent = (QSPanel) parent;
     }
 
     /**
@@ -108,5 +114,45 @@
             ImageButton thisBtn = mMediaNotifView.findViewById(QS_ACTION_IDS[i]);
             thisBtn.setVisibility(View.GONE);
         }
+
+        // Set up long press menu
+        View guts = mMediaNotifView.findViewById(R.id.media_guts);
+        View options = mMediaNotifView.findViewById(R.id.qs_media_controls_options);
+        options.setMinimumHeight(guts.getHeight());
+
+        View clearView = options.findViewById(R.id.remove);
+        clearView.setOnClickListener(b -> {
+            mParent.removeMediaPlayer(QSMediaPlayer.this);
+        });
+        ImageView removeIcon = options.findViewById(R.id.remove_icon);
+        removeIcon.setImageTintList(ColorStateList.valueOf(iconColor));
+        TextView removeText = options.findViewById(R.id.remove_text);
+        removeText.setTextColor(iconColor);
+
+        TextView cancelView = options.findViewById(R.id.cancel);
+        cancelView.setTextColor(iconColor);
+        cancelView.setOnClickListener(b -> {
+            options.setVisibility(View.GONE);
+            guts.setVisibility(View.VISIBLE);
+        });
+        // ... but don't enable it yet, and make sure is reset when the session is updated
+        mMediaNotifView.setOnLongClickListener(null);
+        options.setVisibility(View.GONE);
+        guts.setVisibility(View.VISIBLE);
+    }
+
+    @Override
+    public void clearControls() {
+        super.clearControls();
+
+        View guts = mMediaNotifView.findViewById(R.id.media_guts);
+        View options = mMediaNotifView.findViewById(R.id.qs_media_controls_options);
+
+        mMediaNotifView.setOnLongClickListener(v -> {
+            // Replace player view with close/cancel view
+            guts.setVisibility(View.GONE);
+            options.setVisibility(View.VISIBLE);
+            return true; // consumed click
+        });
     }
 }