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.widget.ImageView;
23 
24 import com.bumptech.glide.Glide;
25 import com.bumptech.glide.load.Key;
26 import com.bumptech.glide.load.MultiTransformation;
27 import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
28 import com.bumptech.glide.load.resource.bitmap.FitCenter;
29 import com.bumptech.glide.request.RequestOptions;
30 
31 /**
32  * Image asset representing a Partner stub APK resource.
33  */
34 public final class SystemStaticAsset extends ResourceAsset {
35     private final String mResName;
36 
37     /**
38      * @param res   Resources containing the asset.
39      * @param resId Resource ID referencing the asset.
40      */
SystemStaticAsset(Resources res, int resId, String resName)41     public SystemStaticAsset(Resources res, int resId, String resName) {
42         super(res, resId);
43         mResName = resName;
44     }
45 
46     @Override
getKey()47     public Key getKey() {
48         if (mKey == null) {
49             mKey = new PackageResourceKey(mRes, mResId, mResName);
50         }
51         return mKey;
52     }
53 
54     @Override
loadLowResDrawable(Activity activity, ImageView imageView, int placeholderColor, BitmapTransformation transformation)55     public void loadLowResDrawable(Activity activity, ImageView imageView, int placeholderColor,
56             BitmapTransformation transformation) {
57         MultiTransformation<Bitmap> multiTransformation =
58                 new MultiTransformation<>(new FitCenter(), transformation);
59         Glide.with(activity)
60                 .asDrawable()
61                 .load(this)
62                 .apply(RequestOptions.bitmapTransform(multiTransformation)
63                         .placeholder(new ColorDrawable(placeholderColor)))
64                 .into(imageView);
65     }
66 
67     /**
68      * Glide caching key for resources from System stub APK.
69      */
70     private static class PackageResourceKey extends ResourceAsset.PackageResourceKey {
71         private String mResName;
72 
PackageResourceKey(Resources res, int resId, String resName)73         PackageResourceKey(Resources res, int resId, String resName) {
74             super(res, resId);
75             mResName = resName;
76         }
77 
78         @Override
getCacheKey()79         protected String getCacheKey() {
80             return "PackageResourceKey{"
81                     + "packageName=" + mPackageName
82                     + ",resId=" + mResId
83                     + ",resName=" + mResName
84                     + '}';
85         }
86     }
87 }
88