5
5
import android .content .Intent ;
6
6
import android .content .IntentFilter ;
7
7
import android .net .Uri ;
8
- import android .nfc .NdefMessage ;
9
- import android .nfc .NfcAdapter ;
10
8
import android .os .Handler ;
11
- import android . os . Parcelable ;
9
+
12
10
import androidx .fragment .app .Fragment ;
13
11
import androidx .fragment .app .FragmentManager ;
14
12
import androidx .drawerlayout .widget .DrawerLayout ;
15
13
import android .os .Bundle ;
16
14
import androidx .appcompat .app .AppCompatActivity ;
17
15
import android .util .Log ;
18
16
import android .view .View ;
19
- import android .view .inputmethod .InputMethodInfo ;
20
- import android .view .inputmethod .InputMethodManager ;
21
17
import android .widget .AdapterView ;
22
18
import android .widget .ListView ;
23
19
import android .widget .TextView ;
29
25
import com .csl .cs108library4a .Cs108Library4A ;
30
26
import com .csl .cs108library4a .ReaderDevice ;
31
27
32
- import java .io .UnsupportedEncodingException ;
33
- import java .util .List ;
34
-
35
28
public class MainActivity extends AppCompatActivity {
36
29
final boolean DEBUG = false ; final String TAG = "Hello" ;
37
30
public static boolean activityActive = false ;
@@ -52,7 +45,6 @@ public class MainActivity extends AppCompatActivity {
52
45
public static ReaderDevice tagSelected ;
53
46
Handler mHandler = new Handler ();
54
47
55
- public static NfcAdapter nfcAdapter = null ;
56
48
PendingIntent mPendingIntent ;
57
49
IntentFilter writeTagFilters [];
58
50
String [][] techList ;
@@ -85,28 +77,6 @@ protected void onCreate(Bundle savedInstanceState) {
85
77
mCs108Library4a = new Cs108Library4A (mContext , mLogView );
86
78
mSensorConnector = new SensorConnector (mContext );
87
79
88
- InputMethodManager imeManager = (InputMethodManager ) getApplicationContext ().getSystemService (INPUT_METHOD_SERVICE );
89
- List <InputMethodInfo > lst = imeManager .getInputMethodList ();
90
- for (InputMethodInfo info : lst ) {
91
- // MainActivity.mCs108Library4a.appendToLog(info.getId() + " " + info.loadLabel(getPackageManager()).toString());
92
- }
93
- // Intent intent = new Intent(MainActivity.this, CustomIME.class);
94
- // startService(intent);
95
- // savedInstanceState = null;
96
-
97
- nfcAdapter = NfcAdapter .getDefaultAdapter (this );
98
- if (nfcAdapter == null ) MainActivity .mCs108Library4a .appendToLog ("onNewIntent !!! This device doesn't support NFC" );
99
- else if (nfcAdapter .isEnabled () == false ) MainActivity .mCs108Library4a .appendToLog ("onNewIntent !!! This device doesn't enable NFC" );
100
- else {
101
- readFromIntent (getIntent ());
102
-
103
- mPendingIntent = PendingIntent .getActivity (this , 0 , new Intent (this , getClass ()).addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP ), 0 );
104
- IntentFilter tagDetected = new IntentFilter (NfcAdapter .ACTION_TAG_DISCOVERED );
105
- tagDetected .addCategory (Intent .CATEGORY_DEFAULT );
106
- writeTagFilters = new IntentFilter [] { tagDetected };
107
- techList = new String [][]{{android .nfc .tech .Ndef .class .getName ()}, {android .nfc .tech .NdefFormatable .class .getName ()}};
108
- }
109
-
110
80
super .onCreate (savedInstanceState );
111
81
if (savedInstanceState == null ) selectItem (DrawerPositions .MAIN );
112
82
Log .i (TAG , "MainActivity.onCreate.onCreate: END" );
@@ -128,15 +98,13 @@ protected void onStart() {
128
98
@ Override
129
99
protected void onResume () {
130
100
super .onResume ();
131
- if (nfcAdapter != null && nfcAdapter .isEnabled ()) nfcAdapter .enableForegroundDispatch (this , mPendingIntent , writeTagFilters , techList );
132
101
activityActive = true ; wedged = false ;
133
102
if (DEBUG ) mCs108Library4a .appendToLog ("MainActivity.onResume()" );
134
103
}
135
104
136
105
@ Override
137
106
protected void onPause () {
138
107
if (DEBUG ) mCs108Library4a .appendToLog ("MainActivity.onPause()" );
139
- if (nfcAdapter != null ) nfcAdapter .disableForegroundDispatch (this );
140
108
activityActive = false ;
141
109
super .onPause ();
142
110
}
@@ -290,7 +258,9 @@ public void onBackPressed() {
290
258
public static boolean permissionRequesting ;
291
259
@ Override
292
260
public void onRequestPermissionsResult (int requestCode , String [] permissions , int [] grantResults ) {
293
- MainActivity .mCs108Library4a .appendToLog ("onRequestPermissionsResult ====" );
261
+ MainActivity .mCs108Library4a .appendToLog ("permissionRequesting: requestCode = " + requestCode + ", permissions is " + (permissions == null ? "null" : "valid" ) + ", grantResults is " + (grantResults == null ? "null" : "valid" ) );
262
+ MainActivity .mCs108Library4a .appendToLog ("permissionRequesting: permissions[" + permissions .length + "] = " + (permissions != null && permissions .length > 0 ? permissions [0 ] : "" ));
263
+ MainActivity .mCs108Library4a .appendToLog ("permissionRequesting: grantResults[" + grantResults .length + "] = " + (grantResults != null && grantResults .length > 0 ? grantResults [0 ] : "" ) );
294
264
super .onRequestPermissionsResult (requestCode , permissions , grantResults );
295
265
permissionRequesting = false ;
296
266
}
@@ -414,58 +384,9 @@ protected void onNewIntent(Intent intent) {
414
384
super .onNewIntent (intent );
415
385
mCs108Library4a .appendToLog ("onNewIntent !!! intent.getAction = " + intent .getAction ());
416
386
readFromIntent (intent );
417
- // if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
418
- // }
419
387
}
420
388
private void readFromIntent (Intent intent ) {
421
389
mCs108Library4a .appendToLog ("onNewIntent !!! readFromIntent entry" );
422
390
String action = intent .getAction ();
423
- if (NfcAdapter .ACTION_TAG_DISCOVERED .equals (action )
424
- || NfcAdapter .ACTION_TECH_DISCOVERED .equals (action )
425
- || NfcAdapter .ACTION_NDEF_DISCOVERED .equals (action )) {
426
- mCs108Library4a .appendToLog ("onNewIntent !!! readFromIntent getAction = " + action );
427
- Parcelable [] rawMsgs = intent .getParcelableArrayExtra (NfcAdapter .EXTRA_NDEF_MESSAGES );
428
- mCs108Library4a .appendToLog ("onNewIntent !!! readFromIntent rawMsgs.length = " + rawMsgs .length );
429
- mCs108Library4a .appendToLog ("onNewIntent !!! readFromIntent rawMsgs[0].toString = " + rawMsgs [0 ].toString ());
430
- NdefMessage [] msgs = null ;
431
- if (rawMsgs != null ) {
432
- msgs = new NdefMessage [rawMsgs .length ];
433
- for (int i = 0 ; i < rawMsgs .length ; i ++) {
434
- msgs [i ] = (NdefMessage ) rawMsgs [i ];
435
- }
436
- }
437
- buildTagViews (msgs );
438
- }
439
- }
440
-
441
- private void buildTagViews (NdefMessage [] msgs ) {
442
- if (msgs == null || msgs .length == 0 ) return ;
443
- mCs108Library4a .appendToLog ("onNewIntent !!! buildTagViews msgs.length = " + msgs .length + ", msgs[0].getRecords().size = " + msgs [0 ].getRecords ().length );
444
-
445
- String text = "" ;
446
- for (int x = 0 ; x < msgs .length ; x ++) {
447
- for (int y = 0 ; y < msgs [0 ].getRecords ().length ; y ++) {
448
- mCs108Library4a .appendToLog ("onNewIntent !!! buildTagViews msgs[" + x + "][" + y + "].Inf = " + msgs [x ].getRecords ()[y ].getTnf ());
449
- mCs108Library4a .appendToLog ("onNewIntent !!! buildTagViews msgs[" + x + "][" + y + "].Type = " + mCs108Library4a .byteArrayToString (msgs [x ].getRecords ()[y ].getType ()));
450
- mCs108Library4a .appendToLog ("onNewIntent !!! buildTagViews msgs[" + x + "][" + y + "].Id = " + mCs108Library4a .byteArrayToString (msgs [x ].getRecords ()[y ].getId ()));
451
- mCs108Library4a .appendToLog ("onNewIntent !!! buildTagViews msgs[" + x + "][" + y + "].Payload = " + mCs108Library4a .byteArrayToString (msgs [x ].getRecords ()[y ].getPayload ()));
452
- mCs108Library4a .appendToLog ("onNewIntent !!! buildTagViews msgs[" + x + "][" + y + "].Class = " + msgs [x ].getRecords ()[y ].getClass ().toString ());
453
- }}
454
-
455
- byte [] payload = msgs [0 ].getRecords ()[0 ].getPayload ();
456
- mCs108Library4a .appendToLog ("onNewIntent !!! buildTagViews payload.length = " + payload .length + ", with payload[0] = " + payload [0 ]);
457
- mCs108Library4a .appendToLog ("onNewIntent !!! buildTagViews payload = " + mCs108Library4a .byteArrayToString (payload ));
458
- String textEncoding = ((payload [0 ] & 128 ) == 0 ) ? "UTF-8" : "UTF-16" ; // Get the Text Encoding
459
- int languageCodeLength = payload [0 ] & 0063 ; // Get the Language Code, e.g. "en"
460
- // String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
461
- try {
462
- // Get the Text
463
- text = new String (payload , languageCodeLength + 1 , payload .length - languageCodeLength - 1 , textEncoding );
464
- mCs108Library4a .appendToLog ("onNewIntent !!! buildTagViews text = " + text );
465
- } catch (UnsupportedEncodingException e ) {
466
- mCs108Library4a .appendToLog ("onNewIntent !!! buildTagViews UnsupportedEncoding" + e .toString ());
467
- Log .e ("UnsupportedEncoding" , e .toString ());
468
- }
469
- //tvNFCContent.setText("NFC Content: " + text);
470
391
}
471
392
}
0 commit comments