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