Commit 186fccf4 authored by MozLando's avatar MozLando
Browse files

Merge #4494



4494: Closes #4396: feature-media: Emit facts. r=Amejia481 a=pocmo



Co-authored-by: default avatarSebastian Kaspari <s.kaspari@gmail.com>
parents f433001e e9b500ba
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -24,6 +24,18 @@ RecordingDevicesNotificationFeature(applicationContext, sessionManager)
    .enable()
```

## Facts

This component emits the following [Facts](../../support/base/README.md#Facts):

| Action | Item            |  Description                              |
|--------|-----------------|-------------------------------------------|
| PLAY   | state           | Media started playing.                    |
| PAUSE  | state           | Media playback was paused.                |
| STOP   | state           | Media playback has ended.                 |
| PLAY   | notification    | Play action of notification was invoked   |
| PAUSE  | notification    | Pause action of notification was invoked  |

## License

    This Source Code Form is subject to the terms of the Mozilla Public
+6 −0
Original line number Diff line number Diff line
@@ -6,6 +6,9 @@ package mozilla.components.feature.media

import android.content.Context
import mozilla.components.feature.media.ext.hasMediaWithSufficientLongDuration
import mozilla.components.feature.media.facts.emitStatePauseFact
import mozilla.components.feature.media.facts.emitStatePlayFact
import mozilla.components.feature.media.facts.emitStateStopFact
import mozilla.components.feature.media.service.MediaService
import mozilla.components.feature.media.state.MediaState
import mozilla.components.feature.media.state.MediaStateMachine
@@ -48,12 +51,14 @@ class MediaFeature(
                if (state.hasMediaWithSufficientLongDuration()) {
                    MediaService.updateState(context)
                    serviceStarted = true
                    emitStatePlayFact()
                }
            }

            is MediaState.Paused -> {
                if (serviceStarted) {
                    MediaService.updateState(context)
                    emitStatePauseFact()
                }
            }

@@ -61,6 +66,7 @@ class MediaFeature(
                if (serviceStarted) {
                    MediaService.updateState(context)
                    serviceStarted = false
                    emitStateStopFact()
                }
            }
        }
+42 −0
Original line number Diff line number Diff line
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.feature.media.facts

import mozilla.components.support.base.Component
import mozilla.components.support.base.facts.Action
import mozilla.components.support.base.facts.Fact
import mozilla.components.support.base.facts.collect

private object MediaItems {
    const val NOTIFICATION = "notification"
    const val STATE = "state"
}

internal fun emitNotificationPlayFact() = emitNotificationFact(Action.PLAY)
internal fun emitNotificationPauseFact() = emitNotificationFact(Action.PAUSE)

internal fun emitStatePlayFact() = emitStateFact(Action.PLAY)
internal fun emitStatePauseFact() = emitStateFact(Action.PAUSE)
internal fun emitStateStopFact() = emitStateFact(Action.STOP)

private fun emitStateFact(
    action: Action
) {
    Fact(
        Component.FEATURE_MEDIA,
        action,
        MediaItems.STATE
    ).collect()
}

private fun emitNotificationFact(
    action: Action
) {
    Fact(
        Component.FEATURE_MEDIA,
        action,
        MediaItems.NOTIFICATION
    ).collect()
}
+10 −2
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ import mozilla.components.feature.media.ext.isForCustomTabSession
import mozilla.components.feature.media.ext.pauseIfPlaying
import mozilla.components.feature.media.ext.playIfPaused
import mozilla.components.feature.media.ext.toPlaybackState
import mozilla.components.feature.media.facts.emitNotificationPauseFact
import mozilla.components.feature.media.facts.emitNotificationPlayFact
import mozilla.components.feature.media.focus.AudioFocus
import mozilla.components.feature.media.notification.MediaNotification
import mozilla.components.feature.media.session.MediaSessionCallback
@@ -54,8 +56,14 @@ internal class MediaService : Service() {

        when (intent?.action) {
            ACTION_UPDATE_STATE -> processCurrentState()
            ACTION_PLAY -> MediaStateMachine.state.playIfPaused()
            ACTION_PAUSE -> MediaStateMachine.state.pauseIfPlaying()
            ACTION_PLAY -> {
                MediaStateMachine.state.playIfPaused()
                emitNotificationPlayFact()
            }
            ACTION_PAUSE -> {
                MediaStateMachine.state.pauseIfPlaying()
                emitNotificationPauseFact()
            }
            else -> logger.debug("Can't process action: ${intent?.action}")
        }

+15 −0
Original line number Diff line number Diff line
@@ -26,6 +26,21 @@ enum class Action {
     */
    COMMIT,

    /**
     * The user has started playing something.
     */
    PLAY,

    /**
     * The user has paused something.
     */
    PAUSE,

    /**
     * The user has stopped something.
     */
    STOP,

    /**
     * A generic interaction that can be caused by a previous action (e.g. the user clicks on a button which causes a
     * [Fact] with [CLICK] action to be emitted. This click may causes something to load which emits a follow-up a