mobile/android/base/DynamicToolbar.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 package org.mozilla.gecko;
michael@0 2
michael@0 3 import java.util.EnumSet;
michael@0 4
michael@0 5 import org.mozilla.gecko.PrefsHelper.PrefHandlerBase;
michael@0 6 import org.mozilla.gecko.gfx.LayerView;
michael@0 7 import org.mozilla.gecko.util.ThreadUtils;
michael@0 8
michael@0 9 import android.os.Bundle;
michael@0 10
michael@0 11 public class DynamicToolbar {
michael@0 12 private static final String STATE_ENABLED = "dynamic_toolbar";
michael@0 13 private static final String CHROME_PREF = "browser.chrome.dynamictoolbar";
michael@0 14
michael@0 15 // DynamicToolbar is enabled iff prefEnabled is true *and* accessibilityEnabled is false,
michael@0 16 // so it is disabled by default on startup. We do not enable it until we explicitly get
michael@0 17 // the pref from Gecko telling us to turn it on.
michael@0 18 private volatile boolean prefEnabled;
michael@0 19 private boolean accessibilityEnabled;
michael@0 20
michael@0 21 private final int prefObserverId;
michael@0 22 private final EnumSet<PinReason> pinFlags = EnumSet.noneOf(PinReason.class);
michael@0 23 private LayerView layerView;
michael@0 24 private OnEnabledChangedListener enabledChangedListener;
michael@0 25
michael@0 26 public enum PinReason {
michael@0 27 RELAYOUT,
michael@0 28 ACTION_MODE
michael@0 29 }
michael@0 30
michael@0 31 public enum VisibilityTransition {
michael@0 32 IMMEDIATE,
michael@0 33 ANIMATE
michael@0 34 }
michael@0 35
michael@0 36 /**
michael@0 37 * Listener for changes to the dynamic toolbar's enabled state.
michael@0 38 */
michael@0 39 public interface OnEnabledChangedListener {
michael@0 40 /**
michael@0 41 * This callback is executed on the UI thread.
michael@0 42 */
michael@0 43 public void onEnabledChanged(boolean enabled);
michael@0 44 }
michael@0 45
michael@0 46 public DynamicToolbar() {
michael@0 47 // Listen to the dynamic toolbar pref
michael@0 48 prefObserverId = PrefsHelper.getPref(CHROME_PREF, new PrefHandler());
michael@0 49 }
michael@0 50
michael@0 51 public void destroy() {
michael@0 52 PrefsHelper.removeObserver(prefObserverId);
michael@0 53 }
michael@0 54
michael@0 55 public void setLayerView(LayerView layerView) {
michael@0 56 ThreadUtils.assertOnUiThread();
michael@0 57
michael@0 58 this.layerView = layerView;
michael@0 59 }
michael@0 60
michael@0 61 public void setEnabledChangedListener(OnEnabledChangedListener listener) {
michael@0 62 ThreadUtils.assertOnUiThread();
michael@0 63
michael@0 64 enabledChangedListener = listener;
michael@0 65 }
michael@0 66
michael@0 67 public void onSaveInstanceState(Bundle outState) {
michael@0 68 ThreadUtils.assertOnUiThread();
michael@0 69
michael@0 70 outState.putBoolean(STATE_ENABLED, prefEnabled);
michael@0 71 }
michael@0 72
michael@0 73 public void onRestoreInstanceState(Bundle savedInstanceState) {
michael@0 74 ThreadUtils.assertOnUiThread();
michael@0 75
michael@0 76 if (savedInstanceState != null) {
michael@0 77 prefEnabled = savedInstanceState.getBoolean(STATE_ENABLED);
michael@0 78 }
michael@0 79 }
michael@0 80
michael@0 81 public boolean isEnabled() {
michael@0 82 ThreadUtils.assertOnUiThread();
michael@0 83
michael@0 84 return prefEnabled && !accessibilityEnabled;
michael@0 85 }
michael@0 86
michael@0 87 public void setAccessibilityEnabled(boolean enabled) {
michael@0 88 ThreadUtils.assertOnUiThread();
michael@0 89
michael@0 90 if (accessibilityEnabled == enabled) {
michael@0 91 return;
michael@0 92 }
michael@0 93
michael@0 94 // Disable the dynamic toolbar when accessibility features are enabled,
michael@0 95 // and re-read the preference when they're disabled.
michael@0 96 accessibilityEnabled = enabled;
michael@0 97 if (prefEnabled) {
michael@0 98 triggerEnabledListener();
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 public void setVisible(boolean visible, VisibilityTransition transition) {
michael@0 103 ThreadUtils.assertOnUiThread();
michael@0 104
michael@0 105 if (layerView == null) {
michael@0 106 return;
michael@0 107 }
michael@0 108
michael@0 109 final boolean immediate = transition.equals(VisibilityTransition.IMMEDIATE);
michael@0 110 if (visible) {
michael@0 111 layerView.getLayerMarginsAnimator().showMargins(immediate);
michael@0 112 } else {
michael@0 113 layerView.getLayerMarginsAnimator().hideMargins(immediate);
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 public void setPinned(boolean pinned, PinReason reason) {
michael@0 118 ThreadUtils.assertOnUiThread();
michael@0 119
michael@0 120 if (layerView == null) {
michael@0 121 return;
michael@0 122 }
michael@0 123
michael@0 124 if (pinned) {
michael@0 125 pinFlags.add(reason);
michael@0 126 } else {
michael@0 127 pinFlags.remove(reason);
michael@0 128 }
michael@0 129
michael@0 130 layerView.getLayerMarginsAnimator().setMarginsPinned(!pinFlags.isEmpty());
michael@0 131 }
michael@0 132
michael@0 133 private void triggerEnabledListener() {
michael@0 134 if (enabledChangedListener != null) {
michael@0 135 enabledChangedListener.onEnabledChanged(isEnabled());
michael@0 136 }
michael@0 137 }
michael@0 138
michael@0 139 private class PrefHandler extends PrefHandlerBase {
michael@0 140 @Override
michael@0 141 public void prefValue(String pref, boolean value) {
michael@0 142 if (value == prefEnabled) {
michael@0 143 return;
michael@0 144 }
michael@0 145
michael@0 146 prefEnabled = value;
michael@0 147
michael@0 148 ThreadUtils.postToUiThread(new Runnable() {
michael@0 149 @Override
michael@0 150 public void run() {
michael@0 151 // If accessibility is enabled, the dynamic toolbar is
michael@0 152 // forced to be off.
michael@0 153 if (!accessibilityEnabled) {
michael@0 154 triggerEnabledListener();
michael@0 155 }
michael@0 156 }
michael@0 157 });
michael@0 158 }
michael@0 159
michael@0 160 @Override
michael@0 161 public boolean isObserver() {
michael@0 162 // We want to be notified of changes to be able to switch mode
michael@0 163 // without restarting.
michael@0 164 return true;
michael@0 165 }
michael@0 166 }
michael@0 167 }

mercurial