1 /* 2 * Copyright (C) 2015 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 com.android.cts.managedprofile; 18 19 import android.hardware.camera2.CameraDevice; 20 import android.hardware.camera2.CameraManager; 21 import android.os.Handler; 22 import android.util.Log; 23 24 import java.util.concurrent.CountDownLatch; 25 import java.util.concurrent.TimeUnit; 26 import java.util.concurrent.atomic.AtomicBoolean; 27 28 /** 29 * A util class to help open camera in a blocking way. 30 */ 31 class CameraUtils { 32 33 private static final String TAG = "CameraUtils"; 34 35 /** 36 * @return true if success to open camera, false otherwise. 37 */ blockUntilOpenCamera(CameraManager cameraManager, Handler handler)38 public static boolean blockUntilOpenCamera(CameraManager cameraManager, Handler handler) { 39 try { 40 String[] cameraIdList = cameraManager.getCameraIdList(); 41 if (cameraIdList == null || cameraIdList.length == 0) { 42 return false; 43 } 44 String cameraId = cameraIdList[0]; 45 CameraCallback callback = new CameraCallback(); 46 cameraManager.openCamera(cameraId, callback, handler); 47 return callback.waitForResult(); 48 } catch (Exception ex) { 49 // No matter what is going wrong, it means fail to open camera. 50 ex.printStackTrace(); 51 return false; 52 } 53 } 54 55 /** 56 * {@link CameraDevice.StateCallback} is called when {@link CameraDevice} changes its state. 57 */ 58 private static class CameraCallback extends CameraDevice.StateCallback { 59 60 private static final int OPEN_TIMEOUT_SECONDS = 5; 61 62 private final CountDownLatch mLatch = new CountDownLatch(1); 63 64 private AtomicBoolean mResult = new AtomicBoolean(false); 65 66 @Override onOpened(CameraDevice cameraDevice)67 public void onOpened(CameraDevice cameraDevice) { 68 Log.d(TAG, "open camera successfully"); 69 mResult.set(true); 70 if (cameraDevice != null) { 71 cameraDevice.close(); 72 } 73 mLatch.countDown(); 74 } 75 76 @Override onDisconnected(CameraDevice cameraDevice)77 public void onDisconnected(CameraDevice cameraDevice) { 78 Log.d(TAG, "disconnect camera"); 79 mLatch.countDown(); 80 } 81 82 @Override onError(CameraDevice cameraDevice, int error)83 public void onError(CameraDevice cameraDevice, int error) { 84 Log.e(TAG, "Fail to open camera, error code = " + error); 85 mLatch.countDown(); 86 } 87 waitForResult()88 public boolean waitForResult() throws InterruptedException { 89 mLatch.await(OPEN_TIMEOUT_SECONDS, TimeUnit.SECONDS); 90 return mResult.get(); 91 } 92 } 93 } 94