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