Commit 1795ba83 authored by MickeyMoz's avatar MickeyMoz
Browse files

Merge #4075 #4104

4075: Improments to tracking protection icon on the toolbar r=ekager,pocmo a=Amejia481

### Pull Request checklist
<!-- Before submitting the PR, please address each item -->
- [x] **Quality**: This PR builds and passes detekt/ktlint checks (A pre-push hook is recommended)
- [x] **Tests**: This PR includes thorough tests or an explanation of why it does not
- [ ] **Changelog**: This PR includes [a changelog entry](https://github.com/mozilla-mobile/android-components/blob/master/docs/changelog.md) or does not need one
- [ ] **Accessibility**: The code in this PR follows [accessibility best practices](https://github.com/mozilla-mobile/shared-docs/blob/master/android/accessibility_guide.md) or does not include any user facing features

### After merge
- [ ] **Milestone**: Make sure issues closed by this pull request are added to the [milestone](https://github.com/mozilla-mobile/android-components/milestones) of the version currently in development.
- [ ] **Breaking Changes**: If this is a breaking change, please push a draft PR on [Reference Browser](https://github.com/mozilla-mobile/reference-browser) to address the breaking issues.

4104: Issue #4033: Abandon audio focus when media service is shutting down. r=Amejia481 a=pocmo

---
<!-- Text above this line will be added to the commit once "bors" merges this PR -->

### Pull Request checklist
<!-- Before submitting the PR, please address each item -->
- [x] **Quality**: This PR builds and passes detekt/ktlint checks (A pre-push hook is recommended)
- [ ] **Tests**: This PR includes thorough tests or an explanation of why it does not
- [ ] **Changelog**: This PR includes [a changelog entry](https://github.com/mozilla-mobile/android-components/blob/master/docs/changelog.md) or does not need one
- [x] **Accessibility**: The code in this PR follows [accessibility best practices](https://github.com/mozilla-mobile/shared-docs/blob/master/android/accessibility_guide.md) or does not include any user facing features

### After merge
- [ ] **Milestone**: Make sure issues closed by this pull request are added to the [milestone](https://github.com/mozilla-mobile/android-components/milestones) of the version currently in development.
- [ ] **Breaking Changes**: If this is a breaking change, please push a draft PR on [Reference Browser](https://github.com/mozilla-mobile/reference-browser

) to address the breaking issues.


Co-authored-by: default avatarArturo Mejia <arturomejiamarmol@gmail.com>
Co-authored-by: default avatarSebastian Kaspari <s.kaspari@gmail.com>
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -147,6 +147,7 @@ internal class DisplayToolbar(
        }

    internal val trackingProtectionIconView = TrackingProtectionIconView(context).apply {
        id = R.id.mozac_browser_toolbar_tracking_protection_icon_view
        isVisible = false
        setImageResource(R.drawable.mozac_tracking_protection_state_list)
        setPadding(resources.getDimensionPixelSize(R.dimen.mozac_browser_toolbar_icon_padding))
@@ -623,9 +624,12 @@ internal class DisplayToolbar(
        }
    }

    private fun shouldTrackingProtectionViewBeVisible() =
        displayTrackingProtectionIcon && (siteTrackingProtection == ON_NO_TRACKERS_BLOCKED ||
            siteTrackingProtection == ON_TRACKERS_BLOCKED)
    private fun shouldTrackingProtectionViewBeVisible(): Boolean {
        val visibleStates = arrayOf(ON_NO_TRACKERS_BLOCKED, ON_TRACKERS_BLOCKED, OFF_FOR_A_SITE)
        val isAVisibleSate = visibleStates.any { it == siteTrackingProtection }

        return displayTrackingProtectionIcon && isAVisibleSate
    }

    companion object {
        internal const val MEASURED_HEIGHT_THIRD_DENOMINATOR = 3
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
  -->

<resources>
    <item name="mozac_browser_toolbar_tracking_protection_icon_view" type="id"/>
    <item name="mozac_browser_toolbar_title_view" type="id"/>
    <item name="mozac_browser_toolbar_url_view" type="id"/>
    <item name="mozac_browser_toolbar_edit_url_view" type="id"/>
+8 −0
Original line number Diff line number Diff line
@@ -188,6 +188,14 @@ class DisplayToolbarTest {

        // Tracking protection views MUST be measured
        assertEquals(totalViewsWidth + trackingProtectionWidth + separatorWidth, view.measuredWidth)

        displayToolbar.setTrackingProtectionState(SiteTrackingProtection.OFF_FOR_A_SITE)
        displayToolbar.measure(widthSpec, heightSpec)

        totalViewsWidth = urlView.measuredWidth + securityIcon.measuredWidth

        // Tracking protection views MUST be measured
        assertEquals(totalViewsWidth + trackingProtectionWidth + separatorWidth, view.measuredWidth)
    }

    @Test
+19 −34
Original line number Diff line number Diff line
@@ -4,9 +4,6 @@

package mozilla.components.feature.media.focus

import android.content.Context
import android.media.AudioAttributes
import android.media.AudioFocusRequest
import android.media.AudioManager
import android.os.Build
import mozilla.components.feature.media.ext.getMedia
@@ -23,19 +20,34 @@ import mozilla.components.support.base.log.logger.Logger
 * https://developer.android.com/guide/topics/media-apps/audio-focus
 */
internal class AudioFocus(
    private val context: Context
    val audioManager: AudioManager
) : AudioManager.OnAudioFocusChangeListener {
    private val logger = Logger("AudioFocus")
    private var playDelayed = false
    private var resumeOnFocusGain = false

    private val audioFocusController = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        AudioFocusControllerV26(audioManager, this)
    } else {
        AudioFocusControllerV21(audioManager, this)
    }

    @Synchronized
    fun request(state: MediaState) {
        val result = requestAudioFocusCompat(context, this)
        val result = audioFocusController.request()
        processAudioFocusResult(state, result)
    }

    @Synchronized
    fun abandon() {
        audioFocusController.abandon()
        playDelayed = false
        resumeOnFocusGain = false
    }

    private fun processAudioFocusResult(state: MediaState, result: Int) {
        logger.debug("processAudioFocusResult($result)")

        when (result) {
            AudioManager.AUDIOFOCUS_REQUEST_GRANTED -> {
                // Granted: Gecko already started playing media.
@@ -60,6 +72,8 @@ internal class AudioFocus(

    @Synchronized
    override fun onAudioFocusChange(focusChange: Int) {
        logger.debug("onAudioFocusChange($focusChange)")

        val state = MediaStateMachine.state

        when (focusChange) {
@@ -93,32 +107,3 @@ internal class AudioFocus(
        }
    }
}

private fun requestAudioFocusCompat(
    context: Context,
    listener: AudioManager.OnAudioFocusChangeListener
): Int {
    val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager

    return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        @Suppress("DEPRECATION")
        audioManager.requestAudioFocus(
            listener,
            AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN
        )
    } else {
        audioManager.requestAudioFocus(
            AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
                .setAudioAttributes(
                    AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_MEDIA)
                        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                        .build()
                )
                .setWillPauseWhenDucked(false)
                .setOnAudioFocusChangeListener(listener)
                .build()
        )
    }
}
+13 −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.focus

/**
 * A controller that knows how to request and abandon audio focus.
 */
internal interface AudioFocusController {
    fun request(): Int
    fun abandon()
}
Loading