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