Mercurial > repos > other > AceTorch
annotate 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 |
rev | line source |
---|---|
0
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 // This file (AceTorchActivity.java) is a part of the AceTorch project and is copyright 2012 IBBoard |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 // |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 // 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. |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 package uk.co.ibboard.acetorch; |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 import android.app.Activity; |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 import android.hardware.Camera; |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 import android.hardware.Camera.Parameters; |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 import android.os.Bundle; |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 import android.view.View; |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 import android.widget.Toast; |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 import android.widget.ToggleButton; |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 public class AceTorchActivity extends Activity { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 private Camera _camera; |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 /** Called when the activity is first created. */ |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 @Override |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 public void onCreate(Bundle savedInstanceState) { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 super.onCreate(savedInstanceState); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 setContentView(R.layout.main); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 public void onToggleClicked(View view) { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 setLightState(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 private boolean desiredTorchStateIsOn() { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 View view = findViewById(R.id.toggleTorch); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 return ((ToggleButton) view).isChecked(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 private void setLightState() { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
35 if (desiredTorchStateIsOn()) { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
36 enableLight(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
37 } else { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
38 disableLight(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
39 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
40 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
41 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 // Correct ordering of the following API calls was a combination of |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 // 1) Pedro Rainho's post (http://stackoverflow.com/a/9751674/283242 - initial "flash mode on" trick) |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
44 // 2) Sham's post (http://stackoverflow.com/a/10253946/283242 - auto-focus just after start preview) |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 // 3) A reasonable amount of trial and error |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 private void enableLight() { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 initCamera(); |
2 | 48 |
0
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 if (_camera == null) { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 Toast.makeText(this, "Unable to get camera", Toast.LENGTH_LONG); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 return; |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 _camera.startPreview(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 Parameters params = _camera.getParameters(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 params.setFlashMode(Parameters.FLASH_MODE_ON); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
59 params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
60 _camera.setParameters(params); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
61 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
62 _camera.startPreview(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
63 |
2 | 64 _camera.autoFocus(null); |
0
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
65 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
66 params = _camera.getParameters(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
67 params.setFlashMode(Parameters.FLASH_MODE_OFF); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
68 _camera.setParameters(params); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
69 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
70 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
71 private void disableLight() { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
72 if (_camera != null) { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
73 _camera.stopPreview(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
74 _camera.release(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
75 _camera = null; |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
76 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
77 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
78 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
79 @Override |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
80 protected void onPause() { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
81 super.onPause(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
82 disableLight(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
83 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
84 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
85 @Override |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
86 protected void onResume() { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
87 setLightState(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
88 super.onResume(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
89 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
90 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
91 private void initCamera() { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
92 if (_camera == null) { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
93 _camera = getCameraInstance(); |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
94 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
95 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
96 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
97 /** A safe way to get an instance of the Camera object. |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
98 * (Taken from Google documentation) */ |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
99 private static Camera getCameraInstance() { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
100 Camera c = null; |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
101 try { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
102 c = Camera.open(); // attempt to get a Camera instance |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
103 } catch (Exception e) { |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
104 // Camera is not available (in use or does not exist) |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
105 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
106 return c; // returns null if camera is unavailable |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
107 } |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
108 |
4a66834572d1
First commit of "Ace Torch" project
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
109 } |