feat: actions

This commit is contained in:
rhunk
2023-05-19 11:47:40 +02:00
parent 50b1d06748
commit cca6ce9ee3
7 changed files with 115 additions and 2 deletions

View File

@@ -13,6 +13,7 @@ import com.google.gson.GsonBuilder
import me.rhunk.snapenhance.bridge.client.BridgeClient
import me.rhunk.snapenhance.database.DatabaseAccess
import me.rhunk.snapenhance.features.Feature
import me.rhunk.snapenhance.manager.impl.ActionManager
import me.rhunk.snapenhance.manager.impl.ConfigManager
import me.rhunk.snapenhance.manager.impl.FeatureManager
import me.rhunk.snapenhance.manager.impl.MappingManager
@@ -36,6 +37,7 @@ class ModContext {
val features = FeatureManager(this)
val mappings = MappingManager(this)
val config = ConfigManager(this)
val actionManager = ActionManager(this)
val database = DatabaseAccess(this)
val downloadServer = DownloadServer(this)
val classCache get() = SnapEnhance.classCache
@@ -86,7 +88,14 @@ class ModContext {
}
fun softRestartApp() {
exitProcess(0)
val intent: Intent? = androidContext.packageManager.getLaunchIntentForPackage(
Constants.SNAPCHAT_PACKAGE_NAME
)
intent?.let {
val mainIntent = Intent.makeRestartActivityTask(intent.component)
androidContext.startActivity(mainIntent)
}
exitProcess(1)
}
fun crash(message: String, throwable: Throwable? = null) {

View File

@@ -60,6 +60,7 @@ class SnapEnhance {
private fun onActivityCreate() {
with(appContext) {
features.onActivityCreate()
actionManager.init()
}
}
}

View File

@@ -0,0 +1,30 @@
package me.rhunk.snapenhance.action
import me.rhunk.snapenhance.ModContext
import java.io.File
abstract class AbstractAction(
val nameKey: String
) {
lateinit var context: ModContext
/**
* called on the main thread when the mod initialize
*/
open fun init() {}
/**
* called when the action is triggered
*/
open fun run() {}
protected open fun deleteRecursively(parent: File?) {
if (parent == null) return
if (parent.isDirectory) for (child in parent.listFiles()!!) deleteRecursively(
child
)
if (parent.exists() && (parent.isFile || parent.isDirectory)) {
parent.delete()
}
}
}

View File

@@ -0,0 +1,38 @@
package me.rhunk.snapenhance.action.impl
import me.rhunk.snapenhance.action.AbstractAction
import java.io.File
class CleanCache : AbstractAction("action.clean_cache") {
companion object {
private val FILES = arrayOf(
"files/mbgl-offline.db",
"files/native_content_manager/*",
"files/file_manager/*",
"files/blizzardv2/*",
"files/streaming/*",
"cache/*",
"databases/arroyo.db",
"databases/arroyo.db-wal",
"databases/native_content_manager/*"
)
}
override fun run() {
FILES.forEach {fileName ->
val fileCache = File(context.androidContext.dataDir, fileName)
if (fileName.endsWith("*")) {
val parent = fileCache.parentFile ?: throw IllegalStateException("Parent file is null")
if (parent.exists()) {
parent.listFiles()?.forEach(this::deleteRecursively)
}
return@forEach
}
if (fileCache.exists()) {
deleteRecursively(fileCache)
}
}
context.softRestartApp()
}
}

View File

@@ -142,7 +142,7 @@ class Notifications : Feature("Notifications", loadParams = FeatureLoadParams.IN
return@onEach
}.onFailure {
Logger.xposedLog("Failed to send preview notification", it)
Logger.debug("urlKey: $urlKey")
Logger.xposedLog("urlKey: $urlKey")
}
}
}

View File

@@ -127,5 +127,16 @@ class SettingsMenu : AbstractMenu() {
addView(createPropertyView(viewModel, it.key))
}
}
//actions
context.actionManager.getActions().forEach {
val button = Button(viewModel.context)
button.text = context.translation.get(it.nameKey)
button.setOnClickListener { _ ->
it.run()
}
ViewAppearanceHelper.applyTheme(viewModel, button)
addView(button)
}
}
}

View File

@@ -0,0 +1,24 @@
package me.rhunk.snapenhance.manager.impl
import me.rhunk.snapenhance.ModContext
import me.rhunk.snapenhance.action.AbstractAction
import me.rhunk.snapenhance.action.impl.CleanCache
import me.rhunk.snapenhance.manager.Manager
import kotlin.reflect.KClass
class ActionManager(
private val context: ModContext,
) : Manager {
private val actions = mutableMapOf<String, AbstractAction>()
fun getActions() = actions.values.toList()
private fun load(clazz: KClass<out AbstractAction>) {
val action = clazz.java.newInstance()
action.context = context
actions[action.nameKey] = action
}
override fun init() {
load(CleanCache::class)
actions.values.forEach(AbstractAction::init)
}
}