Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 17 | package com.android.camera.util; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 18 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.app.admin.DevicePolicyManager; |
| 22 | import android.content.ActivityNotFoundException; |
Sascha Haeberling | fae11a1 | 2013-08-15 14:29:49 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 24 | import android.content.ContentResolver; |
| 25 | import android.content.Context; |
| 26 | import android.content.DialogInterface; |
| 27 | import android.content.Intent; |
Doris Liu | bd1b8f9 | 2014-01-03 17:59:51 -0800 | [diff] [blame] | 28 | import android.content.res.TypedArray; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 29 | import android.graphics.Bitmap; |
| 30 | import android.graphics.BitmapFactory; |
| 31 | import android.graphics.Matrix; |
| 32 | import android.graphics.Point; |
| 33 | import android.graphics.Rect; |
| 34 | import android.graphics.RectF; |
| 35 | import android.hardware.Camera; |
| 36 | import android.hardware.Camera.CameraInfo; |
| 37 | import android.hardware.Camera.Parameters; |
| 38 | import android.hardware.Camera.Size; |
| 39 | import android.location.Location; |
| 40 | import android.net.Uri; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 41 | import android.os.ParcelFileDescriptor; |
| 42 | import android.telephony.TelephonyManager; |
| 43 | import android.util.DisplayMetrics; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 44 | import android.util.Log; |
| 45 | import android.util.TypedValue; |
Doris Liu | 0ba8eaa | 2013-10-16 12:51:02 -0700 | [diff] [blame] | 46 | import android.view.Display; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 47 | import android.view.OrientationEventListener; |
| 48 | import android.view.Surface; |
| 49 | import android.view.View; |
| 50 | import android.view.WindowManager; |
| 51 | import android.view.animation.AlphaAnimation; |
| 52 | import android.view.animation.Animation; |
Angus Kong | b40738a | 2013-06-03 14:55:34 -0700 | [diff] [blame] | 53 | import android.widget.Toast; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 54 | |
Sascha Haeberling | b7639c6 | 2013-09-09 14:42:43 -0700 | [diff] [blame] | 55 | import com.android.camera.CameraActivity; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 56 | import com.android.camera.CameraDisabledException; |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 57 | import com.android.camera2.R; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 58 | |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 59 | import java.io.Closeable; |
| 60 | import java.io.IOException; |
| 61 | import java.lang.reflect.Method; |
| 62 | import java.text.SimpleDateFormat; |
| 63 | import java.util.Date; |
| 64 | import java.util.List; |
| 65 | import java.util.Locale; |
| 66 | import java.util.StringTokenizer; |
| 67 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 68 | /** |
| 69 | * Collection of utility functions used in this package. |
| 70 | */ |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 71 | public class CameraUtil { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 72 | private static final String TAG = "Util"; |
| 73 | |
ztenghui | 16a3520 | 2013-09-23 11:35:36 -0700 | [diff] [blame] | 74 | // For calculate the best fps range for still image capture. |
| 75 | private final static int MAX_PREVIEW_FPS_TIMES_1000 = 400000; |
| 76 | private final static int PREFERRED_PREVIEW_FPS_TIMES_1000 = 30000; |
| 77 | |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 78 | // For creating crop intents. |
| 79 | public static final String KEY_RETURN_DATA = "return-data"; |
| 80 | public static final String KEY_SHOW_WHEN_LOCKED = "showWhenLocked"; |
| 81 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 82 | // Orientation hysteresis amount used in rounding, in degrees |
| 83 | public static final int ORIENTATION_HYSTERESIS = 5; |
| 84 | |
| 85 | public static final String REVIEW_ACTION = "com.android.camera.action.REVIEW"; |
| 86 | // See android.hardware.Camera.ACTION_NEW_PICTURE. |
| 87 | public static final String ACTION_NEW_PICTURE = "android.hardware.action.NEW_PICTURE"; |
| 88 | // See android.hardware.Camera.ACTION_NEW_VIDEO. |
| 89 | public static final String ACTION_NEW_VIDEO = "android.hardware.action.NEW_VIDEO"; |
| 90 | |
Dan Aminzade | fca1c5e | 2013-08-14 17:36:56 -0700 | [diff] [blame] | 91 | // Broadcast Action: The camera application has become active in picture-taking mode. |
| 92 | public static final String ACTION_CAMERA_STARTED = "com.android.camera.action.CAMERA_STARTED"; |
| 93 | // Broadcast Action: The camera application is no longer in active picture-taking mode. |
| 94 | public static final String ACTION_CAMERA_STOPPED = "com.android.camera.action.CAMERA_STOPPED"; |
| 95 | // When the camera application is active in picture-taking mode, it listens for this intent, |
| 96 | // which upon receipt will trigger the shutter to capture a new picture, as if the user had |
| 97 | // pressed the shutter button. |
| 98 | public static final String ACTION_CAMERA_SHUTTER_CLICK = |
| 99 | "com.android.camera.action.SHUTTER_CLICK"; |
| 100 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 101 | // Fields from android.hardware.Camera.Parameters |
| 102 | public static final String FOCUS_MODE_CONTINUOUS_PICTURE = "continuous-picture"; |
| 103 | public static final String RECORDING_HINT = "recording-hint"; |
| 104 | private static final String AUTO_EXPOSURE_LOCK_SUPPORTED = "auto-exposure-lock-supported"; |
| 105 | private static final String AUTO_WHITE_BALANCE_LOCK_SUPPORTED = "auto-whitebalance-lock-supported"; |
| 106 | private static final String VIDEO_SNAPSHOT_SUPPORTED = "video-snapshot-supported"; |
| 107 | public static final String SCENE_MODE_HDR = "hdr"; |
| 108 | public static final String TRUE = "true"; |
| 109 | public static final String FALSE = "false"; |
Sascha Haeberling | fae11a1 | 2013-08-15 14:29:49 -0700 | [diff] [blame] | 110 | |
| 111 | // Fields for the show-on-maps-functionality |
| 112 | private static final String MAPS_PACKAGE_NAME = "com.google.android.apps.maps"; |
| 113 | private static final String MAPS_CLASS_NAME = "com.google.android.maps.MapsActivity"; |
| 114 | |
| 115 | /** Has to be in sync with the receiving MovieActivity. */ |
| 116 | public static final String KEY_TREAT_UP_AS_BACK = "treat-up-as-back"; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 117 | |
| 118 | public static boolean isSupported(String value, List<String> supported) { |
| 119 | return supported == null ? false : supported.indexOf(value) >= 0; |
| 120 | } |
| 121 | |
| 122 | public static boolean isAutoExposureLockSupported(Parameters params) { |
| 123 | return TRUE.equals(params.get(AUTO_EXPOSURE_LOCK_SUPPORTED)); |
| 124 | } |
| 125 | |
| 126 | public static boolean isAutoWhiteBalanceLockSupported(Parameters params) { |
| 127 | return TRUE.equals(params.get(AUTO_WHITE_BALANCE_LOCK_SUPPORTED)); |
| 128 | } |
| 129 | |
| 130 | public static boolean isVideoSnapshotSupported(Parameters params) { |
| 131 | return TRUE.equals(params.get(VIDEO_SNAPSHOT_SUPPORTED)); |
| 132 | } |
| 133 | |
| 134 | public static boolean isCameraHdrSupported(Parameters params) { |
| 135 | List<String> supported = params.getSupportedSceneModes(); |
| 136 | return (supported != null) && supported.contains(SCENE_MODE_HDR); |
| 137 | } |
| 138 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 139 | public static boolean isMeteringAreaSupported(Parameters params) { |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 140 | return params.getMaxNumMeteringAreas() > 0; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 143 | public static boolean isFocusAreaSupported(Parameters params) { |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 144 | return (params.getMaxNumFocusAreas() > 0 |
| 145 | && isSupported(Parameters.FOCUS_MODE_AUTO, |
| 146 | params.getSupportedFocusModes())); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Private intent extras. Test only. |
| 150 | private static final String EXTRAS_CAMERA_FACING = |
| 151 | "android.intent.extras.CAMERA_FACING"; |
| 152 | |
| 153 | private static float sPixelDensity = 1; |
| 154 | private static ImageFileNamer sImageFileNamer; |
| 155 | |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 156 | private CameraUtil() { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | public static void initialize(Context context) { |
| 160 | DisplayMetrics metrics = new DisplayMetrics(); |
| 161 | WindowManager wm = (WindowManager) |
| 162 | context.getSystemService(Context.WINDOW_SERVICE); |
| 163 | wm.getDefaultDisplay().getMetrics(metrics); |
| 164 | sPixelDensity = metrics.density; |
| 165 | sImageFileNamer = new ImageFileNamer( |
| 166 | context.getString(R.string.image_file_name_format)); |
| 167 | } |
| 168 | |
| 169 | public static int dpToPixel(int dp) { |
| 170 | return Math.round(sPixelDensity * dp); |
| 171 | } |
| 172 | |
| 173 | // Rotates the bitmap by the specified degree. |
| 174 | // If a new bitmap is created, the original bitmap is recycled. |
| 175 | public static Bitmap rotate(Bitmap b, int degrees) { |
| 176 | return rotateAndMirror(b, degrees, false); |
| 177 | } |
| 178 | |
| 179 | // Rotates and/or mirrors the bitmap. If a new bitmap is created, the |
| 180 | // original bitmap is recycled. |
| 181 | public static Bitmap rotateAndMirror(Bitmap b, int degrees, boolean mirror) { |
| 182 | if ((degrees != 0 || mirror) && b != null) { |
| 183 | Matrix m = new Matrix(); |
| 184 | // Mirror first. |
| 185 | // horizontal flip + rotation = -rotation + horizontal flip |
| 186 | if (mirror) { |
| 187 | m.postScale(-1, 1); |
| 188 | degrees = (degrees + 360) % 360; |
| 189 | if (degrees == 0 || degrees == 180) { |
| 190 | m.postTranslate(b.getWidth(), 0); |
| 191 | } else if (degrees == 90 || degrees == 270) { |
| 192 | m.postTranslate(b.getHeight(), 0); |
| 193 | } else { |
| 194 | throw new IllegalArgumentException("Invalid degrees=" + degrees); |
| 195 | } |
| 196 | } |
| 197 | if (degrees != 0) { |
| 198 | // clockwise |
| 199 | m.postRotate(degrees, |
| 200 | (float) b.getWidth() / 2, (float) b.getHeight() / 2); |
| 201 | } |
| 202 | |
| 203 | try { |
| 204 | Bitmap b2 = Bitmap.createBitmap( |
| 205 | b, 0, 0, b.getWidth(), b.getHeight(), m, true); |
| 206 | if (b != b2) { |
| 207 | b.recycle(); |
| 208 | b = b2; |
| 209 | } |
| 210 | } catch (OutOfMemoryError ex) { |
| 211 | // We have no memory to rotate. Return the original bitmap. |
| 212 | } |
| 213 | } |
| 214 | return b; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Compute the sample size as a function of minSideLength |
| 219 | * and maxNumOfPixels. |
| 220 | * minSideLength is used to specify that minimal width or height of a |
| 221 | * bitmap. |
| 222 | * maxNumOfPixels is used to specify the maximal size in pixels that is |
| 223 | * tolerable in terms of memory usage. |
| 224 | * |
| 225 | * The function returns a sample size based on the constraints. |
| 226 | * Both size and minSideLength can be passed in as -1 |
| 227 | * which indicates no care of the corresponding constraint. |
| 228 | * The functions prefers returning a sample size that |
| 229 | * generates a smaller bitmap, unless minSideLength = -1. |
| 230 | * |
| 231 | * Also, the function rounds up the sample size to a power of 2 or multiple |
| 232 | * of 8 because BitmapFactory only honors sample size this way. |
| 233 | * For example, BitmapFactory downsamples an image by 2 even though the |
| 234 | * request is 3. So we round up the sample size to avoid OOM. |
| 235 | */ |
| 236 | public static int computeSampleSize(BitmapFactory.Options options, |
| 237 | int minSideLength, int maxNumOfPixels) { |
| 238 | int initialSize = computeInitialSampleSize(options, minSideLength, |
| 239 | maxNumOfPixels); |
| 240 | |
| 241 | int roundedSize; |
| 242 | if (initialSize <= 8) { |
| 243 | roundedSize = 1; |
| 244 | while (roundedSize < initialSize) { |
| 245 | roundedSize <<= 1; |
| 246 | } |
| 247 | } else { |
| 248 | roundedSize = (initialSize + 7) / 8 * 8; |
| 249 | } |
| 250 | |
| 251 | return roundedSize; |
| 252 | } |
| 253 | |
| 254 | private static int computeInitialSampleSize(BitmapFactory.Options options, |
| 255 | int minSideLength, int maxNumOfPixels) { |
| 256 | double w = options.outWidth; |
| 257 | double h = options.outHeight; |
| 258 | |
| 259 | int lowerBound = (maxNumOfPixels < 0) ? 1 : |
| 260 | (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels)); |
| 261 | int upperBound = (minSideLength < 0) ? 128 : |
| 262 | (int) Math.min(Math.floor(w / minSideLength), |
| 263 | Math.floor(h / minSideLength)); |
| 264 | |
| 265 | if (upperBound < lowerBound) { |
| 266 | // return the larger one when there is no overlapping zone. |
| 267 | return lowerBound; |
| 268 | } |
| 269 | |
| 270 | if (maxNumOfPixels < 0 && minSideLength < 0) { |
| 271 | return 1; |
| 272 | } else if (minSideLength < 0) { |
| 273 | return lowerBound; |
| 274 | } else { |
| 275 | return upperBound; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | public static Bitmap makeBitmap(byte[] jpegData, int maxNumOfPixels) { |
| 280 | try { |
| 281 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 282 | options.inJustDecodeBounds = true; |
| 283 | BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length, |
| 284 | options); |
| 285 | if (options.mCancel || options.outWidth == -1 |
| 286 | || options.outHeight == -1) { |
| 287 | return null; |
| 288 | } |
| 289 | options.inSampleSize = computeSampleSize( |
| 290 | options, -1, maxNumOfPixels); |
| 291 | options.inJustDecodeBounds = false; |
| 292 | |
| 293 | options.inDither = false; |
| 294 | options.inPreferredConfig = Bitmap.Config.ARGB_8888; |
| 295 | return BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length, |
| 296 | options); |
| 297 | } catch (OutOfMemoryError ex) { |
| 298 | Log.e(TAG, "Got oom exception ", ex); |
| 299 | return null; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | public static void closeSilently(Closeable c) { |
| 304 | if (c == null) return; |
| 305 | try { |
| 306 | c.close(); |
| 307 | } catch (Throwable t) { |
| 308 | // do nothing |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | public static void Assert(boolean cond) { |
| 313 | if (!cond) { |
| 314 | throw new AssertionError(); |
| 315 | } |
| 316 | } |
| 317 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 318 | private static void throwIfCameraDisabled(Activity activity) throws CameraDisabledException { |
| 319 | // Check if device policy has disabled the camera. |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 320 | DevicePolicyManager dpm = (DevicePolicyManager) activity.getSystemService( |
| 321 | Context.DEVICE_POLICY_SERVICE); |
| 322 | if (dpm.getCameraDisabled(null)) { |
| 323 | throw new CameraDisabledException(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 327 | public static void showErrorAndFinish(final Activity activity, int msgId) { |
| 328 | DialogInterface.OnClickListener buttonListener = |
| 329 | new DialogInterface.OnClickListener() { |
| 330 | @Override |
| 331 | public void onClick(DialogInterface dialog, int which) { |
| 332 | activity.finish(); |
| 333 | } |
| 334 | }; |
| 335 | TypedValue out = new TypedValue(); |
| 336 | activity.getTheme().resolveAttribute(android.R.attr.alertDialogIcon, out, true); |
| 337 | new AlertDialog.Builder(activity) |
| 338 | .setCancelable(false) |
| 339 | .setTitle(R.string.camera_error_title) |
| 340 | .setMessage(msgId) |
| 341 | .setNeutralButton(R.string.dialog_ok, buttonListener) |
| 342 | .setIcon(out.resourceId) |
| 343 | .show(); |
| 344 | } |
| 345 | |
| 346 | public static <T> T checkNotNull(T object) { |
| 347 | if (object == null) throw new NullPointerException(); |
| 348 | return object; |
| 349 | } |
| 350 | |
| 351 | public static boolean equals(Object a, Object b) { |
| 352 | return (a == b) || (a == null ? false : a.equals(b)); |
| 353 | } |
| 354 | |
| 355 | public static int nextPowerOf2(int n) { |
| 356 | n -= 1; |
| 357 | n |= n >>> 16; |
| 358 | n |= n >>> 8; |
| 359 | n |= n >>> 4; |
| 360 | n |= n >>> 2; |
| 361 | n |= n >>> 1; |
| 362 | return n + 1; |
| 363 | } |
| 364 | |
| 365 | public static float distance(float x, float y, float sx, float sy) { |
| 366 | float dx = x - sx; |
| 367 | float dy = y - sy; |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 368 | return (float) Math.sqrt(dx * dx + dy * dy); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | public static int clamp(int x, int min, int max) { |
| 372 | if (x > max) return max; |
| 373 | if (x < min) return min; |
| 374 | return x; |
| 375 | } |
| 376 | |
Igor Murashkin | 160b036 | 2013-10-14 14:57:30 -0700 | [diff] [blame] | 377 | public static float clamp(float x, float min, float max) { |
| 378 | if (x > max) return max; |
| 379 | if (x < min) return min; |
| 380 | return x; |
| 381 | } |
| 382 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 383 | public static int getDisplayRotation(Activity activity) { |
| 384 | int rotation = activity.getWindowManager().getDefaultDisplay() |
| 385 | .getRotation(); |
| 386 | switch (rotation) { |
| 387 | case Surface.ROTATION_0: return 0; |
| 388 | case Surface.ROTATION_90: return 90; |
| 389 | case Surface.ROTATION_180: return 180; |
| 390 | case Surface.ROTATION_270: return 270; |
| 391 | } |
| 392 | return 0; |
| 393 | } |
| 394 | |
Doris Liu | 0ba8eaa | 2013-10-16 12:51:02 -0700 | [diff] [blame] | 395 | /** |
| 396 | * Calculate the default orientation of the device based on the width and |
| 397 | * height of the display when rotation = 0 (i.e. natural width and height) |
| 398 | * @param activity the activity context |
| 399 | * @return whether the default orientation of the device is portrait |
| 400 | */ |
| 401 | public static boolean isDefaultToPortrait(Activity activity) { |
| 402 | Display currentDisplay = activity.getWindowManager().getDefaultDisplay(); |
| 403 | Point displaySize = new Point(); |
| 404 | currentDisplay.getSize(displaySize); |
| 405 | int orientation = currentDisplay.getRotation(); |
| 406 | int naturalWidth, naturalHeight; |
| 407 | if (orientation == Surface.ROTATION_0 || orientation == Surface.ROTATION_180) { |
| 408 | naturalWidth = displaySize.x; |
| 409 | naturalHeight = displaySize.y; |
| 410 | } else { |
| 411 | naturalWidth = displaySize.y; |
| 412 | naturalHeight = displaySize.x; |
| 413 | } |
| 414 | return naturalWidth < naturalHeight; |
| 415 | } |
| 416 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 417 | public static int getDisplayOrientation(int degrees, int cameraId) { |
| 418 | // See android.hardware.Camera.setDisplayOrientation for |
| 419 | // documentation. |
| 420 | Camera.CameraInfo info = new Camera.CameraInfo(); |
| 421 | Camera.getCameraInfo(cameraId, info); |
| 422 | int result; |
| 423 | if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { |
| 424 | result = (info.orientation + degrees) % 360; |
| 425 | result = (360 - result) % 360; // compensate the mirror |
| 426 | } else { // back-facing |
| 427 | result = (info.orientation - degrees + 360) % 360; |
| 428 | } |
| 429 | return result; |
| 430 | } |
| 431 | |
| 432 | public static int getCameraOrientation(int cameraId) { |
| 433 | Camera.CameraInfo info = new Camera.CameraInfo(); |
| 434 | Camera.getCameraInfo(cameraId, info); |
| 435 | return info.orientation; |
| 436 | } |
| 437 | |
| 438 | public static int roundOrientation(int orientation, int orientationHistory) { |
| 439 | boolean changeOrientation = false; |
| 440 | if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) { |
| 441 | changeOrientation = true; |
| 442 | } else { |
| 443 | int dist = Math.abs(orientation - orientationHistory); |
| 444 | dist = Math.min( dist, 360 - dist ); |
| 445 | changeOrientation = ( dist >= 45 + ORIENTATION_HYSTERESIS ); |
| 446 | } |
| 447 | if (changeOrientation) { |
| 448 | return ((orientation + 45) / 90 * 90) % 360; |
| 449 | } |
| 450 | return orientationHistory; |
| 451 | } |
| 452 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 453 | private static Point getDefaultDisplaySize(Activity activity, Point size) { |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 454 | activity.getWindowManager().getDefaultDisplay().getSize(size); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 455 | return size; |
| 456 | } |
| 457 | |
| 458 | public static Size getOptimalPreviewSize(Activity currentActivity, |
| 459 | List<Size> sizes, double targetRatio) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 460 | |
Ruben Brunk | 714d4d0 | 2013-10-09 16:01:16 -0700 | [diff] [blame] | 461 | Point[] points = new Point[sizes.size()]; |
| 462 | |
| 463 | int index = 0; |
| 464 | for (Size s : sizes) { |
| 465 | points[index++] = new Point(s.width, s.height); |
| 466 | } |
| 467 | |
| 468 | int optimalPickIndex = getOptimalPreviewSize(currentActivity, points, targetRatio); |
| 469 | return (optimalPickIndex == -1) ? null : sizes.get(optimalPickIndex); |
| 470 | } |
| 471 | |
| 472 | public static int getOptimalPreviewSize(Activity currentActivity, |
| 473 | Point[] sizes, double targetRatio) { |
| 474 | // Use a very small tolerance because we want an exact match. |
| 475 | final double ASPECT_TOLERANCE = 0.01; |
| 476 | if (sizes == null) return -1; |
| 477 | |
| 478 | int optimalSizeIndex = -1; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 479 | double minDiff = Double.MAX_VALUE; |
| 480 | |
| 481 | // Because of bugs of overlay and layout, we sometimes will try to |
| 482 | // layout the viewfinder in the portrait orientation and thus get the |
| 483 | // wrong size of preview surface. When we change the preview size, the |
| 484 | // new overlay will be created before the old one closed, which causes |
| 485 | // an exception. For now, just get the screen size. |
| 486 | Point point = getDefaultDisplaySize(currentActivity, new Point()); |
| 487 | int targetHeight = Math.min(point.x, point.y); |
| 488 | // Try to find an size match aspect ratio and size |
Ruben Brunk | 714d4d0 | 2013-10-09 16:01:16 -0700 | [diff] [blame] | 489 | for (int i = 0; i < sizes.length; i++) { |
| 490 | Point size = sizes[i]; |
| 491 | double ratio = (double) size.x / size.y; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 492 | if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; |
Ruben Brunk | 714d4d0 | 2013-10-09 16:01:16 -0700 | [diff] [blame] | 493 | if (Math.abs(size.y - targetHeight) < minDiff) { |
| 494 | optimalSizeIndex = i; |
| 495 | minDiff = Math.abs(size.y - targetHeight); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | // Cannot find the one match the aspect ratio. This should not happen. |
| 499 | // Ignore the requirement. |
Ruben Brunk | 714d4d0 | 2013-10-09 16:01:16 -0700 | [diff] [blame] | 500 | if (optimalSizeIndex == -1) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 501 | Log.w(TAG, "No preview size match the aspect ratio"); |
| 502 | minDiff = Double.MAX_VALUE; |
Ruben Brunk | 714d4d0 | 2013-10-09 16:01:16 -0700 | [diff] [blame] | 503 | for (int i = 0; i < sizes.length; i++) { |
| 504 | Point size = sizes[i]; |
| 505 | if (Math.abs(size.y - targetHeight) < minDiff) { |
| 506 | optimalSizeIndex = i; |
| 507 | minDiff = Math.abs(size.y - targetHeight); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
Ruben Brunk | 714d4d0 | 2013-10-09 16:01:16 -0700 | [diff] [blame] | 511 | return optimalSizeIndex; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // Returns the largest picture size which matches the given aspect ratio. |
| 515 | public static Size getOptimalVideoSnapshotPictureSize( |
| 516 | List<Size> sizes, double targetRatio) { |
| 517 | // Use a very small tolerance because we want an exact match. |
| 518 | final double ASPECT_TOLERANCE = 0.001; |
| 519 | if (sizes == null) return null; |
| 520 | |
| 521 | Size optimalSize = null; |
| 522 | |
| 523 | // Try to find a size matches aspect ratio and has the largest width |
| 524 | for (Size size : sizes) { |
| 525 | double ratio = (double) size.width / size.height; |
| 526 | if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; |
| 527 | if (optimalSize == null || size.width > optimalSize.width) { |
| 528 | optimalSize = size; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | // Cannot find one that matches the aspect ratio. This should not happen. |
| 533 | // Ignore the requirement. |
| 534 | if (optimalSize == null) { |
| 535 | Log.w(TAG, "No picture size match the aspect ratio"); |
| 536 | for (Size size : sizes) { |
| 537 | if (optimalSize == null || size.width > optimalSize.width) { |
| 538 | optimalSize = size; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | return optimalSize; |
| 543 | } |
| 544 | |
| 545 | public static void dumpParameters(Parameters parameters) { |
| 546 | String flattened = parameters.flatten(); |
| 547 | StringTokenizer tokenizer = new StringTokenizer(flattened, ";"); |
| 548 | Log.d(TAG, "Dump all camera parameters:"); |
| 549 | while (tokenizer.hasMoreElements()) { |
| 550 | Log.d(TAG, tokenizer.nextToken()); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Returns whether the device is voice-capable (meaning, it can do MMS). |
| 556 | */ |
| 557 | public static boolean isMmsCapable(Context context) { |
| 558 | TelephonyManager telephonyManager = (TelephonyManager) |
| 559 | context.getSystemService(Context.TELEPHONY_SERVICE); |
| 560 | if (telephonyManager == null) { |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | try { |
| 565 | Class<?> partypes[] = new Class[0]; |
| 566 | Method sIsVoiceCapable = TelephonyManager.class.getMethod( |
| 567 | "isVoiceCapable", partypes); |
| 568 | |
| 569 | Object arglist[] = new Object[0]; |
| 570 | Object retobj = sIsVoiceCapable.invoke(telephonyManager, arglist); |
| 571 | return (Boolean) retobj; |
| 572 | } catch (java.lang.reflect.InvocationTargetException ite) { |
| 573 | // Failure, must be another device. |
| 574 | // Assume that it is voice capable. |
| 575 | } catch (IllegalAccessException iae) { |
| 576 | // Failure, must be an other device. |
| 577 | // Assume that it is voice capable. |
| 578 | } catch (NoSuchMethodException nsme) { |
| 579 | } |
| 580 | return true; |
| 581 | } |
| 582 | |
| 583 | // This is for test only. Allow the camera to launch the specific camera. |
| 584 | public static int getCameraFacingIntentExtras(Activity currentActivity) { |
| 585 | int cameraId = -1; |
| 586 | |
| 587 | int intentCameraId = |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 588 | currentActivity.getIntent().getIntExtra(CameraUtil.EXTRAS_CAMERA_FACING, -1); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 589 | |
| 590 | if (isFrontCameraIntent(intentCameraId)) { |
| 591 | // Check if the front camera exist |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 592 | int frontCameraId = ((CameraActivity) currentActivity).getCameraProvider() |
| 593 | .getFirstFrontCameraId(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 594 | if (frontCameraId != -1) { |
| 595 | cameraId = frontCameraId; |
| 596 | } |
| 597 | } else if (isBackCameraIntent(intentCameraId)) { |
| 598 | // Check if the back camera exist |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 599 | int backCameraId = ((CameraActivity) currentActivity).getCameraProvider() |
| 600 | .getFirstBackCameraId(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 601 | if (backCameraId != -1) { |
| 602 | cameraId = backCameraId; |
| 603 | } |
| 604 | } |
| 605 | return cameraId; |
| 606 | } |
| 607 | |
| 608 | private static boolean isFrontCameraIntent(int intentCameraId) { |
| 609 | return (intentCameraId == android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT); |
| 610 | } |
| 611 | |
| 612 | private static boolean isBackCameraIntent(int intentCameraId) { |
| 613 | return (intentCameraId == android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK); |
| 614 | } |
| 615 | |
| 616 | private static int sLocation[] = new int[2]; |
| 617 | |
| 618 | // This method is not thread-safe. |
| 619 | public static boolean pointInView(float x, float y, View v) { |
| 620 | v.getLocationInWindow(sLocation); |
| 621 | return x >= sLocation[0] && x < (sLocation[0] + v.getWidth()) |
| 622 | && y >= sLocation[1] && y < (sLocation[1] + v.getHeight()); |
| 623 | } |
| 624 | |
| 625 | public static int[] getRelativeLocation(View reference, View view) { |
| 626 | reference.getLocationInWindow(sLocation); |
| 627 | int referenceX = sLocation[0]; |
| 628 | int referenceY = sLocation[1]; |
| 629 | view.getLocationInWindow(sLocation); |
| 630 | sLocation[0] -= referenceX; |
| 631 | sLocation[1] -= referenceY; |
| 632 | return sLocation; |
| 633 | } |
| 634 | |
| 635 | public static boolean isUriValid(Uri uri, ContentResolver resolver) { |
| 636 | if (uri == null) return false; |
| 637 | |
| 638 | try { |
| 639 | ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r"); |
| 640 | if (pfd == null) { |
| 641 | Log.e(TAG, "Fail to open URI. URI=" + uri); |
| 642 | return false; |
| 643 | } |
| 644 | pfd.close(); |
| 645 | } catch (IOException ex) { |
| 646 | return false; |
| 647 | } |
| 648 | return true; |
| 649 | } |
| 650 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 651 | public static void dumpRect(RectF rect, String msg) { |
| 652 | Log.v(TAG, msg + "=(" + rect.left + "," + rect.top |
| 653 | + "," + rect.right + "," + rect.bottom + ")"); |
| 654 | } |
| 655 | |
| 656 | public static void rectFToRect(RectF rectF, Rect rect) { |
| 657 | rect.left = Math.round(rectF.left); |
| 658 | rect.top = Math.round(rectF.top); |
| 659 | rect.right = Math.round(rectF.right); |
| 660 | rect.bottom = Math.round(rectF.bottom); |
| 661 | } |
| 662 | |
Doris Liu | 36ebcb1 | 2013-10-28 14:44:24 -0700 | [diff] [blame] | 663 | public static Rect rectFToRect(RectF rectF) { |
| 664 | Rect rect = new Rect(); |
| 665 | rectFToRect(rectF, rect); |
| 666 | return rect; |
| 667 | } |
| 668 | |
| 669 | public static RectF rectToRectF(Rect r) { |
| 670 | return new RectF(r.left, r.top, r.right, r.bottom); |
| 671 | } |
| 672 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 673 | public static void prepareMatrix(Matrix matrix, boolean mirror, int displayOrientation, |
| 674 | int viewWidth, int viewHeight) { |
| 675 | // Need mirror for front camera. |
| 676 | matrix.setScale(mirror ? -1 : 1, 1); |
| 677 | // This is the value for android.hardware.Camera.setDisplayOrientation. |
| 678 | matrix.postRotate(displayOrientation); |
| 679 | // Camera driver coordinates range from (-1000, -1000) to (1000, 1000). |
| 680 | // UI coordinates range from (0, 0) to (width, height). |
| 681 | matrix.postScale(viewWidth / 2000f, viewHeight / 2000f); |
| 682 | matrix.postTranslate(viewWidth / 2f, viewHeight / 2f); |
| 683 | } |
| 684 | |
Doris Liu | 36ebcb1 | 2013-10-28 14:44:24 -0700 | [diff] [blame] | 685 | public static void prepareMatrix(Matrix matrix, boolean mirror, int displayOrientation, |
| 686 | Rect previewRect) { |
| 687 | // Need mirror for front camera. |
| 688 | matrix.setScale(mirror ? -1 : 1, 1); |
| 689 | // This is the value for android.hardware.Camera.setDisplayOrientation. |
| 690 | matrix.postRotate(displayOrientation); |
| 691 | |
| 692 | // Camera driver coordinates range from (-1000, -1000) to (1000, 1000). |
| 693 | // We need to map camera driver coordinates to preview rect coordinates |
| 694 | Matrix mapping = new Matrix(); |
| 695 | mapping.setRectToRect(new RectF(-1000, -1000, 1000, 1000), rectToRectF(previewRect), |
| 696 | Matrix.ScaleToFit.FILL); |
| 697 | matrix.setConcat(mapping, matrix); |
| 698 | } |
| 699 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 700 | public static String createJpegName(long dateTaken) { |
| 701 | synchronized (sImageFileNamer) { |
| 702 | return sImageFileNamer.generateName(dateTaken); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | public static void broadcastNewPicture(Context context, Uri uri) { |
| 707 | context.sendBroadcast(new Intent(ACTION_NEW_PICTURE, uri)); |
| 708 | // Keep compatibility |
| 709 | context.sendBroadcast(new Intent("com.android.camera.NEW_PICTURE", uri)); |
| 710 | } |
| 711 | |
| 712 | public static void fadeIn(View view, float startAlpha, float endAlpha, long duration) { |
| 713 | if (view.getVisibility() == View.VISIBLE) return; |
| 714 | |
| 715 | view.setVisibility(View.VISIBLE); |
| 716 | Animation animation = new AlphaAnimation(startAlpha, endAlpha); |
| 717 | animation.setDuration(duration); |
| 718 | view.startAnimation(animation); |
| 719 | } |
| 720 | |
| 721 | public static void fadeIn(View view) { |
| 722 | fadeIn(view, 0F, 1F, 400); |
| 723 | |
| 724 | // We disabled the button in fadeOut(), so enable it here. |
| 725 | view.setEnabled(true); |
| 726 | } |
| 727 | |
| 728 | public static void fadeOut(View view) { |
| 729 | if (view.getVisibility() != View.VISIBLE) return; |
| 730 | |
| 731 | // Since the button is still clickable before fade-out animation |
| 732 | // ends, we disable the button first to block click. |
| 733 | view.setEnabled(false); |
| 734 | Animation animation = new AlphaAnimation(1F, 0F); |
| 735 | animation.setDuration(400); |
| 736 | view.startAnimation(animation); |
| 737 | view.setVisibility(View.GONE); |
| 738 | } |
| 739 | |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 740 | public static int getJpegRotation(CameraActivity activity, int cameraId, int orientation) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 741 | // See android.hardware.Camera.Parameters.setRotation for |
| 742 | // documentation. |
| 743 | int rotation = 0; |
| 744 | if (orientation != OrientationEventListener.ORIENTATION_UNKNOWN) { |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 745 | CameraInfo info = activity.getCameraProvider().getCameraInfo()[cameraId]; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 746 | if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { |
| 747 | rotation = (info.orientation - orientation + 360) % 360; |
| 748 | } else { // back-facing camera |
| 749 | rotation = (info.orientation + orientation) % 360; |
| 750 | } |
| 751 | } |
| 752 | return rotation; |
| 753 | } |
| 754 | |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 755 | /** |
| 756 | * Down-samples a jpeg byte array. |
| 757 | * @param data a byte array of jpeg data |
| 758 | * @param downSampleFactor down-sample factor |
| 759 | * @return decoded and down-sampled bitmap |
| 760 | */ |
| 761 | public static Bitmap downSample(final byte[] data, int downSampleFactor) { |
| 762 | final BitmapFactory.Options opts = new BitmapFactory.Options(); |
| 763 | // Downsample the image |
| 764 | opts.inSampleSize = downSampleFactor; |
| 765 | return BitmapFactory.decodeByteArray(data, 0, data.length, opts); |
| 766 | } |
| 767 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 768 | public static void setGpsParameters(Parameters parameters, Location loc) { |
| 769 | // Clear previous GPS location from the parameters. |
| 770 | parameters.removeGpsData(); |
| 771 | |
| 772 | // We always encode GpsTimeStamp |
| 773 | parameters.setGpsTimestamp(System.currentTimeMillis() / 1000); |
| 774 | |
| 775 | // Set GPS location. |
| 776 | if (loc != null) { |
| 777 | double lat = loc.getLatitude(); |
| 778 | double lon = loc.getLongitude(); |
| 779 | boolean hasLatLon = (lat != 0.0d) || (lon != 0.0d); |
| 780 | |
| 781 | if (hasLatLon) { |
| 782 | Log.d(TAG, "Set gps location"); |
| 783 | parameters.setGpsLatitude(lat); |
| 784 | parameters.setGpsLongitude(lon); |
| 785 | parameters.setGpsProcessingMethod(loc.getProvider().toUpperCase()); |
| 786 | if (loc.hasAltitude()) { |
| 787 | parameters.setGpsAltitude(loc.getAltitude()); |
| 788 | } else { |
| 789 | // for NETWORK_PROVIDER location provider, we may have |
| 790 | // no altitude information, but the driver needs it, so |
| 791 | // we fake one. |
| 792 | parameters.setGpsAltitude(0); |
| 793 | } |
| 794 | if (loc.getTime() != 0) { |
| 795 | // Location.getTime() is UTC in milliseconds. |
| 796 | // gps-timestamp is UTC in seconds. |
| 797 | long utcTimeSeconds = loc.getTime() / 1000; |
| 798 | parameters.setGpsTimestamp(utcTimeSeconds); |
| 799 | } |
| 800 | } else { |
| 801 | loc = null; |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
ztenghui | 16a3520 | 2013-09-23 11:35:36 -0700 | [diff] [blame] | 806 | /** |
| 807 | * For still image capture, we need to get the right fps range such that the |
| 808 | * camera can slow down the framerate to allow for less-noisy/dark |
| 809 | * viewfinder output in dark conditions. |
| 810 | * |
| 811 | * @param params Camera's parameters. |
| 812 | * @return null if no appropiate fps range can't be found. Otherwise, return |
| 813 | * the right range. |
| 814 | */ |
| 815 | public static int[] getPhotoPreviewFpsRange(Parameters params) { |
Ruben Brunk | 2a74dfd | 2013-10-29 18:28:32 -0700 | [diff] [blame] | 816 | return getPhotoPreviewFpsRange(params.getSupportedPreviewFpsRange()); |
| 817 | } |
| 818 | |
| 819 | public static int[] getPhotoPreviewFpsRange(List<int[]> frameRates) { |
ztenghui | 16a3520 | 2013-09-23 11:35:36 -0700 | [diff] [blame] | 820 | if (frameRates.size() == 0) { |
| 821 | Log.e(TAG, "No suppoted frame rates returned!"); |
| 822 | return null; |
| 823 | } |
| 824 | |
| 825 | // Find the lowest min rate in supported ranges who can cover 30fps. |
| 826 | int lowestMinRate = MAX_PREVIEW_FPS_TIMES_1000; |
| 827 | for (int[] rate : frameRates) { |
| 828 | int minFps = rate[Parameters.PREVIEW_FPS_MIN_INDEX]; |
| 829 | int maxFps = rate[Parameters.PREVIEW_FPS_MAX_INDEX]; |
| 830 | if (maxFps >= PREFERRED_PREVIEW_FPS_TIMES_1000 && |
| 831 | minFps <= PREFERRED_PREVIEW_FPS_TIMES_1000 && |
| 832 | minFps < lowestMinRate) { |
| 833 | lowestMinRate = minFps; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | // Find all the modes with the lowest min rate found above, the pick the |
| 838 | // one with highest max rate. |
| 839 | int resultIndex = -1; |
| 840 | int highestMaxRate = 0; |
| 841 | for (int i = 0; i < frameRates.size(); i++) { |
| 842 | int[] rate = frameRates.get(i); |
| 843 | int minFps = rate[Parameters.PREVIEW_FPS_MIN_INDEX]; |
| 844 | int maxFps = rate[Parameters.PREVIEW_FPS_MAX_INDEX]; |
| 845 | if (minFps == lowestMinRate && highestMaxRate < maxFps) { |
| 846 | highestMaxRate = maxFps; |
| 847 | resultIndex = i; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | if (resultIndex >= 0) { |
| 852 | return frameRates.get(resultIndex); |
| 853 | } |
| 854 | Log.e(TAG, "Can't find an appropiate frame rate range!"); |
| 855 | return null; |
| 856 | } |
Angus Kong | d82eae2 | 2013-06-11 12:00:15 -0700 | [diff] [blame] | 857 | |
| 858 | public static int[] getMaxPreviewFpsRange(Parameters params) { |
| 859 | List<int[]> frameRates = params.getSupportedPreviewFpsRange(); |
| 860 | if (frameRates != null && frameRates.size() > 0) { |
| 861 | // The list is sorted. Return the last element. |
| 862 | return frameRates.get(frameRates.size() - 1); |
| 863 | } |
| 864 | return new int[0]; |
| 865 | } |
| 866 | |
Angus Kong | 20fad24 | 2013-11-11 18:23:46 -0800 | [diff] [blame] | 867 | public static void throwIfCameraDisabled(Context context) throws CameraDisabledException { |
| 868 | // Check if device policy has disabled the camera. |
| 869 | DevicePolicyManager dpm = |
| 870 | (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 871 | if (dpm.getCameraDisabled(null)) { |
| 872 | throw new CameraDisabledException(); |
| 873 | } |
| 874 | } |
| 875 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 876 | private static class ImageFileNamer { |
Igor Murashkin | 160b036 | 2013-10-14 14:57:30 -0700 | [diff] [blame] | 877 | private final SimpleDateFormat mFormat; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 878 | |
| 879 | // The date (in milliseconds) used to generate the last name. |
| 880 | private long mLastDate; |
| 881 | |
| 882 | // Number of names generated for the same second. |
| 883 | private int mSameSecondCount; |
| 884 | |
| 885 | public ImageFileNamer(String format) { |
| 886 | mFormat = new SimpleDateFormat(format); |
| 887 | } |
| 888 | |
| 889 | public String generateName(long dateTaken) { |
| 890 | Date date = new Date(dateTaken); |
| 891 | String result = mFormat.format(date); |
| 892 | |
| 893 | // If the last name was generated for the same second, |
| 894 | // we append _1, _2, etc to the name. |
| 895 | if (dateTaken / 1000 == mLastDate / 1000) { |
| 896 | mSameSecondCount++; |
| 897 | result += "_" + mSameSecondCount; |
| 898 | } else { |
| 899 | mLastDate = dateTaken; |
| 900 | mSameSecondCount = 0; |
| 901 | } |
| 902 | |
| 903 | return result; |
| 904 | } |
| 905 | } |
Angus Kong | b40738a | 2013-06-03 14:55:34 -0700 | [diff] [blame] | 906 | |
Sascha Haeberling | b7639c6 | 2013-09-09 14:42:43 -0700 | [diff] [blame] | 907 | public static void playVideo(Activity activity, Uri uri, String title) { |
Angus Kong | b40738a | 2013-06-03 14:55:34 -0700 | [diff] [blame] | 908 | try { |
ztenghui | 98b2a28 | 2013-10-11 17:15:40 -0700 | [diff] [blame] | 909 | boolean isSecureCamera = ((CameraActivity)activity).isSecureCamera(); |
| 910 | if (!isSecureCamera) { |
Mangesh Ghiware | 2d939fe | 2013-10-14 21:19:22 -0700 | [diff] [blame] | 911 | Intent intent = IntentHelper.getVideoPlayerIntent(activity, uri) |
ztenghui | 98b2a28 | 2013-10-11 17:15:40 -0700 | [diff] [blame] | 912 | .putExtra(Intent.EXTRA_TITLE, title) |
| 913 | .putExtra(KEY_TREAT_UP_AS_BACK, true); |
| 914 | activity.startActivityForResult(intent, CameraActivity.REQ_CODE_DONT_SWITCH_TO_PREVIEW); |
| 915 | } else { |
| 916 | // In order not to send out any intent to be intercepted and |
| 917 | // show the lock screen immediately, we just let the secure |
| 918 | // camera activity finish. |
| 919 | activity.finish(); |
| 920 | } |
Angus Kong | b40738a | 2013-06-03 14:55:34 -0700 | [diff] [blame] | 921 | } catch (ActivityNotFoundException e) { |
Sascha Haeberling | b7639c6 | 2013-09-09 14:42:43 -0700 | [diff] [blame] | 922 | Toast.makeText(activity, activity.getString(R.string.video_err), |
Angus Kong | b40738a | 2013-06-03 14:55:34 -0700 | [diff] [blame] | 923 | Toast.LENGTH_SHORT).show(); |
| 924 | } |
| 925 | } |
Sascha Haeberling | fae11a1 | 2013-08-15 14:29:49 -0700 | [diff] [blame] | 926 | |
| 927 | /** |
| 928 | * Starts GMM with the given location shown. If this fails, and GMM could |
| 929 | * not be found, we use a geo intent as a fallback. |
| 930 | * |
Sascha Haeberling | b7639c6 | 2013-09-09 14:42:43 -0700 | [diff] [blame] | 931 | * @param activity the activity to use for launching the Maps intent. |
Sascha Haeberling | fae11a1 | 2013-08-15 14:29:49 -0700 | [diff] [blame] | 932 | * @param latLong a 2-element array containing {latitude/longitude}. |
| 933 | */ |
Sascha Haeberling | b7639c6 | 2013-09-09 14:42:43 -0700 | [diff] [blame] | 934 | public static void showOnMap(Activity activity, double[] latLong) { |
Sascha Haeberling | fae11a1 | 2013-08-15 14:29:49 -0700 | [diff] [blame] | 935 | try { |
| 936 | // We don't use "geo:latitude,longitude" because it only centers |
| 937 | // the MapView to the specified location, but we need a marker |
| 938 | // for further operations (routing to/from). |
| 939 | // The q=(lat, lng) syntax is suggested by geo-team. |
| 940 | String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?f=q&q=(%f,%f)", |
| 941 | latLong[0], latLong[1]); |
| 942 | ComponentName compName = new ComponentName(MAPS_PACKAGE_NAME, |
| 943 | MAPS_CLASS_NAME); |
| 944 | Intent mapsIntent = new Intent(Intent.ACTION_VIEW, |
| 945 | Uri.parse(uri)).setComponent(compName); |
Sascha Haeberling | b7639c6 | 2013-09-09 14:42:43 -0700 | [diff] [blame] | 946 | activity.startActivityForResult(mapsIntent, |
| 947 | CameraActivity.REQ_CODE_DONT_SWITCH_TO_PREVIEW); |
Sascha Haeberling | fae11a1 | 2013-08-15 14:29:49 -0700 | [diff] [blame] | 948 | } catch (ActivityNotFoundException e) { |
| 949 | // Use the "geo intent" if no GMM is installed |
| 950 | Log.e(TAG, "GMM activity not found!", e); |
| 951 | String url = String.format(Locale.ENGLISH, "geo:%f,%f", latLong[0], latLong[1]); |
| 952 | Intent mapsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
Sascha Haeberling | b7639c6 | 2013-09-09 14:42:43 -0700 | [diff] [blame] | 953 | activity.startActivity(mapsIntent); |
Sascha Haeberling | fae11a1 | 2013-08-15 14:29:49 -0700 | [diff] [blame] | 954 | } |
| 955 | } |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 956 | |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 957 | /** |
| 958 | * Dumps the stack trace. |
| 959 | * |
| 960 | * @param level How many levels of the stack are dumped. 0 means all. |
| 961 | * @return A {@link java.lang.String} of all the output with newline |
| 962 | * between each. |
| 963 | */ |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 964 | public static String dumpStackTrace(int level) { |
| 965 | StackTraceElement[] elems = Thread.currentThread().getStackTrace(); |
| 966 | // Ignore the first 3 elements. |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 967 | level = (level == 0 ? elems.length : Math.min(level + 3, elems.length)); |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 968 | String ret = new String(); |
| 969 | for (int i = 3; i < level; i++) { |
Angus Kong | b21215a | 2013-09-20 18:41:46 -0700 | [diff] [blame] | 970 | ret = ret + "\t" + elems[i].toString() + '\n'; |
Angus Kong | 7d2388d | 2013-09-18 23:56:01 -0700 | [diff] [blame] | 971 | } |
| 972 | return ret; |
| 973 | } |
Doris Liu | bd1b8f9 | 2014-01-03 17:59:51 -0800 | [diff] [blame] | 974 | |
| 975 | |
| 976 | /** |
| 977 | * Gets the theme color of a specific mode. |
| 978 | * |
| 979 | * @param modeIndex index of the mode |
| 980 | * @param context current context |
| 981 | * @return theme color of the mode if input index is valid, otherwise 0 |
| 982 | */ |
| 983 | public static int getCameraThemeColorId(int modeIndex, Context context) { |
| 984 | |
| 985 | // Find the theme color using id from the color array |
| 986 | TypedArray colorRes = context.getResources() |
| 987 | .obtainTypedArray(R.array.camera_mode_theme_color); |
| 988 | if (modeIndex >= colorRes.length() || modeIndex < 0) { |
| 989 | // Mode index not found |
| 990 | Log.e(TAG, "Invalid mode index: " + modeIndex); |
| 991 | return 0; |
| 992 | } |
| 993 | return colorRes.getResourceId(modeIndex, 0); |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * Gets the mode icon resource id of a specific mode. |
| 998 | * |
| 999 | * @param modeIndex index of the mode |
| 1000 | * @param context current context |
| 1001 | * @return icon resource id if the index is valid, otherwise 0 |
| 1002 | */ |
| 1003 | public static int getCameraModeIconResId(int modeIndex, Context context) { |
| 1004 | // Find the camera mode icon using id |
| 1005 | TypedArray cameraModesIcons = context.getResources() |
| 1006 | .obtainTypedArray(R.array.camera_mode_icon); |
| 1007 | if (modeIndex >= cameraModesIcons.length() || modeIndex < 0) { |
| 1008 | // Mode index not found |
| 1009 | Log.e(TAG, "Invalid mode index: " + modeIndex); |
| 1010 | return 0; |
| 1011 | } |
| 1012 | return cameraModesIcons.getResourceId(modeIndex, 0); |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * Gets the mode text of a specific mode. |
| 1017 | * |
| 1018 | * @param modeIndex index of the mode |
| 1019 | * @param context current context |
| 1020 | * @return mode text if the index is valid, otherwise a new empty string |
| 1021 | */ |
| 1022 | public static String getCameraModeText(int modeIndex, Context context) { |
| 1023 | // Find the camera mode icon using id |
| 1024 | String[] cameraModesText = context.getResources() |
| 1025 | .getStringArray(R.array.camera_mode_text); |
| 1026 | if (modeIndex < 0 || modeIndex >= cameraModesText.length) { |
| 1027 | Log.e(TAG, "Invalid mode index: " + modeIndex); |
| 1028 | return new String(); |
| 1029 | } |
| 1030 | return cameraModesText[modeIndex]; |
| 1031 | } |
Erin Dahlgren | fecd723 | 2014-01-16 13:29:42 -0800 | [diff] [blame] | 1032 | |
| 1033 | /** |
Spike Sprague | 79718f6 | 2014-01-28 18:48:22 -0800 | [diff] [blame] | 1034 | * Gets the mode content description of a specific mode. |
| 1035 | * |
| 1036 | * @param modeIndex index of the mode |
| 1037 | * @param context current context |
| 1038 | * @return mode content description if the index is valid, otherwise a new empty string |
| 1039 | */ |
| 1040 | public static String getCameraModeContentDescription(int modeIndex, Context context) { |
| 1041 | String[] cameraModesDesc = context.getResources() |
| 1042 | .getStringArray(R.array.camera_mode_content_description); |
| 1043 | if (modeIndex < 0 || modeIndex >= cameraModesDesc.length) { |
| 1044 | Log.e(TAG, "Invalid mode index: " + modeIndex); |
| 1045 | return new String(); |
| 1046 | } |
| 1047 | return cameraModesDesc[modeIndex]; |
| 1048 | } |
| 1049 | |
| 1050 | /** |
Erin Dahlgren | fecd723 | 2014-01-16 13:29:42 -0800 | [diff] [blame] | 1051 | * Gets the shutter icon res id for a specific mode. |
| 1052 | * |
| 1053 | * @param modeIndex index of the mode |
| 1054 | * @param context current context |
| 1055 | * @return mode shutter icon id if the index is valid, otherwise 0. |
| 1056 | */ |
| 1057 | public static int getCameraShutterIconId(int modeIndex, Context context) { |
| 1058 | // Find the camera mode icon using id |
| 1059 | TypedArray shutterIcons = context.getResources() |
| 1060 | .obtainTypedArray(R.array.camera_mode_shutter_icon); |
| 1061 | if (modeIndex < 0 || modeIndex >= shutterIcons.length()) { |
| 1062 | Log.e(TAG, "Invalid mode index: " + modeIndex); |
| 1063 | return 0; |
| 1064 | } |
| 1065 | return shutterIcons.getResourceId(modeIndex, 0); |
| 1066 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1067 | } |