1package com.android.launcher3;
2
3import android.app.AlertDialog;
4import android.app.WallpaperManager;
5import android.content.Context;
6import android.content.DialogInterface;
7import android.graphics.Rect;
8import android.os.AsyncTask;
9
10import java.io.IOException;
11import java.io.InputStream;
12
13/**
14 * Utility class used to help set lockscreen wallpapers on N+.
15 */
16public 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     */
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
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
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}