Check-in [18c4f81217]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:improved NFC support wrt broadcast listener
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 18c4f8121788d77d069d5667390b5e7d9c5d52fd
User & Date: chw 2016-12-03 09:57:20
Context
2016-12-04
06:32
update Symbola font check-in: d2a35734c8 user: chw tags: trunk
2016-12-03
09:57
improved NFC support wrt broadcast listener check-in: 18c4f81217 user: chw tags: trunk
2016-12-02
19:13
start NFC support in borg command, NDEF tags only, for now check-in: 1e5315f4df user: chw tags: trunk
Changes

Changes to src/tk/tcl/wish/AndroWish.java.

78
79
80
81
82
83
84

85
86
87
88
89
90
91
...
265
266
267
268
269
270
271

272
273
274
275
276
277
278
...
330
331
332
333
334
335
336
337
338
339
340

341
342
343
344
345
346
347
...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
...
458
459
460
461
462
463
464












































465
466
467
468
469
470
471
....
1034
1035
1036
1037
1038
1039
1040

1041
1042
1043
1044
1045
1046
1047
1048



1049
1050
1051



1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064



1065
1066
1067



1068
1069
1070
1071
1072
1073
1074
    protected static NmeaInfo mNmeaInfo;
    protected static TelephonyManager mPhoneManager;
    protected static PSListener mPSListener;
    protected static Map<String, BCRecvr> mBCRecvrs;
    protected static ImageCapture mCamera;
    protected static BroadcastReceiver mUsbRecvr;
    protected static Map<String, UsbDeviceConnection> mUsbHold;

    protected static NfcAdapter mNfcAdapter;
    protected static Tag mNfcTag;

    /*
     * Callback on application startup
     */

................................................................................
	if (hasPerm(android.Manifest.permission.NFC)) {
	    NfcManager manager =
		(NfcManager) getSystemService(Context.NFC_SERVICE);
	    mNfcAdapter = manager.getDefaultAdapter();
	} else {
	    mNfcAdapter = null;
	}

	mNfcTag = null;
    }

    /*
     * Tear down everything
     */

................................................................................
	if (mUsbHold != null) {
	    for (final UsbDeviceConnection c : mUsbHold.values()) {
		c.close();
	    }
	    mUsbHold.clear();
	    mUsbHold = null;
	}
	if (mNfcAdapter != null) {
	    mNfcAdapter.disableForegroundDispatch(this);
	}
	mNfcTag = null;

    }

    /*
     * Find out if OpenGL ES >= 2.x is available.
     */

    private static int getGLESVersion(Context context) {
................................................................................
	mSensors.pause();
	if (mPSListener != null) {
	    mPhoneManager.listen(mPSListener, PhoneStateListener.LISTEN_NONE);
	}
	if (mCamera != null) {
	    mCamera.onPause();
	}
	if (mNfcAdapter != null) {
	    mNfcAdapter.disableForegroundDispatch(this);
	}
	mNfcTag = null;
    }

    @Override
    public void onResume() {
	super.onResume();
	mSensors.resume();
	if (mPSListener != null) {
................................................................................
	}
	if (trigger) {
	    mSingleton.nativeTriggerKeyboard();
	}
	if (mCamera != null) {
	    mCamera.onResume();
	}
	if (mNfcAdapter != null) {
	    PendingIntent pi =
		PendingIntent.getBroadcast(getContext(), 0,
					   new Intent("tk.tcl.wish.nfc"), 0);
	    /*
	     * This should be sufficient but doesn't work, unfortunately:
	     *
	     *   mNfcAdapter.enableForegroundDispatch(this, pi, null, null);
	     *
	     * thus use filters for both intent and tech.
	     */

	    IntentFilter ndef =
		new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
	    IntentFilter[] filt = new IntentFilter[] { ndef };
	    String[][] techs = new String[][] {
		new String[] { Ndef.class.getName() },
		new String[] { NdefFormatable.class.getName() }
	    };
	    mNfcAdapter.enableForegroundDispatch(this, pi, filt, techs);
	    mNfcTag = null;
	}
    }

    /*
     * Configuration (e.g. keyboard) has changed
     */

    @Override
................................................................................
	if (hideIM) {
	    mSingleton.sendMessage(SDLActivity.COMMAND_TEXTEDIT_HIDE, 0);
	}
	if (trigger) {
	    mSingleton.nativeTriggerKeyboard();
	}
    }













































    /*
     * Encode Bundle to String[] for "array set"
     */

    String[] formatBundle(Bundle b, String[] add) {
	Set<String> keys = b.keySet();
................................................................................
     */

    public static int handleBroadcastListener(int op, String action) {
	int ret = -1;
	if (action == null) {
	    return ret;
	}

	synchronized (mBCRecvrs) {
	    BCRecvr bcr = mBCRecvrs.get(action);
	    if (op == 0) {
		/* unregister */
		ret = 0;
		if (bcr != null) {
		    mBCRecvrs.remove(action);
		    final BCRecvr unreg_bcr = bcr;



		    Runnable unreg = new Runnable() {
			public void run() {
			    mSingleton.unregisterReceiver(unreg_bcr);



			}
		    };
		    mSingleton.runOnUiThread(unreg);
		    ret = 1;
		}
	    } else if (op == 1) {
		/* register */
		ret = 0;
		if (bcr == null) {
		    final BCRecvr reg_bcr = new BCRecvr(mSingleton);
		    final IntentFilter filter = new IntentFilter();
		    filter.addAction(action);
		    mBCRecvrs.put(action, reg_bcr);



		    Runnable reg = new Runnable() {
			public void run() {
			    mSingleton.registerReceiver(reg_bcr, filter);



			}
		    };
		    mSingleton.runOnUiThread(reg);
		    ret = 1;
		}
	    }
	}







>







 







>







 







<
<
<
|
>







 







|
<
<
<







 







|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>








>
>
>



>
>
>













>
>
>



>
>
>







78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
...
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
...
332
333
334
335
336
337
338



339
340
341
342
343
344
345
346
347
...
371
372
373
374
375
376
377
378



379
380
381
382
383
384
385
...
402
403
404
405
406
407
408
409





















410
411
412
413
414
415
416
...
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
....
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
    protected static NmeaInfo mNmeaInfo;
    protected static TelephonyManager mPhoneManager;
    protected static PSListener mPSListener;
    protected static Map<String, BCRecvr> mBCRecvrs;
    protected static ImageCapture mCamera;
    protected static BroadcastReceiver mUsbRecvr;
    protected static Map<String, UsbDeviceConnection> mUsbHold;
    protected static BCRecvr mNfcBCRecvr;
    protected static NfcAdapter mNfcAdapter;
    protected static Tag mNfcTag;

    /*
     * Callback on application startup
     */

................................................................................
	if (hasPerm(android.Manifest.permission.NFC)) {
	    NfcManager manager =
		(NfcManager) getSystemService(Context.NFC_SERVICE);
	    mNfcAdapter = manager.getDefaultAdapter();
	} else {
	    mNfcAdapter = null;
	}
	mNfcBCRecvr = null;
	mNfcTag = null;
    }

    /*
     * Tear down everything
     */

................................................................................
	if (mUsbHold != null) {
	    for (final UsbDeviceConnection c : mUsbHold.values()) {
		c.close();
	    }
	    mUsbHold.clear();
	    mUsbHold = null;
	}



	mNfcBCRecvr = null;
	nfcEnable(false);
    }

    /*
     * Find out if OpenGL ES >= 2.x is available.
     */

    private static int getGLESVersion(Context context) {
................................................................................
	mSensors.pause();
	if (mPSListener != null) {
	    mPhoneManager.listen(mPSListener, PhoneStateListener.LISTEN_NONE);
	}
	if (mCamera != null) {
	    mCamera.onPause();
	}
	nfcEnable(false);



    }

    @Override
    public void onResume() {
	super.onResume();
	mSensors.resume();
	if (mPSListener != null) {
................................................................................
	}
	if (trigger) {
	    mSingleton.nativeTriggerKeyboard();
	}
	if (mCamera != null) {
	    mCamera.onResume();
	}
	nfcEnable(true);





















    }

    /*
     * Configuration (e.g. keyboard) has changed
     */

    @Override
................................................................................
	if (hideIM) {
	    mSingleton.sendMessage(SDLActivity.COMMAND_TEXTEDIT_HIDE, 0);
	}
	if (trigger) {
	    mSingleton.nativeTriggerKeyboard();
	}
    }

    /*
     * Turn NFC foreground dispatch on or off.
     */

    int nfcEnable(boolean on) {
	if (mNfcAdapter == null) {
	    return -1;
	}
	if ((mNfcBCRecvr != null) && on) {
	    PendingIntent pi =
		PendingIntent.getBroadcast(getContext(), 0,
					   new Intent("tk.tcl.wish.nfc"), 0);
	    /*
	     * This should be sufficient but doesn't work, unfortunately:
	     *
	     *   mNfcAdapter.enableForegroundDispatch(this, pi, null, null);
	     *
	     * thus use filters for both intent and tech.
	     */

	    IntentFilter ndef =
		new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
	    IntentFilter[] filt = new IntentFilter[] { ndef };
	    String[][] techs = new String[][] {
		new String[] { Ndef.class.getName() },
		new String[] { NdefFormatable.class.getName() }
	    };
	    try {
		mNfcAdapter.enableForegroundDispatch(this, pi, filt, techs);
	    } catch (Exception e) {
		return 0;
	    }
	    mNfcTag = null;
	} else {
	    try {
		mNfcAdapter.disableForegroundDispatch(this);
	    } catch (Exception e) {
		return 0;
	    }
	    mNfcTag = null;
	}
	return 1;
    }

    /*
     * Encode Bundle to String[] for "array set"
     */

    String[] formatBundle(Bundle b, String[] add) {
	Set<String> keys = b.keySet();
................................................................................
     */

    public static int handleBroadcastListener(int op, String action) {
	int ret = -1;
	if (action == null) {
	    return ret;
	}
	final boolean isNfc = action.equals("tk.tcl.wish.nfc");
	synchronized (mBCRecvrs) {
	    BCRecvr bcr = mBCRecvrs.get(action);
	    if (op == 0) {
		/* unregister */
		ret = 0;
		if (bcr != null) {
		    mBCRecvrs.remove(action);
		    final BCRecvr unreg_bcr = bcr;
		    if (isNfc) {
			mNfcBCRecvr = null;
		    }
		    Runnable unreg = new Runnable() {
			public void run() {
			    mSingleton.unregisterReceiver(unreg_bcr);
			    if (isNfc) {
				mSingleton.nfcEnable(false);
			    }
			}
		    };
		    mSingleton.runOnUiThread(unreg);
		    ret = 1;
		}
	    } else if (op == 1) {
		/* register */
		ret = 0;
		if (bcr == null) {
		    final BCRecvr reg_bcr = new BCRecvr(mSingleton);
		    final IntentFilter filter = new IntentFilter();
		    filter.addAction(action);
		    mBCRecvrs.put(action, reg_bcr);
		    if (isNfc) {
			mNfcBCRecvr = reg_bcr;
		    }
		    Runnable reg = new Runnable() {
			public void run() {
			    mSingleton.registerReceiver(reg_bcr, filter);
			    if (isNfc) {
				mSingleton.nfcEnable(true);
			    }
			}
		    };
		    mSingleton.runOnUiThread(reg);
		    ret = 1;
		}
	    }
	}