From ecb43e4fb46386f70469a0c8580e865629ccdf75 Mon Sep 17 00:00:00 2001 From: Emily Kager Date: Wed, 9 Jan 2019 17:06:33 -0800 Subject: [PATCH] Closes #3921 - Show Snackbar after page load after toggling block toggle --- .../mozilla/focus/fragment/BrowserFragment.kt | 33 +++++++++++++++++++ .../menu/browser/BlockingItemViewHolder.kt | 15 +++++---- .../focus/menu/browser/BrowserMenu.java | 4 +++ .../focus/menu/browser/BrowserMenuAdapter.kt | 5 +++ app/src/main/res/values/strings.xml | 8 +++++ 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/mozilla/focus/fragment/BrowserFragment.kt b/app/src/main/java/org/mozilla/focus/fragment/BrowserFragment.kt index 29b4065aa52..6f601c0a6c7 100644 --- a/app/src/main/java/org/mozilla/focus/fragment/BrowserFragment.kt +++ b/app/src/main/java/org/mozilla/focus/fragment/BrowserFragment.kt @@ -27,6 +27,7 @@ import android.preference.PreferenceManager import android.support.annotation.RequiresApi import android.support.design.widget.AppBarLayout import android.support.design.widget.CoordinatorLayout +import android.support.design.widget.Snackbar import android.support.v4.app.ActivityCompat import android.support.v4.content.ContextCompat import android.support.v4.widget.SwipeRefreshLayout @@ -150,6 +151,8 @@ class BrowserFragment : WebFragment(), LifecycleObserver, View.OnClickListener, private var findInPagePrevious: ImageButton? = null private var closeFindInPage: ImageButton? = null + private var showContentBlockingSnackbar = false + private var fullscreenCallback: IWebView.FullscreenCallback? = null private var manager: DownloadManager? = null @@ -1237,6 +1240,11 @@ class BrowserFragment : WebFragment(), LifecycleObserver, View.OnClickListener, fun reload() = getWebView()?.reload() + fun reloadWithNewBlockingState() { + reload() + showContentBlockingSnackbar = true + } + fun setBlockingUI(enabled: Boolean) { val webView = getWebView() webView?.setBlockingEnabled(enabled) @@ -1430,6 +1438,8 @@ class BrowserFragment : WebFragment(), LifecycleObserver, View.OnClickListener, swipeRefresh!!.isRefreshing = false updateSecurityIcon(session) + + showContentBlockingSnackbarIfChanged() } updateBlockingBadging(loading || session.trackerBlockingEnabled) @@ -1485,6 +1495,29 @@ class BrowserFragment : WebFragment(), LifecycleObserver, View.OnClickListener, showCrashReporter(crash) } + private fun showContentBlockingSnackbarIfChanged() { + if (showContentBlockingSnackbar) { + // Show Snackbar to inform users and allow them to undo their choice + val snackbar = Snackbar.make( + activity!!.findViewById(android.R.id.content), + if (!session.trackerBlockingEnabled) + R.string.content_blocking_disabled_snackbar_message else + R.string.content_blocking_enabled_snackbar_message, + Snackbar.LENGTH_SHORT + ) + snackbar.setAction( + if (!session.trackerBlockingEnabled) + R.string.content_blocking_snackbar_enable_action else + R.string.content_blocking_snackbar_disable_action + ) { + val menu = menuWeakReference!!.get() + menu?.updateBlocking(!session.trackerBlockingEnabled) + } + snackbar.show() + showContentBlockingSnackbar = false + } + } + companion object { const val FRAGMENT_TAG = "browser" diff --git a/app/src/main/java/org/mozilla/focus/menu/browser/BlockingItemViewHolder.kt b/app/src/main/java/org/mozilla/focus/menu/browser/BlockingItemViewHolder.kt index 8c02bbe0995..9e355cf1ce8 100644 --- a/app/src/main/java/org/mozilla/focus/menu/browser/BlockingItemViewHolder.kt +++ b/app/src/main/java/org/mozilla/focus/menu/browser/BlockingItemViewHolder.kt @@ -57,8 +57,8 @@ internal class BlockingItemViewHolder(itemView: View, private val fragment: Brow ThreadUtils.postToMainThread(Runnable { view.setText(R.string.content_blocking_disabled) }) } - override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) { - fragment.setBlockingUI(isChecked) + fun addOrRemoveURL(isBlockingEnabled: Boolean) { + fragment.setBlockingUI(isBlockingEnabled) val url = fragment.url val host = try { @@ -67,23 +67,26 @@ internal class BlockingItemViewHolder(itemView: View, private val fragment: Brow url } ?: url - if (!isChecked) { + if (!isBlockingEnabled) { addUrlToExceptionsList(host = host) } else { removeUrlFromExceptionsList(host = host) } - TelemetryWrapper.blockingSwitchEvent(isChecked) + TelemetryWrapper.blockingSwitchEvent(isBlockingEnabled) // Delay closing the menu and reloading the website a bit so that the user can actually see // the switch change its state. ThreadUtils.postToMainThreadDelayed(Runnable { menu.dismiss() - - fragment.reload() + fragment.reloadWithNewBlockingState() }, Switch_THUMB_ANIMATION_DURATION) } + override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) { + addOrRemoveURL(isChecked) + } + private fun addUrlToExceptionsList(host: String) { fragment.launch(IO) { val duplicateURL = ExceptionDomains.load(fragment.requireContext()).contains(host) diff --git a/app/src/main/java/org/mozilla/focus/menu/browser/BrowserMenu.java b/app/src/main/java/org/mozilla/focus/menu/browser/BrowserMenu.java index a0cf6d20a26..6a900f0ffe1 100644 --- a/app/src/main/java/org/mozilla/focus/menu/browser/BrowserMenu.java +++ b/app/src/main/java/org/mozilla/focus/menu/browser/BrowserMenu.java @@ -64,6 +64,10 @@ public void updateLoading(boolean loading) { adapter.updateLoading(loading); } + public void updateBlocking(boolean isBlockingEnabled) { + adapter.updateBlocking(isBlockingEnabled); + } + public void show(View anchor) { final int xOffset = ViewUtils.INSTANCE.isRTL(anchor) ? -anchor.getWidth() : 0; diff --git a/app/src/main/java/org/mozilla/focus/menu/browser/BrowserMenuAdapter.kt b/app/src/main/java/org/mozilla/focus/menu/browser/BrowserMenuAdapter.kt index 3a9c45aacc0..5b83ea955c5 100644 --- a/app/src/main/java/org/mozilla/focus/menu/browser/BrowserMenuAdapter.kt +++ b/app/src/main/java/org/mozilla/focus/menu/browser/BrowserMenuAdapter.kt @@ -165,6 +165,11 @@ class BrowserMenuAdapter( navigationItemViewHolder.updateTrackers(trackers) } + fun updateBlocking(isBlockingEnabled: Boolean) { + val navigationItemViewHolder = blockingItemViewHolderReference.get() ?: return + navigationItemViewHolder.addOrRemoveURL(isBlockingEnabled) + } + fun updateLoading(loading: Boolean) { val navigationItemViewHolder = navigationItemViewHolderReference.get() ?: return navigationItemViewHolder.updateLoading(loading) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b8bba64e30d..a056411db18 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -828,6 +828,14 @@ be temporary, and you can try again later. Send crash report to Mozilla + + Content Blocking is enabled + + Content Blocking is disabled + + Enable + + Disable Erase and open %1$s