blob: 8058c28a359867505461c6e0d83fa6a65044777e [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.annotation.SuppressLint;
19import android.app.WallpaperManager;
20import android.content.Context;
21import android.graphics.Bitmap;
22import android.graphics.Rect;
23import android.graphics.drawable.Drawable;
24import android.os.ParcelFileDescriptor;
25
26import java.io.IOException;
27import java.io.InputStream;
28
29/**
30 * The pre-N implementation of {@link WallpaperManagerCompat} which implements its methods by
31 * delegating to the standard pre-N equivalent methods.
32 */
33public class WallpaperManagerCompatV16 extends WallpaperManagerCompat {
34 protected WallpaperManager mWallpaperManager;
35
36 @SuppressLint("ServiceCast")
37 public WallpaperManagerCompatV16(Context context) {
38 // Retrieve WallpaperManager using Context#getSystemService instead of
39 // WallpaperManager#getInstance so it can be mocked out in test.
40 mWallpaperManager = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE);
41 }
42
43 @Override
44 public int setStream(InputStream data, Rect visibleCropHint, boolean allowBackup,
45 int whichWallpaper) throws IOException {
46 mWallpaperManager.setStream(data);
47 // Return a value greater than zero to indicate success.
48 return 1;
49 }
50
51 @Override
52 public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup,
53 int whichWallpaper) throws IOException {
54 mWallpaperManager.setBitmap(fullImage);
55 // Return a value greater than zero to indicate success.
56 return 1;
57 }
58
59 @Override
60 public int getWallpaperId(@WallpaperLocation int whichWallpaper) {
61 throw new UnsupportedOperationException("This method should not be called on pre-N versions "
62 + "of Android.");
63 }
64
65 @Override
66 public ParcelFileDescriptor getWallpaperFile(int whichWallpaper) {
67 return null;
68 }
69
70 @Override
71 public Drawable getDrawable() {
72 Drawable drawable;
73 try {
74 drawable = mWallpaperManager.getDrawable();
75 } catch (java.lang.Exception e) {
76 // Work around Samsung bug where SecurityException is thrown if device is still using its
77 // default wallpaper, and around Android 7.0 bug where SELinux issues can cause a perfectly
78 // valid access of the current wallpaper to cause a failed Binder transaction manifest here as
79 // a RuntimeException.
80 drawable = mWallpaperManager.getBuiltInDrawable();
81 }
82 return drawable;
83 }
84}