-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathMainActivity.java
More file actions
executable file
·406 lines (341 loc) · 18.4 KB
/
MainActivity.java
File metadata and controls
executable file
·406 lines (341 loc) · 18.4 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// **********************************************************************************************
// * *
// * This application is intended for demonstration purposes only. It is provided as-is *
// * without guarantee or warranty and may be modified to suit individual needs. *
// * *
// **********************************************************************************************
package com.zebra.datacapture1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.Set;
import static android.provider.ContactsContract.Intents.Insert.ACTION;
public class MainActivity extends AppCompatActivity {
// DataWedge Sample supporting DataWedge APIs up to DW 7.0
private static final String EXTRA_PROFILENAME = "DWDataCapture1";
// DataWedge Extras
private static final String EXTRA_GET_VERSION_INFO = "com.symbol.datawedge.api.GET_VERSION_INFO";
private static final String EXTRA_CREATE_PROFILE = "com.symbol.datawedge.api.CREATE_PROFILE";
private static final String EXTRA_KEY_APPLICATION_NAME = "com.symbol.datawedge.api.APPLICATION_NAME";
private static final String EXTRA_KEY_NOTIFICATION_TYPE = "com.symbol.datawedge.api.NOTIFICATION_TYPE";
private static final String EXTRA_SOFT_SCAN_TRIGGER = "com.symbol.datawedge.api.SOFT_SCAN_TRIGGER";
private static final String EXTRA_RESULT_NOTIFICATION = "com.symbol.datawedge.api.NOTIFICATION";
private static final String EXTRA_REGISTER_NOTIFICATION = "com.symbol.datawedge.api.REGISTER_FOR_NOTIFICATION";
private static final String EXTRA_UNREGISTER_NOTIFICATION = "com.symbol.datawedge.api.UNREGISTER_FOR_NOTIFICATION";
private static final String EXTRA_SET_CONFIG = "com.symbol.datawedge.api.SET_CONFIG";
private static final String EXTRA_RESULT_NOTIFICATION_TYPE = "NOTIFICATION_TYPE";
private static final String EXTRA_KEY_VALUE_SCANNER_STATUS = "SCANNER_STATUS";
private static final String EXTRA_KEY_VALUE_PROFILE_SWITCH = "PROFILE_SWITCH";
private static final String EXTRA_KEY_VALUE_CONFIGURATION_UPDATE = "CONFIGURATION_UPDATE";
private static final String EXTRA_KEY_VALUE_NOTIFICATION_STATUS = "STATUS";
private static final String EXTRA_KEY_VALUE_NOTIFICATION_PROFILE_NAME = "PROFILE_NAME";
private static final String EXTRA_SEND_RESULT = "SEND_RESULT";
private static final String EXTRA_EMPTY = "";
private static final String EXTRA_RESULT_GET_VERSION_INFO = "com.symbol.datawedge.api.RESULT_GET_VERSION_INFO";
private static final String EXTRA_RESULT = "RESULT";
private static final String EXTRA_RESULT_INFO = "RESULT_INFO";
private static final String EXTRA_COMMAND = "COMMAND";
// DataWedge Actions
private static final String ACTION_DATAWEDGE = "com.symbol.datawedge.api.ACTION";
private static final String ACTION_RESULT_NOTIFICATION = "com.symbol.datawedge.api.NOTIFICATION_ACTION";
private static final String ACTION_RESULT = "com.symbol.datawedge.api.RESULT_ACTION";
// private variables
private Boolean bRequestSendResult = false;
final String LOG_TAG = "DataCapture1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check selected decoders
// Use SET_CONFIG: http://techdocs.zebra.com/datawedge/latest/guide/api/setconfig/
final Button btnSetDecoders = (Button) findViewById(R.id.btnSetDecoders);
btnSetDecoders.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final CheckBox checkCode128 = (CheckBox) findViewById(R.id.chkCode128);
String Code128Value = setDecoder(checkCode128);
final CheckBox checkCode39 = (CheckBox) findViewById(R.id.chkCode39);
String Code39Value = setDecoder(checkCode39);
final CheckBox checkEAN13 = (CheckBox) findViewById(R.id.chkEAN13);
String EAN13Value = setDecoder(checkEAN13);
final CheckBox checkUPCA = (CheckBox) findViewById(R.id.chkUPCA);
String UPCAValue = setDecoder(checkUPCA);
// Main bundle properties
Bundle profileConfig = new Bundle();
profileConfig.putString("PROFILE_NAME", EXTRA_PROFILENAME);
profileConfig.putString("PROFILE_ENABLED", "true");
profileConfig.putString("CONFIG_MODE", "UPDATE"); // Update specified settings in profile
// PLUGIN_CONFIG bundle properties
Bundle barcodeConfig = new Bundle();
barcodeConfig.putString("PLUGIN_NAME", "BARCODE");
barcodeConfig.putString("RESET_CONFIG", "true");
// PARAM_LIST bundle properties
Bundle barcodeProps = new Bundle();
barcodeProps.putString("scanner_selection", "auto");
barcodeProps.putString("scanner_input_enabled", "true");
barcodeProps.putString("decoder_code128", Code128Value);
barcodeProps.putString("decoder_code39", Code39Value);
barcodeProps.putString("decoder_ean13", EAN13Value);
barcodeProps.putString("decoder_upca", UPCAValue);
// Bundle "barcodeProps" within bundle "barcodeConfig"
barcodeConfig.putBundle("PARAM_LIST", barcodeProps);
// Place "barcodeConfig" bundle within main "profileConfig" bundle
profileConfig.putBundle("PLUGIN_CONFIG", barcodeConfig);
// Create APP_LIST bundle to associate app with profile
Bundle appConfig = new Bundle();
appConfig.putString("PACKAGE_NAME", getPackageName());
appConfig.putStringArray("ACTIVITY_LIST", new String[]{"*"});
profileConfig.putParcelableArray("APP_LIST", new Bundle[]{appConfig});
sendDataWedgeIntentWithExtra(ACTION_DATAWEDGE, EXTRA_SET_CONFIG, profileConfig);
Toast.makeText(getApplicationContext(), "In profile " + EXTRA_PROFILENAME + " the selected decoders are being set: \nCode128=" + Code128Value + "\nCode39="
+ Code39Value + "\nEAN13=" + EAN13Value + "\nUPCA=" + UPCAValue, Toast.LENGTH_LONG).show();
}
});
// Register for status change notification
// Use REGISTER_FOR_NOTIFICATION: http://techdocs.zebra.com/datawedge/latest/guide/api/registerfornotification/
Bundle b = new Bundle();
b.putString(EXTRA_KEY_APPLICATION_NAME, getPackageName());
b.putString(EXTRA_KEY_NOTIFICATION_TYPE, "SCANNER_STATUS"); // register for changes in scanner status
sendDataWedgeIntentWithExtra(ACTION_DATAWEDGE, EXTRA_REGISTER_NOTIFICATION, b);
registerReceivers();
// Get DataWedge version
// Use GET_VERSION_INFO: http://techdocs.zebra.com/datawedge/latest/guide/api/getversioninfo/
sendDataWedgeIntentWithExtra(ACTION_DATAWEDGE, EXTRA_GET_VERSION_INFO, EXTRA_EMPTY); // must be called after registering BroadcastReceiver
}
// Create profile from UI onClick() event
public void CreateProfile (View view){
String profileName = EXTRA_PROFILENAME;
// Send DataWedge intent with extra to create profile
// Use CREATE_PROFILE: http://techdocs.zebra.com/datawedge/latest/guide/api/createprofile/
sendDataWedgeIntentWithExtra(ACTION_DATAWEDGE, EXTRA_CREATE_PROFILE, profileName);
// Configure created profile to apply to this app
Bundle profileConfig = new Bundle();
profileConfig.putString("PROFILE_NAME", EXTRA_PROFILENAME);
profileConfig.putString("PROFILE_ENABLED", "true");
profileConfig.putString("CONFIG_MODE", "CREATE_IF_NOT_EXIST"); // Create profile if it does not exist
// Configure barcode input plugin
Bundle barcodeConfig = new Bundle();
barcodeConfig.putString("PLUGIN_NAME", "BARCODE");
barcodeConfig.putString("RESET_CONFIG", "true"); // This is the default
Bundle barcodeProps = new Bundle();
barcodeConfig.putBundle("PARAM_LIST", barcodeProps);
profileConfig.putBundle("PLUGIN_CONFIG", barcodeConfig);
// Associate profile with this app
Bundle appConfig = new Bundle();
appConfig.putString("PACKAGE_NAME", getPackageName());
appConfig.putStringArray("ACTIVITY_LIST", new String[]{"*"});
profileConfig.putParcelableArray("APP_LIST", new Bundle[]{appConfig});
profileConfig.remove("PLUGIN_CONFIG");
// Apply configs
// Use SET_CONFIG: http://techdocs.zebra.com/datawedge/latest/guide/api/setconfig/
sendDataWedgeIntentWithExtra(ACTION_DATAWEDGE, EXTRA_SET_CONFIG, profileConfig);
// Configure intent output for captured data to be sent to this app
Bundle intentConfig = new Bundle();
intentConfig.putString("PLUGIN_NAME", "INTENT");
intentConfig.putString("RESET_CONFIG", "true");
Bundle intentProps = new Bundle();
intentProps.putString("intent_output_enabled", "true");
intentProps.putString("intent_action", getResources().getString(R.string.activity_intent_filter_action));
intentProps.putString("intent_delivery", "2");
intentConfig.putBundle("PARAM_LIST", intentProps);
profileConfig.putBundle("PLUGIN_CONFIG", intentConfig);
sendDataWedgeIntentWithExtra(ACTION_DATAWEDGE, EXTRA_SET_CONFIG, profileConfig);
Toast.makeText(getApplicationContext(), "Created profile. Check DataWedge app UI.", Toast.LENGTH_LONG).show();
}
// Toggle soft scan trigger from UI onClick() event
// Use SOFT_SCAN_TRIGGER: http://techdocs.zebra.com/datawedge/latest/guide/api/softscantrigger/
public void ToggleSoftScanTrigger (View view){
sendDataWedgeIntentWithExtra(ACTION_DATAWEDGE, EXTRA_SOFT_SCAN_TRIGGER, "TOGGLE_SCANNING");
}
// Create filter for the broadcast intent
private void registerReceivers() {
Log.d(LOG_TAG, "registerReceivers()");
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_RESULT_NOTIFICATION); // for notification result
filter.addAction(ACTION_RESULT); // for error code result
filter.addCategory(Intent.CATEGORY_DEFAULT); // needed to get version info
// register to received broadcasts via DataWedge scanning
filter.addAction(getResources().getString(R.string.activity_intent_filter_action));
filter.addAction(getResources().getString(R.string.activity_action_from_service));
registerReceiver(myBroadcastReceiver, filter);
}
// Unregister scanner status notification
public void unRegisterScannerStatus() {
Log.d(LOG_TAG, "unRegisterScannerStatus()");
Bundle b = new Bundle();
b.putString(EXTRA_KEY_APPLICATION_NAME, getPackageName());
b.putString(EXTRA_KEY_NOTIFICATION_TYPE, EXTRA_KEY_VALUE_SCANNER_STATUS);
Intent i = new Intent();
i.setAction(ACTION);
i.putExtra(EXTRA_UNREGISTER_NOTIFICATION, b);
this.sendBroadcast(i);
}
public String setDecoder (CheckBox decoder)
{
boolean checkValue = decoder.isChecked();
String value = "false";
if (checkValue)
{
value = "true";
return value;
}
else
return value;
}
private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle b = intent.getExtras();
Log.d(LOG_TAG, "DataWedge Action:" + action);
// Get DataWedge version info
if (intent.hasExtra(EXTRA_RESULT_GET_VERSION_INFO))
{
Bundle versionInfo = intent.getBundleExtra(EXTRA_RESULT_GET_VERSION_INFO);
String DWVersion = versionInfo.getString("DATAWEDGE");
TextView txtDWVersion = (TextView) findViewById(R.id.txtGetDWVersion);
txtDWVersion.setText(DWVersion);
Log.i(LOG_TAG, "DataWedge Version: " + DWVersion);
}
if (action.equals(getResources().getString(R.string.activity_intent_filter_action)))
{
// Received a barcode scan
try
{
displayScanResult(intent, "via Broadcast");
}
catch (Exception e)
{
// Catch error if the UI does not exist when we receive the broadcast...
}
}
else if (action.equals(ACTION_RESULT))
{
// Register to receive the result code
if ((intent.hasExtra(EXTRA_RESULT)) && (intent.hasExtra(EXTRA_COMMAND)))
{
String command = intent.getStringExtra(EXTRA_COMMAND);
String result = intent.getStringExtra(EXTRA_RESULT);
String info = "";
if (intent.hasExtra(EXTRA_RESULT_INFO))
{
Bundle result_info = intent.getBundleExtra(EXTRA_RESULT_INFO);
Set<String> keys = result_info.keySet();
for (String key : keys) {
Object object = result_info.get(key);
if (object instanceof String) {
info += key + ": " + object + "\n";
} else if (object instanceof String[]) {
String[] codes = (String[]) object;
for (String code : codes) {
info += key + ": " + code + "\n";
}
}
}
Log.d(LOG_TAG, "Command: "+command+"\n" +
"Result: " +result+"\n" +
"Result Info: " + info + "\n");
Toast.makeText(getApplicationContext(), "Error Resulted. Command:" + command + "\nResult: " + result + "\nResult Info: " +info, Toast.LENGTH_LONG).show();
}
}
}
// Register for scanner change notification
else if (action.equals(ACTION_RESULT_NOTIFICATION))
{
if (intent.hasExtra(EXTRA_RESULT_NOTIFICATION))
{
Bundle extras = intent.getBundleExtra(EXTRA_RESULT_NOTIFICATION);
String notificationType = extras.getString(EXTRA_RESULT_NOTIFICATION_TYPE);
if (notificationType != null)
{
switch (notificationType) {
case EXTRA_KEY_VALUE_SCANNER_STATUS:
// Change in scanner status occurred
String displayScannerStatusText = extras.getString(EXTRA_KEY_VALUE_NOTIFICATION_STATUS) +
", profile: " + extras.getString(EXTRA_KEY_VALUE_NOTIFICATION_PROFILE_NAME);
//Toast.makeText(getApplicationContext(), displayScannerStatusText, Toast.LENGTH_SHORT).show();
final TextView lblScannerStatus = (TextView) findViewById(R.id.lblScannerStatus);
lblScannerStatus.setText(displayScannerStatusText);
Log.i(LOG_TAG, "Scanner status: " + displayScannerStatusText);
break;
case EXTRA_KEY_VALUE_PROFILE_SWITCH:
// Received change in profile
// For future enhancement
break;
case EXTRA_KEY_VALUE_CONFIGURATION_UPDATE:
// Configuration change occurred
// For future enhancement
break;
}
}
}
}
}
};
private void displayScanResult(Intent initiatingIntent, String howDataReceived)
{
// store decoded data
String decodedData = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_data));
// store decoder type
String decodedLabelType = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_label_type));
final TextView lblScanData = (TextView) findViewById(R.id.lblScanData);
final TextView lblScanLabelType = (TextView) findViewById(R.id.lblScanDecoder);
lblScanData.setText(decodedData);
lblScanLabelType.setText(decodedLabelType);
}
private void sendDataWedgeIntentWithExtra(String action, String extraKey, Bundle extras)
{
Intent dwIntent = new Intent();
dwIntent.setAction(action);
dwIntent.putExtra(extraKey, extras);
if (bRequestSendResult)
dwIntent.putExtra(EXTRA_SEND_RESULT, "true");
this.sendBroadcast(dwIntent);
}
private void sendDataWedgeIntentWithExtra(String action, String extraKey, String extraValue)
{
Intent dwIntent = new Intent();
dwIntent.setAction(action);
dwIntent.putExtra(extraKey, extraValue);
if (bRequestSendResult)
dwIntent.putExtra(EXTRA_SEND_RESULT, "true");
this.sendBroadcast(dwIntent);
}
@Override
protected void onResume()
{
super.onResume();
registerReceivers();
}
@Override
protected void onPause()
{
super.onPause();
unregisterReceiver(myBroadcastReceiver);
unRegisterScannerStatus();
}
@Override
protected void onDestroy()
{
super.onDestroy();
}
@Override
protected void onStart()
{
super.onStart();
}
@Override
protected void onStop()
{
super.onStop();
}
}