Skip to content
This repository has been archived by the owner on Apr 17, 2021. It is now read-only.

Update MainActivity handling to not use the Homescreen #815

Merged
merged 10 commits into from
Apr 23, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
import org.mozilla.focus.utils.UrlUtils;
import org.mozilla.focus.iwebview.IWebView;

import java.io.ByteArrayInputStream;

import static org.mozilla.focus.browser.BrowserFragment.APP_URL_HOME;
import static org.mozilla.focus.browser.BrowserFragment.APP_URL_PREFIX;

/**
* WebViewClient layer that handles browser specific WebViewClient functionality, such as error pages
* and external URL handling.
Expand Down Expand Up @@ -109,9 +114,24 @@ public void onLoadResource(AmazonWebView view, String url) {
// WebResourceRequest was added in API21 and there is no equivalent AmazonWebResourceRequest
@Override
public AmazonWebResourceResponse shouldInterceptRequest(AmazonWebView view, String request) {
if (request != null && request.startsWith(APP_URL_PREFIX)) {
switch (request) {
case APP_URL_HOME:
// Home screen should show a blank webview behind the overlay, but keep the url.
callback.onShouldInterceptRequest(request);
final ByteArrayInputStream homeDataStream = getBlankPageStream();
return new AmazonWebResourceResponse("text/html", "utf-8", homeDataStream);
}
}

return super.shouldInterceptRequest(view, request);
}

private ByteArrayInputStream getBlankPageStream() {
// ByteArrayInputStream doesn't need to be closed.
return new ByteArrayInputStream("<html></html>".getBytes());
}

// @Override
// public AmazonWebResourceResponse shouldInterceptRequest(AmazonWebView view, final AmazonWebResourceRequest request) {
// // Only update the user visible URL if:
Expand Down
10 changes: 3 additions & 7 deletions app/src/main/java/org/mozilla/focus/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import com.amazon.android.webkit.AmazonWebKitFactory
import kotlinx.android.synthetic.main.activity_main.*
import org.mozilla.focus.architecture.NonNullObserver
import org.mozilla.focus.browser.BrowserFragment
import org.mozilla.focus.browser.BrowserFragment.Companion.APP_URL_HOME
import org.mozilla.focus.ext.toSafeIntent
import org.mozilla.focus.home.HomeFragment
import org.mozilla.focus.iwebview.IWebView
import org.mozilla.focus.iwebview.WebViewProvider
import org.mozilla.focus.locale.LocaleAwareAppCompatActivity
Expand Down Expand Up @@ -54,9 +54,8 @@ class MainActivity : LocaleAwareAppCompatActivity(), OnUrlEnteredListener {
sessionManager.sessions.observe(this, object : NonNullObserver<List<Session>>() {
public override fun onValueChanged(sessions: List<Session>) {
if (sessions.isEmpty()) {
// There's no active session. Show the URL input screen so that the user can
// start a new session.
ScreenController.showHomeScreen(supportFragmentManager, this@MainActivity)
// There's no active session. Start a new session with "homepage".
ScreenController.showBrowserScreenForUrl(supportFragmentManager, APP_URL_HOME, Source.NONE)
} else {
ScreenController.showBrowserScreenForCurrentSession(supportFragmentManager, sessionManager)
}
Expand Down Expand Up @@ -153,12 +152,9 @@ class MainActivity : LocaleAwareAppCompatActivity(), OnUrlEnteredListener {
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
val fragmentManager = supportFragmentManager
val browserFragment = fragmentManager.findFragmentByTag(BrowserFragment.FRAGMENT_TAG) as BrowserFragment?
val homeFragment = fragmentManager.findFragmentByTag(HomeFragment.FRAGMENT_TAG) as HomeFragment?

return if (browserFragment != null && browserFragment.isVisible) {
browserFragment.dispatchKeyEvent(event) || super.dispatchKeyEvent(event)
} else if (homeFragment != null && homeFragment.isVisible) {
homeFragment.dispatchKeyEvent(event) || super.dispatchKeyEvent(event)
} else {
super.dispatchKeyEvent(event)
}
Expand Down
21 changes: 0 additions & 21 deletions app/src/main/java/org/mozilla/focus/ScreenController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import android.content.Context
import android.support.v4.app.FragmentManager
import android.text.TextUtils
import org.mozilla.focus.browser.BrowserFragment
import org.mozilla.focus.home.HomeFragment
import org.mozilla.focus.session.SessionManager
import org.mozilla.focus.session.Source
import org.mozilla.focus.telemetry.TelemetryWrapper
import org.mozilla.focus.telemetry.UrlTextInputLocation
import org.mozilla.focus.utils.OnUrlEnteredListener
import org.mozilla.focus.utils.UrlUtils
import org.mozilla.focus.widget.InlineAutocompleteEditText

Expand Down Expand Up @@ -48,25 +46,6 @@ object ScreenController {
}
}

fun showHomeScreen(fragmentManager: FragmentManager, onUrlEnteredListener: OnUrlEnteredListener) {
val homeFragment = fragmentManager.findFragmentByTag(HomeFragment.FRAGMENT_TAG) as HomeFragment?
if (homeFragment != null && homeFragment.isVisible) {
// This is already at the top of the stack - do nothing.
return
}

// We don't want to be able to go back from the back stack, so clear the whole fragment back stack.
fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)

// Show the home screen.
val newHomeFragment = HomeFragment.create()
newHomeFragment.onSettingsPressed = { showSettingsScreen(fragmentManager) }
newHomeFragment.onUrlEnteredListener = onUrlEnteredListener
fragmentManager.beginTransaction()
.replace(R.id.container, newHomeFragment, HomeFragment.FRAGMENT_TAG)
.commit()
}

fun showSettingsScreen(fragmentManager: FragmentManager) {
val settingsFragment = SettingsFragment.create()
fragmentManager.beginTransaction()
Expand Down
29 changes: 21 additions & 8 deletions app/src/main/java/org/mozilla/focus/browser/BrowserFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import android.view.ViewGroup
import android.widget.FrameLayout
import kotlinx.android.synthetic.main.fragment_browser.*
import kotlinx.android.synthetic.main.fragment_browser.view.*
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import org.mozilla.focus.MainActivity
import org.mozilla.focus.R
import org.mozilla.focus.ScreenController
import org.mozilla.focus.architecture.NonNullObserver
import org.mozilla.focus.browser.BrowserFragment.Companion.APP_URL_HOME
import org.mozilla.focus.browser.cursor.CursorController
import org.mozilla.focus.ext.isVisible
import org.mozilla.focus.ext.toUri
Expand All @@ -32,7 +35,6 @@ import org.mozilla.focus.session.SessionCallbackProxy
import org.mozilla.focus.session.SessionManager
import org.mozilla.focus.telemetry.TelemetryWrapper
import org.mozilla.focus.telemetry.UrlTextInputLocation
import org.mozilla.focus.utils.OnUrlEnteredListener
import org.mozilla.focus.utils.ViewUtils.showCenteredTopToast
import org.mozilla.focus.widget.InlineAutocompleteEditText

Expand All @@ -46,6 +48,8 @@ private const val TOAST_Y_OFFSET = 200
class BrowserFragment : IWebViewLifecycleFragment() {
companion object {
const val FRAGMENT_TAG = "browser"
const val APP_URL_PREFIX = "firefox:"
const val APP_URL_HOME = "${APP_URL_PREFIX}home"

@JvmStatic
fun createForSession(session: Session) = BrowserFragment().apply {
Expand All @@ -68,6 +72,8 @@ class BrowserFragment : IWebViewLifecycleFragment() {
var url: String? = null
private set

val isUrlEqualToHomepage: Boolean get() = url == APP_URL_HOME

private val sessionManager = SessionManager.getInstance()

/**
Expand Down Expand Up @@ -111,15 +117,14 @@ class BrowserFragment : IWebViewLifecycleFragment() {
NavigationEvent.BACK -> if (webView?.canGoBack() ?: false) webView?.goBack()
NavigationEvent.FORWARD -> if (webView?.canGoForward() ?: false) webView?.goForward()
NavigationEvent.TURBO, NavigationEvent.RELOAD -> webView?.reload()
NavigationEvent.HOME -> ScreenController.showHomeScreen(fragmentManager, activity as OnUrlEnteredListener)
NavigationEvent.SETTINGS -> ScreenController.showSettingsScreen(fragmentManager)
NavigationEvent.LOAD_URL -> {
(activity as MainActivity).onTextInputUrlEntered(value!!, autocompleteResult!!, UrlTextInputLocation.MENU)
setOverlayVisibileByUser(false)
setOverlayVisibleByUser(false)
}
NavigationEvent.LOAD_TILE -> {
(activity as MainActivity).onNonTextInputUrlEntered(value!!)
setOverlayVisibileByUser(false)
setOverlayVisibleByUser(false)
}
NavigationEvent.PIN_ACTION -> {
this@BrowserFragment.url?.let { url ->
Expand Down Expand Up @@ -185,7 +190,7 @@ class BrowserFragment : IWebViewLifecycleFragment() {
webView?.goBack()
TelemetryWrapper.browserBackControllerEvent()
}
browserOverlay.isVisible -> setOverlayVisibileByUser(false)
browserOverlay.isVisible -> setOverlayVisibleByUser(false)
else -> {
fragmentManager.popBackStack()
SessionManager.getInstance().removeCurrentSession()
Expand Down Expand Up @@ -219,10 +224,10 @@ class BrowserFragment : IWebViewLifecycleFragment() {
val actionIsDown = event.action == KeyEvent.ACTION_DOWN
val isOverlayToggleKey = (keyCodeIsMenu || (keyCodeIsBack && browserOverlay.isVisible))

if (isOverlayToggleKey) {
if (isOverlayToggleKey && !isUrlEqualToHomepage) {
if (actionIsDown) {
val toShow = !browserOverlay.isVisible
setOverlayVisibileByUser(toShow)
setOverlayVisibleByUser(toShow)
// Fix this youtube focus hack in #393
if (!toShow && webView!!.isYoutubeTV) {
webView?.requestFocus()
Expand All @@ -247,7 +252,7 @@ class BrowserFragment : IWebViewLifecycleFragment() {
* It's important this is only called for user actions because our Telemetry
* is dependent on it.
*/
private fun setOverlayVisibileByUser(toShow: Boolean) {
private fun setOverlayVisibleByUser(toShow: Boolean) {
browserOverlay.visibility = if (toShow) View.VISIBLE else View.GONE
if (toShow) cursor?.onPause() else cursor?.onResume()
cursor?.setEnabledForCurrentState()
Expand Down Expand Up @@ -282,6 +287,14 @@ private class BrowserIWebViewCallback(
override fun onBlockingStateChanged(isBlockingEnabled: Boolean) {}

override fun onLongPress(hitTarget: IWebView.HitTarget) {}
override fun onShouldInterceptRequest(url: String) {
// This might not be called from the UI thread but needs to be, so we use launch.
launch(UI) {
when (url) {
APP_URL_HOME -> browserFragment.browserOverlay?.visibility = View.VISIBLE
}
}
}

override fun onEnterFullScreen(callback: IWebView.FullscreenCallback, view: View?) {
fullscreenCallback = callback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ private const val NAVIGATION_BUTTON_DISABLED_ALPHA = 0.3f
private const val COL_COUNT = 5

enum class NavigationEvent {
HOME, SETTINGS, BACK, FORWARD, RELOAD, LOAD_URL, LOAD_TILE, TURBO, PIN_ACTION;
SETTINGS, BACK, FORWARD, RELOAD, LOAD_URL, LOAD_TILE, TURBO, PIN_ACTION;

companion object {
fun fromViewClick(viewId: Int?) = when (viewId) {
R.id.navButtonBack -> BACK
R.id.navButtonForward -> FORWARD
R.id.navButtonReload -> RELOAD
R.id.navButtonHome -> HOME
R.id.navButtonSettings -> SETTINGS
R.id.turboButton -> TURBO
R.id.pinButton -> PIN_ACTION
Expand Down Expand Up @@ -83,7 +82,7 @@ class BrowserNavigationOverlay @JvmOverloads constructor(
init {
LayoutInflater.from(context)
.inflate(R.layout.browser_overlay, this, true)
listOf(navButtonBack, navButtonForward, navButtonReload, navButtonHome, navButtonSettings,
listOf(navButtonBack, navButtonForward, navButtonReload, navButtonSettings,
turboButton, pinButton)
.forEach {
it.setOnClickListener(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ class HomeTileAdapter(
iconView.layoutParams = layoutMarginParams
}

fun getItemAtPosition(position: Int): HomeTile? {
if (position > -1 && position < itemCount) {
return tiles[position]
}
return null
}

/**
* takes in the home tiles cache and updates the adapter's data source
* and UI accordingly, assuming only one new tile is added
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/org/mozilla/focus/browser/InfoFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public void onProgress(int progress) {
@Override
public void onLongPress(IWebView.HitTarget hitTarget) {}

@Override
public void onShouldInterceptRequest(String url) {}

@Override
public void onURLChanged(String url) {}

Expand Down
129 changes: 0 additions & 129 deletions app/src/main/java/org/mozilla/focus/home/HomeFragment.kt

This file was deleted.