diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9c895ed..ee5b5f3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -51,6 +51,16 @@ android:windowSoftInputMode="adjustResize|stateHidden" android:theme="@style/Theme.OwnDroid"> + + + + + diff --git a/app/src/main/java/com/bintianqi/owndroid/AutomationActivity.kt b/app/src/main/java/com/bintianqi/owndroid/AutomationActivity.kt new file mode 100644 index 0000000..464f93c --- /dev/null +++ b/app/src/main/java/com/bintianqi/owndroid/AutomationActivity.kt @@ -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() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/bintianqi/owndroid/AutomationReceiver.kt b/app/src/main/java/com/bintianqi/owndroid/AutomationReceiver.kt new file mode 100644 index 0000000..a360199 --- /dev/null +++ b/app/src/main/java/com/bintianqi/owndroid/AutomationReceiver.kt @@ -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" +} diff --git a/app/src/main/java/com/bintianqi/owndroid/Setting.kt b/app/src/main/java/com/bintianqi/owndroid/Setting.kt index 43a0852..92f026e 100644 --- a/app/src/main/java/com/bintianqi/owndroid/Setting.kt +++ b/app/src/main/java/com/bintianqi/owndroid/Setting.kt @@ -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() } + ) } } diff --git a/app/src/main/java/com/bintianqi/owndroid/TaskReceiver.kt b/app/src/main/java/com/bintianqi/owndroid/TaskReceiver.kt deleted file mode 100644 index 5e92e60..0000000 --- a/app/src/main/java/com/bintianqi/owndroid/TaskReceiver.kt +++ /dev/null @@ -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") - } - } -} diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 585b315..d9a0639 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -535,7 +535,9 @@ 你不能清除OwnDroid的存储空间 清除存储空间 清除存储空间成功\n应用即将退出 + 自动化 + 调试模式 读取外部存储 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 21abef9..9ff1e1a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -551,7 +551,9 @@ You can\'t clear storage of OwnDroid Clear storage Clear storage success\nApplication will exit + Automation + Debug mode Read external storage