@simba-dev/react-native-media-player 3.0.5 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,4 +1,152 @@
1
1
 
2
+ ## [3.2.0] - 2026-11-03
3
+
4
+ ### Fixed
5
+
6
+ - **Pressing HOME opened no Picture-in-Picture window at all.** The BUTTON path worked and was
7
+ proven on emulator-5554 (API 37): the app unmounted its chrome in React, waited one animation
8
+ frame, and then asked for the window. The HOME path did not — pressing HOME opened no PiP
9
+ window, and on return the screen was black and chromeless with the consumer's store stuck at
10
+ `pipActive=true` / `pipEntryRequested=true`.
11
+
12
+ The cause was measured, not guessed. `Activity.onUserLeaveHint()` fired and the JS handler ran
13
+ immediately, but the effect that would have driven the entry ran **5.7 s late** and the
14
+ `requestAnimationFrame` **never fired**. React Native pauses the React scheduler with the
15
+ Activity, so a React commit cannot land and there is no frame clock once the Activity is
16
+ backgrounded. The entry therefore has to be **native**, inside `onUserLeaveHint()`, before
17
+ `onPause`.
18
+
19
+ The library now performs the HOME-path entry itself:
20
+
21
+ - the consumer's chrome is hidden with a **native View-system mutation**
22
+ (`visibility = View.GONE`), which lands without a React commit and without a frame clock;
23
+ - the PiP parameters are built **after** the hide, by the same builder the JS path uses;
24
+ - the chrome is restored when Picture-in-Picture ends, and immediately if the OS refuses the
25
+ entry.
26
+
27
+ **Action required:** your Activity has to hand over the Activity.
28
+
29
+ ```kotlin
30
+ // android/app/src/main/java/.../MainActivity.kt
31
+ override fun onUserLeaveHint() {
32
+ super.onUserLeaveHint()
33
+ com.simba.player.mpv.MpvBridgeModule.notifyUserLeaveHint(this)
34
+ }
35
+ ```
36
+
37
+ If your app draws its own chrome over the picture, give that view a `nativeID` and name it in
38
+ the config — the library hides that view, and nothing else:
39
+
40
+ ```tsx
41
+ // the chrome root
42
+ <View nativeID="player-chrome" style={styles.chrome}>{/* your controls */}</View>
43
+ ```
44
+
45
+ ```tsx
46
+ <PlayerProvider
47
+ config={{pip: {enabled: true, autoEnterOnLeave: true, chromeNativeId: 'player-chrome'}}}>
48
+ ```
49
+
50
+ The `nativeID` literal belongs to the consumer; the library never hardcodes one of its own.
51
+ Omit `chromeNativeId` if your app draws no chrome of its own — the library then enters
52
+ Picture-in-Picture without hiding anything. If the id is set and **no view carries it**, the
53
+ library **refuses** the entry and logs a warning naming the id: entering PiP with the chrome
54
+ still mounted paints the app's dead chrome into the PiP window, which is the defect this
55
+ release exists to fix.
56
+
57
+ ### Removed
58
+
59
+ - **The `onUserLeaveHint` JS event.** The library now performs the HOME-path Picture-in-Picture
60
+ entry itself, so the event's only consumer — a consumer that drove the entry from JavaScript —
61
+ is the path the library took over. Leaving it would be a double-entry footgun: the OS would
62
+ refuse the second entry, the consumer would read that refusal as "PiP failed", and the chrome
63
+ would come back over a live PiP window.
64
+
65
+ This is a **public API removal**, which is why the release is 3.2.0 and not a patch. The bridge
66
+ now emits **26** named events; the list has one owner, `ALL_PLAYER_EVENTS` in
67
+ `src/bridge/MpvPlayerModule.ts`.
68
+
69
+ ### Changed
70
+
71
+ - `MpvBridgeModule.notifyUserLeaveHint()` is now
72
+ `notifyUserLeaveHint(activity: Activity): Boolean`. It takes the Activity — the entry happens
73
+ on it, and the chrome is searched under its window — and returns the OS's own answer:
74
+ `true` when the Activity entered Picture-in-Picture.
75
+ - **The PiP parameters have ONE owner.** The JS path (`enterPip`) and the native path
76
+ (`notifyUserLeaveHint`) build them through the same function, so the two cannot drift apart.
77
+ - The native path reports the title from mpv's `media-title` — which IS the title the consumer
78
+ supplied, because the library writes it as mpv's `force-media-title` before the load — and the
79
+ percentage from `time-pos` over `duration`, in the consumer's exact `"NN %"` format.
80
+
81
+ ### Documentation
82
+
83
+ - Five places in this library still carried the false claim that "an Activity in
84
+ Picture-in-Picture does not redraw". The accurate statement is that an Activity in PiP is
85
+ **"paused, but rendering"** (AOSP) — the window keeps rendering; what stops is **REACT**,
86
+ because React Native pauses the React scheduler with the Activity. The ORDER still matters — the
87
+ PiP window keeps whatever the Activity's window held at the transition — but the REASON was
88
+ wrong, and a wrong reason sends the next reader looking for the fix in the wrong place.
89
+ Corrected in `MpvBridgeModule.kt`, `CHANGELOG.md`, `README.md`, `MpvPlayerModule.ts` and
90
+ `config.ts`.
91
+
92
+ ### Added
93
+
94
+ - `PipConfig.chromeNativeId` — the `nativeID` of the consumer's chrome root, the view the
95
+ library hides natively on the HOME path.
96
+ - `PipNativeEntryTest` — 24 guard tests for the native HOME-path entry: the three refusals and
97
+ the order they come in, the chrome hidden with `GONE` **before** the PiP request, a refused
98
+ entry restoring the chrome, Picture-in-Picture ending restoring the chrome, the title read from
99
+ mpv's `media-title`, the percentage read from `time-pos`/`duration` in the consumer's exact
100
+ `"NN %"` format, one params builder for both paths, and the retired event's absence from the
101
+ event list.
102
+
103
+ ## [3.1.0] - 2026-11-03
104
+
105
+ ### Fixed
106
+
107
+ - **The Picture-in-Picture action row did nothing.** The three platform buttons — Play/Pause,
108
+ Expand and Close — rendered at the correct size, right beside the platform's own settings /
109
+ expand / close buttons, and pressing ours produced no broadcast at all. The action's
110
+ `PendingIntent` carried an intent that named `PipActionReceiver` explicitly, and an
111
+ explicit broadcast is resolved through the **manifest**; the receiver was a runtime
112
+ registration, so the broadcast resolved to nothing and was dropped before it was ever
113
+ dispatched. Measured on emulator-5554 (API 37) on 2026-11-03: the same action sent
114
+ explicitly was never dispatched (`dispatchTime=--`, in the foreground and in PiP alike),
115
+ while the same action sent implicitly was dispatched in 0 ms. The action intent is now
116
+ implicit, which is what the runtime registration matches on.
117
+ - **A failure to deliver a PiP action was invisible.** `PipActionReceiver` swallowed the
118
+ exception silently — which is how a row of dead buttons could leave not one line in the
119
+ log. The failure is now logged, under the tag `PipActionReceiver`.
120
+
121
+ ### Changed
122
+
123
+ - **The library owns the PiP action receiver.** It is registered at runtime, once per
124
+ process, on the application context, at the moment the Picture-in-Picture parameters are
125
+ built — so the platform can never be handed a set of buttons that nothing is listening
126
+ for. Consumers no longer register it, and no longer need to know the class exists.
127
+
128
+ **Action required:** if you registered `PipActionReceiver` yourself, remove that
129
+ registration. Two registrations deliver every action twice, and a play/pause that toggles
130
+ twice leaves playback exactly where it was — the same dead-button symptom with a
131
+ different cause. `PipManager.intentFilter()` is now `internal`, so the compiler points
132
+ at the registration instead of letting it reach a runtime double-delivery bug.
133
+
134
+ ### Documentation
135
+
136
+ - The README described the **removed** mechanism: Android's own auto-enter switch, an API 31+
137
+ floor for auto-enter, a PiP action receiver "declared in the manifest", and 26 named
138
+ events where the bridge had 27. It also pointed at a section for "the two lines of Kotlin and
139
+ the JS that goes with them" that did not contain them. All corrected; the auto-enter
140
+ section now carries the Kotlin and the JS a consumer needs, and explains why the consumer
141
+ performs the entry.
142
+
143
+ ### Added
144
+
145
+ - `PipActionDeliveryTest` — eight guard tests pinning the contract above: the intent stays
146
+ implicit, the library registers the receiver, the registration cannot happen twice, the
147
+ consumer cannot make it happen twice, a delivery failure is logged, and the manifest
148
+ neither declares the receiver nor duplicates the action strings.
149
+
2
150
  ## [3.0.5] - 2026-11-03
3
151
 
4
152
  ### Changed
@@ -8,9 +156,11 @@
8
156
  `PictureInPictureParams.Builder.setAutoEnterEnabled` (API 31+), which makes
9
157
  the SYSTEM transition the activity into PiP when the user leaves the app,
10
158
  with the consumer unable to do anything first. That is the wrong order for a
11
- consumer that draws its own chrome over the picture, because **an Activity in
12
- PiP does not redraw**: whatever its window held at the transition is what
13
- the PiP window keeps showing. Measured on device on 2026-11-03, the
159
+ consumer that draws its own chrome over the picture, because an Activity in
160
+ PiP is "paused, but rendering" (AOSP) - what stops is REACT: RN pauses the
161
+ React scheduler with the Activity, so a React commit cannot land and no frame
162
+ is produced. So whatever its window held at the transition is what the PiP
163
+ window keeps showing. Measured on device on 2026-11-03, the
14
164
  consumer's full expanded chrome was painted into the PiP window, its
15
165
  buttons were dead, and its clock was frozen three minutes behind the real
16
166
  position.
@@ -23,13 +173,18 @@
23
173
  to the `onUserLeaveHint` event in JS; both are in the README's
24
174
  Picture-in-Picture section.
25
175
 
176
+ (History: that event is **retired in 3.2.0**, and the entry itself moved
177
+ into the library — it is performed natively, inside `onUserLeaveHint()`,
178
+ because by then no React commit can land. See the 3.2.0 entry above.)
179
+
26
180
  A side effect worth knowing: auto-enter now works on **every** API level.
27
181
  The platform's switch never existed below Android 12; `onUserLeaveHint()`
28
182
  does.
29
183
 
30
- - **`onUserLeaveHint` is the 27th player event.** It is emitted with an empty
31
- payload, and only when the auto-enter policy says yes — a consumer that
32
- turned auto-enter off is never asked to enter PiP.
184
+ - **`onUserLeaveHint` was the 27th player event.** It was emitted with an
185
+ empty payload, and only when the auto-enter policy says yes — a consumer that
186
+ turned auto-enter off was never asked to enter PiP. (The event was removed in
187
+ 3.2.0; the bridge emits 26 events now.)
33
188
 
34
189
  - **The bridge no longer stashes PiP params on the Activity.**
35
190
  `Activity.setPictureInPictureParams` and the helper that pushed them
package/README.md CHANGED
@@ -62,7 +62,8 @@ video, and the two lanes meet in the same bridge:
62
62
  - **MediaSession and foreground service.** Title, artist and album reach the lock screen and the
63
63
  media notification; play, pause, stop, next, previous and seek are advertised to the session, so
64
64
  headset buttons and Bluetooth controls work.
65
- - **Picture-in-Picture.** Manual entry on demand, and OS-level auto-enter on leave from Android 12.
65
+ - **Picture-in-Picture.** Manual entry on demand, and auto-enter on leave on every Android
66
+ version - your app performs the entry itself, after taking its own chrome off screen.
66
67
  - **Typed surface.** Config, state, commands and hooks are all typed.
67
68
 
68
69
  ## What this is not
@@ -112,8 +113,13 @@ so re-running `npm install` is safe. Set `SKIP_DOWNLOAD_NATIVE=1` to skip the do
112
113
 
113
114
  ## Consumer setup
114
115
 
115
- The package's own manifest declares a foreground service and a PiP action receiver. It declares no
116
- Activity: playback happens in the Activity your app already has.
116
+ The package's own manifest declares a foreground service and the media button receiver. It
117
+ declares no Activity: playback happens in the Activity your app already has.
118
+
119
+ The Picture-in-Picture action receiver is registered by this package at runtime, once per
120
+ process. You do not register it, and you must not: a second registration would deliver every
121
+ PiP action twice, and a play/pause that toggles twice does nothing at all.
122
+
117
123
 
118
124
  ### Picture-in-Picture
119
125
 
@@ -139,9 +145,9 @@ Your launcher Activity has to opt in:
139
145
  `resizeableActivity="true"` is not optional. An Activity that is not resizeable cannot enter
140
146
  Picture-in-Picture.
141
147
 
142
- If `config.pip.autoEnterOnLeave` is on, the Activity also has to forward the platform’s leave
143
- hint to the library — see [Picture-in-Picture](#picture-in-picture) for the two lines of Kotlin
144
- and the JS that goes with them.
148
+ If `config.pip.autoEnterOnLeave` is on, the Activity also has to hand the leave hint to the
149
+ library, which performs the Picture-in-Picture entry itself - see
150
+ [Picture-in-Picture](#picture-in-picture) for the two lines of Kotlin that go with it.
145
151
 
146
152
  ### Background audio
147
153
 
@@ -308,7 +314,7 @@ there is no library-owned overlay; the absolutely positioned `View` above is you
308
314
  | Section | Fields | Default |
309
315
  | --- | --- | --- |
310
316
  | `theme` | `accent`, `background`, `text`, `textSecondary`, `surface`, `icon?` | `#FFD700`, `#121216`, `#FFFFFF`, `rgba(255,255,255,0.6)`, `rgba(255,255,255,0.1)` |
311
- | `pip` | `enabled`, `autoEnterOnLeave` | `true`, `true` |
317
+ | `pip` | `enabled`, `autoEnterOnLeave`, `chromeNativeId?` | `true`, `true`, none |
312
318
  | `hardwareDecoding` | `'auto' \| 'mediacodec' \| 'no'` | `'auto'` |
313
319
  | `notifications` | `enabled`, `channelId` | `true`, `'simba_player_media'` |
314
320
  | `audio` | `backgroundPlayback`, `respectAudioFocus` | `true`, `true` |
@@ -330,8 +336,11 @@ import {PlayerProvider} from '@simba-dev/react-native-media-player';
330
336
  ```
331
337
 
332
338
  The resolved config is pushed to the native bridge as JSON. The native side acts on the PiP
333
- section (it drives the auto-enter switch); the remaining sections are values your app reads back
334
- with `usePlayerConfig()` and applies itself. `DEFAULT_PLAYER_CONFIG` and `DEFAULT_THEME` are
339
+ section (the auto-enter policy, and the `nativeID` of your chrome root); the remaining sections
340
+ are values your app reads back with `usePlayerConfig()` and applies itself.
341
+ `DEFAULT_PLAYER_CONFIG` and `DEFAULT_THEME` are exported if you would rather spread and patch.
342
+
343
+
335
344
  exported if you would rather spread and patch.
336
345
 
337
346
  ## Picture-in-Picture
@@ -347,15 +356,89 @@ commands.exitPipAndFinish();
347
356
  `enterPip()` calls `enterPictureInPictureMode()` on the current Activity, so the Activity has to
348
357
  declare `android:supportsPictureInPicture="true"` (see [Consumer setup](#consumer-setup)).
349
358
 
350
- **Auto-enter on leave** is `config.pip.autoEnterOnLeave` (default `true`). It maps onto the
351
- Android 12 / API 31 platform feature `PictureInPictureParams.Builder.setAutoEnterEnabled`: the
352
- OS enters Picture-in-Picture for you when the user leaves the app. **Below API 31 the platform
353
- has no such feature** — the OS cannot auto-enter Picture-in-Picture there, and entry stays manual
354
- through `commands.enterPip()`.
359
+ **Auto-enter on leave** is `config.pip.autoEnterOnLeave` (default `true`): it means "enter
360
+ Picture-in-Picture when the user leaves the app".
361
+
362
+ **The library performs that entry, natively.** Two lines of Kotlin in your Activity:
363
+
364
+ ```kotlin
365
+ // android/app/src/main/java/.../MainActivity.kt
366
+ override fun onUserLeaveHint() {
367
+ super.onUserLeaveHint()
368
+ com.simba.player.mpv.MpvBridgeModule.notifyUserLeaveHint(this)
369
+ }
370
+ ```
371
+
372
+ The library then hides your chrome, asks the OS for the window, and brings your chrome back when
373
+ Picture-in-Picture ends. It returns whether the OS accepted the entry, so you can log it:
374
+
375
+ ```kotlin
376
+ val entered = com.simba.player.mpv.MpvBridgeModule.notifyUserLeaveHint(this)
377
+ ```
378
+
379
+ ### Why it is native, and why your Activity hands over the Activity
380
+
381
+ `Activity.onUserLeaveHint()` is the only hook Android gives you BEFORE the Activity is paused.
382
+ `onPictureInPictureModeChanged(true)` arrives after `onPause`, which is too late to change what
383
+ the window holds.
384
+
385
+ And by then, React Native has already paused the React scheduler with the Activity, so a React
386
+ commit cannot land and there is no frame clock. Measured on emulator-5554 (API 37) on 2026-11-03:
387
+ the JS handler ran immediately, the effect that would have driven the entry ran **5.7 s late**, and
388
+ the `requestAnimationFrame` **never fired**. So the entry has to happen in native code, and the
389
+ chrome has to be hidden with a native View-system mutation (`visibility = View.GONE`), which lands
390
+ without a React commit and without a frame clock.
391
+
392
+ An Activity in Picture-in-Picture is "paused, but rendering" (AOSP) - the window keeps rendering;
393
+ what stops is REACT. So the PiP window keeps whatever your window held at the moment of the
394
+ transition: the chrome has to be GONE before the entry.
395
+
396
+ ### Telling the library which view is your chrome
397
+
398
+ If your app draws its own chrome over the picture, give that view a `nativeID` and tell the library
399
+ about it:
400
+
401
+ ```tsx
402
+ // the consumer's JS - the chrome root
403
+ <View nativeID="player-chrome" style={styles.chrome}>
404
+ {/* your controls */}
405
+ </View>
406
+ ```
407
+
408
+ ```tsx
409
+ // the consumer's config
410
+ <PlayerProvider
411
+ config={{
412
+ pip: {enabled: true, autoEnterOnLeave: true, chromeNativeId: 'player-chrome'},
413
+ }}>
414
+ <App />
415
+ </PlayerProvider>
416
+ ```
417
+
418
+ The library finds that view with React Native's own lookup (`ReactFindViewUtil.findView`), under
419
+ your Activity's window decor view, and hides it natively before it asks for the window. The
420
+ `nativeID` literal is yours - the library never hardcodes one of its own.
421
+
422
+ Omit `chromeNativeId` if your app draws no chrome of its own: the library then enters
423
+ Picture-in-Picture without hiding anything.
424
+
425
+ **If the `nativeID` is set and no view carries it, the library REFUSES the entry** and logs a
426
+ warning naming the id. Entering Picture-in-Picture with the chrome still mounted paints your dead
427
+ controls into the PiP window, scaled down and dead - that is the defect this path exists to fix, so
428
+ the library declines rather than reproduce it.
429
+
430
+ The library deliberately does NOT arm Android's own auto-enter switch
431
+ (`PictureInPictureParams.Builder.setAutoEnterEnabled`): that makes the OS transition the Activity
432
+ with no chance to remove your chrome, and the chrome it froze into the PiP window on 2026-11-03 was
433
+ scaled down and dead.
434
+
435
+ This works on **every** Android version - `onUserLeaveHint()` exists everywhere, and the platform's
436
+ own switch never existed below Android 12.
355
437
 
356
438
  The two PiP flags combine: auto-enter needs `pip.enabled` and `pip.autoEnterOnLeave` to both be
357
439
  true.
358
440
 
441
+
359
442
  The Picture-in-Picture action row has three buttons: **Play/Pause**, **Expand** and **Close**.
360
443
  They arrive in JS as the `onPipPlayPause`, `onPipExpand` and `onPipClose` events, next to
361
444
  `onPipModeChanged`.
@@ -458,8 +541,18 @@ unsubscribe function; `removeAllListeners()`, `setDebugLogging()` and
458
541
  before launching, and check `commands.isSurfaceMounted()`.
459
542
 
460
543
  **Picture-in-Picture does not start.** The Activity is missing
461
- `android:supportsPictureInPicture="true"` or `android:resizeableActivity="true"`. Auto-enter on
462
- leave additionally needs API 31+ — below that, enter PiP yourself with `commands.enterPip()`.
544
+ `android:supportsPictureInPicture="true"` or `android:resizeableActivity="true"`. If auto-enter
545
+ on leave does nothing, the Activity is not calling `MpvBridgeModule.notifyUserLeaveHint(this)`
546
+ from `onUserLeaveHint()` - see [Picture-in-Picture](#picture-in-picture). If the log says the
547
+ library refused the entry because no view carries your chrome `nativeID`, the `nativeID` in your
548
+ config does not match the one on the view.
549
+
550
+ **The Picture-in-Picture action row shows three buttons and none of them work.** That was the
551
+ explicit-broadcast bug, fixed in 3.1.0: the action's PendingIntent named the receiver's
552
+ component, and an explicit broadcast is resolved through the manifest, where the receiver was
553
+ never declared. Check that you have not registered a receiver for the PiP actions yourself -
554
+ this package registers one, and a second registration delivers every action twice.
555
+
463
556
 
464
557
  **Gradle fails with `libmpv.so not found at android/src/main/jniLibs/…`.** The postinstall
465
558
  download did not run. Re-run `npm install` in the app, or run the script by hand; `SKIP_DOWNLOAD_NATIVE=1`
@@ -477,11 +570,13 @@ Call `commands.requestNotificationPermission()`.
477
570
 
478
571
  - **Android only.** There is no iOS implementation.
479
572
  - **No controls.** The library ships none; see [Controls](#controls).
480
- - **Auto-enter Picture-in-Picture needs Android 12 (API 31) or newer.** Below that the OS cannot
481
- do it and entry is manual.
573
+ - **Auto-enter on leave is performed by the library, from your Activity.** Your Activity hands
574
+ over the leave hint and the library enters Picture-in-Picture natively, because by then no
575
+ React commit can land. Your app's own job is the `nativeID` of the chrome the library hides.
482
576
  - **Most config sections are consumed in JavaScript.** The native side receives the resolved
483
- config as JSON and acts on the PiP auto-enter switch; the rest are values your app reads back
577
+ config as JSON and acts on the PiP auto-enter policy; the rest are values your app reads back
484
578
  through `usePlayerConfig()`.
579
+ - **`notifications.channelId` is not read natively.**
485
580
  - **`notifications.channelId` is not read natively.** The foreground service creates its own
486
581
  notification channel, `simba_player_media_playback`.
487
582
  - **The bundled native binaries carry their own licence.** They are built by the mpv-android
@@ -24,14 +24,25 @@ import com.facebook.react.bridge.ReactContext
24
24
  * - Notification text with chapter title + progress percentage (Android 12+)
25
25
  * - No NEXT/PREV actions (removed for simplicity)
26
26
  *
27
- * V12 Phase 9: relocated from the consumer app to the module so
28
- * `PlayerActivity` can call `buildPipParams(...)` directly without
29
- * crossing the Gradle module boundary. The consumer app's
30
- * `MainActivity` continues to register / unregister [PipActionReceiver]
31
- * (since `MainActivity` is the activity that owns the foreground
32
- * during in-app video browsing — PiP can be entered from there too).
33
- * Both activities share the same `com.simba.player.PipManager` FQN,
34
- * which Gradle resolves to the module's copy at app build time.
27
+ * ## Who owns the action receiver
28
+ *
29
+ * THIS LIBRARY owns it, end to end: [registerActionReceiver] registers
30
+ * [PipActionReceiver] on the application context, once per process. The
31
+ * consumer app does NOT register it and does not need to know the class
32
+ * exists.
33
+ *
34
+ * That ownership is not a preference — it is what makes the buttons
35
+ * work. See [buildRemoteAction] for the measured reason the broadcast
36
+ * has to be IMPLICIT, and [registerActionReceiver] for why there must be
37
+ * exactly ONE registration (two would deliver every action twice, and a
38
+ * play/pause that toggles twice leaves playback exactly where it was —
39
+ * buttons that look alive and do nothing).
40
+ *
41
+ * V12 Phase 9 relocated this object from the consumer app to the module
42
+ * so the consumer's Activity can call `buildPipParams(...)` without
43
+ * crossing the Gradle module boundary. Both sides share the same
44
+ * `com.simba.player.PipManager` FQN, which Gradle resolves to the
45
+ * module's copy at app build time.
35
46
  */
36
47
  @RequiresApi(Build.VERSION_CODES.N)
37
48
  object PipManager {
@@ -69,6 +80,17 @@ object PipManager {
69
80
  chapterTitle: String? = null,
70
81
  progressPercentage: String? = null,
71
82
  ): PictureInPictureParams {
83
+ // The params carry the three RemoteActions, and the platform's PiP
84
+ // action row is the ONLY way they can ever be fired. So the
85
+ // receiver that serves them has to exist before these params do.
86
+ //
87
+ // Registering here — at the single place the params are built —
88
+ // makes it impossible to hand the platform a set of buttons that
89
+ // nothing is listening for. That was the measured defect on
90
+ // 2026-11-03: the row rendered three buttons at the right size
91
+ // and none of them did anything.
92
+ registerActionReceiver(context)
93
+
72
94
  val actions = mutableListOf<RemoteAction>()
73
95
 
74
96
  // Play/Pause
@@ -137,10 +159,16 @@ object PipManager {
137
159
  // The platform's auto-enter switch makes the SYSTEM transition the
138
160
  // activity into PiP when the user leaves the app, without the app
139
161
  // being able to do anything first. That is the wrong order for a
140
- // consumer that draws its own chrome over the picture: an Activity
141
- // in PiP does not redraw, so whatever the window held at the
142
- // moment of the transition is what the PiP window keeps showing -
143
- // frozen, dead and scaled down.
162
+ // consumer that draws its own chrome over the picture.
163
+ //
164
+ // The reason is NOT that the window freezes - an Activity in PiP is
165
+ // "paused, but rendering" (source.android.com/docs/core/display/
166
+ // pip). What stops is REACT: RN pauses the React scheduler when
167
+ // the Activity is paused, so a React commit cannot land and no
168
+ // frame is produced. The switch transitions the Activity before
169
+ // the consumer can unmount its chrome, so the chrome is still
170
+ // mounted when the PiP window is created - and with no frame
171
+ // coming, it stays there: dead and scaled down.
144
172
  //
145
173
  // Measured on device on 2026-11-03: the consumer's full expanded
146
174
  // chrome (header band, scrub row, +-10 buttons, the big pause
@@ -150,15 +178,16 @@ object PipManager {
150
178
  // own state already said `pipActive: true` and `isExpanded:
151
179
  // false`.
152
180
  //
153
- // So auto-enter on leave is the CONSUMER's to perform, in the only
154
- // order that works: remove the chrome, let the frame that drew it
155
- // go by, and only then ask for the window. The library's part is
156
- // to tell the consumer WHEN the user is leaving - the
157
- // `onUserLeaveHint` event - and to keep the params it hands the
158
- // platform free of the platform's own auto-enter switch.
181
+ // So auto-enter on leave is performed by the LIBRARY, natively, in
182
+ // the only order that works: hide the chrome, then ask for the
183
+ // window. The library's part is to hide the consumer's chrome
184
+ // natively (a View-system mutation, so it lands without a React
185
+ // commit) and to keep the params it hands the platform free of
186
+ // the platform's own auto-enter switch.
159
187
  //
160
- // The consumer's Activity overrides `onUserLeaveHint()`, which
161
- // works on every API level, unlike the platform switch (API 31+).
188
+ // The consumer's Activity overrides `onUserLeaveHint()` and calls
189
+ // `MpvBridgeModule.notifyUserLeaveHint(activity)`, which works on
190
+ // every API level, unlike the platform switch (API 31+).
162
191
 
163
192
  // Source rect hint for smooth entry animation (Android 12+)
164
193
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && sourceRectHint != null) {
@@ -184,9 +213,36 @@ object PipManager {
184
213
  action: String,
185
214
  requestCode: Int,
186
215
  ): RemoteAction {
187
- val intent = Intent(action).apply {
188
- setClass(context, PipActionReceiver::class.java)
189
- }
216
+ // The intent is IMPLICIT — it carries the action and NOTHING else.
217
+ //
218
+ // It used to name the component explicitly
219
+ // (`setClass(context, PipActionReceiver::class.java)`). THAT was
220
+ // the reason the PiP buttons did NOTHING, measured on
221
+ // emulator-5554 (API 37) on 2026-11-03:
222
+ //
223
+ // * The platform's PiP action row rendered at the correct size,
224
+ // right beside the platform's own settings / expand / close
225
+ // buttons — and all three of THOSE worked.
226
+ // * Tapping ours produced NO broadcast at all:
227
+ // `dumpsys activity broadcasts` held no record of the action,
228
+ // and the platform's own gear button opened the PiP settings
229
+ // screen in the very same gesture.
230
+ //
231
+ // The cause is how the framework resolves an EXPLICIT broadcast:
232
+ // it looks the component up through the MANIFEST.
233
+ // `PipActionReceiver` is not declared there — it is a runtime
234
+ // registration — so the explicit broadcast resolved to no
235
+ // receiver and was dropped before it was ever dispatched
236
+ // (`dumpsys` showed `dispatchTime=--`, foreground and background
237
+ // alike).
238
+ //
239
+ // The same action sent IMPLICITLY was dispatched in 0 ms, and
240
+ // that is the proof: the receiver was registered and matching the
241
+ // whole time. Only the component was the problem.
242
+ //
243
+ // An implicit intent is matched by ACTION, which is exactly what
244
+ // the runtime registration filters on.
245
+ val intent = Intent(action)
190
246
  val pendingIntent = PendingIntent.getBroadcast(
191
247
  context,
192
248
  requestCode,
@@ -203,13 +259,57 @@ object PipManager {
203
259
 
204
260
  // ── Intent Filter ──────────────────────────────────────────────────────
205
261
 
206
- fun intentFilter(): IntentFilter {
262
+ /**
263
+ * The filter the runtime registration matches on.
264
+ *
265
+ * `internal` on purpose: registering the receiver is THIS library's
266
+ * job (see [registerActionReceiver]), and a public helper would
267
+ * only invite a consumer to register a second receiver for the same
268
+ * three actions. Two receivers means every action is delivered
269
+ * twice, and a play/pause that toggles twice leaves playback exactly
270
+ * where it was — a row of buttons that look alive and do nothing.
271
+ */
272
+ internal fun intentFilter(): IntentFilter {
207
273
  return IntentFilter().apply {
208
274
  addAction(ACTION_PLAY_PAUSE)
209
275
  addAction(ACTION_EXPAND)
210
276
  addAction(ACTION_CLOSE)
211
277
  }
212
278
  }
279
+
280
+ // ── Receiver registration ─────────────────────────────────────────────
281
+
282
+ /**
283
+ * Registers [PipActionReceiver] for the three PiP actions — ONCE per
284
+ * process.
285
+ *
286
+ * The application context is used on purpose: the receiver has to
287
+ * outlive any Activity, because the platform's PiP action row can
288
+ * fire while the consumer's Activity is paused inside PiP, and long
289
+ * after a configuration change has replaced it.
290
+ *
291
+ * The once-per-process guard matters because the React context — and
292
+ * with it this module — can be recreated (a debug reload, a process
293
+ * restart), and registering again would deliver every action twice.
294
+ */
295
+ internal fun registerActionReceiver(context: Context) {
296
+ val appContext = context.applicationContext
297
+ synchronized(registrationLock) {
298
+ if (registered) return
299
+ val receiver = PipActionReceiver()
300
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
301
+ appContext.registerReceiver(receiver, intentFilter(), Context.RECEIVER_EXPORTED)
302
+ } else {
303
+ appContext.registerReceiver(receiver, intentFilter())
304
+ }
305
+ registered = true
306
+ }
307
+ }
308
+
309
+ private val registrationLock = Any()
310
+
311
+ @Volatile
312
+ private var registered = false
213
313
  }
214
314
 
215
315
  // ── BroadcastReceiver for PiP Actions ──────────────────────────────────────
@@ -218,18 +318,26 @@ object PipManager {
218
318
  * Receives PiP RemoteAction broadcasts and forwards them to the React Native
219
319
  * event layer via [com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter].
220
320
  *
221
- * Registered/unregistered in `MainActivity.onCreate` / `onDestroy` (since
222
- * `MainActivity` is where the in-app foreground session lives; `PlayerActivity`
223
- * shares the receiver registration via the merged module manifest).
321
+ * Registered by THIS library, once per process, on the application context
322
+ * — see [PipManager.registerActionReceiver]. The consumer app does not
323
+ * register it and does not need to know the class exists.
224
324
  *
225
- * V12: receiver class lives in the module (same FQN `com.simba.player.PipActionReceiver`).
226
- * The consumer app's `MainActivity` instantiates / registers it without any
227
- * import changes — Gradle merges the module's class into the app at build time.
325
+ * It is a RUNTIME registration on purpose: the three action strings are
326
+ * owned by [PipManager] as Kotlin constants, and a manifest declaration
327
+ * would have to repeat them as literals — two owners for one fact. The
328
+ * broadcast those constants feed is implicit, so it matches a runtime
329
+ * registration by action. (An EXPLICIT broadcast would need a manifest
330
+ * declaration instead; see [PipManager.buildRemoteAction] for the
331
+ * measured reason that shape never worked.)
228
332
  *
229
333
  * v3: Handles 3 actions — play/pause, expand (restore fullscreen), close (end session).
230
334
  */
231
335
  class PipActionReceiver : BroadcastReceiver() {
232
336
 
337
+ private companion object {
338
+ const val TAG = "PipActionReceiver"
339
+ }
340
+
233
341
  override fun onReceive(context: Context, intent: Intent) {
234
342
  when (intent.action) {
235
343
  PipManager.ACTION_PLAY_PAUSE -> {
@@ -250,9 +358,15 @@ class PipActionReceiver : BroadcastReceiver() {
250
358
  reactContext
251
359
  .getJSModule(com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
252
360
  ?.emit(eventName, params)
253
- } catch (_: Exception) {
254
- // React context not available — receiver may be triggered after
255
- // the JS bundle has unloaded. Silently swallow (no UI to update).
361
+ } catch (e: Exception) {
362
+ // The React context is not available — the broadcast can arrive
363
+ // after the JS bundle has unloaded, and there is no UI to update
364
+ // in that case.
365
+ //
366
+ // It is still LOGGED, and that is the point. A silent catch
367
+ // here is how the PiP action row could render three buttons
368
+ // that did nothing, with not one line in the log to say so.
369
+ android.util.Log.w(TAG, "cannot emit $eventName: no React context", e)
256
370
  }
257
371
  }
258
372