release v2.0.11:软件源界面 + 装完自动换清华源;终端不再静默退回命令框

This commit is contained in:
l
2026-10-13 09:24:00 +08:00
parent bec25b71ac
commit 7f3dbb4002
5 changed files with 332 additions and 6 deletions

View File

@@ -30,8 +30,8 @@ android {
* versionCode 每次发版 +1(Android 靠它判断"能不能覆盖安装",
* 改小了会装不上)。
*/
versionCode = 210
versionName = "2.0.10"
versionCode = 211
versionName = "2.0.11"
ndk {
// Chaquopy 只支持这两个;Termux bootstrap 也只准备了这两份

View File

@@ -48,7 +48,7 @@ object TermuxInstaller {
* 把戳的值带上版本号,**对不上就只重跑修正式步骤**(不重新解压,
* 解压要几十秒)。以后再加这类"装完之后要修一下"的逻辑,把这里 +1。
*/
private const val STAMP_VALUE = "ok:3"
private const val STAMP_VALUE = "ok:4"
/**
* 安装日志。
@@ -123,6 +123,19 @@ object TermuxInstaller {
val fixed = rewritePrefixes(context)
trace(context, "改写了 $fixed 个文件里的硬编码路径")
// ★ 装完把软件源换成国内的。
//
// 这份 bootstrap 自带的源是 `http://mirror.mephi.ru/...`(俄罗斯的),
// 国内拉索引又慢又容易超时 —— 用户第一次 `pkg install` 就卡住,
// 然后以为"内置的 Termux 是坏的"。
// 默认给清华(国内最快最稳),想换在终端右上角「源」里换。
try {
val n = TermuxMirror.set(context, TermuxMirror.MIRRORS.first())
trace(context, "已把软件源设为 ${TermuxMirror.MIRRORS.first().name}($n 条)")
} catch (t: Throwable) {
trace(context, "设默认源失败(不影响使用):${t.message}")
}
val binCount = File(prefix, "bin").list()?.size ?: 0
stampFile(context).writeText(STAMP_VALUE)
progress("Termux 环境安装完成($binCount 个命令)")

View File

@@ -0,0 +1,132 @@
package com.doubao.lynxclone.termux
import android.content.Context
import java.io.File
/**
* Termux 的软件源(apt 源)管理。
*
* ★ 为什么要单独做这个:Termux 官方源(packages.termux.dev)在国内**基本连不上**,
* 表现是 `pkg install` 卡在 "Reading package lists..." 或者直接超时 ——
* 用户会以为"内置的 Termux 坏了",其实是源的问题。
*
* 国内几个镜像都是完整同步的,换一下就好。做成界面而不是让用户手敲 sed:
* 换源这件事**每次重装都要做**,而且 apt 的源格式写错一点就整个不能用。
*/
object TermuxMirror {
/**
* 一个源。
*
* @param id 稳定标识(存进偏好,别用下标 —— 以后插入新源会串位)
* @param name 显示名
* @param host 给 termux-main 用的仓库根地址(不带 /apt/ 之后的部分)
* @param note 副标题:速度/稳定性之类的实话
*/
data class Mirror(val id: String, val name: String, val host: String, val note: String)
/**
* 源列表。
*
* ★ 顺序是有讲究的:**默认放第一个**。
* 镜像偶尔会挂(同步中断、证书过期),所以官方源要留着能切回去。
*/
val MIRRORS = listOf(
Mirror(
"tuna", "清华 TUNA", "https://mirrors.tuna.tsinghua.edu.cn/termux",
"国内最快最稳的一个,推荐",
),
Mirror(
"ustc", "中科大 USTC", "https://mirrors.ustc.edu.cn/termux",
"同样完整同步,教育网更快",
),
Mirror(
"aliyun", "阿里云", "https://mirrors.aliyun.com/termux",
"公网覆盖好,偶尔缺 science 仓库",
),
Mirror(
"bfsu", "北外 BFSU", "https://mirrors.bfsu.edu.cn/termux",
"清华的备份站",
),
Mirror(
"official", "官方源", "https://packages.termux.dev/apt",
"国内经常连不上,镜像挂了才用它",
),
)
/** Termux 的仓库分几个「频道」,各自一条 sources.list 行 */
private data class Channel(val name: String, val path: String, val components: String)
private val CHANNELS = listOf(
Channel("termux-main", "apt/termux-main", "stable main"),
Channel("termux-root", "apt/termux-root", "root stable"),
Channel("termux-x11", "apt/termux-x11", "x11 main"),
)
private fun aptDir(context: Context) = File(TermuxInstaller.prefix(context), "etc/apt")
fun sourcesFile(context: Context) = File(aptDir(context), "sources.list")
/** 当前用的是哪个源(认不出来就返回 null) */
fun current(context: Context): Mirror? {
val file = sourcesFile(context)
if (!file.exists()) return null
val text = try {
file.readText()
} catch (_: Throwable) {
return null
}
return MIRRORS.firstOrNull { text.contains(it.host) }
}
/**
* 换源。
*
* ★ 有个坑:Termux 官方的 sources.list **里面是带注释和 deb-src 的**,
* 直接整份覆盖会丢掉用户自己加的东西。所以这里只**替换行**:
* 凡是以 `deb ` 开头、且指向 termux 仓库的行,按频道重写;
* 其余(注释、第三方源)原样留着。
*
* @return 写进去的行数
*/
fun set(context: Context, mirror: Mirror): Int {
val dir = aptDir(context)
dir.mkdirs()
val file = sourcesFile(context)
val existing = if (file.exists()) {
try {
file.readLines()
} catch (_: Throwable) {
emptyList()
}
} else {
emptyList()
}
// 保留非 termux 的行(注释、用户自己加的源)
val kept = existing.filter { line ->
val t = line.trim()
!t.startsWith("deb ") || !t.contains("termux")
}
// ★ 只写**原本就有**的频道:termux-root / termux-x11 这两个仓库
// 国内镜像不一定同步了,凭空写进去会让 apt update 整条报错
// (一个源 404,apt 就整个失败,用户看到的是"换源之后更坏了")。
// main 永远要(没有它 pkg 什么都装不了)。
val original = existing.joinToString("\n")
val wanted = CHANNELS.filter {
it.name == "termux-main" || original.contains(it.name)
}
val header = listOf(
"# 由豆包 App 生成 —— 当前源:${mirror.name}",
"# 换源:内置终端右上角「源」按钮;也可以手动改这个文件",
)
val lines = wanted.map { "deb ${mirror.host}/${it.path} ${it.components}" }
file.writeText(
(header + kept.filter { it.isNotBlank() } + lines).joinToString("\n") + "\n",
)
return lines.size
}
}

View File

@@ -0,0 +1,133 @@
package com.doubao.lynxclone.ui
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.doubao.lynxclone.termux.TermuxMirror
/**
* 换源界面(全屏,不是弹窗)。
*
* ★ 为什么做成界面:源列表有五个,每个还要显示说明和当前选中状态 ——
* 弹窗装不下(见 AGENTS.md 的界面约定)。
*
* ★ 为什么需要这个功能:Termux 官方源在国内基本连不上,
* `pkg install` 会卡住或超时,用户会以为"内置的 Termux 坏了"。
* 换源这件事**每次重装都要做**,手敲 sed 又容易把 apt 配置写坏。
*/
@Composable
fun MirrorScreen(onBack: () -> Unit) {
val context = LocalContext.current
// 每次进界面重读一次:用户可能手改过 sources.list
var current by remember { mutableStateOf(TermuxMirror.current(context)) }
var toast by remember { mutableStateOf("") }
FullScreen(
title = "软件源",
subtitle = "换完记得跑一次 pkg update",
onBack = onBack,
) {
Column(Modifier.fillMaxSize().verticalScroll(rememberScrollState())) {
Text(
"Termux 官方源(packages.termux.dev)在国内基本连不上,表现是 " +
"pkg install 卡在 Reading package lists 或者直接超时。" +
"换一个国内镜像就好。",
color = doubaoTextSecondary(),
fontSize = 12.sp,
lineHeight = 18.sp,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 10.dp),
)
if (toast.isNotBlank()) {
Surface(
color = Color(0xFF16301F),
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp),
) {
Text(
toast,
color = Color(0xFF9BE7B4),
fontSize = 12.sp,
lineHeight = 17.sp,
modifier = Modifier.padding(10.dp),
)
}
}
SectionLabel("选一个源")
TermuxMirror.MIRRORS.forEach { m ->
val selected = current?.id == m.id
Row(
Modifier
.fillMaxWidth()
.padding(horizontal = 12.dp, vertical = 3.dp)
.clip(RoundedCornerShape(10.dp))
.background(if (selected) Doubao.Blue.copy(alpha = 0.12f) else Color.Transparent)
.clickable {
val n = try {
TermuxMirror.set(context, m)
} catch (t: Throwable) {
toast = "换源失败:${t.message}"
return@clickable
}
current = m
toast = "已切到 ${m.name}(写入 $n 条),去终端跑一次 pkg update"
}
.padding(horizontal = 12.dp, vertical = 10.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Column(Modifier.weight(1f)) {
Text(
m.name,
fontSize = 14.sp,
fontWeight = if (selected) FontWeight.SemiBold else FontWeight.Normal,
color = if (selected) Doubao.Blue else Color(0xFF1F2328),
)
Text(
m.note,
fontSize = 11.sp,
color = doubaoTextSecondary(),
lineHeight = 15.sp,
)
Text(
m.host,
fontSize = 10.sp,
color = doubaoTextSecondary().copy(alpha = 0.7f),
)
}
if (selected) {
Text("✓ 当前", fontSize = 11.sp, color = Doubao.Blue)
}
}
}
SectionLabel("换完之后")
Text(
"去终端敲:\n" +
" pkg update\n" +
"\n" +
"update 会重新拉索引,第一次可能要等十几秒。" +
"如果报错,换另一个源再试 —— 镜像偶尔会同步中断。",
color = doubaoTextSecondary(),
fontSize = 12.sp,
lineHeight = 19.sp,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 6.dp),
)
Spacer(Modifier.height(24.dp))
}
}
}

View File

@@ -43,19 +43,34 @@ fun TerminalScreen(
val context = LocalContext.current
val terminal = remember { TermuxTerminal(context) }
var title by remember { mutableStateOf("termux") }
/** pty 起不来(有些 ROM 的 /dev/pts 不给应用用)时切成命令控制台 */
/**
* pty 起不来(有些 ROM 的 /dev/pts 不给应用用)时切成命令控制台。
*
* ★ 这里**不再静默切换**:以前一旦判定失败就直接换成"命令框",
* 用户只看到"怎么不是终端",完全不知道发生了什么(真踩过)。
* 现在会**明说原因**并给"重试",终端仍然是主界面。
*/
var ptyFailed by remember { mutableStateOf(false) }
var failReason by remember { mutableStateOf("") }
var startedAt by remember { mutableStateOf(0L) }
var showMirror by remember { mutableStateOf(false) }
DisposableEffect(Unit) {
onDispose { terminal.destroy() }
}
// 换源是独立全屏界面(源列表 + 说明装不进弹窗)
if (showMirror) {
MirrorScreen(onBack = { showMirror = false })
return
}
// pty 不可用的判定:会话建起来后 2 秒内就结束,说明 bash 拿不到 tty(stdin 直接 EOF)
val onSessionExit = {
if (startedAt > 0 && System.currentTimeMillis() - startedAt < 2000) {
ptyFailed = true
TermuxInstaller.trace(context, "pty 不可用(会话秒退),切换到命令控制台")
failReason = "shell 启动后立刻退出了(拿不到 tty)。"
TermuxInstaller.trace(context, "pty 不可用(会话秒退),原因:$failReason")
}
}
@@ -70,6 +85,10 @@ fun TerminalScreen(
}
Text(title, color = Color.White, fontSize = 15.sp, fontWeight = FontWeight.Medium)
Spacer(Modifier.weight(1f))
// 换源:国内用官方源基本装不了东西,这是最常用的一个入口
TextButton(onClick = { showMirror = true }) {
Text("源", color = Color(0xFF9BE7B4), fontSize = 13.sp)
}
IconButton(onClick = {
// 发一个 Ctrl+C,终止前台进程(手机上没键盘,这是最常用的逃生键)
terminal.session?.write("\u0003".toByteArray(), 0, 1)
@@ -99,7 +118,7 @@ fun TerminalScreen(
Surface(color = Color(0xFF16301F), modifier = Modifier.fillMaxWidth()) {
Text(
"Termux 已内置 · ${TermuxInstaller.prefix(context).absolutePath}\n" +
"试试点上面的 + 装东西:pkg install python nodejs-lts",
"装东西前先换源(右上角「源」),官方源国内连不上",
color = Color(0xFF9BE7B4),
fontSize = 11.sp,
lineHeight = 16.sp,
@@ -108,9 +127,38 @@ fun TerminalScreen(
}
}
// ★ pty 失败要**明说**,别让用户以为"这软件就只有命令框"
if (termuxReady && ptyFailed) {
Surface(color = Color(0xFF3A2F10), modifier = Modifier.fillMaxWidth()) {
Column(Modifier.padding(12.dp)) {
Text(
"真终端起不来:$failReason\n" +
"下面先用命令控制台顶着(同一条 Termux bash,只是没有 tty," +
"所以 vim/top 这类全屏程序不能用)。",
color = Color(0xFFFFD08A),
fontSize = 12.sp,
lineHeight = 18.sp,
)
Spacer(Modifier.height(6.dp))
Row {
TextButton(onClick = {
ptyFailed = false
failReason = ""
startedAt = 0
terminal.destroy()
}) { Text("重试真终端") }
TextButton(onClick = { showMirror = true }) { Text("换软件源") }
}
}
}
}
// ---------------- 终端本体 ----------------
if (termuxReady && ptyFailed) {
// pty 用不了 → 命令控制台(跑的是同一条 Termux bash,只是没有 tty)
//
// ★ 上面那条橙色横幅会说明原因 + 给"重试真终端",
// 不能让用户莫名其妙地从终端变成命令框。
Box(Modifier.weight(1f).fillMaxWidth().background(Color(0xFF14161A))) {
CommandConsole(
onRun = { command, done ->