Skip to content

Commit 66dabd5

Browse files
committed
fix: action log not saving - add timber logging for debug
1 parent 5323be0 commit 66dabd5

File tree

3 files changed

+55
-23
lines changed

3 files changed

+55
-23
lines changed

app/src/main/java/com/appcontrolx/rollback/RollbackManager.kt

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import com.appcontrolx.executor.CommandExecutor
55
import com.google.gson.Gson
66
import com.google.gson.reflect.TypeToken
7+
import timber.log.Timber
78
import java.io.File
89
import java.util.UUID
910

@@ -16,6 +17,10 @@ class RollbackManager(
1617
private val snapshotFile = File(snapshotDir, "rollback_snapshot.json")
1718
private val historyFile = File(snapshotDir, "action_history.json")
1819

20+
init {
21+
Timber.d("RollbackManager initialized, historyFile: ${historyFile.absolutePath}")
22+
}
23+
1924
fun saveSnapshot(packages: List<String>): StateSnapshot {
2025
val states = packages.map { pkg ->
2126
val bgStatus = executor.execute("appops get $pkg RUN_IN_BACKGROUND")
@@ -68,20 +73,35 @@ class RollbackManager(
6873
}
6974

7075
fun logAction(action: ActionLog) {
71-
val history = getActionHistory().toMutableList()
72-
history.add(0, action) // Add to beginning
73-
74-
// Keep only last 100 actions
75-
val trimmed = history.take(100)
76-
historyFile.writeText(gson.toJson(trimmed))
76+
try {
77+
Timber.d("Logging action: ${action.action} for ${action.packages.size} packages")
78+
val history = getActionHistory().toMutableList()
79+
history.add(0, action) // Add to beginning
80+
81+
// Keep only last 100 actions
82+
val trimmed = history.take(100)
83+
val json = gson.toJson(trimmed)
84+
historyFile.writeText(json)
85+
Timber.d("Action logged successfully, total logs: ${trimmed.size}")
86+
} catch (e: Exception) {
87+
Timber.e(e, "Failed to log action: ${action.action}")
88+
}
7789
}
7890

7991
fun getActionHistory(): List<ActionLog> {
80-
if (!historyFile.exists()) return emptyList()
92+
if (!historyFile.exists()) {
93+
Timber.d("History file does not exist")
94+
return emptyList()
95+
}
8196
return try {
97+
val content = historyFile.readText()
98+
Timber.d("Reading history file, size: ${content.length} bytes")
8299
val type = object : TypeToken<List<ActionLog>>() {}.type
83-
gson.fromJson(historyFile.readText(), type) ?: emptyList()
100+
val logs: List<ActionLog>? = gson.fromJson(content, type)
101+
Timber.d("Parsed ${logs?.size ?: 0} logs")
102+
logs ?: emptyList()
84103
} catch (e: Exception) {
104+
Timber.e(e, "Failed to read action history")
85105
emptyList()
86106
}
87107
}

app/src/main/java/com/appcontrolx/ui/AppDetailBottomSheet.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,20 @@ class AppDetailBottomSheet : BottomSheetDialogFragment() {
313313
val result = withContext(Dispatchers.IO) { action() }
314314
val success = result?.isSuccess == true
315315

316-
// Log action
317-
if (logActionName != null && appInfo != null) {
316+
// Log action - always log for all action types
317+
if (appInfo != null && rollbackManager != null) {
318+
val finalLogAction = logActionName ?: actionName.uppercase().replace(" ", "_")
318319
withContext(Dispatchers.IO) {
319-
rollbackManager?.logAction(ActionLog(
320-
action = logActionName,
320+
timber.log.Timber.d("Manual action: $finalLogAction, success=$success")
321+
rollbackManager!!.logAction(ActionLog(
322+
action = finalLogAction,
321323
packages = listOf(appInfo!!.packageName),
322324
success = success,
323325
message = if (success) null else "Failed"
324326
))
325327
}
328+
} else {
329+
timber.log.Timber.w("Cannot log: appInfo=${appInfo != null}, rm=${rollbackManager != null}")
326330
}
327331

328332
if (success) {
@@ -365,15 +369,18 @@ class AppDetailBottomSheet : BottomSheetDialogFragment() {
365369
val success = result?.isSuccess == true
366370

367371
// Log action
368-
if (appInfo != null) {
372+
if (appInfo != null && rollbackManager != null) {
369373
withContext(Dispatchers.IO) {
370-
rollbackManager?.logAction(ActionLog(
374+
timber.log.Timber.d("Background action: $logActionName, success=$success")
375+
rollbackManager!!.logAction(ActionLog(
371376
action = logActionName,
372377
packages = listOf(appInfo!!.packageName),
373378
success = success,
374379
message = if (success) null else "Failed"
375380
))
376381
}
382+
} else {
383+
timber.log.Timber.w("Cannot log bg action: appInfo=${appInfo != null}, rm=${rollbackManager != null}")
377384
}
378385

379386
if (success) {

app/src/main/java/com/appcontrolx/ui/AppListFragment.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -417,15 +417,20 @@ class AppListFragment : Fragment() {
417417
}
418418
}
419419
},
420-
onComplete = { _, failCount ->
421-
// Log action
422-
lifecycleScope.launch(Dispatchers.IO) {
423-
rm?.logAction(ActionLog(
424-
action = action.name,
425-
packages = packages,
426-
success = failCount == 0,
427-
message = if (failCount == 0) null else "$failCount failed"
428-
))
420+
onComplete = { successCount, failCount ->
421+
// Log action - always log regardless of action type
422+
if (rm != null) {
423+
lifecycleScope.launch(Dispatchers.IO) {
424+
timber.log.Timber.d("Batch complete: ${action.name}, success=$successCount, fail=$failCount")
425+
rm.logAction(ActionLog(
426+
action = action.name,
427+
packages = packages,
428+
success = failCount == 0,
429+
message = if (failCount == 0) "All $successCount succeeded" else "$failCount failed"
430+
))
431+
}
432+
} else {
433+
timber.log.Timber.w("RollbackManager is null, cannot log action")
429434
}
430435

431436
adapter.deselectAll()

0 commit comments

Comments
 (0)