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;
30 import com.actionbarsherlock.view.Menu;
31 import com.actionbarsherlock.view.MenuItem;
32 import com.android.gallery3d.R;
33 import com.android.gallery3d.app.AbstractGalleryActivity;
34 import com.android.gallery3d.app.CropImage;
35 import com.android.gallery3d.common.ApiHelper;
36 import com.android.gallery3d.common.Utils;
37 import com.android.gallery3d.data.DataManager;
38 import com.android.gallery3d.data.MediaItem;
39 import com.android.gallery3d.data.MediaObject;
40 import com.android.gallery3d.data.Path;
41 import com.android.gallery3d.filtershow.FilterShowActivity;
42 import com.android.gallery3d.util.Future;
43 import com.android.gallery3d.util.GalleryUtils;
44 import com.android.gallery3d.util.ThreadPool.Job;
45 import com.android.gallery3d.util.ThreadPool.JobContext;
47 import java.util.ArrayList;
49 public class MenuExecutor {
50 @SuppressWarnings("unused")
51 private static final String TAG = "MenuExecutor";
53 private static final int MSG_TASK_COMPLETE = 1;
54 private static final int MSG_TASK_UPDATE = 2;
55 private static final int MSG_TASK_START = 3;
56 private static final int MSG_DO_SHARE = 4;
58 public static final int EXECUTION_RESULT_SUCCESS = 1;
59 public static final int EXECUTION_RESULT_FAIL = 2;
60 public static final int EXECUTION_RESULT_CANCEL = 3;
62 private ProgressDialog mDialog;
63 private Future<?> mTask;
64 // wait the operation to finish when we want to stop it.
65 private boolean mWaitOnStop;
67 private final AbstractGalleryActivity mActivity;
68 private final SelectionManager mSelectionManager;
69 private final Handler mHandler;
71 private static ProgressDialog createProgressDialog(
72 Context context, int titleId, int progressMax) {
73 ProgressDialog dialog = new ProgressDialog(context);
74 dialog.setTitle(titleId);
75 dialog.setMax(progressMax);
76 dialog.setCancelable(false);
77 dialog.setIndeterminate(false);
78 if (progressMax > 1) {
79 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
84 public interface ProgressListener {
85 public void onConfirmDialogShown();
86 public void onConfirmDialogDismissed(boolean confirmed);
87 public void onProgressStart();
88 public void onProgressUpdate(int index);
89 public void onProgressComplete(int result);
93 AbstractGalleryActivity activity, SelectionManager selectionManager) {
94 mActivity = Utils.checkNotNull(activity);
95 mSelectionManager = Utils.checkNotNull(selectionManager);
96 mHandler = new SynchronizedHandler(mActivity.getGLRoot()) {
98 public void handleMessage(Message message) {
99 switch (message.what) {
100 case MSG_TASK_START: {
101 if (message.obj != null) {
102 ProgressListener listener = (ProgressListener) message.obj;
103 listener.onProgressStart();
107 case MSG_TASK_COMPLETE: {
108 stopTaskAndDismissDialog();
109 if (message.obj != null) {
110 ProgressListener listener = (ProgressListener) message.obj;
111 listener.onProgressComplete(message.arg1);
113 mSelectionManager.leaveSelectionMode();
116 case MSG_TASK_UPDATE: {
117 if (mDialog != null) mDialog.setProgress(message.arg1);
118 if (message.obj != null) {
119 ProgressListener listener = (ProgressListener) message.obj;
120 listener.onProgressUpdate(message.arg1);
125 ((Activity) mActivity).startActivity((Intent) message.obj);
133 private void stopTaskAndDismissDialog() {
135 if (!mWaitOnStop) mTask.cancel();
143 public void pause() {
144 stopTaskAndDismissDialog();
147 private void onProgressUpdate(int index, ProgressListener listener) {
148 mHandler.sendMessage(
149 mHandler.obtainMessage(MSG_TASK_UPDATE, index, 0, listener));
152 private void onProgressStart(ProgressListener listener) {
153 mHandler.sendMessage(mHandler.obtainMessage(MSG_TASK_START, listener));
156 private void onProgressComplete(int result, ProgressListener listener) {
157 mHandler.sendMessage(mHandler.obtainMessage(MSG_TASK_COMPLETE, result, 0, listener));
160 public static void updateMenuOperation(Menu menu, int supported) {
161 boolean supportDelete = (supported & MediaObject.SUPPORT_DELETE) != 0;
162 boolean supportRotate = (supported & MediaObject.SUPPORT_ROTATE) != 0;
163 boolean supportCrop = (supported & MediaObject.SUPPORT_CROP) != 0;
164 boolean supportTrim = (supported & MediaObject.SUPPORT_TRIM) != 0;
165 boolean supportMute = (supported & MediaObject.SUPPORT_MUTE) != 0;
166 boolean supportShare = (supported & MediaObject.SUPPORT_SHARE) != 0;
167 boolean supportSetAs = (supported & MediaObject.SUPPORT_SETAS) != 0;
168 boolean supportShowOnMap = (supported & MediaObject.SUPPORT_SHOW_ON_MAP) != 0;
169 boolean supportCache = (supported & MediaObject.SUPPORT_CACHE) != 0;
170 boolean supportEdit = (supported & MediaObject.SUPPORT_EDIT) != 0;
171 boolean supportInfo = (supported & MediaObject.SUPPORT_INFO) != 0;
172 boolean supportImport = (supported & MediaObject.SUPPORT_IMPORT) != 0;
174 setMenuItemVisible(menu, R.id.action_delete, supportDelete);
175 setMenuItemVisible(menu, R.id.action_rotate_ccw, supportRotate);
176 setMenuItemVisible(menu, R.id.action_rotate_cw, supportRotate);
177 setMenuItemVisible(menu, R.id.action_crop, supportCrop);
178 setMenuItemVisible(menu, R.id.action_trim, supportTrim);
179 setMenuItemVisible(menu, R.id.action_mute, supportMute);
180 // Hide panorama until call to updateMenuForPanorama corrects it
181 setMenuItemVisible(menu, R.id.action_share_panorama, false);
182 setMenuItemVisible(menu, R.id.action_share, supportShare);
183 setMenuItemVisible(menu, R.id.action_setas, supportSetAs);
184 setMenuItemVisible(menu, R.id.action_show_on_map, supportShowOnMap);
185 setMenuItemVisible(menu, R.id.action_edit, supportEdit);
186 setMenuItemVisible(menu, R.id.action_details, supportInfo);
187 setMenuItemVisible(menu, R.id.action_import, supportImport);
190 public static void updateMenuForPanorama(Menu menu, boolean shareAsPanorama360,
191 boolean disablePanorama360Options) {
192 setMenuItemVisible(menu, R.id.action_share_panorama, shareAsPanorama360);
193 if (disablePanorama360Options) {
194 setMenuItemVisible(menu, R.id.action_rotate_ccw, false);
195 setMenuItemVisible(menu, R.id.action_rotate_cw, false);
199 private static void setMenuItemVisible(Menu menu, int itemId, boolean visible) {
200 MenuItem item = menu.findItem(itemId);
201 if (item != null) item.setVisible(visible);
204 private Path getSingleSelectedPath() {
205 ArrayList<Path> ids = mSelectionManager.getSelected(true);
206 Utils.assertTrue(ids.size() == 1);
210 private Intent getIntentBySingleSelectedPath(String action) {
211 DataManager manager = mActivity.getDataManager();
212 Path path = getSingleSelectedPath();
213 String mimeType = getMimeType(manager.getMediaType(path));
214 return new Intent(action).setDataAndType(manager.getContentUri(path), mimeType);
217 private void onMenuClicked(int action, ProgressListener listener) {
218 onMenuClicked(action, listener, false, true);
221 public void onMenuClicked(int action, ProgressListener listener,
222 boolean waitOnStop, boolean showDialog) {
225 case R.id.action_select_all:
226 if (mSelectionManager.inSelectAllMode()) {
227 mSelectionManager.deSelectAll();
229 mSelectionManager.selectAll();
232 case R.id.action_crop: {
233 Intent intent = getIntentBySingleSelectedPath(FilterShowActivity.CROP_ACTION)
234 .setClass((Activity) mActivity, FilterShowActivity.class);
235 ((Activity) mActivity).startActivity(intent);
238 case R.id.action_edit: {
239 Intent intent = getIntentBySingleSelectedPath(Intent.ACTION_EDIT)
240 .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
241 ((Activity) mActivity).startActivity(Intent.createChooser(intent, null));
244 case R.id.action_setas: {
245 Intent intent = getIntentBySingleSelectedPath(Intent.ACTION_ATTACH_DATA)
246 .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
247 intent.putExtra("mimeType", intent.getType());
248 Activity activity = mActivity;
249 activity.startActivity(Intent.createChooser(
250 intent, activity.getString(R.string.set_as)));
253 case R.id.action_delete:
254 title = R.string.delete;
256 case R.id.action_rotate_cw:
257 title = R.string.rotate_right;
259 case R.id.action_rotate_ccw:
260 title = R.string.rotate_left;
262 case R.id.action_show_on_map:
263 title = R.string.show_on_map;
265 case R.id.action_import:
266 title = R.string.Import;
271 startAction(action, title, listener, waitOnStop, showDialog);
274 private class ConfirmDialogListener implements OnClickListener, OnCancelListener {
275 private final int mActionId;
276 private final ProgressListener mListener;
278 public ConfirmDialogListener(int actionId, ProgressListener listener) {
279 mActionId = actionId;
280 mListener = listener;
284 public void onClick(DialogInterface dialog, int which) {
285 if (which == DialogInterface.BUTTON_POSITIVE) {
286 if (mListener != null) {
287 mListener.onConfirmDialogDismissed(true);
289 onMenuClicked(mActionId, mListener);
291 if (mListener != null) {
292 mListener.onConfirmDialogDismissed(false);
298 public void onCancel(DialogInterface dialog) {
299 if (mListener != null) {
300 mListener.onConfirmDialogDismissed(false);
305 public void onMenuClicked(MenuItem menuItem, String confirmMsg,
306 final ProgressListener listener) {
307 final int action = menuItem.getItemId();
309 if (confirmMsg != null) {
310 if (listener != null) listener.onConfirmDialogShown();
311 ConfirmDialogListener cdl = new ConfirmDialogListener(action, listener);
312 new AlertDialog.Builder(mActivity.getAndroidContext())
313 .setMessage(confirmMsg)
314 .setOnCancelListener(cdl)
315 .setPositiveButton(R.string.ok, cdl)
316 .setNegativeButton(R.string.cancel, cdl)
319 onMenuClicked(action, listener);
323 public void startAction(int action, int title, ProgressListener listener) {
324 startAction(action, title, listener, false, true);
327 public void startAction(int action, int title, ProgressListener listener,
328 boolean waitOnStop, boolean showDialog) {
329 ArrayList<Path> ids = mSelectionManager.getSelected(false);
330 stopTaskAndDismissDialog();
332 Activity activity = mActivity;
333 mDialog = createProgressDialog(activity, title, ids.size());
337 MediaOperation operation = new MediaOperation(action, ids, listener);
338 mTask = mActivity.getThreadPool().submit(operation, null);
339 mWaitOnStop = waitOnStop;
342 public static String getMimeType(int type) {
344 case MediaObject.MEDIA_TYPE_IMAGE :
345 return GalleryUtils.MIME_TYPE_IMAGE;
346 case MediaObject.MEDIA_TYPE_VIDEO :
347 return GalleryUtils.MIME_TYPE_VIDEO;
348 default: return GalleryUtils.MIME_TYPE_ALL;
352 private boolean execute(
353 DataManager manager, JobContext jc, int cmd, Path path) {
354 boolean result = true;
355 Log.v(TAG, "Execute cmd: " + cmd + " for " + path);
356 long startTime = System.currentTimeMillis();
359 case R.id.action_delete:
360 manager.delete(path);
362 case R.id.action_rotate_cw:
363 manager.rotate(path, 90);
365 case R.id.action_rotate_ccw:
366 manager.rotate(path, -90);
368 case R.id.action_toggle_full_caching: {
369 MediaObject obj = manager.getMediaObject(path);
370 int cacheFlag = obj.getCacheFlag();
371 if (cacheFlag == MediaObject.CACHE_FLAG_FULL) {
372 cacheFlag = MediaObject.CACHE_FLAG_SCREENNAIL;
374 cacheFlag = MediaObject.CACHE_FLAG_FULL;
376 obj.cache(cacheFlag);
379 case R.id.action_show_on_map: {
380 MediaItem item = (MediaItem) manager.getMediaObject(path);
381 double latlng[] = new double[2];
382 item.getLatLong(latlng);
383 if (GalleryUtils.isValidLocation(latlng[0], latlng[1])) {
384 GalleryUtils.showOnMap(mActivity, latlng[0], latlng[1]);
388 case R.id.action_import: {
389 MediaObject obj = manager.getMediaObject(path);
390 result = obj.Import();
394 throw new AssertionError();
396 Log.v(TAG, "It takes " + (System.currentTimeMillis() - startTime) +
397 " ms to execute cmd for " + path);
401 private class MediaOperation implements Job<Void> {
402 private final ArrayList<Path> mItems;
403 private final int mOperation;
404 private final ProgressListener mListener;
406 public MediaOperation(int operation, ArrayList<Path> items,
407 ProgressListener listener) {
408 mOperation = operation;
410 mListener = listener;
414 public Void run(JobContext jc) {
416 DataManager manager = mActivity.getDataManager();
417 int result = EXECUTION_RESULT_SUCCESS;
419 onProgressStart(mListener);
420 for (Path id : mItems) {
421 if (jc.isCancelled()) {
422 result = EXECUTION_RESULT_CANCEL;
425 if (!execute(manager, jc, mOperation, id)) {
426 result = EXECUTION_RESULT_FAIL;
428 onProgressUpdate(index++, mListener);
430 } catch (Throwable th) {
431 Log.e(TAG, "failed to execute operation " + mOperation
434 onProgressComplete(result, mListener);