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