commit cca6ce9ee3327076628858de650a4d86a9cd622f
parent 50b1d067484418c0c4f80d83a038482357a6a019
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date: Fri, 19 May 2023 11:47:40 +0200
feat: actions
Diffstat:
7 files changed, 118 insertions(+), 2 deletions(-)
diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/ModContext.kt b/app/src/main/kotlin/me/rhunk/snapenhance/ModContext.kt
@@ -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) {
diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/SnapEnhance.kt b/app/src/main/kotlin/me/rhunk/snapenhance/SnapEnhance.kt
@@ -60,6 +60,7 @@ class SnapEnhance {
private fun onActivityCreate() {
with(appContext) {
features.onActivityCreate()
+ actionManager.init()
}
}
}
\ No newline at end of file
diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/action/AbstractAction.kt b/app/src/main/kotlin/me/rhunk/snapenhance/action/AbstractAction.kt
@@ -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()
+ }
+ }
+}+
\ No newline at end of file
diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/action/impl/CleanCache.kt b/app/src/main/kotlin/me/rhunk/snapenhance/action/impl/CleanCache.kt
@@ -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()
+ }
+}+
\ No newline at end of file
diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/features/impl/extras/Notifications.kt b/app/src/main/kotlin/me/rhunk/snapenhance/features/impl/extras/Notifications.kt
@@ -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")
}
}
}
diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/features/impl/ui/menus/impl/SettingsMenu.kt b/app/src/main/kotlin/me/rhunk/snapenhance/features/impl/ui/menus/impl/SettingsMenu.kt
@@ -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)
+ }
}
}
\ No newline at end of file
diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/manager/impl/ActionManager.kt b/app/src/main/kotlin/me/rhunk/snapenhance/manager/impl/ActionManager.kt
@@ -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)
+ }
+}+
\ No newline at end of file