OSDN Git Service

am 68e4ae64: Merge "Fix overdraw in FilterShow activity" into gb-ub-photos-arches
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / ui / MenuExecutor.java
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
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
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;
46
47 import java.util.ArrayList;
48
49 public class MenuExecutor {
50     @SuppressWarnings("unused")
51     private static final String TAG = "MenuExecutor";
52
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;
57
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;
61
62     private ProgressDialog mDialog;
63     private Future<?> mTask;
64     // wait the operation to finish when we want to stop it.
65     private boolean mWaitOnStop;
66
67     private final AbstractGalleryActivity mActivity;
68     private final SelectionManager mSelectionManager;
69     private final Handler mHandler;
70
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);
80         }
81         return dialog;
82     }
83
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);
90     }
91
92     public MenuExecutor(
93             AbstractGalleryActivity activity, SelectionManager selectionManager) {
94         mActivity = Utils.checkNotNull(activity);
95         mSelectionManager = Utils.checkNotNull(selectionManager);
96         mHandler = new SynchronizedHandler(mActivity.getGLRoot()) {
97             @Override
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();
104                         }
105                         break;
106                     }
107                     case MSG_TASK_COMPLETE: {
108                         stopTaskAndDismissDialog();
109                         if (message.obj != null) {
110                             ProgressListener listener = (ProgressListener) message.obj;
111                             listener.onProgressComplete(message.arg1);
112                         }
113                         mSelectionManager.leaveSelectionMode();
114                         break;
115                     }
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);
121                         }
122                         break;
123                     }
124                     case MSG_DO_SHARE: {
125                         ((Activity) mActivity).startActivity((Intent) message.obj);
126                         break;
127                     }
128                 }
129             }
130         };
131     }
132
133     private void stopTaskAndDismissDialog() {
134         if (mTask != null) {
135             if (!mWaitOnStop) mTask.cancel();
136             mTask.waitDone();
137             mDialog.dismiss();
138             mDialog = null;
139             mTask = null;
140         }
141     }
142
143     public void pause() {
144         stopTaskAndDismissDialog();
145     }
146
147     private void onProgressUpdate(int index, ProgressListener listener) {
148         mHandler.sendMessage(
149                 mHandler.obtainMessage(MSG_TASK_UPDATE, index, 0, listener));
150     }
151
152     private void onProgressStart(ProgressListener listener) {
153         mHandler.sendMessage(mHandler.obtainMessage(MSG_TASK_START, listener));
154     }
155
156     private void onProgressComplete(int result, ProgressListener listener) {
157         mHandler.sendMessage(mHandler.obtainMessage(MSG_TASK_COMPLETE, result, 0, listener));
158     }
159
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;
173
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);
188     }
189
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);
196         }
197     }
198
199     private static void setMenuItemVisible(Menu menu, int itemId, boolean visible) {
200         MenuItem item = menu.findItem(itemId);
201         if (item != null) item.setVisible(visible);
202     }
203
204     private Path getSingleSelectedPath() {
205         ArrayList<Path> ids = mSelectionManager.getSelected(true);
206         Utils.assertTrue(ids.size() == 1);
207         return ids.get(0);
208     }
209
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);
215     }
216
217     private void onMenuClicked(int action, ProgressListener listener) {
218         onMenuClicked(action, listener, false, true);
219     }
220
221     public void onMenuClicked(int action, ProgressListener listener,
222             boolean waitOnStop, boolean showDialog) {
223         int title;
224         switch (action) {
225             case R.id.action_select_all:
226                 if (mSelectionManager.inSelectAllMode()) {
227                     mSelectionManager.deSelectAll();
228                 } else {
229                     mSelectionManager.selectAll();
230                 }
231                 return;
232             case R.id.action_crop: {
233                 Intent intent = getIntentBySingleSelectedPath(FilterShowActivity.CROP_ACTION)
234                         .setClass((Activity) mActivity, FilterShowActivity.class);
235                 ((Activity) mActivity).startActivity(intent);
236                 return;
237             }
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));
242                 return;
243             }
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)));
251                 return;
252             }
253             case R.id.action_delete:
254                 title = R.string.delete;
255                 break;
256             case R.id.action_rotate_cw:
257                 title = R.string.rotate_right;
258                 break;
259             case R.id.action_rotate_ccw:
260                 title = R.string.rotate_left;
261                 break;
262             case R.id.action_show_on_map:
263                 title = R.string.show_on_map;
264                 break;
265             case R.id.action_import:
266                 title = R.string.Import;
267                 break;
268             default:
269                 return;
270         }
271         startAction(action, title, listener, waitOnStop, showDialog);
272     }
273
274     private class ConfirmDialogListener implements OnClickListener, OnCancelListener {
275         private final int mActionId;
276         private final ProgressListener mListener;
277
278         public ConfirmDialogListener(int actionId, ProgressListener listener) {
279             mActionId = actionId;
280             mListener = listener;
281         }
282
283         @Override
284         public void onClick(DialogInterface dialog, int which) {
285             if (which == DialogInterface.BUTTON_POSITIVE) {
286                 if (mListener != null) {
287                     mListener.onConfirmDialogDismissed(true);
288                 }
289                 onMenuClicked(mActionId, mListener);
290             } else {
291                 if (mListener != null) {
292                     mListener.onConfirmDialogDismissed(false);
293                 }
294             }
295         }
296
297         @Override
298         public void onCancel(DialogInterface dialog) {
299             if (mListener != null) {
300                 mListener.onConfirmDialogDismissed(false);
301             }
302         }
303     }
304
305     public void onMenuClicked(MenuItem menuItem, String confirmMsg,
306             final ProgressListener listener) {
307         final int action = menuItem.getItemId();
308
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)
317                     .create().show();
318         } else {
319             onMenuClicked(action, listener);
320         }
321     }
322
323     public void startAction(int action, int title, ProgressListener listener) {
324         startAction(action, title, listener, false, true);
325     }
326
327     public void startAction(int action, int title, ProgressListener listener,
328             boolean waitOnStop, boolean showDialog) {
329         ArrayList<Path> ids = mSelectionManager.getSelected(false);
330         stopTaskAndDismissDialog();
331
332         Activity activity = mActivity;
333         mDialog = createProgressDialog(activity, title, ids.size());
334         if (showDialog) {
335             mDialog.show();
336         }
337         MediaOperation operation = new MediaOperation(action, ids, listener);
338         mTask = mActivity.getThreadPool().submit(operation, null);
339         mWaitOnStop = waitOnStop;
340     }
341
342     public static String getMimeType(int type) {
343         switch (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;
349         }
350     }
351
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();
357
358         switch (cmd) {
359             case R.id.action_delete:
360                 manager.delete(path);
361                 break;
362             case R.id.action_rotate_cw:
363                 manager.rotate(path, 90);
364                 break;
365             case R.id.action_rotate_ccw:
366                 manager.rotate(path, -90);
367                 break;
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;
373                 } else {
374                     cacheFlag = MediaObject.CACHE_FLAG_FULL;
375                 }
376                 obj.cache(cacheFlag);
377                 break;
378             }
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]);
385                 }
386                 break;
387             }
388             case R.id.action_import: {
389                 MediaObject obj = manager.getMediaObject(path);
390                 result = obj.Import();
391                 break;
392             }
393             default:
394                 throw new AssertionError();
395         }
396         Log.v(TAG, "It takes " + (System.currentTimeMillis() - startTime) +
397                 " ms to execute cmd for " + path);
398         return result;
399     }
400
401     private class MediaOperation implements Job<Void> {
402         private final ArrayList<Path> mItems;
403         private final int mOperation;
404         private final ProgressListener mListener;
405
406         public MediaOperation(int operation, ArrayList<Path> items,
407                 ProgressListener listener) {
408             mOperation = operation;
409             mItems = items;
410             mListener = listener;
411         }
412
413         @Override
414         public Void run(JobContext jc) {
415             int index = 0;
416             DataManager manager = mActivity.getDataManager();
417             int result = EXECUTION_RESULT_SUCCESS;
418             try {
419                 onProgressStart(mListener);
420                 for (Path id : mItems) {
421                     if (jc.isCancelled()) {
422                         result = EXECUTION_RESULT_CANCEL;
423                         break;
424                     }
425                     if (!execute(manager, jc, mOperation, id)) {
426                         result = EXECUTION_RESULT_FAIL;
427                     }
428                     onProgressUpdate(index++, mListener);
429                 }
430             } catch (Throwable th) {
431                 Log.e(TAG, "failed to execute operation " + mOperation
432                         + " : " + th);
433             } finally {
434                onProgressComplete(result, mListener);
435             }
436             return null;
437         }
438     }
439 }