1616
1717package com .m2049r .xmrwallet ;
1818
19- import android .app .PendingIntent ;
2019import android .content .Context ;
21- import android .content .Intent ;
22- import android .net .Uri ;
23- import android .nfc .FormatException ;
24- import android .nfc .NdefMessage ;
25- import android .nfc .NdefRecord ;
26- import android .nfc .NfcAdapter ;
27- import android .nfc .Tag ;
28- import android .nfc .tech .Ndef ;
29- import android .os .AsyncTask ;
30- import android .os .Build ;
31- import android .os .Bundle ;
3220import android .os .Handler ;
3321import android .os .Looper ;
3422import android .os .PowerManager ;
35- import android .widget .Toast ;
3623
3724import androidx .annotation .CallSuper ;
38- import androidx .annotation .Nullable ;
39- import androidx .fragment .app .Fragment ;
4025
4126import com .m2049r .xmrwallet .data .BarcodeData ;
4227import com .m2049r .xmrwallet .dialog .ProgressDialog ;
43- import com .m2049r .xmrwallet .fragment .send .SendFragment ;
4428import com .m2049r .xmrwallet .ledger .Ledger ;
4529import com .m2049r .xmrwallet .ledger .LedgerProgressDialog ;
4630
47- import java .io .IOException ;
48-
4931import timber .log .Timber ;
5032
5133public class BaseActivity extends SecureActivity
@@ -141,91 +123,6 @@ void releaseWakeLock() {
141123 Timber .d ("WakeLock released" );
142124 }
143125
144-
145- @ Override
146- protected void onCreate (@ Nullable Bundle savedInstanceState ) {
147- super .onCreate (savedInstanceState );
148- initNfc ();
149- }
150-
151- @ Override
152- protected void onPostResume () {
153- super .onPostResume ();
154- if (nfcAdapter != null ) {
155- nfcAdapter .enableForegroundDispatch (this , nfcPendingIntent , null , null );
156- // intercept all techs so we can tell the user their tag is no good
157- }
158- }
159-
160- @ Override
161- protected void onPause () {
162- Timber .d ("onPause()" );
163- if (nfcAdapter != null )
164- nfcAdapter .disableForegroundDispatch (this );
165- super .onPause ();
166- }
167-
168- @ Override
169- protected void onNewIntent (Intent intent ) {
170- super .onNewIntent (intent );
171- processNfcIntent (intent );
172- }
173-
174- // NFC stuff
175- private NfcAdapter nfcAdapter ;
176- private PendingIntent nfcPendingIntent ;
177-
178- public void initNfc () {
179- nfcAdapter = NfcAdapter .getDefaultAdapter (this );
180- if (nfcAdapter == null ) // no NFC support
181- return ;
182- nfcPendingIntent = PendingIntent .getActivity (this , 0 ,
183- new Intent (this , getClass ()).addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP ),
184- Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ? PendingIntent .FLAG_IMMUTABLE : 0 );
185- }
186-
187- private void processNfcIntent (Intent intent ) {
188- String action = intent .getAction ();
189- Timber .d ("ACTION=%s" , action );
190- if (NfcAdapter .ACTION_NDEF_DISCOVERED .equals (action )
191- || NfcAdapter .ACTION_TAG_DISCOVERED .equals (action )
192- || NfcAdapter .ACTION_TECH_DISCOVERED .equals (action )) {
193- Tag tag = intent .getParcelableExtra (NfcAdapter .EXTRA_TAG );
194- Ndef ndef = Ndef .get (tag );
195- if (ndef == null ) {
196- Toast .makeText (this , getString (R .string .nfc_tag_unsupported ), Toast .LENGTH_LONG ).show ();
197- return ;
198- }
199-
200- Fragment f = getSupportFragmentManager ().findFragmentById (R .id .fragment_container );
201- if (f instanceof ReceiveFragment ) {
202- // We want to write a Tag from the ReceiveFragment
203- BarcodeData bc = ((ReceiveFragment ) f ).getBarcodeData ();
204- if (bc != null ) {
205- new AsyncWriteTag (ndef , bc .getUri ()).execute ();
206- } // else wallet is not loaded yet or receive is otherwise not ready - ignore
207- } else if (f instanceof SendFragment ) {
208- // We want to read a Tag for the SendFragment
209- NdefMessage ndefMessage = ndef .getCachedNdefMessage ();
210- if (ndefMessage == null ) {
211- Toast .makeText (this , getString (R .string .nfc_tag_read_undef ), Toast .LENGTH_LONG ).show ();
212- return ;
213- }
214- NdefRecord firstRecord = ndefMessage .getRecords ()[0 ];
215- Uri uri = firstRecord .toUri (); // we insist on the first record
216- if (uri == null ) {
217- Toast .makeText (this , getString (R .string .nfc_tag_read_undef ), Toast .LENGTH_LONG ).show ();
218- } else {
219- BarcodeData bc = BarcodeData .fromString (uri .toString ());
220- if (bc == null )
221- Toast .makeText (this , getString (R .string .nfc_tag_read_undef ), Toast .LENGTH_LONG ).show ();
222- else
223- onUriScanned (bc );
224- }
225- }
226- }
227- }
228-
229126 // this gets called only if we get data
230127 @ CallSuper
231128 void onUriScanned (BarcodeData barcodeData ) {
@@ -239,75 +136,4 @@ private BarcodeData popBarcodeData() {
239136 barcodeData = null ;
240137 return popped ;
241138 }
242-
243- private class AsyncWriteTag extends AsyncTask <Void , Void , Boolean > {
244-
245- Ndef ndef ;
246- Uri uri ;
247- String errorMessage = null ;
248-
249- AsyncWriteTag (Ndef ndef , Uri uri ) {
250- this .ndef = ndef ;
251- this .uri = uri ;
252- }
253-
254- @ Override
255- protected void onPreExecute () {
256- super .onPreExecute ();
257- showProgressDialog (R .string .progress_nfc_write );
258- }
259-
260- @ Override
261- protected Boolean doInBackground (Void ... params ) {
262- if (params .length != 0 ) return false ;
263- try {
264- writeNdef (ndef , uri );
265- return true ;
266- } catch (IOException | FormatException ex ) {
267- Timber .e (ex );
268- } catch (IllegalArgumentException ex ) {
269- errorMessage = ex .getMessage ();
270- Timber .d (errorMessage );
271- } finally {
272- try {
273- ndef .close ();
274- } catch (IOException ex ) {
275- Timber .e (ex );
276- }
277- }
278- return false ;
279- }
280-
281- @ Override
282- protected void onPostExecute (Boolean result ) {
283- super .onPostExecute (result );
284- if (isDestroyed ()) {
285- return ;
286- }
287- dismissProgressDialog ();
288- if (!result ) {
289- if (errorMessage != null )
290- Toast .makeText (getApplicationContext (), errorMessage , Toast .LENGTH_LONG ).show ();
291- else
292- Toast .makeText (getApplicationContext (), getString (R .string .nfc_write_failed ), Toast .LENGTH_LONG ).show ();
293- } else {
294- Toast .makeText (getApplicationContext (), getString (R .string .nfc_write_successful ), Toast .LENGTH_SHORT ).show ();
295- }
296- }
297- }
298-
299- void writeNdef (Ndef ndef , Uri uri ) throws IOException , FormatException {
300- NfcAdapter nfcAdapter = NfcAdapter .getDefaultAdapter (this );
301- if (nfcAdapter == null ) return ; // no NFC support here
302-
303- NdefRecord recordNFC = NdefRecord .createUri (uri );
304- NdefMessage message = new NdefMessage (recordNFC );
305- ndef .connect ();
306- int tagSize = ndef .getMaxSize ();
307- int msgSize = message .getByteArrayLength ();
308- Timber .d ("tagSize=%d, msgSIze=%d, uriSize=%d" , tagSize , msgSize , uri .toString ().length ());
309- if (tagSize < msgSize )
310- throw new IllegalArgumentException (getString (R .string .nfc_tag_size , tagSize , msgSize ));
311- ndef .writeNdefMessage (message );
312- }
313139}
0 commit comments