1 /* 2 * Copyright (C) 2016 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.settings.localepicker; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.view.View; 22 23 import androidx.core.view.accessibility.AccessibilityNodeInfoCompat; 24 import androidx.recyclerview.widget.LinearLayoutManager; 25 import androidx.recyclerview.widget.RecyclerView; 26 27 import com.android.settings.R; 28 29 /** 30 * Add accessibility actions to the drag-and-drop locale list 31 * 32 * <p>Dragging is not supported neither by TalkBack or the accessibility 33 * framework at the moment. So we need custom actions to be able 34 * to change the order of the locales.</p> 35 * 36 * <p>Also, the remove functionality is difficult to discover and use 37 * with TalkBack only, so we are also adding a "remove" action.</p> 38 * 39 * <p>It only removes one locale at the time, but most users don't 40 * really add many locales "by mistake", so there is no real need 41 * to delete a lot of locales at once.</p> 42 */ 43 public class LocaleLinearLayoutManager extends LinearLayoutManager { 44 private final LocaleDragAndDropAdapter mAdapter; 45 private final Context mContext; 46 47 private final AccessibilityNodeInfoCompat.AccessibilityActionCompat mActionMoveUp; 48 private final AccessibilityNodeInfoCompat.AccessibilityActionCompat mActionMoveDown; 49 private final AccessibilityNodeInfoCompat.AccessibilityActionCompat mActionMoveTop; 50 private final AccessibilityNodeInfoCompat.AccessibilityActionCompat mActionMoveBottom; 51 private final AccessibilityNodeInfoCompat.AccessibilityActionCompat mActionRemove; 52 LocaleLinearLayoutManager(Context context, LocaleDragAndDropAdapter adapter)53 public LocaleLinearLayoutManager(Context context, LocaleDragAndDropAdapter adapter) { 54 super(context); 55 this.mContext = context; 56 this.mAdapter = adapter; 57 58 this.mActionMoveUp = new AccessibilityNodeInfoCompat.AccessibilityActionCompat( 59 R.id.action_drag_move_up, 60 mContext.getString(R.string.action_drag_label_move_up)); 61 this.mActionMoveDown = new AccessibilityNodeInfoCompat.AccessibilityActionCompat( 62 R.id.action_drag_move_down, 63 mContext.getString(R.string.action_drag_label_move_down)); 64 this.mActionMoveTop = new AccessibilityNodeInfoCompat.AccessibilityActionCompat( 65 R.id.action_drag_move_top, 66 mContext.getString(R.string.action_drag_label_move_top)); 67 this.mActionMoveBottom = new AccessibilityNodeInfoCompat.AccessibilityActionCompat( 68 R.id.action_drag_move_bottom, 69 mContext.getString(R.string.action_drag_label_move_bottom)); 70 this.mActionRemove = new AccessibilityNodeInfoCompat.AccessibilityActionCompat( 71 R.id.action_drag_remove, 72 mContext.getString(R.string.action_drag_label_remove)); 73 } 74 75 @Override onInitializeAccessibilityNodeInfoForItem(RecyclerView.Recycler recycler, RecyclerView.State state, View host, AccessibilityNodeInfoCompat info)76 public void onInitializeAccessibilityNodeInfoForItem(RecyclerView.Recycler recycler, 77 RecyclerView.State state, View host, AccessibilityNodeInfoCompat info) { 78 79 super.onInitializeAccessibilityNodeInfoForItem(recycler, state, host, info); 80 81 final int itemCount = this.getItemCount(); 82 final int position = this.getPosition(host); 83 final LocaleDragCell dragCell = (LocaleDragCell) host; 84 85 // We want the description to be something not localizable, so that any TTS engine for 86 // any language can handle it. And we want the position to be part of it. 87 // So we use something like "2, French (France)" 88 final String description = 89 (position + 1) + ", " + dragCell.getCheckbox().getContentDescription(); 90 info.setContentDescription(description); 91 92 if (mAdapter.isRemoveMode()) { // We don't move things around in remove mode 93 return; 94 } 95 96 // The order in which we add the actions is important for the circular selection menu. 97 // With the current order the "up" action will be (more or less) up, and "down" more 98 // or less down ("more or less" because we have 5 actions) 99 if (position > 0) { // it is not the first one 100 info.addAction(mActionMoveUp); 101 info.addAction(mActionMoveTop); 102 } 103 if (position + 1 < itemCount) { // it is not the last one 104 info.addAction(mActionMoveDown); 105 info.addAction(mActionMoveBottom); 106 } 107 if (itemCount > 1) { 108 info.addAction(mActionRemove); 109 } 110 } 111 112 @Override performAccessibilityActionForItem(RecyclerView.Recycler recycler, RecyclerView.State state, View host, int action, Bundle args)113 public boolean performAccessibilityActionForItem(RecyclerView.Recycler recycler, 114 RecyclerView.State state, View host, int action, Bundle args) { 115 116 final int itemCount = this.getItemCount(); 117 final int position = this.getPosition(host); 118 boolean result = false; 119 120 switch (action) { 121 case R.id.action_drag_move_up: 122 if (position > 0) { 123 mAdapter.onItemMove(position, position - 1); 124 result = true; 125 } 126 break; 127 case R.id.action_drag_move_down: 128 if (position + 1 < itemCount) { 129 mAdapter.onItemMove(position, position + 1); 130 result = true; 131 } 132 break; 133 case R.id.action_drag_move_top: 134 if (position != 0) { 135 mAdapter.onItemMove(position, 0); 136 result = true; 137 } 138 break; 139 case R.id.action_drag_move_bottom: 140 if (position != itemCount - 1) { 141 mAdapter.onItemMove(position, itemCount - 1); 142 result = true; 143 } 144 break; 145 case R.id.action_drag_remove: 146 if (itemCount > 1) { 147 mAdapter.removeItem(position); 148 result = true; 149 } 150 break; 151 default: 152 return super.performAccessibilityActionForItem(recycler, state, host, action, args); 153 } 154 155 if (result) { 156 mAdapter.doTheUpdate(); 157 } 158 return result; 159 } 160 } 161