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; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.graphics.Rect; |
| 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.MediaObject; |
| 37 | import com.android.gallery3d.data.MediaSet; |
| 38 | import com.android.gallery3d.data.Path; |
| 39 | import com.android.gallery3d.picasasource.PicasaSource; |
| 40 | import com.android.gallery3d.settings.GallerySettings; |
| 41 | import com.android.gallery3d.ui.ActionModeHandler; |
| 42 | import com.android.gallery3d.ui.ActionModeHandler.ActionModeListener; |
| 43 | import com.android.gallery3d.ui.AlbumSetView; |
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; |
Hung-ying Tyan | 9d62da7 | 2011-09-30 14:42:50 +0800 | [diff] [blame] | 56 | import com.android.gallery3d.util.Future; |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 57 | import com.android.gallery3d.util.GalleryUtils; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 58 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 59 | public class AlbumSetPage extends ActivityState implements |
| 60 | SelectionManager.SelectionListener, GalleryActionBar.ClusterRunner, |
Hung-ying Tyan | 9d62da7 | 2011-09-30 14:42:50 +0800 | [diff] [blame] | 61 | EyePosition.EyePositionListener, MediaSet.SyncListener { |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 62 | @SuppressWarnings("unused") |
| 63 | private static final String TAG = "AlbumSetPage"; |
| 64 | |
| 65 | public static final String KEY_MEDIA_PATH = "media-path"; |
| 66 | public static final String KEY_SET_TITLE = "set-title"; |
| 67 | public static final String KEY_SET_SUBTITLE = "set-subtitle"; |
Ray Chen | f95aed1 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 68 | public static final String KEY_SELECTED_CLUSTER_TYPE = "selected-cluster"; |
Owen Lin | 44aac4b | 2011-08-28 10:50:21 +0800 | [diff] [blame] | 69 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 70 | private static final int DATA_CACHE_SIZE = 256; |
| 71 | private static final int REQUEST_DO_ANIMATION = 1; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 72 | |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 73 | private static final int BIT_LOADING_RELOAD = 1; |
| 74 | private static final int BIT_LOADING_SYNC = 2; |
| 75 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 76 | private boolean mIsActive = false; |
| 77 | private StaticBackground mStaticBackground; |
| 78 | private AlbumSetView mAlbumSetView; |
| 79 | |
| 80 | private MediaSet mMediaSet; |
Ray Chen | 6f20eed | 2011-10-26 11:36:55 +0800 | [diff] [blame] | 81 | private String mTitle; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 82 | private String mSubtitle; |
Ray Chen | f95aed1 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 83 | private boolean mShowClusterMenu; |
| 84 | private int mSelectedAction; |
Ray Chen | 1e86a2b | 2011-09-27 15:48:08 +0800 | [diff] [blame] | 85 | private Vibrator mVibrator; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 86 | |
| 87 | protected SelectionManager mSelectionManager; |
| 88 | private AlbumSetDataAdapter mAlbumSetDataAdapter; |
| 89 | private GridDrawer mGridDrawer; |
| 90 | private HighlightDrawer mHighlightDrawer; |
| 91 | |
| 92 | private boolean mGetContent; |
| 93 | private boolean mGetAlbum; |
| 94 | private ActionMode mActionMode; |
| 95 | private ActionModeHandler mActionModeHandler; |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 96 | private DetailsHelper mDetailsHelper; |
| 97 | private MyDetailsSource mDetailsSource; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 98 | private boolean mShowDetails; |
| 99 | private EyePosition mEyePosition; |
| 100 | |
| 101 | // The eyes' position of the user, the origin is at the center of the |
| 102 | // device and the unit is in pixels. |
| 103 | private float mX; |
| 104 | private float mY; |
| 105 | private float mZ; |
| 106 | |
Hung-ying Tyan | 9d62da7 | 2011-09-30 14:42:50 +0800 | [diff] [blame] | 107 | private Future<Integer> mSyncTask = null; |
| 108 | |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 109 | private int mLoadingBits = 0; |
| 110 | private boolean mInitialSynced = false; |
| 111 | |
Ray Chen | 73e791c | 2011-10-04 15:19:44 +0800 | [diff] [blame] | 112 | private final GLView mRootPane = new GLView() { |
| 113 | private final float mMatrix[] = new float[16]; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 114 | |
| 115 | @Override |
| 116 | protected void onLayout( |
| 117 | boolean changed, int left, int top, int right, int bottom) { |
| 118 | mStaticBackground.layout(0, 0, right - left, bottom - top); |
| 119 | mEyePosition.resetPosition(); |
| 120 | |
| 121 | int slotViewTop = GalleryActionBar.getHeight((Activity) mActivity); |
| 122 | int slotViewBottom = bottom - top; |
| 123 | int slotViewRight = right - left; |
| 124 | |
| 125 | if (mShowDetails) { |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 126 | mDetailsHelper.layout(left, slotViewTop, right, bottom); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 127 | } else { |
| 128 | mAlbumSetView.setSelectionDrawer(mGridDrawer); |
| 129 | } |
| 130 | |
| 131 | mAlbumSetView.layout(0, slotViewTop, slotViewRight, slotViewBottom); |
| 132 | PositionRepository.getInstance(mActivity).setOffset( |
| 133 | 0, slotViewTop); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | protected void render(GLCanvas canvas) { |
| 138 | canvas.save(GLCanvas.SAVE_FLAG_MATRIX); |
| 139 | GalleryUtils.setViewPointMatrix(mMatrix, |
| 140 | getWidth() / 2 + mX, getHeight() / 2 + mY, mZ); |
| 141 | canvas.multiplyMatrix(mMatrix, 0); |
| 142 | super.render(canvas); |
| 143 | canvas.restore(); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | @Override |
| 148 | public void onEyePositionChanged(float x, float y, float z) { |
| 149 | mRootPane.lockRendering(); |
| 150 | mX = x; |
| 151 | mY = y; |
| 152 | mZ = z; |
| 153 | mRootPane.unlockRendering(); |
| 154 | mRootPane.invalidate(); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void onBackPressed() { |
| 159 | if (mShowDetails) { |
| 160 | hideDetails(); |
| 161 | } else if (mSelectionManager.inSelectionMode()) { |
| 162 | mSelectionManager.leaveSelectionMode(); |
| 163 | } else { |
| 164 | mAlbumSetView.savePositions( |
| 165 | PositionRepository.getInstance(mActivity)); |
| 166 | super.onBackPressed(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | private void savePositions(int slotIndex, int center[]) { |
| 171 | Rect offset = new Rect(); |
| 172 | mRootPane.getBoundsOf(mAlbumSetView, offset); |
| 173 | mAlbumSetView.savePositions(PositionRepository.getInstance(mActivity)); |
| 174 | Rect r = mAlbumSetView.getSlotRect(slotIndex); |
| 175 | int scrollX = mAlbumSetView.getScrollX(); |
| 176 | int scrollY = mAlbumSetView.getScrollY(); |
| 177 | center[0] = offset.left + (r.left + r.right) / 2 - scrollX; |
| 178 | center[1] = offset.top + (r.top + r.bottom) / 2 - scrollY; |
| 179 | } |
| 180 | |
| 181 | public void onSingleTapUp(int slotIndex) { |
| 182 | MediaSet targetSet = mAlbumSetDataAdapter.getMediaSet(slotIndex); |
| 183 | if (targetSet == null) return; // Content is dirty, we shall reload soon |
| 184 | |
| 185 | if (mShowDetails) { |
| 186 | Path path = targetSet.getPath(); |
| 187 | mHighlightDrawer.setHighlightItem(path); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 188 | mDetailsHelper.reloadDetails(slotIndex); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 189 | } else if (!mSelectionManager.inSelectionMode()) { |
| 190 | Bundle data = new Bundle(getData()); |
| 191 | String mediaPath = targetSet.getPath().toString(); |
| 192 | int[] center = new int[2]; |
| 193 | savePositions(slotIndex, center); |
| 194 | data.putIntArray(AlbumPage.KEY_SET_CENTER, center); |
| 195 | if (mGetAlbum && targetSet.isLeafAlbum()) { |
| 196 | Activity activity = (Activity) mActivity; |
| 197 | Intent result = new Intent() |
| 198 | .putExtra(AlbumPicker.KEY_ALBUM_PATH, targetSet.getPath().toString()); |
| 199 | activity.setResult(Activity.RESULT_OK, result); |
| 200 | activity.finish(); |
| 201 | } else if (targetSet.getSubMediaSetCount() > 0) { |
| 202 | data.putString(AlbumSetPage.KEY_MEDIA_PATH, mediaPath); |
| 203 | mActivity.getStateManager().startStateForResult( |
| 204 | AlbumSetPage.class, REQUEST_DO_ANIMATION, data); |
| 205 | } else { |
| 206 | if (!mGetContent && (targetSet.getSupportedOperations() |
| 207 | & MediaObject.SUPPORT_IMPORT) != 0) { |
| 208 | data.putBoolean(AlbumPage.KEY_AUTO_SELECT_ALL, true); |
| 209 | } |
| 210 | data.putString(AlbumPage.KEY_MEDIA_PATH, mediaPath); |
| 211 | boolean inAlbum = mActivity.getStateManager().hasStateClass(AlbumPage.class); |
| 212 | // We only show cluster menu in the first AlbumPage in stack |
| 213 | data.putBoolean(AlbumPage.KEY_SHOW_CLUSTER_MENU, !inAlbum); |
| 214 | mActivity.getStateManager().startStateForResult( |
| 215 | AlbumPage.class, REQUEST_DO_ANIMATION, data); |
| 216 | } |
| 217 | } else { |
| 218 | mSelectionManager.toggle(targetSet.getPath()); |
| 219 | mAlbumSetView.invalidate(); |
| 220 | } |
| 221 | } |
| 222 | |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 223 | private void onDown(int index) { |
| 224 | MediaSet set = mAlbumSetDataAdapter.getMediaSet(index); |
| 225 | Path path = (set == null) ? null : set.getPath(); |
| 226 | mSelectionManager.setPressedPath(path); |
| 227 | mAlbumSetView.invalidate(); |
| 228 | } |
| 229 | |
| 230 | private void onUp() { |
| 231 | mSelectionManager.setPressedPath(null); |
| 232 | mAlbumSetView.invalidate(); |
| 233 | } |
| 234 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 235 | public void onLongTap(int slotIndex) { |
| 236 | if (mGetContent || mGetAlbum) return; |
| 237 | if (mShowDetails) { |
| 238 | onSingleTapUp(slotIndex); |
| 239 | } else { |
| 240 | MediaSet set = mAlbumSetDataAdapter.getMediaSet(slotIndex); |
| 241 | if (set == null) return; |
| 242 | mSelectionManager.setAutoLeaveSelectionMode(true); |
| 243 | mSelectionManager.toggle(set.getPath()); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 244 | mDetailsSource.findIndex(slotIndex); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 245 | mAlbumSetView.invalidate(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | public void doCluster(int clusterType) { |
| 250 | String basePath = mMediaSet.getPath().toString(); |
| 251 | String newPath = FilterUtils.switchClusterPath(basePath, clusterType); |
| 252 | Bundle data = new Bundle(getData()); |
| 253 | data.putString(AlbumSetPage.KEY_MEDIA_PATH, newPath); |
Ray Chen | f95aed1 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 254 | data.putInt(KEY_SELECTED_CLUSTER_TYPE, clusterType); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 255 | mAlbumSetView.savePositions(PositionRepository.getInstance(mActivity)); |
| 256 | mActivity.getStateManager().switchState(this, AlbumSetPage.class, data); |
| 257 | } |
| 258 | |
| 259 | public void doFilter(int filterType) { |
| 260 | String basePath = mMediaSet.getPath().toString(); |
| 261 | String newPath = FilterUtils.switchFilterPath(basePath, filterType); |
| 262 | Bundle data = new Bundle(getData()); |
| 263 | data.putString(AlbumSetPage.KEY_MEDIA_PATH, newPath); |
| 264 | mAlbumSetView.savePositions(PositionRepository.getInstance(mActivity)); |
| 265 | mActivity.getStateManager().switchState(this, AlbumSetPage.class, data); |
| 266 | } |
| 267 | |
| 268 | public void onOperationComplete() { |
| 269 | mAlbumSetView.invalidate(); |
| 270 | // TODO: enable animation |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public void onCreate(Bundle data, Bundle restoreState) { |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 275 | initializeViews(); |
| 276 | initializeData(data); |
Ray Chen | 1e86a2b | 2011-09-27 15:48:08 +0800 | [diff] [blame] | 277 | Context context = mActivity.getAndroidContext(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 278 | mGetContent = data.getBoolean(Gallery.KEY_GET_CONTENT, false); |
| 279 | mGetAlbum = data.getBoolean(Gallery.KEY_GET_ALBUM, false); |
Ray Chen | 6f20eed | 2011-10-26 11:36:55 +0800 | [diff] [blame] | 280 | mTitle = data.getString(AlbumSetPage.KEY_SET_TITLE); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 281 | mSubtitle = data.getString(AlbumSetPage.KEY_SET_SUBTITLE); |
Ray Chen | 1e86a2b | 2011-09-27 15:48:08 +0800 | [diff] [blame] | 282 | mEyePosition = new EyePosition(context, this); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 283 | mDetailsSource = new MyDetailsSource(); |
Ray Chen | 1e86a2b | 2011-09-27 15:48:08 +0800 | [diff] [blame] | 284 | mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
Wei Huang | ea80ff9 | 2011-08-30 11:33:24 -0700 | [diff] [blame] | 285 | GalleryActionBar actionBar = mActivity.getGalleryActionBar(); |
| 286 | if (actionBar != null) { |
Ray Chen | f95aed1 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 287 | mSelectedAction = data.getInt( |
| 288 | AlbumSetPage.KEY_SELECTED_CLUSTER_TYPE, FilterUtils.CLUSTER_BY_ALBUM); |
Wei Huang | ea80ff9 | 2011-08-30 11:33:24 -0700 | [diff] [blame] | 289 | } |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 290 | startTransition(); |
| 291 | } |
| 292 | |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 293 | private void clearLoadingBit(int loadingBit) { |
| 294 | mLoadingBits &= ~loadingBit; |
Chih-Chung Chang | 3cc0270 | 2012-01-09 12:41:57 +0800 | [diff] [blame^] | 295 | if (mLoadingBits == 0 && mIsActive) { |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 296 | GalleryUtils.setSpinnerVisibility((Activity) mActivity, false); |
| 297 | |
| 298 | // Only show toast when there's no album and we are going to finish |
| 299 | // the page. Toast is redundant if we are going to stay on this page. |
| 300 | if ((mAlbumSetDataAdapter.size() == 0)) { |
| 301 | Toast.makeText((Context) mActivity, |
| 302 | R.string.empty_album, Toast.LENGTH_LONG).show(); |
| 303 | if (mActivity.getStateManager().getStateCount() > 1) { |
| 304 | mActivity.getStateManager().finishState(this); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private void setLoadingBit(int loadingBit) { |
Chih-Chung Chang | 3cc0270 | 2012-01-09 12:41:57 +0800 | [diff] [blame^] | 311 | if (mLoadingBits == 0 && mIsActive) { |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 312 | GalleryUtils.setSpinnerVisibility((Activity) mActivity, true); |
| 313 | } |
| 314 | mLoadingBits |= loadingBit; |
| 315 | } |
| 316 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 317 | @Override |
| 318 | public void onPause() { |
| 319 | super.onPause(); |
| 320 | mIsActive = false; |
| 321 | mActionModeHandler.pause(); |
| 322 | mAlbumSetDataAdapter.pause(); |
| 323 | mAlbumSetView.pause(); |
| 324 | mEyePosition.pause(); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 325 | DetailsHelper.pause(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 326 | GalleryActionBar actionBar = mActivity.getGalleryActionBar(); |
Ray Chen | f95aed1 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 327 | if (actionBar != null) actionBar.hideClusterMenu(); |
Hung-ying Tyan | 9d62da7 | 2011-09-30 14:42:50 +0800 | [diff] [blame] | 328 | if (mSyncTask != null) { |
| 329 | mSyncTask.cancel(); |
| 330 | mSyncTask = null; |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 331 | clearLoadingBit(BIT_LOADING_SYNC); |
Hung-ying Tyan | 9d62da7 | 2011-09-30 14:42:50 +0800 | [diff] [blame] | 332 | } |
Chih-Chung Chang | 3cc0270 | 2012-01-09 12:41:57 +0800 | [diff] [blame^] | 333 | GalleryUtils.setSpinnerVisibility((Activity) mActivity, false); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public void onResume() { |
| 338 | super.onResume(); |
| 339 | mIsActive = true; |
| 340 | setContentPane(mRootPane); |
Owen Lin | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 341 | |
| 342 | // Set the reload bit here to prevent it exit this page in clearLoadingBit(). |
| 343 | setLoadingBit(BIT_LOADING_RELOAD); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 344 | mAlbumSetDataAdapter.resume(); |
Owen Lin | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 345 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 346 | mAlbumSetView.resume(); |
| 347 | mEyePosition.resume(); |
| 348 | mActionModeHandler.resume(); |
| 349 | GalleryActionBar actionBar = mActivity.getGalleryActionBar(); |
Ray Chen | 73e791c | 2011-10-04 15:19:44 +0800 | [diff] [blame] | 350 | if (mShowClusterMenu && actionBar != null) { |
| 351 | actionBar.showClusterMenu(mSelectedAction, this); |
| 352 | } |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 353 | if (!mInitialSynced) { |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 354 | setLoadingBit(BIT_LOADING_SYNC); |
Owen Lin | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 355 | mSyncTask = mMediaSet.requestSync(AlbumSetPage.this); |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 356 | } |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | private void initializeData(Bundle data) { |
| 360 | String mediaPath = data.getString(AlbumSetPage.KEY_MEDIA_PATH); |
| 361 | mMediaSet = mActivity.getDataManager().getMediaSet(mediaPath); |
| 362 | mSelectionManager.setSourceMediaSet(mMediaSet); |
| 363 | mAlbumSetDataAdapter = new AlbumSetDataAdapter( |
| 364 | mActivity, mMediaSet, DATA_CACHE_SIZE); |
| 365 | mAlbumSetDataAdapter.setLoadingListener(new MyLoadingListener()); |
| 366 | mAlbumSetView.setModel(mAlbumSetDataAdapter); |
| 367 | } |
| 368 | |
| 369 | private void initializeViews() { |
| 370 | mSelectionManager = new SelectionManager(mActivity, true); |
| 371 | mSelectionManager.setSelectionListener(this); |
| 372 | mStaticBackground = new StaticBackground(mActivity.getAndroidContext()); |
| 373 | mRootPane.addComponent(mStaticBackground); |
| 374 | |
| 375 | mGridDrawer = new GridDrawer((Context) mActivity, mSelectionManager); |
| 376 | Config.AlbumSetPage config = Config.AlbumSetPage.get((Context) mActivity); |
| 377 | mAlbumSetView = new AlbumSetView(mActivity, mGridDrawer, |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 378 | config.slotViewSpec, config.labelSpec); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 379 | mAlbumSetView.setListener(new SlotView.SimpleListener() { |
| 380 | @Override |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 381 | public void onDown(int index) { |
| 382 | AlbumSetPage.this.onDown(index); |
| 383 | } |
| 384 | |
| 385 | @Override |
| 386 | public void onUp() { |
| 387 | AlbumSetPage.this.onUp(); |
| 388 | } |
| 389 | |
| 390 | @Override |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 391 | public void onSingleTapUp(int slotIndex) { |
| 392 | AlbumSetPage.this.onSingleTapUp(slotIndex); |
| 393 | } |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 394 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 395 | @Override |
| 396 | public void onLongTap(int slotIndex) { |
| 397 | AlbumSetPage.this.onLongTap(slotIndex); |
| 398 | } |
| 399 | }); |
| 400 | |
| 401 | mActionModeHandler = new ActionModeHandler(mActivity, mSelectionManager); |
| 402 | mActionModeHandler.setActionModeListener(new ActionModeListener() { |
| 403 | public boolean onActionItemClicked(MenuItem item) { |
| 404 | return onItemSelected(item); |
| 405 | } |
| 406 | }); |
| 407 | mRootPane.addComponent(mAlbumSetView); |
| 408 | |
| 409 | mStaticBackground.setImage(R.drawable.background, |
| 410 | R.drawable.background_portrait); |
| 411 | } |
| 412 | |
| 413 | @Override |
| 414 | protected boolean onCreateActionBar(Menu menu) { |
| 415 | Activity activity = (Activity) mActivity; |
| 416 | GalleryActionBar actionBar = mActivity.getGalleryActionBar(); |
| 417 | MenuInflater inflater = activity.getMenuInflater(); |
| 418 | |
| 419 | final boolean inAlbum = mActivity.getStateManager().hasStateClass( |
| 420 | AlbumPage.class); |
| 421 | |
| 422 | if (mGetContent) { |
| 423 | inflater.inflate(R.menu.pickup, menu); |
| 424 | int typeBits = mData.getInt( |
| 425 | Gallery.KEY_TYPE_BITS, DataManager.INCLUDE_IMAGE); |
| 426 | int id = R.string.select_image; |
| 427 | if ((typeBits & DataManager.INCLUDE_VIDEO) != 0) { |
| 428 | id = (typeBits & DataManager.INCLUDE_IMAGE) == 0 |
| 429 | ? R.string.select_video |
| 430 | : R.string.select_item; |
| 431 | } |
| 432 | actionBar.setTitle(id); |
| 433 | } else if (mGetAlbum) { |
| 434 | inflater.inflate(R.menu.pickup, menu); |
| 435 | actionBar.setTitle(R.string.select_album); |
| 436 | } else { |
Ray Chen | f95aed1 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 437 | mShowClusterMenu = !inAlbum; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 438 | inflater.inflate(R.menu.albumset, menu); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 439 | MenuItem selectItem = menu.findItem(R.id.action_select); |
| 440 | |
| 441 | if (selectItem != null) { |
| 442 | boolean selectAlbums = !inAlbum && |
| 443 | actionBar.getClusterTypeAction() == FilterUtils.CLUSTER_BY_ALBUM; |
| 444 | if (selectAlbums) { |
| 445 | selectItem.setTitle(R.string.select_album); |
| 446 | } else { |
| 447 | selectItem.setTitle(R.string.select_group); |
| 448 | } |
| 449 | } |
| 450 | |
Ray Chen | 9802026 | 2011-11-18 15:00:32 +0800 | [diff] [blame] | 451 | FilterUtils.setupMenuItems(actionBar, mMediaSet.getPath(), false); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 452 | MenuItem switchCamera = menu.findItem(R.id.action_camera); |
| 453 | if (switchCamera != null) { |
| 454 | switchCamera.setVisible(GalleryUtils.isCameraAvailable(activity)); |
| 455 | } |
| 456 | |
Ray Chen | 6f20eed | 2011-10-26 11:36:55 +0800 | [diff] [blame] | 457 | actionBar.setTitle(mTitle); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 458 | actionBar.setSubtitle(mSubtitle); |
| 459 | } |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | @Override |
| 464 | protected boolean onItemSelected(MenuItem item) { |
| 465 | Activity activity = (Activity) mActivity; |
| 466 | switch (item.getItemId()) { |
Ray Chen | 11e7f19 | 2011-08-25 11:43:40 +0800 | [diff] [blame] | 467 | case R.id.action_cancel: |
| 468 | activity.setResult(Activity.RESULT_CANCELED); |
| 469 | activity.finish(); |
| 470 | return true; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 471 | case R.id.action_select: |
| 472 | mSelectionManager.setAutoLeaveSelectionMode(false); |
| 473 | mSelectionManager.enterSelectionMode(); |
| 474 | return true; |
| 475 | case R.id.action_details: |
| 476 | if (mAlbumSetDataAdapter.size() != 0) { |
| 477 | if (mShowDetails) { |
| 478 | hideDetails(); |
| 479 | } else { |
| 480 | showDetails(); |
| 481 | } |
| 482 | } else { |
| 483 | Toast.makeText(activity, |
| 484 | activity.getText(R.string.no_albums_alert), |
| 485 | Toast.LENGTH_SHORT).show(); |
| 486 | } |
| 487 | return true; |
| 488 | case R.id.action_camera: { |
| 489 | Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA) |
| 490 | .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
| 491 | | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 492 | activity.startActivity(intent); |
| 493 | return true; |
| 494 | } |
| 495 | case R.id.action_manage_offline: { |
Owen Lin | 44aac4b | 2011-08-28 10:50:21 +0800 | [diff] [blame] | 496 | Bundle data = new Bundle(); |
| 497 | String mediaPath = mActivity.getDataManager().getTopSetPath( |
| 498 | DataManager.INCLUDE_ALL); |
| 499 | data.putString(AlbumSetPage.KEY_MEDIA_PATH, mediaPath); |
| 500 | mActivity.getStateManager().startState(ManageCachePage.class, data); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 501 | return true; |
| 502 | } |
| 503 | case R.id.action_sync_picasa_albums: { |
| 504 | PicasaSource.requestSync(activity); |
| 505 | return true; |
| 506 | } |
| 507 | case R.id.action_settings: { |
| 508 | activity.startActivity(new Intent(activity, GallerySettings.class)); |
| 509 | return true; |
| 510 | } |
| 511 | default: |
| 512 | return false; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | protected void onStateResult(int requestCode, int resultCode, Intent data) { |
| 518 | switch (requestCode) { |
| 519 | case REQUEST_DO_ANIMATION: { |
| 520 | startTransition(); |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | private void startTransition() { |
| 526 | final PositionRepository repository = |
| 527 | PositionRepository.getInstance(mActivity); |
| 528 | mAlbumSetView.startTransition(new PositionProvider() { |
Ray Chen | 73e791c | 2011-10-04 15:19:44 +0800 | [diff] [blame] | 529 | private final Position mTempPosition = new Position(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 530 | public Position getPosition(long identity, Position target) { |
| 531 | Position p = repository.get(identity); |
| 532 | if (p == null) { |
| 533 | p = mTempPosition; |
| 534 | p.set(target.x, target.y, 128, target.theta, 1); |
| 535 | } |
| 536 | return p; |
| 537 | } |
| 538 | }); |
| 539 | } |
| 540 | |
| 541 | private String getSelectedString() { |
| 542 | GalleryActionBar actionBar = mActivity.getGalleryActionBar(); |
| 543 | int count = mSelectionManager.getSelectedCount(); |
| 544 | int action = actionBar.getClusterTypeAction(); |
| 545 | int string = action == FilterUtils.CLUSTER_BY_ALBUM |
| 546 | ? R.plurals.number_of_albums_selected |
| 547 | : R.plurals.number_of_groups_selected; |
| 548 | String format = mActivity.getResources().getQuantityString(string, count); |
| 549 | return String.format(format, count); |
| 550 | } |
| 551 | |
| 552 | public void onSelectionModeChange(int mode) { |
| 553 | |
| 554 | switch (mode) { |
| 555 | case SelectionManager.ENTER_SELECTION_MODE: { |
Ray Chen | f95aed1 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 556 | mActivity.getGalleryActionBar().hideClusterMenu(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 557 | mActionMode = mActionModeHandler.startActionMode(); |
Ray Chen | 1e86a2b | 2011-09-27 15:48:08 +0800 | [diff] [blame] | 558 | mVibrator.vibrate(100); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 559 | break; |
| 560 | } |
| 561 | case SelectionManager.LEAVE_SELECTION_MODE: { |
| 562 | mActionMode.finish(); |
Ray Chen | 6f20eed | 2011-10-26 11:36:55 +0800 | [diff] [blame] | 563 | if (mShowClusterMenu) { |
| 564 | mActivity.getGalleryActionBar().showClusterMenu(mSelectedAction, this); |
| 565 | } |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 566 | mRootPane.invalidate(); |
| 567 | break; |
| 568 | } |
| 569 | case SelectionManager.SELECT_ALL_MODE: { |
| 570 | mActionModeHandler.setTitle(getSelectedString()); |
| 571 | mRootPane.invalidate(); |
| 572 | break; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | public void onSelectionChange(Path path, boolean selected) { |
| 578 | Utils.assertTrue(mActionMode != null); |
| 579 | mActionModeHandler.setTitle(getSelectedString()); |
| 580 | mActionModeHandler.updateSupportedOperation(path, selected); |
| 581 | } |
| 582 | |
| 583 | private void hideDetails() { |
| 584 | mShowDetails = false; |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 585 | mDetailsHelper.hide(); |
Ray Chen | f595f26 | 2011-08-26 21:15:07 +0800 | [diff] [blame] | 586 | mAlbumSetView.setSelectionDrawer(mGridDrawer); |
| 587 | mAlbumSetView.invalidate(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | private void showDetails() { |
| 591 | mShowDetails = true; |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 592 | if (mDetailsHelper == null) { |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 593 | mHighlightDrawer = new HighlightDrawer(mActivity.getAndroidContext(), |
| 594 | mSelectionManager); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 595 | mDetailsHelper = new DetailsHelper(mActivity, mRootPane, mDetailsSource); |
| 596 | mDetailsHelper.setCloseListener(new CloseListener() { |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 597 | public void onClose() { |
| 598 | hideDetails(); |
| 599 | } |
| 600 | }); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 601 | } |
| 602 | mAlbumSetView.setSelectionDrawer(mHighlightDrawer); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 603 | mDetailsHelper.show(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 604 | } |
| 605 | |
Hung-ying Tyan | 9d62da7 | 2011-09-30 14:42:50 +0800 | [diff] [blame] | 606 | @Override |
| 607 | public void onSyncDone(final MediaSet mediaSet, final int resultCode) { |
| 608 | if (resultCode == MediaSet.SYNC_RESULT_ERROR) { |
| 609 | Log.d(TAG, "onSyncDone: " + Utils.maskDebugInfo(mediaSet.getName()) + " result=" |
| 610 | + resultCode); |
| 611 | } |
| 612 | ((Activity) mActivity).runOnUiThread(new Runnable() { |
| 613 | @Override |
| 614 | public void run() { |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 615 | if (resultCode == MediaSet.SYNC_RESULT_SUCCESS) { |
| 616 | mInitialSynced = true; |
| 617 | } |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 618 | clearLoadingBit(BIT_LOADING_SYNC); |
Chih-Chung Chang | 3cc0270 | 2012-01-09 12:41:57 +0800 | [diff] [blame^] | 619 | if (resultCode == MediaSet.SYNC_RESULT_ERROR && mIsActive) { |
Hung-ying Tyan | 9d62da7 | 2011-09-30 14:42:50 +0800 | [diff] [blame] | 620 | Toast.makeText((Context) mActivity, R.string.sync_album_set_error, |
| 621 | Toast.LENGTH_LONG).show(); |
| 622 | } |
| 623 | } |
| 624 | }); |
| 625 | } |
| 626 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 627 | private class MyLoadingListener implements LoadingListener { |
| 628 | public void onLoadingStarted() { |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 629 | setLoadingBit(BIT_LOADING_RELOAD); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | public void onLoadingFinished() { |
Owen Lin | 1a03de8 | 2011-11-08 19:05:56 +0800 | [diff] [blame] | 633 | clearLoadingBit(BIT_LOADING_RELOAD); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 637 | private class MyDetailsSource implements DetailsHelper.DetailsSource { |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 638 | private int mIndex; |
| 639 | public int size() { |
| 640 | return mAlbumSetDataAdapter.size(); |
| 641 | } |
| 642 | |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 643 | public int getIndex() { |
| 644 | return mIndex; |
| 645 | } |
| 646 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 647 | // If requested index is out of active window, suggest a valid index. |
| 648 | // If there is no valid index available, return -1. |
| 649 | public int findIndex(int indexHint) { |
| 650 | if (mAlbumSetDataAdapter.isActive(indexHint)) { |
| 651 | mIndex = indexHint; |
| 652 | } else { |
| 653 | mIndex = mAlbumSetDataAdapter.getActiveStart(); |
| 654 | if (!mAlbumSetDataAdapter.isActive(mIndex)) { |
| 655 | return -1; |
| 656 | } |
| 657 | } |
| 658 | return mIndex; |
| 659 | } |
| 660 | |
| 661 | public MediaDetails getDetails() { |
| 662 | MediaObject item = mAlbumSetDataAdapter.getMediaSet(mIndex); |
| 663 | if (item != null) { |
| 664 | mHighlightDrawer.setHighlightItem(item.getPath()); |
| 665 | return item.getDetails(); |
| 666 | } else { |
| 667 | return null; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | } |