view src/uk/co/ibboard/acetorch/AceTorchActivity.java @ 2:de6a63e771dc

Code cleanup
author IBBoard <dev@ibboard.co.uk>
date Mon, 29 Oct 2012 11:59:17 +0000
parents 4a66834572d1
children 1c0184311e64
line wrap: on
line source

//  This file (AceTorchActivity.java) is a part of the AceTorch project and is copyright 2012 IBBoard
// 
// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU GPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.

package uk.co.ibboard.acetorch;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;

public class AceTorchActivity extends Activity {
	private Camera _camera;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	public void onToggleClicked(View view) {
		setLightState();
	}

	private boolean desiredTorchStateIsOn() {
		View view = findViewById(R.id.toggleTorch);
		return ((ToggleButton) view).isChecked();
	}

	private void setLightState() {
		if (desiredTorchStateIsOn()) {
			enableLight();
		} else {
			disableLight();
		}
	}

	// 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
	private void enableLight() {
		initCamera();
		
		if (_camera == null) {

			Toast.makeText(this, "Unable to get camera", Toast.LENGTH_LONG);
			return;
		}

		_camera.startPreview();

		Parameters params = _camera.getParameters();
		params.setFlashMode(Parameters.FLASH_MODE_ON);
		params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
		_camera.setParameters(params);
		
		_camera.startPreview();

		_camera.autoFocus(null);

		params = _camera.getParameters();
		params.setFlashMode(Parameters.FLASH_MODE_OFF);
		_camera.setParameters(params);
	}

	private void disableLight() {
		if (_camera != null) {
			_camera.stopPreview();
			_camera.release();
			_camera = null;
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		disableLight();
	}

	@Override
	protected void onResume() {
		setLightState();
		super.onResume();
	}

	private void initCamera() {
		if (_camera == null) {
			_camera = getCameraInstance();
		}
	}

	/** A safe way to get an instance of the Camera object. 
	 * (Taken from Google documentation) */
	private static Camera getCameraInstance() {
		Camera c = null;
		try {
			c = Camera.open(); // attempt to get a Camera instance
		} catch (Exception e) {
			// Camera is not available (in use or does not exist)
		}
		return c; // returns null if camera is unavailable
	}

}