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.hardware.Camera;
21 import android.os.Bundle;
22 import android.util.Log;
23 
24 /**
25  * Activity implementing basic access of the Camera1 API.
26  *
27  * <p />
28  * This will log all errors to {@link android.hardware.multiprocess.camera.cts.ErrorLoggingService}.
29  */
30 public class Camera1Activity extends Activity {
31     private static final String TAG = "Camera1Activity";
32 
33     Camera mCamera;
34     ErrorLoggingService.ErrorServiceConnection mErrorServiceConnection;
35 
36     @Override
onCreate(Bundle savedInstanceState)37     protected void onCreate(Bundle savedInstanceState) {
38         Log.i(TAG, "onCreate called.");
39         super.onCreate(savedInstanceState);
40         mErrorServiceConnection = new ErrorLoggingService.ErrorServiceConnection(this);
41         mErrorServiceConnection.start();
42     }
43 
44     @Override
onResume()45     protected void onResume() {
46         Log.i(TAG, "onResume called.");
47         super.onResume();
48         try {
49             mCamera = Camera.open();
50             if (mCamera == null) {
51                 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG +
52                         " no cameras available.");
53             }
54             mCamera.setErrorCallback(new Camera.ErrorCallback() {
55                 @Override
56                 public void onError(int i, Camera camera) {
57                     if (i == Camera.CAMERA_ERROR_EVICTED) {
58                         mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_EVICTED,
59                                 TAG + " camera evicted");
60                         Log.e(TAG, "onError called with event " + i + ", camera evicted");
61                     } else {
62                         mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR,
63                                 TAG + " camera experienced error: " + i);
64                         Log.e(TAG, "onError called with event " + i + ", camera error");
65                     }
66                 }
67             });
68             mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_CONNECT,
69                     TAG + " camera connected");
70         } catch (RuntimeException e) {
71             mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG +
72                     " camera exception during connection: " + e);
73             Log.e(TAG, "Runtime error: " + e);
74         }
75     }
76 
77     @Override
onPause()78     protected void onPause() {
79         Log.i(TAG, "onPause called.");
80         super.onPause();
81     }
82 
83     @Override
onDestroy()84     protected void onDestroy() {
85         Log.i(TAG, "onDestroy called.");
86         super.onDestroy();
87         if (mErrorServiceConnection != null) {
88             mErrorServiceConnection.stop();
89             mErrorServiceConnection = null;
90         }
91     }
92 }
93