惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
人人都是产品经理
人人都是产品经理
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
GbyAI
GbyAI
博客园 - 司徒正美
美团技术团队
Vercel News
Vercel News
IT之家
IT之家
U
Unit 42
Y
Y Combinator Blog
罗磊的独立博客
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
V
Visual Studio Blog
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MyScale Blog
MyScale Blog
博客园 - 叶小钗
A
About on SuperTechFans
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
B
Blog RSS Feed

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(android): isolate timed out permission requests · ope...
obviyus · 2026-05-18 · via Recent Commits to openclaw:main

@@ -17,81 +17,149 @@ import androidx.lifecycle.Lifecycle

1717

import androidx.lifecycle.LifecycleEventObserver

1818

import kotlinx.coroutines.CompletableDeferred

1919

import kotlinx.coroutines.Dispatchers

20+

import kotlinx.coroutines.TimeoutCancellationException

2021

import kotlinx.coroutines.suspendCancellableCoroutine

2122

import kotlinx.coroutines.sync.Mutex

2223

import kotlinx.coroutines.sync.withLock

2324

import kotlinx.coroutines.withContext

25+

import kotlinx.coroutines.withTimeout

2426

import java.util.concurrent.atomic.AtomicBoolean

2527

import kotlin.coroutines.resume

262827-

class PermissionRequester(

29+

class PermissionRequester internal constructor(

2830

private val activity: ComponentActivity,

31+

launcherFactory: ((Map<String, Boolean>) -> Unit) -> ActivityResultLauncher<Array<String>>,

2932

) {

33+

private data class PendingPermissionRequest(

34+

val deferred: CompletableDeferred<Map<String, Boolean>>,

35+

var timedOut: Boolean = false,

36+

)

37+38+

private class PermissionRequestSlot(

39+

val launcher: ActivityResultLauncher<Array<String>>,

40+

var request: PendingPermissionRequest? = null,

41+

)

42+43+

constructor(activity: ComponentActivity) : this(

44+

activity = activity,

45+

launcherFactory = { callback ->

46+

activity.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions(), callback)

47+

},

48+

)

49+3050

private val mutex = Mutex()

31-

private var pending: CompletableDeferred<Map<String, Boolean>>? = null

51+

private val requestSlotsLock = Any()

3252

private val mainHandler = Handler(Looper.getMainLooper())

33-34-

private val launcher: ActivityResultLauncher<Array<String>> =

35-

activity.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result ->

36-

val p = pending

37-

pending = null

38-

p?.complete(result)

39-

}

53+

private val launchers = List(4) { createPermissionRequestSlot(launcherFactory) }

40544155

suspend fun requestIfMissing(

4256

permissions: List<String>,

4357

timeoutMs: Long = 20_000,

44-

): Map<String, Boolean> =

45-

mutex.withLock {

46-

val missing =

47-

permissions.filter { perm ->

48-

ContextCompat.checkSelfPermission(activity, perm) != PackageManager.PERMISSION_GRANTED

58+

): Map<String, Boolean> {

59+

return mutex.withLock {

60+

while (true) {

61+

val missing =

62+

permissions.filter { perm ->

63+

ContextCompat.checkSelfPermission(activity, perm) != PackageManager.PERMISSION_GRANTED

64+

}

65+

if (missing.isEmpty()) {

66+

return permissions.associateWith { true }

4967

}

50-

if (missing.isEmpty()) {

51-

return permissions.associateWith { true }

52-

}

536854-

val needsRationale =

55-

missing.any { ActivityCompat.shouldShowRequestPermissionRationale(activity, it) }

56-

if (needsRationale) {

57-

val proceed = showRationaleDialog(missing)

58-

if (!proceed) {

59-

return permissions.associateWith { perm ->

60-

ContextCompat.checkSelfPermission(activity, perm) == PackageManager.PERMISSION_GRANTED

69+

val needsRationale =

70+

missing.any { ActivityCompat.shouldShowRequestPermissionRationale(activity, it) }

71+

if (needsRationale) {

72+

val proceed = showRationaleDialog(missing)

73+

if (!proceed) {

74+

return permissions.associateWith { perm ->

75+

ContextCompat.checkSelfPermission(activity, perm) == PackageManager.PERMISSION_GRANTED

76+

}

6177

}

6278

}

63-

}

64-65-

val deferred = CompletableDeferred<Map<String, Boolean>>()

66-

pending = deferred

67-

withContext(Dispatchers.Main) {

68-

launcher.launch(missing.toTypedArray())

69-

}

707971-

val result =

72-

withContext(Dispatchers.Default) {

73-

kotlinx.coroutines.withTimeout(timeoutMs) { deferred.await() }

80+

val deferred = CompletableDeferred<Map<String, Boolean>>()

81+

val request = PendingPermissionRequest(deferred)

82+

val slot = reservePermissionRequestSlot(request)

83+

try {

84+

withContext(Dispatchers.Main) {

85+

slot.launcher.launch(missing.toTypedArray())

86+

}

87+

} catch (err: Throwable) {

88+

clearPermissionRequestSlot(slot, request)

89+

throw err

7490

}

759176-

// Merge: if something was already granted, treat it as granted even if launcher omitted it.

77-

val merged =

78-

permissions.associateWith { perm ->

79-

val nowGranted =

80-

ContextCompat.checkSelfPermission(activity, perm) == PackageManager.PERMISSION_GRANTED

81-

result[perm] == true || nowGranted

82-

}

92+

val result =

93+

try {

94+

withTimeout(timeoutMs) { deferred.await() }

95+

} catch (err: TimeoutCancellationException) {

96+

request.timedOut = true

97+

throw err

98+

}

99+100+

val merged =

101+

permissions.associateWith { perm ->

102+

val nowGranted =

103+

ContextCompat.checkSelfPermission(activity, perm) == PackageManager.PERMISSION_GRANTED

104+

result[perm] == true || nowGranted

105+

}

8310684-

val denied =

85-

merged.filterValues { !it }.keys.filter {

86-

!ActivityCompat.shouldShowRequestPermissionRationale(activity, it)

107+

val denied =

108+

merged.filterValues { !it }.keys.filter {

109+

!ActivityCompat.shouldShowRequestPermissionRationale(activity, it)

110+

}

111+

if (denied.isNotEmpty()) {

112+

showSettingsDialog(denied)

87113

}

88-

if (denied.isNotEmpty()) {

89-

showSettingsDialog(denied)

114+115+

return merged

90116

}

117+

error("unreachable")

118+

}

119+

}

120+121+

private fun createPermissionRequestSlot(

122+

launcherFactory: ((Map<String, Boolean>) -> Unit) -> ActivityResultLauncher<Array<String>>,

123+

): PermissionRequestSlot {

124+

var slot: PermissionRequestSlot? = null

125+

val launcher = launcherFactory { result -> completePermissionRequest(checkNotNull(slot), result) }

126+

val created = PermissionRequestSlot(launcher)

127+

slot = created

128+

return created

129+

}

9113092-

return merged

131+

private fun reservePermissionRequestSlot(request: PendingPermissionRequest): PermissionRequestSlot =

132+

synchronized(requestSlotsLock) {

133+

val slot = launchers.firstOrNull { it.request == null } ?: error("permission request launcher busy")

134+

slot.request = request

135+

slot

93136

}

94137138+

private fun completePermissionRequest(

139+

slot: PermissionRequestSlot,

140+

result: Map<String, Boolean>,

141+

) {

142+

val request =

143+

synchronized(requestSlotsLock) {

144+

slot.request.also {

145+

slot.request = null

146+

}

147+

} ?: return

148+

if (request.timedOut) return

149+

request.deferred.complete(result)

150+

}

151+152+

private fun clearPermissionRequestSlot(

153+

slot: PermissionRequestSlot,

154+

request: PendingPermissionRequest,

155+

) {

156+

synchronized(requestSlotsLock) {

157+

if (slot.request === request) {

158+

slot.request = null

159+

}

160+

}

161+

}

162+95163

private suspend fun showRationaleDialog(permissions: List<String>): Boolean =

96164

withContext(Dispatchers.Main) {

97165

if (activity.isFinishing || activity.isDestroyed) {