blob: 5a120ccc2f9ed339bf08371eb5f04a5c4aac296b [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
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 */
16package com.android.wallpaper.compat;
17
18import android.app.WallpaperManager;
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.Rect;
22import android.graphics.drawable.Drawable;
23import android.os.ParcelFileDescriptor;
24import android.support.annotation.IntDef;
25
26import java.io.IOException;
27import 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 */
34public 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}