1 package com.android.wallpaperpicker.common; 2 3 import android.app.WallpaperManager; 4 import android.content.Context; 5 import android.graphics.Rect; 6 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.lang.reflect.InvocationTargetException; 10 import java.lang.reflect.Method; 11 12 public class WallpaperManagerCompatVN extends WallpaperManagerCompatV16 { 13 public WallpaperManagerCompatVN(Context context) { 14 super(context); 15 } 16 17 @Override 18 public void setStream(final InputStream data, Rect visibleCropHint, boolean allowBackup, 19 int whichWallpaper) throws IOException { 20 try { 21 // TODO: use mWallpaperManager.setStream(data, visibleCropHint, allowBackup, which) 22 // without needing reflection. 23 Method setStream = WallpaperManager.class.getMethod("setStream", InputStream.class, 24 Rect.class, boolean.class, int.class); 25 setStream.invoke(mWallpaperManager, data, visibleCropHint, allowBackup, whichWallpaper); 26 } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { 27 // Fall back to previous implementation (set both) 28 super.setStream(data, visibleCropHint, allowBackup, whichWallpaper); 29 } 30 } 31 32 @Override 33 public void clear(int whichWallpaper) throws IOException { 34 try { 35 // TODO: use mWallpaperManager.clear(whichWallpaper) without needing reflection. 36 Method clear = WallpaperManager.class.getMethod("clear", int.class); 37 clear.invoke(mWallpaperManager, whichWallpaper); 38 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { 39 // Fall back to previous implementation (set both) 40 super.clear(whichWallpaper); 41 } 42 } 43 } 44