mirror of
https://github.com/awfixers-stuff/OwnDroid.git
synced 2026-03-23 19:15:58 +00:00
Refactor code of API
Rename Automation API to API Add API section to READMEs
This commit is contained in:
51
app/src/main/java/com/bintianqi/owndroid/ApiReceiver.kt
Normal file
51
app/src/main/java/com/bintianqi/owndroid/ApiReceiver.kt
Normal file
@@ -0,0 +1,51 @@
|
||||
package com.bintianqi.owndroid
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
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 dpm = context.getDPM()
|
||||
val receiver = context.getReceiver()
|
||||
val app = intent.getStringExtra("package") ?: ""
|
||||
try {
|
||||
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 }
|
||||
else -> {
|
||||
Log.w(TAG, "Invalid action")
|
||||
resultData = "Invalid action"
|
||||
false
|
||||
}
|
||||
}
|
||||
if(!ok) resultCode = 1
|
||||
} catch(e: Exception) {
|
||||
e.printStackTrace()
|
||||
val message = (e::class.qualifiedName ?: "Exception") + ": " + (e.message ?: "")
|
||||
Log.w(TAG, message)
|
||||
resultCode = 1
|
||||
resultData = message
|
||||
}
|
||||
} else {
|
||||
Log.w(TAG, "Unauthorized")
|
||||
resultCode = 1
|
||||
resultData = "Unauthorized"
|
||||
}
|
||||
}
|
||||
companion object {
|
||||
private const val TAG = "API"
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
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.ui.platform.LocalContext
|
||||
|
||||
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 {
|
||||
AlertDialog.Builder(LocalContext.current)
|
||||
.setMessage(result)
|
||||
.setOnDismissListener { finish() }
|
||||
.setPositiveButton(R.string.confirm) { _, _ -> finish() }
|
||||
.show()
|
||||
}
|
||||
} else {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.bintianqi.owndroid
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.bintianqi.owndroid.dpm.getDPM
|
||||
import com.bintianqi.owndroid.dpm.getReceiver
|
||||
|
||||
class AutomationReceiver: BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
handleTask(context, intent)
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
fun handleTask(context: Context, intent: Intent): String {
|
||||
val sharedPrefs = context.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
val key = sharedPrefs.getString("automation_key", "")
|
||||
if(key == null || key != intent.getStringExtra("key")) {
|
||||
return "Wrong key"
|
||||
}
|
||||
val operation = intent.getStringExtra("operation")
|
||||
val dpm = context.getDPM()
|
||||
val receiver = context.getReceiver()
|
||||
val app = intent.getStringExtra("app")
|
||||
val restriction = intent.getStringExtra("restriction")
|
||||
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)
|
||||
"addUserRestriction" -> dpm.addUserRestriction(receiver, restriction)
|
||||
"clearUserRestriction" -> dpm.clearUserRestriction(receiver, restriction)
|
||||
else -> return "Operation not defined"
|
||||
}
|
||||
} catch(e: Exception) {
|
||||
return e.message ?: "Failed to get error message"
|
||||
}
|
||||
return "No error, or error is unhandled"
|
||||
}
|
||||
@@ -299,7 +299,7 @@ fun Home(activity: FragmentActivity, vm: MyViewModel) {
|
||||
composable(route = "Options") { SettingsOptions(navCtrl) }
|
||||
composable(route = "Appearance") { Appearance(navCtrl, vm) }
|
||||
composable(route = "AuthSettings") { AuthSettings(navCtrl) }
|
||||
composable(route = "Automation") { Automation(navCtrl) }
|
||||
composable(route = "ApiSettings") { ApiSettings(navCtrl) }
|
||||
composable(route = "About") { About(navCtrl) }
|
||||
|
||||
composable(route = "PackageSelector") { PackageSelector(navCtrl) }
|
||||
|
||||
@@ -82,7 +82,7 @@ class Receiver : DeviceAdminReceiver() {
|
||||
|
||||
val installAppDone = MutableStateFlow(false)
|
||||
|
||||
class PackageInstallerReceiver:BroadcastReceiver(){
|
||||
class PackageInstallerReceiver: BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val toastText = when(intent.getIntExtra(EXTRA_STATUS, 999)){
|
||||
STATUS_PENDING_USER_ACTION -> R.string.status_pending_action
|
||||
|
||||
@@ -32,9 +32,11 @@ import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.DpOffset
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.content.edit
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.navigation.NavHostController
|
||||
import com.bintianqi.owndroid.ui.FunctionItem
|
||||
import com.bintianqi.owndroid.ui.InfoCard
|
||||
import com.bintianqi.owndroid.ui.MyScaffold
|
||||
import com.bintianqi.owndroid.ui.SwitchItem
|
||||
import java.security.SecureRandom
|
||||
@@ -45,7 +47,7 @@ fun Settings(navCtrl: NavHostController) {
|
||||
FunctionItem(R.string.options, "", R.drawable.tune_fill0) { navCtrl.navigate("Options") }
|
||||
FunctionItem(R.string.appearance, "", R.drawable.format_paint_fill0) { navCtrl.navigate("Appearance") }
|
||||
FunctionItem(R.string.security, "", R.drawable.lock_fill0) { navCtrl.navigate("AuthSettings") }
|
||||
FunctionItem(R.string.automation_api, "", R.drawable.apps_fill0) { navCtrl.navigate("Automation") }
|
||||
FunctionItem(R.string.api, "", R.drawable.apps_fill0) { navCtrl.navigate("ApiSettings") }
|
||||
FunctionItem(R.string.about, "", R.drawable.info_fill0) { navCtrl.navigate("About") }
|
||||
}
|
||||
}
|
||||
@@ -154,43 +156,46 @@ fun AuthSettings(navCtrl: NavHostController) {
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun Automation(navCtrl: NavHostController) {
|
||||
fun ApiSettings(navCtrl: NavHostController) {
|
||||
val context = LocalContext.current
|
||||
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
MyScaffold(R.string.automation_api, 8.dp, navCtrl) {
|
||||
Spacer(Modifier.padding(vertical = 5.dp))
|
||||
var key by remember { mutableStateOf("") }
|
||||
OutlinedTextField(
|
||||
value = key, onValueChange = { key = it }, label = { Text("Key")},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
trailingIcon = {
|
||||
IconButton(
|
||||
onClick = {
|
||||
val charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||
val sr = SecureRandom()
|
||||
key = (1..20).map { charset[sr.nextInt(charset.length)] }.joinToString("")
|
||||
}
|
||||
) {
|
||||
Icon(painter = painterResource(R.drawable.casino_fill0), contentDescription = "Random")
|
||||
}
|
||||
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")
|
||||
}
|
||||
)
|
||||
Button(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
onClick = {
|
||||
sharedPref.edit().putString("automation_key", key).apply()
|
||||
Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show()
|
||||
},
|
||||
enabled = key.length >= 20
|
||||
) {
|
||||
Text(stringResource(R.string.apply))
|
||||
}
|
||||
SwitchItem(
|
||||
R.string.automation_debug, "", null,
|
||||
{ sharedPref.getBoolean("automation_debug", false) },
|
||||
{ sharedPref.edit().putBoolean("automation_debug", it).apply() },
|
||||
padding = false
|
||||
)
|
||||
SwitchItem(R.string.enable, "", null, enabled, { enabled = it }, padding = false)
|
||||
if(enabled) {
|
||||
var key by remember { mutableStateOf("") }
|
||||
OutlinedTextField(
|
||||
value = key, onValueChange = { key = it }, label = { Text(stringResource(R.string.api_key)) },
|
||||
modifier = Modifier.fillMaxWidth().padding(bottom = 4.dp), readOnly = true,
|
||||
trailingIcon = {
|
||||
IconButton(
|
||||
onClick = {
|
||||
val charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||
val sr = SecureRandom()
|
||||
key = (1..20).map { charset[sr.nextInt(charset.length)] }.joinToString("")
|
||||
}
|
||||
) {
|
||||
Icon(painter = painterResource(R.drawable.casino_fill0), contentDescription = "Random")
|
||||
}
|
||||
}
|
||||
)
|
||||
Button(
|
||||
modifier = Modifier.fillMaxWidth().padding(bottom = 10.dp),
|
||||
onClick = {
|
||||
sharedPref.edit().putString("api_key", key).apply()
|
||||
Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
) {
|
||||
Text(stringResource(R.string.apply))
|
||||
}
|
||||
if(sharedPref.contains("api_key")) InfoCard(R.string.api_key_exist)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,6 @@ import java.text.SimpleDateFormat
|
||||
import java.time.Instant
|
||||
import java.time.ZoneId
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.format.FormatStyle
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
@@ -143,7 +142,3 @@ fun parseTimestamp(timestamp: Long): String {
|
||||
|
||||
val Long.humanReadableDate: String
|
||||
get() = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault()).format(Date(this))
|
||||
|
||||
val Long.humanReadableTime: String
|
||||
get() = SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(Date(this))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user