2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.ui;
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.ProgressDialog;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.DialogInterface.OnCancelListener;
25 import android.content.DialogInterface.OnClickListener;
26 import android.content.Intent;
27 import android.os.Handler;
28 import android.os.Message;
29 import android.view.Menu;
30 import android.view.MenuItem;
32 import com.android.gallery3d.R;
33 import com.android.gallery3d.app.AbstractGalleryActivity;
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;
39 import com.android.gallery3d.filtershow.crop.CropActivity;
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;
45 import java.util.ArrayList;
47 public class MenuExecutor {
48 @SuppressWarnings("unused")
49 private static final String TAG = "MenuExecutor";
51 private static final int MSG_TASK_COMPLETE = 1;
52 private static final int MSG_TASK_UPDATE = 2;
53 private static final int MSG_TASK_START = 3;
54 private static final int MSG_DO_SHARE = 4;
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;
60 private ProgressDialog mDialog;
61 private Future<?> mTask;
62 // wait the operation to finish when we want to stop it.
63 private boolean mWaitOnStop;
64 private boolean mPaused;
66 private final AbstractGalleryActivity mActivity;
67 private final SelectionManager mSelectionManager;
68 private final Handler mHandler;
70 private static ProgressDialog createProgressDialog(
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);
83 public interface ProgressListener {
84 public void onConfirmDialogShown();
85 public void onConfirmDialogDismissed(boolean confirmed);
86 public void onProgressStart();
87 public void onProgressUpdate(int index);
88 public void onProgressComplete(int result);
92 AbstractGalleryActivity activity, SelectionManager selectionManager) {
93 mActivity = Utils.checkNotNull(activity);
94 mSelectionManager = Utils.checkNotNull(selectionManager);
95 mHandler = new SynchronizedHandler(mActivity.getGLRoot()) {
97 public void handleMessage(Message message) {
98 switch (message.what) {
99 case MSG_TASK_START: {
100 if (message.obj != null) {
101 ProgressListener listener = (ProgressListener) message.obj;
102 listener.onProgressStart();
106 case MSG_TASK_COMPLETE: {
107 stopTaskAndDismissDialog();
108 if (message.obj != null) {
109 ProgressListener listener = (ProgressListener) message.obj;
110 listener.onProgressComplete(message.arg1);
112 mSelectionManager.leaveSelectionMode();
115 case MSG_TASK_UPDATE: {
116 if (mDialog != null && !mPaused) mDialog.setProgress(message.arg1);
117 if (message.obj != null) {
118 ProgressListener listener = (ProgressListener) message.obj;
119 listener.onProgressUpdate(message.arg1);
124 ((Activity) mActivity).startActivity((Intent) message.obj);
132 private void stopTaskAndDismissDialog() {
134 if (!mWaitOnStop) mTask.cancel();
135 if (mDialog != null && mDialog.isShowing()) mDialog.dismiss();
141 public void resume() {
143 if (mDialog != null) mDialog.show();
146 public void pause() {
148 if (mDialog != null && mDialog.isShowing()) mDialog.hide();
151 public void destroy() {
152 stopTaskAndDismissDialog();
155 private void onProgressUpdate(int index, ProgressListener listener) {
156 mHandler.sendMessage(
157 mHandler.obtainMessage(MSG_TASK_UPDATE, index, 0, listener));
160 private void onProgressStart(ProgressListener listener) {
161 mHandler.sendMessage(mHandler.obtainMessage(MSG_TASK_START, listener));
164 private void onProgressComplete(int result, ProgressListener listener) {
165 mHandler.sendMessage(mHandler.obtainMessage(MSG_TASK_COMPLETE, result, 0, listener));
168 public static void updateMenuOperation(Menu menu, int supported) {
169 boolean supportDelete = (supported & MediaObject.SUPPORT_DELETE) != 0;
170 boolean supportRotate = (supported & MediaObject.SUPPORT_ROTATE) != 0;
171 boolean supportCrop = (supported & MediaObject.SUPPORT_CROP) != 0;
172 boolean supportTrim = (supported & MediaObject.SUPPORT_TRIM) != 0;
173 boolean supportMute = (supported & MediaObject.SUPPORT_MUTE) != 0;
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;
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);
185 setMenuItemVisible(menu, R.id.action_trim, supportTrim);
186 setMenuItemVisible(menu, R.id.action_mute, supportMute);
187 // Hide panorama until call to updateMenuForPanorama corrects it
188 setMenuItemVisible(menu, R.id.action_share_panorama, false);
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_simple_edit, supportEdit);
194 setMenuItemVisible(menu, R.id.action_details, supportInfo);
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);
206 private static void setMenuItemVisible(Menu menu, int itemId, boolean visible) {
207 MenuItem item = menu.findItem(itemId);
208 if (item != null) item.setVisible(visible);
211 private Path getSingleSelectedPath() {
212 ArrayList<Path> ids = mSelectionManager.getSelected(true);
213 Utils.assertTrue(ids.size() == 1);
217 private Intent getIntentBySingleSelectedPath(String action) {
218 DataManager manager = mActivity.getDataManager();
219 Path path = getSingleSelectedPath();
220 String mimeType = getMimeType(manager.getMediaType(path));
221 return new Intent(action).setDataAndType(manager.getContentUri(path), mimeType);
224 private void onMenuClicked(int action, ProgressListener listener) {
225 onMenuClicked(action, listener, false, true);
228 public void onMenuClicked(int action, ProgressListener listener,
229 boolean waitOnStop, boolean showDialog) {
232 case R.id.action_select_all:
233 if (mSelectionManager.inSelectAllMode()) {
234 mSelectionManager.deSelectAll();
236 mSelectionManager.selectAll();
239 case R.id.action_crop: {
240 Intent intent = getIntentBySingleSelectedPath(CropActivity.CROP_ACTION);
241 ((Activity) mActivity).startActivity(intent);
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));
250 case R.id.action_setas: {
251 Intent intent = getIntentBySingleSelectedPath(Intent.ACTION_ATTACH_DATA)
252 .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
253 intent.putExtra("mimeType", intent.getType());
254 Activity activity = mActivity;
255 activity.startActivity(Intent.createChooser(
256 intent, activity.getString(R.string.set_as)));
259 case R.id.action_delete:
260 title = R.string.delete;
262 case R.id.action_rotate_cw:
263 title = R.string.rotate_right;
265 case R.id.action_rotate_ccw:
266 title = R.string.rotate_left;
268 case R.id.action_show_on_map:
269 title = R.string.show_on_map;
274 startAction(action, title, listener, waitOnStop, showDialog);
277 private class ConfirmDialogListener implements OnClickListener, OnCancelListener {
278 private final int mActionId;
279 private final ProgressListener mListener;
281 public ConfirmDialogListener(int actionId, ProgressListener listener) {
282 mActionId = actionId;
283 mListener = listener;
287 public void onClick(DialogInterface dialog, int which) {
288 if (which == DialogInterface.BUTTON_POSITIVE) {
289 if (mListener != null) {
290 mListener.onConfirmDialogDismissed(true);
292 onMenuClicked(mActionId, mListener);
294 if (mListener != null) {
295 mListener.onConfirmDialogDismissed(false);
301 public void onCancel(DialogInterface dialog) {
302 if (mListener != null) {
303 mListener.onConfirmDialogDismissed(false);
308 public void onMenuClicked(MenuItem menuItem, String confirmMsg,
309 final ProgressListener listener) {
310 final int action = menuItem.getItemId();
312 if (confirmMsg != null) {
313 if (listener != null) listener.onConfirmDialogShown();
314 ConfirmDialogListener cdl = new ConfirmDialogListener(action, listener);
315 new AlertDialog.Builder(mActivity.getAndroidContext())
316 .setMessage(confirmMsg)
317 .setOnCancelListener(cdl)
318 .setPositiveButton(R.string.ok, cdl)
319 .setNegativeButton(R.string.cancel, cdl)
322 onMenuClicked(action, listener);
326 public void startAction(int action, int title, ProgressListener listener) {
327 startAction(action, title, listener, false, true);
330 public void startAction(int action, int title, ProgressListener listener,
331 boolean waitOnStop, boolean showDialog) {
332 ArrayList<Path> ids = mSelectionManager.getSelected(false);
333 stopTaskAndDismissDialog();
335 Activity activity = mActivity;
337 mDialog = createProgressDialog(activity, title, ids.size());
342 MediaOperation operation = new MediaOperation(action, ids, listener);
343 mTask = mActivity.getBatchServiceThreadPoolIfAvailable().submit(operation, null);
344 mWaitOnStop = waitOnStop;
347 public void startSingleItemAction(int action, Path targetPath) {
348 ArrayList<Path> ids = new ArrayList<Path>(1);
351 MediaOperation operation = new MediaOperation(action, ids, null);
352 mTask = mActivity.getBatchServiceThreadPoolIfAvailable().submit(operation, null);
356 public static String getMimeType(int type) {
358 case MediaObject.MEDIA_TYPE_IMAGE :
359 return GalleryUtils.MIME_TYPE_IMAGE;
360 case MediaObject.MEDIA_TYPE_VIDEO :
361 return GalleryUtils.MIME_TYPE_VIDEO;
362 default: return GalleryUtils.MIME_TYPE_ALL;
366 private boolean execute(
367 DataManager manager, JobContext jc, int cmd, Path path) {
368 boolean result = true;
369 Log.v(TAG, "Execute cmd: " + cmd + " for " + path);
370 long startTime = System.currentTimeMillis();
373 case R.id.action_delete:
374 manager.delete(path);
376 case R.id.action_rotate_cw:
377 manager.rotate(path, 90);
379 case R.id.action_rotate_ccw:
380 manager.rotate(path, -90);
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;
388 cacheFlag = MediaObject.CACHE_FLAG_FULL;
390 obj.cache(cacheFlag);
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])) {
398 GalleryUtils.showOnMap(mActivity, latlng[0], latlng[1]);
403 throw new AssertionError();
405 Log.v(TAG, "It takes " + (System.currentTimeMillis() - startTime) +
406 " ms to execute cmd for " + path);
410 private class MediaOperation implements Job<Void> {
411 private final ArrayList<Path> mItems;
412 private final int mOperation;
413 private final ProgressListener mListener;
415 public MediaOperation(int operation, ArrayList<Path> items,
416 ProgressListener listener) {
417 mOperation = operation;
419 mListener = listener;
423 public Void run(JobContext jc) {
425 DataManager manager = mActivity.getDataManager();
426 int result = EXECUTION_RESULT_SUCCESS;
428 onProgressStart(mListener);
429 for (Path id : mItems) {
430 if (jc.isCancelled()) {
431 result = EXECUTION_RESULT_CANCEL;
434 if (!execute(manager, jc, mOperation, id)) {
435 result = EXECUTION_RESULT_FAIL;
437 onProgressUpdate(index++, mListener);
439 } catch (Throwable th) {
440 Log.e(TAG, "failed to execute operation " + mOperation
443 onProgressComplete(result, mListener);