MainActivity.kt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package com.example.uicceditor
  2. import android.os.Bundle
  3. import android.os.Handler
  4. import android.os.Looper
  5. import android.telephony.TelephonyManager
  6. import android.util.Log
  7. import android.widget.Toast
  8. import androidx.activity.enableEdgeToEdge
  9. import androidx.appcompat.app.AppCompatActivity
  10. import androidx.core.view.ViewCompat
  11. import androidx.core.view.WindowInsetsCompat
  12. import com.example.uicceditor.databinding.ActivityMainBinding
  13. class MainActivity : AppCompatActivity() {
  14. private val TAG = "APDU"
  15. private val handler = Handler(Looper.getMainLooper())
  16. val AID = "D07002CA44900101"
  17. private val binding: ActivityMainBinding by lazy {
  18. ActivityMainBinding.inflate(layoutInflater)
  19. }
  20. override fun onCreate(savedInstanceState: Bundle?) {
  21. super.onCreate(savedInstanceState)
  22. enableEdgeToEdge()
  23. setContentView(binding.root)
  24. ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
  25. val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
  26. v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
  27. insets
  28. }
  29. val telephonyManager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
  30. binding.btnReadIccid.setOnClickListener {
  31. val apduChannel = ApduChannel(telephonyManager, AID)
  32. val iccid = apduChannel.readICCID()
  33. apduChannel.close()
  34. binding.etIccid.setText(iccid)
  35. }
  36. binding.btnWriteIccid.setOnClickListener {
  37. val iccid = binding.etIccid.text.toString()
  38. if (Regex("^[0-9]{20}$").matches(iccid)) {
  39. val apduChannel = ApduChannel(telephonyManager, AID)
  40. apduChannel.writeICCID(iccid)
  41. apduChannel.close()
  42. Toast.makeText(this, "ICCID written", Toast.LENGTH_SHORT).show()
  43. } else {
  44. Toast.makeText(this, "Invalid ICCID", Toast.LENGTH_SHORT).show()
  45. }
  46. }
  47. binding.btnReadImsi.setOnClickListener {
  48. val apduChannel = ApduChannel(telephonyManager, AID)
  49. val imsi = apduChannel.readIMSI()
  50. apduChannel.close()
  51. binding.etImsi.setText(imsi)
  52. }
  53. binding.btnWriteImsi.setOnClickListener {
  54. try {
  55. val imsi = binding.etImsi.text.toString()
  56. if (Regex("^[0-9]{15,18}$").matches(imsi)) {
  57. val apduChannel = ApduChannel(telephonyManager, AID)
  58. apduChannel.writeIMSI(imsi)
  59. apduChannel.close()
  60. Toast.makeText(this, "IMSI written", Toast.LENGTH_SHORT).show()
  61. } else {
  62. Toast.makeText(this, "Invalid IMSI", Toast.LENGTH_SHORT).show()
  63. }
  64. } catch (e: Exception) {
  65. Log.e(TAG, "Error writing IMSI", e)
  66. Toast.makeText(this, "Error writing IMSI", Toast.LENGTH_SHORT).show()
  67. }
  68. }
  69. binding.btnReadMsisdn.setOnClickListener {
  70. val apduChannel = ApduChannel(telephonyManager, AID)
  71. apduChannel.select(SIMView.FID_MF)
  72. apduChannel.select(SIMView.FID_DF_TELECOM)
  73. apduChannel.select(SIMView.FID_EF_MSISDN)
  74. val msisdn = decMSISDN(apduChannel.readRecord(1, 28).substring(28)) ?: ""
  75. binding.etMsisdn.setText(msisdn)
  76. apduChannel.close()
  77. }
  78. binding.btnWriteMsisdn.setOnClickListener {
  79. val msisdn = binding.etMsisdn.text.toString()
  80. Log.i(TAG, "MSISDN: ${encMSISDN(msisdn)}")
  81. val apduChannel = ApduChannel(telephonyManager, AID)
  82. apduChannel.select(SIMView.FID_MF)
  83. apduChannel.select(SIMView.FID_DF_TELECOM)
  84. apduChannel.select(SIMView.FID_EF_MSISDN)
  85. apduChannel.writeRecord(1, encMSISDN(msisdn).padStart(56, 'F'))
  86. apduChannel.close()
  87. }
  88. binding.btnReadPlmn.setOnClickListener {
  89. val apduChannel = ApduChannel(telephonyManager, AID)
  90. apduChannel.select(SIMView.FID_MF)
  91. apduChannel.select(SIMView.FID_DF_GSM)
  92. apduChannel.select(SIMView.FID_EF_PLMNSEL)
  93. binding.etPlmn.setText(decPLMN(apduChannel.readBinary(60)))
  94. apduChannel.close()
  95. }
  96. binding.btnReadEhplmn.setOnClickListener {
  97. val apduChannel = ApduChannel(telephonyManager, AID)
  98. apduChannel.select(SIMView.FID_MF)
  99. apduChannel.select(SIMView.FID_DF_GSM)
  100. apduChannel.select(SIMView.FID_EF_EHPLMN)
  101. binding.etEhplmn.setText(decPLMN(apduChannel.readBinary(12)))
  102. apduChannel.close()
  103. }
  104. binding.btnReadFplmn.setOnClickListener {
  105. val apduChannel = ApduChannel(telephonyManager, AID)
  106. apduChannel.select(SIMView.FID_MF)
  107. apduChannel.select(SIMView.FID_DF_GSM)
  108. apduChannel.select(SIMView.FID_EF_FPLMN)
  109. binding.etFplmn.setText(decPLMN(apduChannel.readBinary(30)))
  110. apduChannel.close()
  111. }
  112. binding.btnReadPlmnwact.setOnClickListener{
  113. val apduChannel = ApduChannel(telephonyManager, AID)
  114. apduChannel.select(SIMView.FID_MF)
  115. apduChannel.select(SIMView.FID_DF_GSM)
  116. apduChannel.select(SIMView.FID_EF_PLMNWACT)
  117. binding.etPlmnwact.setText(decPLMNwAcT(apduChannel.readBinary(120)))
  118. apduChannel.close()
  119. }
  120. binding.btnReadOplmnwact.setOnClickListener{
  121. val apduChannel = ApduChannel(telephonyManager, AID)
  122. apduChannel.select(SIMView.FID_MF)
  123. apduChannel.select(SIMView.FID_DF_GSM)
  124. apduChannel.select(SIMView.FID_EF_OPLMNWACT)
  125. binding.etOplmnwact.setText(decPLMNwAcT(apduChannel.readBinary(60)))
  126. apduChannel.close()
  127. }
  128. binding.btnReadHplmnwact.setOnClickListener{
  129. val apduChannel = ApduChannel(telephonyManager, AID)
  130. apduChannel.select(SIMView.FID_MF)
  131. apduChannel.select(SIMView.FID_DF_GSM)
  132. apduChannel.select(SIMView.FID_EF_HPLMNWACT)
  133. binding.etHplmnwact.setText(decPLMNwAcT(apduChannel.readBinary(20)))
  134. apduChannel.close()
  135. }
  136. binding.btnReset.setOnClickListener {
  137. val apduChannel = ApduChannel(telephonyManager, AID)
  138. apduChannel.reset()
  139. apduChannel.close()
  140. }
  141. }
  142. fun getRandomString(length: Int): String {
  143. val allowedChars = ('0'..'9')
  144. return (1..length)
  145. .map { allowedChars.random() }
  146. .joinToString("")
  147. }
  148. }