Home | History | Annotate | Download | only in helpers
      1 /*
      2  * Copyright 2014 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 
     17 package android.hardware.cts.helpers;
     18 
     19 import android.content.Context;
     20 import android.hardware.Camera;
     21 import android.hardware.camera2.CameraCharacteristics;
     22 import android.hardware.camera2.CameraManager;
     23 
     24 import java.util.Comparator;
     25 
     26 /**
     27  * Utility class containing helper functions for the Camera CTS tests.
     28  */
     29 public class CameraUtils {
     30 
     31     /**
     32      * Returns {@code true} if this device only supports {@code LEGACY} mode operation in the
     33      * Camera2 API for the given camera ID.
     34      *
     35      * @param context {@link Context} to access the {@link CameraManager} in.
     36      * @param cameraId the ID of the camera device to check.
     37      * @return {@code true} if this device only supports {@code LEGACY} mode.
     38      */
     39     public static boolean isLegacyHAL(Context context, int cameraId) throws Exception {
     40         CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
     41         String cameraIdStr = manager.getCameraIdList()[cameraId];
     42         CameraCharacteristics characteristics =
     43                 manager.getCameraCharacteristics(cameraIdStr);
     44 
     45         return characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL) ==
     46                 CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY;
     47     }
     48 
     49     /**
     50      * Returns {@code true} if this device only supports {@code EXTERNAL} mode operation in the
     51      * Camera2 API for the given camera ID.
     52      *
     53      * @param context {@link Context} to access the {@link CameraManager} in.
     54      * @param cameraId the ID of the camera device to check.
     55      * @return {@code true} if this device only supports {@code LEGACY} mode.
     56      */
     57     public static boolean isExternal(Context context, int cameraId) throws Exception {
     58         CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
     59         String cameraIdStr = manager.getCameraIdList()[cameraId];
     60         CameraCharacteristics characteristics =
     61                 manager.getCameraCharacteristics(cameraIdStr);
     62 
     63         return characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL) ==
     64                 CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL;
     65     }
     66 
     67     /**
     68      * Shared size comparison method used by size comparators.
     69      *
     70      * <p>Compares the number of pixels it covers.If two the areas of two sizes are same, compare
     71      * the widths.</p>
     72      */
     73      public static int compareSizes(int widthA, int heightA, int widthB, int heightB) {
     74         long left = widthA * (long) heightA;
     75         long right = widthB * (long) heightB;
     76         if (left == right) {
     77             left = widthA;
     78             right = widthB;
     79         }
     80         return (left < right) ? -1 : (left > right ? 1 : 0);
     81     }
     82 
     83     /**
     84      * Size comparator that compares the number of pixels it covers.
     85      *
     86      * <p>If two the areas of two sizes are same, compare the widths.</p>
     87      */
     88     public static class LegacySizeComparator implements Comparator<Camera.Size> {
     89         @Override
     90         public int compare(Camera.Size lhs, Camera.Size rhs) {
     91             return compareSizes(lhs.width, lhs.height, rhs.width, rhs.height);
     92         }
     93     }
     94 
     95 }
     96