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