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.ApiConfig import com.dmc.callcrm.data.Prefs import com.dmc.callcrm.sync.RecordingWorker /** * Drives recording + sync from call state: * OFFHOOK (call active) -> start mic recording * IDLE (call ended) -> stop + upload, sync call log, harvest OEM files */ 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 micMode = ApiConfig.recordingMode() == "mic" || ApiConfig.recordingMode() == "both" val oemMode = ApiConfig.recordingMode() == "oem" || ApiConfig.recordingMode() == "both" when (intent.getStringExtra(TelephonyManager.EXTRA_STATE)) { TelephonyManager.EXTRA_STATE_OFFHOOK -> { // Call is now connected/active -> begin mic capture. if (micMode) CallMonitorService.startRecording(context) } TelephonyManager.EXTRA_STATE_IDLE -> { // Call ended. if (micMode) CallMonitorService.stopRecording(context) else CallMonitorService.start(context) // still trigger a sync // Harvest any file the built-in recorder wrote (if that mode is on). if (oemMode) RecordingWorker.runSoon(context) } else -> { /* RINGING: ignore */ } } } }