1 /**
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * ```
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * ```
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.healthconnect.controller.permissiontypes.prioritylist
17 
18 import android.content.Context
19 import android.os.Bundle
20 import android.view.View
21 import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
22 import androidx.recyclerview.widget.LinearLayoutManager
23 import androidx.recyclerview.widget.RecyclerView
24 import com.android.healthconnect.controller.R
25 
26 /** Linear layout manager that adds accessibility actions for reordering the priority list. */
27 class PriorityListLinearLayoutManager(context: Context?, private val adapter: PriorityListAdapter) :
28     LinearLayoutManager(context) {
onInitializeAccessibilityNodeInfoForItemnull29     override fun onInitializeAccessibilityNodeInfoForItem(
30         recycler: RecyclerView.Recycler,
31         state: RecyclerView.State,
32         host: View,
33         info: AccessibilityNodeInfoCompat
34     ) {
35         super.onInitializeAccessibilityNodeInfoForItem(recycler, state, host, info)
36         val position = getPosition(host)
37         if (position > 0) {
38             info.addAction(
39                 AccessibilityNodeInfoCompat.AccessibilityActionCompat(
40                     R.id.action_drag_move_up,
41                     host.context.getString(R.string.action_drag_label_move_up)))
42         }
43         if (position < itemCount - 1) {
44             info.addAction(
45                 AccessibilityNodeInfoCompat.AccessibilityActionCompat(
46                     R.id.action_drag_move_down,
47                     host.context.getString(R.string.action_drag_label_move_down)))
48         }
49     }
50 
performAccessibilityActionForItemnull51     override fun performAccessibilityActionForItem(
52         recycler: RecyclerView.Recycler,
53         state: RecyclerView.State,
54         host: View,
55         action: Int,
56         args: Bundle?
57     ): Boolean {
58         val position = getPosition(host)
59         if (action == R.id.action_drag_move_up) {
60             adapter.onItemMove(position, position - 1)
61             return true
62         }
63         if (action == R.id.action_drag_move_down) {
64             adapter.onItemMove(position, position + 1)
65             return true
66         }
67         return false
68     }
69 }
70