diff --git a/app/src/main/java/dev/lotherk/kidstube/remote/DownloadManager.kt b/app/src/main/java/dev/lotherk/kidstube/remote/DownloadManager.kt new file mode 100644 index 0000000..c82a885 --- /dev/null +++ b/app/src/main/java/dev/lotherk/kidstube/remote/DownloadManager.kt @@ -0,0 +1,100 @@ +package dev.lotherk.kidstube.remote + +import android.content.Context +import android.os.Environment +import java.io.File + +/** + * Manages remote downloads stored under Movies/KidsTube/. + * + * Every downloaded video is a .mp4 file; the folder structure mirrors + * the remote library path so the same MediaItem.folder can address it. + */ +object DownloadManager { + + private const val ROOT = "Movies/KidsTube" + + /** One downloaded file. */ + data class Download( + val path: String, + val size: Long, + val folder: String, + val title: String + ) + + /** Returns all currently downloaded remote videos. */ + fun list(context: Context): List { + val root = File( + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), + ROOT + ) + if (!root.exists() || !root.isDirectory) return emptyList() + val out = mutableListOf() + walk(root, root, out) + return out + } + + /** Total size of all downloads in bytes. */ + fun totalSize(context: Context): Long { + var sum = 0L + for (d in list(context)) sum += d.size + return sum + } + + /** Returns true if a file with this folder+title exists. */ + fun exists(context: Context, itemFolder: String, title: String): Boolean { + val root = File( + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), + ROOT + ) + val candidates = File(root, title + ".mp4") + if (candidates.exists()) return true + // Also check subfolders matching the item's folder path + val sub = File(root, itemFolder.replace("\\", "/")) + if (sub.isDirectory) { + val found = sub.listFiles { f -> + f.isFile && f.nameWithoutExtension == title + } + if (found != null && found.isNotEmpty()) return true + } + return false + } + + /** Removes a downloaded file. Returns true if deleted. */ + fun remove(context: Context, download: Download): Boolean { + return File(download.path).delete() + } + + /** Removes all downloads. Returns count of deleted files. */ + fun removeAll(context: Context): Int { + val root = File( + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), + ROOT + ) + if (!root.exists() || !root.isDirectory) return 0 + var count = 0 + for (f in root.listFiles().orEmpty()) { + if (f.isFile && f.name.endsWith(".mp4")) { + if (f.delete()) count++ + } + } + // Also empty subdirectories + for (f in root.listFiles().orEmpty()) { + if (f.isDirectory) f.deleteRecursively() + } + return count + } + + private fun walk(dir: File, root: File, out: MutableList) { + for (f in dir.listFiles().orEmpty()) { + if (f.isFile && f.name.endsWith(".mp4")) { + val rel = f.relativeTo(root).path + val folder = f.parentFile?.relativeTo(root)?.path ?: "" + val title = f.nameWithoutExtension + out.add(Download(f.absolutePath, f.length(), folder, title)) + } else if (f.isDirectory) { + walk(f, root, out) + } + } + } +} diff --git a/app/src/main/java/dev/lotherk/kidstube/ui/DashboardActivity.kt b/app/src/main/java/dev/lotherk/kidstube/ui/DashboardActivity.kt new file mode 100644 index 0000000..03f713d --- /dev/null +++ b/app/src/main/java/dev/lotherk/kidstube/ui/DashboardActivity.kt @@ -0,0 +1,284 @@ +package dev.lotherk.kidstube.ui + +import android.content.Intent +import android.os.Bundle +import android.text.TextUtils +import android.view.Gravity +import android.view.View +import android.view.ViewGroup +import android.widget.Button +import android.widget.LinearLayout +import android.widget.TextView +import android.widget.Toast +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat +import dev.lotherk.kidstube.R +import dev.lotherk.kidstube.data.Library +import dev.lotherk.kidstube.remote.DownloadManager +import dev.lotherk.kidstube.remote.RemoteStore +import dev.lotherk.kidstube.stats.StatsDb +import java.util.concurrent.Executors + +/** + * Parent-only dashboard: stats, downloads, remote libraries, folder management. + * + * Replaces the multiplication-gate AlertDialog with a proper screen. + * The gate is still there (tab profile, gear icon) — the dashboard + * is what parents see after solving it. + */ +class DashboardActivity : AppCompatActivity() { + + private val executor = Executors.newSingleThreadExecutor() + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val root = LinearLayout(this).apply { + orientation = LinearLayout.VERTICAL + setPadding(0, 0, 0, 0) + } + + // --- header ----------------------------------------------------------- + val header = LinearLayout(this).apply { + orientation = LinearLayout.HORIZONTAL + gravity = Gravity.CENTER_VERTICAL + setPadding(0, dp(16), 0, dp(16)) + } + val back = TextView(this).apply { + text = "←" + textSize = 22f + setTextColor(ContextCompat.getColor(this@DashboardActivity, R.color.kt_text)) + setPadding(dp(12), 0, dp(12), 0) + setOnClickListener { finish() } + } + val title = TextView(this).apply { + text = getString(R.string.parent_gate_title) + textSize = 22f + setTextColor(ContextCompat.getColor(this@DashboardActivity, R.color.kt_text)) + setPadding(0, 0, 0, 0) + setPadding(dp(8), 0, dp(8), 0) + setPadding(0, 0, 0, 0) + } + header.addView(back) + header.addView(title) + root.addView(header) + + // --- stats cards ------------------------------------------------------ + val statsCard = LinearLayout(this).apply { + orientation = LinearLayout.VERTICAL + setPadding(dp(20), dp(16), dp(20), dp(16)) + background = ContextCompat.getDrawable(this@DashboardActivity, R.drawable.bg_card) + setPadding(dp(16), dp(16), dp(16), dp(16)) + } + val statsTitle = TextView(this).apply { + text = getString(R.string.stats_title) + textSize = 16f + setTextColor(ContextCompat.getColor(this@DashboardActivity, R.color.kt_text)) + setPadding(0, 0, 0, dp(12)) + setPadding(0, 0, 0, dp(12)) + } + statsCard.addView(statsTitle) + + val statsRow = LinearLayout(this).apply { + orientation = LinearLayout.HORIZONTAL + setPadding(0, 0, 0, 0) + } + val dim = ContextCompat.getColor(this@DashboardActivity, R.color.kt_text_sub) + val bright = ContextCompat.getColor(this@DashboardActivity, R.color.kt_text) + val stats = StatsDb(this, Library.id(this)) + val totals = stats.totals() + + val playsCard = makeStatCard(this, totals[0].toString(), getString(R.string.stats_plays), bright, dim) + val timeCard = makeStatCard(this, stats.formatMs(totals[1]), getString(R.string.stats_time), bright, dim) + val videosCard = makeStatCard(this, totals[2].toString(), getString(R.string.stats_videos), bright, dim) + statsRow.addView(playsCard) + statsRow.addView(timeCard) + statsRow.addView(videosCard) + statsCard.addView(statsRow) + root.addView(statsCard) + + // --- downloads -------------------------------------------------------- + val dlCard = LinearLayout(this).apply { + orientation = LinearLayout.VERTICAL + setPadding(dp(20), dp(8), dp(20), dp(8)) + background = ContextCompat.getDrawable(this@DashboardActivity, R.drawable.bg_card) + setPadding(dp(16), dp(16), dp(16), dp(16)) + } + val dlTitle = TextView(this).apply { + text = getString(R.string.download) + textSize = 16f + setTextColor(ContextCompat.getColor(this@DashboardActivity, R.color.kt_text)) + setPadding(0, 0, 0, dp(12)) + } + dlCard.addView(dlTitle) + + val dlList = DownloadManager.list(this) + val dlSummary = TextView(this).apply { + val sizeStr = formatSize(DownloadManager.totalSize(this@DashboardActivity)) + text = "${dlList.size} videos · $sizeStr" + textSize = 14f + setTextColor(bright) + setPadding(0, 0, 0, dp(8)) + } + dlCard.addView(dlSummary) + + // Download list + for (dl in dlList) { + val row = LinearLayout(this).apply { + orientation = LinearLayout.HORIZONTAL + gravity = Gravity.CENTER_VERTICAL + setPadding(0, dp(6), 0, dp(6)) + } + val info = LinearLayout(this).apply { orientation = LinearLayout.VERTICAL } + info.addView(TextView(this).apply { + text = dl.title + textSize = 13f + setTextColor(bright) + maxLines = 1 + ellipsize = TextUtils.TruncateAt.END + }) + info.addView(TextView(this).apply { + text = formatSize(dl.size) + if (dl.folder.isNotEmpty()) " · $dl.folder" else "" + textSize = 11f + setTextColor(dim) + }) + row.addView(info, LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)) + val removeBtn = TextView(this).apply { + text = "×" + textSize = 18f + setTextColor(ContextCompat.getColor(this@DashboardActivity, R.color.kt_red)) + setPadding(dp(12), dp(4), dp(12), dp(4)) + setOnClickListener { + DownloadManager.remove(this@DashboardActivity, dl) + Toast.makeText(this@DashboardActivity, R.string.download_done, Toast.LENGTH_SHORT).show() + recreate() + } + } + row.addView(removeBtn) + dlCard.addView(row) + } + if (dlList.isEmpty()) { + dlCard.addView(TextView(this).apply { + text = "No downloads yet" + textSize = 13f + setTextColor(dim) + setPadding(0, dp(8), 0, dp(8)) + }) + } else { + val clearAll = TextView(this).apply { + text = "Clear all" + textSize = 13f + setTextColor(ContextCompat.getColor(this@DashboardActivity, R.color.kt_text_sub)) + setPadding(0, dp(12), 0, dp(4)) + setOnClickListener { + AlertDialog.Builder(this@DashboardActivity) + .setTitle("Clear all downloads?") + .setMessage("This will remove ${dlList.size} videos from this tablet.") + .setPositiveButton("Clear") { _, _ -> + val count = DownloadManager.removeAll(this@DashboardActivity) + Toast.makeText(this@DashboardActivity, "Removed $count", Toast.LENGTH_SHORT).show() + recreate() + } + .setNegativeButton("Cancel", null) + .show() + } + } + dlCard.addView(clearAll) + } + root.addView(dlCard) + + // --- actions ---------------------------------------------------------- + val actionCard = LinearLayout(this).apply { + orientation = LinearLayout.VERTICAL + setPadding(dp(20), dp(8), dp(20), dp(8)) + background = ContextCompat.getDrawable(this@DashboardActivity, R.drawable.bg_card) + setPadding(dp(16), dp(12), dp(16), dp(12)) + } + val actionTitle = TextView(this).apply { + text = "Settings" + textSize = 14f + setTextColor(dim) + setPadding(0, 0, 0, dp(8)) + } + actionCard.addView(actionTitle) + + val actions = arrayOf( + getString(R.string.remote_title) to { startActivity(Intent(this, RemoteLibrariesActivity::class.java)) }, + getString(R.string.change_folder) to { pickFolder() }, + getString(R.string.stats_open) to { startActivity(Intent(this, StatsActivity::class.java)) } + ) + for ((label, onClick) in actions) { + actionCard.addView(actionButton(this, label, onClick)) + } + root.addView(actionCard) + + setContentView(root) + } + + private fun pickFolder() { + val launcher = registerForActivityResult( + androidx.activity.result.contract.ActivityResultContracts.OpenDocumentTree() + ) { uri -> + if (uri != null) { + Library.setTree(this, uri) + recreate() + } + } + launcher.launch(Library.treeUri(this)) + } + + private fun makeStatCard( + parent: LinearLayout, + value: String, label: String, valueColor: Int, labelColor: Int + ): LinearLayout { + val card = LinearLayout(parent.context).apply { + orientation = LinearLayout.VERTICAL + gravity = Gravity.CENTER_HORIZONTAL + setPadding(dp(8), 0, dp(8), 0) + } + card.addView(TextView(parent.context).apply { + text = value + textSize = 20f + setTextColor(valueColor) + setPadding(0, 0, 0, dp(4)) + }) + card.addView(TextView(parent.context).apply { + text = label + textSize = 11f + setTextColor(labelColor) + }) + return card + } + + private fun actionButton( + parent: LinearLayout, label: String, onClick: () -> Unit + ): TextView { + return TextView(parent.context).apply { + text = label + textSize = 15f + setTextColor(ContextCompat.getColor(this@DashboardActivity, R.color.kt_text)) + setPadding(dp(12), dp(14), dp(12), dp(14)) + setOnClickListener { onClick() } + setBackgroundResource(android.R.drawable.btn_default) // placeholder + setPadding(dp(12), dp(14), dp(12), dp(14)) + } + } + + private fun formatSize(bytes: Long): String { + return when { + bytes < 1024 -> "$bytes B" + bytes < 1024 * 1024 -> "${bytes / 1024} KB" + bytes < 1024L * 1024 * 1024 -> "${bytes / (1024 * 1024)} MB" + else -> String.format("%.1f GB", bytes / (1024.0 * 1024 * 1024)) + } + } + + private fun dp(px: Int): Int { + return (px * resources.displayMetrics.density + 0.5f).toInt() + } + + override fun onDestroy() { + executor.shutdown() + super.onDestroy() + } +}