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