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