feat: 本机默认信任自己且不能移除
This commit is contained in:
@@ -14,6 +14,7 @@ import com.asio.tapremly.core.Shortcut
|
||||
import com.asio.tapremly.core.Stored
|
||||
|
||||
class AppDb(context: Context) : SQLiteOpenHelper(context.applicationContext, "tapremly.db", null, 1) {
|
||||
private val appContext = context.applicationContext
|
||||
private val gate = Any()
|
||||
|
||||
override fun onCreate(db: SQLiteDatabase) {
|
||||
@@ -163,21 +164,37 @@ class AppDb(context: Context) : SQLiteOpenHelper(context.applicationContext, "ta
|
||||
|
||||
fun deletePublished(id: String) = tx { delete("published", "id=?", arrayOf(id)) }
|
||||
|
||||
fun listTrust(): List<String> = read(
|
||||
"SELECT endpoint_id FROM trust ORDER BY added_at",
|
||||
) { str(0) }
|
||||
fun listTrust(): List<String> {
|
||||
val rows = read(
|
||||
"SELECT endpoint_id FROM trust ORDER BY added_at",
|
||||
) { str(0) }.toMutableList()
|
||||
val self = selfId()
|
||||
if (self.isNotBlank() && self !in rows) rows.add(0, self)
|
||||
return rows
|
||||
}
|
||||
|
||||
fun addTrust(id: String, atMs: Long) = tx {
|
||||
delete("trust", "endpoint_id=?", arrayOf(id))
|
||||
insert("trust", null, values("endpoint_id" to id, "added_at" to atMs))
|
||||
}
|
||||
|
||||
fun removeTrust(id: String) = tx { delete("trust", "endpoint_id=?", arrayOf(id)) }
|
||||
fun removeTrust(id: String) = tx {
|
||||
if (id == selfId()) return@tx
|
||||
delete("trust", "endpoint_id=?", arrayOf(id))
|
||||
}
|
||||
|
||||
fun isTrusted(id: String): Boolean = read(
|
||||
"SELECT 1 FROM trust WHERE endpoint_id=?",
|
||||
arrayOf(id),
|
||||
) { 1 }.isNotEmpty()
|
||||
fun isTrusted(id: String): Boolean {
|
||||
if (id.isNotBlank() && id == selfId()) return true
|
||||
return read(
|
||||
"SELECT 1 FROM trust WHERE endpoint_id=?",
|
||||
arrayOf(id),
|
||||
) { 1 }.isNotEmpty()
|
||||
}
|
||||
|
||||
private fun selfId(): String {
|
||||
val prefs = appContext.getSharedPreferences("tapremly", Context.MODE_PRIVATE)
|
||||
return prefs.getString("endpoint", "").orEmpty()
|
||||
}
|
||||
|
||||
fun getExecuted(id: String, now: Long): Stored? {
|
||||
val row = read(
|
||||
|
||||
@@ -40,9 +40,15 @@ class TrustActivity : AgentActivity() {
|
||||
}
|
||||
for (id in snapshot.trust) {
|
||||
val view = layoutInflater.inflate(R.layout.item_trust, binding.rows, false)
|
||||
view.findViewById<TextView>(R.id.endpoint).text = id
|
||||
view.findViewById<View>(R.id.remove).setOnClickListener {
|
||||
confirmDialog("不再信任这个端?") { runtime?.trustRemove(id) }
|
||||
view.findViewById<TextView>(R.id.endpoint).text =
|
||||
if (id == snapshot.selfId) "本机 $id" else id
|
||||
val remove = view.findViewById<View>(R.id.remove)
|
||||
if (id == snapshot.selfId) {
|
||||
remove.visibility = View.GONE
|
||||
} else {
|
||||
remove.setOnClickListener {
|
||||
confirmDialog("不再信任这个端?") { runtime?.trustRemove(id) }
|
||||
}
|
||||
}
|
||||
binding.rows.addView(view)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user