-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sample_Torch.cs
254 lines (208 loc) · 6.77 KB
/
Sample_Torch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.Collections.Generic;
namespace com.samsung.android.sdk.camera.sample.cases
{
using Activity = android.app.Activity;
using AlertDialog = android.app.AlertDialog;
using DialogInterface = android.content.DialogInterface;
using CameraAccessException = android.hardware.camera2.CameraAccessException;
using Build = android.os.Build;
using Bundle = android.os.Bundle;
using Log = android.util.Log;
using TextureView = android.view.TextureView;
using View = android.view.View;
using ViewGroup = android.view.ViewGroup;
using TextView = android.widget.TextView;
using ToggleButton = android.widget.ToggleButton;
public class Sample_Torch : Activity
{
private static readonly string TAG = typeof(Sample_Torch).Name;
private SCamera mSCamera;
private SCameraManager mSCameraManager;
private IDictionary<string, ToggleButton> mCameraIdViewMap;
protected internal override void onCreate(Bundle savedInstanceState)
{
base.onCreate(savedInstanceState);
ContentView = R.layout.activity_torch;
}
protected internal override void onResume()
{
base.onResume();
// initialize SCamera
mSCamera = new SCamera();
try
{
mSCamera.initialize(this);
}
catch (SsdkUnsupportedException)
{
showAlertDialog("Fail to initialize SCamera.", true);
return;
}
if (!checkRequiredFeatures())
{
return;
}
createUI();
}
private void createUI()
{
ViewGroup torchButtons = (ViewGroup) findViewById(R.id.torch_buttons_layout);
torchButtons.removeAllViews();
mCameraIdViewMap = new Dictionary<>();
mSCameraManager = mSCamera.SCameraManager;
int numberOfCameraWithFlash = 0;
try
{
foreach (string id in mSCameraManager.CameraIdList)
{
ViewGroup torchItem = (ViewGroup)LayoutInflater.inflate(R.layout.torch_item, null);
torchButtons.addView(torchItem);
((TextView)torchItem.findViewById(R.id.cameraId_textView)).Text = id;
ToggleButton toggleButton = (ToggleButton)torchItem.findViewById(R.id.toggleButton);
toggleButton.Tag = id;
if (mSCameraManager.getCameraCharacteristics(id).get(SCameraCharacteristics.FLASH_INFO_AVAILABLE))
{
numberOfCameraWithFlash++;
toggleButton.OnClickListener = new OnClickListenerAnonymousInnerClassHelper(this);
}
else
{
toggleButton.Checked = false;
toggleButton.Enabled = false;
}
mCameraIdViewMap[id] = toggleButton;
}
mSCameraManager.registerTorchCallback(new TorchCallbackAnonymousInnerClassHelper(this), null);
}
catch (CameraAccessException e)
{
Console.WriteLine(e.ToString());
Console.Write(e.StackTrace);
}
((TextView)findViewById(R.id.totalNumber_textView)).Text = "Number of camera device with flash unit = " + numberOfCameraWithFlash;
}
private class OnClickListenerAnonymousInnerClassHelper : View.OnClickListener
{
private readonly Sample_Torch outerInstance;
public OnClickListenerAnonymousInnerClassHelper(Sample_Torch outerInstance)
{
this.outerInstance = outerInstance;
}
public override void onClick(View v)
{
string cameraId = (string) v.Tag;
try
{
outerInstance.mSCameraManager.setTorchMode(cameraId, ((ToggleButton) v).Checked);
}
catch (Exception)
{
outerInstance.showAlertDialog("Fail to change torch mode for camera with iduniquetempvar.", true);
}
}
}
private class TorchCallbackAnonymousInnerClassHelper : SCameraManager.TorchCallback
{
private readonly Sample_Torch outerInstance;
public TorchCallbackAnonymousInnerClassHelper(Sample_Torch outerInstance)
{
this.outerInstance = outerInstance;
}
public override void onTorchModeUnavailable(string cameraId)
{
ToggleButton toggleButton = outerInstance.mCameraIdViewMap[cameraId];
if (null != toggleButton)
{
toggleButton.Enabled = false;
}
}
public override void onTorchModeChanged(string cameraId, bool enabled)
{
ToggleButton toggleButton = outerInstance.mCameraIdViewMap[cameraId];
if (null != toggleButton)
{
//Enable toggle button as mode changed is invoked.
toggleButton.Enabled = true;
if (toggleButton.Checked != enabled)
{
toggleButton.Checked = enabled;
}
}
}
}
private bool checkRequiredFeatures()
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
{
showAlertDialog("Device running Android prior to M is not compatible with torch mode control APIs.", true);
Log.e(TAG, "Device running Android prior to M is not compatible with torch mode control APIs.");
return false;
}
try
{
bool flashFound = false;
foreach (string id in mSCamera.SCameraManager.CameraIdList)
{
SCameraCharacteristics cameraCharacteristics = mSCamera.SCameraManager.getCameraCharacteristics(id);
if (cameraCharacteristics.get(SCameraCharacteristics.FLASH_INFO_AVAILABLE))
{
flashFound = true;
break;
}
}
// no camera device with flash unit.
if (!flashFound)
{
showAlertDialog("Device does not have a camera device with flash unit.", true);
Log.e(TAG, "Device does not have a camera device with flash unit.");
return false;
}
}
catch (CameraAccessException e)
{
showAlertDialog("Cannot access the camera.", true);
Log.e(TAG, "Cannot access the camera.", e);
return false;
}
return true;
}
/// <summary>
/// Shows alert dialog.
/// </summary>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: private void showAlertDialog(String message, final boolean finishActivity)
private void showAlertDialog(string message, bool finishActivity)
{
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(this);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage(message).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Alert").setPositiveButton(android.R.@string.ok, new OnClickListenerAnonymousInnerClassHelper(this, finishActivity, dialog))
.Cancelable = false;
runOnUiThread(() =>
{
dialog.show();
});
}
private class OnClickListenerAnonymousInnerClassHelper : DialogInterface.OnClickListener
{
private readonly Sample_Torch outerInstance;
private bool finishActivity;
private AlertDialog.Builder dialog;
public OnClickListenerAnonymousInnerClassHelper(Sample_Torch outerInstance, bool finishActivity, AlertDialog.Builder dialog)
{
this.outerInstance = outerInstance;
this.finishActivity = finishActivity;
this.dialog = dialog;
}
public override void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
if (finishActivity)
{
finish();
}
}
}
}
}