blob: 65b82725893515a141da4daedaa40185ed2e6952 [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;
Owen Line594b192009-08-13 18:04:45 +080032
Romain Guye480f772010-03-10 12:26:30 -080033import com.android.camera.R;
Chih-Chung Chang3889cf32010-03-05 21:33:49 -080034
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -070035import java.io.Closeable;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080036
Ray Chend5142de2009-05-07 15:24:48 +080037/**
38 * A utility class to handle various kinds of menu operations.
39 */
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080040public class MenuHelper {
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -070041 private static final String TAG = "MenuHelper";
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080042
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -070043 public static final int INCLUDE_ALL = 0xFFFFFFFF;
44 public static final int INCLUDE_VIEWPLAY_MENU = (1 << 0);
45 public static final int INCLUDE_SHARE_MENU = (1 << 1);
46 public static final int INCLUDE_SET_MENU = (1 << 2);
47 public static final int INCLUDE_CROP_MENU = (1 << 3);
48 public static final int INCLUDE_DELETE_MENU = (1 << 4);
49 public static final int INCLUDE_ROTATE_MENU = (1 << 5);
50 public static final int INCLUDE_DETAILS_MENU = (1 << 6);
Ray Chen0a5a78c2009-04-24 14:13:01 +080051 public static final int INCLUDE_SHOWMAP_MENU = (1 << 7);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080052
Chih-Chung Changd1890832009-09-08 13:32:52 +080053 public static final int MENU_IMAGE_SHARE = 1;
54 public static final int MENU_IMAGE_SHOWMAP = 2;
55
56 public static final int POSITION_SWITCH_CAMERA_MODE = 1;
57 public static final int POSITION_GOTO_GALLERY = 2;
58 public static final int POSITION_VIEWPLAY = 3;
59 public static final int POSITION_CAPTURE_PICTURE = 4;
60 public static final int POSITION_CAPTURE_VIDEO = 5;
61 public static final int POSITION_IMAGE_SHARE = 6;
62 public static final int POSITION_IMAGE_ROTATE = 7;
63 public static final int POSITION_IMAGE_TOSS = 8;
64 public static final int POSITION_IMAGE_CROP = 9;
65 public static final int POSITION_IMAGE_SET = 10;
66 public static final int POSITION_DETAILS = 11;
67 public static final int POSITION_SHOWMAP = 12;
68 public static final int POSITION_SLIDESHOW = 13;
69 public static final int POSITION_MULTISELECT = 14;
70 public static final int POSITION_CAMERA_SETTING = 15;
71 public static final int POSITION_GALLERY_SETTING = 16;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080072
73 public static final int NO_STORAGE_ERROR = -1;
74 public static final int CANNOT_STAT_ERROR = -2;
Ray Chen0f0af522009-04-22 13:26:07 +080075 public static final String EMPTY_STRING = "";
Ray Chend5142de2009-05-07 15:24:48 +080076 public static final String JPEG_MIME_TYPE = "image/jpeg";
Ray Chen0a5a78c2009-04-24 14:13:01 +080077 // valid range is -180f to +180f
78 public static final float INVALID_LATLNG = 255f;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080079
80 /** Activity result code used to report crop results.
81 */
82 public static final int RESULT_COMMON_MENU_CROP = 490;
83
Wei-Ta Chenb6be8b62010-02-24 11:34:22 +080084 private static final int NO_ANIMATION = 0;
85
Chih-Chung Chang272c3fd2009-04-08 05:14:37 -070086 public static void closeSilently(Closeable c) {
87 if (c != null) {
88 try {
89 c.close();
90 } catch (Throwable e) {
91 // ignore
92 }
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080093 }
94 }
95
Chih-Chung Chang0e48fe62009-06-26 19:23:56 +080096 public static void confirmAction(Context context, String title,
97 String message, final Runnable action) {
98 OnClickListener listener = new OnClickListener() {
99 public void onClick(DialogInterface dialog, int which) {
100 switch (which) {
101 case DialogInterface.BUTTON_POSITIVE:
102 if (action != null) action.run();
103 }
104 }
105 };
106 new AlertDialog.Builder(context)
107 .setIcon(android.R.drawable.ic_dialog_alert)
108 .setTitle(title)
109 .setMessage(message)
110 .setPositiveButton(android.R.string.ok, listener)
111 .setNegativeButton(android.R.string.cancel, listener)
112 .create()
113 .show();
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800114 }
115
Owen Lin3283e042009-06-26 11:59:58 -0700116 private static void startCameraActivity(Activity activity, String action) {
Chih-Chung Chang272c3fd2009-04-08 05:14:37 -0700117 Intent intent = new Intent(action);
118 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
119 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
Chih-Chung Changcd65be32009-06-09 13:51:29 +0800120
121 // Keep the camera instance for a while.
122 // This avoids re-opening the camera and saves time.
123 CameraHolder.instance().keep();
124
Chih-Chung Chang272c3fd2009-04-08 05:14:37 -0700125 activity.startActivity(intent);
Wei-Ta Chenb6be8b62010-02-24 11:34:22 +0800126 activity.overridePendingTransition(NO_ANIMATION, NO_ANIMATION);
Owen Lin059daa32009-05-18 15:31:17 -0700127 }
128
129 public static void gotoVideoMode(Activity activity) {
Owen Lin3283e042009-06-26 11:59:58 -0700130 startCameraActivity(activity, MediaStore.INTENT_ACTION_VIDEO_CAMERA);
Owen Lin059daa32009-05-18 15:31:17 -0700131 }
132
Owen Lin3283e042009-06-26 11:59:58 -0700133 public static void gotoCameraMode(Activity activity) {
134 startCameraActivity(
135 activity, MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800136 }
137
Owen Lin3283e042009-06-26 11:59:58 -0700138 public static void gotoCameraImageGallery(Activity activity) {
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700139 gotoGallery(activity, R.string.gallery_camera_bucket_name,
140 ImageManager.INCLUDE_IMAGES);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800141 }
142
Owen Lin3283e042009-06-26 11:59:58 -0700143 public static void gotoCameraVideoGallery(Activity activity) {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800144 gotoGallery(activity, R.string.gallery_camera_videos_bucket_name,
145 ImageManager.INCLUDE_VIDEOS);
146 }
147
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700148 private static void gotoGallery(Activity activity, int windowTitleId,
149 int mediaTypes) {
Ray Chendf926b62009-11-16 16:36:21 -0800150 Uri target = Images.Media.EXTERNAL_CONTENT_URI.buildUpon()
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700151 .appendQueryParameter("bucketId",
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800152 ImageManager.CAMERA_IMAGE_BUCKET_ID).build();
153 Intent intent = new Intent(Intent.ACTION_VIEW, target);
The Android Open Source Projecte3f45162009-03-11 12:11:58 -0700154 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800155 intent.putExtra("windowTitle", activity.getString(windowTitleId));
156 intent.putExtra("mediaTypes", mediaTypes);
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700157
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800158 try {
159 activity.startActivity(intent);
160 } catch (ActivityNotFoundException e) {
161 Log.e(TAG, "Could not start gallery activity", e);
162 }
163 }
164
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800165 public static int calculatePicturesRemaining() {
166 try {
167 if (!ImageManager.hasStorage()) {
168 return NO_STORAGE_ERROR;
169 } else {
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700170 String storageDirectory =
171 Environment.getExternalStorageDirectory().toString();
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800172 StatFs stat = new StatFs(storageDirectory);
Chih-Chung Chang05d20c02009-11-04 16:21:57 +0800173 final int PICTURE_BYTES = 1500000;
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700174 float remaining = ((float) stat.getAvailableBlocks()
Chih-Chung Chang05d20c02009-11-04 16:21:57 +0800175 * (float) stat.getBlockSize()) / PICTURE_BYTES;
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700176 return (int) remaining;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800177 }
178 } catch (Exception ex) {
179 // if we can't stat the filesystem then we don't know how many
180 // pictures are remaining. it might be zero but just leave it
181 // blank since we really don't know.
182 return CANNOT_STAT_ERROR;
183 }
184 }
185}