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 import static com.android.launcher3.anim.AnimatorListeners.forSuccessCallback; 21 22 import android.view.View; 23 24 import com.android.launcher3.AbstractFloatingView; 25 import com.android.launcher3.Launcher; 26 import com.android.launcher3.LauncherSettings; 27 import com.android.launcher3.model.data.ItemInfo; 28 import com.android.launcher3.model.data.WorkspaceItemInfo; 29 import com.android.launcher3.shortcuts.DeepShortcutView; 30 31 import java.util.Collections; 32 import java.util.List; 33 34 /** 35 * Extension of {@link LauncherAccessibilityDelegate} with actions specific to shortcuts in 36 * deep shortcuts menu. 37 */ 38 public class ShortcutMenuAccessibilityDelegate extends LauncherAccessibilityDelegate { 39 ShortcutMenuAccessibilityDelegate(Launcher launcher)40 public ShortcutMenuAccessibilityDelegate(Launcher launcher) { 41 super(launcher); 42 } 43 44 @Override getSupportedActions(View host, ItemInfo item, List<LauncherAction> out)45 protected void getSupportedActions(View host, ItemInfo item, List<LauncherAction> out) { 46 if ((host.getParent() instanceof DeepShortcutView)) { 47 out.add(mActions.get(ADD_TO_WORKSPACE)); 48 } 49 } 50 51 @Override performAction(View host, ItemInfo item, int action, boolean fromKeyboard)52 protected boolean performAction(View host, ItemInfo item, int action, boolean fromKeyboard) { 53 if (action == ADD_TO_WORKSPACE) { 54 if (!(host.getParent() instanceof DeepShortcutView)) { 55 return false; 56 } 57 final WorkspaceItemInfo info = ((DeepShortcutView) host.getParent()).getFinalInfo(); 58 final int[] coordinates = new int[2]; 59 final int screenId = findSpaceOnWorkspace(item, coordinates); 60 if (screenId == -1) { 61 return false; 62 } 63 mContext.getStateManager().goToState(NORMAL, true, forSuccessCallback(() -> { 64 mContext.getModelWriter().addItemToDatabase(info, 65 LauncherSettings.Favorites.CONTAINER_DESKTOP, 66 screenId, coordinates[0], coordinates[1]); 67 mContext.bindItems(Collections.singletonList(info), true); 68 AbstractFloatingView.closeAllOpenViews(mContext); 69 })); 70 return true; 71 } 72 return false; 73 } 74 } 75