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