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