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 39 protected Key mKey; 40 41 /** 42 * @param res Resources containing the asset. 43 * @param resId Resource ID referencing the asset. 44 * @param requestOptions {@link RequestOptions} to be applied when loading the asset. 45 */ ResourceAsset(Resources res, int resId, RequestOptions requestOptions)46 public ResourceAsset(Resources res, int resId, RequestOptions requestOptions) { 47 mRes = res; 48 mResId = resId; 49 mRequestOptions = requestOptions; 50 } 51 52 /** 53 * @param res Resources containing the asset. 54 * @param resId Resource ID referencing the asset. 55 */ ResourceAsset(Resources res, int resId)56 public ResourceAsset(Resources res, int resId) { 57 this(res, resId, RequestOptions.centerCropTransform()); 58 } 59 60 @Override loadDrawable(Context context, ImageView imageView, int placeholderColor)61 public void loadDrawable(Context context, ImageView imageView, 62 int placeholderColor) { 63 Glide.with(context) 64 .asDrawable() 65 .load(ResourceAsset.this) 66 .apply(mRequestOptions 67 .placeholder(new ColorDrawable(placeholderColor))) 68 .transition(DrawableTransitionOptions.withCrossFade()) 69 .into(imageView); 70 } 71 72 @Override hashCode()73 public int hashCode() { 74 return getKey().hashCode(); 75 } 76 77 @Override equals(Object object)78 public boolean equals(Object object) { 79 if (object instanceof ResourceAsset) { 80 ResourceAsset otherAsset = (ResourceAsset) object; 81 return this.getKey().equals(otherAsset.getKey()); 82 } 83 return false; 84 } 85 86 /** 87 * Returns a Glide Key used to uniquely identify this asset as a data source in the cache. 88 */ getKey()89 public Key getKey() { 90 if (mKey == null) { 91 mKey = new PackageResourceKey(mRes, mResId); 92 } 93 return mKey; 94 } 95 96 /** 97 * Returns the Resources instance for the resource represented by this asset. 98 */ getResources()99 Resources getResources() { 100 return mRes; 101 } 102 103 /** 104 * Returns the resource ID for the resource represented by this asset. 105 */ getResId()106 int getResId() { 107 return mResId; 108 } 109 110 @Override openInputStream()111 protected InputStream openInputStream() { 112 return mRes.openRawResource(mResId); 113 } 114 115 /** 116 * Glide caching key for resources from any arbitrary package. 117 */ 118 protected static class PackageResourceKey implements Key { 119 protected String mPackageName; 120 protected int mResId; 121 PackageResourceKey(Resources res, int resId)122 public PackageResourceKey(Resources res, int resId) { 123 mPackageName = res.getResourcePackageName(resId); 124 mResId = resId; 125 } 126 127 @Override toString()128 public String toString() { 129 return getCacheKey(); 130 } 131 132 @Override hashCode()133 public int hashCode() { 134 return getCacheKey().hashCode(); 135 } 136 137 @Override equals(Object object)138 public boolean equals(Object object) { 139 if (object instanceof PackageResourceKey) { 140 PackageResourceKey otherKey = (PackageResourceKey) object; 141 return getCacheKey().equals(otherKey.getCacheKey()); 142 } 143 return false; 144 } 145 146 @Override updateDiskCacheKey(MessageDigest messageDigest)147 public void updateDiskCacheKey(MessageDigest messageDigest) { 148 messageDigest.update(getCacheKey().getBytes(CHARSET)); 149 } 150 151 /** 152 * Returns an inexpensively calculated {@link String} suitable for use as a disk cache key. 153 */ getCacheKey()154 protected String getCacheKey() { 155 return "PackageResourceKey{" 156 + "packageName=" + mPackageName 157 + ",resId=" + mResId 158 + '}'; 159 } 160 } 161 } 162