1 /* 2 * Copyright (C) 2021 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.providers.media.photopicker.data.glide; 18 19 import android.net.Uri; 20 21 import androidx.annotation.NonNull; 22 import androidx.annotation.Nullable; 23 24 import com.android.providers.media.photopicker.data.model.Category; 25 import com.android.providers.media.photopicker.data.model.Item; 26 27 import com.bumptech.glide.signature.ObjectKey; 28 29 import java.util.Optional; 30 31 /** 32 * A data class to coalesce {@link Item} and {@link Category} into a common loadable glide object, 33 * with the relevant data required for Glide loading. 34 */ 35 public class GlideLoadable { 36 37 private final Optional<String> mCacheKey; 38 39 @NonNull private final Uri mUri; 40 GlideLoadable(@onNull Uri uri)41 public GlideLoadable(@NonNull Uri uri) { 42 this(uri, /* cacheKey= */ null); 43 } 44 GlideLoadable(@onNull Uri uri, @Nullable String cacheKey)45 public GlideLoadable(@NonNull Uri uri, @Nullable String cacheKey) { 46 this.mUri = uri; 47 this.mCacheKey = Optional.ofNullable(cacheKey); 48 } 49 50 /** 51 * Get a signature string to represent this item in the Glide cache. 52 * 53 * @param prefix Optional prefix to prepend to this item's signature. 54 * @return A glide cache signature string. 55 */ 56 @Nullable getLoadableSignature(@ullable String prefix)57 public ObjectKey getLoadableSignature(@Nullable String prefix) { 58 return new ObjectKey( 59 Optional.ofNullable(prefix).orElse("") + mUri.toString() + mCacheKey.orElse("")); 60 } 61 ; 62 63 /** 64 * @return A {@link Uri} object to locate the media for this loadable. 65 */ getLoadableUri()66 public Uri getLoadableUri() { 67 return mUri; 68 } 69 ; 70 } 71