Fix: operate clipboard on main thread

This commit is contained in:
kr328 2022-05-16 20:00:48 +08:00
parent b472580ca6
commit a405372f6a
2 changed files with 8 additions and 13 deletions

View file

@ -76,11 +76,8 @@ class AccessControlActivity : BaseActivity<AccessControlDesign>() {
val data = clipboard?.primaryClip val data = clipboard?.primaryClip
if (data != null && data.itemCount > 0) { if (data != null && data.itemCount > 0) {
val all = withContext(Dispatchers.IO) { val packages = data.getItemAt(0).text.split("\n").toSet()
val packages = data.getItemAt(0).text.split("\n").toSet() val all = design.apps.map(AppInfo::packageName).intersect(packages)
design.apps.map(AppInfo::packageName).intersect(packages)
}
selected.clear() selected.clear()
selected.addAll(all) selected.addAll(all)
@ -91,14 +88,12 @@ class AccessControlActivity : BaseActivity<AccessControlDesign>() {
AccessControlDesign.Request.Export -> { AccessControlDesign.Request.Export -> {
val clipboard = getSystemService<ClipboardManager>() val clipboard = getSystemService<ClipboardManager>()
withContext(Dispatchers.IO) { val data = ClipData.newPlainText(
val data = ClipData.newPlainText( "packages",
"packages", selected.joinToString("\n")
selected.joinToString("\n") )
)
clipboard?.setPrimaryClip(data) clipboard?.setPrimaryClip(data)
}
} }
} }
} }

View file

@ -26,7 +26,7 @@ class LogcatDesign(
private val binding = DesignLogcatBinding private val binding = DesignLogcatBinding
.inflate(context.layoutInflater, context.root, false) .inflate(context.layoutInflater, context.root, false)
private val adapter = LogMessageAdapter(context) { private val adapter = LogMessageAdapter(context) {
launch(Dispatchers.IO) { launch {
val data = ClipData.newPlainText("log_message", it.message) val data = ClipData.newPlainText("log_message", it.message)
context.getSystemService<ClipboardManager>()?.setPrimaryClip(data) context.getSystemService<ClipboardManager>()?.setPrimaryClip(data)