summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/LauncherAccessibilityDelegate.java
blob: cfc1bd96cbe69c4ce1b030a978426a82d7823e0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.android.launcher3;

import android.annotation.TargetApi;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.accessibility.AccessibilityEventCompat;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.util.SparseArray;
import android.view.View;
import android.view.View.AccessibilityDelegate;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;

import com.android.launcher3.LauncherModel.ScreenPosProvider;
import com.android.launcher3.util.Thunk;

import java.util.ArrayList;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class LauncherAccessibilityDelegate extends AccessibilityDelegate {

    public static final int REMOVE = R.id.action_remove;
    public static final int INFO = R.id.action_info;
    public static final int UNINSTALL = R.id.action_uninstall;
    public static final int ADD_TO_WORKSPACE = R.id.action_add_to_workspace;
    public static final int MOVE = R.id.action_move;

    enum DragType {
        ICON,
        FOLDER,
        WIDGET
    }

    public static class DragInfo {
        DragType dragType;
        ItemInfo info;
        View item;
    }

    private DragInfo mDragInfo = null;

    private final SparseArray<AccessibilityAction> mActions =
            new SparseArray<AccessibilityAction>();
    @Thunk final Launcher mLauncher;

    public LauncherAccessibilityDelegate(Launcher launcher) {
        mLauncher = launcher;

        mActions.put(REMOVE, new AccessibilityAction(REMOVE,
                launcher.getText(R.string.delete_target_label)));
        mActions.put(INFO, new AccessibilityAction(INFO,
                launcher.getText(R.string.info_target_label)));
        mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,
                launcher.getText(R.string.delete_target_uninstall_label)));
        mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,
                launcher.getText(R.string.action_add_to_workspace)));
        mActions.put(MOVE, new AccessibilityAction(MOVE,
                launcher.getText(R.string.action_move)));

    }

    @Override
    public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
        super.onInitializeAccessibilityNodeInfo(host, info);
        if (!(host.getTag() instanceof ItemInfo)) return;
        ItemInfo item = (ItemInfo) host.getTag();

        if ((item instanceof ShortcutInfo)
                || (item instanceof LauncherAppWidgetInfo)
                || (item instanceof FolderInfo)) {
            // Workspace shortcut / widget
            info.addAction(mActions.get(REMOVE));
            info.addAction(mActions.get(MOVE));
        } else if ((item instanceof AppInfo) || (item instanceof PendingAddItemInfo)) {
            // App or Widget from customization tray
            if (item instanceof AppInfo) {
                info.addAction(mActions.get(UNINSTALL));
            }
            info.addAction(mActions.get(INFO));
            info.addAction(mActions.get(ADD_TO_WORKSPACE));
        }
    }

    @Override
    public boolean performAccessibilityAction(View host, int action, Bundle args) {
        if ((host.getTag() instanceof ItemInfo)
                && performAction(host, (ItemInfo) host.getTag(), action)) {
            return true;
        }
        return super.performAccessibilityAction(host, action, args);
    }

    public boolean performAction(View host, ItemInfo item, int action) {
        Resources res = mLauncher.getResources();
        if (action == REMOVE) {
            if (DeleteDropTarget.removeWorkspaceOrFolderItem(mLauncher, item, host)) {
                announceConfirmation(R.string.item_removed_from_workspace);
                return true;
            }
            return false;
        } else if (action == INFO) {
            InfoDropTarget.startDetailsActivityForInfo(item, mLauncher);
            return true;
        } else if (action == UNINSTALL) {
            DeleteDropTarget.uninstallApp(mLauncher, (AppInfo) item);
            return true;
        } else if (action == MOVE) {
            beginAccessibleDrag(host, item);
        } else if (action == ADD_TO_WORKSPACE) {
            final int preferredPage = mLauncher.getWorkspace().getCurrentPage();
            final ScreenPosProvider screenProvider = new ScreenPosProvider() {

                @Override
                public int getScreenIndex(ArrayList<Long> screenIDs) {
                    return preferredPage;
                }
            };
            if (item instanceof AppInfo) {
                final ArrayList<ItemInfo> addShortcuts = new ArrayList<ItemInfo>();
                addShortcuts.add(((AppInfo) item).makeShortcut());
                mLauncher.showWorkspace(true, new Runnable() {
                    @Override
                    public void run() {
                        mLauncher.getModel().addAndBindAddedWorkspaceItems(
                                mLauncher, addShortcuts, screenProvider, 0, true);
                        announceConfirmation(R.string.item_added_to_workspace);
                    }
                });
                return true;
            } else if (item instanceof PendingAddItemInfo) {
                mLauncher.getModel().addAndBindPendingItem(
                        mLauncher, (PendingAddItemInfo) item, screenProvider, 0);
                announceConfirmation(R.string.item_added_to_workspace);
                return true;
            }
        }
        return false;
    }

    @Thunk void announceConfirmation(int resId) {
        announceConfirmation(mLauncher.getResources().getString(resId));
    }

    @Thunk void announceConfirmation(String confirmation) {
        mLauncher.getDragLayer().announceForAccessibility(confirmation);

    }

    public boolean isInAccessibleDrag() {
        return mDragInfo != null;
    }

    public DragInfo getDragInfo() {
        return mDragInfo;
    }

    public void handleAccessibleDrop(CellLayout targetContainer, Rect dropLocation,
            String confirmation) {
        if (!isInAccessibleDrag()) return;

        int[] loc = new int[2];
        loc[0] = dropLocation.centerX();
        loc[1] = dropLocation.centerY();

        mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(targetContainer, loc);
        mLauncher.getDragController().completeAccessibleDrag(loc);

        endAccessibleDrag();
        announceConfirmation(confirmation);
    }

    public void beginAccessibleDrag(View item, ItemInfo info) {
        mDragInfo = new DragInfo();
        mDragInfo.info = info;
        mDragInfo.item = item;
        mDragInfo.dragType = DragType.ICON;
        if (info instanceof FolderInfo) {
            mDragInfo.dragType = DragType.FOLDER;
        } else if (info instanceof LauncherAppWidgetInfo) {
            mDragInfo.dragType = DragType.WIDGET;
        }

        CellLayout.CellInfo cellInfo = new CellLayout.CellInfo(item, info);

        Rect pos = new Rect();
        mLauncher.getDragLayer().getDescendantRectRelativeToSelf(item, pos);

        mLauncher.getDragController().prepareAccessibleDrag(pos.centerX(), pos.centerY());
        mLauncher.getWorkspace().enableAccessibleDrag(true);
        mLauncher.getWorkspace().startDrag(cellInfo, true);
    }

    public boolean onBackPressed() {
        if (isInAccessibleDrag()) {
            cancelAccessibleDrag();
            return true;
        }
        return false;
    }

    private void cancelAccessibleDrag() {
        mLauncher.getDragController().cancelDrag();
        endAccessibleDrag();
    }

    private void endAccessibleDrag() {
        mDragInfo = null;
        mLauncher.getWorkspace().enableAccessibleDrag(false);
    }
}