1 /* 2 * Copyright (C) 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 package com.android.wallpaper.asset; 17 18 import android.content.Context; 19 import android.content.res.Resources; 20 import android.graphics.drawable.ColorDrawable; 21 import android.widget.ImageView; 22 23 import com.bumptech.glide.Glide; 24 import com.bumptech.glide.load.Key; 25 import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions; 26 import com.bumptech.glide.request.RequestOptions; 27 28 import java.io.InputStream; 29 import java.security.MessageDigest; 30 31 /** 32 * Image asset representing an APK resource. 33 */ 34 public class ResourceAsset extends StreamableAsset { 35 protected final Resources mRes; 36 protected final int mResId; 37 private final RequestOptions mRequestOptions; 38 protected boolean mIsThumbnail; 39 40 protected Key mKey; 41 42 /** 43 * @param res Resources containing the asset. 44 * @param resId Resource ID referencing the asset. 45 * @param requestOptions {@link RequestOptions} to be applied when loading the asset. 46 * @param isThumbnail Indicates this resource is specific for thumbnail. 47 */ ResourceAsset(Resources res, int resId, RequestOptions requestOptions, boolean isThumbnail)48 public ResourceAsset(Resources res, int resId, RequestOptions requestOptions, 49 boolean isThumbnail) { 50 mRes = res; 51 mResId = resId; 52 mRequestOptions = requestOptions; 53 mIsThumbnail = isThumbnail; 54 } 55 56 /** 57 * @param res Resources containing the asset. 58 * @param resId Resource ID referencing the asset. 59 */ ResourceAsset(Resources res, int resId)60 public ResourceAsset(Resources res, int resId) { 61 this(res, resId, RequestOptions.centerCropTransform(), false); 62 } 63 64 /** 65 * @param res Resources containing the asset. 66 * @param resId Resource ID referencing the asset. 67 * @param isThumbnail Indicates this resource is specific for thumbnail. 68 */ ResourceAsset(Resources res, int resId, boolean isThumbnail)69 public ResourceAsset(Resources res, int resId, boolean isThumbnail) { 70 this(res, resId, RequestOptions.centerCropTransform(), isThumbnail); 71 } 72 73 @Override loadDrawable(Context context, ImageView imageView, int placeholderColor)74 public void loadDrawable(Context context, ImageView imageView, 75 int placeholderColor) { 76 Glide.with(context) 77 .asDrawable() 78 .load(ResourceAsset.this) 79 .apply(mRequestOptions 80 .placeholder(new ColorDrawable(placeholderColor))) 81 .transition(DrawableTransitionOptions.withCrossFade()) 82 .into(imageView); 83 } 84 85 @Override hashCode()86 public int hashCode() { 87 return getKey().hashCode(); 88 } 89 90 @Override equals(Object object)91 public boolean equals(Object object) { 92 if (object instanceof ResourceAsset) { 93 ResourceAsset otherAsset = (ResourceAsset) object; 94 return this.getKey().equals(otherAsset.getKey()); 95 } 96 return false; 97 } 98 99 /** 100 * Returns a Glide Key used to uniquely identify this asset as a data source in the cache. 101 */ getKey()102 public Key getKey() { 103 if (mKey == null) { 104 mKey = new PackageResourceKey(mRes, mResId); 105 } 106 return mKey; 107 } 108 109 /** 110 * Returns the Resources instance for the resource represented by this asset. 111 */ getResources()112 Resources getResources() { 113 return mRes; 114 } 115 116 /** 117 * Returns the resource ID for the resource represented by this asset. 118 */ getResId()119 int getResId() { 120 return mResId; 121 } 122 123 @Override openInputStream()124 protected InputStream openInputStream() { 125 return mRes.openRawResource(mResId); 126 } 127 128 /** 129 * Glide caching key for resources from any arbitrary package. 130 */ 131 protected static class PackageResourceKey implements Key { 132 protected String mPackageName; 133 protected int mResId; 134 PackageResourceKey(Resources res, int resId)135 public PackageResourceKey(Resources res, int resId) { 136 mPackageName = res.getResourcePackageName(resId); 137 mResId = resId; 138 } 139 140 @Override toString()141 public String toString() { 142 return getCacheKey(); 143 } 144 145 @Override hashCode()146 public int hashCode() { 147 return getCacheKey().hashCode(); 148 } 149 150 @Override equals(Object object)151 public boolean equals(Object object) { 152 if (object instanceof PackageResourceKey) { 153 PackageResourceKey otherKey = (PackageResourceKey) object; 154 return getCacheKey().equals(otherKey.getCacheKey()); 155 } 156 return false; 157 } 158 159 @Override updateDiskCacheKey(MessageDigest messageDigest)160 public void updateDiskCacheKey(MessageDigest messageDigest) { 161 messageDigest.update(getCacheKey().getBytes(CHARSET)); 162 } 163 164 /** 165 * Returns an inexpensively calculated {@link String} suitable for use as a disk cache key. 166 */ getCacheKey()167 protected String getCacheKey() { 168 return "PackageResourceKey{" 169 + "packageName=" + mPackageName 170 + ",resId=" + mResId 171 + '}'; 172 } 173 } 174 } 175