Util.java revision db0757dc9c627e7c5206104664028e386ade7d03
1819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott/*
2819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott * Copyright (C) 2009 The Android Open Source Project
3819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott *
4819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott * Licensed under the Apache License, Version 2.0 (the "License");
5819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott * you may not use this file except in compliance with the License.
6819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott * You may obtain a copy of the License at
7819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott *
8819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott *      http://www.apache.org/licenses/LICENSE-2.0
9819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott *
10819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott * Unless required by applicable law or agreed to in writing, software
11819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott * distributed under the License is distributed on an "AS IS" BASIS,
12819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott * See the License for the specific language governing permissions and
14819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott * limitations under the License.
15819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott */
16819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
17819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottpackage com.android.camera;
18819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
1973b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerottimport android.app.Activity;
20819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.app.AlertDialog;
21819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.content.Context;
22819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.content.DialogInterface;
23819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.graphics.Bitmap;
24819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.graphics.BitmapFactory;
25819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.graphics.Matrix;
26819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.hardware.Camera;
27819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.hardware.Camera.Parameters;
28819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.hardware.Camera.Size;
2975895e73379aa26a3d4135c772af4ecb8a79b4c9Marcus Hagerottimport android.telephony.TelephonyManager;
3069c182afb0e6d82a341a28b4317aa703af768906Gary Maiimport android.util.Log;
3169c182afb0e6d82a341a28b4317aa703af768906Gary Maiimport android.view.Display;
32819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.view.Surface;
33819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.view.View;
34819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.view.animation.Animation;
35819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport android.view.animation.TranslateAnimation;
36819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
37819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport java.io.Closeable;
38819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport java.lang.reflect.Method;
39819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport java.util.List;
40819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerottimport java.util.StringTokenizer;
41819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
42819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott/**
43819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott * Collection of utility functions used in this package.
4473b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott */
4573b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerottpublic class Util {
46819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    private static final String TAG = "Util";
47819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    public static final int DIRECTION_LEFT = 0;
48819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    public static final int DIRECTION_RIGHT = 1;
49819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    public static final int DIRECTION_UP = 2;
50819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    public static final int DIRECTION_DOWN = 3;
51819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
52819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    public static final String REVIEW_ACTION = "com.android.camera.action.REVIEW";
53819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
54819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    // Private intent extras. Test only.
55819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    public static final String EXTRAS_CAMERA_FACING =
56819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        "android.intent.extras.CAMERA_FACING";
57819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
5875895e73379aa26a3d4135c772af4ecb8a79b4c9Marcus Hagerott    private Util() {
59819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    }
60819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
61819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    // Rotates the bitmap by the specified degree.
62819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    // If a new bitmap is created, the original bitmap is recycled.
63819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    public static Bitmap rotate(Bitmap b, int degrees) {
64819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        return rotateAndMirror(b, degrees, false);
65819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    }
66819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
67819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    // Rotates and/or mirrors the bitmap. If a new bitmap is created, the
68819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    // original bitmap is recycled.
69819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    public static Bitmap rotateAndMirror(Bitmap b, int degrees, boolean mirror) {
70819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        if ((degrees != 0 || mirror) && b != null) {
71819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            Matrix m = new Matrix();
72819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            m.setRotate(degrees,
73819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                    (float) b.getWidth() / 2, (float) b.getHeight() / 2);
74819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            if (mirror) {
75819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                m.postScale(-1, 1);
76819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                degrees = (degrees + 360) % 360;
777217e6986c333aebb885b0a07a0a9344ef7a814cMarcus Hagerott                if (degrees == 0 || degrees == 180) {
787217e6986c333aebb885b0a07a0a9344ef7a814cMarcus Hagerott                    m.postTranslate((float) b.getWidth(), 0);
79819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                } else if (degrees == 90 || degrees == 270) {
80819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                    m.postTranslate((float) b.getHeight(), 0);
81819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                } else {
82819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                    throw new IllegalArgumentException("Invalid degrees=" + degrees);
83819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                }
84819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            }
85819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
86819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            try {
87819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                Bitmap b2 = Bitmap.createBitmap(
88819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                        b, 0, 0, b.getWidth(), b.getHeight(), m, true);
89819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                if (b != b2) {
90819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                    b.recycle();
91819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                    b = b2;
92819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                }
93819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            } catch (OutOfMemoryError ex) {
94819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                // We have no memory to rotate. Return the original bitmap.
95819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            }
96819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        }
97819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        return b;
98819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    }
99819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
10075895e73379aa26a3d4135c772af4ecb8a79b4c9Marcus Hagerott    /*
101e7a71cbbe8929a9d8a7ca0a3b5668646d49a611aMarcus Hagerott     * Compute the sample size as a function of minSideLength
102a181ca6021333cbb96ff9a6abeec9e64ea19c7f9Marcus Hagerott     * and maxNumOfPixels.
103a181ca6021333cbb96ff9a6abeec9e64ea19c7f9Marcus Hagerott     * minSideLength is used to specify that minimal width or height of a
104a181ca6021333cbb96ff9a6abeec9e64ea19c7f9Marcus Hagerott     * bitmap.
10575895e73379aa26a3d4135c772af4ecb8a79b4c9Marcus Hagerott     * maxNumOfPixels is used to specify the maximal size in pixels that is
106e7a71cbbe8929a9d8a7ca0a3b5668646d49a611aMarcus Hagerott     * tolerable in terms of memory usage.
107e7a71cbbe8929a9d8a7ca0a3b5668646d49a611aMarcus Hagerott     *
108e7a71cbbe8929a9d8a7ca0a3b5668646d49a611aMarcus Hagerott     * The function returns a sample size based on the constraints.
109e7a71cbbe8929a9d8a7ca0a3b5668646d49a611aMarcus Hagerott     * Both size and minSideLength can be passed in as -1
110e7a71cbbe8929a9d8a7ca0a3b5668646d49a611aMarcus Hagerott     * which indicates no care of the corresponding constraint.
111819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott     * The functions prefers returning a sample size that
11275895e73379aa26a3d4135c772af4ecb8a79b4c9Marcus Hagerott     * generates a smaller bitmap, unless minSideLength = -1.
113819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott     *
114819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott     * Also, the function rounds up the sample size to a power of 2 or multiple
11573b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott     * of 8 because BitmapFactory only honors sample size this way.
11673b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott     * For example, BitmapFactory downsamples an image by 2 even though the
11773b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott     * request is 3. So we round up the sample size to avoid OOM.
11873b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott     */
11973b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott    public static int computeSampleSize(BitmapFactory.Options options,
12073b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott            int minSideLength, int maxNumOfPixels) {
12173b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott        int initialSize = computeInitialSampleSize(options, minSideLength,
12273b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott                maxNumOfPixels);
12373b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott
12473b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott        int roundedSize;
12573b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott        if (initialSize <= 8) {
12673b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott            roundedSize = 1;
127819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            while (roundedSize < initialSize) {
128819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                roundedSize <<= 1;
129819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            }
130e7a71cbbe8929a9d8a7ca0a3b5668646d49a611aMarcus Hagerott        } else {
131819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            roundedSize = (initialSize + 7) / 8 * 8;
13275895e73379aa26a3d4135c772af4ecb8a79b4c9Marcus Hagerott        }
133819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
134c2093f312f71e730e1cc4daaa305782fda394215Marcus Hagerott        return roundedSize;
135819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    }
136819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
137819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    private static int computeInitialSampleSize(BitmapFactory.Options options,
138819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            int minSideLength, int maxNumOfPixels) {
139819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        double w = options.outWidth;
140819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        double h = options.outHeight;
141819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
142819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        int lowerBound = (maxNumOfPixels < 0) ? 1 :
143819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
144819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        int upperBound = (minSideLength < 0) ? 128 :
145819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                (int) Math.min(Math.floor(w / minSideLength),
146819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                Math.floor(h / minSideLength));
147819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
148819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        if (upperBound < lowerBound) {
149819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            // return the larger one when there is no overlapping zone.
1507217e6986c333aebb885b0a07a0a9344ef7a814cMarcus Hagerott            return lowerBound;
1517217e6986c333aebb885b0a07a0a9344ef7a814cMarcus Hagerott        }
1527217e6986c333aebb885b0a07a0a9344ef7a814cMarcus Hagerott
153819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        if (maxNumOfPixels < 0 && minSideLength < 0) {
15475895e73379aa26a3d4135c772af4ecb8a79b4c9Marcus Hagerott            return 1;
15575895e73379aa26a3d4135c772af4ecb8a79b4c9Marcus Hagerott        } else if (minSideLength < 0) {
156819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            return lowerBound;
15775895e73379aa26a3d4135c772af4ecb8a79b4c9Marcus Hagerott        } else {
158819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            return upperBound;
159819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        }
160819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    }
161819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
162819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    public static Bitmap makeBitmap(byte[] jpegData, int maxNumOfPixels) {
163819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott        try {
164819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            BitmapFactory.Options options = new BitmapFactory.Options();
165819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            options.inJustDecodeBounds = true;
166819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length,
167819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                    options);
168819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            if (options.mCancel || options.outWidth == -1
16973b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott                    || options.outHeight == -1) {
170819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                return null;
171819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            }
172819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            options.inSampleSize = computeSampleSize(
173819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                    options, -1, maxNumOfPixels);
174819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            options.inJustDecodeBounds = false;
17573b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott
17673b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott            options.inDither = false;
17773b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
178c2093f312f71e730e1cc4daaa305782fda394215Marcus Hagerott            return BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length,
17973b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott                    options);
18073b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott        } catch (OutOfMemoryError ex) {
18173b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott            Log.e(TAG, "Got oom exception ", ex);
18273b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott            return null;
18373b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott        }
18473b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott    }
18573b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott
18673b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott    public static void closeSilently(Closeable c) {
18773b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott        if (c == null) return;
18873b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott        try {
18973b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott            c.close();
19073b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott        } catch (Throwable t) {
191d3869c7ab4ff85fc9e3a511ea6644be395698321Marcus Hagerott            // do nothing
192d3869c7ab4ff85fc9e3a511ea6644be395698321Marcus Hagerott        }
193d3869c7ab4ff85fc9e3a511ea6644be395698321Marcus Hagerott    }
19473b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott
19573b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott    public static void Assert(boolean cond) {
19673b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott        if (!cond) {
19773b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott            throw new AssertionError();
19873b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott        }
19973b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott    }
20073b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott
20173b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott    public static void showFatalErrorAndFinish(
20273b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott            final Activity activity, String title, String message) {
20373b283f03cd2697ad0098711d62cd0956037952fMarcus Hagerott        DialogInterface.OnClickListener buttonListener =
204819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                new DialogInterface.OnClickListener() {
205819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            public void onClick(DialogInterface dialog, int which) {
206819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                activity.finish();
207819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott            }
2084b11232fdf9e11e73b2ffa41b40f3e153ec3912bMarcus Hagerott        };
2094b11232fdf9e11e73b2ffa41b40f3e153ec3912bMarcus Hagerott        new AlertDialog.Builder(activity)
2104b11232fdf9e11e73b2ffa41b40f3e153ec3912bMarcus Hagerott                .setCancelable(false)
211819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                .setIconAttribute(android.R.attr.alertDialogIcon)
212819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                .setTitle(title)
213819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott                .setMessage(message)
21475895e73379aa26a3d4135c772af4ecb8a79b4c9Marcus Hagerott                .setNeutralButton(R.string.details_ok, buttonListener)
21575895e73379aa26a3d4135c772af4ecb8a79b4c9Marcus Hagerott                .show();
21675895e73379aa26a3d4135c772af4ecb8a79b4c9Marcus Hagerott    }
217819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott
218819214d472fdadf3d69cb4580e238506194ed30eMarcus Hagerott    public static Animation slideOut(View view, int to) {
219        view.setVisibility(View.INVISIBLE);
220        Animation anim;
221        switch (to) {
222            case DIRECTION_LEFT:
223                anim = new TranslateAnimation(0, -view.getWidth(), 0, 0);
224                break;
225            case DIRECTION_RIGHT:
226                anim = new TranslateAnimation(0, view.getWidth(), 0, 0);
227                break;
228            case DIRECTION_UP:
229                anim = new TranslateAnimation(0, 0, 0, -view.getHeight());
230                break;
231            case DIRECTION_DOWN:
232                anim = new TranslateAnimation(0, 0, 0, view.getHeight());
233                break;
234            default:
235                throw new IllegalArgumentException(Integer.toString(to));
236        }
237        anim.setDuration(500);
238        view.startAnimation(anim);
239        return anim;
240    }
241
242    public static Animation slideIn(View view, int from) {
243        view.setVisibility(View.VISIBLE);
244        Animation anim;
245        switch (from) {
246            case DIRECTION_LEFT:
247                anim = new TranslateAnimation(-view.getWidth(), 0, 0, 0);
248                break;
249            case DIRECTION_RIGHT:
250                anim = new TranslateAnimation(view.getWidth(), 0, 0, 0);
251                break;
252            case DIRECTION_UP:
253                anim = new TranslateAnimation(0, 0, -view.getHeight(), 0);
254                break;
255            case DIRECTION_DOWN:
256                anim = new TranslateAnimation(0, 0, view.getHeight(), 0);
257                break;
258            default:
259                throw new IllegalArgumentException(Integer.toString(from));
260        }
261        anim.setDuration(500);
262        view.startAnimation(anim);
263        return anim;
264    }
265
266    public static <T> T checkNotNull(T object) {
267        if (object == null) throw new NullPointerException();
268        return object;
269    }
270
271    public static boolean equals(Object a, Object b) {
272        return (a == b) || (a == null ? false : a.equals(b));
273    }
274
275    public static boolean isPowerOf2(int n) {
276        return (n & -n) == n;
277    }
278
279    public static int nextPowerOf2(int n) {
280        n -= 1;
281        n |= n >>> 16;
282        n |= n >>> 8;
283        n |= n >>> 4;
284        n |= n >>> 2;
285        n |= n >>> 1;
286        return n + 1;
287    }
288
289    public static float distance(float x, float y, float sx, float sy) {
290        float dx = x - sx;
291        float dy = y - sy;
292        return (float) Math.sqrt(dx * dx + dy * dy);
293    }
294
295    public static int clamp(int x, int min, int max) {
296        if (x > max) return max;
297        if (x < min) return min;
298        return x;
299    }
300
301    public static int getDisplayRotation(Activity activity) {
302        int rotation = activity.getWindowManager().getDefaultDisplay()
303                .getRotation();
304        switch (rotation) {
305            case Surface.ROTATION_0: return 0;
306            case Surface.ROTATION_90: return 90;
307            case Surface.ROTATION_180: return 180;
308            case Surface.ROTATION_270: return 270;
309        }
310        return 0;
311    }
312
313    public static void setCameraDisplayOrientation(Activity activity,
314            int cameraId, Camera camera) {
315        // See android.hardware.Camera.setCameraDisplayOrientation for
316        // documentation.
317        Camera.CameraInfo info = new Camera.CameraInfo();
318        Camera.getCameraInfo(cameraId, info);
319        int degrees = getDisplayRotation(activity);
320        int result;
321        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
322            result = (info.orientation + degrees) % 360;
323            result = (360 - result) % 360;  // compensate the mirror
324        } else {  // back-facing
325            result = (info.orientation - degrees + 360) % 360;
326        }
327        camera.setDisplayOrientation(result);
328    }
329
330    public static Size getOptimalPreviewSize(Activity currentActivity,
331            List<Size> sizes, double targetRatio) {
332        // Use a very small tolerance because we want an exact match.
333        final double ASPECT_TOLERANCE = 0.001;
334        if (sizes == null) return null;
335
336        Size optimalSize = null;
337        double minDiff = Double.MAX_VALUE;
338
339        // Because of bugs of overlay and layout, we sometimes will try to
340        // layout the viewfinder in the portrait orientation and thus get the
341        // wrong size of mSurfaceView. When we change the preview size, the
342        // new overlay will be created before the old one closed, which causes
343        // an exception. For now, just get the screen size
344
345        Display display = currentActivity.getWindowManager().getDefaultDisplay();
346        int targetHeight = Math.min(display.getHeight(), display.getWidth());
347
348        if (targetHeight <= 0) {
349            // We don't know the size of SurfaceView, use screen height
350            targetHeight = display.getHeight();
351        }
352
353        // Try to find an size match aspect ratio and size
354        for (Size size : sizes) {
355            double ratio = (double) size.width / size.height;
356            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
357            if (Math.abs(size.height - targetHeight) < minDiff) {
358                optimalSize = size;
359                minDiff = Math.abs(size.height - targetHeight);
360            }
361        }
362
363        // Cannot find the one match the aspect ratio. This should not happen.
364        // Ignore the requirement.
365        if (optimalSize == null) {
366            Log.w(TAG, "No preview size match the aspect ratio");
367            minDiff = Double.MAX_VALUE;
368            for (Size size : sizes) {
369                if (Math.abs(size.height - targetHeight) < minDiff) {
370                    optimalSize = size;
371                    minDiff = Math.abs(size.height - targetHeight);
372                }
373            }
374        }
375        return optimalSize;
376    }
377
378    public static void dumpParameters(Parameters parameters) {
379        String flattened = parameters.flatten();
380        StringTokenizer tokenizer = new StringTokenizer(flattened, ";");
381        Log.d(TAG, "Dump all camera parameters:");
382        while (tokenizer.hasMoreElements()) {
383            Log.d(TAG, tokenizer.nextToken());
384        }
385    }
386
387   /**
388     * Returns whether the device is voice-capable (meaning, it can do MMS).
389     */
390    public static boolean isMmsCapable(Context context) {
391        TelephonyManager telephonyManager = (TelephonyManager)
392                context.getSystemService(Context.TELEPHONY_SERVICE);
393        if (telephonyManager == null) {
394            return false;
395        }
396
397        try {
398            Class partypes[] = new Class[0];
399            Method sIsVoiceCapable = TelephonyManager.class.getMethod(
400                    "isVoiceCapable", partypes);
401
402            Object arglist[] = new Object[0];
403            Object retobj = sIsVoiceCapable.invoke(telephonyManager, arglist);
404            return (Boolean) retobj;
405        } catch (java.lang.reflect.InvocationTargetException ite) {
406            // Failure, must be another device.
407            // Assume that it is voice capable.
408        } catch (java.lang.IllegalAccessException iae) {
409            // Failure, must be an other device.
410            // Assume that it is voice capable.
411        } catch (NoSuchMethodException nsme) {
412        }
413        return true;
414    }
415
416    // This is for test only. Allow the camera to launch the specific camera.
417    public static int getCameraFacingIntentExtras(Activity currentActivity) {
418        int cameraId = -1;
419
420        int intentCameraId =
421                currentActivity.getIntent().getIntExtra(Util.EXTRAS_CAMERA_FACING, -1);
422
423        if (isFrontCameraIntent(intentCameraId)) {
424            // Check if the front camera exist
425            int frontCameraId = CameraHolder.instance().getFrontCameraId();
426            if (frontCameraId != -1) {
427                cameraId = frontCameraId;
428            }
429        } else if (isBackCameraIntent(intentCameraId)) {
430            // Check if the back camera exist
431            int backCameraId = CameraHolder.instance().getBackCameraId();
432            if (backCameraId != -1) {
433                cameraId = backCameraId;
434            }
435        }
436        return cameraId;
437    }
438
439    private static boolean isFrontCameraIntent(int intentCameraId) {
440        return (intentCameraId == android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
441    }
442
443    private static boolean isBackCameraIntent(int intentCameraId) {
444        return (intentCameraId == android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK);
445    }
446
447}
448