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             // TODO: http://b/145308043 move this back to getCameraIdListNoLazy()
67             String[] cameraIds = manager.getCameraIdList();
68 
69             if (cameraIds == null || cameraIds.length == 0) {
70                 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG +
71                         " device reported having no cameras");
72                 return;
73             }
74 
75             manager.registerAvailabilityCallback(new CameraManager.AvailabilityCallback() {
76                 @Override
77                 public void onCameraAvailable(String cameraId) {
78                     super.onCameraAvailable(cameraId);
79                     mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_AVAILABLE,
80                             cameraId);
81                     Log.i(TAG, "Camera " + cameraId + " is available");
82                 }
83 
84                 @Override
85                 public void onCameraUnavailable(String cameraId) {
86                     super.onCameraUnavailable(cameraId);
87                     mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_UNAVAILABLE,
88                             cameraId);
89                     Log.i(TAG, "Camera " + cameraId + " is unavailable");
90                 }
91 
92                 @Override
93                 public void onPhysicalCameraAvailable(String cameraId, String physicalCameraId) {
94                     super.onPhysicalCameraAvailable(cameraId, physicalCameraId);
95                     mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_AVAILABLE,
96                             cameraId + " : " + physicalCameraId);
97                     Log.i(TAG, "Camera " + cameraId + " : " + physicalCameraId + " is available");
98                 }
99 
100                 @Override
101                 public void onPhysicalCameraUnavailable(String cameraId, String physicalCameraId) {
102                     super.onPhysicalCameraUnavailable(cameraId, physicalCameraId);
103                     mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_UNAVAILABLE,
104                             cameraId + " : " + physicalCameraId);
105                     Log.i(TAG, "Camera " + cameraId + " : " + physicalCameraId + " is unavailable");
106                 }
107             }, null);
108 
109             final String chosen = cameraIds[0];
110 
111             manager.openCamera(chosen, new CameraDevice.StateCallback() {
112                 @Override
113                 public void onOpened(CameraDevice cameraDevice) {
114                     mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_CONNECT,
115                             chosen);
116                     Log.i(TAG, "Camera " + chosen + " is opened");
117                 }
118 
119                 @Override
120                 public void onDisconnected(CameraDevice cameraDevice) {
121                     mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_EVICTED,
122                             chosen);
123                     Log.i(TAG, "Camera " + chosen + " is disconnected");
124                 }
125 
126                 @Override
127                 public void onError(CameraDevice cameraDevice, int i) {
128                     mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG +
129                             " Camera " + chosen + " experienced error " + i);
130                     Log.e(TAG, "Camera " + chosen + " onError called with error " + i);
131                 }
132             }, null);
133         } catch (CameraAccessException e) {
134             mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG +
135                     " camera exception during connection: " + e);
136             Log.e(TAG, "Access exception: " + e);
137         }
138     }
139 
140     @Override
onDestroy()141     protected void onDestroy() {
142         Log.i(TAG, "onDestroy called.");
143         super.onDestroy();
144         if (mErrorServiceConnection != null) {
145             mErrorServiceConnection.stop();
146             mErrorServiceConnection = null;
147         }
148     }
149 }
150