The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.camera; |
| 18 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.content.ActivityNotFoundException; |
Owen Lin | 6795ff1 | 2009-06-09 13:39:00 -0700 | [diff] [blame] | 22 | import android.content.Context; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 23 | import android.content.DialogInterface; |
| 24 | import android.content.Intent; |
Chih-Chung Chang | 0e48fe6 | 2009-06-26 19:23:56 +0800 | [diff] [blame] | 25 | import android.content.DialogInterface.OnClickListener; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 26 | import android.net.Uri; |
| 27 | import android.os.Environment; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 28 | import android.os.StatFs; |
| 29 | import android.provider.MediaStore; |
| 30 | import android.provider.MediaStore.Images; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 31 | import android.util.Log; |
Chih-Chung Chang | 9ae7d02 | 2010-03-12 16:37:35 -0800 | [diff] [blame] | 32 | import android.view.Menu; |
| 33 | import android.view.MenuItem; |
| 34 | import android.view.MenuItem.OnMenuItemClickListener; |
Owen Lin | e594b19 | 2009-08-13 18:04:45 +0800 | [diff] [blame] | 35 | |
Romain Guy | e480f77 | 2010-03-10 12:26:30 -0800 | [diff] [blame] | 36 | import com.android.camera.R; |
Chih-Chung Chang | 3889cf3 | 2010-03-05 21:33:49 -0800 | [diff] [blame] | 37 | |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 38 | import java.io.Closeable; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 39 | |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 40 | /** |
| 41 | * A utility class to handle various kinds of menu operations. |
| 42 | */ |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 43 | public class MenuHelper { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 44 | private static final String TAG = "MenuHelper"; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 45 | |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 46 | 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 Chen | 0a5a78c | 2009-04-24 14:13:01 +0800 | [diff] [blame] | 54 | public static final int INCLUDE_SHOWMAP_MENU = (1 << 7); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 55 | |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 56 | 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 Chang | ac9d0a1 | 2010-06-15 16:41:33 +0800 | [diff] [blame] | 61 | public static final int POSITION_SWITCH_CAMERA_ID = 3; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 62 | |
| 63 | public static final int NO_STORAGE_ERROR = -1; |
| 64 | public static final int CANNOT_STAT_ERROR = -2; |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 65 | public static final String EMPTY_STRING = ""; |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 66 | public static final String JPEG_MIME_TYPE = "image/jpeg"; |
Ray Chen | 0a5a78c | 2009-04-24 14:13:01 +0800 | [diff] [blame] | 67 | // valid range is -180f to +180f |
| 68 | public static final float INVALID_LATLNG = 255f; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 69 | |
| 70 | /** Activity result code used to report crop results. |
| 71 | */ |
| 72 | public static final int RESULT_COMMON_MENU_CROP = 490; |
| 73 | |
Wei-Ta Chen | b6be8b6 | 2010-02-24 11:34:22 +0800 | [diff] [blame] | 74 | private static final int NO_ANIMATION = 0; |
| 75 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 76 | 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 Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Chih-Chung Chang | 0e48fe6 | 2009-06-26 19:23:56 +0800 | [diff] [blame] | 86 | 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 Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Chih-Chung Chang | 9ae7d02 | 2010-03-12 16:37:35 -0800 | [diff] [blame] | 106 | 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 Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 125 | private static void startCameraActivity(Activity activity, String action) { |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 126 | Intent intent = new Intent(action); |
| 127 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 128 | intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); |
Chih-Chung Chang | cd65be3 | 2009-06-09 13:51:29 +0800 | [diff] [blame] | 129 | |
| 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 Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 134 | activity.startActivity(intent); |
Wei-Ta Chen | b6be8b6 | 2010-02-24 11:34:22 +0800 | [diff] [blame] | 135 | activity.overridePendingTransition(NO_ANIMATION, NO_ANIMATION); |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | public static void gotoVideoMode(Activity activity) { |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 139 | startCameraActivity(activity, MediaStore.INTENT_ACTION_VIDEO_CAMERA); |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 142 | public static void gotoCameraMode(Activity activity) { |
| 143 | startCameraActivity( |
| 144 | activity, MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 147 | public static void gotoCameraImageGallery(Activity activity) { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 148 | gotoGallery(activity, R.string.gallery_camera_bucket_name, |
| 149 | ImageManager.INCLUDE_IMAGES); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 152 | public static void gotoCameraVideoGallery(Activity activity) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 153 | gotoGallery(activity, R.string.gallery_camera_videos_bucket_name, |
| 154 | ImageManager.INCLUDE_VIDEOS); |
| 155 | } |
| 156 | |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 157 | private static void gotoGallery(Activity activity, int windowTitleId, |
| 158 | int mediaTypes) { |
Ray Chen | df926b6 | 2009-11-16 16:36:21 -0800 | [diff] [blame] | 159 | Uri target = Images.Media.EXTERNAL_CONTENT_URI.buildUpon() |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 160 | .appendQueryParameter("bucketId", |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 161 | ImageManager.CAMERA_IMAGE_BUCKET_ID).build(); |
| 162 | Intent intent = new Intent(Intent.ACTION_VIEW, target); |
The Android Open Source Project | e3f4516 | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 163 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 164 | intent.putExtra("windowTitle", activity.getString(windowTitleId)); |
| 165 | intent.putExtra("mediaTypes", mediaTypes); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 166 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 167 | 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 Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 174 | public static int calculatePicturesRemaining() { |
| 175 | try { |
| 176 | if (!ImageManager.hasStorage()) { |
| 177 | return NO_STORAGE_ERROR; |
| 178 | } else { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 179 | String storageDirectory = |
| 180 | Environment.getExternalStorageDirectory().toString(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 181 | StatFs stat = new StatFs(storageDirectory); |
Chih-Chung Chang | 05d20c0 | 2009-11-04 16:21:57 +0800 | [diff] [blame] | 182 | final int PICTURE_BYTES = 1500000; |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 183 | float remaining = ((float) stat.getAvailableBlocks() |
Chih-Chung Chang | 05d20c0 | 2009-11-04 16:21:57 +0800 | [diff] [blame] | 184 | * (float) stat.getBlockSize()) / PICTURE_BYTES; |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 185 | return (int) remaining; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 186 | } |
| 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 Li | 0ab73a8 | 2010-05-28 19:57:36 +0800 | [diff] [blame] | 191 | Log.e(TAG, "Fail to access sdcard", ex); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 192 | return CANNOT_STAT_ERROR; |
| 193 | } |
| 194 | } |
| 195 | } |