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.WallpaperManager;
19 import android.content.Context;
20 import android.os.ParcelFileDescriptor;
21 import android.os.ParcelFileDescriptor.AutoCloseInputStream;
22 import android.util.Log;
23 import android.widget.ImageView;
24 
25 import com.android.wallpaper.compat.WallpaperManagerCompat;
26 import com.android.wallpaper.compat.WallpaperManagerCompat.WallpaperLocation;
27 import com.bumptech.glide.Glide;
28 import com.bumptech.glide.load.Key;
29 import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
30 import com.bumptech.glide.request.RequestOptions;
31 
32 import java.io.InputStream;
33 import java.security.MessageDigest;
34 
35 /**
36  * Asset representing the currently-set image wallpaper on N+ devices, including when daily rotation
37  * is set with a static wallpaper (but not when daily rotation uses a live wallpaper).
38  */
39 public class CurrentWallpaperAssetVN extends StreamableAsset {
40 
41     private static final String TAG = "CurrentWallpaperAssetVN";
42     int mWallpaperId;
43     private WallpaperManager mWallpaperManager;
44     private WallpaperManagerCompat mWallpaperManagerCompat;
45     @WallpaperLocation
46     private int mWallpaperManagerFlag;
47 
CurrentWallpaperAssetVN(Context context, @WallpaperLocation int wallpaperManagerFlag)48     public CurrentWallpaperAssetVN(Context context, @WallpaperLocation int wallpaperManagerFlag) {
49         mWallpaperManager = WallpaperManager.getInstance(context);
50         mWallpaperManagerCompat = WallpaperManagerCompat.getInstance(context);
51         mWallpaperManagerFlag = wallpaperManagerFlag;
52         mWallpaperId = mWallpaperManagerCompat.getWallpaperId(mWallpaperManagerFlag);
53     }
54 
55     @Override
openInputStream()56     protected InputStream openInputStream() {
57         ParcelFileDescriptor pfd = mWallpaperManagerCompat.getWallpaperFile(mWallpaperManagerFlag);
58 
59         if (pfd == null) {
60             Log.e(TAG, "ParcelFileDescriptor for wallpaper " + mWallpaperManagerFlag + " is null, unable "
61                     + "to open InputStream.");
62             return null;
63         }
64 
65         return new AutoCloseInputStream(pfd);
66     }
67 
68     @Override
hashCode()69     public int hashCode() {
70         int result = 17;
71         result = result * 31 + mWallpaperManagerFlag;
72         result = result * 31 + mWallpaperId;
73         return result;
74     }
75 
76     @Override
equals(Object object)77     public boolean equals(Object object) {
78         if (object instanceof CurrentWallpaperAssetVN) {
79             CurrentWallpaperAssetVN otherAsset = (CurrentWallpaperAssetVN) object;
80             return otherAsset.mWallpaperManagerFlag == mWallpaperManagerFlag
81                     && otherAsset.mWallpaperId == mWallpaperId;
82 
83         }
84         return false;
85     }
86 
87     @Override
loadDrawable(Context context, ImageView imageView, int unusedPlaceholderColor)88     public void loadDrawable(Context context, ImageView imageView,
89                              int unusedPlaceholderColor) {
90         Glide.with(context)
91                 .asDrawable()
92                 .load(CurrentWallpaperAssetVN.this)
93                 .apply(RequestOptions.centerCropTransform())
94                 .transition(DrawableTransitionOptions.withCrossFade())
95                 .into(imageView);
96     }
97 
getKey()98     public Key getKey() {
99         return new CurrentWallpaperVNKey(mWallpaperManager, mWallpaperManagerFlag);
100     }
101 
getWallpaperPfd()102     ParcelFileDescriptor getWallpaperPfd() {
103         return mWallpaperManagerCompat.getWallpaperFile(mWallpaperManagerFlag);
104     }
105 
106     /**
107      * Glide caching key for currently-set wallpapers on Android N or later using wallpaper IDs
108      * provided by WallpaperManager.
109      */
110     private static final class CurrentWallpaperVNKey implements Key {
111         private WallpaperManager mWallpaperManager;
112         private int mWallpaperFlag;
113 
CurrentWallpaperVNKey(WallpaperManager wallpaperManager, @WallpaperLocation int wallpaperFlag)114         public CurrentWallpaperVNKey(WallpaperManager wallpaperManager,
115                                      @WallpaperLocation int wallpaperFlag) {
116             mWallpaperManager = wallpaperManager;
117             mWallpaperFlag = wallpaperFlag;
118         }
119 
120         @Override
toString()121         public String toString() {
122             return getCacheKey();
123         }
124 
125         @Override
hashCode()126         public int hashCode() {
127             return getCacheKey().hashCode();
128         }
129 
130         @Override
equals(Object object)131         public boolean equals(Object object) {
132             if (object instanceof CurrentWallpaperVNKey) {
133                 CurrentWallpaperVNKey otherKey = (CurrentWallpaperVNKey) object;
134                 return getCacheKey().equals(otherKey.getCacheKey());
135 
136             }
137             return false;
138         }
139 
140         @Override
updateDiskCacheKey(MessageDigest messageDigest)141         public void updateDiskCacheKey(MessageDigest messageDigest) {
142             messageDigest.update(getCacheKey().getBytes(CHARSET));
143         }
144 
145         /**
146          * Returns an inexpensively calculated {@link String} suitable for use as a disk cache key.
147          */
getCacheKey()148         private String getCacheKey() {
149             return "CurrentWallpaperVNKey{"
150                     + "flag=" + mWallpaperFlag
151                     + ",id=" + mWallpaperManager.getWallpaperId(mWallpaperFlag)
152                     + '}';
153         }
154     }
155 }
156