1 /* 2 * Copyright (C) 2016 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.accessibility; 18 19 import static com.android.launcher3.LauncherState.NORMAL; 20 import static com.android.launcher3.anim.AnimatorListeners.forSuccessCallback; 21 22 import android.view.KeyEvent; 23 import android.view.View; 24 25 import com.android.launcher3.AbstractFloatingView; 26 import com.android.launcher3.Launcher; 27 import com.android.launcher3.LauncherSettings; 28 import com.android.launcher3.R; 29 import com.android.launcher3.model.data.ItemInfo; 30 import com.android.launcher3.model.data.WorkspaceItemInfo; 31 import com.android.launcher3.notification.NotificationMainView; 32 import com.android.launcher3.shortcuts.DeepShortcutView; 33 34 import java.util.Collections; 35 import java.util.List; 36 37 /** 38 * Extension of {@link LauncherAccessibilityDelegate} with actions specific to shortcuts in 39 * deep shortcuts menu. 40 */ 41 public class ShortcutMenuAccessibilityDelegate extends LauncherAccessibilityDelegate { 42 43 private static final int DISMISS_NOTIFICATION = R.id.action_dismiss_notification; 44 ShortcutMenuAccessibilityDelegate(Launcher launcher)45 public ShortcutMenuAccessibilityDelegate(Launcher launcher) { 46 super(launcher); 47 mActions.put(DISMISS_NOTIFICATION, new LauncherAction(DISMISS_NOTIFICATION, 48 R.string.action_dismiss_notification, KeyEvent.KEYCODE_X)); 49 } 50 51 @Override getSupportedActions(View host, ItemInfo item, List<LauncherAction> out)52 protected void getSupportedActions(View host, ItemInfo item, List<LauncherAction> out) { 53 if ((host.getParent() instanceof DeepShortcutView)) { 54 out.add(mActions.get(ADD_TO_WORKSPACE)); 55 } else if (host instanceof NotificationMainView) { 56 if (((NotificationMainView) host).canChildBeDismissed()) { 57 out.add(mActions.get(DISMISS_NOTIFICATION)); 58 } 59 } 60 } 61 62 @Override performAction(View host, ItemInfo item, int action, boolean fromKeyboard)63 protected boolean performAction(View host, ItemInfo item, int action, boolean fromKeyboard) { 64 if (action == ADD_TO_WORKSPACE) { 65 if (!(host.getParent() instanceof DeepShortcutView)) { 66 return false; 67 } 68 final WorkspaceItemInfo info = ((DeepShortcutView) host.getParent()).getFinalInfo(); 69 final int[] coordinates = new int[2]; 70 final int screenId = findSpaceOnWorkspace(item, coordinates); 71 mLauncher.getStateManager().goToState(NORMAL, true, forSuccessCallback(() -> { 72 mLauncher.getModelWriter().addItemToDatabase(info, 73 LauncherSettings.Favorites.CONTAINER_DESKTOP, 74 screenId, coordinates[0], coordinates[1]); 75 mLauncher.bindItems(Collections.singletonList(info), true); 76 AbstractFloatingView.closeAllOpenViews(mLauncher); 77 announceConfirmation(R.string.item_added_to_workspace); 78 })); 79 return true; 80 } else if (action == DISMISS_NOTIFICATION) { 81 if (!(host instanceof NotificationMainView)) { 82 return false; 83 } 84 ((NotificationMainView) host).onChildDismissed(); 85 announceConfirmation(R.string.notification_dismissed); 86 return true; 87 } 88 return false; 89 } 90 } 91