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
17package com.android.launcher3.accessibility;
18
19import android.view.View;
20import android.view.accessibility.AccessibilityNodeInfo;
21
22import com.android.launcher3.ItemInfo;
23import com.android.launcher3.Launcher;
24import com.android.launcher3.LauncherModel;
25import com.android.launcher3.LauncherSettings;
26import com.android.launcher3.R;
27import com.android.launcher3.ShortcutInfo;
28import com.android.launcher3.shortcuts.DeepShortcutView;
29
30import java.util.ArrayList;
31
32/**
33 * Extension of {@link LauncherAccessibilityDelegate} with actions specific to shortcuts in
34 * deep shortcuts menu.
35 */
36public class ShortcutMenuAccessibilityDelegate extends LauncherAccessibilityDelegate {
37
38    public ShortcutMenuAccessibilityDelegate(Launcher launcher) {
39        super(launcher);
40    }
41
42    @Override
43    protected void addActions(View host, AccessibilityNodeInfo info) {
44        info.addAction(mActions.get(ADD_TO_WORKSPACE));
45    }
46
47    @Override
48    public boolean performAction(View host, ItemInfo item, int action) {
49        if (action == ADD_TO_WORKSPACE) {
50            if (!(host.getParent() instanceof DeepShortcutView)) {
51                return false;
52            }
53            final ShortcutInfo info = ((DeepShortcutView) host.getParent()).getFinalInfo();
54            final int[] coordinates = new int[2];
55            final long screenId = findSpaceOnWorkspace(item, coordinates);
56            Runnable onComplete = new Runnable() {
57                @Override
58                public void run() {
59                    LauncherModel.addItemToDatabase(mLauncher, info,
60                            LauncherSettings.Favorites.CONTAINER_DESKTOP,
61                            screenId, coordinates[0], coordinates[1]);
62                    ArrayList<ItemInfo> itemList = new ArrayList<>();
63                    itemList.add(info);
64                    mLauncher.bindItems(itemList, 0, itemList.size(), true);
65                    mLauncher.closeShortcutsContainer();
66                    announceConfirmation(R.string.item_added_to_workspace);
67                }
68            };
69
70            if (!mLauncher.showWorkspace(true, onComplete)) {
71                onComplete.run();
72            }
73            return true;
74        }
75        return false;
76    }
77}
78