blob: 1c8f0fafc9eaeccad0159d136e90d4e7b079e53d [file] [log] [blame]
The Android Open Source Projectb64d3452009-03-03 19:32:20 -08001/*
2 * Copyright (C) 2008 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.camera;
18
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080019import android.app.Activity;
20import android.app.AlertDialog;
21import android.content.ActivityNotFoundException;
Owen Lin6795ff12009-06-09 13:39:00 -070022import android.content.Context;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080023import android.content.DialogInterface;
24import android.content.Intent;
Chih-Chung Chang0e48fe62009-06-26 19:23:56 +080025import android.content.DialogInterface.OnClickListener;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080026import android.net.Uri;
27import android.os.Environment;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080028import android.os.StatFs;
29import android.provider.MediaStore;
30import android.provider.MediaStore.Images;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080031import android.util.Log;
Chih-Chung Chang9ae7d022010-03-12 16:37:35 -080032import android.view.Menu;
33import android.view.MenuItem;
34import android.view.MenuItem.OnMenuItemClickListener;
Owen Line594b192009-08-13 18:04:45 +080035
Romain Guye480f772010-03-10 12:26:30 -080036import com.android.camera.R;
Chih-Chung Chang3889cf32010-03-05 21:33:49 -080037
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -070038import java.io.Closeable;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080039
Ray Chend5142de2009-05-07 15:24:48 +080040/**
41 * A utility class to handle various kinds of menu operations.
42 */
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080043public class MenuHelper {
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -070044 private static final String TAG = "MenuHelper";
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080045
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -070046 public static final int INCLUDE_ALL = 0xFFFFFFFF;
47 public static final int INCLUDE_VIEWPLAY_MENU = (1 << 0);
48 public static final int INCLUDE_SHARE_MENU = (1 << 1);
49 public static final int INCLUDE_SET_MENU = (1 << 2);
50 public static final int INCLUDE_CROP_MENU = (1 << 3);
51 public static final int INCLUDE_DELETE_MENU = (1 << 4);
52 public static final int INCLUDE_ROTATE_MENU = (1 << 5);
53 public static final int INCLUDE_DETAILS_MENU = (1 << 6);
Ray Chen0a5a78c2009-04-24 14:13:01 +080054 public static final int INCLUDE_SHOWMAP_MENU = (1 << 7);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080055
Chih-Chung Changd1890832009-09-08 13:32:52 +080056 public static final int MENU_IMAGE_SHARE = 1;
57 public static final int MENU_IMAGE_SHOWMAP = 2;
58
59 public static final int POSITION_SWITCH_CAMERA_MODE = 1;
60 public static final int POSITION_GOTO_GALLERY = 2;
61 public static final int POSITION_VIEWPLAY = 3;
62 public static final int POSITION_CAPTURE_PICTURE = 4;
63 public static final int POSITION_CAPTURE_VIDEO = 5;
64 public static final int POSITION_IMAGE_SHARE = 6;
65 public static final int POSITION_IMAGE_ROTATE = 7;
66 public static final int POSITION_IMAGE_TOSS = 8;
67 public static final int POSITION_IMAGE_CROP = 9;
68 public static final int POSITION_IMAGE_SET = 10;
69 public static final int POSITION_DETAILS = 11;
70 public static final int POSITION_SHOWMAP = 12;
71 public static final int POSITION_SLIDESHOW = 13;
72 public static final int POSITION_MULTISELECT = 14;
73 public static final int POSITION_CAMERA_SETTING = 15;
74 public static final int POSITION_GALLERY_SETTING = 16;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080075
76 public static final int NO_STORAGE_ERROR = -1;
77 public static final int CANNOT_STAT_ERROR = -2;
Ray Chen0f0af522009-04-22 13:26:07 +080078 public static final String EMPTY_STRING = "";
Ray Chend5142de2009-05-07 15:24:48 +080079 public static final String JPEG_MIME_TYPE = "image/jpeg";
Ray Chen0a5a78c2009-04-24 14:13:01 +080080 // valid range is -180f to +180f
81 public static final float INVALID_LATLNG = 255f;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080082
83 /** Activity result code used to report crop results.
84 */
85 public static final int RESULT_COMMON_MENU_CROP = 490;
86
Wei-Ta Chenb6be8b62010-02-24 11:34:22 +080087 private static final int NO_ANIMATION = 0;
88
Chih-Chung Chang272c3fd2009-04-08 05:14:37 -070089 public static void closeSilently(Closeable c) {
90 if (c != null) {
91 try {
92 c.close();
93 } catch (Throwable e) {
94 // ignore
95 }
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080096 }
97 }
98
Chih-Chung Chang0e48fe62009-06-26 19:23:56 +080099 public static void confirmAction(Context context, String title,
100 String message, final Runnable action) {
101 OnClickListener listener = new OnClickListener() {
102 public void onClick(DialogInterface dialog, int which) {
103 switch (which) {
104 case DialogInterface.BUTTON_POSITIVE:
105 if (action != null) action.run();
106 }
107 }
108 };
109 new AlertDialog.Builder(context)
110 .setIcon(android.R.drawable.ic_dialog_alert)
111 .setTitle(title)
112 .setMessage(message)
113 .setPositiveButton(android.R.string.ok, listener)
114 .setNegativeButton(android.R.string.cancel, listener)
115 .create()
116 .show();
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800117 }
118
Chih-Chung Chang9ae7d022010-03-12 16:37:35 -0800119 static void addSwitchModeMenuItem(Menu menu, boolean switchToVideo,
120 final Runnable r) {
121 int labelId = switchToVideo
122 ? R.string.switch_to_video_lable
123 : R.string.switch_to_camera_lable;
124 int iconId = switchToVideo
125 ? R.drawable.ic_menu_camera_video_view
126 : android.R.drawable.ic_menu_camera;
127 MenuItem item = menu.add(Menu.NONE, Menu.NONE,
128 POSITION_SWITCH_CAMERA_MODE, labelId)
129 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
130 public boolean onMenuItemClick(MenuItem item) {
131 r.run();
132 return true;
133 }
134 });
135 item.setIcon(iconId);
136 }
137
Owen Lin3283e042009-06-26 11:59:58 -0700138 private static void startCameraActivity(Activity activity, String action) {
Chih-Chung Chang272c3fd2009-04-08 05:14:37 -0700139 Intent intent = new Intent(action);
140 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
141 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
Chih-Chung Changcd65be32009-06-09 13:51:29 +0800142
143 // Keep the camera instance for a while.
144 // This avoids re-opening the camera and saves time.
145 CameraHolder.instance().keep();
146
Chih-Chung Chang272c3fd2009-04-08 05:14:37 -0700147 activity.startActivity(intent);
Wei-Ta Chenb6be8b62010-02-24 11:34:22 +0800148 activity.overridePendingTransition(NO_ANIMATION, NO_ANIMATION);
Owen Lin059daa32009-05-18 15:31:17 -0700149 }
150
151 public static void gotoVideoMode(Activity activity) {
Owen Lin3283e042009-06-26 11:59:58 -0700152 startCameraActivity(activity, MediaStore.INTENT_ACTION_VIDEO_CAMERA);
Owen Lin059daa32009-05-18 15:31:17 -0700153 }
154
Owen Lin3283e042009-06-26 11:59:58 -0700155 public static void gotoCameraMode(Activity activity) {
156 startCameraActivity(
157 activity, MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800158 }
159
Owen Lin3283e042009-06-26 11:59:58 -0700160 public static void gotoCameraImageGallery(Activity activity) {
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700161 gotoGallery(activity, R.string.gallery_camera_bucket_name,
162 ImageManager.INCLUDE_IMAGES);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800163 }
164
Owen Lin3283e042009-06-26 11:59:58 -0700165 public static void gotoCameraVideoGallery(Activity activity) {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800166 gotoGallery(activity, R.string.gallery_camera_videos_bucket_name,
167 ImageManager.INCLUDE_VIDEOS);
168 }
169
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700170 private static void gotoGallery(Activity activity, int windowTitleId,
171 int mediaTypes) {
Ray Chendf926b62009-11-16 16:36:21 -0800172 Uri target = Images.Media.EXTERNAL_CONTENT_URI.buildUpon()
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700173 .appendQueryParameter("bucketId",
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800174 ImageManager.CAMERA_IMAGE_BUCKET_ID).build();
175 Intent intent = new Intent(Intent.ACTION_VIEW, target);
The Android Open Source Projecte3f45162009-03-11 12:11:58 -0700176 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800177 intent.putExtra("windowTitle", activity.getString(windowTitleId));
178 intent.putExtra("mediaTypes", mediaTypes);
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700179
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800180 try {
181 activity.startActivity(intent);
182 } catch (ActivityNotFoundException e) {
183 Log.e(TAG, "Could not start gallery activity", e);
184 }
185 }
186
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800187 public static int calculatePicturesRemaining() {
188 try {
189 if (!ImageManager.hasStorage()) {
190 return NO_STORAGE_ERROR;
191 } else {
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700192 String storageDirectory =
193 Environment.getExternalStorageDirectory().toString();
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800194 StatFs stat = new StatFs(storageDirectory);
Chih-Chung Chang05d20c02009-11-04 16:21:57 +0800195 final int PICTURE_BYTES = 1500000;
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700196 float remaining = ((float) stat.getAvailableBlocks()
Chih-Chung Chang05d20c02009-11-04 16:21:57 +0800197 * (float) stat.getBlockSize()) / PICTURE_BYTES;
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700198 return (int) remaining;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800199 }
200 } catch (Exception ex) {
201 // if we can't stat the filesystem then we don't know how many
202 // pictures are remaining. it might be zero but just leave it
203 // blank since we really don't know.
204 return CANNOT_STAT_ERROR;
205 }
206 }
207}