package com.dmc.callcrm.ui import android.Manifest import android.content.Intent import android.content.pm.PackageManager import android.media.MediaRecorder import android.os.Build import android.os.Bundle import android.widget.TextView import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.core.content.FileProvider import androidx.lifecycle.lifecycleScope import com.dmc.callcrm.ApiConfig import com.dmc.callcrm.R import kotlinx.coroutines.delay import kotlinx.coroutines.launch import java.io.File /** * Standalone mic test — records 5s using the EXACT same MediaRecorder setup * as CallMonitorService's call recording, but outside of a call. Lets you * share the result directly (WhatsApp/email/etc.) to compare against a real * call recording, to tell apart "our recording code is broken" from * "Android mutes the mic specifically during calls on this device." */ class TestMicActivity : AppCompatActivity() { private lateinit var statusText: TextView private var lastFile: File? = null private val micPermissionLauncher = registerForActivityResult( ActivityResultContracts.RequestPermission() ) { granted -> if (granted) doRecording() else statusText.text = "Microphone permission is needed for this test." } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_test_mic) statusText = findViewById(R.id.statusText) findViewById(R.id.btnRecord).setOnClickListener { if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) { doRecording() } else { micPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO) } } findViewById(R.id.btnShare).setOnClickListener { shareLastRecording() } } private fun doRecording() { val btn = findViewById(R.id.btnRecord) btn.isEnabled = false findViewById(R.id.btnShare).visibility = android.view.View.GONE statusText.text = "Recording… talk now (5 seconds)" val dir = File(getExternalFilesDir(null), "callrec").apply { mkdirs() } val f = File(dir, "mictest_${System.currentTimeMillis()}.m4a") @Suppress("DEPRECATION") val r = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) MediaRecorder(this) else MediaRecorder() try { r.setAudioSource(MediaRecorder.AudioSource.MIC) r.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4) r.setAudioEncoder(MediaRecorder.AudioEncoder.AAC) r.setAudioSamplingRate(ApiConfig.MIC_SAMPLE_RATE) r.setAudioEncodingBitRate(ApiConfig.MIC_BITRATE) r.setOutputFile(f.absolutePath) r.prepare() r.start() } catch (e: Exception) { statusText.text = "Couldn't start recording: ${e.message}" btn.isEnabled = true return } lifecycleScope.launch { delay(5000) try { r.stop() } catch (e: Exception) { statusText.text = "Recording failed to stop cleanly: ${e.message}" } try { r.release() } catch (_: Exception) {} btn.isEnabled = true if (f.exists() && f.length() > 0) { lastFile = f statusText.text = "Done — ${f.length()} bytes. Tap below to share it." findViewById(R.id.btnShare).visibility = android.view.View.VISIBLE } else { statusText.text = "No file was produced." } } } private fun shareLastRecording() { val f = lastFile ?: return val uri = FileProvider.getUriForFile(this, "$packageName.fileprovider", f) val intent = Intent(Intent.ACTION_SEND).apply { type = "audio/mp4" putExtra(Intent.EXTRA_STREAM, uri) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) } try { startActivity(Intent.createChooser(intent, "Share mic test recording")) } catch (e: Exception) { Toast.makeText(this, "Couldn't open share sheet: ${e.message}", Toast.LENGTH_LONG).show() } } }