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 if (action == R.id.action_drag_move_up) { 121 if (position > 0) { 122 mAdapter.onItemMove(position, position - 1); 123 result = true; 124 } 125 } else if (action == R.id.action_drag_move_down) { 126 if (position + 1 < itemCount) { 127 mAdapter.onItemMove(position, position + 1); 128 result = true; 129 } 130 } else if (action == R.id.action_drag_move_top) { 131 if (position != 0) { 132 mAdapter.onItemMove(position, 0); 133 result = true; 134 } 135 } else if (action == R.id.action_drag_move_bottom) { 136 if (position != itemCount - 1) { 137 mAdapter.onItemMove(position, itemCount - 1); 138 result = true; 139 } 140 } else if (action == R.id.action_drag_remove) { 141 if (itemCount > 1) { 142 mAdapter.removeItem(position); 143 result = true; 144 } 145 } else { 146 return super.performAccessibilityActionForItem(recycler, state, host, action, args); 147 } 148 149 if (result) { 150 mAdapter.doTheUpdate(); 151 } 152 return result; 153 } 154 } 155