package com.dmc.callcrm.ui import android.content.Intent import android.net.Uri import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import com.dmc.callcrm.ApiConfig import com.dmc.callcrm.R import com.dmc.callcrm.data.Prefs import com.dmc.callcrm.data.RecordingScanner import com.dmc.callcrm.sync.RecordingUploader import com.dmc.callcrm.sync.Syncer import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class MoreFragment : Fragment() { // Registered at fragment-construction time (required by the Activity Result API — // must not be called after onCreate). Handles the folder the user picks in the // "Grant recordings folder access" flow; see rowGrantSafFolder below. private val openFolder = registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri: Uri? -> if (uri == null) { Toast.makeText(context, "No folder selected.", Toast.LENGTH_SHORT).show() return@registerForActivityResult } requireContext().contentResolver.takePersistableUriPermission( uri, Intent.FLAG_GRANT_READ_URI_PERMISSION ) Prefs(requireContext()).safRecordingsTreeUri = uri.toString() Toast.makeText( context, "Folder access granted. Tap Sync now to upload recordings from it.", Toast.LENGTH_LONG ).show() } override fun onCreateView(i: LayoutInflater, c: ViewGroup?, s: Bundle?): View = i.inflate(R.layout.fragment_more, c, false) override fun onViewCreated(v: View, s: Bundle?) { val prefs = Prefs(requireContext()) v.findViewById(R.id.rowSync).setOnClickListener { Toast.makeText(context, "Syncing…", Toast.LENGTH_SHORT).show() viewLifecycleOwner.lifecycleScope.launch { val callLogsOk = Syncer.run(requireContext().applicationContext) val recordingsOk = RecordingUploader.run(requireContext().applicationContext) val msg = when { callLogsOk && recordingsOk -> "Sync done — call logs + recordings uploaded (if any found)." !callLogsOk -> "Call log sync failed — check network/registration." else -> "Call logs synced, but recording upload failed — check network/registration." } Toast.makeText(context, msg, Toast.LENGTH_LONG).show() } } v.findViewById(R.id.rowStatus).setOnClickListener { val status = if (prefs.isRegistered) "Registered — connected" else "Not registered" Toast.makeText(context, status, Toast.LENGTH_LONG).show() } v.findViewById(R.id.rowRec).setOnClickListener { val mode = ApiConfig.recordingMode() val header = "Recording mode: $mode (${android.os.Build.MANUFACTURER} ${android.os.Build.MODEL})\n\n" Toast.makeText(context, "Scanning folders…", Toast.LENGTH_SHORT).show() viewLifecycleOwner.lifecycleScope.launch { val dump = withContext(Dispatchers.IO) { RecordingScanner(requireContext().applicationContext).debugDump() } android.app.AlertDialog.Builder(requireContext()) .setTitle("Recording folder scan") .setMessage(header + dump) .setPositiveButton("OK", null) .show() } } v.findViewById(R.id.rowLinkExt).setOnClickListener { startActivity(android.content.Intent(requireContext(), LinkExtensionActivity::class.java)) } v.findViewById(R.id.rowGrantSafFolder).setOnClickListener { val current = Prefs(requireContext()).safRecordingsTreeUri if (current != null) { android.app.AlertDialog.Builder(requireContext()) .setTitle("Recordings folder access") .setMessage("A folder is already granted:\n$current\n\nPick a different one?") .setPositiveButton("Pick folder") { _, _ -> openFolder.launch(null) } .setNegativeButton("Cancel", null) .show() } else { Toast.makeText( context, "Choose the MIUI/sound_recorder/call_rec folder (or wherever this phone saves call recordings) in the picker that opens.", Toast.LENGTH_LONG ).show() openFolder.launch(null) } } v.findViewById(R.id.rowMicTest).setOnClickListener { startActivity(android.content.Intent(requireContext(), TestMicActivity::class.java)) } v.findViewById(R.id.rowAbout).setOnClickListener { Toast.makeText(context, "DMC Call CRM", Toast.LENGTH_SHORT).show() } v.findViewById(R.id.rowChooseSim).setOnClickListener { startActivity(android.content.Intent(requireContext(), ChooseSimActivity::class.java)) } v.findViewById(R.id.rowWhatsappCalls).setOnClickListener { startActivity(android.content.Intent(requireContext(), WhatsAppCallSetupActivity::class.java)) } } }