1 /* 2 * Copyright (C) 2012 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.ex.camera2.portability; 18 19 import android.os.Handler; 20 21 /** 22 * A handler for all camera api runtime exceptions. 23 * The default behavior is to throw the runtime exception. 24 */ 25 public class CameraExceptionHandler { 26 private Handler mHandler; 27 28 private CameraExceptionCallback mCallback = 29 new CameraExceptionCallback() { 30 @Override 31 public void onCameraError(int errorCode) { 32 } 33 @Override 34 public void onCameraException( 35 RuntimeException e, String commandHistory, int action, int state) { 36 throw e; 37 } 38 @Override 39 public void onDispatchThreadException(RuntimeException e) { 40 throw e; 41 } 42 }; 43 44 /** 45 * A callback helps to handle RuntimeException thrown by camera framework. 46 */ 47 public static interface CameraExceptionCallback { 48 public void onCameraError(int errorCode); 49 public void onCameraException( 50 RuntimeException e, String commandHistory, int action, int state); 51 public void onDispatchThreadException(RuntimeException e); 52 } 53 54 /** 55 * Construct a new instance of {@link CameraExceptionHandler} with a custom callback which will 56 * be executed on a specific {@link Handler}. 57 * 58 * @param callback The callback which will be invoked. 59 * @param handler The handler in which the callback will be invoked in. 60 */ 61 public CameraExceptionHandler(CameraExceptionCallback callback, Handler handler) { 62 mHandler = handler; 63 mCallback = callback; 64 } 65 66 /** 67 * Construct a new instance of {@link CameraExceptionHandler} with a default callback which will 68 * be executed on a specific {@link Handler}. 69 * 70 * @param handler The handler in which the default callback will be invoked in. 71 */ 72 public CameraExceptionHandler(Handler handler) { 73 mHandler = handler; 74 } 75 76 /** 77 * Invoke @{link CameraExceptionCallback} when an error is reported by Android camera framework. 78 * 79 * @param errorCode An integer to represent the error code. 80 * @see android.hardware.Camera#setErrorCallback(android.hardware.Camera.ErrorCallback) 81 */ 82 public void onCameraError(final int errorCode) { 83 mHandler.post(new Runnable() { 84 @Override 85 public void run() { 86 mCallback.onCameraError(errorCode); 87 } 88 }); 89 } 90 91 /** 92 * Invoke @{link CameraExceptionCallback} when a runtime exception is thrown by Android camera 93 * framework. 94 * 95 * @param ex The runtime exception object. 96 */ 97 public void onCameraException( 98 final RuntimeException ex, final String commandHistory, 99 final int action, final int state) { 100 mHandler.post(new Runnable() { 101 @Override 102 public void run() { 103 mCallback.onCameraException(ex, commandHistory, action, state); 104 } 105 }); 106 } 107 108 /** 109 * Invoke @{link CameraExceptionCallback} when a runtime exception is thrown by 110 * @{link DispatchThread}. 111 * 112 * @param ex The runtime exception object. 113 */ 114 public void onDispatchThreadException(final RuntimeException ex) { 115 mHandler.post(new Runnable() { 116 @Override 117 public void run() { 118 mCallback.onDispatchThreadException(ex); 119 } 120 }); 121 } 122 } 123 124