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; |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 26 | import android.location.Geocoder; |
repo sync | a274cf1 | 2009-07-06 15:05:23 +0800 | [diff] [blame] | 27 | import android.media.ExifInterface; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 28 | import android.media.MediaMetadataRetriever; |
| 29 | import android.net.Uri; |
| 30 | import android.os.Environment; |
| 31 | import android.os.Handler; |
| 32 | import android.os.StatFs; |
Chih-Chung Chang | 0e48fe6 | 2009-06-26 19:23:56 +0800 | [diff] [blame] | 33 | import android.preference.PreferenceManager; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 34 | import android.provider.MediaStore; |
| 35 | import android.provider.MediaStore.Images; |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 36 | import android.text.format.Formatter; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | import android.view.Menu; |
| 39 | import android.view.MenuItem; |
| 40 | import android.view.SubMenu; |
| 41 | import android.view.View; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 42 | import android.widget.ImageView; |
| 43 | import android.widget.TextView; |
| 44 | import android.widget.Toast; |
| 45 | |
Owen Lin | e594b19 | 2009-08-13 18:04:45 +0800 | [diff] [blame] | 46 | import com.android.camera.gallery.IImage; |
| 47 | |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 48 | import java.io.Closeable; |
Chih-Chung Chang | b147ba2 | 2009-09-16 18:42:46 +0800 | [diff] [blame] | 49 | import java.io.IOException; |
repo sync | aa7075c | 2009-06-24 14:59:48 +0800 | [diff] [blame] | 50 | import java.lang.ref.WeakReference; |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 51 | import java.text.SimpleDateFormat; |
| 52 | import java.util.ArrayList; |
| 53 | import java.util.Date; |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 54 | import java.util.List; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 55 | |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 56 | /** |
| 57 | * A utility class to handle various kinds of menu operations. |
| 58 | */ |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 59 | public class MenuHelper { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 60 | private static final String TAG = "MenuHelper"; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 61 | |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 62 | public static final int INCLUDE_ALL = 0xFFFFFFFF; |
| 63 | public static final int INCLUDE_VIEWPLAY_MENU = (1 << 0); |
| 64 | public static final int INCLUDE_SHARE_MENU = (1 << 1); |
| 65 | public static final int INCLUDE_SET_MENU = (1 << 2); |
| 66 | public static final int INCLUDE_CROP_MENU = (1 << 3); |
| 67 | public static final int INCLUDE_DELETE_MENU = (1 << 4); |
| 68 | public static final int INCLUDE_ROTATE_MENU = (1 << 5); |
| 69 | public static final int INCLUDE_DETAILS_MENU = (1 << 6); |
Ray Chen | 0a5a78c | 2009-04-24 14:13:01 +0800 | [diff] [blame] | 70 | public static final int INCLUDE_SHOWMAP_MENU = (1 << 7); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 71 | |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 72 | public static final int MENU_IMAGE_SHARE = 1; |
| 73 | public static final int MENU_IMAGE_SHOWMAP = 2; |
| 74 | |
| 75 | public static final int POSITION_SWITCH_CAMERA_MODE = 1; |
| 76 | public static final int POSITION_GOTO_GALLERY = 2; |
| 77 | public static final int POSITION_VIEWPLAY = 3; |
| 78 | public static final int POSITION_CAPTURE_PICTURE = 4; |
| 79 | public static final int POSITION_CAPTURE_VIDEO = 5; |
| 80 | public static final int POSITION_IMAGE_SHARE = 6; |
| 81 | public static final int POSITION_IMAGE_ROTATE = 7; |
| 82 | public static final int POSITION_IMAGE_TOSS = 8; |
| 83 | public static final int POSITION_IMAGE_CROP = 9; |
| 84 | public static final int POSITION_IMAGE_SET = 10; |
| 85 | public static final int POSITION_DETAILS = 11; |
| 86 | public static final int POSITION_SHOWMAP = 12; |
| 87 | public static final int POSITION_SLIDESHOW = 13; |
| 88 | public static final int POSITION_MULTISELECT = 14; |
| 89 | public static final int POSITION_CAMERA_SETTING = 15; |
| 90 | public static final int POSITION_GALLERY_SETTING = 16; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 91 | |
| 92 | public static final int NO_STORAGE_ERROR = -1; |
| 93 | public static final int CANNOT_STAT_ERROR = -2; |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 94 | public static final String EMPTY_STRING = ""; |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 95 | public static final String JPEG_MIME_TYPE = "image/jpeg"; |
Ray Chen | 0a5a78c | 2009-04-24 14:13:01 +0800 | [diff] [blame] | 96 | // valid range is -180f to +180f |
| 97 | public static final float INVALID_LATLNG = 255f; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 98 | |
| 99 | /** Activity result code used to report crop results. |
| 100 | */ |
| 101 | public static final int RESULT_COMMON_MENU_CROP = 490; |
| 102 | |
| 103 | public interface MenuItemsResult { |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 104 | public void gettingReadyToOpen(Menu menu, IImage image); |
| 105 | public void aboutToCall(MenuItem item, IImage image); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | public interface MenuInvoker { |
| 109 | public void run(MenuCallback r); |
| 110 | } |
| 111 | |
| 112 | public interface MenuCallback { |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 113 | public void run(Uri uri, IImage image); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 116 | public static void closeSilently(Closeable c) { |
| 117 | if (c != null) { |
| 118 | try { |
| 119 | c.close(); |
| 120 | } catch (Throwable e) { |
| 121 | // ignore |
| 122 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 126 | public static long getImageFileSize(IImage image) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 127 | java.io.InputStream data = image.fullSizeImageData(); |
The Android Open Source Project | 9c9be2e | 2009-03-05 14:34:37 -0800 | [diff] [blame] | 128 | if (data == null) return -1; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 129 | try { |
| 130 | return data.available(); |
| 131 | } catch (java.io.IOException ex) { |
| 132 | return -1; |
| 133 | } finally { |
| 134 | closeSilently(data); |
| 135 | } |
| 136 | } |
| 137 | |
Chih-Chung Chang | 47bf4ce | 2009-03-25 01:51:03 -0700 | [diff] [blame] | 138 | // This is a hack before we find a solution to pass a permission to other |
Chih-Chung Chang | b2a9765 | 2009-07-10 18:39:49 +0800 | [diff] [blame] | 139 | // applications. See bug #1735149, #1836138. |
| 140 | // Checks if the URI is on our whitelist: |
| 141 | // content://media/... (MediaProvider) |
| 142 | // file:///sdcard/... (Browser download) |
| 143 | public static boolean isWhiteListUri(Uri uri) { |
| 144 | if (uri == null) return false; |
| 145 | |
| 146 | String scheme = uri.getScheme(); |
| 147 | String authority = uri.getAuthority(); |
| 148 | |
| 149 | if (scheme.equals("content") && authority.equals("media")) { |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | if (scheme.equals("file")) { |
| 154 | List<String> p = uri.getPathSegments(); |
| 155 | |
| 156 | if (p.size() >= 1 && p.get(0).equals("sdcard")) { |
| 157 | return true; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return false; |
Chih-Chung Chang | 47bf4ce | 2009-03-25 01:51:03 -0700 | [diff] [blame] | 162 | } |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 163 | |
Chih-Chung Chang | eb9d8a2 | 2009-03-27 16:07:25 -0700 | [diff] [blame] | 164 | public static void enableShareMenuItem(Menu menu, boolean enabled) { |
| 165 | MenuItem item = menu.findItem(MENU_IMAGE_SHARE); |
| 166 | if (item != null) { |
| 167 | item.setVisible(enabled); |
| 168 | item.setEnabled(enabled); |
| 169 | } |
| 170 | } |
| 171 | |
Chih-Chung Chang | 120bf58 | 2009-09-07 18:51:47 +0800 | [diff] [blame] | 172 | public static boolean hasLatLngData(IImage image) { |
Chih-Chung Chang | b147ba2 | 2009-09-16 18:42:46 +0800 | [diff] [blame] | 173 | ExifInterface exif = getExif(image); |
| 174 | if (exif == null) return false; |
Chih-Chung Chang | 55345e9 | 2009-09-17 18:09:49 +0800 | [diff] [blame] | 175 | float latlng[] = new float[2]; |
| 176 | return exif.getLatLong(latlng); |
Chih-Chung Chang | 120bf58 | 2009-09-07 18:51:47 +0800 | [diff] [blame] | 177 | } |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 178 | |
Chih-Chung Chang | 120bf58 | 2009-09-07 18:51:47 +0800 | [diff] [blame] | 179 | public static void enableShowOnMapMenuItem(Menu menu, boolean enabled) { |
| 180 | MenuItem item = menu.findItem(MENU_IMAGE_SHOWMAP); |
| 181 | if (item != null) { |
| 182 | item.setEnabled(enabled); |
| 183 | } |
| 184 | } |
| 185 | |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 186 | private static void setDetailsValue(View d, String text, int valueId) { |
| 187 | ((TextView) d.findViewById(valueId)).setText(text); |
| 188 | } |
| 189 | |
| 190 | private static void hideDetailsRow(View d, int rowId) { |
| 191 | d.findViewById(rowId).setVisibility(View.GONE); |
| 192 | } |
| 193 | |
repo sync | aa7075c | 2009-06-24 14:59:48 +0800 | [diff] [blame] | 194 | private static class UpdateLocationCallback implements |
| 195 | ReverseGeocoderTask.Callback { |
| 196 | WeakReference<View> mView; |
| 197 | |
| 198 | public UpdateLocationCallback(WeakReference<View> view) { |
| 199 | mView = view; |
| 200 | } |
| 201 | |
| 202 | public void onComplete(String location) { |
| 203 | // View d is per-thread data, so when setDetailsValue is |
| 204 | // executed by UI thread, it doesn't matter whether the |
| 205 | // details dialog is dismissed or not. |
| 206 | View view = mView.get(); |
| 207 | if (view == null) return; |
Chih-Chung Chang | 9b93bcb | 2009-09-04 19:46:14 +0800 | [diff] [blame] | 208 | if (!location.equals(MenuHelper.EMPTY_STRING)) { |
repo sync | aa7075c | 2009-06-24 14:59:48 +0800 | [diff] [blame] | 209 | MenuHelper.setDetailsValue(view, location, |
| 210 | R.id.details_location_value); |
| 211 | } else { |
| 212 | MenuHelper.hideDetailsRow(view, R.id.details_location_row); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private static void setLatLngDetails(final View d, Activity context, |
Chih-Chung Chang | b147ba2 | 2009-09-16 18:42:46 +0800 | [diff] [blame] | 218 | ExifInterface exif) { |
Chih-Chung Chang | 55345e9 | 2009-09-17 18:09:49 +0800 | [diff] [blame] | 219 | float[] latlng = new float[2]; |
| 220 | if (exif.getLatLong(latlng)) { |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 221 | setDetailsValue(d, String.valueOf(latlng[0]), |
| 222 | R.id.details_latitude_value); |
| 223 | setDetailsValue(d, String.valueOf(latlng[1]), |
repo sync | aa7075c | 2009-06-24 14:59:48 +0800 | [diff] [blame] | 224 | R.id.details_longitude_value); |
| 225 | |
| 226 | if (latlng[0] == INVALID_LATLNG || latlng[1] == INVALID_LATLNG) { |
| 227 | hideDetailsRow(d, R.id.details_latitude_row); |
| 228 | hideDetailsRow(d, R.id.details_longitude_row); |
| 229 | hideDetailsRow(d, R.id.details_location_row); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | UpdateLocationCallback cb = new UpdateLocationCallback( |
| 234 | new WeakReference<View>(d)); |
| 235 | Geocoder geocoder = new Geocoder(context); |
| 236 | new ReverseGeocoderTask(geocoder, latlng, cb).execute(); |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 237 | } else { |
| 238 | hideDetailsRow(d, R.id.details_latitude_row); |
| 239 | hideDetailsRow(d, R.id.details_longitude_row); |
| 240 | hideDetailsRow(d, R.id.details_location_row); |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 241 | } |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Chih-Chung Chang | b147ba2 | 2009-09-16 18:42:46 +0800 | [diff] [blame] | 244 | private static ExifInterface getExif(IImage image) { |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 245 | if (!JPEG_MIME_TYPE.equals(image.getMimeType())) { |
| 246 | return null; |
| 247 | } |
| 248 | |
Chih-Chung Chang | b147ba2 | 2009-09-16 18:42:46 +0800 | [diff] [blame] | 249 | try { |
| 250 | return new ExifInterface(image.getDataPath()); |
| 251 | } catch (IOException ex) { |
| 252 | Log.e(TAG, "cannot read exif", ex); |
| 253 | return null; |
| 254 | } |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 255 | } |
Ray Chen | 0a5a78c | 2009-04-24 14:13:01 +0800 | [diff] [blame] | 256 | // Called when "Show on Maps" is clicked. |
| 257 | // Displays image location on Google Maps for further operations. |
| 258 | private static boolean onShowMapClicked(MenuInvoker onInvoke, |
| 259 | final Handler handler, |
| 260 | final Activity activity) { |
| 261 | onInvoke.run(new MenuCallback() { |
| 262 | public void run(Uri u, IImage image) { |
| 263 | if (image == null) { |
| 264 | return; |
| 265 | } |
Chih-Chung Chang | 55345e9 | 2009-09-17 18:09:49 +0800 | [diff] [blame] | 266 | |
| 267 | boolean ok = false; |
Chih-Chung Chang | b147ba2 | 2009-09-16 18:42:46 +0800 | [diff] [blame] | 268 | ExifInterface exif = getExif(image); |
Chih-Chung Chang | 55345e9 | 2009-09-17 18:09:49 +0800 | [diff] [blame] | 269 | float latlng[] = null; |
| 270 | if (exif != null) { |
| 271 | latlng = new float[2]; |
| 272 | if (exif.getLatLong(latlng)) { |
| 273 | ok = true; |
| 274 | } |
| 275 | } |
Ray Chen | 4e3fd26 | 2009-09-21 11:05:58 -0700 | [diff] [blame] | 276 | |
| 277 | if (!ok) { |
Ray Chen | 0a5a78c | 2009-04-24 14:13:01 +0800 | [diff] [blame] | 278 | handler.post(new Runnable() { |
| 279 | public void run() { |
| 280 | Toast.makeText(activity, |
| 281 | R.string.no_location_image, |
| 282 | Toast.LENGTH_SHORT).show(); |
| 283 | } |
| 284 | }); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | // Can't use geo:latitude,longitude because it only centers |
| 289 | // the MapView to specified location, but we need a bubble |
| 290 | // for further operations (routing to/from). |
| 291 | // The q=(lat, lng) syntax is suggested by geo-team. |
| 292 | String uri = "http://maps.google.com/maps?f=q&" + |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 293 | "q=(" + latlng[0] + "," + latlng[1] + ")"; |
Ray Chen | 0a5a78c | 2009-04-24 14:13:01 +0800 | [diff] [blame] | 294 | activity.startActivity(new Intent( |
| 295 | android.content.Intent.ACTION_VIEW, |
| 296 | Uri.parse(uri))); |
| 297 | } |
| 298 | }); |
| 299 | return true; |
| 300 | } |
| 301 | |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 302 | private static void hideExifInformation(View d) { |
| 303 | hideDetailsRow(d, R.id.details_resolution_row); |
| 304 | hideDetailsRow(d, R.id.details_make_row); |
| 305 | hideDetailsRow(d, R.id.details_model_row); |
| 306 | hideDetailsRow(d, R.id.details_whitebalance_row); |
| 307 | hideDetailsRow(d, R.id.details_latitude_row); |
| 308 | hideDetailsRow(d, R.id.details_longitude_row); |
| 309 | hideDetailsRow(d, R.id.details_location_row); |
| 310 | } |
| 311 | |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 312 | private static void showExifInformation(IImage image, View d, |
| 313 | Activity activity) { |
Chih-Chung Chang | b147ba2 | 2009-09-16 18:42:46 +0800 | [diff] [blame] | 314 | ExifInterface exif = getExif(image); |
| 315 | if (exif == null) { |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 316 | hideExifInformation(d); |
| 317 | return; |
| 318 | } |
| 319 | |
Chih-Chung Chang | b147ba2 | 2009-09-16 18:42:46 +0800 | [diff] [blame] | 320 | String value = exif.getAttribute(ExifInterface.TAG_MAKE); |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 321 | if (value != null) { |
| 322 | setDetailsValue(d, value, R.id.details_make_value); |
| 323 | } else { |
| 324 | hideDetailsRow(d, R.id.details_make_row); |
| 325 | } |
| 326 | |
Chih-Chung Chang | b147ba2 | 2009-09-16 18:42:46 +0800 | [diff] [blame] | 327 | value = exif.getAttribute(ExifInterface.TAG_MODEL); |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 328 | if (value != null) { |
| 329 | setDetailsValue(d, value, R.id.details_model_value); |
| 330 | } else { |
| 331 | hideDetailsRow(d, R.id.details_model_row); |
| 332 | } |
| 333 | |
Chih-Chung Chang | 55345e9 | 2009-09-17 18:09:49 +0800 | [diff] [blame] | 334 | value = getWhiteBalanceString(exif); |
Chih-Chung Chang | b147ba2 | 2009-09-16 18:42:46 +0800 | [diff] [blame] | 335 | if (value != null && !value.equals(EMPTY_STRING)) { |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 336 | setDetailsValue(d, value, R.id.details_whitebalance_value); |
| 337 | } else { |
| 338 | hideDetailsRow(d, R.id.details_whitebalance_row); |
| 339 | } |
| 340 | |
Chih-Chung Chang | b147ba2 | 2009-09-16 18:42:46 +0800 | [diff] [blame] | 341 | setLatLngDetails(d, activity, exif); |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 342 | } |
| 343 | |
Chih-Chung Chang | 55345e9 | 2009-09-17 18:09:49 +0800 | [diff] [blame] | 344 | /** |
| 345 | * Returns a human-readable string describing the white balance value. Returns empty |
| 346 | * string if there is no white balance value or it is not recognized. |
| 347 | */ |
| 348 | private static String getWhiteBalanceString(ExifInterface exif) { |
| 349 | int whitebalance = exif.getAttributeInt(ExifInterface.TAG_WHITE_BALANCE, -1); |
| 350 | if (whitebalance == -1) return ""; |
| 351 | |
| 352 | switch (whitebalance) { |
| 353 | case ExifInterface.WHITEBALANCE_AUTO: |
| 354 | return "Auto"; |
| 355 | case ExifInterface.WHITEBALANCE_MANUAL: |
| 356 | return "Manual"; |
| 357 | default: |
| 358 | return ""; |
| 359 | } |
| 360 | } |
| 361 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 362 | // Called when "Details" is clicked. |
| 363 | // Displays detailed information about the image/video. |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 364 | private static boolean onDetailsClicked(MenuInvoker onInvoke, |
Ray Chen | 0a5a78c | 2009-04-24 14:13:01 +0800 | [diff] [blame] | 365 | final Handler handler, |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 366 | final Activity activity) { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 367 | onInvoke.run(new MenuCallback() { |
| 368 | public void run(Uri u, IImage image) { |
| 369 | if (image == null) { |
| 370 | return; |
| 371 | } |
| 372 | |
Chih-Chung Chang | 6b27050 | 2009-04-29 11:57:06 +0800 | [diff] [blame] | 373 | final AlertDialog.Builder builder = |
| 374 | new AlertDialog.Builder(activity); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 375 | |
| 376 | final View d = View.inflate(activity, R.layout.detailsview, |
| 377 | null); |
| 378 | |
| 379 | ImageView imageView = (ImageView) d.findViewById( |
| 380 | R.id.details_thumbnail_image); |
| 381 | imageView.setImageBitmap(image.miniThumbBitmap()); |
| 382 | |
| 383 | TextView textView = (TextView) d.findViewById( |
| 384 | R.id.details_image_title); |
| 385 | textView.setText(image.getDisplayName()); |
| 386 | |
| 387 | long length = getImageFileSize(image); |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 388 | String lengthString = length < 0 |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 389 | ? EMPTY_STRING |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 390 | : Formatter.formatFileSize(activity, length); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 391 | ((TextView) d |
| 392 | .findViewById(R.id.details_file_size_value)) |
| 393 | .setText(lengthString); |
| 394 | |
| 395 | int dimensionWidth = 0; |
| 396 | int dimensionHeight = 0; |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 397 | if (ImageManager.isImage(image)) { |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 398 | // getWidth is much slower than reading from EXIF |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 399 | dimensionWidth = image.getWidth(); |
| 400 | dimensionHeight = image.getHeight(); |
| 401 | d.findViewById(R.id.details_duration_row) |
| 402 | .setVisibility(View.GONE); |
| 403 | d.findViewById(R.id.details_frame_rate_row) |
| 404 | .setVisibility(View.GONE); |
| 405 | d.findViewById(R.id.details_bit_rate_row) |
| 406 | .setVisibility(View.GONE); |
| 407 | d.findViewById(R.id.details_format_row) |
| 408 | .setVisibility(View.GONE); |
| 409 | d.findViewById(R.id.details_codec_row) |
| 410 | .setVisibility(View.GONE); |
| 411 | } else { |
| 412 | MediaMetadataRetriever retriever |
| 413 | = new MediaMetadataRetriever(); |
| 414 | try { |
| 415 | retriever.setMode(MediaMetadataRetriever |
| 416 | .MODE_GET_METADATA_ONLY); |
| 417 | retriever.setDataSource(image.getDataPath()); |
| 418 | try { |
| 419 | dimensionWidth = Integer.parseInt( |
| 420 | retriever.extractMetadata( |
| 421 | MediaMetadataRetriever |
| 422 | .METADATA_KEY_VIDEO_WIDTH)); |
| 423 | dimensionHeight = Integer.parseInt( |
| 424 | retriever.extractMetadata( |
| 425 | MediaMetadataRetriever |
| 426 | .METADATA_KEY_VIDEO_HEIGHT)); |
| 427 | } catch (NumberFormatException e) { |
| 428 | dimensionWidth = 0; |
| 429 | dimensionHeight = 0; |
| 430 | } |
| 431 | |
| 432 | try { |
| 433 | int durationMs = Integer.parseInt( |
| 434 | retriever.extractMetadata( |
| 435 | MediaMetadataRetriever |
| 436 | .METADATA_KEY_DURATION)); |
| 437 | String durationValue = formatDuration( |
| 438 | activity, durationMs); |
| 439 | ((TextView) d.findViewById( |
| 440 | R.id.details_duration_value)) |
| 441 | .setText(durationValue); |
| 442 | } catch (NumberFormatException e) { |
| 443 | d.findViewById( |
| 444 | R.id.details_frame_rate_row) |
| 445 | .setVisibility(View.GONE); |
| 446 | } |
| 447 | |
| 448 | try { |
| 449 | String frameRate = String.format( |
| 450 | activity.getString(R.string.details_fps), |
| 451 | Integer.parseInt( |
| 452 | retriever.extractMetadata( |
| 453 | MediaMetadataRetriever |
| 454 | .METADATA_KEY_FRAME_RATE))); |
| 455 | ((TextView) d.findViewById( |
| 456 | R.id.details_frame_rate_value)) |
| 457 | .setText(frameRate); |
| 458 | } catch (NumberFormatException e) { |
| 459 | d.findViewById( |
| 460 | R.id.details_frame_rate_row) |
| 461 | .setVisibility(View.GONE); |
| 462 | } |
| 463 | |
| 464 | try { |
| 465 | long bitRate = Long.parseLong( |
| 466 | retriever.extractMetadata( |
| 467 | MediaMetadataRetriever |
| 468 | .METADATA_KEY_BIT_RATE)); |
| 469 | String bps; |
| 470 | if (bitRate < 1000000) { |
| 471 | bps = String.format( |
| 472 | activity.getString( |
| 473 | R.string.details_kbps), |
| 474 | bitRate / 1000); |
| 475 | } else { |
| 476 | bps = String.format( |
| 477 | activity.getString( |
| 478 | R.string.details_mbps), |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 479 | (bitRate) / 1000000.0); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 480 | } |
| 481 | ((TextView) d.findViewById( |
| 482 | R.id.details_bit_rate_value)) |
| 483 | .setText(bps); |
| 484 | } catch (NumberFormatException e) { |
| 485 | d.findViewById(R.id.details_bit_rate_row) |
| 486 | .setVisibility(View.GONE); |
| 487 | } |
| 488 | |
| 489 | String format = retriever.extractMetadata( |
| 490 | MediaMetadataRetriever |
| 491 | .METADATA_KEY_VIDEO_FORMAT); |
| 492 | ((TextView) d.findViewById( |
| 493 | R.id.details_format_value)) |
| 494 | .setText(format); |
| 495 | |
| 496 | String codec = retriever.extractMetadata( |
| 497 | MediaMetadataRetriever.METADATA_KEY_CODEC); |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 498 | if (codec != null) { |
| 499 | setDetailsValue(d, codec, R.id.details_codec_value); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 500 | } else { |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 501 | hideDetailsRow(d, R.id.details_codec_row); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 502 | } |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 503 | |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 504 | } catch (RuntimeException ex) { |
| 505 | // Assume this is a corrupt video file. |
| 506 | } finally { |
| 507 | try { |
| 508 | retriever.release(); |
| 509 | } catch (RuntimeException ex) { |
| 510 | // Ignore failures while cleaning up. |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 515 | String value = null; |
| 516 | if (dimensionWidth > 0 && dimensionHeight > 0) { |
| 517 | value = String.format( |
| 518 | activity.getString(R.string.details_dimension_x), |
| 519 | dimensionWidth, dimensionHeight); |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 520 | } |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 521 | |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 522 | if (value != null) { |
| 523 | setDetailsValue(d, value, R.id.details_resolution_value); |
| 524 | } else { |
| 525 | hideDetailsRow(d, R.id.details_resolution_row); |
| 526 | } |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 527 | |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 528 | value = EMPTY_STRING; |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 529 | long dateTaken = image.getDateTaken(); |
| 530 | if (dateTaken != 0) { |
| 531 | Date date = new Date(image.getDateTaken()); |
| 532 | SimpleDateFormat dateFormat = new SimpleDateFormat(); |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 533 | value = dateFormat.format(date); |
| 534 | } |
| 535 | if (value != EMPTY_STRING) { |
| 536 | setDetailsValue(d, value, R.id.details_date_taken_value); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 537 | } else { |
Ray Chen | 0f0af52 | 2009-04-22 13:26:07 +0800 | [diff] [blame] | 538 | hideDetailsRow(d, R.id.details_date_taken_row); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Ray Chen | d5142de | 2009-05-07 15:24:48 +0800 | [diff] [blame] | 541 | // Show more EXIF header details for JPEG images. |
| 542 | if (JPEG_MIME_TYPE.equals(image.getMimeType())) { |
| 543 | showExifInformation(image, d, activity); |
| 544 | } else { |
| 545 | hideExifInformation(d); |
| 546 | } |
| 547 | |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 548 | builder.setNeutralButton(R.string.details_ok, |
| 549 | new DialogInterface.OnClickListener() { |
| 550 | public void onClick(DialogInterface dialog, |
| 551 | int which) { |
| 552 | dialog.dismiss(); |
| 553 | } |
| 554 | }); |
| 555 | |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 556 | handler.post( |
| 557 | new Runnable() { |
| 558 | public void run() { |
Chih-Chung Chang | 6b27050 | 2009-04-29 11:57:06 +0800 | [diff] [blame] | 559 | builder.setIcon( |
| 560 | android.R.drawable.ic_dialog_info) |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 561 | .setTitle(R.string.details_panel_title) |
| 562 | .setView(d) |
| 563 | .show(); |
| 564 | } |
| 565 | }); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 566 | } |
| 567 | }); |
| 568 | return true; |
| 569 | } |
| 570 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 571 | // Called when "Rotate left" or "Rotate right" is clicked. |
| 572 | private static boolean onRotateClicked(MenuInvoker onInvoke, |
| 573 | final int degree) { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 574 | onInvoke.run(new MenuCallback() { |
| 575 | public void run(Uri u, IImage image) { |
| 576 | if (image == null || image.isReadonly()) { |
| 577 | return; |
| 578 | } |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 579 | image.rotateImageBy(degree); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 580 | } |
| 581 | }); |
| 582 | return true; |
| 583 | } |
| 584 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 585 | // Called when "Crop" is clicked. |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 586 | private static boolean onCropClicked(MenuInvoker onInvoke, |
| 587 | final Activity activity) { |
| 588 | onInvoke.run(new MenuCallback() { |
| 589 | public void run(Uri u, IImage image) { |
| 590 | if (u == null) { |
| 591 | return; |
| 592 | } |
| 593 | |
Cheng-Ru Lin | da46087 | 2009-10-16 00:07:29 -0700 | [diff] [blame] | 594 | Intent cropIntent = new Intent( |
| 595 | "com.android.camera.action.CROP"); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 596 | cropIntent.setData(u); |
Cheng-Ru Lin | da46087 | 2009-10-16 00:07:29 -0700 | [diff] [blame] | 597 | activity.startActivityForResult( |
| 598 | cropIntent, RESULT_COMMON_MENU_CROP); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 599 | } |
| 600 | }); |
| 601 | return true; |
| 602 | } |
| 603 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 604 | // Called when "Set as" is clicked. |
| 605 | private static boolean onSetAsClicked(MenuInvoker onInvoke, |
| 606 | final Activity activity) { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 607 | onInvoke.run(new MenuCallback() { |
| 608 | public void run(Uri u, IImage image) { |
| 609 | if (u == null || image == null) { |
| 610 | return; |
| 611 | } |
| 612 | |
Chih-Chung Chang | bb18778 | 2009-07-02 16:46:30 +0800 | [diff] [blame] | 613 | Intent intent = Util.createSetAsIntent(image); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 614 | activity.startActivity(Intent.createChooser(intent, |
| 615 | activity.getText(R.string.setImage))); |
| 616 | } |
| 617 | }); |
| 618 | return true; |
| 619 | } |
| 620 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 621 | // Called when "Share" is clicked. |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 622 | private static boolean onImageShareClicked(MenuInvoker onInvoke, |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 623 | final Activity activity) { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 624 | onInvoke.run(new MenuCallback() { |
| 625 | public void run(Uri u, IImage image) { |
| 626 | if (image == null) return; |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 627 | |
| 628 | Intent intent = new Intent(); |
| 629 | intent.setAction(Intent.ACTION_SEND); |
| 630 | String mimeType = image.getMimeType(); |
| 631 | intent.setType(mimeType); |
| 632 | intent.putExtra(Intent.EXTRA_STREAM, u); |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 633 | boolean isImage = ImageManager.isImage(image); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 634 | try { |
| 635 | activity.startActivity(Intent.createChooser(intent, |
| 636 | activity.getText(isImage |
| 637 | ? R.string.sendImage |
| 638 | : R.string.sendVideo))); |
| 639 | } catch (android.content.ActivityNotFoundException ex) { |
| 640 | Toast.makeText(activity, isImage |
| 641 | ? R.string.no_way_to_share_image |
| 642 | : R.string.no_way_to_share_video, |
| 643 | Toast.LENGTH_SHORT).show(); |
| 644 | } |
| 645 | } |
| 646 | }); |
| 647 | return true; |
| 648 | } |
| 649 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 650 | // Called when "Play" is clicked. |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 651 | private static boolean onViewPlayClicked(MenuInvoker onInvoke, |
| 652 | final Activity activity) { |
| 653 | onInvoke.run(new MenuCallback() { |
| 654 | public void run(Uri uri, IImage image) { |
| 655 | if (image != null) { |
| 656 | Intent intent = new Intent(Intent.ACTION_VIEW, |
| 657 | image.fullSizeImageUri()); |
| 658 | activity.startActivity(intent); |
| 659 | } |
| 660 | }}); |
| 661 | return true; |
| 662 | } |
| 663 | |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 664 | // Called when "Delete" is clicked. |
| 665 | private static boolean onDeleteClicked(MenuInvoker onInvoke, |
| 666 | final Activity activity, final Runnable onDelete) { |
| 667 | onInvoke.run(new MenuCallback() { |
| 668 | public void run(Uri uri, IImage image) { |
| 669 | if (image != null) { |
| 670 | deleteImage(activity, onDelete, image); |
| 671 | } |
| 672 | }}); |
| 673 | return true; |
| 674 | } |
| 675 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 676 | static MenuItemsResult addImageMenuItems( |
| 677 | Menu menu, |
| 678 | int inclusions, |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 679 | final Activity activity, |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 680 | final Handler handler, |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 681 | final Runnable onDelete, |
| 682 | final MenuInvoker onInvoke) { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 683 | final ArrayList<MenuItem> requiresWriteAccessItems = |
| 684 | new ArrayList<MenuItem>(); |
| 685 | final ArrayList<MenuItem> requiresNoDrmAccessItems = |
| 686 | new ArrayList<MenuItem>(); |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 687 | final ArrayList<MenuItem> requiresImageItems = |
| 688 | new ArrayList<MenuItem>(); |
| 689 | final ArrayList<MenuItem> requiresVideoItems = |
| 690 | new ArrayList<MenuItem>(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 691 | |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 692 | if ((inclusions & INCLUDE_ROTATE_MENU) != 0) { |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 693 | SubMenu rotateSubmenu = menu.addSubMenu(Menu.NONE, Menu.NONE, |
| 694 | POSITION_IMAGE_ROTATE, R.string.rotate) |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 695 | .setIcon(android.R.drawable.ic_menu_rotate); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 696 | // Don't show the rotate submenu if the item at hand is read only |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 697 | // since the items within the submenu won't be shown anyway. This |
| 698 | // is really a framework bug in that it shouldn't show the submenu |
| 699 | // if the submenu has no visible items. |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 700 | MenuItem rotateLeft = rotateSubmenu.add(R.string.rotate_left) |
Chih-Chung Chang | f6e6d2b | 2009-05-14 19:19:17 +0800 | [diff] [blame] | 701 | .setOnMenuItemClickListener( |
| 702 | new MenuItem.OnMenuItemClickListener() { |
| 703 | public boolean onMenuItemClick(MenuItem item) { |
| 704 | return onRotateClicked(onInvoke, -90); |
| 705 | } |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 706 | }).setAlphabeticShortcut('l'); |
| 707 | |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 708 | MenuItem rotateRight = rotateSubmenu.add(R.string.rotate_right) |
Chih-Chung Chang | f6e6d2b | 2009-05-14 19:19:17 +0800 | [diff] [blame] | 709 | .setOnMenuItemClickListener( |
| 710 | new MenuItem.OnMenuItemClickListener() { |
| 711 | public boolean onMenuItemClick(MenuItem item) { |
| 712 | return onRotateClicked(onInvoke, 90); |
| 713 | } |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 714 | }).setAlphabeticShortcut('r'); |
| 715 | |
| 716 | requiresWriteAccessItems.add(rotateSubmenu.getItem()); |
| 717 | requiresWriteAccessItems.add(rotateLeft); |
| 718 | requiresWriteAccessItems.add(rotateRight); |
| 719 | |
| 720 | requiresImageItems.add(rotateSubmenu.getItem()); |
| 721 | requiresImageItems.add(rotateLeft); |
| 722 | requiresImageItems.add(rotateRight); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 723 | } |
| 724 | |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 725 | if ((inclusions & INCLUDE_CROP_MENU) != 0) { |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 726 | MenuItem autoCrop = menu.add(Menu.NONE, Menu.NONE, |
| 727 | POSITION_IMAGE_CROP, R.string.camera_crop); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 728 | autoCrop.setIcon(android.R.drawable.ic_menu_crop); |
| 729 | autoCrop.setOnMenuItemClickListener( |
| 730 | new MenuItem.OnMenuItemClickListener() { |
| 731 | public boolean onMenuItemClick(MenuItem item) { |
| 732 | return onCropClicked(onInvoke, activity); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 733 | } |
| 734 | }); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 735 | requiresWriteAccessItems.add(autoCrop); |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 736 | requiresImageItems.add(autoCrop); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 739 | if ((inclusions & INCLUDE_SET_MENU) != 0) { |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 740 | MenuItem setMenu = menu.add(Menu.NONE, Menu.NONE, |
| 741 | POSITION_IMAGE_SET, R.string.camera_set); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 742 | setMenu.setIcon(android.R.drawable.ic_menu_set_as); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 743 | setMenu.setOnMenuItemClickListener( |
| 744 | new MenuItem.OnMenuItemClickListener() { |
| 745 | public boolean onMenuItemClick(MenuItem item) { |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 746 | return onSetAsClicked(onInvoke, activity); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 747 | } |
| 748 | }); |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 749 | requiresImageItems.add(setMenu); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | if ((inclusions & INCLUDE_SHARE_MENU) != 0) { |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 753 | MenuItem item1 = menu.add(Menu.NONE, MENU_IMAGE_SHARE, |
| 754 | POSITION_IMAGE_SHARE, R.string.camera_share) |
| 755 | .setOnMenuItemClickListener( |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 756 | new MenuItem.OnMenuItemClickListener() { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 757 | public boolean onMenuItemClick(MenuItem item) { |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 758 | return onImageShareClicked(onInvoke, activity); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 759 | } |
| 760 | }); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 761 | item1.setIcon(android.R.drawable.ic_menu_share); |
| 762 | MenuItem item = item1; |
| 763 | requiresNoDrmAccessItems.add(item); |
| 764 | } |
| 765 | |
| 766 | if ((inclusions & INCLUDE_DELETE_MENU) != 0) { |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 767 | MenuItem deleteItem = menu.add(Menu.NONE, Menu.NONE, |
| 768 | POSITION_IMAGE_TOSS, R.string.camera_toss); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 769 | requiresWriteAccessItems.add(deleteItem); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 770 | deleteItem.setOnMenuItemClickListener( |
| 771 | new MenuItem.OnMenuItemClickListener() { |
| 772 | public boolean onMenuItemClick(MenuItem item) { |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 773 | return onDeleteClicked(onInvoke, activity, |
| 774 | onDelete); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 775 | } |
| 776 | }) |
| 777 | .setAlphabeticShortcut('d') |
| 778 | .setIcon(android.R.drawable.ic_menu_delete); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | if ((inclusions & INCLUDE_DETAILS_MENU) != 0) { |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 782 | MenuItem detailsMenu = menu.add(Menu.NONE, Menu.NONE, |
| 783 | POSITION_DETAILS, R.string.details) |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 784 | .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 785 | public boolean onMenuItemClick(MenuItem item) { |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 786 | return onDetailsClicked(onInvoke, handler, activity); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 787 | } |
| 788 | }); |
| 789 | detailsMenu.setIcon(R.drawable.ic_menu_view_details); |
| 790 | } |
| 791 | |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 792 | if ((inclusions & INCLUDE_SHOWMAP_MENU) != 0) { |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 793 | MenuItem showOnMapItem = menu.add(Menu.NONE, MENU_IMAGE_SHOWMAP, |
| 794 | POSITION_SHOWMAP, R.string.show_on_map); |
Chih-Chung Chang | 120bf58 | 2009-09-07 18:51:47 +0800 | [diff] [blame] | 795 | showOnMapItem.setOnMenuItemClickListener( |
Ray Chen | 0a5a78c | 2009-04-24 14:13:01 +0800 | [diff] [blame] | 796 | new MenuItem.OnMenuItemClickListener() { |
| 797 | public boolean onMenuItemClick(MenuItem item) { |
| 798 | return onShowMapClicked(onInvoke, |
| 799 | handler, activity); |
| 800 | } |
Ray Chen | 1e99d7c | 2009-05-13 13:55:29 +0800 | [diff] [blame] | 801 | }).setIcon(R.drawable.ic_menu_3d_globe); |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 802 | requiresImageItems.add(showOnMapItem); |
Ray Chen | 0a5a78c | 2009-04-24 14:13:01 +0800 | [diff] [blame] | 803 | } |
| 804 | |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 805 | if ((inclusions & INCLUDE_VIEWPLAY_MENU) != 0) { |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 806 | MenuItem videoPlayItem = menu.add(Menu.NONE, Menu.NONE, |
| 807 | POSITION_VIEWPLAY, R.string.video_play) |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 808 | .setOnMenuItemClickListener( |
| 809 | new MenuItem.OnMenuItemClickListener() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 810 | public boolean onMenuItemClick(MenuItem item) { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 811 | return onViewPlayClicked(onInvoke, activity); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 812 | } |
| 813 | }); |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 814 | videoPlayItem.setIcon( |
| 815 | com.android.internal.R.drawable.ic_menu_play_clip); |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 816 | requiresVideoItems.add(videoPlayItem); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 817 | } |
| 818 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 819 | return new MenuItemsResult() { |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 820 | public void gettingReadyToOpen(Menu menu, IImage image) { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 821 | // protect against null here. this isn't strictly speaking |
| 822 | // required but if a client app isn't handling sdcard removal |
| 823 | // properly it could happen |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 824 | if (image == null) { |
| 825 | return; |
| 826 | } |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 827 | |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 828 | ArrayList<MenuItem> enableList = new ArrayList<MenuItem>(); |
| 829 | ArrayList<MenuItem> disableList = new ArrayList<MenuItem>(); |
| 830 | ArrayList<MenuItem> list; |
| 831 | |
| 832 | list = image.isReadonly() ? disableList : enableList; |
| 833 | list.addAll(requiresWriteAccessItems); |
| 834 | |
| 835 | list = image.isDrm() ? disableList : enableList; |
| 836 | list.addAll(requiresNoDrmAccessItems); |
| 837 | |
| 838 | list = ImageManager.isImage(image) ? enableList : disableList; |
| 839 | list.addAll(requiresImageItems); |
| 840 | |
| 841 | list = ImageManager.isVideo(image) ? enableList : disableList; |
| 842 | list.addAll(requiresVideoItems); |
repo sync | a274cf1 | 2009-07-06 15:05:23 +0800 | [diff] [blame] | 843 | |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 844 | for (MenuItem item : enableList) { |
| 845 | item.setVisible(true); |
| 846 | item.setEnabled(true); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 847 | } |
Chih-Chung Chang | 35f8af0 | 2009-07-01 14:52:24 +0800 | [diff] [blame] | 848 | |
| 849 | for (MenuItem item : disableList) { |
| 850 | item.setVisible(false); |
| 851 | item.setEnabled(false); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 852 | } |
| 853 | } |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 854 | |
| 855 | // must override abstract method |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 856 | public void aboutToCall(MenuItem menu, IImage image) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 857 | } |
| 858 | }; |
| 859 | } |
| 860 | |
| 861 | static void deletePhoto(Activity activity, Runnable onDelete) { |
Ray Chen | 7638a54 | 2009-03-24 20:37:45 -0700 | [diff] [blame] | 862 | deleteImpl(activity, onDelete, true); |
| 863 | } |
Owen Lin | 095afa1 | 2009-03-24 23:41:27 -0700 | [diff] [blame] | 864 | |
Ray Chen | 7638a54 | 2009-03-24 20:37:45 -0700 | [diff] [blame] | 865 | static void deleteVideo(Activity activity, Runnable onDelete) { |
| 866 | deleteImpl(activity, onDelete, false); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Chih-Chung Chang | 0e48fe6 | 2009-06-26 19:23:56 +0800 | [diff] [blame] | 869 | static void deleteImage( |
| 870 | Activity activity, Runnable onDelete, IImage image) { |
| 871 | deleteImpl(activity, onDelete, ImageManager.isImage(image)); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Chih-Chung Chang | 0e48fe6 | 2009-06-26 19:23:56 +0800 | [diff] [blame] | 874 | static void deleteImpl( |
| 875 | Activity activity, Runnable onDelete, boolean isImage) { |
| 876 | boolean needConfirm = PreferenceManager |
| 877 | .getDefaultSharedPreferences(activity) |
| 878 | .getBoolean("pref_gallery_confirm_delete_key", true); |
| 879 | if (!needConfirm) { |
| 880 | if (onDelete != null) onDelete.run(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 881 | } else { |
Chih-Chung Chang | 0e48fe6 | 2009-06-26 19:23:56 +0800 | [diff] [blame] | 882 | String title = activity.getString(R.string.confirm_delete_title); |
| 883 | String message = activity.getString(isImage |
| 884 | ? R.string.confirm_delete_message |
| 885 | : R.string.confirm_delete_video_message); |
| 886 | confirmAction(activity, title, message, onDelete); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
Chih-Chung Chang | 0e48fe6 | 2009-06-26 19:23:56 +0800 | [diff] [blame] | 890 | public static void deleteMultiple(Context context, Runnable action) { |
| 891 | boolean needConfirm = PreferenceManager |
| 892 | .getDefaultSharedPreferences(context) |
| 893 | .getBoolean("pref_gallery_confirm_delete_key", true); |
| 894 | if (!needConfirm) { |
| 895 | if (action != null) action.run(); |
| 896 | } else { |
| 897 | String title = context.getString(R.string.confirm_delete_title); |
| 898 | String message = context.getString( |
| 899 | R.string.confirm_delete_multiple_message); |
| 900 | confirmAction(context, title, message, action); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | public static void confirmAction(Context context, String title, |
| 905 | String message, final Runnable action) { |
| 906 | OnClickListener listener = new OnClickListener() { |
| 907 | public void onClick(DialogInterface dialog, int which) { |
| 908 | switch (which) { |
| 909 | case DialogInterface.BUTTON_POSITIVE: |
| 910 | if (action != null) action.run(); |
| 911 | } |
| 912 | } |
| 913 | }; |
| 914 | new AlertDialog.Builder(context) |
| 915 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 916 | .setTitle(title) |
| 917 | .setMessage(message) |
| 918 | .setPositiveButton(android.R.string.ok, listener) |
| 919 | .setNegativeButton(android.R.string.cancel, listener) |
| 920 | .create() |
| 921 | .show(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 922 | } |
| 923 | |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 924 | private static void startCameraActivity(Activity activity, String action) { |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 925 | Intent intent = new Intent(action); |
| 926 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 927 | intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); |
Chih-Chung Chang | cd65be3 | 2009-06-09 13:51:29 +0800 | [diff] [blame] | 928 | |
| 929 | // Keep the camera instance for a while. |
| 930 | // This avoids re-opening the camera and saves time. |
| 931 | CameraHolder.instance().keep(); |
| 932 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 933 | activity.startActivity(intent); |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | public static void gotoVideoMode(Activity activity) { |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 937 | startCameraActivity(activity, MediaStore.INTENT_ACTION_VIDEO_CAMERA); |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 940 | public static void gotoCameraMode(Activity activity) { |
| 941 | startCameraActivity( |
| 942 | activity, MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 943 | } |
| 944 | |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 945 | public static void gotoCameraImageGallery(Activity activity) { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 946 | gotoGallery(activity, R.string.gallery_camera_bucket_name, |
| 947 | ImageManager.INCLUDE_IMAGES); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 948 | } |
| 949 | |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 950 | public static void gotoCameraVideoGallery(Activity activity) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 951 | gotoGallery(activity, R.string.gallery_camera_videos_bucket_name, |
| 952 | ImageManager.INCLUDE_VIDEOS); |
| 953 | } |
| 954 | |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 955 | private static void gotoGallery(Activity activity, int windowTitleId, |
| 956 | int mediaTypes) { |
Ray Chen | df926b6 | 2009-11-16 16:36:21 -0800 | [diff] [blame^] | 957 | Uri target = Images.Media.EXTERNAL_CONTENT_URI.buildUpon() |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 958 | .appendQueryParameter("bucketId", |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 959 | ImageManager.CAMERA_IMAGE_BUCKET_ID).build(); |
| 960 | Intent intent = new Intent(Intent.ACTION_VIEW, target); |
The Android Open Source Project | e3f4516 | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 961 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 962 | intent.putExtra("windowTitle", activity.getString(windowTitleId)); |
| 963 | intent.putExtra("mediaTypes", mediaTypes); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 964 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 965 | try { |
| 966 | activity.startActivity(intent); |
| 967 | } catch (ActivityNotFoundException e) { |
| 968 | Log.e(TAG, "Could not start gallery activity", e); |
| 969 | } |
| 970 | } |
| 971 | |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 972 | static void addCapturePictureMenuItems(Menu menu, final Activity activity) { |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 973 | menu.add(Menu.NONE, Menu.NONE, POSITION_CAPTURE_PICTURE, |
| 974 | R.string.capture_picture) |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 975 | .setOnMenuItemClickListener( |
| 976 | new MenuItem.OnMenuItemClickListener() { |
| 977 | public boolean onMenuItemClick(MenuItem item) { |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 978 | return onCapturePictureClicked(activity); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 979 | } |
| 980 | }).setIcon(android.R.drawable.ic_menu_camera); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 981 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 982 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 983 | private static boolean onCapturePictureClicked(Activity activity) { |
| 984 | Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); |
| 985 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 986 | try { |
| 987 | activity.startActivity(intent); |
| 988 | } catch (android.content.ActivityNotFoundException e) { |
| 989 | // Ignore exception |
| 990 | } |
| 991 | return true; |
| 992 | } |
| 993 | |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 994 | static void addCaptureVideoMenuItems(Menu menu, final Activity activity) { |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 995 | menu.add(Menu.NONE, Menu.NONE, POSITION_CAPTURE_VIDEO, |
| 996 | R.string.capture_video) |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 997 | .setOnMenuItemClickListener( |
| 998 | new MenuItem.OnMenuItemClickListener() { |
| 999 | public boolean onMenuItemClick(MenuItem item) { |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 1000 | return onCaptureVideoClicked(activity); |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 1001 | } |
| 1002 | }).setIcon(R.drawable.ic_menu_camera_video_view); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1003 | } |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1004 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 1005 | private static boolean onCaptureVideoClicked(Activity activity) { |
| 1006 | Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA); |
| 1007 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 1008 | try { |
| 1009 | activity.startActivity(intent); |
| 1010 | } catch (android.content.ActivityNotFoundException e) { |
| 1011 | // Ignore exception |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 1012 | } |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 1013 | return true; |
| 1014 | } |
| 1015 | |
Chih-Chung Chang | 272c3fd | 2009-04-08 05:14:37 -0700 | [diff] [blame] | 1016 | public static void addCaptureMenuItems(Menu menu, final Activity activity) { |
| 1017 | addCapturePictureMenuItems(menu, activity); |
| 1018 | addCaptureVideoMenuItems(menu, activity); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
Owen Lin | 6795ff1 | 2009-06-09 13:39:00 -0700 | [diff] [blame] | 1021 | public static String formatDuration(final Context context, |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 1022 | int durationMs) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1023 | int duration = durationMs / 1000; |
| 1024 | int h = duration / 3600; |
| 1025 | int m = (duration - h * 3600) / 60; |
| 1026 | int s = duration - (h * 3600 + m * 60); |
| 1027 | String durationValue; |
| 1028 | if (h == 0) { |
| 1029 | durationValue = String.format( |
Owen Lin | 6795ff1 | 2009-06-09 13:39:00 -0700 | [diff] [blame] | 1030 | context.getString(R.string.details_ms), m, s); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1031 | } else { |
| 1032 | durationValue = String.format( |
Owen Lin | 6795ff1 | 2009-06-09 13:39:00 -0700 | [diff] [blame] | 1033 | context.getString(R.string.details_hms), h, m, s); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1034 | } |
| 1035 | return durationValue; |
| 1036 | } |
| 1037 | |
| 1038 | public static void showStorageToast(Activity activity) { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 1039 | showStorageToast(activity, calculatePicturesRemaining()); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | public static void showStorageToast(Activity activity, int remaining) { |
| 1043 | String noStorageText = null; |
| 1044 | |
| 1045 | if (remaining == MenuHelper.NO_STORAGE_ERROR) { |
| 1046 | String state = Environment.getExternalStorageState(); |
| 1047 | if (state == Environment.MEDIA_CHECKING) { |
| 1048 | noStorageText = activity.getString(R.string.preparing_sd); |
| 1049 | } else { |
| 1050 | noStorageText = activity.getString(R.string.no_storage); |
| 1051 | } |
| 1052 | } else if (remaining < 1) { |
| 1053 | noStorageText = activity.getString(R.string.not_enough_space); |
| 1054 | } |
| 1055 | |
| 1056 | if (noStorageText != null) { |
| 1057 | Toast.makeText(activity, noStorageText, 5000).show(); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | public static int calculatePicturesRemaining() { |
| 1062 | try { |
| 1063 | if (!ImageManager.hasStorage()) { |
| 1064 | return NO_STORAGE_ERROR; |
| 1065 | } else { |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 1066 | String storageDirectory = |
| 1067 | Environment.getExternalStorageDirectory().toString(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1068 | StatFs stat = new StatFs(storageDirectory); |
Chih-Chung Chang | 05d20c0 | 2009-11-04 16:21:57 +0800 | [diff] [blame] | 1069 | final int PICTURE_BYTES = 1500000; |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 1070 | float remaining = ((float) stat.getAvailableBlocks() |
Chih-Chung Chang | 05d20c0 | 2009-11-04 16:21:57 +0800 | [diff] [blame] | 1071 | * (float) stat.getBlockSize()) / PICTURE_BYTES; |
Chih-Chung Chang | 9ae6df0 | 2009-04-08 01:51:45 -0700 | [diff] [blame] | 1072 | return (int) remaining; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1073 | } |
| 1074 | } catch (Exception ex) { |
| 1075 | // if we can't stat the filesystem then we don't know how many |
| 1076 | // pictures are remaining. it might be zero but just leave it |
| 1077 | // blank since we really don't know. |
| 1078 | return CANNOT_STAT_ERROR; |
| 1079 | } |
| 1080 | } |
| 1081 | } |