Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.camera.gallery; |
| 18 | |
| 19 | import com.android.camera.BitmapManager; |
| 20 | import com.android.camera.Util; |
| 21 | |
| 22 | import android.content.ContentResolver; |
| 23 | import android.graphics.Bitmap; |
| 24 | import android.graphics.BitmapFactory; |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 25 | import android.net.Uri; |
| 26 | import android.os.ParcelFileDescriptor; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | import java.io.File; |
| 30 | import java.io.FileNotFoundException; |
| 31 | import java.io.InputStream; |
| 32 | |
| 33 | class UriImage implements IImage { |
| 34 | private static final String TAG = "UriImage"; |
Owen Lin | d30b587 | 2009-04-16 18:42:03 +0800 | [diff] [blame] | 35 | private final Uri mUri; |
| 36 | private final BaseImageList mContainer; |
| 37 | private final ContentResolver mContentResolver; |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 38 | |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 39 | UriImage(BaseImageList container, ContentResolver cr, Uri uri) { |
| 40 | mContainer = container; |
| 41 | mContentResolver = cr; |
| 42 | mUri = uri; |
| 43 | } |
| 44 | |
| 45 | public String getDataPath() { |
| 46 | return mUri.getPath(); |
| 47 | } |
| 48 | |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 49 | private InputStream getInputStream() { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 50 | try { |
| 51 | if (mUri.getScheme().equals("file")) { |
| 52 | String path = mUri.getPath(); |
| 53 | return new java.io.FileInputStream(mUri.getPath()); |
| 54 | } else { |
| 55 | return mContentResolver.openInputStream(mUri); |
| 56 | } |
| 57 | } catch (FileNotFoundException ex) { |
| 58 | return null; |
| 59 | } |
| 60 | } |
| 61 | |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 62 | private ParcelFileDescriptor getPFD() { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 63 | try { |
| 64 | if (mUri.getScheme().equals("file")) { |
| 65 | String path = mUri.getPath(); |
| 66 | return ParcelFileDescriptor.open(new File(path), |
| 67 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 68 | } else { |
| 69 | return mContentResolver.openFileDescriptor(mUri, "r"); |
| 70 | } |
| 71 | } catch (FileNotFoundException ex) { |
| 72 | return null; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public Bitmap fullSizeBitmap(int targetWidthHeight) { |
Chih-Chung Chang | 4250e21 | 2009-07-24 10:58:40 +0800 | [diff] [blame^] | 77 | return fullSizeBitmap(targetWidthHeight, IImage.ROTATE_AS_NEEDED, |
| 78 | IImage.NO_NATIVE); |
| 79 | } |
| 80 | |
| 81 | public Bitmap fullSizeBitmap(int targetWidthHeight, boolean rotateAsNeeded, |
| 82 | boolean useNative) { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 83 | try { |
| 84 | ParcelFileDescriptor pfdInput = getPFD(); |
Chih-Chung Chang | 4250e21 | 2009-07-24 10:58:40 +0800 | [diff] [blame^] | 85 | Bitmap b = Util.makeBitmap(targetWidthHeight, pfdInput, |
| 86 | useNative); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 87 | return b; |
| 88 | } catch (Exception ex) { |
Chih-Chung Chang | 4250e21 | 2009-07-24 10:58:40 +0800 | [diff] [blame^] | 89 | Log.e(TAG, "got exception decoding bitmap ", ex); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 90 | return null; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | final class LoadBitmapCancelable extends BaseCancelable<Bitmap> { |
| 95 | ParcelFileDescriptor mPfdInput; |
Wei-Ta Chen | d5d7464 | 2009-06-11 16:13:13 +0800 | [diff] [blame] | 96 | BitmapFactory.Options mOptions; |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 97 | int mTargetWidthOrHeight; |
| 98 | |
Wei-Ta Chen | d5d7464 | 2009-06-11 16:13:13 +0800 | [diff] [blame] | 99 | public LoadBitmapCancelable(ParcelFileDescriptor pfd, |
| 100 | int targetWidthOrHeight, BitmapFactory.Options options) { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 101 | mPfdInput = pfd; |
| 102 | mTargetWidthOrHeight = targetWidthOrHeight; |
Wei-Ta Chen | d5d7464 | 2009-06-11 16:13:13 +0800 | [diff] [blame] | 103 | if (options != null) { |
| 104 | mOptions = options; |
| 105 | } else { |
| 106 | mOptions = new BitmapFactory.Options(); |
| 107 | } |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 111 | public boolean requestCancel() { |
| 112 | if (super.requestCancel()) { |
| 113 | mOptions.requestCancelDecode(); |
| 114 | return true; |
| 115 | } |
| 116 | return false; |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 117 | } |
| 118 | |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 119 | @Override |
| 120 | protected Bitmap execute() { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 121 | try { |
Chih-Chung Chang | c1c20e9 | 2009-04-15 18:06:14 +0800 | [diff] [blame] | 122 | Bitmap b = Util.makeBitmap(mTargetWidthOrHeight, |
| 123 | fullSizeImageUri(), mContentResolver, mPfdInput, |
| 124 | mOptions); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 125 | return b; |
| 126 | } catch (Exception ex) { |
| 127 | return null; |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 132 | public Cancelable<Bitmap> fullSizeBitmapCancelable( |
Wei-Ta Chen | d5d7464 | 2009-06-11 16:13:13 +0800 | [diff] [blame] | 133 | int targetWidthOrHeight, BitmapFactory.Options options) { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 134 | try { |
| 135 | ParcelFileDescriptor pfdInput = getPFD(); |
| 136 | if (pfdInput == null) return null; |
Wei-Ta Chen | d5d7464 | 2009-06-11 16:13:13 +0800 | [diff] [blame] | 137 | return new LoadBitmapCancelable(pfdInput, |
| 138 | targetWidthOrHeight, options); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 139 | } catch (UnsupportedOperationException ex) { |
| 140 | return null; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | public Uri fullSizeImageUri() { |
| 145 | return mUri; |
| 146 | } |
| 147 | |
| 148 | public InputStream fullSizeImageData() { |
| 149 | return getInputStream(); |
| 150 | } |
| 151 | |
| 152 | public Bitmap miniThumbBitmap() { |
| 153 | return thumbBitmap(); |
| 154 | } |
| 155 | |
| 156 | public String getTitle() { |
| 157 | return mUri.toString(); |
| 158 | } |
| 159 | |
| 160 | public String getDisplayName() { |
| 161 | return getTitle(); |
| 162 | } |
| 163 | |
| 164 | public Bitmap thumbBitmap() { |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 165 | return fullSizeBitmap(THUMBNAIL_TARGET_SIZE); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | private BitmapFactory.Options snifBitmapOptions() { |
| 169 | ParcelFileDescriptor input = getPFD(); |
| 170 | if (input == null) return null; |
| 171 | try { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 172 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 173 | options.inJustDecodeBounds = true; |
| 174 | BitmapManager.instance().decodeFileDescriptor( |
Owen Lin | d30b587 | 2009-04-16 18:42:03 +0800 | [diff] [blame] | 175 | input.getFileDescriptor(), options); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 176 | return options; |
| 177 | } finally { |
Chih-Chung Chang | 724e9b3 | 2009-05-19 14:35:15 +0800 | [diff] [blame] | 178 | Util.closeSilently(input); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | public String getMimeType() { |
| 183 | BitmapFactory.Options options = snifBitmapOptions(); |
| 184 | return (options != null) ? options.outMimeType : ""; |
| 185 | } |
| 186 | |
| 187 | public int getHeight() { |
| 188 | BitmapFactory.Options options = snifBitmapOptions(); |
| 189 | return (options != null) ? options.outHeight : 0; |
| 190 | } |
| 191 | |
| 192 | public int getWidth() { |
| 193 | BitmapFactory.Options options = snifBitmapOptions(); |
| 194 | return (options != null) ? options.outWidth : 0; |
| 195 | } |
| 196 | |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 197 | public long fullSizeImageId() { |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | public IImageList getContainer() { |
| 202 | return mContainer; |
| 203 | } |
| 204 | |
| 205 | public long getDateTaken() { |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | public int getRow() { |
Chih-Chung Chang | f32ca41 | 2009-05-05 18:30:29 +0800 | [diff] [blame] | 210 | return 0; |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | public boolean isReadonly() { |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | public boolean isDrm() { |
| 218 | return false; |
| 219 | } |
| 220 | |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 221 | public boolean rotateImageBy(int degrees) { |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | public void setTitle(String name) { |
| 226 | throw new UnsupportedOperationException(); |
| 227 | } |
| 228 | |
| 229 | public Uri thumbUri() { |
| 230 | throw new UnsupportedOperationException(); |
| 231 | } |
| 232 | } |