mirror of
https://github.com/awfixers-stuff/OwnDroid.git
synced 2026-03-23 19:15:58 +00:00
Refactor code related to navigation, close #104
New PackageChooserActivity Delete code of parsing apk metadata
This commit is contained in:
@@ -9,10 +9,12 @@ import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Binder
|
||||
import android.os.Build.VERSION
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import android.os.RemoteException
|
||||
import android.os.UserManager
|
||||
import android.widget.Toast
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
@@ -41,10 +43,7 @@ import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.core.os.bundleOf
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.navigation.NavOptions
|
||||
import com.bintianqi.owndroid.MyViewModel
|
||||
import com.bintianqi.owndroid.ChoosePackageContract
|
||||
import com.bintianqi.owndroid.R
|
||||
import com.bintianqi.owndroid.SharedPrefs
|
||||
import com.bintianqi.owndroid.backToHomeStateFlow
|
||||
@@ -55,12 +54,15 @@ import com.bintianqi.owndroid.yesOrNo
|
||||
import com.rosan.dhizuku.api.Dhizuku
|
||||
import com.rosan.dhizuku.api.DhizukuRequestPermissionListener
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.serialization.Serializable
|
||||
import rikka.shizuku.Shizuku
|
||||
import rikka.sui.Sui
|
||||
|
||||
@Serializable object Permissions
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
@Composable
|
||||
fun Permissions(navCtrl: NavHostController) {
|
||||
fun PermissionsScreen(onNavigateUp: () -> Unit, onNavigate: (Any) -> Unit, onNavigateToShizuku: (Bundle) -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val dpm = context.getDPM()
|
||||
val receiver = context.getReceiver()
|
||||
@@ -71,7 +73,7 @@ fun Permissions(navCtrl: NavHostController) {
|
||||
var dialog by remember { mutableIntStateOf(0) }
|
||||
var bindingShizuku by remember { mutableStateOf(false) }
|
||||
val enrollmentSpecificId = if(VERSION.SDK_INT >= 31 && (deviceOwner || profileOwner)) dpm.enrollmentSpecificId else ""
|
||||
MyScaffold(R.string.permissions, 0.dp, navCtrl) {
|
||||
MyScaffold(R.string.permissions, 0.dp, onNavigateUp) {
|
||||
if(!dpm.isDeviceOwnerApp(context.packageName)) {
|
||||
SwitchItem(
|
||||
R.string.dhizuku,
|
||||
@@ -82,18 +84,18 @@ fun Permissions(navCtrl: NavHostController) {
|
||||
}
|
||||
FunctionItem(
|
||||
R.string.device_admin, stringResource(if(deviceAdmin) R.string.activated else R.string.deactivated),
|
||||
operation = { navCtrl.navigate("DeviceAdmin") }
|
||||
operation = { onNavigate(DeviceAdmin) }
|
||||
)
|
||||
if(profileOwner || !userManager.isSystemUser) {
|
||||
FunctionItem(
|
||||
R.string.profile_owner, stringResource(if(profileOwner) R.string.activated else R.string.deactivated),
|
||||
operation = { navCtrl.navigate("ProfileOwner") }
|
||||
operation = { onNavigate(ProfileOwner) }
|
||||
)
|
||||
}
|
||||
if(!profileOwner && userManager.isSystemUser) {
|
||||
FunctionItem(
|
||||
R.string.device_owner, stringResource(if(deviceOwner) R.string.activated else R.string.deactivated),
|
||||
operation = { navCtrl.navigate("DeviceOwner") }
|
||||
operation = { onNavigate(DeviceOwner) }
|
||||
)
|
||||
}
|
||||
FunctionItem(R.string.shizuku) {
|
||||
@@ -101,9 +103,8 @@ fun Permissions(navCtrl: NavHostController) {
|
||||
if(Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED) {
|
||||
bindingShizuku = true
|
||||
fun onBind(binder: IBinder) {
|
||||
val destinationId = navCtrl.graph.findNode("Shizuku")!!.id
|
||||
bindingShizuku = false
|
||||
navCtrl.navigate(destinationId, bundleOf("binder" to binder), NavOptions.Builder().setLaunchSingleTop(true).build())
|
||||
onNavigateToShizuku(bundleOf("binder" to binder))
|
||||
}
|
||||
try {
|
||||
controlShizukuService(context, ::onBind, { bindingShizuku = false }, true)
|
||||
@@ -129,8 +130,8 @@ fun Permissions(navCtrl: NavHostController) {
|
||||
}
|
||||
}
|
||||
if(VERSION.SDK_INT >= 26 && (deviceOwner || profileOwner))
|
||||
FunctionItem(R.string.delegated_admins) { navCtrl.navigate("DelegatedAdmins") }
|
||||
FunctionItem(R.string.device_info, icon = R.drawable.perm_device_information_fill0) { navCtrl.navigate("DeviceInfo") }
|
||||
FunctionItem(R.string.delegated_admins) { onNavigate(DelegatedAdmins) }
|
||||
FunctionItem(R.string.device_info, icon = R.drawable.perm_device_information_fill0) { onNavigate(DeviceInfo) }
|
||||
if(VERSION.SDK_INT >= 24 && (profileOwner || (VERSION.SDK_INT >= 26 && deviceOwner))) {
|
||||
FunctionItem(R.string.org_name, icon = R.drawable.corporate_fare_fill0) { dialog = 2 }
|
||||
}
|
||||
@@ -141,13 +142,13 @@ fun Permissions(navCtrl: NavHostController) {
|
||||
FunctionItem(R.string.enrollment_specific_id, icon = R.drawable.id_card_fill0) { dialog = 1 }
|
||||
}
|
||||
if(VERSION.SDK_INT >= 24 && (deviceOwner || dpm.isOrgProfile(receiver))) {
|
||||
FunctionItem(R.string.lock_screen_info, icon = R.drawable.screen_lock_portrait_fill0) { navCtrl.navigate("LockScreenInfo") }
|
||||
FunctionItem(R.string.lock_screen_info, icon = R.drawable.screen_lock_portrait_fill0) { onNavigate(LockScreenInfo) }
|
||||
}
|
||||
if(VERSION.SDK_INT >= 24 && deviceAdmin) {
|
||||
FunctionItem(R.string.support_messages, icon = R.drawable.chat_fill0) { navCtrl.navigate("SupportMessages") }
|
||||
FunctionItem(R.string.support_messages, icon = R.drawable.chat_fill0) { onNavigate(SupportMessage) }
|
||||
}
|
||||
if(VERSION.SDK_INT >= 28 && (deviceOwner || profileOwner)) {
|
||||
FunctionItem(R.string.transfer_ownership, icon = R.drawable.admin_panel_settings_fill0) { navCtrl.navigate("TransferOwnership") }
|
||||
FunctionItem(R.string.transfer_ownership, icon = R.drawable.admin_panel_settings_fill0) { onNavigate(TransferOwnership) }
|
||||
}
|
||||
}
|
||||
if(bindingShizuku) {
|
||||
@@ -265,15 +266,17 @@ private fun toggleDhizukuMode(status: Boolean, context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable object LockScreenInfo
|
||||
|
||||
@RequiresApi(24)
|
||||
@Composable
|
||||
fun LockScreenInfo(navCtrl: NavHostController) {
|
||||
fun LockScreenInfoScreen(onNavigateUp: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val dpm = context.getDPM()
|
||||
val receiver = context.getReceiver()
|
||||
val focusMgr = LocalFocusManager.current
|
||||
var infoText by remember { mutableStateOf(dpm.deviceOwnerLockScreenInfo?.toString() ?: "") }
|
||||
MyScaffold(R.string.lock_screen_info, 8.dp, navCtrl) {
|
||||
MyScaffold(R.string.lock_screen_info, 8.dp, onNavigateUp) {
|
||||
OutlinedTextField(
|
||||
value = infoText,
|
||||
label = { Text(stringResource(R.string.lock_screen_info)) },
|
||||
@@ -307,14 +310,16 @@ fun LockScreenInfo(navCtrl: NavHostController) {
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable object DeviceAdmin
|
||||
|
||||
@Composable
|
||||
fun DeviceAdmin(navCtrl: NavHostController) {
|
||||
fun DeviceAdminScreen(onNavigateUp: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val dpm = context.getDPM()
|
||||
val receiver = context.getReceiver()
|
||||
var deactivateDialog by remember { mutableStateOf(false) }
|
||||
val deviceAdmin = context.isDeviceAdmin
|
||||
MyScaffold(R.string.device_admin, 8.dp, navCtrl) {
|
||||
MyScaffold(R.string.device_admin, 8.dp, onNavigateUp) {
|
||||
Text(text = stringResource(if(context.isDeviceAdmin) R.string.activated else R.string.deactivated), style = typography.titleLarge)
|
||||
Spacer(Modifier.padding(vertical = 5.dp))
|
||||
AnimatedVisibility(deviceAdmin) {
|
||||
@@ -364,14 +369,16 @@ fun DeviceAdmin(navCtrl: NavHostController) {
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable object ProfileOwner
|
||||
|
||||
@Composable
|
||||
fun ProfileOwner(navCtrl: NavHostController) {
|
||||
fun ProfileOwnerScreen(onNavigateUp: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val dpm = context.getDPM()
|
||||
val receiver = context.getReceiver()
|
||||
var deactivateDialog by remember { mutableStateOf(false) }
|
||||
val profileOwner = context.isProfileOwner
|
||||
MyScaffold(R.string.profile_owner, 8.dp, navCtrl) {
|
||||
MyScaffold(R.string.profile_owner, 8.dp, onNavigateUp) {
|
||||
Text(stringResource(if(profileOwner) R.string.activated else R.string.deactivated), style = typography.titleLarge)
|
||||
Spacer(Modifier.padding(vertical = 5.dp))
|
||||
if(VERSION.SDK_INT >= 24 && profileOwner) {
|
||||
@@ -417,13 +424,15 @@ fun ProfileOwner(navCtrl: NavHostController) {
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable object DeviceOwner
|
||||
|
||||
@Composable
|
||||
fun DeviceOwner(navCtrl: NavHostController) {
|
||||
fun DeviceOwnerScreen(onNavigateUp: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val dpm = context.getDPM()
|
||||
var deactivateDialog by remember { mutableStateOf(false) }
|
||||
val deviceOwner = context.isDeviceOwner
|
||||
MyScaffold(R.string.device_owner, 8.dp, navCtrl) {
|
||||
MyScaffold(R.string.device_owner, 8.dp, onNavigateUp) {
|
||||
Text(text = stringResource(if(deviceOwner) R.string.activated else R.string.deactivated), style = typography.titleLarge)
|
||||
Spacer(Modifier.padding(vertical = 5.dp))
|
||||
AnimatedVisibility(deviceOwner) {
|
||||
@@ -502,9 +511,11 @@ private enum class DelegatedScope(val id: String, @StringRes val string: Int, va
|
||||
SecurityLogging(DevicePolicyManager.DELEGATION_SECURITY_LOGGING, R.string.security_logging, 31)
|
||||
}
|
||||
|
||||
@Serializable object DelegatedAdmins
|
||||
|
||||
@RequiresApi(26)
|
||||
@Composable
|
||||
fun DelegatedAdmins(navCtrl: NavHostController, vm: MyViewModel) {
|
||||
fun DelegatedAdminsScreen(onNavigateUp: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val dpm = context.getDPM()
|
||||
val receiver = context.getReceiver()
|
||||
@@ -529,7 +540,7 @@ fun DelegatedAdmins(navCtrl: NavHostController, vm: MyViewModel) {
|
||||
packages.putAll(list)
|
||||
}
|
||||
LaunchedEffect(Unit) { refresh() }
|
||||
MyScaffold(R.string.delegated_admins, 0.dp, navCtrl) {
|
||||
MyScaffold(R.string.delegated_admins, 0.dp, onNavigateUp) {
|
||||
packages.forEach { (pkg, scopes) ->
|
||||
Column(
|
||||
modifier = Modifier
|
||||
@@ -558,12 +569,8 @@ fun DelegatedAdmins(navCtrl: NavHostController, vm: MyViewModel) {
|
||||
Text(stringResource(R.string.add_delegated_admin), style = typography.titleLarge)
|
||||
}
|
||||
if(dialog != 0) {
|
||||
val selectedPackage by vm.selectedPackage.collectAsStateWithLifecycle()
|
||||
LaunchedEffect(selectedPackage) {
|
||||
if(selectedPackage != "") {
|
||||
inputPackageName = selectedPackage
|
||||
vm.selectedPackage.value = ""
|
||||
}
|
||||
val choosePackage = rememberLauncherForActivityResult(ChoosePackageContract()) { result ->
|
||||
result?.let { inputPackageName = it }
|
||||
}
|
||||
AlertDialog(
|
||||
text = {
|
||||
@@ -573,7 +580,7 @@ fun DelegatedAdmins(navCtrl: NavHostController, vm: MyViewModel) {
|
||||
value = inputPackageName, onValueChange = { inputPackageName = it },
|
||||
label = { Text(stringResource(R.string.package_name)) },
|
||||
trailingIcon = {
|
||||
if(dialog == 2) IconButton({ navCtrl.navigate("PackageSelector") }) {
|
||||
if(dialog == 2) IconButton({ choosePackage.launch(null) }) {
|
||||
Icon(painterResource(R.drawable.list_fill0), null)
|
||||
}
|
||||
},
|
||||
@@ -614,13 +621,15 @@ fun DelegatedAdmins(navCtrl: NavHostController, vm: MyViewModel) {
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable object DeviceInfo
|
||||
|
||||
@Composable
|
||||
fun DeviceInfo(navCtrl: NavHostController) {
|
||||
fun DeviceInfoScreen(onNavigateUp: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val dpm = context.getDPM()
|
||||
val receiver = context.getReceiver()
|
||||
var dialog by remember { mutableIntStateOf(0) }
|
||||
MyScaffold(R.string.device_info, 8.dp, navCtrl) {
|
||||
MyScaffold(R.string.device_info, 8.dp, onNavigateUp) {
|
||||
if(VERSION.SDK_INT>=34 && (context.isDeviceOwner || dpm.isOrgProfile(receiver))) {
|
||||
CardItem(R.string.financed_device, dpm.isDeviceFinanced.yesOrNo)
|
||||
}
|
||||
@@ -654,9 +663,11 @@ fun DeviceInfo(navCtrl: NavHostController) {
|
||||
)
|
||||
}
|
||||
|
||||
@Serializable object SupportMessage
|
||||
|
||||
@RequiresApi(24)
|
||||
@Composable
|
||||
fun SupportMessages(navCtrl: NavHostController) {
|
||||
fun SupportMessageScreen(onNavigateUp: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val dpm = context.getDPM()
|
||||
val receiver = context.getReceiver()
|
||||
@@ -667,7 +678,7 @@ fun SupportMessages(navCtrl: NavHostController) {
|
||||
longMsg = dpm.getLongSupportMessage(receiver)?.toString() ?: ""
|
||||
}
|
||||
LaunchedEffect(Unit) { refreshMsg() }
|
||||
MyScaffold(R.string.support_messages, 8.dp, navCtrl) {
|
||||
MyScaffold(R.string.support_messages, 8.dp, onNavigateUp) {
|
||||
OutlinedTextField(
|
||||
value = shortMsg,
|
||||
label = { Text(stringResource(R.string.short_support_msg)) },
|
||||
@@ -732,15 +743,17 @@ fun SupportMessages(navCtrl: NavHostController) {
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable object TransferOwnership
|
||||
|
||||
@RequiresApi(28)
|
||||
@Composable
|
||||
fun TransferOwnership(navCtrl: NavHostController) {
|
||||
fun TransferOwnershipScreen(onNavigateUp: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val focusMgr = LocalFocusManager.current
|
||||
var input by remember { mutableStateOf("") }
|
||||
val componentName = ComponentName.unflattenFromString(input)
|
||||
var dialog by remember { mutableStateOf(false) }
|
||||
MyScaffold(R.string.transfer_ownership, 8.dp, navCtrl) {
|
||||
MyScaffold(R.string.transfer_ownership, 8.dp, onNavigateUp) {
|
||||
OutlinedTextField(
|
||||
value = input, onValueChange = { input = it }, label = { Text(stringResource(R.string.target_component_name)) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
|
||||
Reference in New Issue
Block a user