package com.dmc.callcrm.service import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.telephony.TelephonyManager import com.dmc.callcrm.data.Prefs /** * Fires on PHONE_STATE changes. When a call transitions back to IDLE * (i.e. a call just ended), the log now has a fresh row, so we kick a sync. */ class PhoneStateReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (intent.action != TelephonyManager.ACTION_PHONE_STATE_CHANGED) return if (!Prefs(context).isRegistered) return val state = intent.getStringExtra(TelephonyManager.EXTRA_STATE) if (state == TelephonyManager.EXTRA_STATE_IDLE) { // The row for the just-ended call is written slightly after IDLE, // so let the foreground service do the read (it starts a coroutine). CallMonitorService.start(context) // Give the phone's recorder ~20s to finish writing the audio, then upload. com.dmc.callcrm.sync.RecordingWorker.runSoon(context) } } }