blob: a0f3449829a69707e297559877cb6c34dc535bcd [file] [log] [blame]
Owen Linf9a0a432011-08-17 22:07:43 +08001/*
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
17package com.android.gallery3d.ui;
18
Ray Chen0c1f2c82011-10-05 14:32:58 +080019import android.app.Activity;
Ray Chen67098d12012-04-05 17:25:43 +080020import android.app.AlertDialog;
Ray Chen0c1f2c82011-10-05 14:32:58 +080021import android.app.ProgressDialog;
22import android.content.Context;
Ray Chen67098d12012-04-05 17:25:43 +080023import android.content.DialogInterface;
24import android.content.DialogInterface.OnClickListener;
Ray Chen0c1f2c82011-10-05 14:32:58 +080025import android.content.Intent;
26import android.os.Handler;
27import android.os.Message;
28import android.view.Menu;
29import android.view.MenuItem;
30import android.widget.Toast;
31
Owen Linf9a0a432011-08-17 22:07:43 +080032import com.android.gallery3d.R;
33import com.android.gallery3d.app.CropImage;
34import com.android.gallery3d.app.GalleryActivity;
35import com.android.gallery3d.common.Utils;
36import com.android.gallery3d.data.DataManager;
37import com.android.gallery3d.data.MediaItem;
38import com.android.gallery3d.data.MediaObject;
39import com.android.gallery3d.data.Path;
40import com.android.gallery3d.util.Future;
41import com.android.gallery3d.util.GalleryUtils;
42import com.android.gallery3d.util.ThreadPool.Job;
43import com.android.gallery3d.util.ThreadPool.JobContext;
44
Owen Linf9a0a432011-08-17 22:07:43 +080045import java.util.ArrayList;
46
47public 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;
53 private static final int MSG_DO_SHARE = 3;
54
55 public static final int EXECUTION_RESULT_SUCCESS = 1;
56 public static final int EXECUTION_RESULT_FAIL = 2;
57 public static final int EXECUTION_RESULT_CANCEL = 3;
58
59 private ProgressDialog mDialog;
60 private Future<?> mTask;
61
62 private final GalleryActivity mActivity;
63 private final SelectionManager mSelectionManager;
64 private final Handler mHandler;
65
66 private static ProgressDialog showProgressDialog(
67 Context context, int titleId, int progressMax) {
68 ProgressDialog dialog = new ProgressDialog(context);
69 dialog.setTitle(titleId);
70 dialog.setMax(progressMax);
71 dialog.setCancelable(false);
72 dialog.setIndeterminate(false);
73 if (progressMax > 1) {
74 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
75 }
76 dialog.show();
77 return dialog;
78 }
79
80 public interface ProgressListener {
81 public void onProgressUpdate(int index);
82 public void onProgressComplete(int result);
83 }
84
85 public MenuExecutor(
86 GalleryActivity activity, SelectionManager selectionManager) {
87 mActivity = Utils.checkNotNull(activity);
88 mSelectionManager = Utils.checkNotNull(selectionManager);
89 mHandler = new SynchronizedHandler(mActivity.getGLRoot()) {
90 @Override
91 public void handleMessage(Message message) {
92 switch (message.what) {
93 case MSG_TASK_COMPLETE: {
Ray Chenb2b45182011-08-31 16:37:04 +080094 stopTaskAndDismissDialog();
Owen Linf9a0a432011-08-17 22:07:43 +080095 if (message.obj != null) {
96 ProgressListener listener = (ProgressListener) message.obj;
97 listener.onProgressComplete(message.arg1);
98 }
99 mSelectionManager.leaveSelectionMode();
100 break;
101 }
102 case MSG_TASK_UPDATE: {
103 if (mDialog != null) mDialog.setProgress(message.arg1);
104 if (message.obj != null) {
105 ProgressListener listener = (ProgressListener) message.obj;
106 listener.onProgressUpdate(message.arg1);
107 }
108 break;
109 }
110 case MSG_DO_SHARE: {
111 ((Activity) mActivity).startActivity((Intent) message.obj);
112 break;
113 }
114 }
115 }
116 };
117 }
118
Ray Chenb2b45182011-08-31 16:37:04 +0800119 private void stopTaskAndDismissDialog() {
Owen Linf9a0a432011-08-17 22:07:43 +0800120 if (mTask != null) {
121 mTask.cancel();
122 mTask.waitDone();
123 mDialog.dismiss();
124 mDialog = null;
125 mTask = null;
126 }
127 }
128
Ray Chenb2b45182011-08-31 16:37:04 +0800129 public void pause() {
130 stopTaskAndDismissDialog();
131 }
132
Owen Linf9a0a432011-08-17 22:07:43 +0800133 private void onProgressUpdate(int index, ProgressListener listener) {
134 mHandler.sendMessage(
135 mHandler.obtainMessage(MSG_TASK_UPDATE, index, 0, listener));
136 }
137
138 private void onProgressComplete(int result, ProgressListener listener) {
139 mHandler.sendMessage(mHandler.obtainMessage(MSG_TASK_COMPLETE, result, 0, listener));
140 }
141
Owen Linf9a0a432011-08-17 22:07:43 +0800142 private static void setMenuItemVisibility(
143 Menu menu, int id, boolean visibility) {
144 MenuItem item = menu.findItem(id);
145 if (item != null) item.setVisible(visibility);
146 }
147
148 public static void updateMenuOperation(Menu menu, int supported) {
149 boolean supportDelete = (supported & MediaObject.SUPPORT_DELETE) != 0;
150 boolean supportRotate = (supported & MediaObject.SUPPORT_ROTATE) != 0;
151 boolean supportCrop = (supported & MediaObject.SUPPORT_CROP) != 0;
152 boolean supportShare = (supported & MediaObject.SUPPORT_SHARE) != 0;
153 boolean supportSetAs = (supported & MediaObject.SUPPORT_SETAS) != 0;
154 boolean supportShowOnMap = (supported & MediaObject.SUPPORT_SHOW_ON_MAP) != 0;
155 boolean supportCache = (supported & MediaObject.SUPPORT_CACHE) != 0;
156 boolean supportEdit = (supported & MediaObject.SUPPORT_EDIT) != 0;
157 boolean supportInfo = (supported & MediaObject.SUPPORT_INFO) != 0;
158 boolean supportImport = (supported & MediaObject.SUPPORT_IMPORT) != 0;
159
160 setMenuItemVisibility(menu, R.id.action_delete, supportDelete);
161 setMenuItemVisibility(menu, R.id.action_rotate_ccw, supportRotate);
162 setMenuItemVisibility(menu, R.id.action_rotate_cw, supportRotate);
163 setMenuItemVisibility(menu, R.id.action_crop, supportCrop);
164 setMenuItemVisibility(menu, R.id.action_share, supportShare);
165 setMenuItemVisibility(menu, R.id.action_setas, supportSetAs);
166 setMenuItemVisibility(menu, R.id.action_show_on_map, supportShowOnMap);
167 setMenuItemVisibility(menu, R.id.action_edit, supportEdit);
168 setMenuItemVisibility(menu, R.id.action_details, supportInfo);
169 setMenuItemVisibility(menu, R.id.action_import, supportImport);
170 }
171
172 private Path getSingleSelectedPath() {
173 ArrayList<Path> ids = mSelectionManager.getSelected(true);
174 Utils.assertTrue(ids.size() == 1);
175 return ids.get(0);
176 }
177
Ray Chen67098d12012-04-05 17:25:43 +0800178 private void onMenuClicked(int action, ProgressListener listener) {
Owen Linf9a0a432011-08-17 22:07:43 +0800179 int title;
180 DataManager manager = mActivity.getDataManager();
Owen Linf9a0a432011-08-17 22:07:43 +0800181 switch (action) {
182 case R.id.action_select_all:
183 if (mSelectionManager.inSelectAllMode()) {
184 mSelectionManager.deSelectAll();
185 } else {
186 mSelectionManager.selectAll();
187 }
Owen Linf9a0a432011-08-17 22:07:43 +0800188 case R.id.action_crop: {
189 Path path = getSingleSelectedPath();
190 String mimeType = getMimeType(manager.getMediaType(path));
191 Intent intent = new Intent(CropImage.ACTION_CROP)
192 .setDataAndType(manager.getContentUri(path), mimeType);
193 ((Activity) mActivity).startActivity(intent);
Owen Linf9a0a432011-08-17 22:07:43 +0800194 }
195 case R.id.action_setas: {
196 Path path = getSingleSelectedPath();
197 int type = manager.getMediaType(path);
198 Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
199 String mimeType = getMimeType(type);
200 intent.setDataAndType(manager.getContentUri(path), mimeType);
201 intent.putExtra("mimeType", mimeType);
202 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
203 Activity activity = (Activity) mActivity;
204 activity.startActivity(Intent.createChooser(
205 intent, activity.getString(R.string.set_as)));
Owen Linf9a0a432011-08-17 22:07:43 +0800206 }
Ray Chen67098d12012-04-05 17:25:43 +0800207 case R.id.action_delete:
Owen Linf9a0a432011-08-17 22:07:43 +0800208 title = R.string.delete;
209 break;
210 case R.id.action_rotate_cw:
211 title = R.string.rotate_right;
212 break;
213 case R.id.action_rotate_ccw:
214 title = R.string.rotate_left;
215 break;
216 case R.id.action_show_on_map:
217 title = R.string.show_on_map;
218 break;
219 case R.id.action_edit:
220 title = R.string.edit;
221 break;
222 case R.id.action_import:
223 title = R.string.Import;
224 break;
225 default:
Ray Chen67098d12012-04-05 17:25:43 +0800226 return;
Owen Linf9a0a432011-08-17 22:07:43 +0800227 }
228 startAction(action, title, listener);
Ray Chen67098d12012-04-05 17:25:43 +0800229 }
230
231 public void onMenuClicked(MenuItem menuItem, boolean needsConfirm,
232 final ProgressListener listener) {
233 final int action = menuItem.getItemId();
234
235 if (needsConfirm) {
236 new AlertDialog.Builder(mActivity.getAndroidContext())
237 .setMessage(R.string.confirm_action)
238 .setPositiveButton(R.string.confirm, new OnClickListener() {
239 public void onClick(DialogInterface dialog, int which) {
240 onMenuClicked(action, listener);
241 }
242 })
243 .setNegativeButton(R.string.cancel, null).create().show();
244 } else {
245 onMenuClicked(action, listener);
246 }
Owen Linf9a0a432011-08-17 22:07:43 +0800247 }
248
249 public void startAction(int action, int title, ProgressListener listener) {
250 ArrayList<Path> ids = mSelectionManager.getSelected(false);
Ray Chenb2b45182011-08-31 16:37:04 +0800251 stopTaskAndDismissDialog();
Owen Linf9a0a432011-08-17 22:07:43 +0800252
253 Activity activity = (Activity) mActivity;
254 mDialog = showProgressDialog(activity, title, ids.size());
255 MediaOperation operation = new MediaOperation(action, ids, listener);
256 mTask = mActivity.getThreadPool().submit(operation, null);
257 }
258
259 public static String getMimeType(int type) {
260 switch (type) {
261 case MediaObject.MEDIA_TYPE_IMAGE :
262 return "image/*";
263 case MediaObject.MEDIA_TYPE_VIDEO :
264 return "video/*";
265 default: return "*/*";
266 }
267 }
268
269 private boolean execute(
270 DataManager manager, JobContext jc, int cmd, Path path) {
271 boolean result = true;
Ray Chenb2b45182011-08-31 16:37:04 +0800272 Log.v(TAG, "Execute cmd: " + cmd + " for " + path);
273 long startTime = System.currentTimeMillis();
274
Owen Linf9a0a432011-08-17 22:07:43 +0800275 switch (cmd) {
Ray Chen67098d12012-04-05 17:25:43 +0800276 case R.id.action_delete:
Owen Linf9a0a432011-08-17 22:07:43 +0800277 manager.delete(path);
278 break;
279 case R.id.action_rotate_cw:
280 manager.rotate(path, 90);
281 break;
282 case R.id.action_rotate_ccw:
283 manager.rotate(path, -90);
284 break;
285 case R.id.action_toggle_full_caching: {
286 MediaObject obj = manager.getMediaObject(path);
287 int cacheFlag = obj.getCacheFlag();
288 if (cacheFlag == MediaObject.CACHE_FLAG_FULL) {
289 cacheFlag = MediaObject.CACHE_FLAG_SCREENNAIL;
290 } else {
291 cacheFlag = MediaObject.CACHE_FLAG_FULL;
292 }
293 obj.cache(cacheFlag);
294 break;
295 }
296 case R.id.action_show_on_map: {
297 MediaItem item = (MediaItem) manager.getMediaObject(path);
298 double latlng[] = new double[2];
299 item.getLatLong(latlng);
300 if (GalleryUtils.isValidLocation(latlng[0], latlng[1])) {
301 GalleryUtils.showOnMap((Context) mActivity, latlng[0], latlng[1]);
302 }
303 break;
304 }
305 case R.id.action_import: {
306 MediaObject obj = manager.getMediaObject(path);
307 result = obj.Import();
308 break;
309 }
310 case R.id.action_edit: {
311 Activity activity = (Activity) mActivity;
312 MediaItem item = (MediaItem) manager.getMediaObject(path);
313 try {
314 activity.startActivity(Intent.createChooser(
315 new Intent(Intent.ACTION_EDIT)
Owen Lin1801ef02011-08-26 22:34:13 +0800316 .setDataAndType(item.getContentUri(), item.getMimeType())
Owen Linf9a0a432011-08-17 22:07:43 +0800317 .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION),
318 null));
319 } catch (Throwable t) {
320 Log.w(TAG, "failed to start edit activity: ", t);
321 Toast.makeText(activity,
322 activity.getString(R.string.activity_not_found),
323 Toast.LENGTH_SHORT).show();
324 }
325 break;
326 }
327 default:
328 throw new AssertionError();
329 }
Ray Chenb2b45182011-08-31 16:37:04 +0800330 Log.v(TAG, "It takes " + (System.currentTimeMillis() - startTime) +
331 " ms to execute cmd for " + path);
Owen Linf9a0a432011-08-17 22:07:43 +0800332 return result;
333 }
334
335 private class MediaOperation implements Job<Void> {
336 private final ArrayList<Path> mItems;
337 private final int mOperation;
338 private final ProgressListener mListener;
339
340 public MediaOperation(int operation, ArrayList<Path> items, ProgressListener listener) {
341 mOperation = operation;
342 mItems = items;
343 mListener = listener;
344 }
345
346 public Void run(JobContext jc) {
347 int index = 0;
348 DataManager manager = mActivity.getDataManager();
349 int result = EXECUTION_RESULT_SUCCESS;
Ray Chenb2b45182011-08-31 16:37:04 +0800350 try {
351 for (Path id : mItems) {
352 if (jc.isCancelled()) {
353 result = EXECUTION_RESULT_CANCEL;
354 break;
355 }
356 if (!execute(manager, jc, mOperation, id)) {
357 result = EXECUTION_RESULT_FAIL;
358 }
359 onProgressUpdate(index++, mListener);
Owen Linf9a0a432011-08-17 22:07:43 +0800360 }
Ray Chenb2b45182011-08-31 16:37:04 +0800361 } catch (Throwable th) {
362 Log.e(TAG, "failed to execute operation " + mOperation
363 + " : " + th);
364 } finally {
365 onProgressComplete(result, mListener);
Owen Linf9a0a432011-08-17 22:07:43 +0800366 }
Owen Linf9a0a432011-08-17 22:07:43 +0800367 return null;
368 }
369 }
370}
371