Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [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; |
| 18 | |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 19 | |
Chih-Chung Chang | 0435bac | 2009-04-01 00:11:56 -0700 | [diff] [blame] | 20 | import android.content.ContentResolver; |
Owen Lin | e594b19 | 2009-08-13 18:04:45 +0800 | [diff] [blame] | 21 | import android.content.res.Resources; |
Chih-Chung Chang | 0435bac | 2009-04-01 00:11:56 -0700 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.BitmapFactory; |
| 24 | import android.graphics.drawable.BitmapDrawable; |
| 25 | import android.graphics.drawable.Drawable; |
Chih-Chung Chang | 0435bac | 2009-04-01 00:11:56 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.TransitionDrawable; |
Ray Chen | 9f1480b | 2009-08-27 17:59:29 -0700 | [diff] [blame] | 27 | import android.media.ThumbnailUtil; |
Chih-Chung Chang | 0435bac | 2009-04-01 00:11:56 -0700 | [diff] [blame] | 28 | import android.net.Uri; |
Wu-cheng Li | 8d16a5c | 2009-06-18 16:37:32 +0800 | [diff] [blame] | 29 | import android.os.ParcelFileDescriptor; |
| 30 | import android.util.Log; |
Chih-Chung Chang | 0435bac | 2009-04-01 00:11:56 -0700 | [diff] [blame] | 31 | import android.view.ViewGroup.LayoutParams; |
Chih-Chung Chang | 0435bac | 2009-04-01 00:11:56 -0700 | [diff] [blame] | 32 | import android.widget.ImageView; |
| 33 | |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 34 | import java.io.BufferedInputStream; |
| 35 | import java.io.BufferedOutputStream; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 36 | import java.io.DataInputStream; |
| 37 | import java.io.DataOutputStream; |
| 38 | import java.io.FileInputStream; |
| 39 | import java.io.FileOutputStream; |
| 40 | import java.io.IOException; |
| 41 | |
Chih-Chung Chang | 0435bac | 2009-04-01 00:11:56 -0700 | [diff] [blame] | 42 | /** A controller shows thumbnail picture on a button. The thumbnail picture |
| 43 | * corresponds to a URI of the original picture/video. The thumbnail bitmap |
| 44 | * and the URI can be saved to a file (and later loaded from it). |
| 45 | * <pre> |
| 46 | * public ThumbnailController(ImageView button) |
| 47 | * public void setData(Uri uri, Bitmap original) |
| 48 | * public void updateDisplayIfNeeded() |
| 49 | * public Uri getUri() |
Chih-Chung Chang | 0435bac | 2009-04-01 00:11:56 -0700 | [diff] [blame] | 50 | * public boolean storeData(String filePath) |
| 51 | * public boolean loadData(String filePath) |
| 52 | * </pre> |
| 53 | */ |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 54 | public class ThumbnailController { |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 55 | |
| 56 | @SuppressWarnings("unused") |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 57 | private static final String TAG = "ThumbnailController"; |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 58 | private final ContentResolver mContentResolver; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 59 | private Uri mUri; |
| 60 | private Bitmap mThumb; |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 61 | private final ImageView mButton; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 62 | private Drawable[] mThumbs; |
| 63 | private TransitionDrawable mThumbTransition; |
| 64 | private boolean mShouldAnimateThumb; |
Owen Lin | e594b19 | 2009-08-13 18:04:45 +0800 | [diff] [blame] | 65 | private final Resources mResources; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 66 | |
| 67 | // The "frame" is a drawable we want to put on top of the thumbnail. |
Owen Lin | e594b19 | 2009-08-13 18:04:45 +0800 | [diff] [blame] | 68 | public ThumbnailController(Resources resources, |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 69 | ImageView button, ContentResolver contentResolver) { |
Owen Lin | e594b19 | 2009-08-13 18:04:45 +0800 | [diff] [blame] | 70 | mResources = resources; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 71 | mButton = button; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 72 | mContentResolver = contentResolver; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | public void setData(Uri uri, Bitmap original) { |
Chih-Chung Chang | 0435bac | 2009-04-01 00:11:56 -0700 | [diff] [blame] | 76 | // Make sure uri and original are consistently both null or both |
| 77 | // non-null. |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 78 | if (uri == null || original == null) { |
| 79 | uri = null; |
| 80 | original = null; |
| 81 | } |
| 82 | mUri = uri; |
| 83 | updateThumb(original); |
| 84 | } |
| 85 | |
| 86 | public Uri getUri() { |
| 87 | return mUri; |
| 88 | } |
| 89 | |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 90 | private static final int BUFSIZE = 4096; |
| 91 | |
| 92 | // Stores the data from the specified file. |
| 93 | // Returns true for success. |
| 94 | public boolean storeData(String filePath) { |
| 95 | if (mUri == null) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | FileOutputStream f = null; |
| 100 | BufferedOutputStream b = null; |
| 101 | DataOutputStream d = null; |
| 102 | try { |
| 103 | f = new FileOutputStream(filePath); |
| 104 | b = new BufferedOutputStream(f, BUFSIZE); |
| 105 | d = new DataOutputStream(b); |
| 106 | d.writeUTF(mUri.toString()); |
| 107 | mThumb.compress(Bitmap.CompressFormat.PNG, 100, d); |
| 108 | d.close(); |
| 109 | } catch (IOException e) { |
| 110 | return false; |
| 111 | } finally { |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 112 | MenuHelper.closeSilently(f); |
| 113 | MenuHelper.closeSilently(b); |
| 114 | MenuHelper.closeSilently(d); |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | // Loads the data from the specified file. |
| 120 | // Returns true for success. |
| 121 | public boolean loadData(String filePath) { |
| 122 | FileInputStream f = null; |
| 123 | BufferedInputStream b = null; |
| 124 | DataInputStream d = null; |
| 125 | try { |
| 126 | f = new FileInputStream(filePath); |
| 127 | b = new BufferedInputStream(f, BUFSIZE); |
| 128 | d = new DataInputStream(b); |
| 129 | Uri uri = Uri.parse(d.readUTF()); |
| 130 | Bitmap thumb = BitmapFactory.decodeStream(d); |
| 131 | setData(uri, thumb); |
| 132 | d.close(); |
| 133 | } catch (IOException e) { |
| 134 | return false; |
| 135 | } finally { |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 136 | MenuHelper.closeSilently(f); |
| 137 | MenuHelper.closeSilently(b); |
| 138 | MenuHelper.closeSilently(d); |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 143 | public void updateDisplayIfNeeded() { |
| 144 | if (mUri == null) { |
Chih-Chung Chang | b7b36b9 | 2009-09-25 13:32:17 -0700 | [diff] [blame] | 145 | mButton.setImageDrawable(null); |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 149 | if (mShouldAnimateThumb) { |
| 150 | mThumbTransition.startTransition(500); |
| 151 | mShouldAnimateThumb = false; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private void updateThumb(Bitmap original) { |
| 156 | if (original == null) { |
| 157 | mThumb = null; |
| 158 | mThumbs = null; |
| 159 | return; |
| 160 | } |
| 161 | |
Chih-Chung Chang | 0435bac | 2009-04-01 00:11:56 -0700 | [diff] [blame] | 162 | // Make the mini-thumb size smaller than the button size so that the |
| 163 | // image corners don't peek out from the rounded corners of the |
| 164 | // frame_thumb graphic: |
Owen Lin | dad4b18 | 2009-06-11 16:02:29 -0700 | [diff] [blame] | 165 | final int PADDING_WIDTH = 2; |
| 166 | final int PADDING_HEIGHT = 2; |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 167 | LayoutParams param = mButton.getLayoutParams(); |
| 168 | final int miniThumbWidth = param.width - 2 * PADDING_WIDTH; |
| 169 | final int miniThumbHeight = param.height - 2 * PADDING_HEIGHT; |
Ray Chen | 9f1480b | 2009-08-27 17:59:29 -0700 | [diff] [blame] | 170 | mThumb = ThumbnailUtil.extractMiniThumb( |
Chih-Chung Chang | 34fe2a9 | 2009-08-19 15:52:32 +0800 | [diff] [blame] | 171 | original, miniThumbWidth, miniThumbHeight, |
| 172 | Util.NO_RECYCLE_INPUT); |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 173 | Drawable drawable; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 174 | if (mThumbs == null) { |
| 175 | mThumbs = new Drawable[2]; |
Owen Lin | e594b19 | 2009-08-13 18:04:45 +0800 | [diff] [blame] | 176 | mThumbs[1] = new BitmapDrawable(mResources, mThumb); |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 177 | drawable = mThumbs[1]; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 178 | mShouldAnimateThumb = false; |
| 179 | } else { |
| 180 | mThumbs[0] = mThumbs[1]; |
Owen Lin | e594b19 | 2009-08-13 18:04:45 +0800 | [diff] [blame] | 181 | mThumbs[1] = new BitmapDrawable(mResources, mThumb); |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 182 | mThumbTransition = new TransitionDrawable(mThumbs); |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 183 | drawable = mThumbTransition; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 184 | mShouldAnimateThumb = true; |
| 185 | } |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 186 | mButton.setImageDrawable(drawable); |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 189 | public boolean isUriValid() { |
Ray Chen | 23c51b7 | 2009-04-10 03:41:00 -0700 | [diff] [blame] | 190 | if (mUri == null) { |
| 191 | return false; |
| 192 | } |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 193 | try { |
Wu-cheng Li | 8d16a5c | 2009-06-18 16:37:32 +0800 | [diff] [blame] | 194 | ParcelFileDescriptor pfd = |
| 195 | mContentResolver.openFileDescriptor(mUri, "r"); |
| 196 | if (pfd == null) { |
| 197 | Log.e(TAG, "Fail to open URI."); |
| 198 | return false; |
| 199 | } |
| 200 | pfd.close(); |
Chih-Chung Chang | 0435bac | 2009-04-01 00:11:56 -0700 | [diff] [blame] | 201 | } catch (IOException ex) { |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | } |