aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/invalid/lena/scrcpy/Sessions.java
diff options
context:
space:
mode:
authorLena <lena@omega>2026-08-01 00:00:00 +0000
committerLena <lena@omega>2026-08-01 00:00:00 +0000
commit852a8a00273c9128efeed0217a8e5d3fcd8bf780 (patch)
treec72f959731fa3c7c809cf1a0222363b6c9c9b03b /app/src/main/java/invalid/lena/scrcpy/Sessions.java
parentf379b93d52bf0dbd3816ec3f64d165314771d2a6 (diff)
downloadscrcpy-android-852a8a00273c9128efeed0217a8e5d3fcd8bf780.tar.gz
app: harden mirroring lifecycle and state
Diffstat (limited to 'app/src/main/java/invalid/lena/scrcpy/Sessions.java')
-rw-r--r--app/src/main/java/invalid/lena/scrcpy/Sessions.java24
1 files changed, 20 insertions, 4 deletions
diff --git a/app/src/main/java/invalid/lena/scrcpy/Sessions.java b/app/src/main/java/invalid/lena/scrcpy/Sessions.java
index 91144be..fbf19d7 100644
--- a/app/src/main/java/invalid/lena/scrcpy/Sessions.java
+++ b/app/src/main/java/invalid/lena/scrcpy/Sessions.java
@@ -33,12 +33,18 @@ public final class Sessions extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
+ // Mirror passes its target so the notification can lead back to
+ // the session it describes. Restarting the service with a new
+ // target just rebuilds the notification.
+ String host = intent == null ? null : intent.getStringExtra(Mirror.EXTRA_HOST);
+ int port = intent == null ? -1 : intent.getIntExtra(Mirror.EXTRA_PORT, -1);
+
ensureChannel();
Notification n = new Notification.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.notif_session_active))
- .setContentIntent(reopenIntent())
+ .setContentIntent(reopenIntent(host, port))
.setOngoing(true)
.build();
startForeground(NOTIF_ID, n, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
@@ -66,9 +72,19 @@ public final class Sessions extends Service {
nm.createNotificationChannel(ch);
}
- private PendingIntent reopenIntent() {
- Intent i = new Intent(this, Main.class);
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ // Tapping an ongoing "mirroring active" notification must return to
+ // the mirror. It used to point at Main with FLAG_ACTIVITY_CLEAR_TOP,
+ // which finished Mirror on the way - the notification destroyed the
+ // session it was advertising. Mirror is singleTask, so a plain
+ // NEW_TASK launch brings the existing instance forward, and carrying
+ // the target means a launch after the activity died still works.
+ private PendingIntent reopenIntent(String host, int port) {
+ Intent i = new Intent(this, Mirror.class);
+ i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ if (host != null) {
+ i.putExtra(Mirror.EXTRA_HOST, host);
+ i.putExtra(Mirror.EXTRA_PORT, port);
+ }
return PendingIntent.getActivity(this, 0, i,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
}