Home | History | Annotate | Download | only in common
      1 package com.android.wallpaperpicker.common;
      2 
      3 import android.app.AlertDialog;
      4 import android.content.Context;
      5 import android.content.DialogInterface;
      6 import android.os.AsyncTask;
      7 
      8 import com.android.wallpaperpicker.R;
      9 
     10 /**
     11  * Utility class used to show dialogs for things like picking which wallpaper to set.
     12  */
     13 public class DialogUtils {
     14     /**
     15      * Calls cropTask.execute(), once the user has selected which wallpaper to set. On pre-N
     16      * devices, the prompt is not displayed since there is no API to set the lockscreen wallpaper.
     17      *
     18      * TODO: Don't use CropAndSetWallpaperTask on N+, because the new API will handle cropping instead.
     19      */
     20     public static void executeCropTaskAfterPrompt(
     21             Context context, final AsyncTask<Integer, ?, ?> cropTask,
     22             DialogInterface.OnCancelListener onCancelListener) {
     23         if (Utilities.isAtLeastN()) {
     24             new AlertDialog.Builder(context)
     25                     .setTitle(R.string.wallpaper_instructions)
     26                     .setItems(R.array.which_wallpaper_options, new DialogInterface.OnClickListener() {
     27                     @Override
     28                     public void onClick(DialogInterface dialog, int selectedItemIndex) {
     29                         int whichWallpaper;
     30                         if (selectedItemIndex == 0) {
     31                             whichWallpaper = WallpaperManagerCompat.FLAG_SET_SYSTEM;
     32                         } else if (selectedItemIndex == 1) {
     33                             whichWallpaper = WallpaperManagerCompat.FLAG_SET_LOCK;
     34                         } else {
     35                             whichWallpaper = WallpaperManagerCompat.FLAG_SET_SYSTEM
     36                                     | WallpaperManagerCompat.FLAG_SET_LOCK;
     37                         }
     38                         cropTask.execute(whichWallpaper);
     39                     }
     40                 })
     41                 .setOnCancelListener(onCancelListener)
     42                 .show();
     43         } else {
     44             cropTask.execute(WallpaperManagerCompat.FLAG_SET_SYSTEM);
     45         }
     46     }
     47 }
     48