commit 29e552569e00fba9b9fd560451b2796952ae9d5d
parent 122a48e0475a835e9c0db533614c59500ea3c22d
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Wed, 16 Aug 2023 22:28:43 +0200

refactor(bridge): file operation call

Diffstat:
Mapp/src/main/kotlin/me/rhunk/snapenhance/bridge/BridgeService.kt | 88+++++++++++++++++++++++++------------------------------------------------------
Mcore/src/main/aidl/me/rhunk/snapenhance/bridge/BridgeInterface.aidl | 55+++----------------------------------------------------
Mcore/src/main/kotlin/me/rhunk/snapenhance/bridge/BridgeClient.kt | 11++++++-----
Acore/src/main/kotlin/me/rhunk/snapenhance/bridge/types/FileActionType.kt | 6++++++
4 files changed, 43 insertions(+), 117 deletions(-)

diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/bridge/BridgeService.kt b/app/src/main/kotlin/me/rhunk/snapenhance/bridge/BridgeService.kt @@ -6,6 +6,7 @@ import android.os.IBinder import me.rhunk.snapenhance.RemoteSideContext import me.rhunk.snapenhance.SharedContextHolder import me.rhunk.snapenhance.bridge.types.BridgeFileType +import me.rhunk.snapenhance.bridge.types.FileActionType import me.rhunk.snapenhance.bridge.wrapper.LocaleWrapper import me.rhunk.snapenhance.bridge.wrapper.MessageLoggerWrapper import me.rhunk.snapenhance.download.DownloadProcessor @@ -23,60 +24,35 @@ class BridgeService : Service() { } inner class BridgeBinder : BridgeInterface.Stub() { - override fun createAndReadFile(fileType: Int, defaultContent: ByteArray?): ByteArray { - val file = BridgeFileType.fromValue(fileType)?.resolve(this@BridgeService) - ?: return defaultContent ?: ByteArray(0) - - if (!file.exists()) { - if (defaultContent == null) { - return ByteArray(0) + override fun fileOperation(action: Int, fileType: Int, content: ByteArray?): ByteArray { + val resolvedFile by lazy { BridgeFileType.fromValue(fileType)?.resolve(this@BridgeService) } + + return when (FileActionType.values()[action]) { + FileActionType.CREATE_AND_READ -> { + resolvedFile?.let { + if (!it.exists()) { + return content?.also { content -> it.writeBytes(content) } ?: ByteArray(0) + } + + it.readBytes() + } ?: ByteArray(0) + } + FileActionType.READ -> { + resolvedFile?.takeIf { it.exists() }?.readBytes() ?: ByteArray(0) + } + FileActionType.WRITE -> { + content?.also { resolvedFile?.writeBytes(content) } ?: ByteArray(0) + } + FileActionType.DELETE -> { + resolvedFile?.takeIf { it.exists() }?.delete() + ByteArray(0) + } + FileActionType.EXISTS -> { + if (resolvedFile?.exists() == true) + ByteArray(1) + else ByteArray(0) } - - file.writeBytes(defaultContent) - } - - return file.readBytes() - } - - override fun readFile(fileType: Int): ByteArray { - val file = BridgeFileType.fromValue(fileType)?.resolve(this@BridgeService) - ?: return ByteArray(0) - - if (!file.exists()) { - return ByteArray(0) - } - - return file.readBytes() - } - - override fun writeFile(fileType: Int, content: ByteArray?): Boolean { - val file = BridgeFileType.fromValue(fileType)?.resolve(this@BridgeService) - ?: return false - - if (content == null) { - return false - } - - file.writeBytes(content) - return true - } - - override fun deleteFile(fileType: Int): Boolean { - val file = BridgeFileType.fromValue(fileType)?.resolve(this@BridgeService) - ?: return false - - if (!file.exists()) { - return false } - - return file.delete() - } - - override fun isFileExists(fileType: Int): Boolean { - val file = BridgeFileType.fromValue(fileType)?.resolve(this@BridgeService) - ?: return false - - return file.exists() } override fun getLoggedMessageIds(conversationId: String, limit: Int) = messageLoggerWrapper.getMessageIds(conversationId, limit).toLongArray() @@ -95,14 +71,6 @@ class BridgeService : Service() { it.locale to it.content } - override fun getAutoUpdaterTime(): Long { - throw UnsupportedOperationException() - } - - override fun setAutoUpdaterTime(time: Long) { - throw UnsupportedOperationException() - } - override fun enqueueDownload(intent: Intent, callback: DownloadCallback) { DownloadProcessor( remoteSideContext = SharedContextHolder.remote(this@BridgeService), diff --git a/core/src/main/aidl/me/rhunk/snapenhance/bridge/BridgeInterface.aidl b/core/src/main/aidl/me/rhunk/snapenhance/bridge/BridgeInterface.aidl @@ -5,46 +5,9 @@ import me.rhunk.snapenhance.bridge.DownloadCallback; interface BridgeInterface { /** - * Create a file if it doesn't exist, and read it - * - * @param fileType the type of file to create and read - * @param defaultContent the default content to write to the file if it doesn't exist - * @return the content of the file - */ - byte[] createAndReadFile(int fileType, in byte[] defaultContent); - - /** - * Read a file - * - * @param fileType the type of file to read - * @return the content of the file - */ - byte[] readFile(int fileType); - - /** - * Write a file - * - * @param fileType the type of file to write - * @param content the content to write to the file - * @return true if the file was written successfully - */ - boolean writeFile(int fileType, in byte[] content); - - /** - * Delete a file - * - * @param fileType the type of file to delete - * @return true if the file was deleted successfully - */ - boolean deleteFile(int fileType); - - /** - * Check if a file exists - * - * @param fileType the type of file to check - * @return true if the file exists - */ - boolean isFileExists(int fileType); + * Execute a file operation + */ + byte[] fileOperation(int action, int fileType, in @nullable byte[] content); /** * Get the content of a logged message from the database @@ -90,18 +53,6 @@ interface BridgeInterface { Map<String, String> fetchLocales(String userLocale); /** - * Get check for updates last time - * @return the last time check for updates was done - */ - long getAutoUpdaterTime(); - - /** - * Set check for updates last time - * @param time the time to set - */ - void setAutoUpdaterTime(long time); - - /** * Enqueue a download */ void enqueueDownload(in Intent intent, DownloadCallback callback); diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/bridge/BridgeClient.kt b/core/src/main/kotlin/me/rhunk/snapenhance/bridge/BridgeClient.kt @@ -13,6 +13,7 @@ import de.robv.android.xposed.XposedHelpers import me.rhunk.snapenhance.Logger.xposedLog import me.rhunk.snapenhance.ModContext import me.rhunk.snapenhance.bridge.types.BridgeFileType +import me.rhunk.snapenhance.bridge.types.FileActionType import me.rhunk.snapenhance.core.BuildConfig import me.rhunk.snapenhance.data.LocalePair import java.util.concurrent.CompletableFuture @@ -81,18 +82,18 @@ class BridgeClient( fun createAndReadFile( fileType: BridgeFileType, defaultContent: ByteArray - ): ByteArray = service.createAndReadFile(fileType.value, defaultContent) + ): ByteArray = service.fileOperation(FileActionType.CREATE_AND_READ.ordinal, fileType.value, defaultContent) - fun readFile(fileType: BridgeFileType): ByteArray = service.readFile(fileType.value) + fun readFile(fileType: BridgeFileType): ByteArray = service.fileOperation(FileActionType.READ.ordinal, fileType.value, null) fun writeFile( fileType: BridgeFileType, content: ByteArray? - ): Boolean = service.writeFile(fileType.value, content) + ) { service.fileOperation(FileActionType.WRITE.ordinal, fileType.value, content) } - fun deleteFile(fileType: BridgeFileType) = service.deleteFile(fileType.value) + fun deleteFile(fileType: BridgeFileType) { service.fileOperation(FileActionType.DELETE.ordinal, fileType.value, null) } - fun isFileExists(fileType: BridgeFileType) = service.isFileExists(fileType.value) + fun isFileExists(fileType: BridgeFileType) = service.fileOperation(FileActionType.EXISTS.ordinal, fileType.value, null).isNotEmpty() fun getLoggedMessageIds(conversationId: String, limit: Int): LongArray = service.getLoggedMessageIds(conversationId, limit) diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/bridge/types/FileActionType.kt b/core/src/main/kotlin/me/rhunk/snapenhance/bridge/types/FileActionType.kt @@ -0,0 +1,5 @@ +package me.rhunk.snapenhance.bridge.types + +enum class FileActionType { + CREATE_AND_READ, READ, WRITE, DELETE, EXISTS +}+ \ No newline at end of file