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