update Automation API

This commit is contained in:
BinTianqi
2024-07-16 21:15:12 +08:00
parent 172f7d081e
commit 97cd7447e4
7 changed files with 22 additions and 15 deletions

View File

@@ -1,11 +1,11 @@
package com.bintianqi.owndroid
import android.app.AlertDialog
import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.Text
import androidx.compose.ui.platform.LocalContext
class AutomationActivity: ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@@ -14,9 +14,11 @@ class AutomationActivity: ComponentActivity() {
val sharedPrefs = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE)
if(sharedPrefs.getBoolean("automation_debug", false)) {
setContent {
SelectionContainer {
Text(result)
}
AlertDialog.Builder(LocalContext.current)
.setMessage(result)
.setOnDismissListener { finish() }
.setPositiveButton(R.string.confirm) { _, _ -> finish() }
.show()
}
} else {
finish()

View File

@@ -1,11 +1,11 @@
package com.bintianqi.owndroid
import android.annotation.SuppressLint
import android.app.admin.DevicePolicyManager
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.activity.ComponentActivity
class AutomationReceiver: BroadcastReceiver() {
@@ -14,6 +14,7 @@ class AutomationReceiver: BroadcastReceiver() {
}
}
@SuppressLint("NewApi")
fun handleTask(context: Context, intent: Intent): String {
val sharedPrefs = context.getSharedPreferences("data", Context.MODE_PRIVATE)
val key = sharedPrefs.getString("automation_key", "") ?: ""
@@ -27,6 +28,7 @@ fun handleTask(context: Context, intent: Intent): String {
val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager
val receiver = ComponentName(context,Receiver::class.java)
val app = intent.getStringExtra("app")
val restriction = intent.getStringExtra("restriction")
try {
when(operation) {
"suspend" -> dpm.setPackagesSuspended(receiver, arrayOf(app), true)
@@ -35,6 +37,8 @@ fun handleTask(context: Context, intent: Intent): String {
"unhide" -> dpm.setApplicationHidden(receiver, app, false)
"lock" -> dpm.lockNow()
"reboot" -> dpm.reboot(receiver)
"addUserRestriction" -> dpm.addUserRestriction(receiver, restriction)
"clearUserRestriction" -> dpm.clearUserRestriction(receiver, restriction)
else -> return "Operation not defined"
}
} catch(e: Exception) {

View File

@@ -57,7 +57,7 @@ private fun Home(navCtrl: NavHostController) {
Column(modifier = Modifier.fillMaxSize()) {
SubPageItem(R.string.theme, "", R.drawable.format_paint_fill0) { navCtrl.navigate("Theme") }
SubPageItem(R.string.security, "", R.drawable.lock_fill0) { navCtrl.navigate("Auth") }
SubPageItem(R.string.automation, "", R.drawable.apps_fill0) { navCtrl.navigate("Automation") }
SubPageItem(R.string.automation_api, "", R.drawable.apps_fill0) { navCtrl.navigate("Automation") }
SubPageItem(R.string.about, "", R.drawable.info_fill0) { navCtrl.navigate("About") }
}
}
@@ -132,10 +132,10 @@ private fun Automation() {
val context = LocalContext.current
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())) {
Spacer(Modifier.padding(vertical = 10.dp))
Text(text = stringResource(R.string.automation_api), style = typography.headlineLarge)
Spacer(Modifier.padding(vertical = 5.dp))
var key by remember { mutableStateOf("") }
LaunchedEffect(Unit) {
key = sharedPref.getString("automation_key", "")?: ""
}
TextField(
value = key, onValueChange = { key = it }, label = { Text("Key")},
modifier = Modifier.fillMaxWidth()