package tv.tivilux.nativeplayer.data import android.content.Context import android.os.Build import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.json.JSONObject import tv.tivilux.nativeplayer.BuildConfig import java.net.HttpURLConnection import java.net.URL import java.util.UUID /** * Non-sensitive context attached to a member support report. * Passwords, provider URLs and Xtream credentials are intentionally excluded. */ data class SupportReportContext( val accountHint: String = "", val profile: String = "", val channelName: String = "", val channelNumber: String = "", val program: String = "", val contentType: String = "", val sportsEvent: String = "", val sportsFeed: String = "", val resolution: String = "", val codec: String = "", val playerState: String = "", val bufferedPercent: String = "", val droppedFrames: String = "", ) data class SupportReportResult( val ok: Boolean, val reportId: String = "", val message: String = "", val error: String = "", ) object SupportReportClient { private const val PREFS = "tivilux_support" private const val INSTALL_ID = "install_id" suspend fun submit( context: Context, issueType: String, comment: String, details: SupportReportContext, ): SupportReportResult = withContext(Dispatchers.IO) { val endpoint = BuildConfig.REPORT_ENDPOINT_URL if (endpoint.isBlank()) { return@withContext SupportReportResult(false, error = "Reporting service is not configured.") } val payload = JSONObject().apply { put("issueType", issueType.take(80)) put("comment", comment.trim().take(500)) put("installId", installationId(context)) put("appPackage", context.packageName) put("device", buildDeviceLabel()) put("androidVersion", Build.VERSION.RELEASE ?: Build.VERSION.SDK_INT.toString()) put("appVersion", BuildConfig.VERSION_NAME) put("build", BuildConfig.VERSION_CODE.toString()) put("accountHint", details.accountHint.take(80)) put("profile", details.profile.take(80)) put("channelName", details.channelName.take(150)) put("channelNumber", details.channelNumber.take(30)) put("program", details.program.take(200)) put("contentType", details.contentType.take(40)) put("sportsEvent", details.sportsEvent.take(200)) put("sportsFeed", details.sportsFeed.take(200)) put("resolution", details.resolution.take(40)) put("codec", details.codec.take(60)) put("playerState", details.playerState.take(60)) put("bufferedPercent", details.bufferedPercent.take(20)) put("droppedFrames", details.droppedFrames.take(30)) } var connection: HttpURLConnection? = null try { connection = (URL(endpoint).openConnection() as HttpURLConnection).apply { requestMethod = "POST" connectTimeout = 7_000 readTimeout = 12_000 doOutput = true useCaches = false setRequestProperty("Content-Type", "application/json; charset=utf-8") setRequestProperty("Accept", "application/json") setRequestProperty("Cache-Control", "no-cache") setRequestProperty("User-Agent", "TiviLux/${BuildConfig.VERSION_NAME} Android") } connection.outputStream.bufferedWriter(Charsets.UTF_8).use { writer -> writer.write(payload.toString()) } val status = connection.responseCode val body = runCatching { val stream = if (status in 200..299) connection.inputStream else connection.errorStream stream?.bufferedReader(Charsets.UTF_8)?.use { it.readText() }.orEmpty() }.getOrDefault("") val json = runCatching { JSONObject(body) }.getOrNull() if (status in 200..299 && json?.optBoolean("ok") == true) { SupportReportResult( ok = true, reportId = json.optString("reportId"), message = json.optString("message", "Report sent to the TiviLux team"), ) } else { SupportReportResult( ok = false, error = json?.optString("error")?.takeIf { it.isNotBlank() } ?: "Unable to send report (HTTP $status).", ) } } catch (t: Throwable) { SupportReportResult(false, error = t.message ?: "Unable to contact the reporting service.") } finally { connection?.disconnect() } } private fun installationId(context: Context): String { val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) val existing = prefs.getString(INSTALL_ID, null) if (!existing.isNullOrBlank()) return existing val created = UUID.randomUUID().toString() prefs.edit().putString(INSTALL_ID, created).apply() return created } private fun buildDeviceLabel(): String { val manufacturer = Build.MANUFACTURER?.trim().orEmpty() val model = Build.MODEL?.trim().orEmpty() return listOf(manufacturer, model) .filter { it.isNotBlank() } .distinctBy { it.lowercase() } .joinToString(" ") .ifBlank { "Android TV device" } } }