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