Skip to content

Commit 33f147d

Browse files
committed
Android >=12 fixes for card emulation and some code clean up
1 parent 6811553 commit 33f147d

File tree

17 files changed

+143
-155
lines changed

17 files changed

+143
-155
lines changed

.idea/runConfigurations.xml

-12
This file was deleted.

app/build.gradle

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ apply plugin: 'com.android.application'
22

33
apply plugin: 'kotlin-android'
44

5-
apply plugin: 'kotlin-android-extensions'
65

76
android {
8-
compileSdkVersion 29
7+
compileSdkVersion 33
98
defaultConfig {
109
applicationId "com.qifan.nfcbank"
1110
minSdkVersion 21
12-
targetSdkVersion 29
11+
targetSdkVersion 33
1312
versionCode 1
1413
versionName "1.0"
1514
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

app/src/main/AndroidManifest.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
android:supportsRtl="true"
1919
android:theme="@style/AppTheme"
2020
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
21-
<activity android:name=".CardEmulatorActivity">
21+
<activity android:name=".CardEmulatorActivity"
22+
android:exported="true">
2223
<intent-filter>
2324
<action android:name="android.intent.action.MAIN"/>
2425

app/src/main/java/com/qifan/nfcbank/CardEmulatorActivity.kt

+17-22
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package com.qifan.nfcbank
33
import android.content.Intent
44
import android.content.pm.PackageManager
55
import android.nfc.NfcAdapter
6-
import android.os.Build
7-
import android.os.Build.VERSION_CODES.JELLY_BEAN
86
import android.os.Bundle
97
import android.text.TextUtils
108
import android.view.View
@@ -14,6 +12,7 @@ import android.widget.TextView
1412
import android.widget.Toast
1513
import androidx.appcompat.app.AlertDialog
1614
import androidx.appcompat.app.AppCompatActivity
15+
import com.google.android.material.dialog.MaterialAlertDialogBuilder
1716
import com.qifan.nfcbank.cardEmulation.KHostApduService
1817

1918
/**
@@ -35,13 +34,10 @@ class CardEmulatorActivity : AppCompatActivity() {
3534
editText = findViewById<View>(R.id.editText) as EditText
3635
textView = findViewById<View>(R.id.textView) as TextView
3736
initNFCFunction()
38-
3937
}
4038

41-
4239
private fun initNFCFunction() {
43-
44-
if (checkNFCEnable() && packageManager.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
40+
if (supportNfcHceFeature()) {
4541
textView.visibility = View.GONE
4642
editText.visibility = View.VISIBLE
4743
button.visibility = View.VISIBLE
@@ -50,64 +46,63 @@ class CardEmulatorActivity : AppCompatActivity() {
5046
textView.visibility = View.VISIBLE
5147
editText.visibility = View.GONE
5248
button.visibility = View.GONE
53-
showTurnOnNfcDialog()
49+
// Prevent phone that doesn't support NFC to trigger dialog
50+
if (supportNfcHceFeature()) {
51+
showTurnOnNfcDialog()
52+
}
5453
}
5554
}
5655

56+
private fun supportNfcHceFeature() =
57+
checkNFCEnable() && packageManager.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
58+
5759
private fun initService() {
5860
button.setOnClickListener {
5961
if (TextUtils.isEmpty(editText.text)) {
6062
Toast.makeText(
6163
this@CardEmulatorActivity,
6264
getString(R.string.toast_msg),
63-
Toast.LENGTH_LONG
65+
Toast.LENGTH_LONG,
6466
).show()
6567
} else {
6668
val intent = Intent(this@CardEmulatorActivity, KHostApduService::class.java)
6769
intent.putExtra("ndefMessage", editText.text.toString())
6870
startService(intent)
6971
}
7072
}
71-
7273
}
7374

7475
private fun checkNFCEnable(): Boolean {
7576
return if (mNfcAdapter == null) {
7677
textView.text = getString(R.string.tv_noNfc)
7778
false
7879
} else {
79-
mNfcAdapter!!.isEnabled
80+
mNfcAdapter?.isEnabled == true
8081
}
8182
}
8283

8384
private fun showTurnOnNfcDialog() {
84-
mTurnNfcDialog = AlertDialog.Builder(this)
85+
mTurnNfcDialog = MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialog_rounded)
8586
.setTitle(getString(R.string.ad_nfcTurnOn_title))
8687
.setMessage(getString(R.string.ad_nfcTurnOn_message))
8788
.setPositiveButton(
88-
getString(R.string.ad_nfcTurnOn_pos)
89+
getString(R.string.ad_nfcTurnOn_pos),
8990
) { _, _ ->
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))
9592
}.setNegativeButton(getString(R.string.ad_nfcTurnOn_neg)) { _, _ ->
96-
onBackPressed()
93+
onBackPressedDispatcher.onBackPressed()
9794
}
9895
.create()
9996
mTurnNfcDialog.show()
10097
}
10198

10299
override fun onResume() {
103100
super.onResume()
104-
if (mNfcAdapter!!.isEnabled) {
101+
if (mNfcAdapter?.isEnabled == true) {
105102
textView.visibility = View.GONE
106103
editText.visibility = View.VISIBLE
107104
button.visibility = View.VISIBLE
108105
initService()
109106
}
110107
}
111-
112-
113-
}
108+
}

0 commit comments

Comments
 (0)