Skip to content

Commit

Permalink
For mozilla-mobile#24040 - App should not crash when the search widge…
Browse files Browse the repository at this point in the history
…t is clicked while PIP mode is active

Unlocks the screen orientation in StartSearchIntentProcessor and navigates to the HomeFragment. The onStop() method in BaseBrowserFragment handles the fullscreen exit functionality.

Co-Authored-By: Mugurell <[email protected]>
  • Loading branch information
indurs and Mugurell committed May 3, 2022
1 parent fb18aa3 commit 7ad1ed3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .experimenter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ messaging:
type: string
description: What should be displayed when a control message is selected.
enum:
- show-next-message
- show-none
- show-next-message
styles:
type: json
description: "A map of styles to configure message appearance.\n"
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.Context
import android.content.Intent
import android.content.Intent.ACTION_MAIN
import android.content.Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
Expand Down Expand Up @@ -62,6 +63,7 @@ import mozilla.components.support.ktx.android.arch.lifecycle.addObservers
import mozilla.components.support.ktx.android.content.call
import mozilla.components.support.ktx.android.content.email
import mozilla.components.support.ktx.android.content.share
import mozilla.components.support.ktx.android.view.exitImmersiveModeIfNeeded
import mozilla.components.support.ktx.kotlin.isUrl
import mozilla.components.support.ktx.kotlin.toNormalizedUrl
import mozilla.components.support.locale.LocaleAwareAppCompatActivity
Expand Down Expand Up @@ -173,7 +175,9 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
listOf(
HomeDeepLinkIntentProcessor(this),
SpeechProcessingIntentProcessor(this, components.core.store, components.analytics.metrics),
StartSearchIntentProcessor(components.analytics.metrics,components.core.store),
StartSearchIntentProcessor(components.analytics.metrics,
unlockOrientationIfInPiPMode = { unlockOrientationIfInPiPMode() },
components.core.store),
OpenBrowserIntentProcessor(this, ::getIntentSessionId),
OpenSpecificTabIntentProcessor(this),
DefaultBrowserIntentProcessor(this)
Expand Down Expand Up @@ -511,6 +515,8 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
intentProcessors.any { it.process(intent, navHost.navController, this.intent) }
browsingModeManager.mode = getModeFromIntentOrLastKnown(intent)



if (intentHandled) {
supportFragmentManager
.primaryNavigationFragment
Expand Down Expand Up @@ -969,6 +975,16 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
return DefaultThemeManager(browsingModeManager.mode, this)
}

private fun unlockOrientationIfInPiPMode(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (isInPictureInPictureMode) {
exitImmersiveModeIfNeeded()
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
themeManager.applyStatusBarTheme(this)
}
}
}

private fun openPopup(webExtensionState: WebExtensionState) {
val action = NavGraphDirections.actionGlobalWebExtensionActionPopupFragment(
webExtensionId = webExtensionState.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ import mozilla.components.feature.downloads.DownloadsFeature
import mozilla.components.feature.downloads.manager.FetchDownloadManager
import mozilla.components.feature.downloads.share.ShareDownloadFeature
import mozilla.components.feature.intent.ext.EXTRA_SESSION_ID
import mozilla.components.feature.media.fullscreen.MediaSessionFullscreenFeature
import mozilla.components.feature.privatemode.feature.SecureWindowFeature
import mozilla.components.feature.prompts.PromptFeature
import mozilla.components.feature.prompts.PromptFeature.Companion.PIN_REQUEST
Expand All @@ -76,6 +75,7 @@ import mozilla.components.feature.session.PictureInPictureFeature
import mozilla.components.feature.session.SessionFeature
import mozilla.components.feature.session.SwipeRefreshFeature
import mozilla.components.concept.engine.permission.SitePermissions
import mozilla.components.feature.media.fullscreen.MediaSessionFullscreenFeature
import mozilla.components.feature.session.ScreenOrientationFeature
import mozilla.components.feature.sitepermissions.SitePermissionsFeature
import mozilla.components.lib.state.ext.consumeFlow
Expand Down Expand Up @@ -1316,7 +1316,7 @@ abstract class BaseBrowserFragment :
* Exit fullscreen mode when exiting PIP mode
*/
private fun pipModeChanged(session: SessionState) {
if (!session.content.pictureInPictureEnabled && session.content.fullScreen) {
if (!session.content.pictureInPictureEnabled && session.content.fullScreen && isAdded) {
onBackPressed()
fullScreenChanged(false)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package org.mozilla.fenix.browser
/* 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 org.mozilla.fenix.ext

import mozilla.components.browser.state.action.BrowserAction
import mozilla.components.browser.state.state.BrowserState
Expand All @@ -10,17 +14,20 @@ fun BrowserStore.waitForPIPToClose(
block: () -> Unit
) {

if (state.findActiveMediaTab() == null || !state.findActiveMediaTab()?.mediaSessionState?.fullscreen!!) {
if (state.findActiveMediaTab() == null || isMediaPlayingInFullScreen(state)) {
block()
return
}

var subscription: Store.Subscription<BrowserState, BrowserAction>? = null
subscription = observeManually { state ->
if (state.findActiveMediaTab() == null || !state.findActiveMediaTab()?.mediaSessionState?.fullscreen!!) {
if (state.findActiveMediaTab() == null || isMediaPlayingInFullScreen(state)) {
block()
subscription!!.unsubscribe()
}
}
subscription.resume()
}

fun isMediaPlayingInFullScreen(state:BrowserState): Boolean{
return state.findActiveMediaTab()?.mediaSessionState?.fullscreen == true
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,42 @@ import android.content.Intent
import androidx.navigation.NavController
import androidx.navigation.navOptions
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.feature.media.ext.findActiveMediaTab
import mozilla.components.service.glean.private.NoExtras
import org.mozilla.fenix.GleanMetrics.SearchWidget
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.NavGraphDirections
import org.mozilla.fenix.R
import org.mozilla.fenix.browser.waitForPIPToClose
import org.mozilla.fenix.components.metrics.MetricController
import org.mozilla.fenix.components.metrics.MetricsUtils
import org.mozilla.fenix.ext.nav
import org.mozilla.gecko.util.ThreadUtils.runOnUiThread

/**
* When the search widget is tapped, Fenix should open to the search fragment.
* Tapping the private browsing mode launcher icon should also open to the search fragment.
*/
class StartSearchIntentProcessor(
private val metrics: MetricController,
private val store: BrowserStore
val unlockOrientationIfInPiPMode: ()->Unit,
val store: BrowserStore,

) : HomeIntentProcessor {

override fun process(intent: Intent, navController: NavController, out: Intent): Boolean {
val event = intent.extras?.getString(HomeActivity.OPEN_TO_SEARCH)
var processed = false

return if (event != null) {
store.waitForPIPToClose {
processed = openSearch(event,navController,out)
openSearch(event,navController,out)
if(store.state.findActiveMediaTab()?.id !=null){
unlockOrientationIfInPiPMode()
}
return processed
return true
} else {
false
}
}

private fun openSearch(event: String,navController: NavController,out: Intent): Boolean{
private fun openSearch(event: String,navController: NavController,out: Intent){
val source = when (event) {
SEARCH_WIDGET -> {
SearchWidget.newTabButton.record(NoExtras())
Expand All @@ -58,21 +59,18 @@ class StartSearchIntentProcessor(

out.removeExtra(HomeActivity.OPEN_TO_SEARCH)

runOnUiThread {
val directions = source?.let {
NavGraphDirections.actionGlobalSearchDialog(
sessionId = null,
searchAccessPoint = it
)
}
directions?.let {
val options = navOptions {
popUpTo = R.id.homeFragment
}
navController.nav(null, it, options)
val directions = source?.let {
NavGraphDirections.actionGlobalSearchDialog(
sessionId = null,
searchAccessPoint = it
)
}
directions?.let {
val options = navOptions {
popUpTo = R.id.homeFragment
}
navController.nav(null, it, options)
}
return true
}

companion object {
Expand Down

0 comments on commit 7ad1ed3

Please sign in to comment.