MainActivity.kt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. private lateinit var apduChannel: ApduChannel
  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 sharedPrefs = getSharedPreferences(applicationInfo.packageName, MODE_PRIVATE)
  30. binding.etAdm.setText(sharedPrefs.getString("adm", ""))
  31. apduChannel = ApduChannel(getSystemService(TELEPHONY_SERVICE) as TelephonyManager)
  32. binding.btnAuth.setOnClickListener {
  33. val admKey = binding.etAdm.text.toString()
  34. if (Regex("^\\d{8}$").matches(admKey)) {
  35. apduChannel.authorize(admKey)
  36. Toast.makeText(this, "Authorized", Toast.LENGTH_SHORT).show()
  37. sharedPrefs.edit().putString("adm", admKey).apply()
  38. } else {
  39. Toast.makeText(this, "Invalid ADM", Toast.LENGTH_SHORT).show()
  40. }
  41. }
  42. binding.btnReadIccid.setOnClickListener {
  43. val iccid = apduChannel.readIccid()
  44. binding.etIccid.setText(iccid)
  45. }
  46. binding.btnWriteIccid.setOnClickListener {
  47. if (!apduChannel.authorized) {
  48. Toast.makeText(this, "Not authorized", Toast.LENGTH_SHORT).show()
  49. return@setOnClickListener
  50. }
  51. val iccid = binding.etIccid.text.toString()
  52. if (Regex("^[0-9]{20}$").matches(iccid)) {
  53. apduChannel.writeIccid(iccid)
  54. Toast.makeText(this, "ICCID written", Toast.LENGTH_SHORT).show()
  55. } else {
  56. Toast.makeText(this, "Invalid ICCID", Toast.LENGTH_SHORT).show()
  57. }
  58. }
  59. binding.btnReadImsi.setOnClickListener {
  60. val imsi = apduChannel.readImsi()
  61. binding.etImsi.setText(imsi)
  62. }
  63. binding.btnWriteImsi.setOnClickListener {
  64. if (!apduChannel.authorized) {
  65. Toast.makeText(this, "Not authorized", Toast.LENGTH_SHORT).show()
  66. return@setOnClickListener
  67. }
  68. try {
  69. val imsi = binding.etImsi.text.toString()
  70. if (Regex("^[0-9]{15,18}$").matches(imsi)) {
  71. apduChannel.writeImsi(imsi)
  72. Toast.makeText(this, "IMSI written", Toast.LENGTH_SHORT).show()
  73. } else {
  74. Toast.makeText(this, "Invalid IMSI", Toast.LENGTH_SHORT).show()
  75. }
  76. } catch (e: Exception) {
  77. Log.e(TAG, "Error writing IMSI", e)
  78. Toast.makeText(this, "Error writing IMSI", Toast.LENGTH_SHORT).show()
  79. }
  80. }
  81. }
  82. fun getRandomString(length: Int): String {
  83. val allowedChars = ('0'..'9')
  84. return (1..length)
  85. .map { allowedChars.random() }
  86. .joinToString("")
  87. }
  88. }