1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/* 2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project 3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License"); 5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License. 6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at 7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * http://www.apache.org/licenses/LICENSE-2.0 9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software 11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS, 12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and 14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License. 15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.datamodel.media; 17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context; 19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Bitmap; 20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.BitmapFactory; 21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.media.ExifInterface; 22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.media.PoolableImageCache.ReusableImageResourcePool; 24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.ImageUtils; 25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.LogUtil; 26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.io.IOException; 28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/** 30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Serves file system based image requests. Since file paths can be expressed in Uri form, this 31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * extends regular UriImageRequest but performs additional optimizations such as loading thumbnails 32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * directly from Exif information. 33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class FileImageRequest extends UriImageRequest { 35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private final String mPath; 36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private final boolean mCanUseThumbnail; 37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public FileImageRequest(final Context context, 39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final FileImageRequestDescriptor descriptor) { 40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd super(context, descriptor); 41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mPath = descriptor.path; 42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mCanUseThumbnail = descriptor.canUseThumbnail; 43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd protected Bitmap loadBitmapInternal() 47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd throws IOException { 48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Before using the FileInputStream, check if the Exif has a thumbnail that we can use. 49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (mCanUseThumbnail) { 50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd byte[] thumbnail = null; 51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd try { 52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final ExifInterface exif = new ExifInterface(mPath); 53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (exif.hasThumbnail()) { 54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd thumbnail = exif.getThumbnail(); 55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } catch (final IOException e) { 57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Nothing to do 58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (thumbnail != null) { 61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final BitmapFactory.Options options = PoolableImageCache.getBitmapOptionsForPool( 62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd false /* scaled */, 0 /* inputDensity */, 0 /* targetDensity */); 63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // First, check dimensions of the bitmap. 64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd options.inJustDecodeBounds = true; 65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length, options); 66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Calculate inSampleSize 68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd options.inSampleSize = ImageUtils.get().calculateInSampleSize(options, 69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mDescriptor.desiredWidth, mDescriptor.desiredHeight); 70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd options.inJustDecodeBounds = false; 72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Actually decode the bitmap, optionally using the bitmap pool. 74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd try { 75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Get the orientation. We should be able to get the orientation from 76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // the thumbnail itself but at least on some phones, the thumbnail 77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // doesn't have an orientation tag. So use the outer image's orientation 78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // tag and hope for the best. 79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mOrientation = ImageUtils.getOrientation(getInputStreamForResource()); 80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (com.android.messaging.util.exif.ExifInterface. 81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd getOrientationParams(mOrientation).invertDimensions) { 82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mDescriptor.updateSourceDimensions(options.outHeight, options.outWidth); 83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } else { 84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mDescriptor.updateSourceDimensions(options.outWidth, options.outHeight); 85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final ReusableImageResourcePool bitmapPool = getBitmapPool(); 87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (bitmapPool == null) { 88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length, 89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd options); 90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } else { 91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final int sampledWidth = options.outWidth / options.inSampleSize; 92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final int sampledHeight = options.outHeight / options.inSampleSize; 93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return bitmapPool.decodeByteArray(thumbnail, options, sampledWidth, 94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd sampledHeight); 95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } catch (IOException ex) { 97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // If the thumbnail is broken due to IOException, this will 98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // fall back to default bitmap loading. 99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd LogUtil.e(LogUtil.BUGLE_IMAGE_TAG, "FileImageRequest: failed to load " + 100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd "thumbnail from Exif", ex); 101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Fall back to default InputStream-based loading if no thumbnails could be retrieved. 106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return super.loadBitmapInternal(); 107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd} 109