Skip to content

Commit

Permalink
add global install_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
iamr0s committed Jun 23, 2023
1 parent e169b3c commit 98c93ed
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 34 deletions.
Binary file modified app/src/main/assets/databases/installer.db
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ data class ConfigEntity(
description = "",
authorizer = Authorizer.Global,
customizeAuthorizer = "",
installMode = InstallMode.Dialog,
installMode = InstallMode.Global,
installer = null,
forAllUser = false,
allowTestOnly = false,
Expand All @@ -55,6 +55,7 @@ data class ConfigEntity(
}

enum class InstallMode(val value: String) {
Global("global"),
Dialog("dialog"),
AutoDialog("auto_dialog"),
Notification("notification"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import com.rosan.installer.data.settings.model.room.entity.ConfigEntity

object InstallModeConverter {
@TypeConverter
fun revert(value: String): ConfigEntity.InstallMode =
ConfigEntity.InstallMode.values().find { it.value == value }
?: ConfigEntity.InstallMode.Dialog
fun revert(value: String?): ConfigEntity.InstallMode =
(if (value != null) ConfigEntity.InstallMode.values().find { it.value == value }
else null) ?: ConfigEntity.InstallMode.Dialog

@TypeConverter
fun convert(value: ConfigEntity.InstallMode): String = value.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import com.rosan.installer.data.settings.model.room.entity.AppEntity
import com.rosan.installer.data.settings.model.room.entity.ConfigEntity
import com.rosan.installer.data.settings.model.room.entity.converter.AuthorizerConverter
import com.rosan.installer.data.settings.model.room.entity.converter.InstallModeConverter
import com.rosan.installer.data.settings.repo.AppRepo
import com.rosan.installer.data.settings.repo.ConfigRepo
import kotlinx.coroutines.Dispatchers
Expand All @@ -24,14 +25,19 @@ class ConfigUtil {
val globalCustomizeAuthorizer: String
get() = sharedPreferences.getString("customize_authorizer", null) ?: ""

val globalInstallMode: ConfigEntity.InstallMode
get() = InstallModeConverter.revert(sharedPreferences.getString("install_mode", null))

suspend fun getByPackageName(packageName: String? = null): ConfigEntity {
val entity = getByPackageNameInner(packageName)
return if (entity.authorizer != ConfigEntity.Authorizer.Global)
entity.copy()
else entity.copy(
authorizer = globalAuthorizer,
customizeAuthorizer = globalCustomizeAuthorizer
)
var entity = getByPackageNameInner(packageName)
if (entity.authorizer == ConfigEntity.Authorizer.Global)
entity = entity.copy(
authorizer = globalAuthorizer,
customizeAuthorizer = globalCustomizeAuthorizer
)
if (entity.installMode == ConfigEntity.InstallMode.Global)
entity = entity.copy(installMode = globalInstallMode)
return entity
}

private suspend fun getByPackageNameInner(packageName: String? = null): ConfigEntity =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,16 @@ fun DataDescriptionWidget(viewModel: EditViewModel) {
fun DataAuthorizerWidget(viewModel: EditViewModel) {
val authorizer = viewModel.state.data.authorizer
val data = mapOf(
ConfigEntity.Authorizer.Global to stringResource(id = R.string.config_authorizer_global),
ConfigEntity.Authorizer.None to stringResource(id = R.string.config_authorizer_none),
ConfigEntity.Authorizer.Root to stringResource(id = R.string.config_authorizer_root),
ConfigEntity.Authorizer.Shizuku to stringResource(id = R.string.config_authorizer_shizuku),
ConfigEntity.Authorizer.Dhizuku to stringResource(id = R.string.config_authorizer_dhizuku),
ConfigEntity.Authorizer.Customize to stringResource(id = R.string.config_authorizer_customize),
ConfigEntity.Authorizer.Global to stringResource(R.string.config_authorizer_global),
ConfigEntity.Authorizer.None to stringResource(R.string.config_authorizer_none),
ConfigEntity.Authorizer.Root to stringResource(R.string.config_authorizer_root),
ConfigEntity.Authorizer.Shizuku to stringResource(R.string.config_authorizer_shizuku),
ConfigEntity.Authorizer.Dhizuku to stringResource(R.string.config_authorizer_dhizuku),
ConfigEntity.Authorizer.Customize to stringResource(R.string.config_authorizer_customize)
)
DropDownMenuWidget(
icon = Icons.TwoTone.Memory,
title = stringResource(id = R.string.config_authorizer),
title = stringResource(R.string.config_authorizer),
description = if (data.containsKey(authorizer)) data[authorizer] else null,
choice = data.keys.toList().indexOf(authorizer),
data = data.values.toList(),
Expand All @@ -223,7 +223,7 @@ fun DataCustomizeAuthorizerWidget(viewModel: EditViewModel) {
Icon(imageVector = Icons.TwoTone.Terminal, contentDescription = null)
},
label = {
Text(text = stringResource(id = R.string.config_customize_authorizer))
Text(text = stringResource(R.string.config_customize_authorizer))
},
value = customizeAuthorizer,
onValueChange = { viewModel.dispatch(EditViewAction.ChangeDataCustomizeAuthorizer(it)) },
Expand All @@ -236,15 +236,16 @@ fun DataCustomizeAuthorizerWidget(viewModel: EditViewModel) {
fun DataInstallModeWidget(viewModel: EditViewModel) {
val installMode = viewModel.state.data.installMode
val data = mapOf(
ConfigEntity.InstallMode.Dialog to stringResource(id = R.string.config_install_mode_dialog),
ConfigEntity.InstallMode.AutoDialog to stringResource(id = R.string.config_install_mode_auto_dialog),
ConfigEntity.InstallMode.Notification to stringResource(id = R.string.config_install_mode_notification),
ConfigEntity.InstallMode.AutoNotification to stringResource(id = R.string.config_install_mode_auto_notification),
ConfigEntity.InstallMode.Ignore to stringResource(id = R.string.config_install_mode_ignore),
ConfigEntity.InstallMode.Global to stringResource(R.string.config_install_mode_global),
ConfigEntity.InstallMode.Dialog to stringResource(R.string.config_install_mode_dialog),
ConfigEntity.InstallMode.AutoDialog to stringResource(R.string.config_install_mode_auto_dialog),
ConfigEntity.InstallMode.Notification to stringResource(R.string.config_install_mode_notification),
ConfigEntity.InstallMode.AutoNotification to stringResource(R.string.config_install_mode_auto_notification),
ConfigEntity.InstallMode.Ignore to stringResource(R.string.config_install_mode_ignore),
)
DropDownMenuWidget(
icon = Icons.TwoTone.Downloading,
title = stringResource(id = R.string.config_install_mode),
title = stringResource(R.string.config_install_mode),
description = if (data.containsKey(installMode)) data[installMode] else null,
choice = data.keys.toList().indexOf(installMode),
data = data.values.toList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fun PreferredPage(
item { LabelWidget(stringResource(R.string.config)) }
item { DataAuthorizerWidget(viewModel) }
item { DataCustomizeAuthorizerWidget(viewModel) }
item { DataInstallModeWidget(viewModel) }
item { LabelWidget(label = stringResource(id = R.string.basic)) }
item { DefaultInstaller(snackBarHostState, true) }
item { DefaultInstaller(snackBarHostState, false) }
Expand All @@ -90,15 +91,15 @@ fun openUrl(context: Context, url: String) {
fun DataAuthorizerWidget(viewModel: PreferredViewModel) {
val authorizer = viewModel.state.authorizer
val data = mapOf(
ConfigEntity.Authorizer.None to stringResource(id = R.string.config_authorizer_none),
ConfigEntity.Authorizer.Root to stringResource(id = R.string.config_authorizer_root),
ConfigEntity.Authorizer.Shizuku to stringResource(id = R.string.config_authorizer_shizuku),
ConfigEntity.Authorizer.Dhizuku to stringResource(id = R.string.config_authorizer_dhizuku),
ConfigEntity.Authorizer.Customize to stringResource(id = R.string.config_authorizer_customize),
ConfigEntity.Authorizer.None to stringResource(R.string.config_authorizer_none),
ConfigEntity.Authorizer.Root to stringResource(R.string.config_authorizer_root),
ConfigEntity.Authorizer.Shizuku to stringResource(R.string.config_authorizer_shizuku),
ConfigEntity.Authorizer.Dhizuku to stringResource(R.string.config_authorizer_dhizuku),
ConfigEntity.Authorizer.Customize to stringResource(R.string.config_authorizer_customize),
)
DropDownMenuWidget(
icon = Icons.TwoTone.Memory,
title = stringResource(id = R.string.config_authorizer),
title = stringResource(R.string.config_authorizer),
description = if (data.containsKey(authorizer)) data[authorizer] else null,
choice = data.keys.toList().indexOf(authorizer),
data = data.values.toList(),
Expand All @@ -122,14 +123,37 @@ fun DataCustomizeAuthorizerWidget(viewModel: PreferredViewModel) {
Icon(imageVector = Icons.TwoTone.Terminal, contentDescription = null)
},
label = {
Text(text = stringResource(id = R.string.config_customize_authorizer))
Text(stringResource(R.string.config_customize_authorizer))
},
value = customizeAuthorizer,
onValueChange = { viewModel.dispatch(PreferredViewAction.ChangeGlobalCustomizeAuthorizer(it)) },
maxLines = 8,
)
}

@Composable
fun DataInstallModeWidget(viewModel: PreferredViewModel) {
val installMode = viewModel.state.installMode
val data = mapOf(
ConfigEntity.InstallMode.Dialog to stringResource(R.string.config_install_mode_dialog),
ConfigEntity.InstallMode.AutoDialog to stringResource(R.string.config_install_mode_auto_dialog),
ConfigEntity.InstallMode.Notification to stringResource(R.string.config_install_mode_notification),
ConfigEntity.InstallMode.AutoNotification to stringResource(R.string.config_install_mode_auto_notification),
ConfigEntity.InstallMode.Ignore to stringResource(R.string.config_install_mode_ignore),
)
DropDownMenuWidget(
icon = Icons.TwoTone.Downloading,
title = stringResource(R.string.config_install_mode),
description = if (data.containsKey(installMode)) data[installMode] else null,
choice = data.keys.toList().indexOf(installMode),
data = data.values.toList(),
) {
data.keys.toList().getOrNull(it)?.let {
viewModel.dispatch(PreferredViewAction.ChangeGlobalInstallMode(it))
}
}
}

@Composable
fun DefaultInstaller(snackBarHostState: SnackbarHostState, lock: Boolean) {
val context = LocalContext.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ sealed class PreferredViewAction {

data class ChangeGlobalCustomizeAuthorizer(val customizeAuthorizer: String) :
PreferredViewAction()

data class ChangeGlobalInstallMode(val installMode: ConfigEntity.InstallMode) :
PreferredViewAction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.core.content.edit
import androidx.lifecycle.ViewModel
import com.rosan.installer.data.settings.model.room.entity.ConfigEntity
import com.rosan.installer.data.settings.model.room.entity.converter.AuthorizerConverter
import com.rosan.installer.data.settings.model.room.entity.converter.InstallModeConverter
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

Expand All @@ -27,6 +28,8 @@ class PreferredViewModel : ViewModel(), KoinComponent {
is PreferredViewAction.ChangeGlobalCustomizeAuthorizer -> changeGlobalCustomizeAuthorizer(
action.customizeAuthorizer
)

is PreferredViewAction.ChangeGlobalInstallMode -> changeGlobalInstallMode(action.installMode)
}
}

Expand All @@ -43,8 +46,12 @@ class PreferredViewModel : ViewModel(), KoinComponent {
(if (authorizer == ConfigEntity.Authorizer.Customize) sharedPreferences.getString(
"customize_authorizer", null
) else null) ?: ""
val installMode =
InstallModeConverter.revert(sharedPreferences.getString("install_mode", null))
state = state.copy(
authorizer = authorizer, customizeAuthorizer = customizeAuthorizer
authorizer = authorizer,
customizeAuthorizer = customizeAuthorizer,
installMode = installMode
)
}
listener.onSharedPreferenceChanged(appSharedPreferences, null)
Expand All @@ -66,4 +73,10 @@ class PreferredViewModel : ViewModel(), KoinComponent {
else remove(key)
}
}

private fun changeGlobalInstallMode(installMode: ConfigEntity.InstallMode) {
appSharedPreferences.edit(true) {
putString("install_mode", InstallModeConverter.convert(installMode))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import com.rosan.installer.data.settings.model.room.entity.ConfigEntity

data class PreferredViewState(
val authorizer: ConfigEntity.Authorizer = ConfigEntity.Authorizer.Shizuku,
val customizeAuthorizer: String = ""
val customizeAuthorizer: String = "",
val installMode: ConfigEntity.InstallMode = ConfigEntity.InstallMode.Dialog
) {
val authorizerCustomize = authorizer == ConfigEntity.Authorizer.Customize
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<string name="config_authorizer_customize">自定义</string>
<string name="config_customize_authorizer">自定义授权器</string>
<string name="config_install_mode">安装方式</string>
<string name="config_install_mode_global">跟随全局</string>
<string name="config_install_mode_dialog">对话框</string>
<string name="config_install_mode_auto_dialog">对话框自动</string>
<string name="config_install_mode_notification">通知栏</string>
Expand Down

0 comments on commit 98c93ed

Please sign in to comment.