changeset 3:1c0184311e64

Try to support "normal" flash LEDs that support Torch mode
author IBBoard <dev@ibboard.co.uk>
date Mon, 29 Oct 2012 13:41:02 +0000
parents de6a63e771dc
children e63120e7b31d
files src/uk/co/ibboard/acetorch/AceTorchActivity.java
diffstat 1 files changed, 53 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/uk/co/ibboard/acetorch/AceTorchActivity.java	Mon Oct 29 11:59:17 2012 +0000
+++ b/src/uk/co/ibboard/acetorch/AceTorchActivity.java	Mon Oct 29 13:41:02 2012 +0000
@@ -4,6 +4,8 @@
 
 package uk.co.ibboard.acetorch;
 
+import java.util.List;
+
 import android.app.Activity;
 import android.hardware.Camera;
 import android.hardware.Camera.Parameters;
@@ -40,25 +42,46 @@
 	}
 
 	// Correct ordering of the following API calls was a combination of
-	//  1) Pedro Rainho's post (http://stackoverflow.com/a/9751674/283242 - initial "flash mode on" trick)
-	//  2) Sham's post (http://stackoverflow.com/a/10253946/283242 - auto-focus just after start preview)
-	//  3) A reasonable amount of trial and error
+	// 1) Pedro Rainho's post (http://stackoverflow.com/a/9751674/283242 -
+	// initial "flash mode on" trick)
+	// 2) Sham's post (http://stackoverflow.com/a/10253946/283242 - auto-focus
+	// just after start preview)
+	// 3) A reasonable amount of trial and error
 	private void enableLight() {
 		initCamera();
-		
+
 		if (_camera == null) {
 
 			Toast.makeText(this, "Unable to get camera", Toast.LENGTH_LONG);
 			return;
 		}
 
-		_camera.startPreview();
+		Parameters params = _camera.getParameters();
+
+		if (cameraHasTorchSupport(params)) {
+			setNormalFlashOn(params);
+		} else {
+			setWorkaroundFlashOn(params);
+		}
+	}
 
-		Parameters params = _camera.getParameters();
+	private void setNormalFlashOn(Parameters params) {
+		params.setFlashMode(Parameters.FLASH_MODE_TORCH);
+        _camera.setParameters(params);
+	}
+
+	private boolean cameraHasTorchSupport(Parameters cameraParams) {
+		List<String> supportedFlashModes = cameraParams
+				.getSupportedFlashModes();
+		return supportedFlashModes != null
+				&& supportedFlashModes.contains(Parameters.FLASH_MODE_TORCH);
+	}
+
+	private void setWorkaroundFlashOn(Parameters params) {
 		params.setFlashMode(Parameters.FLASH_MODE_ON);
 		params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
 		_camera.setParameters(params);
-		
+
 		_camera.startPreview();
 
 		_camera.autoFocus(null);
@@ -70,12 +93,29 @@
 
 	private void disableLight() {
 		if (_camera != null) {
-			_camera.stopPreview();
-			_camera.release();
+			Parameters params = _camera.getParameters();
+
+			if (cameraHasTorchSupport(params)) {
+				setNormalFlashOff(params);
+			} else {
+				setWorkaroundFlashOff();
+			}
 			_camera = null;
 		}
 	}
 
+	private void setNormalFlashOff(Parameters params) {
+		params.setFlashMode(Parameters.FLASH_MODE_OFF);
+		_camera.setParameters(params);
+		_camera.stopPreview();
+		_camera.release();
+	}
+
+	private void setWorkaroundFlashOff() {
+		_camera.stopPreview();
+		_camera.release();
+	}
+
 	@Override
 	protected void onPause() {
 		super.onPause();
@@ -94,8 +134,10 @@
 		}
 	}
 
-	/** A safe way to get an instance of the Camera object. 
-	 * (Taken from Google documentation) */
+	/**
+	 * A safe way to get an instance of the Camera object. (Taken from Google
+	 * documentation)
+	 */
 	private static Camera getCameraInstance() {
 		Camera c = null;
 		try {