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.app.Activity; 19 import android.content.res.Resources; 20 import android.graphics.Bitmap; 21 import android.graphics.drawable.ColorDrawable; 22 import android.os.FileUtils; 23 import android.widget.ImageView; 24 25 import com.bumptech.glide.Glide; 26 import com.bumptech.glide.load.Key; 27 import com.bumptech.glide.load.MultiTransformation; 28 import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; 29 import com.bumptech.glide.load.resource.bitmap.FitCenter; 30 import com.bumptech.glide.request.RequestOptions; 31 32 import java.io.File; 33 import java.io.FileOutputStream; 34 import java.io.IOException; 35 import java.io.InputStream; 36 import java.io.OutputStream; 37 38 /** 39 * Image asset representing a Partner stub APK resource. 40 */ 41 public final class SystemStaticAsset extends ResourceAsset { 42 private final String mResName; 43 44 /** 45 * @param res Resources containing the asset. 46 * @param resId Resource ID referencing the asset. 47 * @param isThumbnail Indicates this resource is specific for thumbnail. 48 */ SystemStaticAsset(Resources res, int resId, String resName, boolean isThumbnail)49 public SystemStaticAsset(Resources res, int resId, String resName, boolean isThumbnail) { 50 super(res, resId, isThumbnail); 51 mResName = resName; 52 } 53 54 @Override getKey()55 public Key getKey() { 56 if (mKey == null) { 57 mKey = new PackageResourceKey(mRes, mResId, mResName); 58 } 59 return mKey; 60 } 61 62 @Override loadLowResDrawable(Activity activity, ImageView imageView, int placeholderColor, BitmapTransformation transformation)63 public void loadLowResDrawable(Activity activity, ImageView imageView, int placeholderColor, 64 BitmapTransformation transformation) { 65 MultiTransformation<Bitmap> multiTransformation = 66 new MultiTransformation<>(new FitCenter(), transformation); 67 Glide.with(activity) 68 .asDrawable() 69 .load(this) 70 .apply(RequestOptions.bitmapTransform(multiTransformation) 71 .placeholder(new ColorDrawable(placeholderColor))) 72 .into(imageView); 73 } 74 75 76 @Override copy(File dest)77 public void copy(File dest) { 78 super.copy(dest); 79 try (InputStream inputStream = openInputStream(); 80 OutputStream outputStream = new FileOutputStream(dest)) { 81 FileUtils.copy(inputStream, outputStream); 82 } catch (IOException e) { 83 throw new RuntimeException(e); 84 } 85 } 86 87 /** 88 * Glide caching key for resources from System stub APK. 89 */ 90 private static class PackageResourceKey extends ResourceAsset.PackageResourceKey { 91 private String mResName; 92 PackageResourceKey(Resources res, int resId, String resName)93 PackageResourceKey(Resources res, int resId, String resName) { 94 super(res, resId); 95 mResName = resName; 96 } 97 98 @Override getCacheKey()99 protected String getCacheKey() { 100 return "PackageResourceKey{" 101 + "packageName=" + mPackageName 102 + ",resId=" + mResId 103 + ",resName=" + mResName 104 + '}'; 105 } 106 } 107 } 108