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; |
Owen Lin | e594b19 | 2009-08-13 18:04:45 +0800 | [diff] [blame] | 32 | |
Chih-Chung Chang | 3889cf3 | 2010-03-05 21:33:49 -0800 | [diff] [blame^] | 33 | import com.google.android.camera.R; |
| 34 | |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 35 | import java.io.Closeable; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 36 | |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 37 | /** |
| 38 | * A utility class to handle various kinds of menu operations. |
| 39 | */ |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 40 | public class MenuHelper { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 41 | private static final String TAG = "MenuHelper"; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 42 | |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 43 | 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 Chen | 0a5a78c | 2009-04-24 14:13:01 +0800 | [diff] [blame] | 51 | public static final int INCLUDE_SHOWMAP_MENU = (1 << 7); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 52 | |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 53 | 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 Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 72 | |
| 73 | public static final int NO_STORAGE_ERROR = -1; |
| 74 | public static final int CANNOT_STAT_ERROR = -2; |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 75 | public static final String EMPTY_STRING = ""; |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 76 | public static final String JPEG_MIME_TYPE = "image/jpeg"; |
Ray Chen | 0a5a78c | 2009-04-24 14:13:01 +0800 | [diff] [blame] | 77 | // valid range is -180f to +180f |
| 78 | public static final float INVALID_LATLNG = 255f; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 79 | |
| 80 | /** Activity result code used to report crop results. |
| 81 | */ |
| 82 | public static final int RESULT_COMMON_MENU_CROP = 490; |
| 83 | |
Wei-Ta Chen | b6be8b6 | 2010-02-24 11:34:22 +0800 | [diff] [blame] | 84 | private static final int NO_ANIMATION = 0; |
| 85 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 86 | 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 Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Chih-Chung Chang | 0e48fe6 | 2009-06-26 19:23:56 +0800 | [diff] [blame] | 96 | 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 Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 116 | private static void startCameraActivity(Activity activity, String action) { |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 117 | Intent intent = new Intent(action); |
| 118 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 119 | intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); |
Chih-Chung Chang | cd65be3 | 2009-06-09 13:51:29 +0800 | [diff] [blame] | 120 | |
| 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 Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 125 | activity.startActivity(intent); |
Wei-Ta Chen | b6be8b6 | 2010-02-24 11:34:22 +0800 | [diff] [blame] | 126 | activity.overridePendingTransition(NO_ANIMATION, NO_ANIMATION); |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | public static void gotoVideoMode(Activity activity) { |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 130 | startCameraActivity(activity, MediaStore.INTENT_ACTION_VIDEO_CAMERA); |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 133 | public static void gotoCameraMode(Activity activity) { |
| 134 | startCameraActivity( |
| 135 | activity, MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 138 | public static void gotoCameraImageGallery(Activity activity) { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 139 | gotoGallery(activity, R.string.gallery_camera_bucket_name, |
| 140 | ImageManager.INCLUDE_IMAGES); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 143 | public static void gotoCameraVideoGallery(Activity activity) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 144 | gotoGallery(activity, R.string.gallery_camera_videos_bucket_name, |
| 145 | ImageManager.INCLUDE_VIDEOS); |
| 146 | } |
| 147 | |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 148 | private static void gotoGallery(Activity activity, int windowTitleId, |
| 149 | int mediaTypes) { |
Ray Chen | df926b6 | 2009-11-16 16:36:21 -0800 | [diff] [blame] | 150 | Uri target = Images.Media.EXTERNAL_CONTENT_URI.buildUpon() |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 151 | .appendQueryParameter("bucketId", |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 152 | ImageManager.CAMERA_IMAGE_BUCKET_ID).build(); |
| 153 | Intent intent = new Intent(Intent.ACTION_VIEW, target); |
The Android Open Source Project | e3f4516 | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 154 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 155 | intent.putExtra("windowTitle", activity.getString(windowTitleId)); |
| 156 | intent.putExtra("mediaTypes", mediaTypes); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 157 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 158 | 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 Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 165 | public static int calculatePicturesRemaining() { |
| 166 | try { |
| 167 | if (!ImageManager.hasStorage()) { |
| 168 | return NO_STORAGE_ERROR; |
| 169 | } else { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 170 | String storageDirectory = |
| 171 | Environment.getExternalStorageDirectory().toString(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 172 | StatFs stat = new StatFs(storageDirectory); |
Chih-Chung Chang | 05d20c0 | 2009-11-04 16:21:57 +0800 | [diff] [blame] | 173 | final int PICTURE_BYTES = 1500000; |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 174 | float remaining = ((float) stat.getAvailableBlocks() |
Chih-Chung Chang | 05d20c0 | 2009-11-04 16:21:57 +0800 | [diff] [blame] | 175 | * (float) stat.getBlockSize()) / PICTURE_BYTES; |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 176 | return (int) remaining; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 177 | } |
| 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 | } |