55import android .content .Intent ;
66import android .content .IntentFilter ;
77import android .net .Uri ;
8- import android .nfc .NdefMessage ;
9- import android .nfc .NfcAdapter ;
108import android .os .Handler ;
11- import android . os . Parcelable ;
9+
1210import androidx .fragment .app .Fragment ;
1311import androidx .fragment .app .FragmentManager ;
1412import androidx .drawerlayout .widget .DrawerLayout ;
1513import android .os .Bundle ;
1614import androidx .appcompat .app .AppCompatActivity ;
1715import android .util .Log ;
1816import android .view .View ;
19- import android .view .inputmethod .InputMethodInfo ;
20- import android .view .inputmethod .InputMethodManager ;
2117import android .widget .AdapterView ;
2218import android .widget .ListView ;
2319import android .widget .TextView ;
2925import com .csl .cs108library4a .Cs108Library4A ;
3026import com .csl .cs108library4a .ReaderDevice ;
3127
32- import java .io .UnsupportedEncodingException ;
33- import java .util .List ;
34-
3528public class MainActivity extends AppCompatActivity {
3629 final boolean DEBUG = false ; final String TAG = "Hello" ;
3730 public static boolean activityActive = false ;
@@ -52,7 +45,6 @@ public class MainActivity extends AppCompatActivity {
5245 public static ReaderDevice tagSelected ;
5346 Handler mHandler = new Handler ();
5447
55- public static NfcAdapter nfcAdapter = null ;
5648 PendingIntent mPendingIntent ;
5749 IntentFilter writeTagFilters [];
5850 String [][] techList ;
@@ -85,28 +77,6 @@ protected void onCreate(Bundle savedInstanceState) {
8577 mCs108Library4a = new Cs108Library4A (mContext , mLogView );
8678 mSensorConnector = new SensorConnector (mContext );
8779
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-
11080 super .onCreate (savedInstanceState );
11181 if (savedInstanceState == null ) selectItem (DrawerPositions .MAIN );
11282 Log .i (TAG , "MainActivity.onCreate.onCreate: END" );
@@ -128,15 +98,13 @@ protected void onStart() {
12898 @ Override
12999 protected void onResume () {
130100 super .onResume ();
131- if (nfcAdapter != null && nfcAdapter .isEnabled ()) nfcAdapter .enableForegroundDispatch (this , mPendingIntent , writeTagFilters , techList );
132101 activityActive = true ; wedged = false ;
133102 if (DEBUG ) mCs108Library4a .appendToLog ("MainActivity.onResume()" );
134103 }
135104
136105 @ Override
137106 protected void onPause () {
138107 if (DEBUG ) mCs108Library4a .appendToLog ("MainActivity.onPause()" );
139- if (nfcAdapter != null ) nfcAdapter .disableForegroundDispatch (this );
140108 activityActive = false ;
141109 super .onPause ();
142110 }
@@ -290,7 +258,9 @@ public void onBackPressed() {
290258 public static boolean permissionRequesting ;
291259 @ Override
292260 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 ] : "" ) );
294264 super .onRequestPermissionsResult (requestCode , permissions , grantResults );
295265 permissionRequesting = false ;
296266 }
@@ -414,58 +384,9 @@ protected void onNewIntent(Intent intent) {
414384 super .onNewIntent (intent );
415385 mCs108Library4a .appendToLog ("onNewIntent !!! intent.getAction = " + intent .getAction ());
416386 readFromIntent (intent );
417- // if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
418- // }
419387 }
420388 private void readFromIntent (Intent intent ) {
421389 mCs108Library4a .appendToLog ("onNewIntent !!! readFromIntent entry" );
422390 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);
470391 }
471392}
0 commit comments