Home | History | Annotate | Download | only in tileinfo
      1 package com.android.wallpaperpicker.tileinfo;
      2 
      3 import android.graphics.Bitmap;
      4 import android.graphics.drawable.BitmapDrawable;
      5 import android.net.Uri;
      6 import android.os.AsyncTask;
      7 import android.util.Log;
      8 import android.view.View;
      9 import android.view.ViewGroup;
     10 import android.widget.Toast;
     11 
     12 import com.android.wallpaperpicker.common.CropAndSetWallpaperTask;
     13 import com.android.photos.BitmapRegionTileSource;
     14 import com.android.photos.BitmapRegionTileSource.BitmapSource;
     15 import com.android.wallpaperpicker.R;
     16 import com.android.wallpaperpicker.WallpaperPickerActivity;
     17 import com.android.wallpaperpicker.common.InputStreamProvider;
     18 
     19 public class UriWallpaperInfo extends DrawableThumbWallpaperInfo {
     20 
     21     private static final String TAG = "UriWallpaperInfo";
     22 
     23     public final Uri mUri;
     24 
     25     public UriWallpaperInfo(Uri uri) {
     26         super(null);
     27         mUri = uri;
     28     }
     29 
     30     @Override
     31     public void onClick(final WallpaperPickerActivity a) {
     32         a.setWallpaperButtonEnabled(false);
     33         final BitmapRegionTileSource.InputStreamSource bitmapSource =
     34                 new BitmapRegionTileSource.InputStreamSource(a, mUri);
     35         a.setCropViewTileSource(bitmapSource, true, false, null, new Runnable() {
     36 
     37             @Override
     38             public void run() {
     39                 if (bitmapSource.getLoadingState() == BitmapSource.State.LOADED) {
     40                     a.selectTile(mView);
     41                     a.setWallpaperButtonEnabled(true);
     42                 } else {
     43                     ViewGroup parent = (ViewGroup) mView.getParent();
     44                     if (parent != null) {
     45                         parent.removeView(mView);
     46                         Toast.makeText(a, R.string.image_load_fail,
     47                                 Toast.LENGTH_SHORT).show();
     48                     }
     49                 }
     50             }
     51         });
     52     }
     53 
     54     @Override
     55     public void onSave(final WallpaperPickerActivity a) {
     56         CropAndSetWallpaperTask.OnBitmapCroppedHandler h =
     57                 new CropAndSetWallpaperTask.OnBitmapCroppedHandler() {
     58             public void onBitmapCropped(byte[] imageBytes) {
     59                 // rotation is set to 0 since imageBytes has already been correctly rotated
     60                 Bitmap thumb = createThumbnail(
     61                         InputStreamProvider.fromBytes(imageBytes), a, 0, true);
     62                 a.getSavedImages().writeImage(thumb, imageBytes);
     63             }
     64         };
     65         boolean shouldFadeOutOnFinish = a.getWallpaperParallaxOffset() == 0f;
     66         a.cropImageAndSetWallpaper(mUri, h, shouldFadeOutOnFinish);
     67     }
     68 
     69     @Override
     70     public boolean isSelectable() {
     71         return true;
     72     }
     73 
     74     @Override
     75     public boolean isNamelessWallpaper() {
     76         return true;
     77     }
     78 
     79     public void loadThumbnaleAsync(final WallpaperPickerActivity activity) {
     80         mView.setVisibility(View.GONE);
     81         new AsyncTask<Void, Void, Bitmap>() {
     82             protected Bitmap doInBackground(Void...args) {
     83                 try {
     84                     InputStreamProvider isp = InputStreamProvider.fromUri(activity, mUri);
     85                     int rotation = isp.getRotationFromExif(activity);
     86                     return createThumbnail(isp, activity, rotation, false);
     87                 } catch (SecurityException securityException) {
     88                     if (activity.isActivityDestroyed()) {
     89                         // Temporarily granted permissions are revoked when the activity
     90                         // finishes, potentially resulting in a SecurityException here.
     91                         // Even though {@link #isDestroyed} might also return true in different
     92                         // situations where the configuration changes, we are fine with
     93                         // catching these cases here as well.
     94                         cancel(false);
     95                     } else {
     96                         // otherwise it had a different cause and we throw it further
     97                         throw securityException;
     98                     }
     99                     return null;
    100                 }
    101             }
    102             protected void onPostExecute(Bitmap thumb) {
    103                 if (!isCancelled() && thumb != null) {
    104                     setThumb(new BitmapDrawable(activity.getResources(), thumb));
    105                     mView.setVisibility(View.VISIBLE);
    106                 } else {
    107                     Log.e(TAG, "Error loading thumbnail for uri=" + mUri);
    108                 }
    109             }
    110         }.execute();
    111     }
    112 }