From 327009efc4247f6a5cd0dcda19e94fe25228acfd Mon Sep 17 00:00:00 2001 From: Emily Kager Date: Wed, 1 Apr 2020 19:56:12 -0700 Subject: [PATCH] For #768: Add first time PWA dialog (#9308) Co-authored-by: Tiger Oakes --- .../mozilla/fenix/browser/BrowserFragment.kt | 24 ++++- .../fenix/shortcut/FirstTimePwaFragment.kt | 44 +++++++++ .../fenix/shortcut/FirstTimePwaObserver.kt | 33 +++++++ .../java/org/mozilla/fenix/utils/Settings.kt | 5 + .../drawable/onboarding_button_background.xml | 20 +++- .../res/layout/fragment_pwa_first_time.xml | 96 +++++++++++++++++++ app/src/main/res/navigation/nav_graph.xml | 10 ++ app/src/main/res/values/preference_keys.xml | 2 + app/src/main/res/values/strings.xml | 4 + 9 files changed, 230 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/org/mozilla/fenix/shortcut/FirstTimePwaFragment.kt create mode 100644 app/src/main/java/org/mozilla/fenix/shortcut/FirstTimePwaObserver.kt create mode 100644 app/src/main/res/layout/fragment_pwa_first_time.xml diff --git a/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt index bf6aae94a..b301c048d 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt @@ -10,6 +10,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.coordinatorlayout.widget.CoordinatorLayout +import androidx.navigation.fragment.findNavController import com.google.android.material.snackbar.Snackbar import kotlinx.android.synthetic.main.fragment_browser.* import kotlinx.android.synthetic.main.fragment_browser.view.* @@ -35,6 +36,7 @@ import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.nav import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.ext.settings +import org.mozilla.fenix.shortcut.FirstTimePwaObserver import org.mozilla.fenix.trackingprotection.TrackingProtectionOverlay /** @@ -107,14 +109,30 @@ class BrowserFragment : BaseBrowserFragment(), UserInteractionHandler { override fun onStart() { super.onStart() + val context = requireContext() + val settings = context.settings() + val session = getSessionById() + val toolbarSessionObserver = TrackingProtectionOverlay( - context = requireContext(), - settings = requireContext().settings() + context = context, + settings = settings ) { browserToolbarView.view } - getSessionById()?.register(toolbarSessionObserver, this, autoPause = true) + session?.register(toolbarSessionObserver, this, autoPause = true) updateEngineBottomMargin() + + if (settings.shouldShowFirstTimePwaFragment) { + session?.register( + FirstTimePwaObserver( + navController = findNavController(), + settings = settings, + webAppUseCases = context.components.useCases.webAppUseCases + ), + owner = this, + autoPause = true + ) + } } private fun updateEngineBottomMargin() { diff --git a/app/src/main/java/org/mozilla/fenix/shortcut/FirstTimePwaFragment.kt b/app/src/main/java/org/mozilla/fenix/shortcut/FirstTimePwaFragment.kt new file mode 100644 index 000000000..c42c2842f --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/shortcut/FirstTimePwaFragment.kt @@ -0,0 +1,44 @@ +/* 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.shortcut + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.DialogFragment +import androidx.lifecycle.lifecycleScope +import kotlinx.android.synthetic.main.fragment_create_shortcut.* +import kotlinx.coroutines.launch +import org.mozilla.fenix.R +import org.mozilla.fenix.ext.requireComponents + +/** + * Dialog displayed the first time the user navigates to an installable web app. + */ +class FirstTimePwaFragment : DialogFragment() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setStyle(STYLE_NO_TITLE, R.style.CreateShortcutDialogStyle) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? = inflater.inflate(R.layout.fragment_pwa_first_time, container, false) + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + val components = requireComponents + + cancel_button.setOnClickListener { dismiss() } + add_button.setOnClickListener { + viewLifecycleOwner.lifecycleScope.launch { + components.useCases.webAppUseCases.addToHomescreen() + }.invokeOnCompletion { dismiss() } + } + } +} diff --git a/app/src/main/java/org/mozilla/fenix/shortcut/FirstTimePwaObserver.kt b/app/src/main/java/org/mozilla/fenix/shortcut/FirstTimePwaObserver.kt new file mode 100644 index 000000000..d0fdb5c62 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/shortcut/FirstTimePwaObserver.kt @@ -0,0 +1,33 @@ +/* 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.shortcut + +import androidx.navigation.NavController +import mozilla.components.browser.session.Session +import mozilla.components.concept.engine.manifest.WebAppManifest +import mozilla.components.feature.pwa.WebAppUseCases +import org.mozilla.fenix.R +import org.mozilla.fenix.browser.BrowserFragmentDirections +import org.mozilla.fenix.ext.nav +import org.mozilla.fenix.utils.Settings + +/** + * Displays the [FirstTimePwaFragment] info dialog when a PWA is first opened in the browser. + */ +class FirstTimePwaObserver( + private val navController: NavController, + private val settings: Settings, + private val webAppUseCases: WebAppUseCases +) : Session.Observer { + + override fun onWebAppManifestChanged(session: Session, manifest: WebAppManifest?) { + if (webAppUseCases.isInstallable() && settings.shouldShowFirstTimePwaFragment) { + val directions = BrowserFragmentDirections.actionBrowserFragmentToFirstTimePwaFragment() + navController.nav(R.id.browserFragment, directions) + + settings.shouldShowFirstTimePwaFragment = false + } + } +} diff --git a/app/src/main/java/org/mozilla/fenix/utils/Settings.kt b/app/src/main/java/org/mozilla/fenix/utils/Settings.kt index 1b0478db7..73b1de499 100644 --- a/app/src/main/java/org/mozilla/fenix/utils/Settings.kt +++ b/app/src/main/java/org/mozilla/fenix/utils/Settings.kt @@ -434,6 +434,11 @@ class Settings private constructor( default = false ) + var shouldShowFirstTimePwaFragment by booleanPreference( + appContext.getPreferenceKey(R.string.pref_key_show_first_time_pwa), + default = true + ) + @VisibleForTesting(otherwise = PRIVATE) internal val trackingProtectionOnboardingCount by intPreference( appContext.getPreferenceKey(R.string.pref_key_tracking_protection_onboarding), diff --git a/app/src/main/res/drawable/onboarding_button_background.xml b/app/src/main/res/drawable/onboarding_button_background.xml index e4aaec8f3..ce3863996 100644 --- a/app/src/main/res/drawable/onboarding_button_background.xml +++ b/app/src/main/res/drawable/onboarding_button_background.xml @@ -2,8 +2,18 @@ - - - - - \ No newline at end of file + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_pwa_first_time.xml b/app/src/main/res/layout/fragment_pwa_first_time.xml new file mode 100644 index 000000000..395a933a3 --- /dev/null +++ b/app/src/main/res/layout/fragment_pwa_first_time.xml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + diff --git a/app/src/main/res/navigation/nav_graph.xml b/app/src/main/res/navigation/nav_graph.xml index 636cf9f88..92f33aabb 100644 --- a/app/src/main/res/navigation/nav_graph.xml +++ b/app/src/main/res/navigation/nav_graph.xml @@ -211,6 +211,9 @@ + @@ -649,10 +652,17 @@ app:argType="org.mozilla.fenix.collections.SaveCollectionStep" app:nullable="false" /> + + + pref_key_encryption_key_generated pref_key_pocket_top_site_added pref_key_top_sites_size + + pref_key_show_first_time_pwa diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 40f656d24..0ac2b5688 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1099,8 +1099,12 @@ Cancel Add + + Continue to website Shortcut name + + You can easily add this website to your phone’s Home screen to have instant access and browse faster with an app-like experience. Logins and passwords