1 /*
2  * Copyright (C) 2020 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.internal.app;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 
22 import com.android.internal.widget.GridLayoutManager;
23 import com.android.internal.widget.RecyclerView;
24 
25 /**
26  * For a11y and per {@link RecyclerView#onInitializeAccessibilityNodeInfo}, override
27  * methods to ensure proper row counts.
28  */
29 public class ChooserGridLayoutManager extends GridLayoutManager {
30 
31     private boolean mVerticalScrollEnabled = true;
32 
33     /**
34      * Constructor used when layout manager is set in XML by RecyclerView attribute
35      * "layoutManager". If spanCount is not specified in the XML, it defaults to a
36      * single column.
37      *
38      */
ChooserGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)39     public ChooserGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr,
40             int defStyleRes) {
41         super(context, attrs, defStyleAttr, defStyleRes);
42     }
43 
44     /**
45      * Creates a vertical GridLayoutManager
46      *
47      * @param context   Current context, will be used to access resources.
48      * @param spanCount The number of columns in the grid
49      */
ChooserGridLayoutManager(Context context, int spanCount)50     public ChooserGridLayoutManager(Context context, int spanCount) {
51         super(context, spanCount);
52     }
53 
54     /**
55      * @param context       Current context, will be used to access resources.
56      * @param spanCount     The number of columns or rows in the grid
57      * @param orientation   Layout orientation. Should be {@link #HORIZONTAL} or {@link
58      *                      #VERTICAL}.
59      * @param reverseLayout When set to true, layouts from end to start.
60      */
ChooserGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout)61     public ChooserGridLayoutManager(Context context, int spanCount, int orientation,
62             boolean reverseLayout) {
63         super(context, spanCount, orientation, reverseLayout);
64     }
65 
66     @Override
getRowCountForAccessibility(RecyclerView.Recycler recycler, RecyclerView.State state)67     public int getRowCountForAccessibility(RecyclerView.Recycler recycler,
68             RecyclerView.State state) {
69         // Do not count the footer view in the official count
70         return super.getRowCountForAccessibility(recycler, state) - 1;
71     }
72 
setVerticalScrollEnabled(boolean verticalScrollEnabled)73     void setVerticalScrollEnabled(boolean verticalScrollEnabled) {
74         mVerticalScrollEnabled = verticalScrollEnabled;
75     }
76 
77     @Override
canScrollVertically()78     public boolean canScrollVertically() {
79         return mVerticalScrollEnabled && super.canScrollVertically();
80     }
81 }
82