1
+ package com.qifan.nfcbank
2
+
3
+ import android.content.Intent
4
+ import android.nfc.NfcAdapter
5
+ import android.os.Build
6
+ import android.os.Build.VERSION_CODES.JELLY_BEAN
7
+ import android.os.Bundle
8
+ import android.support.v7.app.AlertDialog
9
+ import android.support.v7.app.AppCompatActivity
10
+ import android.text.TextUtils
11
+ import android.view.View
12
+ import android.widget.Button
13
+ import android.widget.EditText
14
+ import android.widget.TextView
15
+ import android.widget.Toast
16
+ import com.qifan.nfcbank.cardEmulation.KHostApduService
17
+
18
+ /* *
19
+ * Created by Qifan on 28/11/2018.
20
+ */
21
+ class CardEmulatorActivity : AppCompatActivity () {
22
+ private var mNfcAdapter: NfcAdapter ? = null
23
+ private lateinit var button: Button
24
+ private lateinit var editText: EditText
25
+ private lateinit var textView: TextView
26
+ private lateinit var mTurnNfcDialog: AlertDialog
27
+
28
+ override fun onCreate (savedInstanceState : Bundle ? ) {
29
+ super .onCreate(savedInstanceState)
30
+ setContentView(R .layout.card_emulator)
31
+
32
+ mNfcAdapter = NfcAdapter .getDefaultAdapter(this )
33
+ button = findViewById<View >(R .id.button) as Button
34
+ editText = findViewById<View >(R .id.editText) as EditText
35
+ textView = findViewById<View >(R .id.textView) as TextView
36
+ if (checkNFCEnable()) {
37
+ textView.visibility = View .GONE
38
+ editText.visibility = View .VISIBLE
39
+ button.visibility = View .VISIBLE
40
+ initService()
41
+ } else {
42
+ textView.visibility = View .VISIBLE
43
+ editText.visibility = View .GONE
44
+ button.visibility = View .GONE
45
+ showTurnOnNfcDialog()
46
+
47
+ }
48
+
49
+
50
+ }
51
+
52
+ private fun initService () {
53
+ button.setOnClickListener {
54
+ if (TextUtils .isEmpty(editText.text)) {
55
+ Toast .makeText(this @CardEmulatorActivity, getString(R .string.toast_msg), Toast .LENGTH_LONG ).show()
56
+ } else {
57
+ val intent = Intent (this @CardEmulatorActivity, KHostApduService ::class .java)
58
+ intent.putExtra(" ndefMessage" , editText.text.toString())
59
+ startService(intent)
60
+ }
61
+ }
62
+
63
+ }
64
+
65
+ private fun checkNFCEnable (): Boolean {
66
+ return if (mNfcAdapter == null ) {
67
+ textView.text = getString(R .string.tv_noNfc)
68
+ false
69
+ } else {
70
+ mNfcAdapter!! .isEnabled
71
+ }
72
+ }
73
+
74
+ private fun showTurnOnNfcDialog () {
75
+ mTurnNfcDialog = AlertDialog .Builder (this )
76
+ .setTitle(getString(R .string.ad_nfcTurnOn_title))
77
+ .setMessage(getString(R .string.ad_nfcTurnOn_message))
78
+ .setPositiveButton(
79
+ getString(R .string.ad_nfcTurnOn_pos)
80
+ ) { _, _ ->
81
+ if (Build .VERSION .SDK_INT >= JELLY_BEAN ) {
82
+ startActivity(Intent (android.provider.Settings .ACTION_NFC_SETTINGS ))
83
+ } else {
84
+ startActivity(Intent (android.provider.Settings .ACTION_NFC_SETTINGS ))
85
+ }
86
+ }.setNegativeButton(getString(R .string.ad_nfcTurnOn_neg)) { _, _ ->
87
+ onBackPressed()
88
+ }
89
+ .create()
90
+ mTurnNfcDialog.show()
91
+ }
92
+
93
+
94
+ }
0 commit comments