1 package com.android.launcher3; 2 3 import android.app.AlertDialog; 4 import android.app.WallpaperManager; 5 import android.content.Context; 6 import android.content.DialogInterface; 7 import android.graphics.Rect; 8 import android.os.AsyncTask; 9 10 import java.io.IOException; 11 import java.io.InputStream; 12 13 /** 14 * Utility class used to help set lockscreen wallpapers on N+. 15 */ 16 public class NycWallpaperUtils { 17 18 /** 19 * Calls cropTask.execute(), once the user has selected which wallpaper to set. On pre-N 20 * devices, the prompt is not displayed since there is no API to set the lockscreen wallpaper. 21 */ executeCropTaskAfterPrompt( Context context, final AsyncTask<Integer, ?, ?> cropTask, DialogInterface.OnCancelListener onCancelListener)22 public static void executeCropTaskAfterPrompt( 23 Context context, final AsyncTask<Integer, ?, ?> cropTask, 24 DialogInterface.OnCancelListener onCancelListener) { 25 if (Utilities.ATLEAST_N) { 26 new AlertDialog.Builder(context) 27 .setTitle(R.string.wallpaper_instructions) 28 .setItems(R.array.which_wallpaper_options, new DialogInterface.OnClickListener() { 29 @Override 30 public void onClick(DialogInterface dialog, int selectedItemIndex) { 31 int whichWallpaper; 32 if (selectedItemIndex == 0) { 33 whichWallpaper = WallpaperManager.FLAG_SYSTEM; 34 } else if (selectedItemIndex == 1) { 35 whichWallpaper = WallpaperManager.FLAG_LOCK; 36 } else { 37 whichWallpaper = WallpaperManager.FLAG_SYSTEM 38 | WallpaperManager.FLAG_LOCK; 39 } 40 cropTask.execute(whichWallpaper); 41 } 42 }) 43 .setOnCancelListener(onCancelListener) 44 .show(); 45 } else { 46 cropTask.execute(WallpaperManager.FLAG_SYSTEM); 47 } 48 } 49 setStream(Context context, final InputStream data, Rect visibleCropHint, boolean allowBackup, int whichWallpaper)50 public static void setStream(Context context, final InputStream data, Rect visibleCropHint, 51 boolean allowBackup, int whichWallpaper) throws IOException { 52 WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); 53 if (Utilities.ATLEAST_N) { 54 wallpaperManager.setStream(data, visibleCropHint, allowBackup, whichWallpaper); 55 } else { 56 // Fall back to previous implementation (set system) 57 wallpaperManager.setStream(data); 58 } 59 } 60 clear(Context context, int whichWallpaper)61 public static void clear(Context context, int whichWallpaper) throws IOException { 62 WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); 63 if (Utilities.ATLEAST_N) { 64 wallpaperManager.clear(whichWallpaper); 65 } else { 66 // Fall back to previous implementation (clear system) 67 wallpaperManager.clear(); 68 } 69 } 70 }