auto js
This commit is contained in:
parent
1e7ceae4ed
commit
3c50b9b90c
|
@ -1,11 +1,8 @@
|
|||
package com.android.grape
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.viewModels
|
||||
|
@ -13,23 +10,16 @@ import androidx.appcompat.app.AlertDialog
|
|||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.android.grape.databinding.ActivityMainBinding
|
||||
import com.android.grape.job.MonitorService
|
||||
import com.android.grape.receiver.ScriptReceiver
|
||||
import com.android.grape.util.ClashUtil
|
||||
import com.android.grape.util.HttpUtil
|
||||
import com.android.grape.util.NotificationPermissionHandler
|
||||
import com.android.grape.util.ScriptUtil
|
||||
import com.android.grape.util.StoragePermissionHelper
|
||||
import com.blankj.utilcode.util.LogUtils
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
private val viewModel by viewModels<MainViewModel>()
|
||||
private lateinit var viewBinding: ActivityMainBinding
|
||||
private var intentFilter: IntentFilter? = null
|
||||
private var scriptReceiver: ScriptReceiver? = null
|
||||
private lateinit var permissionHandler: NotificationPermissionHandler
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
@ -42,7 +32,7 @@ class MainActivity : AppCompatActivity() {
|
|||
insets
|
||||
}
|
||||
checkPermission()
|
||||
registerReceiver()
|
||||
ScriptUtil.registerScriptResultReceiver()
|
||||
viewBinding.start.setOnClickListener {
|
||||
try {
|
||||
ClashUtil.startProxy(this)
|
||||
|
@ -66,19 +56,10 @@ class MainActivity : AppCompatActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressLint("UnspecifiedRegisterReceiverFlag")
|
||||
private fun registerReceiver() {
|
||||
intentFilter = IntentFilter().apply {
|
||||
addAction("com.ak.lu.SCRIPT_START")
|
||||
scriptReceiver = ScriptReceiver()
|
||||
registerReceiver(scriptReceiver, this)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
ClashUtil.unregisterReceiver(this)
|
||||
unregisterReceiver(scriptReceiver)
|
||||
ScriptUtil.unregisterScriptResultReceiver()
|
||||
}
|
||||
|
||||
private fun checkPermission() {
|
||||
|
|
|
@ -5,36 +5,24 @@ import android.content.Context
|
|||
import android.content.Intent
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import com.android.grape.job.UnInstallService
|
||||
import com.android.grape.util.ScriptUtil
|
||||
import com.android.grape.util.ScriptUtil.SCRIPT_RESULT_KEY
|
||||
import com.android.grape.util.Util
|
||||
import com.blankj.utilcode.util.LogUtils
|
||||
|
||||
class ScriptReceiver : BroadcastReceiver() {
|
||||
var hook_action: String = "com.android.task.board"
|
||||
var EVENT: String = "event"
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val action = intent.action
|
||||
Log.i(ScriptUtil.TAG, context.packageName + " Receive action:" + action)
|
||||
val event = intent.getIntExtra(ScriptUtil.EVENT, -1)
|
||||
when (event) {
|
||||
ScriptUtil.START -> Log.i(ScriptUtil.TAG, "script start run...")
|
||||
ScriptUtil.STOP -> {
|
||||
Log.i(ScriptUtil.TAG, "proxyme revice script stop!")
|
||||
val hook_intent = Intent(this.hook_action)
|
||||
hook_intent.putExtra(this.EVENT, 0)
|
||||
context.sendBroadcast(hook_intent)
|
||||
LogUtils.i("TAG", context.packageName + " Receive action:" + action)
|
||||
val scriptResult = intent.getStringExtra(SCRIPT_RESULT_KEY)
|
||||
Util.script_status = 0
|
||||
Log.i(
|
||||
LogUtils.i(
|
||||
"IOSTQ:脚本结束",
|
||||
context.packageName + " send broadcast:" + this.hook_action + ":" + this.EVENT
|
||||
"result----> $scriptResult"
|
||||
)
|
||||
Handler(Looper.getMainLooper()).postDelayed(
|
||||
{ UnInstallService.onEvent(context) },
|
||||
3000
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,25 +1,103 @@
|
|||
package com.android.grape.util
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.android.grape.MainApplication
|
||||
import com.android.grape.receiver.ScriptReceiver
|
||||
import com.blankj.utilcode.util.LogUtils
|
||||
|
||||
object ScriptUtil {
|
||||
const val TAG: String = "LU_SCRIPT"
|
||||
const val START: Int = 1
|
||||
const val STOP: Int = 2
|
||||
const val SRC_FILES: String = "sc_file"
|
||||
const val SC_ACTION: String = "com.ak.lu.SCRIPT"
|
||||
const val EVENT: String = "event"
|
||||
const val AUTOJS_SCRIPT_FINISHED_ACTION: String = "org.autojs.SCRIPT_FINISHED"
|
||||
const val SCRIPT_RESULT_KEY: String = "result"
|
||||
var scriptResultReceiver: BroadcastReceiver? = null
|
||||
fun execScript(context: Context, src: String) {
|
||||
if (!isAppInstalled("org.autojs.autojs6")) {
|
||||
runOnUiThread {
|
||||
Toast.makeText(context, "Auto.js app not installed", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
fun execScript(context: Context, src: String?) {
|
||||
val intent = Intent(SC_ACTION)
|
||||
// todo 需要替换为调度app的pkg
|
||||
// intent.setComponent(new ComponentName(TARGET_PKG, ScriptReceiver.class.getName()));
|
||||
intent.putExtra(EVENT, START)
|
||||
intent.putExtra(SRC_FILES, src)
|
||||
context.sendBroadcast(intent)
|
||||
Util.script_status = 0
|
||||
Log.i(TAG, context.packageName + " send broadcast:" + SC_ACTION + ":" + EVENT)
|
||||
val intent = Intent()
|
||||
intent.setClassName(
|
||||
"org.autojs.autojs6",
|
||||
"org.autojs.autojs.external.open.RunIntentActivity"
|
||||
)
|
||||
intent.putExtra("path", src)
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
try {
|
||||
context.startActivity(intent)
|
||||
LogUtils.d("脚本运行中:$src")
|
||||
} catch (e: Exception) {
|
||||
LogUtils.e("AutoJsUtil", "运行脚本失败", e)
|
||||
runOnUiThread {
|
||||
Toast.makeText(context, "运行脚本失败: " + e.message, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun registerScriptResultReceiver() {
|
||||
if (scriptResultReceiver == null) {
|
||||
scriptResultReceiver = ScriptReceiver()
|
||||
try {
|
||||
val filter = IntentFilter(AUTOJS_SCRIPT_FINISHED_ACTION)
|
||||
ContextCompat.registerReceiver(
|
||||
MainApplication.instance,
|
||||
scriptResultReceiver,
|
||||
filter,
|
||||
ContextCompat.RECEIVER_EXPORTED
|
||||
)
|
||||
LogUtils.d("广播接收器成功注册", null)
|
||||
} catch (e: java.lang.Exception) {
|
||||
LogUtils.e("Failed to register receiver", e)
|
||||
scriptResultReceiver = null
|
||||
}
|
||||
} else {
|
||||
LogUtils.d("广播接收器已注册,无需重复注册")
|
||||
}
|
||||
}
|
||||
|
||||
fun unregisterScriptResultReceiver() {
|
||||
if (scriptResultReceiver != null) {
|
||||
try {
|
||||
MainApplication.instance.unregisterReceiver(scriptResultReceiver)
|
||||
LogUtils.d("广播接收器成功注销", null)
|
||||
} catch (e: java.lang.Exception) {
|
||||
LogUtils.e("Failed to unregister receiver", e)
|
||||
}
|
||||
scriptResultReceiver = null
|
||||
} else {
|
||||
LogUtils.d("广播接收器未注册,无需注销")
|
||||
}
|
||||
}
|
||||
|
||||
private fun runOnUiThread(action: Runnable) {
|
||||
Handler(Looper.getMainLooper()).post(action)
|
||||
}
|
||||
|
||||
fun isAppInstalled(packageName: String): Boolean {
|
||||
LogUtils.d("Checking if app is installed: $packageName", null)
|
||||
try {
|
||||
val cmd = "pm list packages | grep $packageName"
|
||||
val result = ShellUtils.execRootCmdAndGetResult(cmd)
|
||||
|
||||
if (result != null && result.contains(packageName)) {
|
||||
LogUtils.d("App is installed: $packageName", null)
|
||||
return true
|
||||
} else {
|
||||
LogUtils.w("App not installed: $packageName", null)
|
||||
return false
|
||||
}
|
||||
} catch (e: java.lang.Exception) {
|
||||
LogUtils.e("Unexpected exception while checking app installation: $packageName", e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue