• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
41 @TargetApi(Build.VERSION_CODES.Q)
42 public class RemoteActionShortcut extends SystemShortcut<BaseDraggingActivity> {
43     private static final String TAG = "RemoteActionShortcut";
44     private static final boolean DEBUG = Utilities.IS_DEBUG_DEVICE;
45 
46     private final RemoteAction mAction;
47 
RemoteActionShortcut(RemoteAction action, BaseDraggingActivity activity, ItemInfo itemInfo, View originalView)48     public RemoteActionShortcut(RemoteAction action,
49             BaseDraggingActivity activity, ItemInfo itemInfo, View originalView) {
50         super(0, R.id.action_remote_action_shortcut, activity, itemInfo, originalView);
51         mAction = action;
52     }
53 
54     @Override
setIconAndLabelFor(View iconView, TextView labelView)55     public void setIconAndLabelFor(View iconView, TextView labelView) {
56         mAction.getIcon().loadDrawableAsync(iconView.getContext(),
57                 iconView::setBackground,
58                 MAIN_EXECUTOR.getHandler());
59         labelView.setText(mAction.getTitle());
60     }
61 
62     @Override
setIconAndContentDescriptionFor(ImageView view)63     public void setIconAndContentDescriptionFor(ImageView view) {
64         mAction.getIcon().loadDrawableAsync(view.getContext(),
65                 view::setImageDrawable,
66                 MAIN_EXECUTOR.getHandler());
67         view.setContentDescription(mAction.getContentDescription());
68     }
69 
70     @Override
createAccessibilityAction(Context context)71     public AccessibilityNodeInfo.AccessibilityAction createAccessibilityAction(Context context) {
72         return new AccessibilityNodeInfo.AccessibilityAction(
73                 R.id.action_remote_action_shortcut, mAction.getContentDescription());
74     }
75 
76     @Override
onClick(View view)77     public void onClick(View view) {
78         AbstractFloatingView.closeAllOpenViews(mTarget);
79         mTarget.getStatsLogManager().logger().withItemInfo(mItemInfo)
80                 .log(LAUNCHER_SYSTEM_SHORTCUT_PAUSE_TAP);
81 
82         final String actionIdentity = mAction.getTitle() + ", "
83                 + mItemInfo.getTargetComponent().getPackageName();
84         try {
85             if (DEBUG) Log.d(TAG, "Sending action: " + actionIdentity);
86             mAction.getActionIntent().send(
87                     mTarget,
88                     0,
89                     new Intent().putExtra(
90                             Intent.EXTRA_PACKAGE_NAME,
91                             mItemInfo.getTargetComponent().getPackageName()),
92                     (pendingIntent, intent, resultCode, resultData, resultExtras) -> {
93                         if (DEBUG) Log.d(TAG, "Action is complete: " + actionIdentity);
94                         if (resultData != null && !resultData.isEmpty()) {
95                             Log.e(TAG, "Remote action returned result: " + actionIdentity
96                                     + " : " + resultData);
97                             Toast.makeText(mTarget, resultData, Toast.LENGTH_SHORT).show();
98                         }
99                     },
100                     MAIN_EXECUTOR.getHandler());
101         } catch (PendingIntent.CanceledException e) {
102             Log.e(TAG, "Remote action canceled: " + actionIdentity, e);
103             Toast.makeText(mTarget, mTarget.getString(
104                     R.string.remote_action_failed,
105                     mAction.getTitle()),
106                     Toast.LENGTH_SHORT)
107                     .show();
108         }
109     }
110 
111     @Override
isLeftGroup()112     public boolean isLeftGroup() {
113         return true;
114     }
115 }
116