• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.Context;
20 import android.os.Bundle;
21 import android.util.SparseArray;
22 import android.view.View;
23 import android.view.View.AccessibilityDelegate;
24 import android.view.accessibility.AccessibilityNodeInfo;
25 import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
26 
27 import com.android.launcher3.R;
28 import com.android.launcher3.Utilities;
29 import com.android.launcher3.Workspace;
30 import com.android.launcher3.config.FeatureFlags;
31 
32 public 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 
OverviewScreenAccessibilityDelegate(Workspace workspace)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
performAccessibilityAction(View host, int action, Bundle args)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 
movePage(int finalIndex, View view)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
onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info)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