@@ -4,6 +4,7 @@ import android.content.Context
44import com.appcontrolx.executor.CommandExecutor
55import com.google.gson.Gson
66import com.google.gson.reflect.TypeToken
7+ import timber.log.Timber
78import java.io.File
89import 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 }
0 commit comments