|
1 | 1 | package io.scalaproject.vault; |
2 | 2 |
|
3 | | -import android.app.PendingIntent; |
4 | 3 | import android.content.Context; |
5 | | -import android.content.Intent; |
6 | | -import android.net.Uri; |
7 | | -import android.nfc.FormatException; |
8 | | -import android.nfc.NdefMessage; |
9 | | -import android.nfc.NdefRecord; |
10 | | -import android.nfc.NfcAdapter; |
11 | | -import android.nfc.Tag; |
12 | | -import android.nfc.tech.Ndef; |
13 | | -import android.os.AsyncTask; |
14 | | -import android.os.Bundle; |
15 | 4 | import android.os.Handler; |
16 | 5 | import android.os.Looper; |
17 | 6 | import android.os.PowerManager; |
| 7 | + |
18 | 8 | import androidx.annotation.CallSuper; |
19 | | -import androidx.annotation.Nullable; |
20 | | -import androidx.fragment.app.Fragment; |
21 | | -import android.widget.Toast; |
22 | 9 |
|
23 | 10 | import io.scalaproject.vault.data.BarcodeData; |
24 | 11 | import io.scalaproject.vault.dialog.ProgressDialog; |
25 | | -import io.scalaproject.vault.fragment.send.SendFragment; |
26 | 12 | import io.scalaproject.vault.ledger.Ledger; |
27 | 13 | import io.scalaproject.vault.ledger.LedgerProgressDialog; |
28 | 14 |
|
29 | | -import java.io.IOException; |
30 | 15 |
|
31 | 16 | import timber.log.Timber; |
32 | 17 |
|
@@ -122,175 +107,9 @@ void releaseWakeLock() { |
122 | 107 | Timber.d("WakeLock released"); |
123 | 108 | } |
124 | 109 |
|
125 | | - |
126 | | - @Override |
127 | | - protected void onCreate(@Nullable Bundle savedInstanceState) { |
128 | | - super.onCreate(savedInstanceState); |
129 | | - initNfc(); |
130 | | - } |
131 | | - |
132 | | - @Override |
133 | | - protected void onPostResume() { |
134 | | - super.onPostResume(); |
135 | | - if (nfcAdapter != null) { |
136 | | - nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, null, null); |
137 | | - // intercept all techs so we can tell the user their tag is no good |
138 | | - } |
139 | | - } |
140 | | - |
141 | | - @Override |
142 | | - protected void onPause() { |
143 | | - Timber.d("onPause()"); |
144 | | - if (nfcAdapter != null) |
145 | | - nfcAdapter.disableForegroundDispatch(this); |
146 | | - super.onPause(); |
147 | | - } |
148 | | - |
149 | | - @Override |
150 | | - protected void onNewIntent(Intent intent) { |
151 | | - super.onNewIntent(intent); |
152 | | - processNfcIntent(intent); |
153 | | - } |
154 | | - |
155 | | - // NFC stuff |
156 | | - private NfcAdapter nfcAdapter; |
157 | | - private PendingIntent nfcPendingIntent; |
158 | | - |
159 | | - public void initNfc() { |
160 | | - nfcAdapter = NfcAdapter.getDefaultAdapter(this); |
161 | | - if (nfcAdapter == null) // no NFC support |
162 | | - return; |
163 | | - |
164 | | - nfcPendingIntent = PendingIntent.getActivity(this, 0, |
165 | | - new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), |
166 | | - PendingIntent.FLAG_IMMUTABLE); |
167 | | - } |
168 | | - |
169 | | - private void processNfcIntent(Intent intent) { |
170 | | - String action = intent.getAction(); |
171 | | - Timber.d("ACTION=%s", action); |
172 | | - if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action) |
173 | | - || NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) |
174 | | - || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) { |
175 | | - Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); |
176 | | - Ndef ndef = Ndef.get(tag); |
177 | | - if (ndef == null) { |
178 | | - Toast.makeText(this, getString(R.string.nfc_tag_unsupported), Toast.LENGTH_LONG).show(); |
179 | | - return; |
180 | | - } |
181 | | - |
182 | | - Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container); |
183 | | - if (f instanceof ReceiveFragment) { |
184 | | - // We want to write a Tag from the ReceiveFragment |
185 | | - BarcodeData bc = ((ReceiveFragment) f).getBarcodeData(); |
186 | | - if (bc != null) { |
187 | | - new AsyncWriteTag(ndef, bc.getUri()).execute(); |
188 | | - } // else wallet is not loaded yet or receive is otherwise not ready - ignore |
189 | | - } else if (f instanceof SendFragment) { |
190 | | - // We want to read a Tag for the SendFragment |
191 | | - NdefMessage ndefMessage = ndef.getCachedNdefMessage(); |
192 | | - if (ndefMessage == null) { |
193 | | - Toast.makeText(this, getString(R.string.nfc_tag_read_undef), Toast.LENGTH_LONG).show(); |
194 | | - return; |
195 | | - } |
196 | | - NdefRecord firstRecord = ndefMessage.getRecords()[0]; |
197 | | - Uri uri = firstRecord.toUri(); // we insist on the first record |
198 | | - if (uri == null) { |
199 | | - Toast.makeText(this, getString(R.string.nfc_tag_read_undef), Toast.LENGTH_LONG).show(); |
200 | | - } else { |
201 | | - BarcodeData.fromString(uri.toString(), data -> runOnUiThread(() -> { |
202 | | - if (data == null) |
203 | | - Toast.makeText(BaseActivity.this, getString(R.string.nfc_tag_read_undef), Toast.LENGTH_LONG).show(); |
204 | | - else |
205 | | - onUriScanned(data); |
206 | | - })); |
207 | | - } |
208 | | - } |
209 | | - } |
210 | | - } |
211 | | - |
212 | 110 | // this gets called only if we get data |
213 | 111 | @CallSuper |
214 | 112 | void onUriScanned(BarcodeData barcodeData) { |
215 | 113 | // do nothing by default yet |
216 | 114 | } |
217 | | - |
218 | | - private BarcodeData barcodeData = null; |
219 | | - |
220 | | - private BarcodeData popBarcodeData() { |
221 | | - BarcodeData popped = barcodeData; |
222 | | - barcodeData = null; |
223 | | - return popped; |
224 | | - } |
225 | | - |
226 | | - private class AsyncWriteTag extends AsyncTask<Void, Void, Boolean> { |
227 | | - |
228 | | - Ndef ndef; |
229 | | - Uri uri; |
230 | | - String errorMessage = null; |
231 | | - |
232 | | - AsyncWriteTag(Ndef ndef, Uri uri) { |
233 | | - this.ndef = ndef; |
234 | | - this.uri = uri; |
235 | | - } |
236 | | - |
237 | | - @Override |
238 | | - protected void onPreExecute() { |
239 | | - super.onPreExecute(); |
240 | | - showProgressDialog(R.string.progress_nfc_write); |
241 | | - } |
242 | | - |
243 | | - @Override |
244 | | - protected Boolean doInBackground(Void... params) { |
245 | | - if (params.length != 0) return false; |
246 | | - try { |
247 | | - writeNdef(ndef, uri); |
248 | | - return true; |
249 | | - } catch (IOException | FormatException ex) { |
250 | | - Timber.e(ex); |
251 | | - } catch (IllegalArgumentException ex) { |
252 | | - errorMessage = ex.getMessage(); |
253 | | - Timber.d(errorMessage); |
254 | | - } finally { |
255 | | - try { |
256 | | - ndef.close(); |
257 | | - } catch (IOException ex) { |
258 | | - Timber.e(ex); |
259 | | - } |
260 | | - } |
261 | | - return false; |
262 | | - } |
263 | | - |
264 | | - @Override |
265 | | - protected void onPostExecute(Boolean result) { |
266 | | - super.onPostExecute(result); |
267 | | - if (isDestroyed()) { |
268 | | - return; |
269 | | - } |
270 | | - dismissProgressDialog(); |
271 | | - if (!result) { |
272 | | - if (errorMessage != null) |
273 | | - Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_LONG).show(); |
274 | | - else |
275 | | - Toast.makeText(getApplicationContext(), getString(R.string.nfc_write_failed), Toast.LENGTH_LONG).show(); |
276 | | - } else { |
277 | | - Toast.makeText(getApplicationContext(), getString(R.string.nfc_write_successful), Toast.LENGTH_SHORT).show(); |
278 | | - } |
279 | | - } |
280 | | - } |
281 | | - |
282 | | - void writeNdef(Ndef ndef, Uri uri) throws IOException, FormatException { |
283 | | - NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); |
284 | | - if (nfcAdapter == null) return; // no NFC support here |
285 | | - |
286 | | - NdefRecord recordNFC = NdefRecord.createUri(uri); |
287 | | - NdefMessage message = new NdefMessage(recordNFC); |
288 | | - ndef.connect(); |
289 | | - int tagSize = ndef.getMaxSize(); |
290 | | - int msgSize = message.getByteArrayLength(); |
291 | | - Timber.d("tagSize=%d, msgSIze=%d, uriSize=%d", tagSize, msgSize, uri.toString().length()); |
292 | | - if (tagSize < msgSize) |
293 | | - throw new IllegalArgumentException(getString(R.string.nfc_tag_size, tagSize, msgSize)); |
294 | | - ndef.writeNdefMessage(message); |
295 | | - } |
296 | 115 | } |
0 commit comments