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.module;
17 
18 import static android.app.WallpaperManager.FLAG_LOCK;
19 import static android.app.WallpaperManager.FLAG_SYSTEM;
20 import static android.app.WallpaperManager.SetWallpaperFlags;
21 
22 import android.graphics.Bitmap;
23 import android.graphics.Point;
24 import android.graphics.Rect;
25 
26 import androidx.annotation.IntDef;
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 
30 import com.android.wallpaper.asset.Asset;
31 import com.android.wallpaper.model.StaticWallpaperPrefMetadata;
32 import com.android.wallpaper.model.WallpaperInfo;
33 
34 import java.io.InputStream;
35 import java.util.List;
36 import java.util.Map;
37 
38 /**
39  * Interface for classes which persist wallpapers to the system.
40  */
41 public interface WallpaperPersister {
42 
43     int DEST_HOME_SCREEN = 0;
44     int DEST_LOCK_SCREEN = 1;
45     int DEST_BOTH = 2;
46 
47     /**
48      * Sets a static individual wallpaper to the system via the WallpaperManager.
49      *
50      * @param wallpaper   Wallpaper model object. Wallpaper image will be set from the asset provided
51      *                    to this method.
52      * @param asset       Wallpaper asset from which to retrieve image data.
53      * @param cropRect    Desired crop area of the wallpaper in post-scale units. If null, then the
54      *                    wallpaper image will be set without any scaling or cropping.
55      * @param scale       Scaling factor applied to the source image before setting the wallpaper to the
56      *                    device.
57      * @param destination The destination - where to set the wallpaper to.
58      * @param callback    Called once the wallpaper was set or if an error occurred.
59      */
setIndividualWallpaper(WallpaperInfo wallpaper, Asset asset, @Nullable Rect cropRect, float scale, @Destination int destination, SetWallpaperCallback callback)60     void setIndividualWallpaper(WallpaperInfo wallpaper, Asset asset, @Nullable Rect cropRect,
61                                 float scale, @Destination int destination, SetWallpaperCallback callback);
62 
63     /**
64      * Sets an individual wallpaper to the system as the wallpaper in the current rotation along with
65      * its metadata. Prevents automatic wallpaper backup to conserve user data.
66      * <p>
67      * This method should only be called off the main UI thread because it will compress and set the
68      * bitmap on the same thread as the caller.
69      *
70      * @param wallpaperBitmap Cropped and scaled wallpaper bitmap. This bitmap will be persisted as-is
71      *                        with no additional processing.
72      * @param attributions    List of attribution items.
73      * @param actionUrl       The action or "explore" URL for the wallpaper.
74      * @param collectionId    ID of this wallpaper's collection.
75      * @param remoteId        Remote ID of this wallpaper
76      * @return Whether the set wallpaper operation was successful.
77      */
setWallpaperInRotation(Bitmap wallpaperBitmap, List<String> attributions, String actionUrl, String collectionId, String remoteId)78     boolean setWallpaperInRotation(Bitmap wallpaperBitmap, List<String> attributions,
79             String actionUrl, String collectionId, String remoteId);
80 
81     /**
82      * Sets only the bitmap of a rotating wallpaper of the next rotation to the system and stores
83      * the given static wallpaper data in the recent wallpapers list (and not metadata).
84      * This function is used for the very first time setting the rotation wallpaper. It is called
85      * after the user initiates rotation wallpaper and the first wallpaper is downloaded.
86      *
87      * @param wallpaperBitmap The rotating wallpaper's bitmap.
88      * @param attributions List of attribution items.
89      * @param actionUrl    The action or "explore" URL for the wallpaper.
90      * @param collectionId ID of this wallpaper's collection.
91      * @return wallpaper ID, which is a positive integer if the set wallpaper operation was
92      * successful, or 0 otherwise. On Android versions prior to N, this method will always return
93      * 1 if the operation was successful because wallpaper IDs are not supported prior to N.
94      */
setWallpaperBitmapInNextRotation(Bitmap wallpaperBitmap, List<String> attributions, String actionUrl, String collectionId)95     int setWallpaperBitmapInNextRotation(Bitmap wallpaperBitmap, List<String> attributions,
96             String actionUrl, String collectionId);
97 
98     /**
99      * Persists rotating wallpaper metadata for the next rotation and finalizes the preview wallpaper
100      * image so that it's visible as the actual device wallpaper.
101      *
102      * @param attributions List of attribution items.
103      * @param actionUrl    The action or "explore" URL for the wallpaper.
104      * @param collectionId ID of this wallpaper's collection.
105      * @param wallpaperId  Wallpaper ID that on Android N and later uniquely identifies the wallpaper
106      *                     image.
107      * @param remoteId     Remote ID of this wallpaper.
108      * @return Whether the operation succeeded.
109      */
finalizeWallpaperForNextRotation(List<String> attributions, String actionUrl, String collectionId, int wallpaperId, String remoteId)110     boolean finalizeWallpaperForNextRotation(List<String> attributions, String actionUrl,
111             String collectionId, int wallpaperId, String remoteId);
112 
113     /**
114      * Finalizes wallpaper metadata by persisting them to SharedPreferences and finalizes the
115      * wallpaper image for live rotating components by copying the "preview" image to the "final"
116      * image file location.
117      *
118      * @param attributions      List of attribution items.
119      * @param actionUrl         The action or "explore" URL for the wallpaper.
120      * @param collectionId      ID of this wallpaper's collection.
121      * @param wallpaperId       ID that uniquely identifies a wallpaper set to the
122      *                          {@link android.app.WallpaperManager}.
123      * @param remoteId          Remote ID of this wallpaper.
124      * @param destination       Lock, home screen or both.
125      * @return Whether the operation was successful.
126      */
saveStaticWallpaperMetadata(List<String> attributions, String actionUrl, String collectionId, int wallpaperId, String remoteId, @Destination int destination )127     boolean saveStaticWallpaperMetadata(List<String> attributions,
128             String actionUrl,
129             String collectionId,
130             int wallpaperId,
131             String remoteId,
132             @Destination int destination
133         );
134 
135     /**
136      * Save static image wallpaper's meta to the system preferences.
137      */
saveStaticWallpaperToPreferences(int destination, StaticWallpaperPrefMetadata metadata)138     boolean saveStaticWallpaperToPreferences(int destination,
139             StaticWallpaperPrefMetadata metadata);
140 
141     /**
142      * @return the flag indicating which wallpaper to set when we're trying to set a wallpaper with
143      * no user intervention. The idea is that if there's a static wallpaper on lock, we will only
144      * override home, otherwise both
145      */
getDefaultWhichWallpaper()146     int getDefaultWhichWallpaper();
147 
148     /**
149      * Sets a wallpaper bitmap to the {@link android.app.WallpaperManager}.
150      *
151      * @return an integer wallpaper ID. This is an actual wallpaper ID on N and later versions of
152      * Android, otherwise on pre-N versions of Android will return a positive integer when the
153      * operation was successful and zero if the operation encountered an error.
154      */
setBitmapToWallpaperManager(Bitmap wallpaperBitmap, Rect cropHint, boolean allowBackup, int whichWallpaper)155     int setBitmapToWallpaperManager(Bitmap wallpaperBitmap, Rect cropHint,
156             boolean allowBackup, int whichWallpaper);
157 
158     /**
159      * Sets a wallpaper stream to the {@link android.app.WallpaperManager}.
160      *
161      * @return an integer wallpaper ID. This is an actual wallpaper ID on N and later versions of
162      * Android, otherwise on pre-N versions of Android will return a positive integer when the
163      * operation was successful and zero if the operation encountered an error.
164      */
setStreamToWallpaperManager(InputStream inputStream, @Nullable Rect cropHint, boolean allowBackup, int whichWallpaper)165     int setStreamToWallpaperManager(InputStream inputStream, @Nullable Rect cropHint,
166             boolean allowBackup, int whichWallpaper);
167 
168     /**
169      * Sets a wallpaper stream to the {@link android.app.WallpaperManager}.
170      *
171      * @return an integer wallpaper ID. This is an actual wallpaper ID on N and later versions of
172      * Android, otherwise on pre-N versions of Android will return a positive integer when the
173      * operation was successful and zero if the operation encountered an error.
174      */
setStreamWithCropsToWallpaperManager(InputStream inputStream, @NonNull Map<Point, Rect> cropModel, boolean allowBackup, int whichWallpaper)175     int setStreamWithCropsToWallpaperManager(InputStream inputStream,
176             @NonNull Map<Point, Rect> cropModel,
177             boolean allowBackup, int whichWallpaper);
178 
179     /**
180      * Saves the last wallpaper which showed a preview from this app.
181      */
setWallpaperInfoInPreview(WallpaperInfo wallpaper)182     void setWallpaperInfoInPreview(WallpaperInfo wallpaper);
183 
184     /**
185      * Saves attributions to WallpaperPreferences for the last previewed wallpaper if it has an
186      * {@link android.app.WallpaperInfo} component matching the one currently set to the
187      * {@link android.app.WallpaperManager}.
188      *
189      * @param destination Live wallpaper destination (home/lock/both)
190      */
onLiveWallpaperSet(@estination int destination)191     void onLiveWallpaperSet(@Destination int destination);
192 
193     /**
194      * Updates lie wallpaper metadata by persisting them to SharedPreferences.
195      *
196      * @param wallpaperInfo Wallpaper model for the live wallpaper
197      * @param effects Comma-separate list of effect (see {@link WallpaperInfo#getEffectNames})
198      * @param destination Live wallpaper destination (home/lock/both)
199      */
setLiveWallpaperMetadata(WallpaperInfo wallpaperInfo, String effects, @Destination int destination)200     void setLiveWallpaperMetadata(WallpaperInfo wallpaperInfo, String effects,
201             @Destination int destination);
202 
203     /**
204      * Interface for tracking success or failure of set wallpaper operations.
205      */
206     interface SetWallpaperCallback {
onSuccess(WallpaperInfo wallpaperInfo, @Destination int destination)207         void onSuccess(WallpaperInfo wallpaperInfo, @Destination int destination);
208 
onError(@ullable Throwable throwable)209         void onError(@Nullable Throwable throwable);
210     }
211 
212     /**
213      * The possible destinations to which a wallpaper may be set.
214      */
215     @IntDef({
216             DEST_HOME_SCREEN,
217             DEST_LOCK_SCREEN,
218             DEST_BOTH})
219     @interface Destination {
220     }
221 
222     /**
223      * Converts a {@link Destination} to the corresponding set of {@link SetWallpaperFlags}.
224      */
225     @SetWallpaperFlags
destinationToFlags(@estination int destination)226     static int destinationToFlags(@Destination int destination) {
227         switch (destination) {
228             case DEST_HOME_SCREEN:
229                 return FLAG_SYSTEM;
230             case DEST_LOCK_SCREEN:
231                 return FLAG_LOCK;
232             case DEST_BOTH:
233                 return FLAG_SYSTEM | FLAG_LOCK;
234             default:
235                 throw new AssertionError("Unknown @Destination");
236         }
237     }
238 
239     /**
240      * Converts a set of {@link SetWallpaperFlags} to the corresponding {@link Destination}.
241      */
242     @Destination
flagsToDestination(@etWallpaperFlags int flags)243     static int flagsToDestination(@SetWallpaperFlags int flags) {
244         if (flags == (FLAG_SYSTEM | FLAG_LOCK)) {
245             return DEST_BOTH;
246         } else if (flags == FLAG_SYSTEM) {
247             return DEST_HOME_SCREEN;
248         } else if (flags == FLAG_LOCK) {
249             return DEST_LOCK_SCREEN;
250         } else {
251             throw new AssertionError("Unknown @SetWallpaperFlags value");
252         }
253     }
254 }
255