mirror of
https://github.com/awfixers-stuff/OwnDroid.git
synced 2026-03-23 11:05:59 +00:00
Fix API
Add SharedPrefs helper class to access shared preferences
This commit is contained in:
@@ -12,6 +12,7 @@ Use Android Device owner privilege to manage your device.
|
||||
- _Wipe data_
|
||||
- ...
|
||||
- Network
|
||||
- Add/modify/delete Wi-Fi
|
||||
- Minimum Wi-Fi security level
|
||||
- Always-on VPN
|
||||
- Network logging
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
- 清除数据
|
||||
- ...
|
||||
- 网络
|
||||
- 添加/修改/删除 Wi-Fi
|
||||
- 最小Wi-Fi安全等级
|
||||
- VPN保持打开
|
||||
- 网络日志
|
||||
|
||||
@@ -76,13 +76,6 @@
|
||||
<receiver
|
||||
android:name=".ApiReceiver"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="com.bintianqi.owndroid.action.HIDE"/>
|
||||
<action android:name="com.bintianqi.owndroid.action.UNHIDE"/>
|
||||
<action android:name="com.bintianqi.owndroid.action.SUSPEND"/>
|
||||
<action android:name="com.bintianqi.owndroid.action.UNSUSPEND"/>
|
||||
<action android:name="com.bintianqi.owndroid.action.LOCK"/>
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<provider
|
||||
android:name="rikka.shizuku.ShizukuProvider"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.bintianqi.owndroid
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
@@ -9,41 +8,40 @@ import com.bintianqi.owndroid.dpm.getDPM
|
||||
import com.bintianqi.owndroid.dpm.getReceiver
|
||||
|
||||
class ApiReceiver: BroadcastReceiver() {
|
||||
@SuppressLint("NewApi")
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val sharedPrefs = context.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
if(sharedPrefs.getBoolean("enable_api", false)) return
|
||||
val key = sharedPrefs.getString("api_key", null)
|
||||
if(key != null && key == intent.getStringExtra("key")) {
|
||||
val requestKey = intent.getStringExtra("key") ?: ""
|
||||
var log = "OwnDroid API request received. action: ${intent.action}\nkey: $requestKey"
|
||||
val sp = SharedPrefs(context)
|
||||
if(!sp.isApiEnabled) return
|
||||
val key = sp.apiKey
|
||||
if(!key.isNullOrEmpty() && key == requestKey) {
|
||||
val dpm = context.getDPM()
|
||||
val receiver = context.getReceiver()
|
||||
val app = intent.getStringExtra("package") ?: ""
|
||||
val app = intent.getStringExtra("package")
|
||||
if(!app.isNullOrEmpty()) log += "\npackage: $app"
|
||||
try {
|
||||
@SuppressWarnings("NewApi")
|
||||
val ok = when(intent.action) {
|
||||
"com.bintianqi.owndroid.action.HIDE" -> dpm.setApplicationHidden(receiver, app, true)
|
||||
"com.bintianqi.owndroid.action.UNHIDE" -> dpm.setApplicationHidden(receiver, app, false)
|
||||
"com.bintianqi.owndroid.action.SUSPEND" -> dpm.setPackagesSuspended(receiver, arrayOf(app), true).isEmpty()
|
||||
"com.bintianqi.owndroid.action.UNSUSPEND" -> dpm.setPackagesSuspended(receiver, arrayOf(app), false).isEmpty()
|
||||
"com.bintianqi.owndroid.action.LOCK" -> { dpm.lockNow(); true }
|
||||
"com.bintianqi.owndroid.action.LOCK" -> { dpm.lockNow(); null }
|
||||
else -> {
|
||||
Log.w(TAG, "Invalid action")
|
||||
resultData = "Invalid action"
|
||||
log += "\nInvalid action"
|
||||
false
|
||||
}
|
||||
}
|
||||
if(!ok) resultCode = 1
|
||||
log += "success: $ok"
|
||||
} catch(e: Exception) {
|
||||
e.printStackTrace()
|
||||
val message = (e::class.qualifiedName ?: "Exception") + ": " + (e.message ?: "")
|
||||
Log.w(TAG, message)
|
||||
resultCode = 1
|
||||
resultData = message
|
||||
log += "\n$message"
|
||||
}
|
||||
} else {
|
||||
Log.w(TAG, "Unauthorized")
|
||||
resultCode = 1
|
||||
resultData = "Unauthorized"
|
||||
log += "\nUnauthorized"
|
||||
}
|
||||
Log.d(TAG, log)
|
||||
}
|
||||
companion object {
|
||||
private const val TAG = "API"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.bintianqi.owndroid
|
||||
|
||||
import android.content.Context
|
||||
import android.widget.Toast
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.biometric.BiometricManager
|
||||
@@ -81,9 +80,8 @@ fun Authenticate(activity: FragmentActivity, navCtrl: NavHostController) {
|
||||
|
||||
fun startAuth(activity: FragmentActivity, callback: AuthenticationCallback) {
|
||||
val context = activity.applicationContext
|
||||
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
val promptInfo = Builder().setTitle(context.getText(R.string.authenticate))
|
||||
if(sharedPref.getInt("biometrics_auth", 0) != 0) {
|
||||
if(SharedPrefs(context).biometricsAuth != 0) {
|
||||
promptInfo.setAllowedAuthenticators(BiometricManager.Authenticators.DEVICE_CREDENTIAL or BiometricManager.Authenticators.BIOMETRIC_WEAK)
|
||||
} else {
|
||||
promptInfo.setAllowedAuthenticators(BiometricManager.Authenticators.DEVICE_CREDENTIAL)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.bintianqi.owndroid
|
||||
|
||||
import android.app.admin.DevicePolicyManager
|
||||
import android.content.Context
|
||||
import android.os.Build.VERSION
|
||||
import android.os.Bundle
|
||||
import android.widget.Toast
|
||||
@@ -170,12 +169,12 @@ class MainActivity : FragmentActivity() {
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
val sharedPref = applicationContext.getSharedPreferences("data", MODE_PRIVATE)
|
||||
if (sharedPref.getBoolean("dhizuku", false)) {
|
||||
val sp = SharedPrefs(applicationContext)
|
||||
if (sp.dhizuku) {
|
||||
if (Dhizuku.init(applicationContext)) {
|
||||
if (!dhizukuPermissionGranted()) { dhizukuErrorStatus.value = 2 }
|
||||
} else {
|
||||
sharedPref.edit().putBoolean("dhizuku", false).apply()
|
||||
sp.dhizuku = false
|
||||
dhizukuErrorStatus.value = 1
|
||||
}
|
||||
}
|
||||
@@ -190,7 +189,7 @@ fun Home(activity: FragmentActivity, vm: MyViewModel) {
|
||||
val context = LocalContext.current
|
||||
val dpm = context.getDPM()
|
||||
val receiver = context.getReceiver()
|
||||
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
val sp = SharedPrefs(context)
|
||||
val focusMgr = LocalFocusManager.current
|
||||
val backToHome by backToHomeStateFlow.collectAsState()
|
||||
val lifecycleOwner = LocalLifecycleOwner.current
|
||||
@@ -325,10 +324,8 @@ fun Home(activity: FragmentActivity, vm: MyViewModel) {
|
||||
DisposableEffect(lifecycleOwner) {
|
||||
val observer = LifecycleEventObserver { _, event ->
|
||||
if(
|
||||
(event == Lifecycle.Event.ON_RESUME &&
|
||||
sharedPref.getBoolean("auth", false) &&
|
||||
sharedPref.getBoolean("lock_in_background", false)) ||
|
||||
(event == Lifecycle.Event.ON_CREATE && sharedPref.getBoolean("auth", false))
|
||||
(event == Lifecycle.Event.ON_RESUME && sp.auth && sp.lockInBackground) ||
|
||||
(event == Lifecycle.Event.ON_CREATE && sp.auth)
|
||||
) {
|
||||
navCtrl.navigate("Authenticate") { launchSingleTop = true }
|
||||
}
|
||||
@@ -339,11 +336,10 @@ fun Home(activity: FragmentActivity, vm: MyViewModel) {
|
||||
}
|
||||
}
|
||||
LaunchedEffect(Unit) {
|
||||
val profileInitialized = sharedPref.getBoolean("ManagedProfileActivated", false)
|
||||
val profileNotActivated = !profileInitialized && context.isProfileOwner && (VERSION.SDK_INT < 24 || dpm.isManagedProfile(receiver))
|
||||
val profileNotActivated = !sp.managedProfileActivated && context.isProfileOwner && (VERSION.SDK_INT < 24 || dpm.isManagedProfile(receiver))
|
||||
if(profileNotActivated) {
|
||||
dpm.setProfileEnabled(receiver)
|
||||
sharedPref.edit().putBoolean("ManagedProfileActivated", true).apply()
|
||||
sp.managedProfileActivated = true
|
||||
Toast.makeText(context, R.string.work_profile_activated, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
@@ -355,7 +351,6 @@ private fun HomePage(navCtrl:NavHostController) {
|
||||
val context = LocalContext.current
|
||||
val dpm = context.getDPM()
|
||||
val receiver = context.getReceiver()
|
||||
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
var activated by remember { mutableStateOf(false) }
|
||||
var activateType by remember { mutableStateOf("") }
|
||||
val deviceAdmin = context.isDeviceAdmin
|
||||
@@ -364,7 +359,7 @@ private fun HomePage(navCtrl:NavHostController) {
|
||||
val refreshStatus by dhizukuErrorStatus.collectAsState()
|
||||
LaunchedEffect(refreshStatus) {
|
||||
activated = context.isProfileOwner || context.isDeviceOwner
|
||||
activateType = if(sharedPref.getBoolean("dhizuku", false)) context.getString(R.string.dhizuku) + " - " else ""
|
||||
activateType = if(SharedPrefs(context).dhizuku) context.getString(R.string.dhizuku) + " - " else ""
|
||||
activateType += context.getString(
|
||||
if(deviceOwner) { R.string.device_owner }
|
||||
else if(profileOwner) {
|
||||
@@ -456,10 +451,9 @@ fun HomePageItem(name: Int, imgVector: Int, navTo: String, navCtrl: NavHostContr
|
||||
private fun DhizukuErrorDialog() {
|
||||
val status by dhizukuErrorStatus.collectAsState()
|
||||
if (status != 0) {
|
||||
val context = LocalContext.current
|
||||
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
val sp = SharedPrefs(LocalContext.current)
|
||||
LaunchedEffect(Unit) {
|
||||
sharedPref.edit().putBoolean("dhizuku", false).apply()
|
||||
sp.dhizuku = false
|
||||
}
|
||||
AlertDialog(
|
||||
onDismissRequest = { dhizukuErrorStatus.value = 0 },
|
||||
@@ -477,7 +471,7 @@ private fun DhizukuErrorDialog() {
|
||||
else -> R.string.failed_to_init_dhizuku
|
||||
}
|
||||
)
|
||||
if(sharedPref.getBoolean("dhizuku", false)) text += "\n" + stringResource(R.string.dhizuku_mode_disabled)
|
||||
if(sp.dhizuku) text += "\n" + stringResource(R.string.dhizuku_mode_disabled)
|
||||
Text(text)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -25,8 +25,7 @@ class ManageSpaceActivity: FragmentActivity() {
|
||||
enableEdgeToEdge()
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
super.onCreate(savedInstanceState)
|
||||
val sharedPref = applicationContext.getSharedPreferences("data", MODE_PRIVATE)
|
||||
val authenticate = sharedPref.getBoolean("auth", false)
|
||||
val authenticate = SharedPrefs(applicationContext).auth
|
||||
val vm by viewModels<MyViewModel>()
|
||||
fun clearStorage() {
|
||||
filesDir.deleteRecursively()
|
||||
@@ -35,6 +34,7 @@ class ManageSpaceActivity: FragmentActivity() {
|
||||
if(Build.VERSION.SDK_INT >= 24) {
|
||||
dataDir.resolve("shared_prefs").deleteRecursively()
|
||||
} else {
|
||||
val sharedPref = applicationContext.getSharedPreferences("data", MODE_PRIVATE)
|
||||
sharedPref.edit().clear().apply()
|
||||
}
|
||||
finish()
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.bintianqi.owndroid
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
@@ -16,19 +14,13 @@ class MyViewModel(application: Application): AndroidViewModel(application) {
|
||||
val userRestrictions = MutableStateFlow(Bundle())
|
||||
|
||||
init {
|
||||
val sharedPrefs = application.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
theme.value = ThemeSettings(
|
||||
materialYou = sharedPrefs.getBoolean("material_you", Build.VERSION.SDK_INT >= 31),
|
||||
darkTheme = if(sharedPrefs.contains("dark_theme")) sharedPrefs.getBoolean("dark_theme", false) else null,
|
||||
blackTheme = sharedPrefs.getBoolean("black_theme", false)
|
||||
)
|
||||
val sp = SharedPrefs(application)
|
||||
theme.value = ThemeSettings(sp.materialYou, sp.darkTheme, sp.blackTheme)
|
||||
viewModelScope.launch {
|
||||
theme.collect {
|
||||
val editor = sharedPrefs.edit()
|
||||
editor.putBoolean("material_you", it.materialYou)
|
||||
if(it.darkTheme == null) editor.remove("dark_theme") else editor.putBoolean("dark_theme", it.darkTheme)
|
||||
editor.putBoolean("black_theme", it.blackTheme)
|
||||
editor.commit()
|
||||
sp.materialYou = it.materialYou
|
||||
sp.darkTheme = it.darkTheme
|
||||
sp.blackTheme = it.blackTheme
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,6 +28,6 @@ class MyViewModel(application: Application): AndroidViewModel(application) {
|
||||
|
||||
data class ThemeSettings(
|
||||
val materialYou: Boolean = false,
|
||||
val darkTheme: Boolean? = null,
|
||||
val darkTheme: Int = -1,
|
||||
val blackTheme: Boolean = false
|
||||
)
|
||||
|
||||
@@ -74,8 +74,7 @@ class Receiver : DeviceAdminReceiver() {
|
||||
|
||||
override fun onTransferOwnershipComplete(context: Context, bundle: PersistableBundle?) {
|
||||
super.onTransferOwnershipComplete(context, bundle)
|
||||
val sp = context.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
sp.edit().putBoolean("dhizuku", false).apply()
|
||||
SharedPrefs(context).dhizuku = false
|
||||
}
|
||||
|
||||
override fun onLockTaskModeEntering(context: Context, intent: Intent, pkg: String) {
|
||||
|
||||
@@ -53,12 +53,12 @@ fun Settings(navCtrl: NavHostController) {
|
||||
|
||||
@Composable
|
||||
fun SettingsOptions(navCtrl: NavHostController) {
|
||||
val sharedPref = LocalContext.current.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
val sp = SharedPrefs(LocalContext.current)
|
||||
MyScaffold(R.string.options, 0.dp, navCtrl) {
|
||||
SwitchItem(
|
||||
R.string.show_dangerous_features, icon = R.drawable.warning_fill0,
|
||||
getState = { sharedPref.getBoolean("dangerous_features", false) },
|
||||
onCheckedChange = { sharedPref.edit().putBoolean("dangerous_features", it).apply() }
|
||||
getState = { sp.displayDangerousFeatures },
|
||||
onCheckedChange = { sp.displayDangerousFeatures = it }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -68,9 +68,9 @@ fun Appearance(navCtrl: NavHostController, vm: MyViewModel) {
|
||||
val theme by vm.theme.collectAsStateWithLifecycle()
|
||||
var darkThemeMenu by remember { mutableStateOf(false) }
|
||||
val darkThemeTextID = when(theme.darkTheme) {
|
||||
true -> R.string.on
|
||||
false -> R.string.off
|
||||
null -> R.string.follow_system
|
||||
1 -> R.string.on
|
||||
0 -> R.string.off
|
||||
else -> R.string.follow_system
|
||||
}
|
||||
MyScaffold(R.string.appearance, 0.dp, navCtrl) {
|
||||
if(VERSION.SDK_INT >= 31) {
|
||||
@@ -85,27 +85,27 @@ fun Appearance(navCtrl: NavHostController, vm: MyViewModel) {
|
||||
DropdownMenuItem(
|
||||
text = { Text(stringResource(R.string.follow_system)) },
|
||||
onClick = {
|
||||
vm.theme.value = theme.copy(darkTheme = null)
|
||||
vm.theme.value = theme.copy(darkTheme = -1)
|
||||
darkThemeMenu = false
|
||||
}
|
||||
)
|
||||
DropdownMenuItem(
|
||||
text = { Text(stringResource(R.string.on)) },
|
||||
onClick = {
|
||||
vm.theme.value = theme.copy(darkTheme = true)
|
||||
vm.theme.value = theme.copy(darkTheme = 1)
|
||||
darkThemeMenu = false
|
||||
}
|
||||
)
|
||||
DropdownMenuItem(
|
||||
text = { Text(stringResource(R.string.off)) },
|
||||
onClick = {
|
||||
vm.theme.value = theme.copy(darkTheme = false)
|
||||
vm.theme.value = theme.copy(darkTheme = 0)
|
||||
darkThemeMenu = false
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
AnimatedVisibility(theme.darkTheme == true || (theme.darkTheme == null && isSystemInDarkTheme())) {
|
||||
AnimatedVisibility(theme.darkTheme == 1 || (theme.darkTheme == -1 && isSystemInDarkTheme())) {
|
||||
SwitchItem(R.string.black_theme, state = theme.blackTheme, onCheckedChange = { vm.theme.value = theme.copy(blackTheme = it) })
|
||||
}
|
||||
}
|
||||
@@ -114,33 +114,33 @@ fun Appearance(navCtrl: NavHostController, vm: MyViewModel) {
|
||||
@Composable
|
||||
fun AuthSettings(navCtrl: NavHostController) {
|
||||
val context = LocalContext.current
|
||||
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
var auth by remember{ mutableStateOf(sharedPref.getBoolean("auth",false)) }
|
||||
val sp = SharedPrefs(context)
|
||||
var auth by remember{ mutableStateOf(sp.auth) }
|
||||
MyScaffold(R.string.security, 0.dp, navCtrl) {
|
||||
SwitchItem(
|
||||
R.string.lock_owndroid, state = auth,
|
||||
onCheckedChange = {
|
||||
sharedPref.edit().putBoolean("auth", it).apply()
|
||||
auth = sharedPref.getBoolean("auth", false)
|
||||
sp.auth = it
|
||||
auth = it
|
||||
}
|
||||
)
|
||||
if(auth) {
|
||||
var bioAuth by remember { mutableIntStateOf(sharedPref.getInt("biometrics_auth", 0)) } // 0:Disabled, 1:Enabled 2:Force enabled
|
||||
var bioAuth by remember { mutableIntStateOf(sp.biometricsAuth) } // 0:Disabled, 1:Enabled 2:Force enabled
|
||||
LaunchedEffect(Unit) {
|
||||
val bioManager = BiometricManager.from(context)
|
||||
if(bioManager.canAuthenticate(BiometricManager.Authenticators.DEVICE_CREDENTIAL) != BiometricManager.BIOMETRIC_SUCCESS) {
|
||||
sp.biometricsAuth = 2
|
||||
bioAuth = 2
|
||||
sharedPref.edit().putInt("biometrics_auth", 2).apply()
|
||||
}
|
||||
}
|
||||
SwitchItem(
|
||||
R.string.enable_bio_auth, state = bioAuth != 0,
|
||||
onCheckedChange = { bioAuth = if(it) 1 else 0; sharedPref.edit().putInt("biometrics_auth", bioAuth).apply() }, enabled = bioAuth != 2
|
||||
onCheckedChange = { bioAuth = if(it) 1 else 0; sp.biometricsAuth = bioAuth }, enabled = bioAuth != 2
|
||||
)
|
||||
SwitchItem(
|
||||
R.string.lock_in_background,
|
||||
getState = { sharedPref.getBoolean("lock_in_background", false) },
|
||||
onCheckedChange = { sharedPref.edit().putBoolean("lock_in_background", it).apply() }
|
||||
getState = { sp.lockInBackground },
|
||||
onCheckedChange = { sp.lockInBackground = it }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -149,16 +149,14 @@ fun AuthSettings(navCtrl: NavHostController) {
|
||||
@Composable
|
||||
fun ApiSettings(navCtrl: NavHostController) {
|
||||
val context = LocalContext.current
|
||||
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
val sp = SharedPrefs(context)
|
||||
MyScaffold(R.string.api, 8.dp, navCtrl) {
|
||||
var enabled by remember { mutableStateOf(sharedPref.getBoolean("enable_api", false)) }
|
||||
LaunchedEffect(enabled) {
|
||||
sharedPref.edit {
|
||||
putBoolean("enable_api", enabled)
|
||||
if(!enabled) remove("api_key")
|
||||
}
|
||||
}
|
||||
SwitchItem(R.string.enable, state = enabled, onCheckedChange = { enabled = it }, padding = false)
|
||||
var enabled by remember { mutableStateOf(sp.isApiEnabled) }
|
||||
SwitchItem(R.string.enable, state = enabled, onCheckedChange = {
|
||||
enabled = it
|
||||
sp.isApiEnabled = it
|
||||
if(!it) sp.sharedPrefs.edit { remove("api.key") }
|
||||
}, padding = false)
|
||||
if(enabled) {
|
||||
var key by remember { mutableStateOf("") }
|
||||
OutlinedTextField(
|
||||
@@ -179,13 +177,13 @@ fun ApiSettings(navCtrl: NavHostController) {
|
||||
Button(
|
||||
modifier = Modifier.fillMaxWidth().padding(bottom = 10.dp),
|
||||
onClick = {
|
||||
sharedPref.edit().putString("api_key", key).apply()
|
||||
sp.apiKey = key
|
||||
context.showOperationResultToast(true)
|
||||
}
|
||||
) {
|
||||
Text(stringResource(R.string.apply))
|
||||
}
|
||||
if(sharedPref.contains("api_key")) InfoCard(R.string.api_key_exist)
|
||||
if(sp.apiKey != null) InfoCard(R.string.api_key_exist)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
45
app/src/main/java/com/bintianqi/owndroid/SharedPrefs.kt
Normal file
45
app/src/main/java/com/bintianqi/owndroid/SharedPrefs.kt
Normal file
@@ -0,0 +1,45 @@
|
||||
package com.bintianqi.owndroid
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import androidx.core.content.edit
|
||||
import kotlin.properties.ReadWriteProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class SharedPrefs(context: Context) {
|
||||
val sharedPrefs = context.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
var managedProfileActivated by BooleanSharedPref("managed_profile_activated")
|
||||
var dhizuku by BooleanSharedPref("dhizuku_mode")
|
||||
var isDefaultAffiliationIdSet by BooleanSharedPref("default_affiliation_id_set")
|
||||
var displayDangerousFeatures by BooleanSharedPref("display_dangerous_features")
|
||||
var isApiEnabled by BooleanSharedPref("api.enabled")
|
||||
var apiKey by StringSharedPref("api.key")
|
||||
var auth by BooleanSharedPref("auth")
|
||||
var biometricsAuth by IntSharedPref("auth.biometrics")
|
||||
var lockInBackground by BooleanSharedPref("auth.lock_in_background")
|
||||
var materialYou by BooleanSharedPref("theme.material_you", Build.VERSION.SDK_INT >= 31)
|
||||
/** -1: follow system, 0: off, 1: on */
|
||||
var darkTheme by IntSharedPref("theme.dark", -1)
|
||||
var blackTheme by BooleanSharedPref("theme.black")
|
||||
}
|
||||
|
||||
private class BooleanSharedPref(val key: String, val defValue: Boolean = false): ReadWriteProperty<SharedPrefs, Boolean> {
|
||||
override fun getValue(thisRef: SharedPrefs, property: KProperty<*>): Boolean =
|
||||
thisRef.sharedPrefs.getBoolean(key, defValue)
|
||||
override fun setValue(thisRef: SharedPrefs, property: KProperty<*>, value: Boolean) =
|
||||
thisRef.sharedPrefs.edit { putBoolean(key, value) }
|
||||
}
|
||||
|
||||
private class StringSharedPref(val key: String): ReadWriteProperty<SharedPrefs, String?> {
|
||||
override fun getValue(thisRef: SharedPrefs, property: KProperty<*>): String? =
|
||||
thisRef.sharedPrefs.getString(key, null)
|
||||
override fun setValue(thisRef: SharedPrefs, property: KProperty<*>, value: String?) =
|
||||
thisRef.sharedPrefs.edit { putString(key, value) }
|
||||
}
|
||||
|
||||
private class IntSharedPref(val key: String, val defValue: Int = 0): ReadWriteProperty<SharedPrefs, Int> {
|
||||
override fun getValue(thisRef: SharedPrefs, property: KProperty<*>): Int =
|
||||
thisRef.sharedPrefs.getInt(key, defValue)
|
||||
override fun setValue(thisRef: SharedPrefs, property: KProperty<*>, value: Int) =
|
||||
thisRef.sharedPrefs.edit { putInt(key, value) }
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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") }
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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") }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ fun OwnDroidTheme(
|
||||
content: @Composable () -> Unit
|
||||
) {
|
||||
val theme by vm.theme.collectAsStateWithLifecycle()
|
||||
val darkTheme = theme.darkTheme == true || (theme.darkTheme == null && isSystemInDarkTheme())
|
||||
val darkTheme = theme.darkTheme == 1 || (theme.darkTheme == -1 && isSystemInDarkTheme())
|
||||
val context = LocalContext.current
|
||||
var colorScheme = when {
|
||||
theme.materialYou && VERSION.SDK_INT >= 31 -> {
|
||||
|
||||
Reference in New Issue
Block a user