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 com.example.android.supportv7.widget.selection.fancy; 18 19import android.net.Uri; 20 21import androidx.annotation.Nullable; 22import androidx.recyclerview.selection.ItemKeyProvider; 23 24import java.util.HashMap; 25import java.util.Map; 26 27class ContentUriKeyProvider extends ItemKeyProvider<Uri> { 28 29 private final Uri[] mUris; 30 private final Map<Uri, Integer> mPositions; 31 32 ContentUriKeyProvider(String authority, String[] values) { 33 // Advise the world we can supply ids/position for entire copus 34 // at any time. 35 super(SCOPE_MAPPED); 36 37 mUris = new Uri[values.length]; 38 mPositions = new HashMap<>(); 39 40 for (int i = 0; i < values.length; i++) { 41 mUris[i] = new Uri.Builder() 42 .scheme("content") 43 .encodedAuthority(authority) 44 .appendPath(values[i]) 45 .build(); 46 mPositions.put(mUris[i], i); 47 } 48 } 49 50 @Override 51 public @Nullable Uri getKey(int position) { 52 return mUris[position]; 53 } 54 55 @Override 56 public int getPosition(Uri key) { 57 return mPositions.get(key); 58 } 59} 60