| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package com.example.uicceditor
- import android.os.Bundle
- import android.os.Handler
- import android.os.Looper
- import android.telephony.TelephonyManager
- import android.util.Log
- import android.widget.Toast
- import androidx.activity.enableEdgeToEdge
- import androidx.appcompat.app.AppCompatActivity
- import androidx.core.view.ViewCompat
- import androidx.core.view.WindowInsetsCompat
- import com.example.uicceditor.databinding.ActivityMainBinding
- class MainActivity : AppCompatActivity() {
- private val TAG = "APDU"
- private val handler = Handler(Looper.getMainLooper())
- private lateinit var apduChannel: ApduChannel
- private val binding: ActivityMainBinding by lazy {
- ActivityMainBinding.inflate(layoutInflater)
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- enableEdgeToEdge()
- setContentView(binding.root)
- ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
- val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
- v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
- insets
- }
- val sharedPrefs = getSharedPreferences(applicationInfo.packageName, MODE_PRIVATE)
- binding.etAdm.setText(sharedPrefs.getString("adm", ""))
- apduChannel = ApduChannel(getSystemService(TELEPHONY_SERVICE) as TelephonyManager)
- binding.btnAuth.setOnClickListener {
- val admKey = binding.etAdm.text.toString()
- if (Regex("^\\d{8}$").matches(admKey)) {
- apduChannel.authorize(admKey)
- Toast.makeText(this, "Authorized", Toast.LENGTH_SHORT).show()
- sharedPrefs.edit().putString("adm", admKey).apply()
- } else {
- Toast.makeText(this, "Invalid ADM", Toast.LENGTH_SHORT).show()
- }
- }
- binding.btnReadIccid.setOnClickListener {
- val iccid = apduChannel.readIccid()
- binding.etIccid.setText(iccid)
- }
- binding.btnWriteIccid.setOnClickListener {
- if (!apduChannel.authorized) {
- Toast.makeText(this, "Not authorized", Toast.LENGTH_SHORT).show()
- return@setOnClickListener
- }
- val iccid = binding.etIccid.text.toString()
- if (Regex("^[0-9]{20}$").matches(iccid)) {
- apduChannel.writeIccid(iccid)
- Toast.makeText(this, "ICCID written", Toast.LENGTH_SHORT).show()
- } else {
- Toast.makeText(this, "Invalid ICCID", Toast.LENGTH_SHORT).show()
- }
- }
- binding.btnReadImsi.setOnClickListener {
- val imsi = apduChannel.readImsi()
- binding.etImsi.setText(imsi)
- }
- binding.btnWriteImsi.setOnClickListener {
- if (!apduChannel.authorized) {
- Toast.makeText(this, "Not authorized", Toast.LENGTH_SHORT).show()
- return@setOnClickListener
- }
- try {
- val imsi = binding.etImsi.text.toString()
- if (Regex("^[0-9]{15,18}$").matches(imsi)) {
- apduChannel.writeImsi(imsi)
- Toast.makeText(this, "IMSI written", Toast.LENGTH_SHORT).show()
- } else {
- Toast.makeText(this, "Invalid IMSI", Toast.LENGTH_SHORT).show()
- }
- } catch (e: Exception) {
- Log.e(TAG, "Error writing IMSI", e)
- Toast.makeText(this, "Error writing IMSI", Toast.LENGTH_SHORT).show()
- }
- }
- }
- fun getRandomString(length: Int): String {
- val allowedChars = ('0'..'9')
- return (1..length)
- .map { allowedChars.random() }
- .joinToString("")
- }
- }
|