1 /*
2  * Copyright 2017 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 androidx.recyclerview.selection;
18 
19 import androidx.recyclerview.selection.ItemDetailsLookup.ItemDetails;
20 import androidx.recyclerview.widget.RecyclerView;
21 
22 import org.jspecify.annotations.NonNull;
23 
24 /**
25  * Override methods in this class to provide application specific behaviors
26  * related to focusing item.
27  *
28  * @param <K> Selection key type. @see {@link StorageStrategy} for supported types.
29  */
30 public abstract class FocusDelegate<K> {
31 
stub()32     static <K> FocusDelegate<K> stub() {
33         return new FocusDelegate<K>() {
34             @Override
35             public void focusItem(@NonNull ItemDetails<K> item) {
36             }
37 
38             @Override
39             public boolean hasFocusedItem() {
40                 return false;
41             }
42 
43             @Override
44             public int getFocusedPosition() {
45                 return RecyclerView.NO_POSITION;
46             }
47 
48             @Override
49             public void clearFocus() {
50             }
51         };
52     }
53 
54     /**
55      * If environment supports focus, focus {@code item}.
56      */
57     public abstract void focusItem(@NonNull ItemDetails<K> item);
58 
59     /**
60      * @return true if there is a focused item.
61      */
62     public abstract boolean hasFocusedItem();
63 
64     /**
65      * Returns the position of the currently focused item, or
66      * {@link RecyclerView#NO_POSITION} if nothing is focused.
67      *
68      * <p>You must implement this feature if you intend your app
69      * to work well with mouse and keyboard. Selection
70      * ranges are inferred from focused item when there is
71      * no explicit last-selected item.
72      *
73      * <p>You can manage and advance focus using keyboard arrows,
74      * reflecting this state visibly in the view item.
75      * Use can then press shift, then click another item with
76      * their mouse to select all items between the focused
77      * item and the clicked item.
78      *
79      * @return the position of the currently focused item,
80      *     or {@code RecyclerView#NO_POSITION} if none.
81      */
82     public abstract int getFocusedPosition();
83 
84     /**
85      * If the environment supports focus and something is focused, unfocus it.
86      */
87     public abstract void clearFocus();
88 }
89