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.compat; 17 18 import android.app.WallpaperManager; 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.Rect; 22 import android.graphics.drawable.Drawable; 23 import android.os.Build; 24 import android.os.ParcelFileDescriptor; 25 26 import androidx.annotation.IntDef; 27 28 import java.io.IOException; 29 import java.io.InputStream; 30 31 /** 32 * An abstraction over WallpaperManager to allow for the transitional state in which the N SDK 33 * is not yet ready but we need to use new N API methods. Provides wrapper methods for the new 34 * N API methods. 35 */ 36 public abstract class WallpaperManagerCompat { 37 public static final int FLAG_SYSTEM = WallpaperManager.FLAG_SYSTEM; 38 public static final int FLAG_LOCK = WallpaperManager.FLAG_LOCK; 39 private static final Object sInstanceLock = new Object(); 40 private static WallpaperManagerCompat sInstance; 41 getInstance(Context context)42 public static WallpaperManagerCompat getInstance(Context context) { 43 synchronized (sInstanceLock) { 44 if (sInstance == null) { 45 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 46 sInstance = new WallpaperManagerCompatVN(context.getApplicationContext()); 47 } else { 48 sInstance = new WallpaperManagerCompatV16(context.getApplicationContext()); 49 } 50 } 51 return sInstance; 52 } 53 } 54 55 /** 56 * Sets the static instance of {@link WallpaperManagerCompat} as the provided object. Used for 57 * testing. 58 */ setInstance(WallpaperManagerCompat wallpaperManagerCompat)59 public static void setInstance(WallpaperManagerCompat wallpaperManagerCompat) { 60 synchronized (sInstanceLock) { 61 sInstance = wallpaperManagerCompat; 62 } 63 } 64 65 /** 66 * Thin wrapper around WallpaperManager's setStream method as defined in the N API. 67 */ setStream(InputStream stream, Rect visibleCropHint, boolean allowBackup, int whichWallpaper)68 public abstract int setStream(InputStream stream, Rect visibleCropHint, boolean allowBackup, 69 int whichWallpaper) throws IOException; 70 71 /** 72 * Thin wrapper around WallpaperManager's setBitmap method as defined in the N API. 73 */ setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, int whichWallpaper)74 public abstract int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, 75 int whichWallpaper) throws IOException; 76 77 /** 78 * Thin wrapper around WallpaperManager's getWallpaperId method as defined in the N API. 79 */ getWallpaperId(@allpaperLocation int whichWallpaper)80 public abstract int getWallpaperId(@WallpaperLocation int whichWallpaper); 81 82 /** 83 * Thin wrapper around WallpaperManager's getWallpaperFile method as defined ONLY in the N API. 84 * This method must only be called when N is detected on the device as there is no pre-N fallback 85 * counterpart! On pre-N devices, null is always returned. 86 */ getWallpaperFile(int whichWallpaper)87 public abstract ParcelFileDescriptor getWallpaperFile(int whichWallpaper); 88 89 /** 90 * Thin wrapper around WallpaperManager's getDrawable method. Needed to work around issue on 91 * certain Samsung devices where a SecurityException is thrown if this is called when the 92 * device had never changed from its default wallpaper. 93 */ getDrawable()94 public abstract Drawable getDrawable(); 95 96 /** 97 * Possible locations to which a wallpaper may be set. 98 */ 99 @IntDef({ 100 FLAG_SYSTEM, 101 FLAG_LOCK}) 102 public @interface WallpaperLocation { 103 } 104 } 105