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.Utilities.allowBGLaunch; 20 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_SYSTEM_SHORTCUT_PAUSE_TAP; 21 import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; 22 23 import android.app.ActivityOptions; 24 import android.app.PendingIntent; 25 import android.app.RemoteAction; 26 import android.content.Context; 27 import android.content.Intent; 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.R; 37 import com.android.launcher3.Utilities; 38 import com.android.launcher3.model.data.ItemInfo; 39 import com.android.launcher3.views.ActivityContext; 40 41 import java.lang.ref.WeakReference; 42 43 public class RemoteActionShortcut<T extends Context & ActivityContext> extends SystemShortcut<T> { 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, T context, ItemInfo itemInfo, View originalView)49 public RemoteActionShortcut(RemoteAction action, 50 T context, ItemInfo itemInfo, View originalView) { 51 super(0, R.id.action_remote_action_shortcut, context, itemInfo, originalView); 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 WeakReference<T> weakTarget = new WeakReference<>(mTarget); 84 final String actionIdentity = mAction.getTitle() + ", " 85 + mItemInfo.getTargetComponent().getPackageName(); 86 87 ActivityOptions options = allowBGLaunch(ActivityOptions.makeBasic()); 88 try { 89 if (DEBUG) Log.d(TAG, "Sending action: " + actionIdentity); 90 mAction.getActionIntent().send( 91 mTarget, 92 0, 93 new Intent().putExtra( 94 Intent.EXTRA_PACKAGE_NAME, 95 mItemInfo.getTargetComponent().getPackageName()), 96 (pendingIntent, intent, resultCode, resultData, resultExtras) -> { 97 if (DEBUG) Log.d(TAG, "Action is complete: " + actionIdentity); 98 final T target = weakTarget.get(); 99 if (resultData != null && !resultData.isEmpty()) { 100 Log.e(TAG, "Remote action returned result: " + actionIdentity 101 + " : " + resultData); 102 if (target != null) { 103 Toast.makeText(target, resultData, Toast.LENGTH_SHORT).show(); 104 } 105 } 106 }, 107 MAIN_EXECUTOR.getHandler(), 108 null, 109 options.toBundle()); 110 } catch (PendingIntent.CanceledException e) { 111 Log.e(TAG, "Remote action canceled: " + actionIdentity, e); 112 Toast.makeText(mTarget, mTarget.getString( 113 R.string.remote_action_failed, 114 mAction.getTitle()), 115 Toast.LENGTH_SHORT) 116 .show(); 117 } 118 } 119 } 120