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.annotation.SuppressLint; 19 import android.app.WallpaperManager; 20 import android.content.Context; 21 import android.graphics.Bitmap; 22 import android.graphics.Rect; 23 import android.graphics.drawable.Drawable; 24 import android.os.ParcelFileDescriptor; 25 26 import java.io.IOException; 27 import 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 */ 33 public class WallpaperManagerCompatV16 extends WallpaperManagerCompat { 34 protected WallpaperManager mWallpaperManager; 35 36 @SuppressLint("ServiceCast") WallpaperManagerCompatV16(Context context)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 setStream(InputStream data, Rect visibleCropHint, boolean allowBackup, int whichWallpaper)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 setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, int whichWallpaper)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 getWallpaperId(@allpaperLocation int whichWallpaper)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 getWallpaperFile(int whichWallpaper)66 public ParcelFileDescriptor getWallpaperFile(int whichWallpaper) { 67 return null; 68 } 69 70 @Override getDrawable()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 } 85