1 package com.android.wallpaperpicker.tileinfo; 2 3 import android.graphics.Point; 4 import android.graphics.drawable.Drawable; 5 import android.net.Uri; 6 import android.os.AsyncTask; 7 import android.util.Log; 8 import android.widget.Toast; 9 10 import com.android.gallery3d.common.Utils; 11 import com.android.photos.BitmapRegionTileSource; 12 import com.android.photos.BitmapRegionTileSource.BitmapSource; 13 import com.android.wallpaperpicker.R; 14 import com.android.wallpaperpicker.WallpaperPickerActivity; 15 import com.android.wallpaperpicker.common.DialogUtils; 16 import com.android.wallpaperpicker.common.InputStreamProvider; 17 import com.android.wallpaperpicker.common.WallpaperManagerCompat; 18 19 import java.io.File; 20 import java.io.IOException; 21 import java.io.InputStream; 22 23 public class FileWallpaperInfo extends DrawableThumbWallpaperInfo { 24 private static final String TAG = "FileWallpaperInfo"; 25 26 private final File mFile; 27 FileWallpaperInfo(File target, Drawable thumb)28 public FileWallpaperInfo(File target, Drawable thumb) { 29 super(thumb); 30 mFile = target; 31 } 32 33 @Override onClick(final WallpaperPickerActivity a)34 public void onClick(final WallpaperPickerActivity a) { 35 a.setWallpaperButtonEnabled(false); 36 final BitmapRegionTileSource.FilePathBitmapSource bitmapSource = 37 new BitmapRegionTileSource.FilePathBitmapSource(mFile, a); 38 a.setCropViewTileSource(bitmapSource, false, true, null, new Runnable() { 39 40 @Override 41 public void run() { 42 if (bitmapSource.getLoadingState() == BitmapSource.State.LOADED) { 43 a.setWallpaperButtonEnabled(true); 44 } 45 } 46 }); 47 } 48 49 @Override onSave(final WallpaperPickerActivity a)50 public void onSave(final WallpaperPickerActivity a) { 51 final InputStreamProvider isp = InputStreamProvider.fromUri(a, Uri.fromFile(mFile)); 52 AsyncTask<Integer, Void, Point> cropTask = new AsyncTask<Integer, Void, Point>() { 53 54 @Override 55 protected Point doInBackground(Integer... params) { 56 InputStream is = null; 57 try { 58 Point bounds = isp.getImageBounds(); 59 if (bounds == null) { 60 Log.w(TAG, "Error loading image bounds"); 61 return null; 62 } 63 is = isp.newStreamNotNull(); 64 WallpaperManagerCompat.getInstance(a).setStream(is, null, true, params[0]); 65 return bounds; 66 } catch (IOException e) { 67 Log.w(TAG, "cannot write stream to wallpaper", e); 68 } finally { 69 Utils.closeSilently(is); 70 } 71 return null; 72 } 73 74 @Override 75 protected void onPostExecute(Point bounds) { 76 if (bounds != null) { 77 a.setBoundsAndFinish(bounds, a.getWallpaperParallaxOffset() == 0f); 78 } else { 79 Toast.makeText(a, R.string.wallpaper_set_fail, Toast.LENGTH_SHORT).show(); 80 } 81 } 82 }; 83 84 DialogUtils.executeCropTaskAfterPrompt(a, cropTask, a.getOnDialogCancelListener()); 85 } 86 87 @Override isSelectable()88 public boolean isSelectable() { 89 return true; 90 } 91 92 @Override isNamelessWallpaper()93 public boolean isNamelessWallpaper() { 94 return true; 95 } 96 }