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