package com.dmc.callcrm.ui import android.Manifest import android.content.pm.PackageManager import android.os.Bundle import android.telephony.SubscriptionManager import android.widget.LinearLayout import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import com.dmc.callcrm.R import com.dmc.callcrm.data.Prefs /** * Lets the user pick which SIM this phone's registration should be locked to. * Saves the chosen slot into Prefs.simSlot, which SimUtil/CallStats/ * RecordingScanner/Syncer all read to filter out the other SIM's calls. * Registration itself never asks for this, so this screen is how it gets set. */ class ChooseSimActivity : AppCompatActivity() { private lateinit var prefs: Prefs override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_choose_sim) prefs = Prefs(this) val list = findViewById(R.id.simList) val statusText = findViewById(R.id.statusText) if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { statusText.setTextColor(0xFFDC2626.toInt()) statusText.text = "Phone permission is needed to list SIMs. Grant it from Settings and reopen this screen." return } val subManager = getSystemService(SubscriptionManager::class.java) val subs = try { subManager?.activeSubscriptionInfoList } catch (_: SecurityException) { null } if (subs.isNullOrEmpty()) { statusText.setTextColor(0xFFDC2626.toInt()) statusText.text = "Couldn't read SIM info on this device." return } for (sub in subs) { val slot = sub.simSlotIndex val label = "SIM ${slot + 1}: ${sub.displayName ?: sub.carrierName ?: "Unknown carrier"}" list.addView(makeRow(label, slot, slot == prefs.simSlot) { prefs.simSlot = slot statusText.setTextColor(0xFF16A34A.toInt()) statusText.text = "Saved — this phone will now only sync/record SIM ${slot + 1}." refreshSelection(list, slot) }) } // Escape hatch: go back to "don't filter, use both SIMs." list.addView(makeRow("Don't filter — use both SIMs", -1, prefs.simSlot < 0) { prefs.simSlot = -1 statusText.setTextColor(0xFF16A34A.toInt()) statusText.text = "Saved — both SIMs will be synced/recorded again." refreshSelection(list, -1) }) } private fun refreshSelection(list: LinearLayout, selectedSlot: Int) { for (i in 0 until list.childCount) { val row = list.getChildAt(i) val isSelected = row.tag == selectedSlot row.setBackgroundColor(if (isSelected) 0xFFDCFCE7.toInt() else 0xFFFFFFFF.toInt()) } } private fun makeRow(label: String, slot: Int, selected: Boolean, onClick: () -> Unit): TextView { return TextView(this).apply { text = (if (selected) "✓ " else "◯ ") + label textSize = 15f setTextColor(0xFF0F172A.toInt()) setPadding(20, 20, 20, 20) setBackgroundColor(if (selected) 0xFFDCFCE7.toInt() else 0xFFFFFFFF.toInt()) val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT) params.bottomMargin = 10 layoutParams = params tag = slot setOnClickListener { onClick() Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show() } } } }