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