1. sync from flutter and pass app_dir from MainService.kt to backend server when app start on boot.

2. add start_service when start on boot.
This commit is contained in:
csf
2023-03-01 00:05:06 +09:00
parent be2fa3e444
commit f26088765e
8 changed files with 56 additions and 9 deletions

View File

@@ -34,6 +34,7 @@ class BootReceiver : BroadcastReceiver() {
val it = Intent(context, MainService::class.java).apply {
action = ACT_INIT_MEDIA_PROJECTION_AND_SERVICE
putExtra(EXT_INIT_FROM_BOOT, true)
}
Toast.makeText(context, "RustDesk is Open", Toast.LENGTH_LONG).show()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

View File

@@ -15,7 +15,6 @@ import android.os.Build
import android.os.IBinder
import android.util.Log
import android.view.WindowManager
import androidx.annotation.RequiresApi
import com.hjq.permissions.XXPermissions
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
@@ -31,7 +30,6 @@ class MainActivity : FlutterActivity() {
private val logTag = "mMainActivity"
private var mainService: MainService? = null
@RequiresApi(Build.VERSION_CODES.M)
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
if (MainService.isReady) {
@@ -208,6 +206,17 @@ class MainActivity : FlutterActivity() {
result.success(false)
}
}
SYNC_APP_DIR_CONFIG_PATH -> {
if (call.arguments is String) {
val prefs = getSharedPreferences(KEY_SHARED_PREFERENCES, MODE_PRIVATE)
val edit = prefs.edit()
edit.putString(KEY_APP_DIR_CONFIG_PATH, call.arguments as String)
edit.apply()
result.success(true)
} else {
result.success(false)
}
}
else -> {
result.error("-1", "No such method", null)
}

View File

@@ -35,6 +35,7 @@ import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import io.flutter.embedding.android.FlutterActivity
import java.util.concurrent.Executors
import kotlin.concurrent.thread
import org.json.JSONException
@@ -143,7 +144,11 @@ class MainService : Service() {
// jvm call rust
private external fun init(ctx: Context)
private external fun startServer()
/// When app start on boot, app_dir will not be passed from flutter
/// so pass a app_dir here to rust server
private external fun startServer(app_dir: String)
private external fun startService()
private external fun onVideoFrameUpdate(buf: ByteBuffer)
private external fun onAudioFrameUpdate(buf: ByteBuffer)
private external fun translateLocale(localeName: String, input: String): String
@@ -199,7 +204,12 @@ class MainService : Service() {
}
updateScreenInfo(resources.configuration.orientation)
initNotification()
startServer()
// keep the config dir same with flutter
val prefs = applicationContext.getSharedPreferences(KEY_SHARED_PREFERENCES, FlutterActivity.MODE_PRIVATE)
val configPath = prefs.getString(KEY_APP_DIR_CONFIG_PATH, "") ?: ""
startServer(configPath)
createForegroundNotification()
}
@@ -279,6 +289,10 @@ class MainService : Service() {
super.onStartCommand(intent, flags, startId)
if (intent?.action == ACT_INIT_MEDIA_PROJECTION_AND_SERVICE) {
createForegroundNotification()
if (intent.getBooleanExtra(EXT_INIT_FROM_BOOT, false)) {
startService()
}
Log.d(logTag, "service starting: ${startId}:${Thread.currentThread()}")
val mediaProjectionManager =
getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager

View File

@@ -27,6 +27,7 @@ import java.util.*
const val ACT_REQUEST_MEDIA_PROJECTION = "REQUEST_MEDIA_PROJECTION"
const val ACT_INIT_MEDIA_PROJECTION_AND_SERVICE = "INIT_MEDIA_PROJECTION_AND_SERVICE"
const val ACT_LOGIN_REQ_NOTIFY = "LOGIN_REQ_NOTIFY"
const val EXT_INIT_FROM_BOOT = "EXT_INIT_FROM_BOOT"
const val EXT_MEDIA_PROJECTION_RES_INTENT = "MEDIA_PROJECTION_RES_INTENT"
const val EXT_LOGIN_REQ_NOTIFY = "LOGIN_REQ_NOTIFY"
@@ -41,9 +42,11 @@ const val RES_FAILED = -100
const val START_ACTION = "start_action"
const val GET_START_ON_BOOT_OPT = "get_start_on_boot_opt"
const val SET_START_ON_BOOT_OPT = "set_start_on_boot_opt"
const val SYNC_APP_DIR_CONFIG_PATH = "sync_app_dir"
const val KEY_SHARED_PREFERENCES = "KEY_SHARED_PREFERENCES"
const val KEY_START_ON_BOOT_OPT = "KEY_START_ON_BOOT_OPT"
const val KEY_APP_DIR_CONFIG_PATH = "KEY_APP_DIR_CONFIG_PATH"
@SuppressLint("ConstantLocale")
val LOCAL_NAME = Locale.getDefault().toString()