Adding SystemShortcut for RemoteAction

Bug: 117888000
Test: manual test with a prototype
Change-Id: I10ed0a8c0237a85af5ac32a7fc68e1f9d2ffc5ba
diff --git a/protos/launcher_log.proto b/protos/launcher_log.proto
index 41dd0bd..b3ed365 100644
--- a/protos/launcher_log.proto
+++ b/protos/launcher_log.proto
@@ -112,6 +112,7 @@
   CANCEL_TARGET = 14;
   TASK_PREVIEW = 15;
   SPLIT_SCREEN_TARGET = 16;
+  REMOTE_ACTION_SHORTCUT = 17;
 }
 
 enum TipType {
diff --git a/res/values/config.xml b/res/values/config.xml
index 0efaccf..85c2e65 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -123,6 +123,7 @@
     <item type="id" name="action_deep_shortcuts" />
     <item type="id" name="action_shortcuts_and_notifications"/>
     <item type="id" name="action_dismiss_notification" />
+    <item type="id" name="action_remote_action_shortcut" />
 
 <!-- QSB IDs. DO not change -->
     <item type="id" name="search_container_workspace" />
diff --git a/res/values/strings.xml b/res/values/strings.xml
index eb6b284..7e5784d 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -347,4 +347,7 @@
     <string name="work_mode_off_label">Notifications and apps are off</string>
     <string name="bottom_work_tab_user_education_close_button">Close</string>
     <string name="bottom_work_tab_user_education_closed">Closed</string>
+
+    <!-- Failed action error message: e.g. Failed: Pause -->
+    <string name="remote_action_failed">Failed: <xliff:g id="what" example="Pause">%1$s</xliff:g></string>
 </resources>
diff --git a/src/com/android/launcher3/popup/RemoteActionShortcut.java b/src/com/android/launcher3/popup/RemoteActionShortcut.java
new file mode 100644
index 0000000..af0d3da
--- /dev/null
+++ b/src/com/android/launcher3/popup/RemoteActionShortcut.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.launcher3.popup;
+
+import android.app.PendingIntent;
+import android.app.RemoteAction;
+import android.os.Handler;
+import android.os.Looper;
+import android.util.Log;
+import android.view.View;
+import android.widget.Toast;
+
+import com.android.launcher3.AbstractFloatingView;
+import com.android.launcher3.ItemInfo;
+import com.android.launcher3.Launcher;
+import com.android.launcher3.R;
+import com.android.launcher3.userevent.nano.LauncherLogProto;
+
+public class RemoteActionShortcut extends SystemShortcut<Launcher> {
+    private static final String TAG = "RemoteActionShortcut";
+
+    private final RemoteAction mAction;
+
+    public RemoteActionShortcut(RemoteAction action) {
+        super(action.getIcon(), action.getTitle(), action.getContentDescription(),
+                R.id.action_remote_action_shortcut);
+        mAction = action;
+    }
+
+    @Override
+    public View.OnClickListener getOnClickListener(
+            final Launcher launcher, final ItemInfo itemInfo) {
+        return view -> {
+            AbstractFloatingView.closeAllOpenViews(launcher);
+
+            try {
+                mAction.getActionIntent().send(0,
+                        (pendingIntent, intent, resultCode, resultData, resultExtras) -> {
+                            if (resultData != null && !resultData.isEmpty()) {
+                                Log.e(TAG, "Remote action returned result: " + mAction.getTitle()
+                                        + " : " + resultData);
+                                Toast.makeText(launcher, resultData, Toast.LENGTH_SHORT).show();
+                            }
+                        }, new Handler(Looper.getMainLooper()));
+            } catch (PendingIntent.CanceledException e) {
+                Log.e(TAG, "Remote action canceled: " + mAction.getTitle(), e);
+                Toast.makeText(launcher, launcher.getString(
+                        R.string.remote_action_failed,
+                        mAction.getTitle()),
+                        Toast.LENGTH_SHORT)
+                        .show();
+            }
+
+            launcher.getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.Touch.TAP,
+                    LauncherLogProto.ControlType.REMOTE_ACTION_SHORTCUT, view);
+        };
+    }
+}
diff --git a/src/com/android/launcher3/popup/SystemShortcut.java b/src/com/android/launcher3/popup/SystemShortcut.java
index b80ba8a..f9a2007 100644
--- a/src/com/android/launcher3/popup/SystemShortcut.java
+++ b/src/com/android/launcher3/popup/SystemShortcut.java
@@ -6,8 +6,10 @@
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Rect;
-import android.graphics.drawable.Drawable;
+import android.graphics.drawable.Icon;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
 import android.view.View;
 import android.view.accessibility.AccessibilityNodeInfo;
 import android.widget.ImageView;
@@ -36,7 +38,7 @@
 public abstract class SystemShortcut<T extends BaseDraggingActivity> extends ItemInfo {
     private final int mIconResId;
     private final int mLabelResId;
-    private final Drawable mIcon;
+    private final Icon mIcon;
     private final CharSequence mLabel;
     private final CharSequence mContentDescription;
     private final int mAccessibilityActionId;
@@ -50,7 +52,7 @@
         mContentDescription = null;
     }
 
-    public SystemShortcut(Drawable icon, CharSequence label, CharSequence contentDescription,
+    public SystemShortcut(Icon icon, CharSequence label, CharSequence contentDescription,
             int accessibilityActionId) {
         mIcon = icon;
         mLabel = label;
@@ -71,7 +73,9 @@
 
     public void setIconAndLabelFor(View iconView, TextView labelView) {
         if (mIcon != null) {
-            iconView.setBackground(mIcon);
+            mIcon.loadDrawableAsync(iconView.getContext(),
+                    iconView::setBackground,
+                    new Handler(Looper.getMainLooper()));
         } else {
             iconView.setBackgroundResource(mIconResId);
         }
@@ -85,7 +89,9 @@
 
     public void setIconAndContentDescriptionFor(ImageView view) {
         if (mIcon != null) {
-            view.setImageDrawable(mIcon);
+            mIcon.loadDrawableAsync(view.getContext(),
+                    view::setImageDrawable,
+                    new Handler(Looper.getMainLooper()));
         } else {
             view.setImageResource(mIconResId);
         }