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

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * Copyright (C) 2014 Square, Inc.
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16 package com.squareup.picasso;
michael@0 17
michael@0 18 import android.content.ContentResolver;
michael@0 19 import android.content.Context;
michael@0 20 import android.database.Cursor;
michael@0 21 import android.graphics.Bitmap;
michael@0 22 import android.graphics.BitmapFactory;
michael@0 23 import android.net.Uri;
michael@0 24 import android.provider.MediaStore;
michael@0 25 import java.io.IOException;
michael@0 26
michael@0 27 import static android.content.ContentUris.parseId;
michael@0 28 import static android.provider.MediaStore.Images.Thumbnails.FULL_SCREEN_KIND;
michael@0 29 import static android.provider.MediaStore.Images.Thumbnails.MICRO_KIND;
michael@0 30 import static android.provider.MediaStore.Images.Thumbnails.MINI_KIND;
michael@0 31 import static android.provider.MediaStore.Images.Thumbnails.getThumbnail;
michael@0 32 import static com.squareup.picasso.MediaStoreBitmapHunter.PicassoKind.FULL;
michael@0 33 import static com.squareup.picasso.MediaStoreBitmapHunter.PicassoKind.MICRO;
michael@0 34 import static com.squareup.picasso.MediaStoreBitmapHunter.PicassoKind.MINI;
michael@0 35
michael@0 36 class MediaStoreBitmapHunter extends ContentStreamBitmapHunter {
michael@0 37 private static final String[] CONTENT_ORIENTATION = new String[] {
michael@0 38 MediaStore.Images.ImageColumns.ORIENTATION
michael@0 39 };
michael@0 40
michael@0 41 MediaStoreBitmapHunter(Context context, Picasso picasso, Dispatcher dispatcher, Cache cache,
michael@0 42 Stats stats, Action action) {
michael@0 43 super(context, picasso, dispatcher, cache, stats, action);
michael@0 44 }
michael@0 45
michael@0 46 @Override Bitmap decode(Request data) throws IOException {
michael@0 47 ContentResolver contentResolver = context.getContentResolver();
michael@0 48 setExifRotation(getExitOrientation(contentResolver, data.uri));
michael@0 49
michael@0 50 if (data.hasSize()) {
michael@0 51 PicassoKind picassoKind = getPicassoKind(data.targetWidth, data.targetHeight);
michael@0 52 if (picassoKind == FULL) {
michael@0 53 return super.decode(data);
michael@0 54 }
michael@0 55
michael@0 56 long id = parseId(data.uri);
michael@0 57
michael@0 58 BitmapFactory.Options options = new BitmapFactory.Options();
michael@0 59 options.inJustDecodeBounds = true;
michael@0 60
michael@0 61 calculateInSampleSize(data.targetWidth, data.targetHeight, picassoKind.width,
michael@0 62 picassoKind.height, options);
michael@0 63
michael@0 64 Bitmap result = getThumbnail(contentResolver, id, picassoKind.androidKind, options);
michael@0 65
michael@0 66 if (result != null) {
michael@0 67 return result;
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 return super.decode(data);
michael@0 72 }
michael@0 73
michael@0 74 static PicassoKind getPicassoKind(int targetWidth, int targetHeight) {
michael@0 75 if (targetWidth <= MICRO.width && targetHeight <= MICRO.height) {
michael@0 76 return MICRO;
michael@0 77 } else if (targetWidth <= MINI.width && targetHeight <= MINI.height) {
michael@0 78 return MINI;
michael@0 79 }
michael@0 80 return FULL;
michael@0 81 }
michael@0 82
michael@0 83 static int getExitOrientation(ContentResolver contentResolver, Uri uri) {
michael@0 84 Cursor cursor = null;
michael@0 85 try {
michael@0 86 cursor = contentResolver.query(uri, CONTENT_ORIENTATION, null, null, null);
michael@0 87 if (cursor == null || !cursor.moveToFirst()) {
michael@0 88 return 0;
michael@0 89 }
michael@0 90 return cursor.getInt(0);
michael@0 91 } catch (RuntimeException ignored) {
michael@0 92 // If the orientation column doesn't exist, assume no rotation.
michael@0 93 return 0;
michael@0 94 } finally {
michael@0 95 if (cursor != null) {
michael@0 96 cursor.close();
michael@0 97 }
michael@0 98 }
michael@0 99 }
michael@0 100
michael@0 101 enum PicassoKind {
michael@0 102 MICRO(MICRO_KIND, 96, 96),
michael@0 103 MINI(MINI_KIND, 512, 384),
michael@0 104 FULL(FULL_SCREEN_KIND, -1, -1);
michael@0 105
michael@0 106 final int androidKind;
michael@0 107 final int width;
michael@0 108 final int height;
michael@0 109
michael@0 110 PicassoKind(int androidKind, int width, int height) {
michael@0 111 this.androidKind = androidKind;
michael@0 112 this.width = width;
michael@0 113 this.height = height;
michael@0 114 }
michael@0 115 }
michael@0 116 }

mercurial