1 /* 2 * Copyright (C) 2020 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.views; 18 19 import static com.android.launcher3.LauncherState.ALL_APPS; 20 import static com.android.launcher3.LauncherState.NORMAL; 21 22 import android.content.Context; 23 import android.os.Bundle; 24 import android.util.AttributeSet; 25 import android.view.View; 26 import android.view.accessibility.AccessibilityNodeInfo; 27 import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; 28 29 import androidx.annotation.Nullable; 30 31 import com.android.launcher3.Launcher; 32 import com.android.launcher3.LauncherState; 33 import com.android.launcher3.R; 34 import com.android.launcher3.statemanager.StateManager.StateListener; 35 import com.android.launcher3.views.OptionsPopupView.OptionItem; 36 37 /** 38 * Placeholder view to expose additional Launcher actions via accessibility actions 39 */ 40 public class AccessibilityActionsView extends View implements StateListener<LauncherState> { 41 AccessibilityActionsView(Context context)42 public AccessibilityActionsView(Context context) { 43 this(context, null); 44 } 45 AccessibilityActionsView(Context context, @Nullable AttributeSet attrs)46 public AccessibilityActionsView(Context context, @Nullable AttributeSet attrs) { 47 this(context, attrs, 0); 48 } 49 AccessibilityActionsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)50 public AccessibilityActionsView(Context context, @Nullable AttributeSet attrs, 51 int defStyleAttr) { 52 super(context, attrs, defStyleAttr); 53 Launcher.getLauncher(context).getStateManager().addStateListener(this); 54 setWillNotDraw(true); 55 } 56 57 @Override onStateTransitionComplete(LauncherState finalState)58 public void onStateTransitionComplete(LauncherState finalState) { 59 setImportantForAccessibility(finalState == NORMAL 60 ? IMPORTANT_FOR_ACCESSIBILITY_YES : IMPORTANT_FOR_ACCESSIBILITY_NO); 61 } 62 63 @Override createAccessibilityNodeInfo()64 public AccessibilityNodeInfo createAccessibilityNodeInfo() { 65 AccessibilityNodeInfo info = super.createAccessibilityNodeInfo(); 66 Launcher l = Launcher.getLauncher(getContext()); 67 info.addAction(new AccessibilityAction( 68 R.string.all_apps_button_label, l.getText(R.string.all_apps_button_label))); 69 for (OptionItem item : OptionsPopupView.getOptions(l)) { 70 info.addAction(new AccessibilityAction(item.labelRes, item.label)); 71 } 72 return info; 73 } 74 75 @Override performAccessibilityAction(int action, Bundle arguments)76 public boolean performAccessibilityAction(int action, Bundle arguments) { 77 if (super.performAccessibilityAction(action, arguments)) { 78 return true; 79 } 80 Launcher l = Launcher.getLauncher(getContext()); 81 if (action == R.string.all_apps_button_label) { 82 l.getStateManager().goToState(ALL_APPS); 83 return true; 84 } 85 for (OptionItem item : OptionsPopupView.getOptions(l)) { 86 if (item.labelRes == action) { 87 if (item.eventId.getId() > 0) { 88 l.getStatsLogManager().logger().log(item.eventId); 89 } 90 if (item.clickListener.onLongClick(this)) { 91 return true; 92 } 93 } 94 } 95 return false; 96 } 97 } 98