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