Add SharedPrefs helper class to access shared preferences
This commit is contained in:
BinTianqi
2025-02-07 15:51:01 +08:00
parent 5e18c2684c
commit 5e109d74b1
17 changed files with 140 additions and 125 deletions

View File

@@ -23,6 +23,7 @@ import androidx.annotation.RequiresApi
import androidx.annotation.StringRes
import com.bintianqi.owndroid.R
import com.bintianqi.owndroid.Receiver
import com.bintianqi.owndroid.SharedPrefs
import com.bintianqi.owndroid.backToHomeStateFlow
import com.rosan.dhizuku.api.Dhizuku
import com.rosan.dhizuku.api.Dhizuku.binderWrapper
@@ -42,10 +43,9 @@ lateinit var addDeviceAdmin: ActivityResultLauncher<Intent>
val Context.isDeviceOwner: Boolean
get() {
val sharedPref = getSharedPreferences("data", Context.MODE_PRIVATE)
val dpm = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
return dpm.isDeviceOwnerApp(
if(sharedPref.getBoolean("dhizuku", false)) {
if(SharedPrefs(this).dhizuku) {
Dhizuku.getOwnerPackageName()
} else {
"com.bintianqi.owndroid"
@@ -66,8 +66,7 @@ val Context.isDeviceAdmin: Boolean
val Context.dpcPackageName: String
get() {
val sharedPref = getSharedPreferences("data", Context.MODE_PRIVATE)
return if(sharedPref.getBoolean("dhizuku", false)) {
return if(SharedPrefs(this).dhizuku) {
Dhizuku.getOwnerPackageName()
} else {
"com.bintianqi.owndroid"
@@ -119,8 +118,7 @@ private fun binderWrapperPackageInstaller(appContext: Context): PackageInstaller
}
fun Context.getPackageInstaller(): PackageInstaller {
val sharedPref = this.getSharedPreferences("data", Context.MODE_PRIVATE)
if(sharedPref.getBoolean("dhizuku", false)) {
if(SharedPrefs(this).dhizuku) {
if (!dhizukuPermissionGranted()) {
dhizukuErrorStatus.value = 2
backToHomeStateFlow.value = true
@@ -133,8 +131,7 @@ fun Context.getPackageInstaller(): PackageInstaller {
}
fun Context.getDPM(): DevicePolicyManager {
val sharedPref = this.getSharedPreferences("data", Context.MODE_PRIVATE)
if(sharedPref.getBoolean("dhizuku", false)) {
if(SharedPrefs(this).dhizuku) {
if (!dhizukuPermissionGranted()) {
dhizukuErrorStatus.value = 2
backToHomeStateFlow.value = true
@@ -147,8 +144,7 @@ fun Context.getDPM(): DevicePolicyManager {
}
fun Context.getReceiver(): ComponentName {
val sharedPref = this.getSharedPreferences("data", Context.MODE_PRIVATE)
return if(sharedPref.getBoolean("dhizuku", false)) {
return if(SharedPrefs(this).dhizuku) {
Dhizuku.getOwnerComponent()
} else {
ComponentName(this, Receiver::class.java)
@@ -547,8 +543,8 @@ fun parseSecurityEventData(event: SecurityLog.SecurityEvent): JsonElement? {
fun setDefaultAffiliationID(context: Context) {
if(VERSION.SDK_INT < 26) return
val sharedPrefs = context.getSharedPreferences("data", Context.MODE_PRIVATE)
if(!sharedPrefs.getBoolean("default_affiliation_id_set", false)) {
val sp = SharedPrefs(context)
if(!sp.isDefaultAffiliationIdSet) {
try {
val um = context.getSystemService(Context.USER_SERVICE) as UserManager
if(context.isDeviceOwner || (!um.isSystemUser && context.isProfileOwner)) {
@@ -557,7 +553,7 @@ fun setDefaultAffiliationID(context: Context) {
val affiliationIDs = dpm.getAffiliationIds(receiver)
if(affiliationIDs.isEmpty()) {
dpm.setAffiliationIds(receiver, setOf("OwnDroid_default_affiliation_id"))
sharedPrefs.edit().putBoolean("default_affiliation_id_set", true).apply()
sp.isDefaultAffiliationIdSet = true
Log.d("DPM", "Default affiliation id set")
}
}

View File

@@ -134,6 +134,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavHostController
import com.bintianqi.owndroid.MyViewModel
import com.bintianqi.owndroid.R
import com.bintianqi.owndroid.SharedPrefs
import com.bintianqi.owndroid.formatFileSize
import com.bintianqi.owndroid.humanReadableDate
import com.bintianqi.owndroid.showOperationResultToast
@@ -164,8 +165,7 @@ fun Network(navCtrl:NavHostController) {
val receiver = context.getReceiver()
val deviceOwner = context.isDeviceOwner
val profileOwner = context.isProfileOwner
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
val dhizuku = sharedPref.getBoolean("dhizuku", false)
val dhizuku = SharedPrefs(context).dhizuku
MyScaffold(R.string.network, 0.dp, navCtrl) {
if(!dhizuku) FunctionItem(R.string.wifi, icon = R.drawable.wifi_fill0) { navCtrl.navigate("Wifi") }
if(VERSION.SDK_INT >= 30) {

View File

@@ -69,6 +69,7 @@ import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat.startActivity
import androidx.navigation.NavHostController
import com.bintianqi.owndroid.R
import com.bintianqi.owndroid.SharedPrefs
import com.bintianqi.owndroid.showOperationResultToast
import com.bintianqi.owndroid.ui.CardItem
import com.bintianqi.owndroid.ui.CheckBoxItem
@@ -82,14 +83,13 @@ import com.bintianqi.owndroid.yesOrNo
@Composable
fun Password(navCtrl: NavHostController) {
val context = LocalContext.current
val sharedPrefs = context.getSharedPreferences("data", Context.MODE_PRIVATE)
val deviceAdmin = context.isDeviceAdmin
val deviceOwner = context.isDeviceOwner
val profileOwner = context.isProfileOwner
var dialog by remember { mutableIntStateOf(0) }
MyScaffold(R.string.password_and_keyguard, 0.dp, navCtrl) {
FunctionItem(R.string.password_info, icon = R.drawable.info_fill0) { navCtrl.navigate("PasswordInfo") }
if(sharedPrefs.getBoolean("dangerous_features", false)) {
if(SharedPrefs(context).displayDangerousFeatures) {
if(VERSION.SDK_INT >= 26 && (deviceOwner || profileOwner)) {
FunctionItem(R.string.reset_password_token, icon = R.drawable.key_vertical_fill0) { navCtrl.navigate("ResetPasswordToken") }
}

View File

@@ -46,6 +46,7 @@ import androidx.navigation.NavHostController
import androidx.navigation.NavOptions
import com.bintianqi.owndroid.MyViewModel
import com.bintianqi.owndroid.R
import com.bintianqi.owndroid.SharedPrefs
import com.bintianqi.owndroid.backToHomeStateFlow
import com.bintianqi.owndroid.showOperationResultToast
import com.bintianqi.owndroid.ui.*
@@ -63,7 +64,6 @@ fun Permissions(navCtrl: NavHostController) {
val context = LocalContext.current
val dpm = context.getDPM()
val receiver = context.getReceiver()
val sharedPref = LocalContext.current.getSharedPreferences("data", Context.MODE_PRIVATE)
val deviceAdmin = context.isDeviceAdmin
val deviceOwner = context.isDeviceOwner
val profileOwner = context.isProfileOwner
@@ -75,7 +75,7 @@ fun Permissions(navCtrl: NavHostController) {
if(!dpm.isDeviceOwnerApp(context.packageName)) {
SwitchItem(
R.string.dhizuku,
getState = { sharedPref.getBoolean("dhizuku", false) },
getState = { SharedPrefs(context).dhizuku },
onCheckedChange = { toggleDhizukuMode(it, context) },
onClickBlank = { dialog = 4 }
)
@@ -235,9 +235,9 @@ fun Permissions(navCtrl: NavHostController) {
}
private fun toggleDhizukuMode(status: Boolean, context: Context) {
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
val sp = SharedPrefs(context)
if(!status) {
sharedPref.edit().putBoolean("dhizuku", false).apply()
sp.dhizuku = false
backToHomeStateFlow.value = true
return
}
@@ -246,7 +246,7 @@ private fun toggleDhizukuMode(status: Boolean, context: Context) {
return
}
if(dhizukuPermissionGranted()) {
sharedPref.edit().putBoolean("dhizuku", true).apply()
sp.dhizuku = true
Dhizuku.init(context)
backToHomeStateFlow.value = true
} else {
@@ -254,7 +254,7 @@ private fun toggleDhizukuMode(status: Boolean, context: Context) {
@Throws(RemoteException::class)
override fun onRequestPermission(grantResult: Int) {
if(grantResult == PackageManager.PERMISSION_GRANTED) {
sharedPref.edit().putBoolean("dhizuku", true).apply()
sp.dhizuku = true
Dhizuku.init(context)
backToHomeStateFlow.value = true
} else {
@@ -444,14 +444,14 @@ fun DeviceOwner(navCtrl: NavHostController) {
}
}
if(deactivateDialog) {
val sp = SharedPrefs(context)
var resetPolicy by remember { mutableStateOf(false) }
val sharedPref = LocalContext.current.getSharedPreferences("data", Context.MODE_PRIVATE)
val coroutine = rememberCoroutineScope()
AlertDialog(
title = { Text(stringResource(R.string.deactivate)) },
text = {
Column {
if(sharedPref.getBoolean("dhizuku", false)) Text(stringResource(R.string.dhizuku_will_be_deactivated))
if(sp.dhizuku) Text(stringResource(R.string.dhizuku_will_be_deactivated))
Spacer(Modifier.padding(vertical = 4.dp))
CheckBoxItem(text = R.string.reset_device_policy, checked = resetPolicy, operation = { resetPolicy = it })
}
@@ -470,9 +470,9 @@ fun DeviceOwner(navCtrl: NavHostController) {
coroutine.launch {
if(resetPolicy) context.resetDevicePolicy()
dpm.clearDeviceOwnerApp(context.dpcPackageName)
if(sharedPref.getBoolean("dhizuku", false)) {
if(sp.dhizuku) {
if (!Dhizuku.init(context)) {
sharedPref.edit().putBoolean("dhizuku", false).apply()
sp.dhizuku = false
backToHomeStateFlow.value = true
}
}

View File

@@ -124,6 +124,7 @@ import androidx.navigation.NavHostController
import com.bintianqi.owndroid.MyViewModel
import com.bintianqi.owndroid.NotificationUtils
import com.bintianqi.owndroid.R
import com.bintianqi.owndroid.SharedPrefs
import com.bintianqi.owndroid.formatFileSize
import com.bintianqi.owndroid.humanReadableDate
import com.bintianqi.owndroid.showOperationResultToast
@@ -151,9 +152,8 @@ fun SystemManage(navCtrl: NavHostController) {
val context = LocalContext.current
val dpm = context.getDPM()
val receiver = context.getReceiver()
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
val dhizuku = sharedPref.getBoolean("dhizuku", false)
val dangerousFeatures = sharedPref.getBoolean("dangerous_features", false)
val sp = SharedPrefs(context)
val dhizuku = sp.dhizuku
val deviceOwner = context.isDeviceOwner
val profileOwner = context.isProfileOwner
var dialog by remember { mutableIntStateOf(0) }
@@ -208,7 +208,7 @@ fun SystemManage(navCtrl: NavHostController) {
if(VERSION.SDK_INT >= 30 && (deviceOwner || dpm.isOrgProfile(receiver))) {
FunctionItem(R.string.frp_policy, icon = R.drawable.device_reset_fill0) { navCtrl.navigate("FRPPolicy") }
}
if(dangerousFeatures && context.isDeviceAdmin && !(VERSION.SDK_INT >= 24 && profileOwner && dpm.isManagedProfile(receiver))) {
if(sp.displayDangerousFeatures && context.isDeviceAdmin && !(VERSION.SDK_INT >= 24 && profileOwner && dpm.isManagedProfile(receiver))) {
FunctionItem(R.string.wipe_data, icon = R.drawable.device_reset_fill0) { navCtrl.navigate("WipeData") }
}
}