1/*
2 * Copyright (C) 2013 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.mediaframeworktest;
18
19import android.hardware.Camera;
20import android.hardware.Camera.Parameters;
21import android.hardware.Camera.PictureCallback;
22import android.hardware.Camera.ShutterCallback;
23import android.os.Environment;
24import android.util.Log;
25import android.view.SurfaceHolder;
26
27import java.io.BufferedWriter;
28import java.io.File;
29import java.io.FilenameFilter;
30import java.io.FileNotFoundException;
31import java.io.FileOutputStream;
32import java.io.IOException;
33
34import junit.framework.Assert;
35
36public class CameraTestHelper {
37
38    public Camera mCamera;
39    private String TAG = "CameraTestHelper";
40    private static final int CAMERA_ID = 0;
41    private static final long WAIT_GENERIC = 3 * 1000; // 3 seconds
42    private static final long WAIT_ZOOM_ANIMATION = 5 * 1000; // 5 seconds
43    protected static final String CAMERA_STRESS_IMAGES_DIRECTORY = "cameraStressImages";
44    private static final String CAMERA_STRESS_IMAGES_PREFIX = "camera-stress-test";
45    private final CameraErrorCallback mCameraErrorCallback = new CameraErrorCallback();
46
47    private final class CameraErrorCallback implements android.hardware.Camera.ErrorCallback {
48        public void onError(int error, android.hardware.Camera camera) {
49            Assert.fail(String.format("Camera error, code: %d", error));
50        }
51    }
52
53    private ShutterCallback shutterCallback = new ShutterCallback() {
54        @Override
55        public void onShutter() {
56            Log.v(TAG, "Shutter");
57        }
58    };
59
60    private PictureCallback rawCallback = new PictureCallback() {
61        @Override
62        public void onPictureTaken(byte[] data, Camera camera) {
63            Log.v(TAG, "Raw picture taken");
64        }
65    };
66
67    private PictureCallback jpegCallback = new PictureCallback() {
68        @Override
69        public void onPictureTaken(byte[] data, Camera camera) {
70            FileOutputStream fos = null;
71
72            try {
73                Log.v(TAG, "JPEG picture taken");
74                fos = new FileOutputStream(String.format("%s/%s/%s-%d.jpg",
75                        Environment.getExternalStorageDirectory(), CAMERA_STRESS_IMAGES_DIRECTORY,
76                        CAMERA_STRESS_IMAGES_PREFIX, System.currentTimeMillis()));
77                fos.write(data);
78            } catch (FileNotFoundException e) {
79                Log.e(TAG, "File not found: " + e.toString());
80            } catch (IOException e) {
81                Log.e(TAG, "Error accessing file: " + e.toString());
82            } finally {
83                try {
84                    if (fos != null) {
85                        fos.close();
86                    }
87                } catch (IOException e) {
88                    Log.e(TAG, "Error closing file: " + e.toString());
89                }
90            }
91        }
92    };
93
94    /**
95     * Helper method for prepping test
96     */
97    public void setupCameraTest() {
98        // Create the test images directory if it doesn't exist
99        File stressImagesDirectory = new File(String.format("%s/%s",
100                Environment.getExternalStorageDirectory(), CAMERA_STRESS_IMAGES_DIRECTORY));
101        if (!stressImagesDirectory.exists()) {
102            stressImagesDirectory.mkdir();
103        }
104
105        mCamera = Camera.open(CAMERA_ID);
106    }
107
108    /**
109     * Helper method for getting the available parameters of the default camera
110     */
111    public Parameters getCameraParameters() {
112        mCamera = Camera.open(CAMERA_ID);
113        Parameters params = mCamera.getParameters();
114        mCamera.release();
115        return params;
116    }
117
118    /**
119     * Helper method for taking a photo
120     */
121    public void capturePhoto() throws Exception {
122        mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
123        Thread.sleep(WAIT_GENERIC);
124        mCamera.stopPreview();
125        mCamera.release();
126    }
127
128    /**
129     * Helper method for cleaning up pics taken during tests
130     */
131    public void cleanupTestImages() {
132        try {
133            File stressImagesDirectory = new File(String.format("%s/%s",
134                    Environment.getExternalStorageDirectory(), CAMERA_STRESS_IMAGES_DIRECTORY));
135            File[] stressImages = stressImagesDirectory.listFiles();
136            for (File f : stressImages) {
137                f.delete();
138            }
139        } catch (SecurityException e) {
140            Log.e(TAG, "Security manager access violation: " + e.toString());
141        }
142    }
143
144    /**
145     * Helper method for setting the camera parameters
146     */
147    public void setParameters(Parameters params) {
148        try {
149            mCamera.setParameters(params);
150        } catch (Exception e) {
151            Log.e(TAG, "Error setting camera parameters");
152        }
153    }
154
155    /**
156     * Helper method for starting up the camera preview
157     */
158    public void startCameraPreview(SurfaceHolder surfaceHolder) throws Exception {
159        mCamera.setErrorCallback(mCameraErrorCallback);
160        mCamera.setPreviewDisplay(surfaceHolder);
161        mCamera.startPreview();
162        Thread.sleep(WAIT_GENERIC);
163    }
164}
165
166