Owen Lin | f9a0a43 | 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.ui; |
| 18 | |
Ray Chen | 0c1f2c8 | 2011-10-05 14:32:58 +0800 | [diff] [blame] | 19 | import android.app.Activity; |
Ray Chen | 67098d1 | 2012-04-05 17:25:43 +0800 | [diff] [blame] | 20 | import android.app.AlertDialog; |
Ray Chen | 0c1f2c8 | 2011-10-05 14:32:58 +0800 | [diff] [blame] | 21 | import android.app.ProgressDialog; |
| 22 | import android.content.Context; |
Ray Chen | 67098d1 | 2012-04-05 17:25:43 +0800 | [diff] [blame] | 23 | import android.content.DialogInterface; |
Owen Lin | d759b7c | 2012-05-16 15:32:02 -0700 | [diff] [blame] | 24 | import android.content.DialogInterface.OnCancelListener; |
Ray Chen | 67098d1 | 2012-04-05 17:25:43 +0800 | [diff] [blame] | 25 | import android.content.DialogInterface.OnClickListener; |
Ray Chen | 0c1f2c8 | 2011-10-05 14:32:58 +0800 | [diff] [blame] | 26 | import android.content.Intent; |
| 27 | import android.os.Handler; |
| 28 | import android.os.Message; |
John Reck | 2abaaf7 | 2012-12-12 13:43:43 -0800 | [diff] [blame] | 29 | import android.view.Menu; |
| 30 | import android.view.MenuItem; |
Ray Chen | 0c1f2c8 | 2011-10-05 14:32:58 +0800 | [diff] [blame] | 31 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 32 | import com.android.gallery3d.R; |
Owen Lin | b21b8e5 | 2012-08-24 12:25:57 +0800 | [diff] [blame] | 33 | import com.android.gallery3d.app.AbstractGalleryActivity; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 34 | import com.android.gallery3d.common.Utils; |
| 35 | import com.android.gallery3d.data.DataManager; |
| 36 | import com.android.gallery3d.data.MediaItem; |
| 37 | import com.android.gallery3d.data.MediaObject; |
| 38 | import com.android.gallery3d.data.Path; |
Ruben Brunk | 5aa454b | 2012-10-25 15:53:42 -0700 | [diff] [blame] | 39 | import com.android.gallery3d.filtershow.FilterShowActivity; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 40 | import com.android.gallery3d.util.Future; |
| 41 | import com.android.gallery3d.util.GalleryUtils; |
| 42 | import com.android.gallery3d.util.ThreadPool.Job; |
| 43 | import com.android.gallery3d.util.ThreadPool.JobContext; |
| 44 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | |
| 47 | public class MenuExecutor { |
| 48 | @SuppressWarnings("unused") |
| 49 | private static final String TAG = "MenuExecutor"; |
| 50 | |
| 51 | private static final int MSG_TASK_COMPLETE = 1; |
| 52 | private static final int MSG_TASK_UPDATE = 2; |
Bobby Georgescu | ba50b94 | 2012-08-08 00:09:50 -0700 | [diff] [blame] | 53 | private static final int MSG_TASK_START = 3; |
| 54 | private static final int MSG_DO_SHARE = 4; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 55 | |
| 56 | public static final int EXECUTION_RESULT_SUCCESS = 1; |
| 57 | public static final int EXECUTION_RESULT_FAIL = 2; |
| 58 | public static final int EXECUTION_RESULT_CANCEL = 3; |
| 59 | |
| 60 | private ProgressDialog mDialog; |
| 61 | private Future<?> mTask; |
Chih-Chung Chang | 6b891c6 | 2012-06-07 20:09:13 +0800 | [diff] [blame] | 62 | // wait the operation to finish when we want to stop it. |
| 63 | private boolean mWaitOnStop; |
Bobby Georgescu | a2d0d34 | 2012-12-03 13:56:00 -0800 | [diff] [blame] | 64 | private boolean mPaused; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 65 | |
Owen Lin | b21b8e5 | 2012-08-24 12:25:57 +0800 | [diff] [blame] | 66 | private final AbstractGalleryActivity mActivity; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 67 | private final SelectionManager mSelectionManager; |
| 68 | private final Handler mHandler; |
| 69 | |
Chih-Chung Chang | 6b891c6 | 2012-06-07 20:09:13 +0800 | [diff] [blame] | 70 | private static ProgressDialog createProgressDialog( |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 71 | Context context, int titleId, int progressMax) { |
| 72 | ProgressDialog dialog = new ProgressDialog(context); |
| 73 | dialog.setTitle(titleId); |
| 74 | dialog.setMax(progressMax); |
| 75 | dialog.setCancelable(false); |
| 76 | dialog.setIndeterminate(false); |
| 77 | if (progressMax > 1) { |
| 78 | dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
| 79 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 80 | return dialog; |
| 81 | } |
| 82 | |
| 83 | public interface ProgressListener { |
Owen Lin | d759b7c | 2012-05-16 15:32:02 -0700 | [diff] [blame] | 84 | public void onConfirmDialogShown(); |
| 85 | public void onConfirmDialogDismissed(boolean confirmed); |
Bobby Georgescu | ba50b94 | 2012-08-08 00:09:50 -0700 | [diff] [blame] | 86 | public void onProgressStart(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 87 | public void onProgressUpdate(int index); |
| 88 | public void onProgressComplete(int result); |
| 89 | } |
| 90 | |
| 91 | public MenuExecutor( |
Owen Lin | b21b8e5 | 2012-08-24 12:25:57 +0800 | [diff] [blame] | 92 | AbstractGalleryActivity activity, SelectionManager selectionManager) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 93 | mActivity = Utils.checkNotNull(activity); |
| 94 | mSelectionManager = Utils.checkNotNull(selectionManager); |
| 95 | mHandler = new SynchronizedHandler(mActivity.getGLRoot()) { |
| 96 | @Override |
| 97 | public void handleMessage(Message message) { |
| 98 | switch (message.what) { |
Bobby Georgescu | ba50b94 | 2012-08-08 00:09:50 -0700 | [diff] [blame] | 99 | case MSG_TASK_START: { |
| 100 | if (message.obj != null) { |
| 101 | ProgressListener listener = (ProgressListener) message.obj; |
| 102 | listener.onProgressStart(); |
| 103 | } |
| 104 | break; |
| 105 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 106 | case MSG_TASK_COMPLETE: { |
Ray Chen | b2b4518 | 2011-08-31 16:37:04 +0800 | [diff] [blame] | 107 | stopTaskAndDismissDialog(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 108 | if (message.obj != null) { |
| 109 | ProgressListener listener = (ProgressListener) message.obj; |
| 110 | listener.onProgressComplete(message.arg1); |
| 111 | } |
| 112 | mSelectionManager.leaveSelectionMode(); |
| 113 | break; |
| 114 | } |
| 115 | case MSG_TASK_UPDATE: { |
Bobby Georgescu | a2d0d34 | 2012-12-03 13:56:00 -0800 | [diff] [blame] | 116 | if (mDialog != null && !mPaused) mDialog.setProgress(message.arg1); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 117 | if (message.obj != null) { |
| 118 | ProgressListener listener = (ProgressListener) message.obj; |
| 119 | listener.onProgressUpdate(message.arg1); |
| 120 | } |
| 121 | break; |
| 122 | } |
| 123 | case MSG_DO_SHARE: { |
| 124 | ((Activity) mActivity).startActivity((Intent) message.obj); |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | }; |
| 130 | } |
| 131 | |
Ray Chen | b2b4518 | 2011-08-31 16:37:04 +0800 | [diff] [blame] | 132 | private void stopTaskAndDismissDialog() { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 133 | if (mTask != null) { |
Chih-Chung Chang | 6b891c6 | 2012-06-07 20:09:13 +0800 | [diff] [blame] | 134 | if (!mWaitOnStop) mTask.cancel(); |
Bobby Georgescu | a2d0d34 | 2012-12-03 13:56:00 -0800 | [diff] [blame] | 135 | if (mDialog != null && mDialog.isShowing()) mDialog.dismiss(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 136 | mDialog = null; |
| 137 | mTask = null; |
| 138 | } |
| 139 | } |
| 140 | |
Bobby Georgescu | a2d0d34 | 2012-12-03 13:56:00 -0800 | [diff] [blame] | 141 | public void resume() { |
| 142 | mPaused = false; |
| 143 | if (mDialog != null) mDialog.show(); |
| 144 | } |
| 145 | |
Ray Chen | b2b4518 | 2011-08-31 16:37:04 +0800 | [diff] [blame] | 146 | public void pause() { |
Bobby Georgescu | a2d0d34 | 2012-12-03 13:56:00 -0800 | [diff] [blame] | 147 | mPaused = true; |
| 148 | if (mDialog != null && mDialog.isShowing()) mDialog.hide(); |
| 149 | } |
| 150 | |
| 151 | public void destroy() { |
Ray Chen | b2b4518 | 2011-08-31 16:37:04 +0800 | [diff] [blame] | 152 | stopTaskAndDismissDialog(); |
| 153 | } |
| 154 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 155 | private void onProgressUpdate(int index, ProgressListener listener) { |
| 156 | mHandler.sendMessage( |
| 157 | mHandler.obtainMessage(MSG_TASK_UPDATE, index, 0, listener)); |
| 158 | } |
| 159 | |
Bobby Georgescu | ba50b94 | 2012-08-08 00:09:50 -0700 | [diff] [blame] | 160 | private void onProgressStart(ProgressListener listener) { |
| 161 | mHandler.sendMessage(mHandler.obtainMessage(MSG_TASK_START, listener)); |
| 162 | } |
| 163 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 164 | private void onProgressComplete(int result, ProgressListener listener) { |
| 165 | mHandler.sendMessage(mHandler.obtainMessage(MSG_TASK_COMPLETE, result, 0, listener)); |
| 166 | } |
| 167 | |
Owen Lin | b21b8e5 | 2012-08-24 12:25:57 +0800 | [diff] [blame] | 168 | public static void updateMenuOperation(Menu menu, int supported) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 169 | boolean supportDelete = (supported & MediaObject.SUPPORT_DELETE) != 0; |
| 170 | boolean supportRotate = (supported & MediaObject.SUPPORT_ROTATE) != 0; |
| 171 | boolean supportCrop = (supported & MediaObject.SUPPORT_CROP) != 0; |
Teng-Hui Zhu | 5e92668 | 2012-09-26 14:20:46 -0700 | [diff] [blame] | 172 | boolean supportTrim = (supported & MediaObject.SUPPORT_TRIM) != 0; |
Teng-Hui Zhu | 648b339 | 2012-11-13 10:39:26 -0800 | [diff] [blame] | 173 | boolean supportMute = (supported & MediaObject.SUPPORT_MUTE) != 0; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 174 | boolean supportShare = (supported & MediaObject.SUPPORT_SHARE) != 0; |
| 175 | boolean supportSetAs = (supported & MediaObject.SUPPORT_SETAS) != 0; |
| 176 | boolean supportShowOnMap = (supported & MediaObject.SUPPORT_SHOW_ON_MAP) != 0; |
| 177 | boolean supportCache = (supported & MediaObject.SUPPORT_CACHE) != 0; |
| 178 | boolean supportEdit = (supported & MediaObject.SUPPORT_EDIT) != 0; |
| 179 | boolean supportInfo = (supported & MediaObject.SUPPORT_INFO) != 0; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 180 | |
Owen Lin | b21b8e5 | 2012-08-24 12:25:57 +0800 | [diff] [blame] | 181 | setMenuItemVisible(menu, R.id.action_delete, supportDelete); |
| 182 | setMenuItemVisible(menu, R.id.action_rotate_ccw, supportRotate); |
| 183 | setMenuItemVisible(menu, R.id.action_rotate_cw, supportRotate); |
| 184 | setMenuItemVisible(menu, R.id.action_crop, supportCrop); |
Teng-Hui Zhu | 50ea2d2 | 2012-08-23 16:45:11 -0700 | [diff] [blame] | 185 | setMenuItemVisible(menu, R.id.action_trim, supportTrim); |
Teng-Hui Zhu | 648b339 | 2012-11-13 10:39:26 -0800 | [diff] [blame] | 186 | setMenuItemVisible(menu, R.id.action_mute, supportMute); |
George Mount | 4b4dbd2 | 2012-10-18 14:20:39 -0700 | [diff] [blame] | 187 | // Hide panorama until call to updateMenuForPanorama corrects it |
| 188 | setMenuItemVisible(menu, R.id.action_share_panorama, false); |
Owen Lin | b21b8e5 | 2012-08-24 12:25:57 +0800 | [diff] [blame] | 189 | setMenuItemVisible(menu, R.id.action_share, supportShare); |
| 190 | setMenuItemVisible(menu, R.id.action_setas, supportSetAs); |
| 191 | setMenuItemVisible(menu, R.id.action_show_on_map, supportShowOnMap); |
| 192 | setMenuItemVisible(menu, R.id.action_edit, supportEdit); |
| 193 | setMenuItemVisible(menu, R.id.action_details, supportInfo); |
Owen Lin | b21b8e5 | 2012-08-24 12:25:57 +0800 | [diff] [blame] | 194 | } |
| 195 | |
George Mount | 4b4dbd2 | 2012-10-18 14:20:39 -0700 | [diff] [blame] | 196 | public static void updateMenuForPanorama(Menu menu, boolean shareAsPanorama360, |
| 197 | boolean disablePanorama360Options) { |
| 198 | setMenuItemVisible(menu, R.id.action_share_panorama, shareAsPanorama360); |
| 199 | if (disablePanorama360Options) { |
| 200 | setMenuItemVisible(menu, R.id.action_rotate_ccw, false); |
| 201 | setMenuItemVisible(menu, R.id.action_rotate_cw, false); |
George Mount | 4b4dbd2 | 2012-10-18 14:20:39 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Owen Lin | b21b8e5 | 2012-08-24 12:25:57 +0800 | [diff] [blame] | 205 | private static void setMenuItemVisible(Menu menu, int itemId, boolean visible) { |
| 206 | MenuItem item = menu.findItem(itemId); |
| 207 | if (item != null) item.setVisible(visible); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | private Path getSingleSelectedPath() { |
| 211 | ArrayList<Path> ids = mSelectionManager.getSelected(true); |
| 212 | Utils.assertTrue(ids.size() == 1); |
| 213 | return ids.get(0); |
| 214 | } |
| 215 | |
Yuli Huang | f50ce2b | 2012-05-02 00:50:56 +0800 | [diff] [blame] | 216 | private Intent getIntentBySingleSelectedPath(String action) { |
| 217 | DataManager manager = mActivity.getDataManager(); |
| 218 | Path path = getSingleSelectedPath(); |
Mangesh Ghiware | 5172dee | 2012-09-27 21:05:41 -0700 | [diff] [blame] | 219 | String mimeType = getMimeType(manager.getMediaType(path)); |
Yuli Huang | f50ce2b | 2012-05-02 00:50:56 +0800 | [diff] [blame] | 220 | return new Intent(action).setDataAndType(manager.getContentUri(path), mimeType); |
| 221 | } |
| 222 | |
Ray Chen | 67098d1 | 2012-04-05 17:25:43 +0800 | [diff] [blame] | 223 | private void onMenuClicked(int action, ProgressListener listener) { |
Chih-Chung Chang | 6b891c6 | 2012-06-07 20:09:13 +0800 | [diff] [blame] | 224 | onMenuClicked(action, listener, false, true); |
| 225 | } |
| 226 | |
| 227 | public void onMenuClicked(int action, ProgressListener listener, |
| 228 | boolean waitOnStop, boolean showDialog) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 229 | int title; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 230 | switch (action) { |
| 231 | case R.id.action_select_all: |
| 232 | if (mSelectionManager.inSelectAllMode()) { |
| 233 | mSelectionManager.deSelectAll(); |
| 234 | } else { |
| 235 | mSelectionManager.selectAll(); |
| 236 | } |
Wu-cheng Li | 57263d3 | 2012-04-30 23:49:17 +0800 | [diff] [blame] | 237 | return; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 238 | case R.id.action_crop: { |
Ruben Brunk | 5aa454b | 2012-10-25 15:53:42 -0700 | [diff] [blame] | 239 | Intent intent = getIntentBySingleSelectedPath(FilterShowActivity.CROP_ACTION) |
| 240 | .setClass((Activity) mActivity, FilterShowActivity.class); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 241 | ((Activity) mActivity).startActivity(intent); |
Wu-cheng Li | 57263d3 | 2012-04-30 23:49:17 +0800 | [diff] [blame] | 242 | return; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 243 | } |
Yuli Huang | f50ce2b | 2012-05-02 00:50:56 +0800 | [diff] [blame] | 244 | case R.id.action_edit: { |
| 245 | Intent intent = getIntentBySingleSelectedPath(Intent.ACTION_EDIT) |
| 246 | .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 247 | ((Activity) mActivity).startActivity(Intent.createChooser(intent, null)); |
| 248 | return; |
| 249 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 250 | case R.id.action_setas: { |
Yuli Huang | f50ce2b | 2012-05-02 00:50:56 +0800 | [diff] [blame] | 251 | Intent intent = getIntentBySingleSelectedPath(Intent.ACTION_ATTACH_DATA) |
| 252 | .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 253 | intent.putExtra("mimeType", intent.getType()); |
Owen Lin | 28cb416 | 2012-08-29 11:53:10 +0800 | [diff] [blame] | 254 | Activity activity = mActivity; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 255 | activity.startActivity(Intent.createChooser( |
| 256 | intent, activity.getString(R.string.set_as))); |
Wu-cheng Li | 57263d3 | 2012-04-30 23:49:17 +0800 | [diff] [blame] | 257 | return; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 258 | } |
Ray Chen | 67098d1 | 2012-04-05 17:25:43 +0800 | [diff] [blame] | 259 | case R.id.action_delete: |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 260 | title = R.string.delete; |
| 261 | break; |
| 262 | case R.id.action_rotate_cw: |
| 263 | title = R.string.rotate_right; |
| 264 | break; |
| 265 | case R.id.action_rotate_ccw: |
| 266 | title = R.string.rotate_left; |
| 267 | break; |
| 268 | case R.id.action_show_on_map: |
| 269 | title = R.string.show_on_map; |
| 270 | break; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 271 | default: |
Ray Chen | 67098d1 | 2012-04-05 17:25:43 +0800 | [diff] [blame] | 272 | return; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 273 | } |
Chih-Chung Chang | 6b891c6 | 2012-06-07 20:09:13 +0800 | [diff] [blame] | 274 | startAction(action, title, listener, waitOnStop, showDialog); |
Ray Chen | 67098d1 | 2012-04-05 17:25:43 +0800 | [diff] [blame] | 275 | } |
| 276 | |
Owen Lin | d759b7c | 2012-05-16 15:32:02 -0700 | [diff] [blame] | 277 | private class ConfirmDialogListener implements OnClickListener, OnCancelListener { |
| 278 | private final int mActionId; |
| 279 | private final ProgressListener mListener; |
| 280 | |
| 281 | public ConfirmDialogListener(int actionId, ProgressListener listener) { |
| 282 | mActionId = actionId; |
| 283 | mListener = listener; |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public void onClick(DialogInterface dialog, int which) { |
| 288 | if (which == DialogInterface.BUTTON_POSITIVE) { |
| 289 | if (mListener != null) { |
| 290 | mListener.onConfirmDialogDismissed(true); |
| 291 | } |
| 292 | onMenuClicked(mActionId, mListener); |
| 293 | } else { |
| 294 | if (mListener != null) { |
| 295 | mListener.onConfirmDialogDismissed(false); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public void onCancel(DialogInterface dialog) { |
| 302 | if (mListener != null) { |
| 303 | mListener.onConfirmDialogDismissed(false); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
Ray Chen | 9a033b0 | 2012-05-15 11:22:21 +0800 | [diff] [blame] | 308 | public void onMenuClicked(MenuItem menuItem, String confirmMsg, |
Ray Chen | 67098d1 | 2012-04-05 17:25:43 +0800 | [diff] [blame] | 309 | final ProgressListener listener) { |
| 310 | final int action = menuItem.getItemId(); |
| 311 | |
Ray Chen | 9a033b0 | 2012-05-15 11:22:21 +0800 | [diff] [blame] | 312 | if (confirmMsg != null) { |
Owen Lin | d759b7c | 2012-05-16 15:32:02 -0700 | [diff] [blame] | 313 | if (listener != null) listener.onConfirmDialogShown(); |
| 314 | ConfirmDialogListener cdl = new ConfirmDialogListener(action, listener); |
Ray Chen | 67098d1 | 2012-04-05 17:25:43 +0800 | [diff] [blame] | 315 | new AlertDialog.Builder(mActivity.getAndroidContext()) |
Ray Chen | 9a033b0 | 2012-05-15 11:22:21 +0800 | [diff] [blame] | 316 | .setMessage(confirmMsg) |
Owen Lin | d759b7c | 2012-05-16 15:32:02 -0700 | [diff] [blame] | 317 | .setOnCancelListener(cdl) |
| 318 | .setPositiveButton(R.string.ok, cdl) |
| 319 | .setNegativeButton(R.string.cancel, cdl) |
| 320 | .create().show(); |
Ray Chen | 67098d1 | 2012-04-05 17:25:43 +0800 | [diff] [blame] | 321 | } else { |
| 322 | onMenuClicked(action, listener); |
| 323 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | public void startAction(int action, int title, ProgressListener listener) { |
Chih-Chung Chang | 6b891c6 | 2012-06-07 20:09:13 +0800 | [diff] [blame] | 327 | startAction(action, title, listener, false, true); |
| 328 | } |
| 329 | |
| 330 | public void startAction(int action, int title, ProgressListener listener, |
| 331 | boolean waitOnStop, boolean showDialog) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 332 | ArrayList<Path> ids = mSelectionManager.getSelected(false); |
Ray Chen | b2b4518 | 2011-08-31 16:37:04 +0800 | [diff] [blame] | 333 | stopTaskAndDismissDialog(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 334 | |
Owen Lin | 28cb416 | 2012-08-29 11:53:10 +0800 | [diff] [blame] | 335 | Activity activity = mActivity; |
Chih-Chung Chang | 6b891c6 | 2012-06-07 20:09:13 +0800 | [diff] [blame] | 336 | if (showDialog) { |
Bobby Georgescu | d00f521 | 2013-01-28 16:16:44 -0800 | [diff] [blame^] | 337 | mDialog = createProgressDialog(activity, title, ids.size()); |
Chih-Chung Chang | 6b891c6 | 2012-06-07 20:09:13 +0800 | [diff] [blame] | 338 | mDialog.show(); |
Bobby Georgescu | d00f521 | 2013-01-28 16:16:44 -0800 | [diff] [blame^] | 339 | } else { |
| 340 | mDialog = null; |
Chih-Chung Chang | 6b891c6 | 2012-06-07 20:09:13 +0800 | [diff] [blame] | 341 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 342 | MediaOperation operation = new MediaOperation(action, ids, listener); |
Bobby Georgescu | a2d0d34 | 2012-12-03 13:56:00 -0800 | [diff] [blame] | 343 | mTask = mActivity.getBatchServiceThreadPoolIfAvailable().submit(operation, null); |
Chih-Chung Chang | 6b891c6 | 2012-06-07 20:09:13 +0800 | [diff] [blame] | 344 | mWaitOnStop = waitOnStop; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 345 | } |
| 346 | |
Bobby Georgescu | d00f521 | 2013-01-28 16:16:44 -0800 | [diff] [blame^] | 347 | public void startSingleItemAction(int action, Path targetPath) { |
| 348 | ArrayList<Path> ids = new ArrayList<Path>(1); |
| 349 | ids.add(targetPath); |
| 350 | mDialog = null; |
| 351 | MediaOperation operation = new MediaOperation(action, ids, null); |
| 352 | mTask = mActivity.getBatchServiceThreadPoolIfAvailable().submit(operation, null); |
| 353 | mWaitOnStop = false; |
| 354 | } |
| 355 | |
Mangesh Ghiware | 5172dee | 2012-09-27 21:05:41 -0700 | [diff] [blame] | 356 | public static String getMimeType(int type) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 357 | switch (type) { |
| 358 | case MediaObject.MEDIA_TYPE_IMAGE : |
Mangesh Ghiware | 5172dee | 2012-09-27 21:05:41 -0700 | [diff] [blame] | 359 | return GalleryUtils.MIME_TYPE_IMAGE; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 360 | case MediaObject.MEDIA_TYPE_VIDEO : |
Mangesh Ghiware | 5172dee | 2012-09-27 21:05:41 -0700 | [diff] [blame] | 361 | return GalleryUtils.MIME_TYPE_VIDEO; |
| 362 | default: return GalleryUtils.MIME_TYPE_ALL; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | private boolean execute( |
| 367 | DataManager manager, JobContext jc, int cmd, Path path) { |
| 368 | boolean result = true; |
Ray Chen | b2b4518 | 2011-08-31 16:37:04 +0800 | [diff] [blame] | 369 | Log.v(TAG, "Execute cmd: " + cmd + " for " + path); |
| 370 | long startTime = System.currentTimeMillis(); |
| 371 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 372 | switch (cmd) { |
Ray Chen | 67098d1 | 2012-04-05 17:25:43 +0800 | [diff] [blame] | 373 | case R.id.action_delete: |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 374 | manager.delete(path); |
| 375 | break; |
| 376 | case R.id.action_rotate_cw: |
| 377 | manager.rotate(path, 90); |
| 378 | break; |
| 379 | case R.id.action_rotate_ccw: |
| 380 | manager.rotate(path, -90); |
| 381 | break; |
| 382 | case R.id.action_toggle_full_caching: { |
| 383 | MediaObject obj = manager.getMediaObject(path); |
| 384 | int cacheFlag = obj.getCacheFlag(); |
| 385 | if (cacheFlag == MediaObject.CACHE_FLAG_FULL) { |
| 386 | cacheFlag = MediaObject.CACHE_FLAG_SCREENNAIL; |
| 387 | } else { |
| 388 | cacheFlag = MediaObject.CACHE_FLAG_FULL; |
| 389 | } |
| 390 | obj.cache(cacheFlag); |
| 391 | break; |
| 392 | } |
| 393 | case R.id.action_show_on_map: { |
| 394 | MediaItem item = (MediaItem) manager.getMediaObject(path); |
| 395 | double latlng[] = new double[2]; |
| 396 | item.getLatLong(latlng); |
| 397 | if (GalleryUtils.isValidLocation(latlng[0], latlng[1])) { |
Owen Lin | 28cb416 | 2012-08-29 11:53:10 +0800 | [diff] [blame] | 398 | GalleryUtils.showOnMap(mActivity, latlng[0], latlng[1]); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 399 | } |
| 400 | break; |
| 401 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 402 | default: |
| 403 | throw new AssertionError(); |
| 404 | } |
Ray Chen | b2b4518 | 2011-08-31 16:37:04 +0800 | [diff] [blame] | 405 | Log.v(TAG, "It takes " + (System.currentTimeMillis() - startTime) + |
| 406 | " ms to execute cmd for " + path); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 407 | return result; |
| 408 | } |
| 409 | |
| 410 | private class MediaOperation implements Job<Void> { |
| 411 | private final ArrayList<Path> mItems; |
| 412 | private final int mOperation; |
| 413 | private final ProgressListener mListener; |
| 414 | |
Chih-Chung Chang | 6b891c6 | 2012-06-07 20:09:13 +0800 | [diff] [blame] | 415 | public MediaOperation(int operation, ArrayList<Path> items, |
| 416 | ProgressListener listener) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 417 | mOperation = operation; |
| 418 | mItems = items; |
| 419 | mListener = listener; |
| 420 | } |
| 421 | |
Ahbong Chang | 7817979 | 2012-07-30 11:34:13 +0800 | [diff] [blame] | 422 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 423 | public Void run(JobContext jc) { |
| 424 | int index = 0; |
| 425 | DataManager manager = mActivity.getDataManager(); |
| 426 | int result = EXECUTION_RESULT_SUCCESS; |
Ray Chen | b2b4518 | 2011-08-31 16:37:04 +0800 | [diff] [blame] | 427 | try { |
Bobby Georgescu | ba50b94 | 2012-08-08 00:09:50 -0700 | [diff] [blame] | 428 | onProgressStart(mListener); |
Ray Chen | b2b4518 | 2011-08-31 16:37:04 +0800 | [diff] [blame] | 429 | for (Path id : mItems) { |
| 430 | if (jc.isCancelled()) { |
| 431 | result = EXECUTION_RESULT_CANCEL; |
| 432 | break; |
| 433 | } |
| 434 | if (!execute(manager, jc, mOperation, id)) { |
| 435 | result = EXECUTION_RESULT_FAIL; |
| 436 | } |
| 437 | onProgressUpdate(index++, mListener); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 438 | } |
Ray Chen | b2b4518 | 2011-08-31 16:37:04 +0800 | [diff] [blame] | 439 | } catch (Throwable th) { |
| 440 | Log.e(TAG, "failed to execute operation " + mOperation |
| 441 | + " : " + th); |
| 442 | } finally { |
| 443 | onProgressComplete(result, mListener); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 444 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 445 | return null; |
| 446 | } |
| 447 | } |
| 448 | } |