@@ -4,8 +4,9 @@ import android.app.Application
44import android.content.Context
55import app.revanced.manager.util.signing.Signer
66import app.revanced.manager.util.signing.SigningOptions
7+ import kotlinx.coroutines.Dispatchers
8+ import kotlinx.coroutines.withContext
79import java.io.File
8- import java.io.InputStream
910import java.io.OutputStream
1011import java.nio.file.Files
1112import java.nio.file.Path
@@ -23,39 +24,46 @@ class KeystoreManager(app: Application, private val prefs: PreferencesManager) {
2324 private val keystorePath =
2425 app.getDir(" signing" , Context .MODE_PRIVATE ).resolve(" manager.keystore" ).toPath()
2526
26- private fun options (
27- cn : String = prefs.keystoreCommonName!!,
28- pass : String = prefs.keystorePass!!,
29- ) = SigningOptions (cn, pass, keystorePath)
30-
31- private fun updatePrefs (cn : String , pass : String ) {
32- prefs.keystoreCommonName = cn
33- prefs.keystorePass = pass
27+ private suspend fun updatePrefs (cn : String , pass : String ) = prefs.edit {
28+ prefs.keystoreCommonName.value = cn
29+ prefs.keystorePass.value = pass
3430 }
3531
36- fun sign (input : File , output : File ) = Signer (options()).signApk(input, output)
37-
38- init {
39- if (! keystorePath.exists()) {
40- regenerate()
41- }
32+ suspend fun sign (input : File , output : File ) = withContext(Dispatchers .Default ) {
33+ Signer (
34+ SigningOptions (
35+ prefs.keystoreCommonName.get(),
36+ prefs.keystorePass.get(),
37+ keystorePath
38+ )
39+ ).signApk(
40+ input,
41+ output
42+ )
4243 }
4344
44- fun regenerate () = Signer (options(DEFAULT , DEFAULT )).regenerateKeystore().also {
45+ suspend fun regenerate () = withContext(Dispatchers .Default ) {
46+ Signer (SigningOptions (DEFAULT , DEFAULT , keystorePath)).regenerateKeystore()
4547 updatePrefs(DEFAULT , DEFAULT )
4648 }
4749
48- fun import (cn : String , pass : String , keystore : Path ): Boolean {
50+ suspend fun import (cn : String , pass : String , keystore : Path ): Boolean {
4951 if (! Signer (SigningOptions (cn, pass, keystore)).canUnlock()) {
5052 return false
5153 }
52- Files .copy(keystore, keystorePath, StandardCopyOption .REPLACE_EXISTING )
54+ withContext(Dispatchers .IO ) {
55+ Files .copy(keystore, keystorePath, StandardCopyOption .REPLACE_EXISTING )
56+ }
5357
5458 updatePrefs(cn, pass)
5559 return true
5660 }
5761
58- fun export (target : OutputStream ) {
59- Files .copy(keystorePath, target)
62+ fun hasKeystore () = keystorePath.exists()
63+
64+ suspend fun export (target : OutputStream ) {
65+ withContext(Dispatchers .IO ) {
66+ Files .copy(keystorePath, target)
67+ }
6068 }
6169}
0 commit comments