blob: 17755f9fd47c7d505bb57da13f365d4b3bc39937 [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;
Chih-Chung Changac9d0a12010-06-15 16:41:33 +080061 public static final int POSITION_SWITCH_CAMERA_ID = 3;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080062
63 public static final int NO_STORAGE_ERROR = -1;
64 public static final int CANNOT_STAT_ERROR = -2;
Ray Chen0f0af522009-04-22 13:26:07 +080065 public static final String EMPTY_STRING = "";
Ray Chend5142de2009-05-07 15:24:48 +080066 public static final String JPEG_MIME_TYPE = "image/jpeg";
Ray Chen0a5a78c2009-04-24 14:13:01 +080067 // valid range is -180f to +180f
68 public static final float INVALID_LATLNG = 255f;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080069
70 /** Activity result code used to report crop results.
71 */
72 public static final int RESULT_COMMON_MENU_CROP = 490;
73
Wei-Ta Chenb6be8b62010-02-24 11:34:22 +080074 private static final int NO_ANIMATION = 0;
75
Chih-Chung Chang272c3fd2009-04-08 05:14:37 -070076 public static void closeSilently(Closeable c) {
77 if (c != null) {
78 try {
79 c.close();
80 } catch (Throwable e) {
81 // ignore
82 }
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080083 }
84 }
85
Chih-Chung Chang0e48fe62009-06-26 19:23:56 +080086 public static void confirmAction(Context context, String title,
87 String message, final Runnable action) {
88 OnClickListener listener = new OnClickListener() {
89 public void onClick(DialogInterface dialog, int which) {
90 switch (which) {
91 case DialogInterface.BUTTON_POSITIVE:
92 if (action != null) action.run();
93 }
94 }
95 };
96 new AlertDialog.Builder(context)
97 .setIcon(android.R.drawable.ic_dialog_alert)
98 .setTitle(title)
99 .setMessage(message)
100 .setPositiveButton(android.R.string.ok, listener)
101 .setNegativeButton(android.R.string.cancel, listener)
102 .create()
103 .show();
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800104 }
105
Chih-Chung Chang9ae7d022010-03-12 16:37:35 -0800106 static void addSwitchModeMenuItem(Menu menu, boolean switchToVideo,
107 final Runnable r) {
108 int labelId = switchToVideo
109 ? R.string.switch_to_video_lable
110 : R.string.switch_to_camera_lable;
111 int iconId = switchToVideo
112 ? R.drawable.ic_menu_camera_video_view
113 : android.R.drawable.ic_menu_camera;
114 MenuItem item = menu.add(Menu.NONE, Menu.NONE,
115 POSITION_SWITCH_CAMERA_MODE, labelId)
116 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
117 public boolean onMenuItemClick(MenuItem item) {
118 r.run();
119 return true;
120 }
121 });
122 item.setIcon(iconId);
123 }
124
Owen Lin3283e042009-06-26 11:59:58 -0700125 private static void startCameraActivity(Activity activity, String action) {
Chih-Chung Chang272c3fd2009-04-08 05:14:37 -0700126 Intent intent = new Intent(action);
127 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
128 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
Chih-Chung Changcd65be32009-06-09 13:51:29 +0800129
130 // Keep the camera instance for a while.
131 // This avoids re-opening the camera and saves time.
132 CameraHolder.instance().keep();
133
Chih-Chung Chang272c3fd2009-04-08 05:14:37 -0700134 activity.startActivity(intent);
Wei-Ta Chenb6be8b62010-02-24 11:34:22 +0800135 activity.overridePendingTransition(NO_ANIMATION, NO_ANIMATION);
Owen Lin059daa32009-05-18 15:31:17 -0700136 }
137
138 public static void gotoVideoMode(Activity activity) {
Owen Lin3283e042009-06-26 11:59:58 -0700139 startCameraActivity(activity, MediaStore.INTENT_ACTION_VIDEO_CAMERA);
Owen Lin059daa32009-05-18 15:31:17 -0700140 }
141
Owen Lin3283e042009-06-26 11:59:58 -0700142 public static void gotoCameraMode(Activity activity) {
143 startCameraActivity(
144 activity, MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800145 }
146
Owen Lin3283e042009-06-26 11:59:58 -0700147 public static void gotoCameraImageGallery(Activity activity) {
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700148 gotoGallery(activity, R.string.gallery_camera_bucket_name,
149 ImageManager.INCLUDE_IMAGES);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800150 }
151
Owen Lin3283e042009-06-26 11:59:58 -0700152 public static void gotoCameraVideoGallery(Activity activity) {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800153 gotoGallery(activity, R.string.gallery_camera_videos_bucket_name,
154 ImageManager.INCLUDE_VIDEOS);
155 }
156
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700157 private static void gotoGallery(Activity activity, int windowTitleId,
158 int mediaTypes) {
Ray Chendf926b62009-11-16 16:36:21 -0800159 Uri target = Images.Media.EXTERNAL_CONTENT_URI.buildUpon()
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700160 .appendQueryParameter("bucketId",
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800161 ImageManager.CAMERA_IMAGE_BUCKET_ID).build();
162 Intent intent = new Intent(Intent.ACTION_VIEW, target);
The Android Open Source Projecte3f45162009-03-11 12:11:58 -0700163 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800164 intent.putExtra("windowTitle", activity.getString(windowTitleId));
165 intent.putExtra("mediaTypes", mediaTypes);
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700166
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800167 try {
168 activity.startActivity(intent);
169 } catch (ActivityNotFoundException e) {
170 Log.e(TAG, "Could not start gallery activity", e);
171 }
172 }
173
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800174 public static int calculatePicturesRemaining() {
175 try {
176 if (!ImageManager.hasStorage()) {
177 return NO_STORAGE_ERROR;
178 } else {
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700179 String storageDirectory =
180 Environment.getExternalStorageDirectory().toString();
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800181 StatFs stat = new StatFs(storageDirectory);
Chih-Chung Chang05d20c02009-11-04 16:21:57 +0800182 final int PICTURE_BYTES = 1500000;
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700183 float remaining = ((float) stat.getAvailableBlocks()
Chih-Chung Chang05d20c02009-11-04 16:21:57 +0800184 * (float) stat.getBlockSize()) / PICTURE_BYTES;
Chih-Chung Chang9ae6df02009-04-08 01:51:45 -0700185 return (int) remaining;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800186 }
187 } catch (Exception ex) {
188 // if we can't stat the filesystem then we don't know how many
189 // pictures are remaining. it might be zero but just leave it
190 // blank since we really don't know.
Wu-cheng Li0ab73a82010-05-28 19:57:36 +0800191 Log.e(TAG, "Fail to access sdcard", ex);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800192 return CANNOT_STAT_ERROR;
193 }
194 }
195}