Skip to content

Commit

Permalink
Check if NFC is enabled in onResume activity
Browse files Browse the repository at this point in the history
This essentially forces the user to enable NFC to even be able to enter
the passport number etc., but you can't really do anything with the app
anyways. So I figured it was better to just forcefully request the user
to enable it?

It shows the help text in a toast message.

Fixes tananaev#49
  • Loading branch information
nickoe committed Jan 11, 2023
1 parent 5cdbbcf commit 8757f8d
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions app/src/main/java/com/tananaev/passportreader/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
import com.tananaev.passportreader.ImageUtil.decodeImage
Expand Down Expand Up @@ -164,11 +165,22 @@ abstract class MainActivity : AppCompatActivity() {
super.onResume()
val adapter = NfcAdapter.getDefaultAdapter(this)
if (adapter != null) {
val intent = Intent(applicationContext, this.javaClass)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE)
val filter = arrayOf(arrayOf("android.nfc.tech.IsoDep"))
adapter.enableForegroundDispatch(this, pendingIntent, null, filter)
if (adapter.isEnabled) {
val intent = Intent(applicationContext, this.javaClass)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
val pendingIntent =
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE)
val filter = arrayOf(arrayOf("android.nfc.tech.IsoDep"))
adapter.enableForegroundDispatch(this, pendingIntent, null, filter)
} else {
Toast.makeText(
getApplicationContext(),
"Please activate NFC and press Back to return to the application!",
Toast.LENGTH_LONG
).show();
Thread.sleep(5_000)
startActivity(Intent(android.provider.Settings.ACTION_NFC_SETTINGS));
}
}
if (passportNumberFromIntent) {
// When the passport number field is populated from the caller, we hide the
Expand Down

0 comments on commit 8757f8d

Please sign in to comment.