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 17package androidx.recyclerview.selection; 18 19import static androidx.core.util.Preconditions.checkArgument; 20 21import androidx.annotation.IntDef; 22import androidx.annotation.NonNull; 23import androidx.annotation.Nullable; 24import androidx.recyclerview.widget.RecyclerView; 25 26import java.lang.annotation.Retention; 27import java.lang.annotation.RetentionPolicy; 28 29/** 30 * Provides selection library access to stable selection keys identifying items 31 * presented by a {@link RecyclerView RecyclerView} instance. 32 * 33 * @param <K> Selection key type. @see {@link StorageStrategy} for supported types. 34 */ 35public abstract class ItemKeyProvider<K> { 36 37 /** 38 * Provides access to all data, regardless of whether it is bound to a view or not. 39 * Key providers with this access type enjoy support for enhanced features like: 40 * SHIFT+click range selection, and band selection. 41 */ 42 public static final int SCOPE_MAPPED = 0; 43 44 /** 45 * Provides access to cached data based for items that were recently bound in the view. 46 * Employing this provider will result in a reduced feature-set, as some 47 * features like SHIFT+click range selection and band selection are dependent 48 * on mapped access. 49 */ 50 public static final int SCOPE_CACHED = 1; 51 52 @IntDef({ 53 SCOPE_MAPPED, 54 SCOPE_CACHED 55 }) 56 @Retention(RetentionPolicy.SOURCE) 57 public @interface Scope {} 58 59 private final @Scope int mScope; 60 61 /** 62 * Creates a new provider with the given scope. 63 * 64 * @param scope Scope can't be changed at runtime. 65 */ 66 protected ItemKeyProvider(@Scope int scope) { 67 checkArgument(scope == SCOPE_MAPPED || scope == SCOPE_CACHED); 68 69 mScope = scope; 70 } 71 72 final boolean hasAccess(@Scope int scope) { 73 return scope == mScope; 74 } 75 76 /** 77 * @return The selection key at the given adapter position, or null. 78 */ 79 public abstract @Nullable K getKey(int position); 80 81 /** 82 * @return the position corresponding to the selection key, or RecyclerView.NO_POSITION. 83 */ 84 public abstract int getPosition(@NonNull K key); 85} 86