1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.launcher3.popup;
18 
19 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_SYSTEM_SHORTCUT_PAUSE_TAP;
20 import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
21 
22 import android.annotation.TargetApi;
23 import android.app.PendingIntent;
24 import android.app.RemoteAction;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.os.Build;
28 import android.util.Log;
29 import android.view.View;
30 import android.view.accessibility.AccessibilityNodeInfo;
31 import android.widget.ImageView;
32 import android.widget.TextView;
33 import android.widget.Toast;
34 
35 import com.android.launcher3.AbstractFloatingView;
36 import com.android.launcher3.BaseDraggingActivity;
37 import com.android.launcher3.R;
38 import com.android.launcher3.Utilities;
39 import com.android.launcher3.model.data.ItemInfo;
40 import com.android.launcher3.userevent.nano.LauncherLogProto;
41 
42 @TargetApi(Build.VERSION_CODES.Q)
43 public class RemoteActionShortcut extends SystemShortcut<BaseDraggingActivity> {
44     private static final String TAG = "RemoteActionShortcut";
45     private static final boolean DEBUG = Utilities.IS_DEBUG_DEVICE;
46 
47     private final RemoteAction mAction;
48 
RemoteActionShortcut(RemoteAction action, BaseDraggingActivity activity, ItemInfo itemInfo)49     public RemoteActionShortcut(RemoteAction action,
50             BaseDraggingActivity activity, ItemInfo itemInfo) {
51         super(0, R.id.action_remote_action_shortcut, activity, itemInfo);
52         mAction = action;
53     }
54 
55     @Override
setIconAndLabelFor(View iconView, TextView labelView)56     public void setIconAndLabelFor(View iconView, TextView labelView) {
57         mAction.getIcon().loadDrawableAsync(iconView.getContext(),
58                 iconView::setBackground,
59                 MAIN_EXECUTOR.getHandler());
60         labelView.setText(mAction.getTitle());
61     }
62 
63     @Override
setIconAndContentDescriptionFor(ImageView view)64     public void setIconAndContentDescriptionFor(ImageView view) {
65         mAction.getIcon().loadDrawableAsync(view.getContext(),
66                 view::setImageDrawable,
67                 MAIN_EXECUTOR.getHandler());
68         view.setContentDescription(mAction.getContentDescription());
69     }
70 
71     @Override
createAccessibilityAction(Context context)72     public AccessibilityNodeInfo.AccessibilityAction createAccessibilityAction(Context context) {
73         return new AccessibilityNodeInfo.AccessibilityAction(
74                 R.id.action_remote_action_shortcut, mAction.getContentDescription());
75     }
76 
77     @Override
onClick(View view)78     public void onClick(View view) {
79         AbstractFloatingView.closeAllOpenViews(mTarget);
80         mTarget.getStatsLogManager().logger().withItemInfo(mItemInfo)
81                 .log(LAUNCHER_SYSTEM_SHORTCUT_PAUSE_TAP);
82 
83         final String actionIdentity = mAction.getTitle() + ", "
84                 + mItemInfo.getTargetComponent().getPackageName();
85         try {
86             if (DEBUG) Log.d(TAG, "Sending action: " + actionIdentity);
87             mAction.getActionIntent().send(
88                     mTarget,
89                     0,
90                     new Intent().putExtra(
91                             Intent.EXTRA_PACKAGE_NAME,
92                             mItemInfo.getTargetComponent().getPackageName()),
93                     (pendingIntent, intent, resultCode, resultData, resultExtras) -> {
94                         if (DEBUG) Log.d(TAG, "Action is complete: " + actionIdentity);
95                         if (resultData != null && !resultData.isEmpty()) {
96                             Log.e(TAG, "Remote action returned result: " + actionIdentity
97                                     + " : " + resultData);
98                             Toast.makeText(mTarget, resultData, Toast.LENGTH_SHORT).show();
99                         }
100                     },
101                     MAIN_EXECUTOR.getHandler());
102         } catch (PendingIntent.CanceledException e) {
103             Log.e(TAG, "Remote action canceled: " + actionIdentity, e);
104             Toast.makeText(mTarget, mTarget.getString(
105                     R.string.remote_action_failed,
106                     mAction.getTitle()),
107                     Toast.LENGTH_SHORT)
108                     .show();
109         }
110 
111         mTarget.getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.Touch.TAP,
112                 LauncherLogProto.ControlType.REMOTE_ACTION_SHORTCUT, view);
113     }
114 
115     @Override
isLeftGroup()116     public boolean isLeftGroup() {
117         return true;
118     }
119 }
120