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