Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.gallery3d.app; |
| 18 | |
Ray Chen | 1e86a2b | 2011-09-27 15:48:08 +0800 | [diff] [blame] | 19 | import android.app.Activity; |
Ray Chen | 1e86a2b | 2011-09-27 15:48:08 +0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.net.Uri; |
| 23 | import android.os.Bundle; |
| 24 | import android.os.Vibrator; |
| 25 | import android.provider.MediaStore; |
| 26 | import android.view.ActionMode; |
| 27 | import android.view.Menu; |
| 28 | import android.view.MenuInflater; |
| 29 | import android.view.MenuItem; |
| 30 | import android.widget.Toast; |
| 31 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 32 | import com.android.gallery3d.R; |
| 33 | import com.android.gallery3d.common.Utils; |
| 34 | import com.android.gallery3d.data.DataManager; |
| 35 | import com.android.gallery3d.data.MediaDetails; |
| 36 | import com.android.gallery3d.data.MediaItem; |
| 37 | import com.android.gallery3d.data.MediaObject; |
| 38 | import com.android.gallery3d.data.MediaSet; |
| 39 | import com.android.gallery3d.data.MtpDevice; |
| 40 | import com.android.gallery3d.data.Path; |
| 41 | import com.android.gallery3d.ui.ActionModeHandler; |
| 42 | import com.android.gallery3d.ui.ActionModeHandler.ActionModeListener; |
| 43 | import com.android.gallery3d.ui.AlbumView; |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 44 | import com.android.gallery3d.ui.DetailsHelper; |
| 45 | import com.android.gallery3d.ui.DetailsHelper.CloseListener; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 46 | import com.android.gallery3d.ui.GLCanvas; |
| 47 | import com.android.gallery3d.ui.GLView; |
| 48 | import com.android.gallery3d.ui.GridDrawer; |
| 49 | import com.android.gallery3d.ui.HighlightDrawer; |
| 50 | import com.android.gallery3d.ui.PositionProvider; |
| 51 | import com.android.gallery3d.ui.PositionRepository; |
| 52 | import com.android.gallery3d.ui.PositionRepository.Position; |
| 53 | import com.android.gallery3d.ui.SelectionManager; |
| 54 | import com.android.gallery3d.ui.SlotView; |
| 55 | import com.android.gallery3d.ui.StaticBackground; |
| 56 | import com.android.gallery3d.util.Future; |
| 57 | import com.android.gallery3d.util.GalleryUtils; |
| 58 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 59 | import java.util.Random; |
| 60 | |
| 61 | public class AlbumPage extends ActivityState implements GalleryActionBar.ClusterRunner, |
Hung-ying Tyan | 9d62da7 | 2011-09-30 14:42:50 +0800 | [diff] [blame] | 62 | SelectionManager.SelectionListener, MediaSet.SyncListener { |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 63 | @SuppressWarnings("unused") |
| 64 | private static final String TAG = "AlbumPage"; |
| 65 | |
| 66 | public static final String KEY_MEDIA_PATH = "media-path"; |
| 67 | public static final String KEY_SET_CENTER = "set-center"; |
| 68 | public static final String KEY_AUTO_SELECT_ALL = "auto-select-all"; |
| 69 | public static final String KEY_SHOW_CLUSTER_MENU = "cluster-menu"; |
| 70 | |
| 71 | private static final int REQUEST_SLIDESHOW = 1; |
| 72 | private static final int REQUEST_PHOTO = 2; |
| 73 | private static final int REQUEST_DO_ANIMATION = 3; |
| 74 | |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 75 | private static final int BIT_LOADING_RELOAD = 1; |
| 76 | private static final int BIT_LOADING_SYNC = 2; |
| 77 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 78 | private static final float USER_DISTANCE_METER = 0.3f; |
| 79 | |
| 80 | private boolean mIsActive = false; |
| 81 | private StaticBackground mStaticBackground; |
| 82 | private AlbumView mAlbumView; |
| 83 | private Path mMediaSetPath; |
| 84 | |
| 85 | private AlbumDataAdapter mAlbumDataAdapter; |
| 86 | |
| 87 | protected SelectionManager mSelectionManager; |
Ray Chen | 1e86a2b | 2011-09-27 15:48:08 +0800 | [diff] [blame] | 88 | private Vibrator mVibrator; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 89 | private GridDrawer mGridDrawer; |
| 90 | private HighlightDrawer mHighlightDrawer; |
| 91 | |
| 92 | private boolean mGetContent; |
| 93 | private boolean mShowClusterMenu; |
| 94 | |
| 95 | private ActionMode mActionMode; |
| 96 | private ActionModeHandler mActionModeHandler; |
| 97 | private int mFocusIndex = 0; |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 98 | private DetailsHelper mDetailsHelper; |
| 99 | private MyDetailsSource mDetailsSource; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 100 | private MediaSet mMediaSet; |
| 101 | private boolean mShowDetails; |
| 102 | private float mUserDistance; // in pixel |
| 103 | |
Hung-ying Tyan | 9d62da7 | 2011-09-30 14:42:50 +0800 | [diff] [blame] | 104 | private Future<Integer> mSyncTask = null; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 105 | |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 106 | private int mLoadingBits = 0; |
| 107 | private boolean mInitialSynced = false; |
| 108 | |
Ray Chen | 83046ac | 2011-10-06 18:03:33 +0800 | [diff] [blame] | 109 | private final GLView mRootPane = new GLView() { |
| 110 | private final float mMatrix[] = new float[16]; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 111 | |
| 112 | @Override |
| 113 | protected void onLayout( |
| 114 | boolean changed, int left, int top, int right, int bottom) { |
| 115 | mStaticBackground.layout(0, 0, right - left, bottom - top); |
| 116 | |
| 117 | int slotViewTop = GalleryActionBar.getHeight((Activity) mActivity); |
| 118 | int slotViewBottom = bottom - top; |
| 119 | int slotViewRight = right - left; |
| 120 | |
| 121 | if (mShowDetails) { |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 122 | mDetailsHelper.layout(left, slotViewTop, right, bottom); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 123 | } else { |
| 124 | mAlbumView.setSelectionDrawer(mGridDrawer); |
| 125 | } |
| 126 | |
| 127 | mAlbumView.layout(0, slotViewTop, slotViewRight, slotViewBottom); |
| 128 | GalleryUtils.setViewPointMatrix(mMatrix, |
| 129 | (right - left) / 2, (bottom - top) / 2, -mUserDistance); |
| 130 | PositionRepository.getInstance(mActivity).setOffset( |
| 131 | 0, slotViewTop); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | protected void render(GLCanvas canvas) { |
| 136 | canvas.save(GLCanvas.SAVE_FLAG_MATRIX); |
| 137 | canvas.multiplyMatrix(mMatrix, 0); |
| 138 | super.render(canvas); |
| 139 | canvas.restore(); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | @Override |
| 144 | protected void onBackPressed() { |
| 145 | if (mShowDetails) { |
| 146 | hideDetails(); |
| 147 | } else if (mSelectionManager.inSelectionMode()) { |
| 148 | mSelectionManager.leaveSelectionMode(); |
| 149 | } else { |
| 150 | mAlbumView.savePositions(PositionRepository.getInstance(mActivity)); |
| 151 | super.onBackPressed(); |
| 152 | } |
| 153 | } |
| 154 | |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 155 | private void onDown(int index) { |
| 156 | MediaItem item = mAlbumDataAdapter.get(index); |
| 157 | Path path = (item == null) ? null : item.getPath(); |
| 158 | mSelectionManager.setPressedPath(path); |
| 159 | mAlbumView.invalidate(); |
| 160 | } |
| 161 | |
| 162 | private void onUp() { |
| 163 | mSelectionManager.setPressedPath(null); |
| 164 | mAlbumView.invalidate(); |
| 165 | } |
| 166 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 167 | public void onSingleTapUp(int slotIndex) { |
| 168 | MediaItem item = mAlbumDataAdapter.get(slotIndex); |
| 169 | if (item == null) { |
| 170 | Log.w(TAG, "item not ready yet, ignore the click"); |
| 171 | return; |
| 172 | } |
| 173 | if (mShowDetails) { |
| 174 | mHighlightDrawer.setHighlightItem(item.getPath()); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 175 | mDetailsHelper.reloadDetails(slotIndex); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 176 | } else if (!mSelectionManager.inSelectionMode()) { |
| 177 | if (mGetContent) { |
| 178 | onGetContent(item); |
| 179 | } else { |
Chih-Chung Chang | 0358bbb | 2011-11-10 18:01:33 +0800 | [diff] [blame] | 180 | // Get into the PhotoPage. |
| 181 | Bundle data = new Bundle(); |
| 182 | mAlbumView.savePositions(PositionRepository.getInstance(mActivity)); |
| 183 | data.putInt(PhotoPage.KEY_INDEX_HINT, slotIndex); |
| 184 | data.putString(PhotoPage.KEY_MEDIA_SET_PATH, |
| 185 | mMediaSetPath.toString()); |
| 186 | data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH, |
| 187 | item.getPath().toString()); |
| 188 | mActivity.getStateManager().startStateForResult( |
| 189 | PhotoPage.class, REQUEST_PHOTO, data); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 190 | } |
| 191 | } else { |
| 192 | mSelectionManager.toggle(item.getPath()); |
Ray Chen | 83046ac | 2011-10-06 18:03:33 +0800 | [diff] [blame] | 193 | mDetailsSource.findIndex(slotIndex); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 194 | mAlbumView.invalidate(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | private void onGetContent(final MediaItem item) { |
| 199 | DataManager dm = mActivity.getDataManager(); |
| 200 | Activity activity = (Activity) mActivity; |
| 201 | if (mData.getString(Gallery.EXTRA_CROP) != null) { |
| 202 | // TODO: Handle MtpImagew |
| 203 | Uri uri = dm.getContentUri(item.getPath()); |
| 204 | Intent intent = new Intent(CropImage.ACTION_CROP, uri) |
| 205 | .addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT) |
| 206 | .putExtras(getData()); |
| 207 | if (mData.getParcelable(MediaStore.EXTRA_OUTPUT) == null) { |
| 208 | intent.putExtra(CropImage.KEY_RETURN_DATA, true); |
| 209 | } |
| 210 | activity.startActivity(intent); |
| 211 | activity.finish(); |
| 212 | } else { |
| 213 | activity.setResult(Activity.RESULT_OK, |
| 214 | new Intent(null, item.getContentUri())); |
| 215 | activity.finish(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | public void onLongTap(int slotIndex) { |
| 220 | if (mGetContent) return; |
| 221 | if (mShowDetails) { |
| 222 | onSingleTapUp(slotIndex); |
| 223 | } else { |
| 224 | MediaItem item = mAlbumDataAdapter.get(slotIndex); |
| 225 | if (item == null) return; |
| 226 | mSelectionManager.setAutoLeaveSelectionMode(true); |
| 227 | mSelectionManager.toggle(item.getPath()); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 228 | mDetailsSource.findIndex(slotIndex); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 229 | mAlbumView.invalidate(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | public void doCluster(int clusterType) { |
| 234 | String basePath = mMediaSet.getPath().toString(); |
| 235 | String newPath = FilterUtils.newClusterPath(basePath, clusterType); |
| 236 | Bundle data = new Bundle(getData()); |
| 237 | data.putString(AlbumSetPage.KEY_MEDIA_PATH, newPath); |
| 238 | if (mShowClusterMenu) { |
| 239 | Context context = mActivity.getAndroidContext(); |
| 240 | data.putString(AlbumSetPage.KEY_SET_TITLE, mMediaSet.getName()); |
| 241 | data.putString(AlbumSetPage.KEY_SET_SUBTITLE, |
| 242 | GalleryActionBar.getClusterByTypeString(context, clusterType)); |
| 243 | } |
| 244 | |
| 245 | mAlbumView.savePositions(PositionRepository.getInstance(mActivity)); |
| 246 | mActivity.getStateManager().startStateForResult( |
| 247 | AlbumSetPage.class, REQUEST_DO_ANIMATION, data); |
| 248 | } |
| 249 | |
| 250 | public void doFilter(int filterType) { |
| 251 | String basePath = mMediaSet.getPath().toString(); |
| 252 | String newPath = FilterUtils.switchFilterPath(basePath, filterType); |
| 253 | Bundle data = new Bundle(getData()); |
| 254 | data.putString(AlbumPage.KEY_MEDIA_PATH, newPath); |
| 255 | mAlbumView.savePositions(PositionRepository.getInstance(mActivity)); |
| 256 | mActivity.getStateManager().switchState(this, AlbumPage.class, data); |
| 257 | } |
| 258 | |
| 259 | public void onOperationComplete() { |
| 260 | mAlbumView.invalidate(); |
| 261 | // TODO: enable animation |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | protected void onCreate(Bundle data, Bundle restoreState) { |
| 266 | mUserDistance = GalleryUtils.meterToPixel(USER_DISTANCE_METER); |
| 267 | initializeViews(); |
| 268 | initializeData(data); |
| 269 | mGetContent = data.getBoolean(Gallery.KEY_GET_CONTENT, false); |
| 270 | mShowClusterMenu = data.getBoolean(KEY_SHOW_CLUSTER_MENU, false); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 271 | mDetailsSource = new MyDetailsSource(); |
Ray Chen | 1e86a2b | 2011-09-27 15:48:08 +0800 | [diff] [blame] | 272 | Context context = mActivity.getAndroidContext(); |
| 273 | mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 274 | |
| 275 | startTransition(data); |
| 276 | |
| 277 | // Enable auto-select-all for mtp album |
| 278 | if (data.getBoolean(KEY_AUTO_SELECT_ALL)) { |
| 279 | mSelectionManager.selectAll(); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | private void startTransition() { |
| 284 | final PositionRepository repository = |
| 285 | PositionRepository.getInstance(mActivity); |
| 286 | mAlbumView.startTransition(new PositionProvider() { |
Ray Chen | 83046ac | 2011-10-06 18:03:33 +0800 | [diff] [blame] | 287 | private final Position mTempPosition = new Position(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 288 | public Position getPosition(long identity, Position target) { |
| 289 | Position p = repository.get(identity); |
| 290 | if (p != null) return p; |
| 291 | mTempPosition.set(target); |
| 292 | mTempPosition.z = 128; |
| 293 | return mTempPosition; |
| 294 | } |
| 295 | }); |
| 296 | } |
| 297 | |
| 298 | private void startTransition(Bundle data) { |
| 299 | final PositionRepository repository = |
| 300 | PositionRepository.getInstance(mActivity); |
| 301 | final int[] center = data == null |
| 302 | ? null |
| 303 | : data.getIntArray(KEY_SET_CENTER); |
| 304 | final Random random = new Random(); |
| 305 | mAlbumView.startTransition(new PositionProvider() { |
Ray Chen | 83046ac | 2011-10-06 18:03:33 +0800 | [diff] [blame] | 306 | private final Position mTempPosition = new Position(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 307 | public Position getPosition(long identity, Position target) { |
| 308 | Position p = repository.get(identity); |
| 309 | if (p != null) return p; |
| 310 | if (center != null) { |
| 311 | random.setSeed(identity); |
| 312 | mTempPosition.set(center[0], center[1], |
| 313 | 0, random.nextInt(60) - 30, 0); |
| 314 | } else { |
| 315 | mTempPosition.set(target); |
| 316 | mTempPosition.z = 128; |
| 317 | } |
| 318 | return mTempPosition; |
| 319 | } |
| 320 | }); |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | protected void onResume() { |
| 325 | super.onResume(); |
| 326 | mIsActive = true; |
| 327 | setContentPane(mRootPane); |
Owen Lin | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 328 | |
| 329 | // Set the reload bit here to prevent it exit this page in clearLoadingBit(). |
| 330 | setLoadingBit(BIT_LOADING_RELOAD); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 331 | mAlbumDataAdapter.resume(); |
Owen Lin | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 332 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 333 | mAlbumView.resume(); |
| 334 | mActionModeHandler.resume(); |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 335 | if (!mInitialSynced) { |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 336 | setLoadingBit(BIT_LOADING_SYNC); |
Owen Lin | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 337 | mSyncTask = mMediaSet.requestSync(this); |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 338 | } |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | @Override |
| 342 | protected void onPause() { |
| 343 | super.onPause(); |
| 344 | mIsActive = false; |
| 345 | mAlbumDataAdapter.pause(); |
| 346 | mAlbumView.pause(); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 347 | DetailsHelper.pause(); |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 348 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 349 | if (mSyncTask != null) { |
| 350 | mSyncTask.cancel(); |
| 351 | mSyncTask = null; |
Chih-Chung Chang | 3cc0270 | 2012-01-09 12:41:57 +0800 | [diff] [blame^] | 352 | clearLoadingBit(BIT_LOADING_SYNC); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 353 | } |
| 354 | mActionModeHandler.pause(); |
Chih-Chung Chang | 3cc0270 | 2012-01-09 12:41:57 +0800 | [diff] [blame^] | 355 | GalleryUtils.setSpinnerVisibility((Activity) mActivity, false); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @Override |
| 359 | protected void onDestroy() { |
Hung-ying Tyan | 9d62da7 | 2011-09-30 14:42:50 +0800 | [diff] [blame] | 360 | super.onDestroy(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 361 | if (mAlbumDataAdapter != null) { |
| 362 | mAlbumDataAdapter.setLoadingListener(null); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | private void initializeViews() { |
| 367 | mStaticBackground = new StaticBackground((Context) mActivity); |
| 368 | mRootPane.addComponent(mStaticBackground); |
| 369 | |
| 370 | mSelectionManager = new SelectionManager(mActivity, false); |
| 371 | mSelectionManager.setSelectionListener(this); |
| 372 | mGridDrawer = new GridDrawer((Context) mActivity, mSelectionManager); |
| 373 | Config.AlbumPage config = Config.AlbumPage.get((Context) mActivity); |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 374 | mAlbumView = new AlbumView(mActivity, config.slotViewSpec, |
| 375 | 0 /* don't cache thumbnail */); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 376 | mAlbumView.setSelectionDrawer(mGridDrawer); |
| 377 | mRootPane.addComponent(mAlbumView); |
| 378 | mAlbumView.setListener(new SlotView.SimpleListener() { |
| 379 | @Override |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 380 | public void onDown(int index) { |
| 381 | AlbumPage.this.onDown(index); |
| 382 | } |
| 383 | |
| 384 | @Override |
| 385 | public void onUp() { |
| 386 | AlbumPage.this.onUp(); |
| 387 | } |
| 388 | |
| 389 | @Override |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 390 | public void onSingleTapUp(int slotIndex) { |
| 391 | AlbumPage.this.onSingleTapUp(slotIndex); |
| 392 | } |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 393 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 394 | @Override |
| 395 | public void onLongTap(int slotIndex) { |
| 396 | AlbumPage.this.onLongTap(slotIndex); |
| 397 | } |
| 398 | }); |
| 399 | mActionModeHandler = new ActionModeHandler(mActivity, mSelectionManager); |
| 400 | mActionModeHandler.setActionModeListener(new ActionModeListener() { |
| 401 | public boolean onActionItemClicked(MenuItem item) { |
| 402 | return onItemSelected(item); |
| 403 | } |
| 404 | }); |
| 405 | mStaticBackground.setImage(R.drawable.background, |
| 406 | R.drawable.background_portrait); |
| 407 | } |
| 408 | |
| 409 | private void initializeData(Bundle data) { |
| 410 | mMediaSetPath = Path.fromString(data.getString(KEY_MEDIA_PATH)); |
| 411 | mMediaSet = mActivity.getDataManager().getMediaSet(mMediaSetPath); |
| 412 | Utils.assertTrue(mMediaSet != null, |
| 413 | "MediaSet is null. Path = %s", mMediaSetPath); |
| 414 | mSelectionManager.setSourceMediaSet(mMediaSet); |
| 415 | mAlbumDataAdapter = new AlbumDataAdapter(mActivity, mMediaSet); |
| 416 | mAlbumDataAdapter.setLoadingListener(new MyLoadingListener()); |
| 417 | mAlbumView.setModel(mAlbumDataAdapter); |
| 418 | } |
| 419 | |
| 420 | private void showDetails() { |
| 421 | mShowDetails = true; |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 422 | if (mDetailsHelper == null) { |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 423 | mHighlightDrawer = new HighlightDrawer(mActivity.getAndroidContext(), |
| 424 | mSelectionManager); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 425 | mDetailsHelper = new DetailsHelper(mActivity, mRootPane, mDetailsSource); |
| 426 | mDetailsHelper.setCloseListener(new CloseListener() { |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 427 | public void onClose() { |
| 428 | hideDetails(); |
| 429 | } |
| 430 | }); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 431 | } |
| 432 | mAlbumView.setSelectionDrawer(mHighlightDrawer); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 433 | mDetailsHelper.show(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | private void hideDetails() { |
| 437 | mShowDetails = false; |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 438 | mDetailsHelper.hide(); |
Ray Chen | f595f26 | 2011-08-26 21:15:07 +0800 | [diff] [blame] | 439 | mAlbumView.setSelectionDrawer(mGridDrawer); |
| 440 | mAlbumView.invalidate(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | @Override |
| 444 | protected boolean onCreateActionBar(Menu menu) { |
| 445 | Activity activity = (Activity) mActivity; |
| 446 | GalleryActionBar actionBar = mActivity.getGalleryActionBar(); |
| 447 | MenuInflater inflater = activity.getMenuInflater(); |
| 448 | |
| 449 | if (mGetContent) { |
| 450 | inflater.inflate(R.menu.pickup, menu); |
| 451 | int typeBits = mData.getInt(Gallery.KEY_TYPE_BITS, |
| 452 | DataManager.INCLUDE_IMAGE); |
| 453 | |
| 454 | actionBar.setTitle(GalleryUtils.getSelectionModePrompt(typeBits)); |
| 455 | } else { |
| 456 | inflater.inflate(R.menu.album, menu); |
| 457 | actionBar.setTitle(mMediaSet.getName()); |
| 458 | if (mMediaSet instanceof MtpDevice) { |
| 459 | menu.findItem(R.id.action_slideshow).setVisible(false); |
| 460 | } else { |
| 461 | menu.findItem(R.id.action_slideshow).setVisible(true); |
| 462 | } |
| 463 | |
| 464 | MenuItem groupBy = menu.findItem(R.id.action_group_by); |
| 465 | FilterUtils.setupMenuItems(actionBar, mMediaSetPath, true); |
| 466 | |
| 467 | if (groupBy != null) { |
| 468 | groupBy.setVisible(mShowClusterMenu); |
| 469 | } |
| 470 | |
| 471 | actionBar.setTitle(mMediaSet.getName()); |
| 472 | } |
| 473 | actionBar.setSubtitle(null); |
| 474 | |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | protected boolean onItemSelected(MenuItem item) { |
| 480 | switch (item.getItemId()) { |
Ray Chen | 11e7f19 | 2011-08-25 11:43:40 +0800 | [diff] [blame] | 481 | case R.id.action_cancel: |
| 482 | mActivity.getStateManager().finishState(this); |
| 483 | return true; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 484 | case R.id.action_select: |
| 485 | mSelectionManager.setAutoLeaveSelectionMode(false); |
| 486 | mSelectionManager.enterSelectionMode(); |
| 487 | return true; |
| 488 | case R.id.action_group_by: { |
| 489 | mActivity.getGalleryActionBar().showClusterDialog(this); |
| 490 | return true; |
| 491 | } |
| 492 | case R.id.action_slideshow: { |
| 493 | Bundle data = new Bundle(); |
| 494 | data.putString(SlideshowPage.KEY_SET_PATH, |
| 495 | mMediaSetPath.toString()); |
| 496 | data.putBoolean(SlideshowPage.KEY_REPEAT, true); |
| 497 | mActivity.getStateManager().startStateForResult( |
| 498 | SlideshowPage.class, REQUEST_SLIDESHOW, data); |
| 499 | return true; |
| 500 | } |
| 501 | case R.id.action_details: { |
| 502 | if (mShowDetails) { |
| 503 | hideDetails(); |
| 504 | } else { |
| 505 | showDetails(); |
| 506 | } |
| 507 | return true; |
| 508 | } |
| 509 | default: |
| 510 | return false; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | @Override |
| 515 | protected void onStateResult(int request, int result, Intent data) { |
| 516 | switch (request) { |
| 517 | case REQUEST_SLIDESHOW: { |
| 518 | // data could be null, if there is no images in the album |
| 519 | if (data == null) return; |
| 520 | mFocusIndex = data.getIntExtra(SlideshowPage.KEY_PHOTO_INDEX, 0); |
| 521 | mAlbumView.setCenterIndex(mFocusIndex); |
| 522 | break; |
| 523 | } |
| 524 | case REQUEST_PHOTO: { |
| 525 | if (data == null) return; |
| 526 | mFocusIndex = data.getIntExtra(PhotoPage.KEY_INDEX_HINT, 0); |
| 527 | mAlbumView.setCenterIndex(mFocusIndex); |
| 528 | startTransition(); |
| 529 | break; |
| 530 | } |
| 531 | case REQUEST_DO_ANIMATION: { |
| 532 | startTransition(null); |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | public void onSelectionModeChange(int mode) { |
| 539 | switch (mode) { |
| 540 | case SelectionManager.ENTER_SELECTION_MODE: { |
| 541 | mActionMode = mActionModeHandler.startActionMode(); |
Ray Chen | 1e86a2b | 2011-09-27 15:48:08 +0800 | [diff] [blame] | 542 | mVibrator.vibrate(100); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 543 | break; |
| 544 | } |
| 545 | case SelectionManager.LEAVE_SELECTION_MODE: { |
| 546 | mActionMode.finish(); |
| 547 | mRootPane.invalidate(); |
| 548 | break; |
| 549 | } |
| 550 | case SelectionManager.SELECT_ALL_MODE: { |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 551 | mActionModeHandler.updateSupportedOperation(); |
| 552 | mRootPane.invalidate(); |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | public void onSelectionChange(Path path, boolean selected) { |
| 559 | Utils.assertTrue(mActionMode != null); |
| 560 | int count = mSelectionManager.getSelectedCount(); |
| 561 | String format = mActivity.getResources().getQuantityString( |
| 562 | R.plurals.number_of_items_selected, count); |
| 563 | mActionModeHandler.setTitle(String.format(format, count)); |
| 564 | mActionModeHandler.updateSupportedOperation(path, selected); |
| 565 | } |
| 566 | |
Hung-ying Tyan | 9d62da7 | 2011-09-30 14:42:50 +0800 | [diff] [blame] | 567 | @Override |
| 568 | public void onSyncDone(final MediaSet mediaSet, final int resultCode) { |
| 569 | Log.d(TAG, "onSyncDone: " + Utils.maskDebugInfo(mediaSet.getName()) + " result=" |
| 570 | + resultCode); |
| 571 | ((Activity) mActivity).runOnUiThread(new Runnable() { |
| 572 | @Override |
| 573 | public void run() { |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 574 | if (resultCode == MediaSet.SYNC_RESULT_SUCCESS) { |
| 575 | mInitialSynced = true; |
| 576 | } |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 577 | clearLoadingBit(BIT_LOADING_SYNC); |
Chih-Chung Chang | 3cc0270 | 2012-01-09 12:41:57 +0800 | [diff] [blame^] | 578 | if (resultCode == MediaSet.SYNC_RESULT_ERROR && mIsActive) { |
Hung-ying Tyan | 9d62da7 | 2011-09-30 14:42:50 +0800 | [diff] [blame] | 579 | Toast.makeText((Context) mActivity, R.string.sync_album_error, |
| 580 | Toast.LENGTH_LONG).show(); |
| 581 | } |
| 582 | } |
| 583 | }); |
| 584 | } |
| 585 | |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 586 | private void setLoadingBit(int loadTaskBit) { |
Chih-Chung Chang | 3cc0270 | 2012-01-09 12:41:57 +0800 | [diff] [blame^] | 587 | if (mLoadingBits == 0 && mIsActive) { |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 588 | GalleryUtils.setSpinnerVisibility((Activity) mActivity, true); |
| 589 | } |
| 590 | mLoadingBits |= loadTaskBit; |
| 591 | } |
| 592 | |
| 593 | private void clearLoadingBit(int loadTaskBit) { |
| 594 | mLoadingBits &= ~loadTaskBit; |
Chih-Chung Chang | 3cc0270 | 2012-01-09 12:41:57 +0800 | [diff] [blame^] | 595 | if (mLoadingBits == 0 && mIsActive) { |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 596 | GalleryUtils.setSpinnerVisibility((Activity) mActivity, false); |
| 597 | |
| 598 | if (mAlbumDataAdapter.size() == 0) { |
| 599 | Toast.makeText((Context) mActivity, |
| 600 | R.string.empty_album, Toast.LENGTH_LONG).show(); |
| 601 | mActivity.getStateManager().finishState(AlbumPage.this); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 606 | private class MyLoadingListener implements LoadingListener { |
| 607 | @Override |
| 608 | public void onLoadingStarted() { |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 609 | setLoadingBit(BIT_LOADING_RELOAD); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | @Override |
| 613 | public void onLoadingFinished() { |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 614 | clearLoadingBit(BIT_LOADING_RELOAD); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 618 | private class MyDetailsSource implements DetailsHelper.DetailsSource { |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 619 | private int mIndex; |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 620 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 621 | public int size() { |
| 622 | return mAlbumDataAdapter.size(); |
| 623 | } |
| 624 | |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 625 | public int getIndex() { |
| 626 | return mIndex; |
| 627 | } |
| 628 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 629 | // If requested index is out of active window, suggest a valid index. |
| 630 | // If there is no valid index available, return -1. |
| 631 | public int findIndex(int indexHint) { |
| 632 | if (mAlbumDataAdapter.isActive(indexHint)) { |
| 633 | mIndex = indexHint; |
| 634 | } else { |
| 635 | mIndex = mAlbumDataAdapter.getActiveStart(); |
| 636 | if (!mAlbumDataAdapter.isActive(mIndex)) { |
| 637 | return -1; |
| 638 | } |
| 639 | } |
| 640 | return mIndex; |
| 641 | } |
| 642 | |
| 643 | public MediaDetails getDetails() { |
| 644 | MediaObject item = mAlbumDataAdapter.get(mIndex); |
| 645 | if (item != null) { |
| 646 | mHighlightDrawer.setHighlightItem(item.getPath()); |
| 647 | return item.getDetails(); |
| 648 | } else { |
| 649 | return null; |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | } |