add AutomationActivity

This commit is contained in:
BinTianqi
2024-07-02 14:31:01 +08:00
parent 015c571546
commit 172f7d081e
7 changed files with 107 additions and 34 deletions

View File

@@ -51,6 +51,16 @@
android:windowSoftInputMode="adjustResize|stateHidden"
android:theme="@style/Theme.OwnDroid">
</activity>
<activity
android:name=".AutomationActivity"
android:exported="true"
android:launchMode="singleInstance"
android:windowSoftInputMode="adjustResize|stateHidden"
android:theme="@style/Theme.OwnDroid">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
<activity
android:name=".InstallAppActivity"
android:exported="true"
@@ -91,7 +101,7 @@
android:permission="android.permission.BIND_DEVICE_ADMIN">
</receiver>
<receiver
android:name=".TaskReceiver"
android:name=".AutomationReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

View File

@@ -0,0 +1,25 @@
package com.bintianqi.owndroid
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
class AutomationActivity: ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val result = handleTask(applicationContext, this.intent)
val sharedPrefs = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE)
if(sharedPrefs.getBoolean("automation_debug", false)) {
setContent {
SelectionContainer {
Text(result)
}
}
} else {
finish()
}
}
}

View File

@@ -0,0 +1,44 @@
package com.bintianqi.owndroid
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() {
override fun onReceive(context: Context, intent: Intent) {
handleTask(context, intent)
}
}
fun handleTask(context: Context, intent: Intent): String {
val sharedPrefs = context.getSharedPreferences("data", Context.MODE_PRIVATE)
val key = sharedPrefs.getString("automation_key", "") ?: ""
if(key.length < 6) {
return "Key length must longer than 6"
}
if(key != intent.getStringExtra("key")) {
return "Wrong key"
}
val operation = intent.getStringExtra("operation")
val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager
val receiver = ComponentName(context,Receiver::class.java)
val app = intent.getStringExtra("app")
try {
when(operation) {
"suspend" -> dpm.setPackagesSuspended(receiver, arrayOf(app), true)
"unsuspend" -> dpm.setPackagesSuspended(receiver, arrayOf(app), false)
"hide" -> dpm.setApplicationHidden(receiver, app, true)
"unhide" -> dpm.setApplicationHidden(receiver, app, false)
"lock" -> dpm.lockNow()
"reboot" -> dpm.reboot(receiver)
else -> return "Operation not defined"
}
} catch(e: Exception) {
return e.message ?: "Failed to get error message"
}
return "No error, or error is unhandled"
}

View File

@@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build.VERSION
import android.widget.Toast
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
@@ -128,16 +129,31 @@ private fun AuthSettings() {
@Composable
private fun Automation() {
val sharedPref = LocalContext.current.getSharedPreferences("data", Context.MODE_PRIVATE)
Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState())) {
var pkgName by remember { mutableStateOf("") }
val context = LocalContext.current
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())) {
var key by remember { mutableStateOf("") }
LaunchedEffect(Unit) {
pkgName = sharedPref.getString("AutomationApp", "")?: ""
key = sharedPref.getString("automation_key", "")?: ""
}
TextField(value = pkgName, onValueChange = { pkgName = it }, label = { Text("Package name")})
Button(onClick = {sharedPref.edit().putString("AutomationApp", pkgName).apply()}) {
Text("apply")
TextField(
value = key, onValueChange = { key = it }, label = { Text("Key")},
modifier = Modifier.fillMaxWidth()
)
Button(
modifier = Modifier.fillMaxWidth(),
onClick = {
sharedPref.edit().putString("automation_key", key).apply()
Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show()
}
) {
Text(stringResource(R.string.apply))
}
SwitchItem(
R.string.automation_debug, "", null,
{ sharedPref.getBoolean("automation_debug", false) },
{ sharedPref.edit().putBoolean("automation_debug", it).apply() }
)
}
}

View File

@@ -1,26 +0,0 @@
package com.bintianqi.owndroid
import android.app.admin.DevicePolicyManager
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build.VERSION
import android.util.Log
import androidx.activity.ComponentActivity
class TaskReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.getStringExtra("action")
val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager
val receiver = ComponentName(context,Receiver::class.java)
val app = intent.getStringExtra("app")
if(action == "suspend") {
dpm.setPackagesSuspended(receiver, arrayOf(app), true)
} else if(action == "unsuspend") {
dpm.setPackagesSuspended(receiver, arrayOf(app), false)
} else {
Log.d("OwnDroid", "unknown action")
}
}
}

View File

@@ -535,7 +535,9 @@
<string name="you_cant_clear_storage">你不能清除OwnDroid的存储空间</string>
<string name="clear_storage">清除存储空间</string>
<string name="clear_storage_success">清除存储空间成功\n应用即将退出</string>
<string name="automation">自动化</string>
<string name="automation_debug">调试模式</string>
<!--AndroidPermission-->
<string name="permission_READ_EXTERNAL_STORAGE">读取外部存储</string>

View File

@@ -551,7 +551,9 @@
<string name="you_cant_clear_storage">You can\'t clear storage of OwnDroid</string>
<string name="clear_storage">Clear storage</string>
<string name="clear_storage_success">Clear storage success\nApplication will exit</string>
<string name="automation">Automation</string>
<string name="automation_debug">Debug mode</string>
<!--AndroidPermission-->
<string name="permission_READ_EXTERNAL_STORAGE">Read external storage</string>