Skip to content
Prev Previous commit
Next Next commit
fix bug
  • Loading branch information
AAswordman committed Dec 12, 2025
commit 189c45eb5acc9d6f355fb02f350e9e9a02a84eb5
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.ai.assistance.operit.data.model.AITool
import com.ai.assistance.operit.data.model.FunctionType
import com.ai.assistance.operit.data.model.ToolParameter
import com.ai.assistance.operit.data.model.ToolResult
import com.ai.assistance.operit.services.FloatingChatService
import com.ai.assistance.operit.ui.common.displays.UIOperationOverlay
import com.ai.assistance.operit.util.AppLogger
import com.ai.assistance.operit.util.ImagePoolManager
Expand Down Expand Up @@ -383,25 +384,31 @@ open class StandardUITools(protected val context: Context) {
while (step < maxSteps && !finished) {
step++

val userMessageBuilder = StringBuilder()
userMessageBuilder.appendLine("Task: $intent")
userMessageBuilder.appendLine("Step: $step / $maxSteps")

if (actionLogs.isNotEmpty()) {
userMessageBuilder.appendLine("Previous actions (most recent first):")
actionLogs.asReversed().take(5).forEach { log ->
userMessageBuilder.appendLine("- $log")
}
}

val screenshotLink = captureScreenshotForAgent()
if (screenshotLink != null) {
userMessageBuilder.appendLine()
userMessageBuilder.appendLine("[SCREENSHOT] Below is the latest screen image:")
userMessageBuilder.appendLine(screenshotLink)
}

val userMessage = userMessageBuilder.toString().trim()
val screenInfo = buildString {
if (screenshotLink != null) {
appendLine("[SCREENSHOT] Below is the latest screen image:")
appendLine(screenshotLink)
} else {
appendLine("No screenshot available for this step.")
}
}.trim()

val userMessage =
if (step == 1) {
buildString {
appendLine(intent)
appendLine()
appendLine(screenInfo)
}.trim()
} else {
buildString {
appendLine("** Screen Info **")
appendLine()
appendLine(screenInfo)
}.trim()
}

history.add("user" to userMessage)

Expand Down Expand Up @@ -522,19 +529,26 @@ open class StandardUITools(protected val context: Context) {
val fileName = "ui_screenshot_${timestampFormat.format(Date())}.png"
val file = File(screenshotDir, fileName)

val command = "screencap -p ${file.absolutePath}"
val result = AndroidShellExecutor.executeShellCommand(command)
if (!result.success) {
AppLogger.w(TAG, "captureScreenshotForAgent: screencap failed: ${result.stderr}")
return null
}
val floatingService = FloatingChatService.getInstance()
try {
// Temporarily hide the floating status indicator from screenshots
floatingService?.setStatusIndicatorAlpha(0f)
val command = "screencap -p ${file.absolutePath}"
val result = AndroidShellExecutor.executeShellCommand(command)
if (!result.success) {
AppLogger.w(TAG, "captureScreenshotForAgent: screencap failed: ${result.stderr}")
return null
}

val imageId = ImagePoolManager.addImage(file.absolutePath)
if (imageId == "error") {
AppLogger.e(TAG, "captureScreenshotForAgent: failed to register image: ${file.absolutePath}")
null
} else {
"<link type=\"image\" id=\"$imageId\"></link>"
val imageId = ImagePoolManager.addImage(file.absolutePath)
if (imageId == "error") {
AppLogger.e(TAG, "captureScreenshotForAgent: failed to register image: ${file.absolutePath}")
null
} else {
"<link type=\"image\" id=\"$imageId\"></link>"
}
} finally {
floatingService?.setStatusIndicatorAlpha(1f)
}
} catch (e: Exception) {
AppLogger.e(TAG, "captureScreenshotForAgent failed", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class FloatingChatService : Service(), FloatingWindowCallback {
private val typography = mutableStateOf<Typography?>(null)
private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)

companion object {
@Volatile
private var instance: FloatingChatService? = null

fun getInstance(): FloatingChatService? = instance
}

inner class LocalBinder : Binder() {
private var closeCallback: (() -> Unit)? = null
private var reloadCallback: (() -> Unit)? = null
Expand Down Expand Up @@ -133,6 +140,8 @@ class FloatingChatService : Service(), FloatingWindowCallback {
super.onCreate()
AppLogger.d(TAG, "onCreate")

instance = this

Thread.setDefaultUncaughtExceptionHandler(customExceptionHandler)

prefs = getSharedPreferences("floating_chat_prefs", Context.MODE_PRIVATE)
Expand Down Expand Up @@ -446,6 +455,7 @@ class FloatingChatService : Service(), FloatingWindowCallback {
} catch (e: Exception) {
AppLogger.e(TAG, "Error in onDestroy", e)
}
instance = null
}

override fun onClose() {
Expand Down Expand Up @@ -565,6 +575,12 @@ class FloatingChatService : Service(), FloatingWindowCallback {
}
}

fun setStatusIndicatorAlpha(alpha: Float) {
if (::windowManager.isInitialized) {
windowManager.setStatusIndicatorAlpha(alpha)
}
}

/**
* 获取 ChatServiceCore 实例
* @return ChatServiceCore 聊天服务核心实例
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ class FloatingWindowManager(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE or
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS or
WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT
).apply {
gravity = Gravity.TOP or Gravity.START
Expand Down Expand Up @@ -319,6 +320,17 @@ class FloatingWindowManager(
}
}

fun setStatusIndicatorAlpha(alpha: Float) {
val view = statusIndicatorView ?: return
if (Looper.myLooper() == Looper.getMainLooper()) {
view.alpha = alpha
} else {
Handler(Looper.getMainLooper()).post {
statusIndicatorView?.alpha = alpha
}
}
}

@Composable
private fun FullscreenRainbowStatusIndicator() {
val infiniteTransition = rememberInfiniteTransition(label = "status_indicator_rainbow")
Expand Down
35 changes: 27 additions & 8 deletions app/src/main/java/com/ai/assistance/operit/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -726,18 +726,37 @@ class MainActivity : ComponentActivity() {
if (!noMediaFile.exists()) {
noMediaFile.createNewFile()
}

AppLogger.d(TAG, "开始清理临时文件目录: ${tempDir.absolutePath}")
val files = tempDir.listFiles()
var deletedCount = 0

files?.forEach { file ->
if (file.isFile && file.name != ".nomedia" && file.delete()) {
deletedCount++
fun deleteRecursively(file: java.io.File, isRoot: Boolean = false): Int {
var deletedCount = 0
if (file.isDirectory) {
val children = file.listFiles()
children?.forEach { child ->
// 递归删除所有子目录和文件,是否为根目录通过 isRoot 控制目录本身是否删除
deletedCount += deleteRecursively(child, isRoot = false)
}
// 根目录本身不删除,只删除其子目录
if (!isRoot && file.exists()) {
if (file.delete()) {
// 目录删除不计入文件数量统计
}
}
} else if (file.isFile) {
// 保留根目录下的 .nomedia 文件,其余全部删除
val isRootNoMedia =
(file.parentFile?.absolutePath == tempDir.absolutePath &&
file.name == ".nomedia")
if (!isRootNoMedia && file.delete()) {
deletedCount++
}
}
return deletedCount
}

AppLogger.d(TAG, "已删除${deletedCount}个临时文件")

val totalDeleted = deleteRecursively(tempDir, isRoot = true)
AppLogger.d(TAG, "已删除${totalDeleted}个临时文件")
}
} catch (e: Exception) {
AppLogger.e(TAG, "清理临时文件失败", e)
Expand Down