1/* 2 * Copyright (C) 2015 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.content.Context; 20import android.os.Bundle; 21import android.util.SparseArray; 22import android.view.View; 23import android.view.View.AccessibilityDelegate; 24import android.view.accessibility.AccessibilityNodeInfo; 25import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; 26 27import com.android.launcher3.R; 28import com.android.launcher3.Utilities; 29import com.android.launcher3.Workspace; 30import com.android.launcher3.config.FeatureFlags; 31 32public class OverviewScreenAccessibilityDelegate extends AccessibilityDelegate { 33 34 private static final int MOVE_BACKWARD = R.id.action_move_screen_backwards; 35 private static final int MOVE_FORWARD = R.id.action_move_screen_forwards; 36 37 private final SparseArray<AccessibilityAction> mActions = new SparseArray<>(); 38 private final Workspace mWorkspace; 39 40 public OverviewScreenAccessibilityDelegate(Workspace workspace) { 41 mWorkspace = workspace; 42 43 Context context = mWorkspace.getContext(); 44 boolean isRtl = Utilities.isRtl(context.getResources()); 45 mActions.put(MOVE_BACKWARD, new AccessibilityAction(MOVE_BACKWARD, 46 context.getText(isRtl ? R.string.action_move_screen_right : 47 R.string.action_move_screen_left))); 48 mActions.put(MOVE_FORWARD, new AccessibilityAction(MOVE_FORWARD, 49 context.getText(isRtl ? R.string.action_move_screen_left : 50 R.string.action_move_screen_right))); 51 } 52 53 @Override 54 public boolean performAccessibilityAction(View host, int action, Bundle args) { 55 if (host != null) { 56 if (action == AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS ) { 57 int index = mWorkspace.indexOfChild(host); 58 mWorkspace.setCurrentPage(index); 59 } else if (action == MOVE_FORWARD) { 60 movePage(mWorkspace.indexOfChild(host) + 1, host); 61 return true; 62 } else if (action == MOVE_BACKWARD) { 63 movePage(mWorkspace.indexOfChild(host) - 1, host); 64 return true; 65 } 66 } 67 68 return super.performAccessibilityAction(host, action, args); 69 } 70 71 private void movePage(int finalIndex, View view) { 72 mWorkspace.onStartReordering(); 73 mWorkspace.removeView(view); 74 mWorkspace.addView(view, finalIndex); 75 mWorkspace.onEndReordering(); 76 mWorkspace.announceForAccessibility(mWorkspace.getContext().getText(R.string.screen_moved)); 77 78 mWorkspace.updateAccessibilityFlags(); 79 view.performAccessibilityAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null); 80 } 81 82 @Override 83 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { 84 super.onInitializeAccessibilityNodeInfo(host, info); 85 86 int index = mWorkspace.indexOfChild(host); 87 if (index < mWorkspace.getChildCount() - 1) { 88 info.addAction(mActions.get(MOVE_FORWARD)); 89 } 90 91 int startIndex = mWorkspace.numCustomPages() + (FeatureFlags.QSB_ON_FIRST_SCREEN ? 1 : 0); 92 if (index > startIndex) { 93 info.addAction(mActions.get(MOVE_BACKWARD)); 94 } 95 } 96} 97