Skip to content

Commit 80c9f5e

Browse files
committed
Fix screen mirror issue
1 parent 9f37a4a commit 80c9f5e

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ android {
4141
else -> 0
4242
}
4343

44-
val vCode = 415
44+
val vCode = 418
4545
versionCode = vCode - singleAbiNum
46-
versionName = "2.1.11"
46+
versionName = "2.1.12"
4747

4848
ndk {
4949
//noinspection ChromeOsAbiSupport

app/src/main/java/com/ismartcoding/plain/services/ScreenMirrorService.kt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class ScreenMirrorService : LifecycleService() {
4141
private var mBitmap: Bitmap? = null
4242
private lateinit var orientationEventListener: OrientationEventListener
4343
private var isPortrait = true
44+
private var notificationId: Int = 0
4445

4546
private var mMediaProjection: MediaProjection? = null
4647
private var mImageReaderPortrait: ImageReader? = null
@@ -74,14 +75,6 @@ class ScreenMirrorService : LifecycleService() {
7475
}
7576
}
7677
}
77-
val notification =
78-
NotificationHelper.createServiceNotification(
79-
this,
80-
"${BuildConfig.APPLICATION_ID}.action.stop_screen_mirror",
81-
getString(R.string.screen_mirror_service_is_running),
82-
)
83-
val id = NotificationHelper.generateId()
84-
ServiceCompat.startForeground(this, id, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION)
8578
}
8679

8780
@SuppressLint("WrongConstant")
@@ -96,6 +89,22 @@ class ScreenMirrorService : LifecycleService() {
9689
mResultData = intent.parcelable("data")
9790
}
9891

92+
// Acquire MediaProjection before starting FGS with mediaProjection type to satisfy Android 14/15
93+
if (mResultCode != -1 && mResultData != null && mMediaProjection == null) {
94+
mMediaProjection = mediaProjectionManager.getMediaProjection(mResultCode, mResultData!!)
95+
}
96+
97+
if (notificationId == 0) {
98+
notificationId = NotificationHelper.generateId()
99+
}
100+
val notification =
101+
NotificationHelper.createServiceNotification(
102+
this,
103+
"${BuildConfig.APPLICATION_ID}.action.stop_screen_mirror",
104+
getString(R.string.screen_mirror_service_is_running),
105+
)
106+
ServiceCompat.startForeground(this, notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION)
107+
99108
mImageReaderHandlerThread = HandlerThread("ImageReader")
100109
mImageReaderHandlerThread?.start()
101110
handler = Handler(mImageReaderHandlerThread!!.looper)
@@ -172,7 +181,9 @@ class ScreenMirrorService : LifecycleService() {
172181
}
173182

174183
private fun doMirror() {
175-
mMediaProjection = mediaProjectionManager.getMediaProjection(mResultCode, mResultData!!)
184+
if (mMediaProjection == null) {
185+
mMediaProjection = mediaProjectionManager.getMediaProjection(mResultCode, mResultData!!)
186+
}
176187
val width =
177188
if (isPortrait) {
178189
widthPortrait

app/src/main/java/com/ismartcoding/plain/ui/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class MainActivity : AppCompatActivity() {
131131
val service = Intent(this, ScreenMirrorService::class.java)
132132
service.putExtra("code", result.resultCode)
133133
service.putExtra("data", result.data)
134-
startService(service)
134+
ContextCompat.startForegroundService(this, service)
135135
} else {
136136
sendEvent(WebSocketEvent(EventType.SCREEN_MIRRORING, image))
137137
}

0 commit comments

Comments
 (0)