Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Commit

Permalink
For #5509 - Show the toolbar and floating buttons when a11y services …
Browse files Browse the repository at this point in the history
…are enabled
  • Loading branch information
Ionut Cristian Bedregeanu authored and mergify[bot] committed Oct 8, 2021
1 parent 1831de1 commit 237b103
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
18 changes: 17 additions & 1 deletion app/src/main/java/org/mozilla/focus/browser/DisplayToolbar.kt
Expand Up @@ -7,7 +7,12 @@ package org.mozilla.focus.browser
import android.content.Context
import android.util.AttributeSet
import com.google.android.material.appbar.AppBarLayout
import kotlinx.android.synthetic.main.browser_display_toolbar.view.*
import com.google.android.material.appbar.AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS
import com.google.android.material.appbar.AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
import com.google.android.material.appbar.AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP
import kotlinx.android.synthetic.main.browser_display_toolbar.view.browserToolbar
import kotlinx.android.synthetic.main.browser_display_toolbar.view.urlbar
import org.mozilla.focus.utils.Settings

/**
* The toolbar of the BrowserFragment; displaying the URL and other controls.
Expand All @@ -16,12 +21,23 @@ class DisplayToolbar(
context: Context,
attrs: AttributeSet
) : AppBarLayout(context, attrs), AppBarLayout.OnOffsetChangedListener {

init {
addOnOffsetChangedListener(this)
}

@Suppress("MagicNumber") // A mathematical expression - No need to add constants for 100% and 50%.
override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {

if (Settings.getInstance(context).isAccessibilityEnabled()) {
(urlbar.layoutParams as LayoutParams).scrollFlags = LayoutParams.SCROLL_FLAG_NO_SCROLL
browserToolbar.alpha = 1f
return
} else {
(urlbar.layoutParams as LayoutParams).scrollFlags =
SCROLL_FLAG_SCROLL or SCROLL_FLAG_ENTER_ALWAYS or SCROLL_FLAG_SNAP
}

// When scrolling the toolbar away we want to fade out the content on the toolbar
// with an alpha animation. This will avoid that the text clashes with the status bar.

Expand Down
30 changes: 29 additions & 1 deletion app/src/main/java/org/mozilla/focus/utils/Settings.kt
Expand Up @@ -4,9 +4,11 @@

package org.mozilla.focus.utils

import android.accessibilityservice.AccessibilityServiceInfo
import android.content.Context
import android.content.SharedPreferences
import android.content.res.Resources
import android.view.accessibility.AccessibilityManager
import androidx.preference.PreferenceManager
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSession
Expand All @@ -18,7 +20,7 @@ import org.mozilla.focus.searchsuggestions.SearchSuggestionsPreferences
/**
* A simple wrapper for SharedPreferences that makes reading preference a little bit easier.
*/
@Suppress("TooManyFunctions") // This class is designed to have a lot of (simple) functions
@Suppress("TooManyFunctions", "LargeClass") // This class is designed to have a lot of (simple) functions
class Settings private constructor(
private val context: Context
) {
Expand All @@ -35,6 +37,26 @@ class Settings private constructor(
}
}

private val accessibilityManager =
context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager?

/**
* Check each active accessibility service to see if it can perform gestures, if any can,
* then it is *likely* a switch service is enabled.
*/
private val switchServiceIsEnabled: Boolean
get() {
accessibilityManager?.getEnabledAccessibilityServiceList(0)?.let { activeServices ->
for (service in activeServices) {
if (service.capabilities.and(AccessibilityServiceInfo.CAPABILITY_CAN_PERFORM_GESTURES) == 1) {
return true
}
}
}

return false
}

private val preferencesListener = EngineSharedPreferencesListener(context)

private val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context).apply {
Expand Down Expand Up @@ -210,6 +232,12 @@ class Settings private constructor(
false
)

/**
* This is automatically inferred based on the current system status. Not a setting in our app.
*/
fun isAccessibilityEnabled() =
accessibilityManager?.isTouchExplorationEnabled ?: false || switchServiceIsEnabled

fun userHasToggledSearchSuggestions(): Boolean =
preferences.getBoolean(SearchSuggestionsPreferences.TOGGLED_SUGGESTIONS_PREF, false)

Expand Down
Expand Up @@ -14,6 +14,8 @@
import android.util.AttributeSet;
import android.view.View;

import org.mozilla.focus.utils.Settings;

/**
* A Behavior implementation that will hide/show a FloatingActionButton based on whether an AppBarLayout
* is visible or not.
Expand Down Expand Up @@ -58,7 +60,7 @@ public void onDependentViewRemoved(CoordinatorLayout parent, FloatingActionButto

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (verticalOffset == 0 && !visible) {
if ((verticalOffset == 0 && !visible) || Settings.getInstance(appBarLayout.getContext()).isAccessibilityEnabled()) {
showButton();
} else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange() && visible) {
hideButton();
Expand Down
Expand Up @@ -9,7 +9,6 @@
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import android.util.AttributeSet;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.view.animation.AnimationUtils;

import org.mozilla.focus.R;
Expand All @@ -32,17 +31,11 @@ public FloatingEraseButton(Context context, AttributeSet attrs, int defStyleAttr
public void updateSessionsCount(int tabCount) {
final CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) getLayoutParams();
final FloatingActionButtonBehavior behavior = (FloatingActionButtonBehavior) params.getBehavior();
AccessibilityManager accessibilityManager = (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);

keepHidden = tabCount != 1;

if (behavior != null) {
if (accessibilityManager != null && accessibilityManager.isTouchExplorationEnabled()) {
// Always display erase button if Talk Back is enabled
behavior.setAutoHideEnabled(false);
} else {
behavior.setAutoHideEnabled(!keepHidden);
}
behavior.setAutoHideEnabled(!keepHidden);
}

if (keepHidden) {
Expand Down
Expand Up @@ -70,13 +70,7 @@ public void updateSessionsCount(int tabCount) {
if (behavior != null) {
behavior.setAutoHideEnabled(shouldBeVisible);
}

if (shouldBeVisible) {
show();
invalidate();
} else {
hide();
}
invalidate();
}
}

Expand Down

0 comments on commit 237b103

Please sign in to comment.