@@ -3,8 +3,6 @@ package com.qifan.nfcbank
3
3
import android.content.Intent
4
4
import android.content.pm.PackageManager
5
5
import android.nfc.NfcAdapter
6
- import android.os.Build
7
- import android.os.Build.VERSION_CODES.JELLY_BEAN
8
6
import android.os.Bundle
9
7
import android.text.TextUtils
10
8
import android.view.View
@@ -14,6 +12,7 @@ import android.widget.TextView
14
12
import android.widget.Toast
15
13
import androidx.appcompat.app.AlertDialog
16
14
import androidx.appcompat.app.AppCompatActivity
15
+ import com.google.android.material.dialog.MaterialAlertDialogBuilder
17
16
import com.qifan.nfcbank.cardEmulation.KHostApduService
18
17
19
18
/* *
@@ -35,13 +34,10 @@ class CardEmulatorActivity : AppCompatActivity() {
35
34
editText = findViewById<View >(R .id.editText) as EditText
36
35
textView = findViewById<View >(R .id.textView) as TextView
37
36
initNFCFunction()
38
-
39
37
}
40
38
41
-
42
39
private fun initNFCFunction () {
43
-
44
- if (checkNFCEnable() && packageManager.hasSystemFeature(PackageManager .FEATURE_NFC_HOST_CARD_EMULATION )) {
40
+ if (supportNfcHceFeature()) {
45
41
textView.visibility = View .GONE
46
42
editText.visibility = View .VISIBLE
47
43
button.visibility = View .VISIBLE
@@ -50,64 +46,63 @@ class CardEmulatorActivity : AppCompatActivity() {
50
46
textView.visibility = View .VISIBLE
51
47
editText.visibility = View .GONE
52
48
button.visibility = View .GONE
53
- showTurnOnNfcDialog()
49
+ // Prevent phone that doesn't support NFC to trigger dialog
50
+ if (supportNfcHceFeature()) {
51
+ showTurnOnNfcDialog()
52
+ }
54
53
}
55
54
}
56
55
56
+ private fun supportNfcHceFeature () =
57
+ checkNFCEnable() && packageManager.hasSystemFeature(PackageManager .FEATURE_NFC_HOST_CARD_EMULATION )
58
+
57
59
private fun initService () {
58
60
button.setOnClickListener {
59
61
if (TextUtils .isEmpty(editText.text)) {
60
62
Toast .makeText(
61
63
this @CardEmulatorActivity,
62
64
getString(R .string.toast_msg),
63
- Toast .LENGTH_LONG
65
+ Toast .LENGTH_LONG ,
64
66
).show()
65
67
} else {
66
68
val intent = Intent (this @CardEmulatorActivity, KHostApduService ::class .java)
67
69
intent.putExtra(" ndefMessage" , editText.text.toString())
68
70
startService(intent)
69
71
}
70
72
}
71
-
72
73
}
73
74
74
75
private fun checkNFCEnable (): Boolean {
75
76
return if (mNfcAdapter == null ) {
76
77
textView.text = getString(R .string.tv_noNfc)
77
78
false
78
79
} else {
79
- mNfcAdapter!! .isEnabled
80
+ mNfcAdapter? .isEnabled == true
80
81
}
81
82
}
82
83
83
84
private fun showTurnOnNfcDialog () {
84
- mTurnNfcDialog = AlertDialog . Builder (this )
85
+ mTurnNfcDialog = MaterialAlertDialogBuilder (this , R .style. MaterialAlertDialog_rounded )
85
86
.setTitle(getString(R .string.ad_nfcTurnOn_title))
86
87
.setMessage(getString(R .string.ad_nfcTurnOn_message))
87
88
.setPositiveButton(
88
- getString(R .string.ad_nfcTurnOn_pos)
89
+ getString(R .string.ad_nfcTurnOn_pos),
89
90
) { _, _ ->
90
- if (Build .VERSION .SDK_INT >= JELLY_BEAN ) {
91
- startActivity(Intent (android.provider.Settings .ACTION_NFC_SETTINGS ))
92
- } else {
93
- startActivity(Intent (android.provider.Settings .ACTION_NFC_SETTINGS ))
94
- }
91
+ startActivity(Intent (android.provider.Settings .ACTION_NFC_SETTINGS ))
95
92
}.setNegativeButton(getString(R .string.ad_nfcTurnOn_neg)) { _, _ ->
96
- onBackPressed()
93
+ onBackPressedDispatcher. onBackPressed()
97
94
}
98
95
.create()
99
96
mTurnNfcDialog.show()
100
97
}
101
98
102
99
override fun onResume () {
103
100
super .onResume()
104
- if (mNfcAdapter!! .isEnabled) {
101
+ if (mNfcAdapter? .isEnabled == true ) {
105
102
textView.visibility = View .GONE
106
103
editText.visibility = View .VISIBLE
107
104
button.visibility = View .VISIBLE
108
105
initService()
109
106
}
110
107
}
111
-
112
-
113
- }
108
+ }
0 commit comments