mobile/android/thirdparty/com/squareup/picasso/MediaStoreBitmapHunter.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/com/squareup/picasso/MediaStoreBitmapHunter.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +/*
     1.5 + * Copyright (C) 2014 Square, Inc.
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +package com.squareup.picasso;
    1.20 +
    1.21 +import android.content.ContentResolver;
    1.22 +import android.content.Context;
    1.23 +import android.database.Cursor;
    1.24 +import android.graphics.Bitmap;
    1.25 +import android.graphics.BitmapFactory;
    1.26 +import android.net.Uri;
    1.27 +import android.provider.MediaStore;
    1.28 +import java.io.IOException;
    1.29 +
    1.30 +import static android.content.ContentUris.parseId;
    1.31 +import static android.provider.MediaStore.Images.Thumbnails.FULL_SCREEN_KIND;
    1.32 +import static android.provider.MediaStore.Images.Thumbnails.MICRO_KIND;
    1.33 +import static android.provider.MediaStore.Images.Thumbnails.MINI_KIND;
    1.34 +import static android.provider.MediaStore.Images.Thumbnails.getThumbnail;
    1.35 +import static com.squareup.picasso.MediaStoreBitmapHunter.PicassoKind.FULL;
    1.36 +import static com.squareup.picasso.MediaStoreBitmapHunter.PicassoKind.MICRO;
    1.37 +import static com.squareup.picasso.MediaStoreBitmapHunter.PicassoKind.MINI;
    1.38 +
    1.39 +class MediaStoreBitmapHunter extends ContentStreamBitmapHunter {
    1.40 +  private static final String[] CONTENT_ORIENTATION = new String[] {
    1.41 +      MediaStore.Images.ImageColumns.ORIENTATION
    1.42 +  };
    1.43 +
    1.44 +  MediaStoreBitmapHunter(Context context, Picasso picasso, Dispatcher dispatcher, Cache cache,
    1.45 +      Stats stats, Action action) {
    1.46 +    super(context, picasso, dispatcher, cache, stats, action);
    1.47 +  }
    1.48 +
    1.49 +  @Override Bitmap decode(Request data) throws IOException {
    1.50 +    ContentResolver contentResolver = context.getContentResolver();
    1.51 +    setExifRotation(getExitOrientation(contentResolver, data.uri));
    1.52 +
    1.53 +    if (data.hasSize()) {
    1.54 +      PicassoKind picassoKind = getPicassoKind(data.targetWidth, data.targetHeight);
    1.55 +      if (picassoKind == FULL) {
    1.56 +        return super.decode(data);
    1.57 +      }
    1.58 +
    1.59 +      long id = parseId(data.uri);
    1.60 +
    1.61 +      BitmapFactory.Options options = new BitmapFactory.Options();
    1.62 +      options.inJustDecodeBounds = true;
    1.63 +
    1.64 +      calculateInSampleSize(data.targetWidth, data.targetHeight, picassoKind.width,
    1.65 +          picassoKind.height, options);
    1.66 +
    1.67 +      Bitmap result = getThumbnail(contentResolver, id, picassoKind.androidKind, options);
    1.68 +
    1.69 +      if (result != null) {
    1.70 +        return result;
    1.71 +      }
    1.72 +    }
    1.73 +
    1.74 +    return super.decode(data);
    1.75 +  }
    1.76 +
    1.77 +  static PicassoKind getPicassoKind(int targetWidth, int targetHeight) {
    1.78 +    if (targetWidth <= MICRO.width && targetHeight <= MICRO.height) {
    1.79 +      return MICRO;
    1.80 +    } else if (targetWidth <= MINI.width && targetHeight <= MINI.height) {
    1.81 +      return MINI;
    1.82 +    }
    1.83 +    return FULL;
    1.84 +  }
    1.85 +
    1.86 +  static int getExitOrientation(ContentResolver contentResolver, Uri uri) {
    1.87 +    Cursor cursor = null;
    1.88 +    try {
    1.89 +      cursor = contentResolver.query(uri, CONTENT_ORIENTATION, null, null, null);
    1.90 +      if (cursor == null || !cursor.moveToFirst()) {
    1.91 +        return 0;
    1.92 +      }
    1.93 +      return cursor.getInt(0);
    1.94 +    } catch (RuntimeException ignored) {
    1.95 +      // If the orientation column doesn't exist, assume no rotation.
    1.96 +      return 0;
    1.97 +    } finally {
    1.98 +      if (cursor != null) {
    1.99 +        cursor.close();
   1.100 +      }
   1.101 +    }
   1.102 +  }
   1.103 +
   1.104 +  enum PicassoKind {
   1.105 +    MICRO(MICRO_KIND, 96, 96),
   1.106 +    MINI(MINI_KIND, 512, 384),
   1.107 +    FULL(FULL_SCREEN_KIND, -1, -1);
   1.108 +
   1.109 +    final int androidKind;
   1.110 +    final int width;
   1.111 +    final int height;
   1.112 +
   1.113 +    PicassoKind(int androidKind, int width, int height) {
   1.114 +      this.androidKind = androidKind;
   1.115 +      this.width = width;
   1.116 +      this.height = height;
   1.117 +    }
   1.118 +  }
   1.119 +}

mercurial