1
2package com.android.mediaframeworktest.integration;
3
4import static org.junit.Assert.assertNotNull;
5
6import android.content.Context;
7import android.content.pm.FeatureInfo;
8import android.content.pm.PackageManager;
9import android.hardware.ICameraService;
10import android.os.IBinder;
11import android.os.ServiceManager;
12import android.util.Log;
13
14public class CameraBinderTestUtils {
15    private final ICameraService mCameraService;
16    private int mGuessedNumCameras;
17
18    static final String CAMERA_SERVICE_BINDER_NAME = "media.camera";
19
20    protected static final int USE_CALLING_UID = -1;
21    protected static final int BAD_VALUE = -22;
22    protected static final int INVALID_OPERATION = -38;
23    protected static final int ALREADY_EXISTS = -17;
24    public static final int NO_ERROR = 0;
25    public static final int EOPNOTSUPP = -95;
26    private final Context mContext;
27
28    public CameraBinderTestUtils(Context context) {
29
30        mContext = context;
31
32        guessNumCameras();
33
34        IBinder cameraServiceBinder = ServiceManager
35                .getService(CameraBinderTestUtils.CAMERA_SERVICE_BINDER_NAME);
36        assertNotNull("Camera service IBinder should not be null", cameraServiceBinder);
37
38        this.mCameraService = ICameraService.Stub.asInterface(cameraServiceBinder);
39        assertNotNull("Camera service should not be null", getCameraService());
40    }
41
42    private void guessNumCameras() {
43
44        /**
45         * Why do we need this? This way we have no dependency on getNumCameras
46         * actually working. On most systems there are only 0, 1, or 2 cameras,
47         * and this covers that 'usual case'. On other systems there might be 3+
48         * cameras, but this will at least check the first 2.
49         */
50        this.mGuessedNumCameras = 0;
51
52        // Front facing camera
53        if (CameraBinderTestUtils.isFeatureAvailable(mContext,
54                PackageManager.FEATURE_CAMERA_FRONT)) {
55            this.mGuessedNumCameras = getGuessedNumCameras() + 1;
56        }
57
58        // Back facing camera
59        if (CameraBinderTestUtils.isFeatureAvailable(mContext,
60                PackageManager.FEATURE_CAMERA)) {
61            this.mGuessedNumCameras = getGuessedNumCameras() + 1;
62        }
63
64        // Any facing camera
65        if (getGuessedNumCameras() == 0
66                && CameraBinderTestUtils.isFeatureAvailable(mContext,
67                        PackageManager.FEATURE_CAMERA_ANY)) {
68            this.mGuessedNumCameras = getGuessedNumCameras() + 1;
69        }
70
71        Log.v(CameraBinderTest.TAG, "Guessing there are at least " + getGuessedNumCameras()
72                + " cameras");
73    }
74
75    final static public boolean isFeatureAvailable(Context context, String feature) {
76        final PackageManager packageManager = context.getPackageManager();
77        final FeatureInfo[] featuresList = packageManager.getSystemAvailableFeatures();
78        for (FeatureInfo f : featuresList) {
79            if (f.name != null && f.name.equals(feature)) {
80                return true;
81            }
82        }
83
84        return false;
85    }
86
87    ICameraService getCameraService() {
88        return mCameraService;
89    }
90
91    int getGuessedNumCameras() {
92        return mGuessedNumCameras;
93    }
94}
95