Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame^] | 1 | package com.android.launcher3; |
| 2 | |
| 3 | import android.annotation.TargetApi; |
| 4 | import android.os.Build; |
| 5 | import android.os.Bundle; |
| 6 | import android.util.SparseArray; |
| 7 | import android.view.View; |
| 8 | import android.view.View.AccessibilityDelegate; |
| 9 | import android.view.accessibility.AccessibilityNodeInfo; |
| 10 | import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; |
| 11 | |
| 12 | import com.android.launcher3.LauncherModel.ScreenPosProvider; |
| 13 | |
| 14 | import java.util.ArrayList; |
| 15 | |
| 16 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 17 | public class LauncherAccessibilityDelegate extends AccessibilityDelegate { |
| 18 | |
| 19 | public static final int REMOVE = R.id.action_remove; |
| 20 | public static final int INFO = R.id.action_info; |
| 21 | public static final int UNINSTALL = R.id.action_uninstall; |
| 22 | public static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace; |
| 23 | |
| 24 | private final SparseArray<AccessibilityAction> mActions = |
| 25 | new SparseArray<AccessibilityAction>(); |
| 26 | private final Launcher mLauncher; |
| 27 | |
| 28 | public LauncherAccessibilityDelegate(Launcher launcher) { |
| 29 | mLauncher = launcher; |
| 30 | |
| 31 | mActions.put(REMOVE, new AccessibilityAction(REMOVE, |
| 32 | launcher.getText(R.string.delete_target_label))); |
| 33 | mActions.put(INFO, new AccessibilityAction(INFO, |
| 34 | launcher.getText(R.string.info_target_label))); |
| 35 | mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL, |
| 36 | launcher.getText(R.string.delete_target_uninstall_label))); |
| 37 | mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE, |
| 38 | launcher.getText(R.string.action_add_to_workspace))); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { |
| 43 | super.onInitializeAccessibilityNodeInfo(host, info); |
| 44 | if (!(host.getTag() instanceof ItemInfo)) return; |
| 45 | ItemInfo item = (ItemInfo) host.getTag(); |
| 46 | |
| 47 | if ((item instanceof ShortcutInfo) |
| 48 | || (item instanceof LauncherAppWidgetInfo) |
| 49 | || (item instanceof FolderInfo)) { |
| 50 | // Workspace shortcut / widget |
| 51 | info.addAction(mActions.get(REMOVE)); |
| 52 | } else if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) { |
| 53 | // App or Widget from customization tray |
| 54 | if (item instanceof AppInfo) { |
| 55 | info.addAction(mActions.get(UNINSTALL)); |
| 56 | } |
| 57 | info.addAction(mActions.get(INFO)); |
| 58 | info.addAction(mActions.get(ADD_TO_WORKSPACE)); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public boolean performAccessibilityAction(View host, int action, Bundle args) { |
| 64 | if ((host.getTag() instanceof ItemInfo) |
| 65 | && performAction(host, (ItemInfo) host.getTag(), action)) { |
| 66 | return true; |
| 67 | } |
| 68 | return super.performAccessibilityAction(host, action, args); |
| 69 | } |
| 70 | |
| 71 | public boolean performAction(View host, ItemInfo item, int action) { |
| 72 | if (action == REMOVE) { |
| 73 | return DeleteDropTarget.removeWorkspaceOrFolderItem(mLauncher, item, host); |
| 74 | } else if (action == INFO) { |
| 75 | InfoDropTarget.startDetailsActivityForInfo(item, mLauncher); |
| 76 | return true; |
| 77 | } else if (action == UNINSTALL) { |
| 78 | DeleteDropTarget.uninstallApp(mLauncher, (AppInfo) item); |
| 79 | return true; |
| 80 | } else if (action == ADD_TO_WORKSPACE) { |
| 81 | final int preferredPage = mLauncher.getWorkspace().getCurrentPage(); |
| 82 | final ScreenPosProvider screenProvider = new ScreenPosProvider() { |
| 83 | |
| 84 | @Override |
| 85 | public int getScreenIndex(ArrayList<Long> screenIDs) { |
| 86 | return preferredPage; |
| 87 | } |
| 88 | }; |
| 89 | if (item instanceof AppInfo) { |
| 90 | final ArrayList<ItemInfo> addShortcuts = new ArrayList<ItemInfo>(); |
| 91 | addShortcuts.add(((AppInfo) item).makeShortcut()); |
| 92 | mLauncher.showWorkspace(true, new Runnable() { |
| 93 | |
| 94 | @Override |
| 95 | public void run() { |
| 96 | mLauncher.getModel().addAndBindAddedWorkspaceApps( |
| 97 | mLauncher, addShortcuts, screenProvider, 0, true); |
| 98 | } |
| 99 | }); |
| 100 | return true; |
| 101 | } else if (item instanceof PendingAddItemInfo) { |
| 102 | mLauncher.getModel().addAndBindPendingItem( |
| 103 | mLauncher, (PendingAddItemInfo) item, screenProvider, 0); |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | } |