The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 19 | import com.android.camera.gallery.Cancelable; |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 20 | import com.android.camera.gallery.IImage; |
| 21 | import com.android.camera.gallery.IImageList; |
| 22 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 23 | import android.app.Activity; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.SharedPreferences; |
| 27 | import android.content.pm.ActivityInfo; |
| 28 | import android.graphics.Bitmap; |
| 29 | import android.graphics.Matrix; |
| 30 | import android.net.Uri; |
| 31 | import android.os.Bundle; |
| 32 | import android.os.Handler; |
| 33 | import android.os.Message; |
| 34 | import android.preference.PreferenceManager; |
| 35 | import android.provider.MediaStore; |
| 36 | import android.util.AttributeSet; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | import android.view.GestureDetector; |
| 39 | import android.view.KeyEvent; |
| 40 | import android.view.Menu; |
| 41 | import android.view.MenuItem; |
| 42 | import android.view.MotionEvent; |
| 43 | import android.view.View; |
| 44 | import android.view.Window; |
| 45 | import android.view.WindowManager; |
| 46 | import android.view.animation.AlphaAnimation; |
| 47 | import android.view.animation.Animation; |
| 48 | import android.view.animation.AnimationUtils; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 49 | import android.widget.Toast; |
| 50 | import android.widget.ZoomButtonsController; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 51 | |
Chih-Chung Chang | 063f09d | 2009-04-01 04:02:00 -0700 | [diff] [blame] | 52 | import java.util.Random; |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 53 | import java.util.concurrent.CancellationException; |
| 54 | import java.util.concurrent.ExecutionException; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 55 | |
Chih-Chung Chang | 73b7a3a | 2009-04-12 23:28:16 -0700 | [diff] [blame] | 56 | // This activity can display a whole picture and navigate them in a specific |
| 57 | // gallery. It has two modes: normal mode and slide show mode. In normal mode |
| 58 | // the user view one image at a time, and can click "previous" and "next" |
| 59 | // button to see the previous or next image. In slide show mode it shows one |
| 60 | // image after another, with some transition effect. |
Chih-Chung Chang | 063f09d | 2009-04-01 04:02:00 -0700 | [diff] [blame] | 61 | public class ViewImage extends Activity implements View.OnClickListener { |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 62 | private static final String TAG = "ViewImage"; |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 63 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 64 | private ImageGetter mGetter; |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 65 | private Uri mSavedUri; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 66 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 67 | // Choices for what adjacents to load. |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 68 | private static final int[] sOrder_adjacents = new int[] {0, 1, -1}; |
| 69 | private static final int[] sOrder_slideshow = new int[] {0}; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 70 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 71 | LocalHandler mHandler = new LocalHandler(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 72 | |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 73 | private final Random mRandom = new Random(System.currentTimeMillis()); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 74 | private int [] mShuffleOrder; |
| 75 | private boolean mUseShuffleOrder = false; |
| 76 | private boolean mSlideShowLoop = false; |
| 77 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 78 | static final int MODE_NORMAL = 1; |
| 79 | static final int MODE_SLIDESHOW = 2; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 80 | private int mMode = MODE_NORMAL; |
| 81 | private boolean mFullScreenInNormalMode; |
| 82 | private boolean mShowActionIcons; |
| 83 | private View mActionIconPanel; |
| 84 | private View mShutterButton; |
| 85 | |
| 86 | private boolean mSortAscending = false; |
| 87 | private int mSlideShowInterval; |
| 88 | private int mLastSlideShowImage; |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 89 | int mCurrentPosition = 0; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 90 | |
| 91 | // represents which style animation to use |
| 92 | private int mAnimationIndex; |
| 93 | private Animation [] mSlideShowInAnimation; |
| 94 | private Animation [] mSlideShowOutAnimation; |
| 95 | |
| 96 | private SharedPreferences mPrefs; |
| 97 | |
| 98 | private View mNextImageView, mPrevImageView; |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 99 | private final Animation mHideNextImageViewAnimation = new AlphaAnimation(1F, 0F); |
| 100 | private final Animation mHidePrevImageViewAnimation = new AlphaAnimation(1F, 0F); |
| 101 | private final Animation mShowNextImageViewAnimation = new AlphaAnimation(0F, 1F); |
| 102 | private final Animation mShowPrevImageViewAnimation = new AlphaAnimation(0F, 1F); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 103 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 104 | static final int PADDING = 20; |
| 105 | static final int HYSTERESIS = PADDING * 2; |
| 106 | static final int BASE_SCROLL_DURATION = 1000; // ms |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 107 | |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 108 | IImageList mAllImages; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 109 | |
| 110 | private int mSlideShowImageCurrent = 0; |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 111 | private final ImageViewTouchBase [] mSlideShowImageViews = |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 112 | new ImageViewTouchBase[2]; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 113 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 114 | GestureDetector mGestureDetector; |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 115 | private ZoomButtonsController mZoomButtonsController; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 116 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 117 | // The image view displayed for normal mode. |
| 118 | private ImageViewTouch mImageView; |
| 119 | // This is the cache for thumbnail bitmaps. |
| 120 | private BitmapCache mCache; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 121 | private MenuHelper.MenuItemsResult mImageMenuRunnable; |
| 122 | |
| 123 | private Runnable mDismissOnScreenControlsRunnable; |
| 124 | private boolean mCameraReviewMode; |
| 125 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 126 | private void updateNextPrevControls() { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 127 | boolean showPrev = mCurrentPosition > 0; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 128 | boolean showNext = mCurrentPosition < mAllImages.getCount() - 1; |
| 129 | |
| 130 | boolean prevIsVisible = mPrevImageView.getVisibility() == View.VISIBLE; |
| 131 | boolean nextIsVisible = mNextImageView.getVisibility() == View.VISIBLE; |
| 132 | |
| 133 | if (showPrev && !prevIsVisible) { |
| 134 | Animation a = mShowPrevImageViewAnimation; |
| 135 | a.setDuration(500); |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 136 | mPrevImageView.startAnimation(a); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 137 | mPrevImageView.setVisibility(View.VISIBLE); |
| 138 | } else if (!showPrev && prevIsVisible) { |
| 139 | Animation a = mHidePrevImageViewAnimation; |
| 140 | a.setDuration(500); |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 141 | mPrevImageView.startAnimation(a); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 142 | mPrevImageView.setVisibility(View.GONE); |
| 143 | } |
| 144 | |
| 145 | if (showNext && !nextIsVisible) { |
| 146 | Animation a = mShowNextImageViewAnimation; |
| 147 | a.setDuration(500); |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 148 | mNextImageView.startAnimation(a); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 149 | mNextImageView.setVisibility(View.VISIBLE); |
| 150 | } else if (!showNext && nextIsVisible) { |
| 151 | Animation a = mHideNextImageViewAnimation; |
| 152 | a.setDuration(500); |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 153 | mNextImageView.startAnimation(a); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 154 | mNextImageView.setVisibility(View.GONE); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | private void showOnScreenControls() { |
Owen Lin | 31817e9 | 2009-03-27 16:30:02 -0700 | [diff] [blame] | 159 | mHandler.removeCallbacks(mDismissOnScreenControlsRunnable); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 160 | updateNextPrevControls(); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 161 | updateZoomButtonsEnabled(); |
| 162 | mZoomButtonsController.setVisible(true); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public boolean dispatchTouchEvent(MotionEvent m) { |
| 167 | boolean sup = super.dispatchTouchEvent(m); |
Owen Lin | 31817e9 | 2009-03-27 16:30:02 -0700 | [diff] [blame] | 168 | |
| 169 | // This is a hack to show the on screen controls. We should make sure |
| 170 | // this event is not handled by others(ie. sup == false), and listen for |
| 171 | // the events on zoom/prev/next buttons. |
| 172 | // However, since we have no other pressable views, it is OK now. |
| 173 | // TODO: Fix the above issue. |
| 174 | if (mMode == MODE_NORMAL) { |
| 175 | switch (m.getAction()) { |
| 176 | case MotionEvent.ACTION_DOWN: |
| 177 | showOnScreenControls(); |
| 178 | break; |
| 179 | case MotionEvent.ACTION_UP: |
| 180 | scheduleDismissOnScreenControls(); |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 185 | if (sup == false) { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 186 | mGestureDetector.onTouchEvent(m); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | private void scheduleDismissOnScreenControls() { |
| 193 | mHandler.removeCallbacks(mDismissOnScreenControlsRunnable); |
| 194 | mHandler.postDelayed(mDismissOnScreenControlsRunnable, 1500); |
| 195 | } |
| 196 | |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 197 | private void updateZoomButtonsEnabled() { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 198 | ImageViewTouch imageView = mImageView; |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 199 | float scale = imageView.getScale(); |
| 200 | mZoomButtonsController.setZoomInEnabled(scale < imageView.mMaxZoom); |
| 201 | mZoomButtonsController.setZoomOutEnabled(scale > 1); |
| 202 | } |
| 203 | |
Andreas Huber | 5ae6532 | 2009-03-24 18:39:29 -0700 | [diff] [blame] | 204 | @Override |
| 205 | protected void onDestroy() { |
Owen Lin | 9e0bda2 | 2009-05-04 17:10:37 -0700 | [diff] [blame^] | 206 | |
Andreas Huber | 5ae6532 | 2009-03-24 18:39:29 -0700 | [diff] [blame] | 207 | // This is necessary to make the ZoomButtonsController unregister |
| 208 | // its configuration change receiver. |
| 209 | if (mZoomButtonsController != null) { |
| 210 | mZoomButtonsController.setVisible(false); |
| 211 | } |
| 212 | |
| 213 | super.onDestroy(); |
| 214 | } |
| 215 | |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 216 | private void setupZoomButtonController(View rootView) { |
| 217 | mGestureDetector = new GestureDetector(this, new MyGestureListener()); |
| 218 | mZoomButtonsController = new ZoomButtonsController(rootView); |
| 219 | mZoomButtonsController.setAutoDismissed(false); |
| 220 | mZoomButtonsController.setOnZoomListener( |
| 221 | new ZoomButtonsController.OnZoomListener() { |
| 222 | public void onVisibilityChanged(boolean visible) { |
| 223 | if (visible) { |
| 224 | updateZoomButtonsEnabled(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | public void onZoom(boolean zoomIn) { |
| 229 | if (zoomIn) { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 230 | mImageView.zoomIn(); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 231 | } else { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 232 | mImageView.zoomOut(); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 233 | } |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 234 | } |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 235 | }); |
| 236 | } |
| 237 | |
| 238 | private class MyGestureListener extends |
| 239 | GestureDetector.SimpleOnGestureListener { |
| 240 | |
| 241 | @Override |
| 242 | public boolean onScroll(MotionEvent e1, MotionEvent e2, |
| 243 | float distanceX, float distanceY) { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 244 | ImageViewTouch imageView = mImageView; |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 245 | if (imageView.getScale() > 1F) { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 246 | imageView.postTranslateCenter(-distanceX, -distanceY); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 247 | } |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 248 | return true; |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public boolean onSingleTapUp(MotionEvent e) { |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 253 | setMode(MODE_NORMAL); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 254 | return true; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | private void setupDismissOnScreenControlRunnable() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 259 | mDismissOnScreenControlsRunnable = new Runnable() { |
| 260 | public void run() { |
| 261 | if (!mShowActionIcons) { |
| 262 | if (mNextImageView.getVisibility() == View.VISIBLE) { |
| 263 | Animation a = mHideNextImageViewAnimation; |
| 264 | a.setDuration(500); |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 265 | mNextImageView.startAnimation(a); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 266 | mNextImageView.setVisibility(View.INVISIBLE); |
| 267 | } |
| 268 | |
| 269 | if (mPrevImageView.getVisibility() == View.VISIBLE) { |
| 270 | Animation a = mHidePrevImageViewAnimation; |
| 271 | a.setDuration(500); |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 272 | mPrevImageView.startAnimation(a); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 273 | mPrevImageView.setVisibility(View.INVISIBLE); |
| 274 | } |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 275 | mZoomButtonsController.setVisible(false); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | }; |
| 279 | } |
| 280 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 281 | boolean isPickIntent() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 282 | String action = getIntent().getAction(); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 283 | return (Intent.ACTION_PICK.equals(action) |
| 284 | || Intent.ACTION_GET_CONTENT.equals(action)); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 285 | } |
| 286 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 287 | @Override |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 288 | public boolean onCreateOptionsMenu(Menu menu) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 289 | super.onCreateOptionsMenu(menu); |
| 290 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 291 | if (!mCameraReviewMode) { |
| 292 | MenuItem item = menu.add(Menu.CATEGORY_SECONDARY, 203, 0, |
| 293 | R.string.slide_show); |
| 294 | item.setOnMenuItemClickListener( |
| 295 | new MenuItem.OnMenuItemClickListener() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 296 | public boolean onMenuItemClick(MenuItem item) { |
| 297 | setMode(MODE_SLIDESHOW); |
| 298 | mLastSlideShowImage = mCurrentPosition; |
| 299 | loadNextImage(mCurrentPosition, 0, true); |
| 300 | return true; |
| 301 | } |
| 302 | }); |
| 303 | item.setIcon(android.R.drawable.ic_menu_slideshow); |
| 304 | } |
| 305 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 306 | final SelectedImageGetter selectedImageGetter = |
| 307 | new SelectedImageGetter() { |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 308 | public IImage getCurrentImage() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 309 | return mAllImages.getImageAt(mCurrentPosition); |
| 310 | } |
| 311 | |
| 312 | public Uri getCurrentImageUri() { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 313 | return mAllImages.getImageAt(mCurrentPosition) |
| 314 | .fullSizeImageUri(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 315 | } |
| 316 | }; |
| 317 | |
| 318 | mImageMenuRunnable = MenuHelper.addImageMenuItems( |
| 319 | menu, |
| 320 | MenuHelper.INCLUDE_ALL, |
| 321 | true, |
| 322 | ViewImage.this, |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 323 | mHandler, |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 324 | mDeletePhotoRunnable, |
| 325 | new MenuHelper.MenuInvoker() { |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 326 | public void run(final MenuHelper.MenuCallback cb) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 327 | setMode(MODE_NORMAL); |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 328 | Thread t = new Thread() { |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 329 | @Override |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 330 | public void run() { |
| 331 | cb.run(selectedImageGetter.getCurrentImageUri(), |
| 332 | selectedImageGetter.getCurrentImage()); |
| 333 | mHandler.post(new Runnable() { |
| 334 | public void run() { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 335 | mImageView.clear(); |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 336 | setImage(mCurrentPosition); |
| 337 | } |
| 338 | }); |
| 339 | } |
| 340 | }; |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 341 | t.start(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 342 | } |
| 343 | }); |
| 344 | |
| 345 | if (true) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 346 | MenuItem item = menu.add(Menu.CATEGORY_SECONDARY, 203, 1000, |
| 347 | R.string.camerasettings); |
| 348 | item.setOnMenuItemClickListener( |
| 349 | new MenuItem.OnMenuItemClickListener() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 350 | public boolean onMenuItemClick(MenuItem item) { |
| 351 | Intent preferences = new Intent(); |
| 352 | preferences.setClass(ViewImage.this, GallerySettings.class); |
| 353 | startActivity(preferences); |
| 354 | return true; |
| 355 | } |
| 356 | }); |
| 357 | item.setAlphabeticShortcut('p'); |
| 358 | item.setIcon(android.R.drawable.ic_menu_preferences); |
| 359 | } |
| 360 | |
| 361 | // Hidden menu just so the shortcut will bring up the zoom controls |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 362 | // the string resource is a placeholder |
| 363 | menu.add(Menu.CATEGORY_SECONDARY, 203, 0, R.string.camerasettings) |
| 364 | .setOnMenuItemClickListener( |
| 365 | new MenuItem.OnMenuItemClickListener() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 366 | public boolean onMenuItemClick(MenuItem item) { |
| 367 | showOnScreenControls(); |
Owen Lin | 31817e9 | 2009-03-27 16:30:02 -0700 | [diff] [blame] | 368 | scheduleDismissOnScreenControls(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 369 | return true; |
| 370 | } |
| 371 | }) |
| 372 | .setAlphabeticShortcut('z') |
| 373 | .setVisible(false); |
| 374 | |
| 375 | |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | protected Runnable mDeletePhotoRunnable = new Runnable() { |
| 380 | public void run() { |
| 381 | mAllImages.removeImageAt(mCurrentPosition); |
| 382 | if (mAllImages.getCount() == 0) { |
| 383 | finish(); |
| 384 | } else { |
| 385 | if (mCurrentPosition == mAllImages.getCount()) { |
| 386 | mCurrentPosition -= 1; |
| 387 | } |
| 388 | } |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 389 | mImageView.clear(); |
| 390 | mCache.clear(); // Because the position number is changed. |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 391 | setImage(mCurrentPosition); |
| 392 | } |
| 393 | }; |
| 394 | |
| 395 | @Override |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 396 | public boolean onPrepareOptionsMenu(Menu menu) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 397 | super.onPrepareOptionsMenu(menu); |
| 398 | setMode(MODE_NORMAL); |
| 399 | |
| 400 | if (mImageMenuRunnable != null) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 401 | mImageMenuRunnable.gettingReadyToOpen(menu, |
| 402 | mAllImages.getImageAt(mCurrentPosition)); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Chih-Chung Chang | eb9d8a2 | 2009-03-27 16:07:25 -0700 | [diff] [blame] | 405 | Uri uri = mAllImages.getImageAt(mCurrentPosition).fullSizeImageUri(); |
| 406 | MenuHelper.enableShareMenuItem(menu, !MenuHelper.isMMSUri(uri)); |
| 407 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 408 | return true; |
| 409 | } |
| 410 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 411 | @Override |
| 412 | public boolean onMenuItemSelected(int featureId, MenuItem item) { |
| 413 | boolean b = super.onMenuItemSelected(featureId, item); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 414 | if (mImageMenuRunnable != null) { |
| 415 | mImageMenuRunnable.aboutToCall(item, |
| 416 | mAllImages.getImageAt(mCurrentPosition)); |
| 417 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 418 | return b; |
| 419 | } |
| 420 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 421 | void setImage(int pos) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 422 | mCurrentPosition = pos; |
| 423 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 424 | Bitmap b = mCache.getBitmap(pos); |
| 425 | if (b != null) { |
| 426 | mImageView.setImageBitmapResetBase(b, true); |
| 427 | updateZoomButtonsEnabled(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 428 | } |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 429 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 430 | ImageGetterCallback cb = new ImageGetterCallback() { |
| 431 | public void completed(boolean wasCanceled) { |
| 432 | if (!mShowActionIcons) { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 433 | mImageView.setFocusableInTouchMode(true); |
| 434 | mImageView.requestFocus(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | public boolean wantsThumbnail(int pos, int offset) { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 439 | return !mCache.hasBitmap(pos + offset); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | public boolean wantsFullImage(int pos, int offset) { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 443 | return (offset == 0); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | public int fullImageSizeToUse(int pos, int offset) { |
| 447 | // TODO |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 448 | // this number should be bigger so that we can zoom. we may |
| 449 | // need to get fancier and read in the fuller size image as the |
| 450 | // user starts to zoom. use -1 to get the full full size image. |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 451 | // for now use 480 so we don't run out of memory |
| 452 | final int imageViewSize = 480; |
| 453 | return imageViewSize; |
| 454 | } |
| 455 | |
| 456 | public int [] loadOrder() { |
| 457 | return sOrder_adjacents; |
| 458 | } |
| 459 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 460 | public void imageLoaded(int pos, int offset, Bitmap bitmap, |
| 461 | boolean isThumb) { |
Owen Lin | 3102f06 | 2009-03-24 20:38:25 -0700 | [diff] [blame] | 462 | // shouldn't get here after onPause() |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 463 | if (isThumb) { |
| 464 | mCache.put(pos + offset, bitmap); |
| 465 | } |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 466 | if (offset == 0) { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 467 | // isThumb: We always load thumb bitmap first, so we will |
| 468 | // reset the supp matrix for then thumb bitmap, and keep |
| 469 | // the supp matrix when the full bitmap is loaded. |
| 470 | mImageView.setImageBitmapResetBase(bitmap, isThumb); |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 471 | updateZoomButtonsEnabled(); |
| 472 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 473 | } |
| 474 | }; |
| 475 | |
| 476 | // Could be null if we're stopping a slide show in the course of pausing |
| 477 | if (mGetter != null) { |
| 478 | mGetter.setPosition(pos, cb); |
| 479 | } |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 480 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 481 | showOnScreenControls(); |
Owen Lin | 31817e9 | 2009-03-27 16:30:02 -0700 | [diff] [blame] | 482 | scheduleDismissOnScreenControls(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | @Override |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 486 | public void onCreate(Bundle instanceState) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 487 | super.onCreate(instanceState); |
| 488 | Intent intent = getIntent(); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 489 | mCameraReviewMode = intent.getBooleanExtra( |
| 490 | "com.android.camera.ReviewMode", false); |
| 491 | mFullScreenInNormalMode = intent.getBooleanExtra( |
| 492 | MediaStore.EXTRA_FULL_SCREEN, true); |
| 493 | mShowActionIcons = intent.getBooleanExtra( |
| 494 | MediaStore.EXTRA_SHOW_ACTION_ICONS, false); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 495 | |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 496 | setRequestedOrientation(); |
| 497 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 498 | mPrefs = PreferenceManager.getDefaultSharedPreferences(this); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 499 | |
| 500 | setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT); |
| 501 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 502 | setContentView(R.layout.viewimage); |
| 503 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 504 | mImageView = (ImageViewTouch) findViewById(R.id.image); |
| 505 | mImageView.setEnableTrackballScroll(!mShowActionIcons); |
| 506 | mCache = new BitmapCache(3); |
| 507 | mImageView.setRecycler(mCache); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 508 | |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 509 | |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 510 | BitmapManager bitmapManager = BitmapManager.instance(); |
| 511 | bitmapManager.setCheckResourceLock(false); |
| 512 | bitmapManager.allowAllDecoding(); |
| 513 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 514 | makeGetter(); |
| 515 | |
| 516 | mAnimationIndex = -1; |
| 517 | |
| 518 | mSlideShowInAnimation = new Animation[] { |
| 519 | makeInAnimation(R.anim.transition_in), |
| 520 | makeInAnimation(R.anim.slide_in), |
| 521 | makeInAnimation(R.anim.slide_in_vertical), |
| 522 | }; |
| 523 | |
| 524 | mSlideShowOutAnimation = new Animation[] { |
| 525 | makeOutAnimation(R.anim.transition_out), |
| 526 | makeOutAnimation(R.anim.slide_out), |
| 527 | makeOutAnimation(R.anim.slide_out_vertical), |
| 528 | }; |
| 529 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 530 | mSlideShowImageViews[0] = |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 531 | (ImageViewTouchBase) findViewById(R.id.image1_slideShow); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 532 | mSlideShowImageViews[1] = |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 533 | (ImageViewTouchBase) findViewById(R.id.image2_slideShow); |
| 534 | for (ImageViewTouchBase v : mSlideShowImageViews) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 535 | v.setVisibility(View.INVISIBLE); |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 536 | v.setRecycler(mCache); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | mActionIconPanel = findViewById(R.id.action_icon_panel); |
| 540 | { |
| 541 | int[] pickIds = {R.id.attach, R.id.cancel}; |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 542 | int[] normalIds = {R.id.gallery, R.id.setas, R.id.share, |
| 543 | R.id.discard}; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 544 | int[] hideIds = pickIds; |
| 545 | int[] connectIds = normalIds; |
| 546 | if (isPickIntent()) { |
| 547 | hideIds = normalIds; |
| 548 | connectIds = pickIds; |
| 549 | } |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 550 | for (int id : hideIds) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 551 | mActionIconPanel.findViewById(id).setVisibility(View.GONE); |
| 552 | } |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 553 | for (int id : connectIds) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 554 | View view = mActionIconPanel.findViewById(id); |
| 555 | view.setOnClickListener(this); |
| 556 | Animation animation = new AlphaAnimation(0F, 1F); |
| 557 | animation.setDuration(500); |
| 558 | view.setAnimation(animation); |
| 559 | } |
| 560 | } |
| 561 | mShutterButton = findViewById(R.id.shutter_button); |
| 562 | mShutterButton.setOnClickListener(this); |
| 563 | |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 564 | Uri uri = getIntent().getData(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 565 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 566 | if (instanceState != null) { |
| 567 | if (instanceState.containsKey("uri")) { |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 568 | uri = Uri.parse(instanceState.getString("uri")); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 569 | } |
| 570 | } |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 571 | if (uri == null) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 572 | finish(); |
| 573 | return; |
| 574 | } |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 575 | init(uri); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 576 | |
| 577 | Bundle b = getIntent().getExtras(); |
| 578 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 579 | boolean slideShow = (b != null) |
| 580 | ? b.getBoolean("slideshow", false) |
| 581 | : false; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 582 | if (slideShow) { |
| 583 | setMode(MODE_SLIDESHOW); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 584 | } else { |
| 585 | if (mFullScreenInNormalMode) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 586 | getWindow().addFlags( |
| 587 | WindowManager.LayoutParams.FLAG_FULLSCREEN); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 588 | } |
| 589 | if (mShowActionIcons) { |
| 590 | mActionIconPanel.setVisibility(View.VISIBLE); |
| 591 | mShutterButton.setVisibility(View.VISIBLE); |
| 592 | } |
| 593 | } |
| 594 | |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 595 | setupZoomButtonController(findViewById(R.id.rootLayout)); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 596 | setupDismissOnScreenControlRunnable(); |
| 597 | |
| 598 | mNextImageView = findViewById(R.id.next_image); |
| 599 | mPrevImageView = findViewById(R.id.prev_image); |
| 600 | mNextImageView.setOnClickListener(this); |
| 601 | mPrevImageView.setOnClickListener(this); |
| 602 | |
| 603 | if (mShowActionIcons) { |
| 604 | mNextImageView.setFocusable(true); |
| 605 | mPrevImageView.setFocusable(true); |
| 606 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 609 | private void setRequestedOrientation() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 610 | Intent intent = getIntent(); |
| 611 | if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 612 | int orientation = intent.getIntExtra( |
| 613 | MediaStore.EXTRA_SCREEN_ORIENTATION, |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 614 | ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); |
| 615 | if (orientation != getRequestedOrientation()) { |
| 616 | setRequestedOrientation(orientation); |
| 617 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
| 621 | private Animation makeInAnimation(int id) { |
| 622 | Animation inAnimation = AnimationUtils.loadAnimation(this, id); |
| 623 | return inAnimation; |
| 624 | } |
| 625 | |
| 626 | private Animation makeOutAnimation(int id) { |
| 627 | Animation outAnimation = AnimationUtils.loadAnimation(this, id); |
| 628 | return outAnimation; |
| 629 | } |
| 630 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 631 | void setMode(int mode) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 632 | if (mMode == mode) { |
| 633 | return; |
| 634 | } |
| 635 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 636 | findViewById(R.id.slideShowContainer).setVisibility( |
| 637 | mode == MODE_SLIDESHOW ? View.VISIBLE : View.GONE); |
| 638 | findViewById(R.id.abs).setVisibility(mode == MODE_NORMAL |
| 639 | ? View.VISIBLE : View.GONE); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 640 | |
| 641 | Window win = getWindow(); |
| 642 | mMode = mode; |
| 643 | if (mode == MODE_SLIDESHOW) { |
| 644 | win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
| 645 | | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 646 | mImageView.clear(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 647 | mActionIconPanel.setVisibility(View.GONE); |
| 648 | mShutterButton.setVisibility(View.GONE); |
| 649 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 650 | findViewById(R.id.slideShowContainer).getRootView().requestLayout(); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 651 | |
| 652 | // The preferences we want to read: |
| 653 | // mUseShuffleOrder |
| 654 | // mSlideShowLoop |
| 655 | // mAnimationIndex |
| 656 | // mSlideShowInterval |
| 657 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 658 | mUseShuffleOrder = mPrefs.getBoolean( |
| 659 | "pref_gallery_slideshow_shuffle_key", false); |
| 660 | mSlideShowLoop = mPrefs.getBoolean( |
| 661 | "pref_gallery_slideshow_repeat_key", false); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 662 | try { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 663 | mAnimationIndex = Integer.parseInt(mPrefs.getString( |
| 664 | "pref_gallery_slideshow_transition_key", "0")); |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 665 | } catch (NumberFormatException ex) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 666 | Log.e(TAG, "couldn't parse preference: " + ex.toString()); |
| 667 | mAnimationIndex = 0; |
| 668 | } |
| 669 | try { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 670 | mSlideShowInterval = Integer.parseInt(mPrefs.getString( |
| 671 | "pref_gallery_slideshow_interval_key", "3")) * 1000; |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 672 | } catch (NumberFormatException ex) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 673 | Log.e(TAG, "couldn't parse preference: " + ex.toString()); |
| 674 | mSlideShowInterval = 3000; |
| 675 | } |
| 676 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 677 | |
| 678 | if (mUseShuffleOrder) { |
| 679 | generateShuffleOrder(); |
| 680 | } |
| 681 | } else { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 682 | win.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 683 | if (mFullScreenInNormalMode) { |
| 684 | win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 685 | } else { |
| 686 | win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 687 | } |
| 688 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 689 | if (mGetter != null) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 690 | mGetter.cancelCurrent(); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 691 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 692 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 693 | if (mShowActionIcons) { |
| 694 | mActionIconPanel.setVisibility(View.VISIBLE); |
| 695 | mShutterButton.setVisibility(View.VISIBLE); |
| 696 | } |
| 697 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 698 | ImageViewTouchBase dst = mImageView; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 699 | dst.mLastXTouchPos = -1; |
| 700 | dst.mLastYTouchPos = -1; |
| 701 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 702 | for (ImageViewTouchBase ivt : mSlideShowImageViews) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 703 | ivt.clear(); |
| 704 | } |
| 705 | |
| 706 | mShuffleOrder = null; |
| 707 | |
| 708 | // mGetter null is a proxy for being paused |
| 709 | if (mGetter != null) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 710 | setImage(mCurrentPosition); |
| 711 | } |
| 712 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | private void generateShuffleOrder() { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 716 | if (mShuffleOrder == null |
| 717 | || mShuffleOrder.length != mAllImages.getCount()) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 718 | mShuffleOrder = new int[mAllImages.getCount()]; |
| 719 | } |
| 720 | |
| 721 | for (int i = 0; i < mShuffleOrder.length; i++) { |
| 722 | mShuffleOrder[i] = i; |
| 723 | } |
| 724 | |
| 725 | for (int i = mShuffleOrder.length - 1; i > 0; i--) { |
| 726 | int r = mRandom.nextInt(i); |
| 727 | int tmp = mShuffleOrder[r]; |
| 728 | mShuffleOrder[r] = mShuffleOrder[i]; |
| 729 | mShuffleOrder[i] = tmp; |
| 730 | } |
| 731 | } |
| 732 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 733 | private void loadNextImage(final int requestedPos, final long delay, |
| 734 | final boolean firstCall) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 735 | if (firstCall && mUseShuffleOrder) { |
| 736 | generateShuffleOrder(); |
| 737 | } |
| 738 | |
| 739 | final long targetDisplayTime = System.currentTimeMillis() + delay; |
| 740 | |
| 741 | ImageGetterCallback cb = new ImageGetterCallback() { |
| 742 | public void completed(boolean wasCanceled) { |
| 743 | } |
| 744 | |
| 745 | public boolean wantsThumbnail(int pos, int offset) { |
| 746 | return true; |
| 747 | } |
| 748 | |
| 749 | public boolean wantsFullImage(int pos, int offset) { |
| 750 | return false; |
| 751 | } |
| 752 | |
| 753 | public int [] loadOrder() { |
| 754 | return sOrder_slideshow; |
| 755 | } |
| 756 | |
| 757 | public int fullImageSizeToUse(int pos, int offset) { |
| 758 | return 480; // TODO compute this |
| 759 | } |
| 760 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 761 | public void imageLoaded(final int pos, final int offset, |
| 762 | final Bitmap bitmap, final boolean isThumb) { |
| 763 | long timeRemaining = Math.max(0, |
| 764 | targetDisplayTime - System.currentTimeMillis()); |
Owen Lin | 3102f06 | 2009-03-24 20:38:25 -0700 | [diff] [blame] | 765 | mHandler.postDelayedGetterCallback(new Runnable() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 766 | public void run() { |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 767 | if (mMode == MODE_NORMAL) { |
| 768 | return; |
| 769 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 770 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 771 | ImageViewTouchBase oldView = |
| 772 | mSlideShowImageViews[mSlideShowImageCurrent]; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 773 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 774 | if (++mSlideShowImageCurrent |
| 775 | == mSlideShowImageViews.length) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 776 | mSlideShowImageCurrent = 0; |
| 777 | } |
| 778 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 779 | ImageViewTouchBase newView = |
| 780 | mSlideShowImageViews[mSlideShowImageCurrent]; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 781 | newView.setVisibility(View.VISIBLE); |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 782 | newView.setImageBitmapResetBase(bitmap, true); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 783 | newView.bringToFront(); |
| 784 | |
| 785 | int animation = 0; |
| 786 | |
| 787 | if (mAnimationIndex == -1) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 788 | int n = mRandom.nextInt( |
| 789 | mSlideShowInAnimation.length); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 790 | animation = n; |
| 791 | } else { |
| 792 | animation = mAnimationIndex; |
| 793 | } |
| 794 | |
| 795 | Animation aIn = mSlideShowInAnimation[animation]; |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 796 | newView.startAnimation(aIn); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 797 | newView.setVisibility(View.VISIBLE); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 798 | |
| 799 | Animation aOut = mSlideShowOutAnimation[animation]; |
| 800 | oldView.setVisibility(View.INVISIBLE); |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 801 | oldView.startAnimation(aOut); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 802 | |
| 803 | mCurrentPosition = requestedPos; |
| 804 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 805 | if (mCurrentPosition == mLastSlideShowImage |
| 806 | && !firstCall) { |
Owen Lin | 3102f06 | 2009-03-24 20:38:25 -0700 | [diff] [blame] | 807 | if (mSlideShowLoop) { |
| 808 | if (mUseShuffleOrder) { |
| 809 | generateShuffleOrder(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 810 | } |
Owen Lin | 3102f06 | 2009-03-24 20:38:25 -0700 | [diff] [blame] | 811 | } else { |
| 812 | setMode(MODE_NORMAL); |
| 813 | return; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 814 | } |
Owen Lin | 3102f06 | 2009-03-24 20:38:25 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 817 | loadNextImage( |
| 818 | (mCurrentPosition + 1) % mAllImages.getCount(), |
| 819 | mSlideShowInterval, false); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 820 | } |
| 821 | }, timeRemaining); |
| 822 | } |
| 823 | }; |
| 824 | // Could be null if we're stopping a slide show in the course of pausing |
| 825 | if (mGetter != null) { |
| 826 | int pos = requestedPos; |
| 827 | if (mShuffleOrder != null) { |
| 828 | pos = mShuffleOrder[pos]; |
| 829 | } |
| 830 | mGetter.setPosition(pos, cb); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | private void makeGetter() { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 835 | mGetter = new ImageGetter(this); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | private boolean desiredSortOrder() { |
| 839 | String sortOrder = mPrefs.getString("pref_gallery_sort_key", null); |
| 840 | boolean sortAscending = false; |
| 841 | if (sortOrder != null) { |
| 842 | sortAscending = sortOrder.equals("ascending"); |
| 843 | } |
| 844 | if (mCameraReviewMode) { |
| 845 | // Force left-arrow older pictures, right-arrow newer pictures. |
| 846 | sortAscending = true; |
| 847 | } |
| 848 | return sortAscending; |
| 849 | } |
| 850 | |
| 851 | private void init(Uri uri) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 852 | if (uri == null) { |
Ray Chen | 3593747 | 2009-03-24 20:39:36 -0700 | [diff] [blame] | 853 | return; |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 854 | } |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 855 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 856 | mSortAscending = desiredSortOrder(); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 857 | int sort = mSortAscending |
| 858 | ? ImageManager.SORT_ASCENDING |
| 859 | : ImageManager.SORT_DESCENDING; |
Chih-Chung Chang | a6e2144 | 2009-04-17 23:18:09 +0800 | [diff] [blame] | 860 | mAllImages = ImageManager.makeImageList(uri, getContentResolver(), |
| 861 | sort); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 862 | |
| 863 | uri = uri.buildUpon().query(null).build(); |
Chih-Chung Chang | fd6da32 | 2009-05-04 16:49:30 +0800 | [diff] [blame] | 864 | |
| 865 | IImage image = mAllImages.getImageForUri(uri); |
| 866 | if (image != null) { |
| 867 | mCurrentPosition = image.getRow(); |
| 868 | mLastSlideShowImage = mCurrentPosition; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 872 | private Uri getCurrentUri() { |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 873 | IImage image = mAllImages.getImageAt(mCurrentPosition); |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 874 | Uri uri = null; |
| 875 | if (image != null){ |
| 876 | String bucket = null; |
| 877 | uri = image.fullSizeImageUri(); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 878 | if (getIntent() != null && getIntent().getData() != null) { |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 879 | bucket = getIntent().getData().getQueryParameter("bucketId"); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 880 | } |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 881 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 882 | if (bucket != null) { |
| 883 | uri = uri.buildUpon().appendQueryParameter("bucketId", bucket) |
| 884 | .build(); |
| 885 | } |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 886 | } |
| 887 | return uri; |
| 888 | } |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 889 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 890 | @Override |
| 891 | public void onSaveInstanceState(Bundle b) { |
| 892 | super.onSaveInstanceState(b); |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 893 | |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 894 | Uri uri = getCurrentUri(); |
| 895 | if (uri != null) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 896 | b.putString("uri", uri.toString()); |
| 897 | } |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 898 | |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 899 | if (mMode == MODE_SLIDESHOW) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 900 | b.putBoolean("slideshow", true); |
Ray Chen | 3593747 | 2009-03-24 20:39:36 -0700 | [diff] [blame] | 901 | } |
| 902 | } |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 903 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 904 | @Override |
Owen Lin | 9e0bda2 | 2009-05-04 17:10:37 -0700 | [diff] [blame^] | 905 | public void onStart() { |
| 906 | super.onStart(); |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 907 | |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 908 | BitmapManager.instance().allowAllDecoding(false); |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 909 | |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 910 | init(mSavedUri); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 911 | |
| 912 | // normally this will never be zero but if one "backs" into this |
| 913 | // activity after removing the sdcard it could be zero. in that |
| 914 | // case just "finish" since there's nothing useful that can happen. |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 915 | int count = mAllImages.getCount(); |
| 916 | if (count == 0) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 917 | finish(); |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 918 | } else if (count <= mCurrentPosition) { |
| 919 | mCurrentPosition = count - 1; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 920 | } |
| 921 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 922 | if (mGetter == null) { |
| 923 | makeGetter(); |
| 924 | } |
| 925 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 926 | if (mMode == MODE_SLIDESHOW) { |
| 927 | loadNextImage(mCurrentPosition, 0, true); |
| 928 | } else { // MODE_NORMAL |
| 929 | setImage(mCurrentPosition); |
| 930 | } |
The Android Open Source Project | e3f4516 | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | @Override |
Owen Lin | 9e0bda2 | 2009-05-04 17:10:37 -0700 | [diff] [blame^] | 934 | public void onStop() { |
| 935 | super.onStop(); |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 936 | BitmapManager.instance().cancelAllDecoding(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 937 | |
| 938 | mGetter.cancelCurrent(); |
| 939 | mGetter.stop(); |
| 940 | mGetter = null; |
| 941 | setMode(MODE_NORMAL); |
| 942 | |
Owen Lin | 3102f06 | 2009-03-24 20:38:25 -0700 | [diff] [blame] | 943 | // removing all callback in the message queue |
| 944 | mHandler.removeAllGetterCallbacks(); |
Owen Lin | 9b2e912 | 2009-03-27 15:10:14 -0700 | [diff] [blame] | 945 | |
Ray Chen | 72e1dfd | 2009-03-24 21:13:16 -0700 | [diff] [blame] | 946 | mSavedUri = getCurrentUri(); |
Owen Lin | 3102f06 | 2009-03-24 20:38:25 -0700 | [diff] [blame] | 947 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 948 | mAllImages.deactivate(); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 949 | mDismissOnScreenControlsRunnable.run(); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 950 | if (mDismissOnScreenControlsRunnable != null) { |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 951 | mHandler.removeCallbacks(mDismissOnScreenControlsRunnable); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 952 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 953 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 954 | mImageView.clear(); |
| 955 | mCache.clear(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 956 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 957 | for (ImageViewTouchBase iv : mSlideShowImageViews) { |
| 958 | iv.clear(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 959 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | public void onClick(View v) { |
| 963 | switch (v.getId()) { |
| 964 | |
| 965 | case R.id.shutter_button: { |
| 966 | if (mCameraReviewMode) { |
| 967 | finish(); |
| 968 | } else { |
| 969 | MenuHelper.gotoStillImageCapture(this); |
| 970 | } |
| 971 | } |
| 972 | break; |
| 973 | |
| 974 | case R.id.gallery: { |
| 975 | MenuHelper.gotoCameraImageGallery(this); |
| 976 | } |
| 977 | break; |
| 978 | |
| 979 | case R.id.discard: { |
Ray Chen | 7638a54 | 2009-03-24 20:37:45 -0700 | [diff] [blame] | 980 | MenuHelper.deletePhoto(this, mDeletePhotoRunnable); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 981 | } |
| 982 | break; |
| 983 | |
| 984 | case R.id.share: { |
| 985 | Uri u = mAllImages.getImageAt(mCurrentPosition).fullSizeImageUri(); |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 986 | if (MenuHelper.isMMSUri(u)) { |
| 987 | return; |
| 988 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 989 | Intent intent = new Intent(); |
| 990 | intent.setAction(Intent.ACTION_SEND); |
| 991 | intent.setType("image/jpeg"); |
| 992 | intent.putExtra(Intent.EXTRA_STREAM, u); |
| 993 | try { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 994 | startActivity(Intent.createChooser( |
| 995 | intent, getText(R.string.sendImage))); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 996 | } catch (android.content.ActivityNotFoundException ex) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 997 | Toast.makeText(this, R.string.no_way_to_share_image, |
| 998 | Toast.LENGTH_SHORT).show(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | break; |
| 1002 | |
| 1003 | case R.id.setas: { |
| 1004 | Uri u = mAllImages.getImageAt(mCurrentPosition).fullSizeImageUri(); |
| 1005 | Intent intent = new Intent(Intent.ACTION_ATTACH_DATA, u); |
| 1006 | try { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1007 | startActivity(Intent.createChooser( |
| 1008 | intent, getText(R.string.setImage))); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1009 | } catch (android.content.ActivityNotFoundException ex) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1010 | Toast.makeText(this, R.string.no_way_to_share_video, |
| 1011 | Toast.LENGTH_SHORT).show(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | break; |
| 1015 | |
| 1016 | case R.id.next_image: { |
| 1017 | moveNextOrPrevious(1); |
| 1018 | } |
| 1019 | break; |
| 1020 | |
| 1021 | case R.id.prev_image: { |
| 1022 | moveNextOrPrevious(-1); |
| 1023 | } |
| 1024 | break; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | private void moveNextOrPrevious(int delta) { |
| 1029 | int nextImagePos = mCurrentPosition + delta; |
| 1030 | if ((0 <= nextImagePos) && (nextImagePos < mAllImages.getCount())) { |
| 1031 | setImage(nextImagePos); |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | @Override |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1036 | protected void onActivityResult(int requestCode, int resultCode, |
| 1037 | Intent data) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1038 | switch (requestCode) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1039 | case MenuHelper.RESULT_COMMON_MENU_CROP: |
| 1040 | if (resultCode == RESULT_OK) { |
| 1041 | // The CropImage activity passes back the Uri of the |
| 1042 | // cropped image as the Action rather than the Data. |
| 1043 | mSavedUri = Uri.parse(data.getAction()); |
| 1044 | } |
| 1045 | break; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1046 | } |
| 1047 | } |
Owen Lin | 3102f06 | 2009-03-24 20:38:25 -0700 | [diff] [blame] | 1048 | |
| 1049 | static class LocalHandler extends Handler { |
| 1050 | private static final int IMAGE_GETTER_CALLBACK = 1; |
| 1051 | |
| 1052 | @Override |
| 1053 | public void handleMessage(Message message) { |
| 1054 | switch(message.what) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1055 | case IMAGE_GETTER_CALLBACK: |
| 1056 | ((Runnable) message.obj).run(); |
| 1057 | break; |
Owen Lin | 3102f06 | 2009-03-24 20:38:25 -0700 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | public void postGetterCallback(Runnable callback) { |
| 1062 | postDelayedGetterCallback(callback, 0); |
| 1063 | } |
| 1064 | |
| 1065 | public void postDelayedGetterCallback(Runnable callback, long delay) { |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1066 | if (callback == null) { |
| 1067 | throw new NullPointerException(); |
| 1068 | } |
Owen Lin | 3102f06 | 2009-03-24 20:38:25 -0700 | [diff] [blame] | 1069 | Message message = Message.obtain(); |
| 1070 | message.what = IMAGE_GETTER_CALLBACK; |
| 1071 | message.obj = callback; |
| 1072 | sendMessageDelayed(message, delay); |
| 1073 | } |
| 1074 | |
| 1075 | public void removeAllGetterCallbacks() { |
| 1076 | removeMessages(IMAGE_GETTER_CALLBACK); |
| 1077 | } |
| 1078 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1079 | } |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1080 | |
| 1081 | class ImageViewTouch extends ImageViewTouchBase { |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 1082 | private final ViewImage mViewImage; |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1083 | private boolean mEnableTrackballScroll; |
| 1084 | |
| 1085 | public ImageViewTouch(Context context) { |
| 1086 | super(context); |
| 1087 | mViewImage = (ViewImage) context; |
| 1088 | } |
| 1089 | |
| 1090 | public ImageViewTouch(Context context, AttributeSet attrs) { |
| 1091 | super(context, attrs); |
| 1092 | mViewImage = (ViewImage) context; |
| 1093 | } |
| 1094 | |
| 1095 | public void setEnableTrackballScroll(boolean enable) { |
| 1096 | mEnableTrackballScroll = enable; |
| 1097 | } |
| 1098 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 1099 | protected void postTranslateCenter(float dx, float dy) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1100 | super.postTranslate(dx, dy); |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 1101 | center(true, true); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 1104 | static final float PAN_RATE = 20; |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1105 | |
| 1106 | @Override |
| 1107 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 1108 | // Don't respond to arrow keys if trackball scrolling is not enabled |
| 1109 | if (!mEnableTrackballScroll) { |
| 1110 | if ((keyCode >= KeyEvent.KEYCODE_DPAD_UP) |
| 1111 | && (keyCode <= KeyEvent.KEYCODE_DPAD_RIGHT)) { |
| 1112 | return super.onKeyDown(keyCode, event); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | int current = mViewImage.mCurrentPosition; |
| 1117 | |
| 1118 | int nextImagePos = -2; // default no next image |
| 1119 | try { |
| 1120 | switch (keyCode) { |
| 1121 | case KeyEvent.KEYCODE_DPAD_CENTER: { |
| 1122 | if (mViewImage.isPickIntent()) { |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 1123 | IImage img = mViewImage.mAllImages |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1124 | .getImageAt(mViewImage.mCurrentPosition); |
| 1125 | mViewImage.setResult(ViewImage.RESULT_OK, |
| 1126 | new Intent().setData(img.fullSizeImageUri())); |
| 1127 | mViewImage.finish(); |
| 1128 | } |
| 1129 | break; |
| 1130 | } |
| 1131 | case KeyEvent.KEYCODE_DPAD_LEFT: { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1132 | int maxOffset = (current == 0) ? 0 : ViewImage.HYSTERESIS; |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 1133 | if (getScale() <= 1F |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1134 | || isShiftedToNextImage(true, maxOffset)) { |
| 1135 | nextImagePos = current - 1; |
| 1136 | } else { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 1137 | panBy(PAN_RATE, 0); |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 1138 | center(true, false); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1139 | } |
| 1140 | return true; |
| 1141 | } |
| 1142 | case KeyEvent.KEYCODE_DPAD_RIGHT: { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1143 | int maxOffset = |
| 1144 | (current == mViewImage.mAllImages.getCount() - 1) |
| 1145 | ? 0 |
| 1146 | : ViewImage.HYSTERESIS; |
| 1147 | if (getScale() <= 1F |
| 1148 | || isShiftedToNextImage(false, maxOffset)) { |
| 1149 | nextImagePos = current + 1; |
| 1150 | } else { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 1151 | panBy(-PAN_RATE, 0); |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 1152 | center(true, false); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1153 | } |
| 1154 | return true; |
| 1155 | } |
| 1156 | case KeyEvent.KEYCODE_DPAD_UP: { |
| 1157 | panBy(0, PAN_RATE); |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 1158 | center(false, true); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1159 | return true; |
| 1160 | } |
| 1161 | case KeyEvent.KEYCODE_DPAD_DOWN: { |
| 1162 | panBy(0, -PAN_RATE); |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 1163 | center(false, true); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1164 | return true; |
| 1165 | } |
| 1166 | case KeyEvent.KEYCODE_DEL: |
| 1167 | MenuHelper.deletePhoto( |
| 1168 | mViewImage, mViewImage.mDeletePhotoRunnable); |
| 1169 | break; |
| 1170 | } |
| 1171 | } finally { |
| 1172 | if (nextImagePos >= 0 |
| 1173 | && nextImagePos < mViewImage.mAllImages.getCount()) { |
| 1174 | synchronized (mViewImage) { |
| 1175 | mViewImage.setMode(ViewImage.MODE_NORMAL); |
| 1176 | mViewImage.setImage(nextImagePos); |
| 1177 | } |
| 1178 | } else if (nextImagePos != -2) { |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 1179 | center(true, true); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | return super.onKeyDown(keyCode, event); |
| 1184 | } |
| 1185 | |
| 1186 | protected boolean isShiftedToNextImage(boolean left, int maxOffset) { |
| 1187 | boolean retval; |
| 1188 | Bitmap bitmap = mBitmapDisplayed; |
| 1189 | Matrix m = getImageViewMatrix(); |
| 1190 | if (left) { |
| 1191 | float [] t1 = new float[] { 0, 0 }; |
| 1192 | m.mapPoints(t1); |
| 1193 | retval = t1[0] > maxOffset; |
| 1194 | } else { |
| 1195 | int width = bitmap != null ? bitmap.getWidth() : getWidth(); |
| 1196 | float [] t1 = new float[] { width, 0 }; |
| 1197 | m.mapPoints(t1); |
| 1198 | retval = t1[0] + maxOffset < getWidth(); |
| 1199 | } |
| 1200 | return retval; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | /* |
| 1205 | * Here's the loading strategy. For any given image, load the thumbnail |
| 1206 | * into memory and post a callback to display the resulting bitmap. |
| 1207 | * |
| 1208 | * Then proceed to load the full image bitmap. Three things can |
| 1209 | * happen at this point: |
| 1210 | * |
| 1211 | * 1. the image fails to load because the UI thread decided |
| 1212 | * to move on to a different image. This "cancellation" happens |
| 1213 | * by virtue of the UI thread closing the stream containing the |
| 1214 | * image being decoded. BitmapFactory.decodeStream returns null |
| 1215 | * in this case. |
| 1216 | * |
| 1217 | * 2. the image loaded successfully. At that point we post |
| 1218 | * a callback to the UI thread to actually show the bitmap. |
| 1219 | * |
| 1220 | * 3. when the post runs it checks to see if the image that was |
| 1221 | * loaded is still the one we want. The UI may have moved on |
| 1222 | * to some other image and if so we just drop the newly loaded |
| 1223 | * bitmap on the floor. |
| 1224 | */ |
| 1225 | |
| 1226 | interface ImageGetterCallback { |
| 1227 | public void imageLoaded(int pos, int offset, Bitmap bitmap, |
| 1228 | boolean isThumb); |
| 1229 | public boolean wantsThumbnail(int pos, int offset); |
| 1230 | public boolean wantsFullImage(int pos, int offset); |
| 1231 | public int fullImageSizeToUse(int pos, int offset); |
| 1232 | public void completed(boolean wasCanceled); |
| 1233 | public int [] loadOrder(); |
| 1234 | } |
| 1235 | |
| 1236 | class ImageGetter { |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 1237 | |
| 1238 | @SuppressWarnings("unused") |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1239 | private static final String TAG = "ImageGetter"; |
| 1240 | |
| 1241 | // The thread which does the work. |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 1242 | private final Thread mGetterThread; |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1243 | |
| 1244 | // The base position that's being retrieved. The actual images retrieved |
| 1245 | // are this base plus each of the offets. |
| 1246 | private int mCurrentPosition = -1; |
| 1247 | |
| 1248 | // The callback to invoke for each image. |
| 1249 | private ImageGetterCallback mCB; |
| 1250 | |
| 1251 | // This is the loader cancelable that gets set while we're loading an image. |
| 1252 | // If we change position we can cancel the current load using this. |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 1253 | private Cancelable<Bitmap> mLoad; |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1254 | |
| 1255 | // True if we're canceling the current load. |
| 1256 | private boolean mCancelCurrent = false; |
| 1257 | |
| 1258 | // True when the therad should exit. |
| 1259 | private boolean mDone = false; |
| 1260 | |
| 1261 | // True when the loader thread is waiting for work. |
| 1262 | private boolean mReady = false; |
| 1263 | |
| 1264 | // The ViewImage this ImageGetter belongs to |
| 1265 | ViewImage mViewImage; |
| 1266 | |
| 1267 | void cancelCurrent() { |
| 1268 | synchronized (this) { |
| 1269 | if (!mReady) { |
| 1270 | mCancelCurrent = true; |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 1271 | Cancelable<Bitmap> load = mLoad; |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1272 | if (load != null) { |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 1273 | load.requestCancel(); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1274 | } |
| 1275 | mCancelCurrent = false; |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | private class ImageGetterRunnable implements Runnable { |
| 1281 | private Runnable callback(final int position, final int offset, |
| 1282 | final boolean isThumb, final Bitmap bitmap) { |
| 1283 | return new Runnable() { |
| 1284 | public void run() { |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 1285 | // check for inflight callbacks that aren't applicable |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1286 | // any longer before delivering them |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1287 | if (!isCanceled() && position == mCurrentPosition) { |
| 1288 | mCB.imageLoaded(position, offset, bitmap, isThumb); |
| 1289 | } else if (bitmap != null) { |
| 1290 | bitmap.recycle(); |
| 1291 | } |
| 1292 | } |
| 1293 | }; |
| 1294 | } |
| 1295 | |
| 1296 | private Runnable completedCallback(final boolean wasCanceled) { |
| 1297 | return new Runnable() { |
| 1298 | public void run() { |
| 1299 | mCB.completed(wasCanceled); |
| 1300 | } |
| 1301 | }; |
| 1302 | } |
| 1303 | |
| 1304 | public void run() { |
| 1305 | int lastPosition = -1; |
| 1306 | while (!mDone) { |
| 1307 | synchronized (ImageGetter.this) { |
| 1308 | mReady = true; |
| 1309 | ImageGetter.this.notify(); |
| 1310 | |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 1311 | if (mCurrentPosition == -1 |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1312 | || lastPosition == mCurrentPosition) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1313 | try { |
| 1314 | ImageGetter.this.wait(); |
| 1315 | } catch (InterruptedException ex) { |
| 1316 | continue; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | lastPosition = mCurrentPosition; |
| 1321 | mReady = false; |
| 1322 | } |
| 1323 | |
| 1324 | if (lastPosition != -1) { |
| 1325 | int imageCount = mViewImage.mAllImages.getCount(); |
| 1326 | |
| 1327 | int [] order = mCB.loadOrder(); |
| 1328 | for (int i = 0; i < order.length; i++) { |
| 1329 | int offset = order[i]; |
| 1330 | int imageNumber = lastPosition + offset; |
| 1331 | if (imageNumber >= 0 && imageNumber < imageCount) { |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1332 | IImage image = mViewImage.mAllImages |
| 1333 | .getImageAt(lastPosition + offset); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1334 | if (image == null || isCanceled()) { |
| 1335 | break; |
| 1336 | } |
| 1337 | if (mCB.wantsThumbnail(lastPosition, offset)) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1338 | Bitmap b = image.thumbBitmap(); |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1339 | mViewImage.mHandler.postGetterCallback( |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 1340 | callback(lastPosition, offset, |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1341 | true, b)); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | for (int i = 0; i < order.length; i++) { |
| 1347 | int offset = order[i]; |
| 1348 | int imageNumber = lastPosition + offset; |
| 1349 | if (imageNumber >= 0 && imageNumber < imageCount) { |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1350 | IImage image = mViewImage.mAllImages |
| 1351 | .getImageAt(lastPosition + offset); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1352 | if (mCB.wantsFullImage(lastPosition, offset)) { |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1353 | int sizeToUse = mCB.fullImageSizeToUse( |
| 1354 | lastPosition, offset); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1355 | if (image != null && !isCanceled()) { |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1356 | mLoad = image.fullSizeBitmapCancelable( |
| 1357 | sizeToUse); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1358 | } |
| 1359 | if (mLoad != null) { |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1360 | // The return value could be null if the |
| 1361 | // bitmap is too big, or we cancelled it. |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 1362 | Bitmap b; |
| 1363 | try { |
| 1364 | b = mLoad.get(); |
| 1365 | } catch (InterruptedException e) { |
| 1366 | b = null; |
| 1367 | } catch (ExecutionException e) { |
| 1368 | throw new RuntimeException(e); |
| 1369 | } catch (CancellationException e) { |
| 1370 | b = null; |
| 1371 | } |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1372 | mLoad = null; |
| 1373 | if (b != null) { |
| 1374 | if (isCanceled()) { |
| 1375 | b.recycle(); |
| 1376 | } else { |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1377 | Runnable cb = callback( |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 1378 | lastPosition, offset, |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1379 | false, b); |
| 1380 | mViewImage.mHandler |
| 1381 | .postGetterCallback(cb); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | mViewImage.mHandler.postGetterCallback( |
| 1389 | completedCallback(isCanceled())); |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | } |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 1394 | |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1395 | public ImageGetter(ViewImage viewImage) { |
| 1396 | mViewImage = viewImage; |
| 1397 | mGetterThread = new Thread(new ImageGetterRunnable()); |
| 1398 | mGetterThread.setName("ImageGettter"); |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1399 | BitmapManager.instance().allowThreadDecoding(mGetterThread); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1400 | mGetterThread.start(); |
| 1401 | } |
| 1402 | |
| 1403 | private boolean isCanceled() { |
| 1404 | synchronized (this) { |
| 1405 | return mCancelCurrent; |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | public void setPosition(int position, ImageGetterCallback cb) { |
| 1410 | synchronized (this) { |
| 1411 | if (!mReady) { |
| 1412 | try { |
| 1413 | mCancelCurrent = true; |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1414 | // if the thread is waiting before loading the full size |
| 1415 | // image then this will free it up |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1416 | BitmapManager.instance() |
| 1417 | .cancelThreadDecoding(mGetterThread); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1418 | ImageGetter.this.notify(); |
| 1419 | ImageGetter.this.wait(); |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1420 | BitmapManager.instance() |
| 1421 | .allowThreadDecoding(mGetterThread); |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1422 | mCancelCurrent = false; |
| 1423 | } catch (InterruptedException ex) { |
| 1424 | // not sure what to do here |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | mCurrentPosition = position; |
| 1430 | mCB = cb; |
| 1431 | |
| 1432 | synchronized (this) { |
| 1433 | ImageGetter.this.notify(); |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | public void stop() { |
| 1438 | synchronized (this) { |
| 1439 | mDone = true; |
| 1440 | ImageGetter.this.notify(); |
| 1441 | } |
| 1442 | try { |
| 1443 | mGetterThread.join(); |
| 1444 | } catch (InterruptedException ex) { |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 1445 | // Ignore the exception |
Chih-Chung Chang | 3321d40 | 2009-04-02 21:45:14 -0700 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | } |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 1449 | |
| 1450 | // This is a cache for Bitmap displayed in ViewImage (normal mode, thumb only). |
| 1451 | class BitmapCache implements ImageViewTouchBase.Recycler { |
Chih-Chung Chang | 73b7a3a | 2009-04-12 23:28:16 -0700 | [diff] [blame] | 1452 | public static class Entry { |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 1453 | int mPos; |
| 1454 | Bitmap mBitmap; |
| 1455 | public Entry() { |
| 1456 | clear(); |
| 1457 | } |
| 1458 | public void clear() { |
| 1459 | mPos = -1; |
| 1460 | mBitmap = null; |
| 1461 | } |
| 1462 | } |
| 1463 | |
Owen Lin | 41a8578 | 2009-04-20 15:59:57 +0800 | [diff] [blame] | 1464 | private final Entry[] mCache; |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 1465 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 1466 | public BitmapCache(int size) { |
| 1467 | mCache = new Entry[size]; |
| 1468 | for (int i = 0; i < mCache.length; i++) { |
| 1469 | mCache[i] = new Entry(); |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | // Given the position, find the associated entry. Returns null if there is |
| 1474 | // no such entry. |
| 1475 | private Entry findEntry(int pos) { |
| 1476 | for (Entry e : mCache) { |
| 1477 | if (pos == e.mPos) { |
| 1478 | return e; |
| 1479 | } |
| 1480 | } |
| 1481 | return null; |
| 1482 | } |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 1483 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 1484 | // Returns the thumb bitmap if we have it, otherwise return null. |
| 1485 | public synchronized Bitmap getBitmap(int pos) { |
| 1486 | Entry e = findEntry(pos); |
| 1487 | if (e != null) { |
| 1488 | return e.mBitmap; |
| 1489 | } |
| 1490 | return null; |
| 1491 | } |
| 1492 | |
| 1493 | public synchronized void put(int pos, Bitmap bitmap) { |
| 1494 | // First see if we already have this entry. |
| 1495 | if (findEntry(pos) != null) { |
| 1496 | return; |
| 1497 | } |
| 1498 | |
| 1499 | // Find the best entry we should replace. |
| 1500 | // See if there is any empty entry. |
| 1501 | // Otherwise assuming sequential access, kick out the entry with the |
| 1502 | // greatest distance. |
| 1503 | Entry best = null; |
| 1504 | int maxDist = -1; |
| 1505 | for (Entry e : mCache) { |
| 1506 | if (e.mPos == -1) { |
| 1507 | best = e; |
| 1508 | break; |
| 1509 | } else { |
| 1510 | int dist = Math.abs(pos - e.mPos); |
| 1511 | if (dist > maxDist) { |
| 1512 | maxDist = dist; |
| 1513 | best = e; |
| 1514 | } |
| 1515 | } |
| 1516 | } |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 1517 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 1518 | // Recycle the image being kicked out. |
| 1519 | // This only works because our current usage is sequential, so we |
| 1520 | // do not happen to recycle the image being displayed. |
| 1521 | if (best.mBitmap != null) { |
| 1522 | best.mBitmap.recycle(); |
| 1523 | } |
| 1524 | |
| 1525 | best.mPos = pos; |
| 1526 | best.mBitmap = bitmap; |
| 1527 | } |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 1528 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 1529 | // Recycle all bitmaps in the cache and clear the cache. |
| 1530 | public synchronized void clear() { |
| 1531 | for (Entry e : mCache) { |
| 1532 | if (e.mBitmap != null) { |
| 1533 | e.mBitmap.recycle(); |
| 1534 | } |
| 1535 | e.clear(); |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | // Returns whether the bitmap is in the cache. |
| 1540 | public synchronized boolean hasBitmap(int pos) { |
| 1541 | Entry e = findEntry(pos); |
| 1542 | return (e != null); |
| 1543 | } |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 1544 | |
Chih-Chung Chang | 7db87ab | 2009-04-12 21:42:43 -0700 | [diff] [blame] | 1545 | // Recycle the bitmap if it's not in the cache. |
| 1546 | // The input must be non-null. |
| 1547 | public synchronized void recycle(Bitmap b) { |
| 1548 | for (Entry e : mCache) { |
| 1549 | if (e.mPos != -1) { |
| 1550 | if (e.mBitmap == b) { |
| 1551 | return; |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | b.recycle(); |
| 1556 | } |
| 1557 | } |