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 android.hardware.multiprocess.camera.cts; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.hardware.camera2.CameraAccessException; 22 import android.hardware.camera2.CameraDevice; 23 import android.hardware.camera2.CameraManager; 24 import android.os.Bundle; 25 import android.os.Handler; 26 import android.util.Log; 27 28 /** 29 * Activity implementing basic access of the Camera2 API. 30 * 31 * <p /> 32 * This will log all errors to {@link android.hardware.multiprocess.camera.cts.ErrorLoggingService}. 33 */ 34 public class Camera2Activity extends Activity { 35 private static final String TAG = "Camera2Activity"; 36 37 ErrorLoggingService.ErrorServiceConnection mErrorServiceConnection; 38 39 @Override onCreate(Bundle savedInstanceState)40 protected void onCreate(Bundle savedInstanceState) { 41 Log.i(TAG, "onCreate called."); 42 super.onCreate(savedInstanceState); 43 mErrorServiceConnection = new ErrorLoggingService.ErrorServiceConnection(this); 44 mErrorServiceConnection.start(); 45 } 46 47 @Override onPause()48 protected void onPause() { 49 Log.i(TAG, "onPause called."); 50 super.onPause(); 51 } 52 53 @Override onResume()54 protected void onResume() { 55 Log.i(TAG, "onResume called."); 56 super.onResume(); 57 58 try { 59 CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); 60 61 if (manager == null) { 62 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG + 63 " could not connect camera service"); 64 return; 65 } 66 String[] cameraIds = manager.getCameraIdList(); 67 68 if (cameraIds == null || cameraIds.length == 0) { 69 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG + 70 " device reported having no cameras"); 71 return; 72 } 73 74 manager.registerAvailabilityCallback(new CameraManager.AvailabilityCallback() { 75 @Override 76 public void onCameraAvailable(String cameraId) { 77 super.onCameraAvailable(cameraId); 78 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_AVAILABLE, 79 cameraId); 80 Log.i(TAG, "Camera " + cameraId + " is available"); 81 } 82 83 @Override 84 public void onCameraUnavailable(String cameraId) { 85 super.onCameraUnavailable(cameraId); 86 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_UNAVAILABLE, 87 cameraId); 88 Log.i(TAG, "Camera " + cameraId + " is unavailable"); 89 } 90 }, null); 91 92 final String chosen = cameraIds[0]; 93 94 manager.openCamera(chosen, new CameraDevice.StateCallback() { 95 @Override 96 public void onOpened(CameraDevice cameraDevice) { 97 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_CONNECT, 98 chosen); 99 Log.i(TAG, "Camera " + chosen + " is opened"); 100 } 101 102 @Override 103 public void onDisconnected(CameraDevice cameraDevice) { 104 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_EVICTED, 105 chosen); 106 Log.i(TAG, "Camera " + chosen + " is disconnected"); 107 } 108 109 @Override 110 public void onError(CameraDevice cameraDevice, int i) { 111 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG + 112 " Camera " + chosen + " experienced error " + i); 113 Log.e(TAG, "Camera " + chosen + " onError called with error " + i); 114 } 115 }, null); 116 } catch (CameraAccessException e) { 117 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG + 118 " camera exception during connection: " + e); 119 Log.e(TAG, "Access exception: " + e); 120 } 121 } 122 123 @Override onDestroy()124 protected void onDestroy() { 125 Log.i(TAG, "onDestroy called."); 126 super.onDestroy(); 127 if (mErrorServiceConnection != null) { 128 mErrorServiceConnection.stop(); 129 mErrorServiceConnection = null; 130 } 131 } 132 } 133