small fix android server

This commit is contained in:
csf 2022-05-07 18:01:19 +08:00
parent 396b49e955
commit 368f4eb69e
2 changed files with 12 additions and 9 deletions

View File

@ -15,6 +15,7 @@ import android.content.pm.PackageManager
import android.content.res.Configuration import android.content.res.Configuration
import android.graphics.Color import android.graphics.Color
import android.graphics.PixelFormat import android.graphics.PixelFormat
import android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR
import android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC import android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC
import android.hardware.display.VirtualDisplay import android.hardware.display.VirtualDisplay
import android.media.* import android.media.*
@ -199,6 +200,7 @@ class MainService : Service() {
private fun updateScreenInfo() { private fun updateScreenInfo() {
var w: Int var w: Int
var h: Int var h: Int
var dpi: Int
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
@ -206,11 +208,13 @@ class MainService : Service() {
val m = windowManager.maximumWindowMetrics val m = windowManager.maximumWindowMetrics
w = m.bounds.width() w = m.bounds.width()
h = m.bounds.height() h = m.bounds.height()
dpi = resources.configuration.densityDpi
} else { } else {
val dm = DisplayMetrics() val dm = DisplayMetrics()
windowManager.defaultDisplay.getRealMetrics(dm) windowManager.defaultDisplay.getRealMetrics(dm)
w = dm.widthPixels w = dm.widthPixels
h = dm.heightPixels h = dm.heightPixels
dpi = dm.densityDpi
} }
var scale = 1 var scale = 1
@ -219,11 +223,13 @@ class MainService : Service() {
scale = 2 scale = 2
w /= scale w /= scale
h /= scale h /= scale
dpi /= scale
} }
if (SCREEN_INFO.width != w) { if (SCREEN_INFO.width != w) {
SCREEN_INFO.width = w SCREEN_INFO.width = w
SCREEN_INFO.height = h SCREEN_INFO.height = h
SCREEN_INFO.scale = scale SCREEN_INFO.scale = scale
SCREEN_INFO.dpi = dpi
if (isStart) { if (isStart) {
stopCapture() stopCapture()
refreshScreen() refreshScreen()
@ -249,6 +255,7 @@ class MainService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("whichService", "this service:${Thread.currentThread()}") Log.d("whichService", "this service:${Thread.currentThread()}")
super.onStartCommand(intent, flags, startId)
if (intent?.action == INIT_SERVICE) { if (intent?.action == INIT_SERVICE) {
Log.d(logTag, "service starting:${startId}:${Thread.currentThread()}") Log.d(logTag, "service starting:${startId}:${Thread.currentThread()}")
createForegroundNotification() createForegroundNotification()
@ -262,7 +269,6 @@ class MainService : Service() {
_isReady = true _isReady = true
} }
} }
super.onStartCommand(intent, flags, startId)
return START_NOT_STICKY // don't use sticky (auto restart),the new service (from auto restart) will lose control return START_NOT_STICKY // don't use sticky (auto restart),the new service (from auto restart) will lose control
} }
@ -385,7 +391,6 @@ class MainService : Service() {
return isReady return isReady
} }
@SuppressLint("WrongConstant")
private fun startRawVideoRecorder(mp: MediaProjection) { private fun startRawVideoRecorder(mp: MediaProjection) {
Log.d(logTag, "startRawVideoRecorder,screen info:$SCREEN_INFO") Log.d(logTag, "startRawVideoRecorder,screen info:$SCREEN_INFO")
if (surface == null) { if (surface == null) {
@ -393,13 +398,12 @@ class MainService : Service() {
return return
} }
virtualDisplay = mp.createVirtualDisplay( virtualDisplay = mp.createVirtualDisplay(
"RustDesk", "RustDeskVD",
SCREEN_INFO.width, SCREEN_INFO.height, 200, VIRTUAL_DISPLAY_FLAG_PUBLIC, SCREEN_INFO.width, SCREEN_INFO.height, SCREEN_INFO.dpi, VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
surface, null, null surface, null, null
) )
} }
@SuppressLint("WrongConstant")
private fun startVP9VideoRecorder(mp: MediaProjection) { private fun startVP9VideoRecorder(mp: MediaProjection) {
createMediaCodec() createMediaCodec()
videoEncoder?.let { videoEncoder?.let {
@ -411,7 +415,7 @@ class MainService : Service() {
it.start() it.start()
virtualDisplay = mp.createVirtualDisplay( virtualDisplay = mp.createVirtualDisplay(
"RustDeskVD", "RustDeskVD",
SCREEN_INFO.width, SCREEN_INFO.height, 200, VIRTUAL_DISPLAY_FLAG_PUBLIC, SCREEN_INFO.width, SCREEN_INFO.height, SCREEN_INFO.dpi, VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
surface, null, null surface, null, null
) )
} }

View File

@ -18,10 +18,10 @@ import java.util.*
@SuppressLint("ConstantLocale") @SuppressLint("ConstantLocale")
val LOCAL_NAME = Locale.getDefault().toString() val LOCAL_NAME = Locale.getDefault().toString()
val SCREEN_INFO = Info(0, 0) val SCREEN_INFO = Info(0, 0, 1, 200)
data class Info( data class Info(
var width: Int, var height: Int, var scale: Int = 1 var width: Int, var height: Int, var scale: Int, var dpi: Int
) )
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
@ -54,7 +54,6 @@ fun requestPermission(context: Context, type: String) {
.permission(permission) .permission(permission)
.request { permissions, all -> .request { permissions, all ->
if (all) { if (all) {
Log.d("checkPermissions", "获取存储权限成功:$permissions")
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
MainActivity.flutterMethodChannel.invokeMethod( MainActivity.flutterMethodChannel.invokeMethod(
"on_android_permission_result", "on_android_permission_result",