Skip to content

Commit

Permalink
feat: switch to Preferences DataStore (ReVanced#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Axelen123 authored Jul 15, 2023
1 parent 5d3b963 commit 879884a
Show file tree
Hide file tree
Showing 18 changed files with 303 additions and 196 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ dependencies {
implementation(libs.compose.activity)
implementation(libs.paging.common.ktx)
implementation(libs.work.runtime.ktx)
implementation(libs.preferences.datastore)

// Compose
implementation(platform(libs.compose.bom))
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/app/revanced/manager/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.getValue
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import app.revanced.manager.domain.manager.PreferencesManager
import app.revanced.manager.ui.destination.Destination
Expand Down Expand Up @@ -56,9 +57,12 @@ class MainActivity : ComponentActivity() {
)

setContent {
val theme by prefs.theme.getAsState()
val dynamicColor by prefs.dynamicColor.getAsState()

ReVancedManagerTheme(
darkTheme = prefs.theme == Theme.SYSTEM && isSystemInDarkTheme() || prefs.theme == Theme.DARK,
dynamicColor = prefs.dynamicColor
darkTheme = theme == Theme.SYSTEM && isSystemInDarkTheme() || theme == Theme.DARK,
dynamicColor = dynamicColor
) {
val navController =
rememberNavController<Destination>(startDestination = Destination.Dashboard)
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/app/revanced/manager/ManagerApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package app.revanced.manager

import android.app.Application
import app.revanced.manager.di.*
import app.revanced.manager.domain.manager.PreferencesManager
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.androidx.workmanager.koin.workManagerFactory
import org.koin.core.context.startKoin

class ManagerApplication : Application() {
private val scope = MainScope()
private val prefs: PreferencesManager by inject()
override fun onCreate() {
super.onCreate()

Expand All @@ -26,5 +32,9 @@ class ManagerApplication : Application() {
databaseModule,
)
}

scope.launch {
prefs.preload()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package app.revanced.manager.di

import android.content.Context
import app.revanced.manager.domain.manager.PreferencesManager
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.module

val preferencesModule = module {
fun providePreferences(
context: Context
) = PreferencesManager(context.getSharedPreferences("preferences", Context.MODE_PRIVATE))

singleOf(::providePreferences)
singleOf(::PreferencesManager)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import android.app.Application
import android.content.Context
import app.revanced.manager.util.signing.Signer
import app.revanced.manager.util.signing.SigningOptions
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.io.InputStream
import java.io.OutputStream
import java.nio.file.Files
import java.nio.file.Path
Expand All @@ -23,39 +24,46 @@ class KeystoreManager(app: Application, private val prefs: PreferencesManager) {
private val keystorePath =
app.getDir("signing", Context.MODE_PRIVATE).resolve("manager.keystore").toPath()

private fun options(
cn: String = prefs.keystoreCommonName!!,
pass: String = prefs.keystorePass!!,
) = SigningOptions(cn, pass, keystorePath)

private fun updatePrefs(cn: String, pass: String) {
prefs.keystoreCommonName = cn
prefs.keystorePass = pass
private suspend fun updatePrefs(cn: String, pass: String) = prefs.edit {
prefs.keystoreCommonName.value = cn
prefs.keystorePass.value = pass
}

fun sign(input: File, output: File) = Signer(options()).signApk(input, output)

init {
if (!keystorePath.exists()) {
regenerate()
}
suspend fun sign(input: File, output: File) = withContext(Dispatchers.Default) {
Signer(
SigningOptions(
prefs.keystoreCommonName.get(),
prefs.keystorePass.get(),
keystorePath
)
).signApk(
input,
output
)
}

fun regenerate() = Signer(options(DEFAULT, DEFAULT)).regenerateKeystore().also {
suspend fun regenerate() = withContext(Dispatchers.Default) {
Signer(SigningOptions(DEFAULT, DEFAULT, keystorePath)).regenerateKeystore()
updatePrefs(DEFAULT, DEFAULT)
}

fun import(cn: String, pass: String, keystore: Path): Boolean {
suspend fun import(cn: String, pass: String, keystore: Path): Boolean {
if (!Signer(SigningOptions(cn, pass, keystore)).canUnlock()) {
return false
}
Files.copy(keystore, keystorePath, StandardCopyOption.REPLACE_EXISTING)
withContext(Dispatchers.IO) {
Files.copy(keystore, keystorePath, StandardCopyOption.REPLACE_EXISTING)
}

updatePrefs(cn, pass)
return true
}

fun export(target: OutputStream) {
Files.copy(keystorePath, target)
fun hasKeystore() = keystorePath.exists()

suspend fun export(target: OutputStream) {
withContext(Dispatchers.IO) {
Files.copy(keystorePath, target)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package app.revanced.manager.domain.manager

import android.content.SharedPreferences
import app.revanced.manager.domain.manager.base.BasePreferenceManager
import android.content.Context
import app.revanced.manager.domain.manager.base.BasePreferencesManager
import app.revanced.manager.ui.theme.Theme

/**
* @author Hyperion Authors, zt64
*/
class PreferencesManager(
sharedPreferences: SharedPreferences
) : BasePreferenceManager(sharedPreferences) {
var dynamicColor by booleanPreference("dynamic_color", true)
var theme by enumPreference("theme", Theme.SYSTEM)
context: Context
) : BasePreferencesManager(context, "settings") {
val dynamicColor = booleanPreference("dynamic_color", true)
val theme = enumPreference("theme", Theme.SYSTEM)

var allowExperimental by booleanPreference("allow_experimental", false)
val allowExperimental = booleanPreference("allow_experimental", false)

var preferSplits by booleanPreference("prefer_splits", false)
val keystoreCommonName = stringPreference("keystore_cn", KeystoreManager.DEFAULT)
val keystorePass = stringPreference("keystore_pass", KeystoreManager.DEFAULT)

var keystoreCommonName by stringPreference("keystore_cn", KeystoreManager.DEFAULT)
var keystorePass by stringPreference("keystore_pass", KeystoreManager.DEFAULT)
val preferSplits = booleanPreference("prefer_splits", false)
}
Loading

0 comments on commit 879884a

Please sign in to comment.