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 if (Camera.getNumberOfCameras() > 0) { 50 // Open camera 51 mCamera = Camera.open(0); 52 } 53 if (mCamera == null) { 54 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG + 55 " no cameras available."); 56 } 57 mCamera.setErrorCallback(new Camera.ErrorCallback() { 58 @Override 59 public void onError(int i, Camera camera) { 60 if (i == Camera.CAMERA_ERROR_EVICTED) { 61 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_EVICTED, 62 TAG + " camera evicted"); 63 Log.e(TAG, "onError called with event " + i + ", camera evicted"); 64 } else { 65 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, 66 TAG + " camera experienced error: " + i); 67 Log.e(TAG, "onError called with event " + i + ", camera error"); 68 } 69 } 70 }); 71 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_CONNECT, 72 TAG + " camera connected"); 73 } catch (RuntimeException e) { 74 mErrorServiceConnection.logAsync(TestConstants.EVENT_CAMERA_ERROR, TAG + 75 " camera exception during connection: " + e); 76 Log.e(TAG, "Runtime error: " + e); 77 } 78 } 79 80 @Override onPause()81 protected void onPause() { 82 Log.i(TAG, "onPause called."); 83 super.onPause(); 84 } 85 86 @Override onDestroy()87 protected void onDestroy() { 88 Log.i(TAG, "onDestroy called."); 89 super.onDestroy(); 90 if (mErrorServiceConnection != null) { 91 mErrorServiceConnection.stop(); 92 mErrorServiceConnection = null; 93 } 94 } 95 } 96