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