[go: nahoru, domu]

blob: 0f79f6fac7b71270ecc957a65e37dbbced20bbba [file] [log] [blame]
tedchocb026e9942016-11-16 01:36:441// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Theresa Wellington33a92382020-06-19 19:23:005package org.chromium.chrome.browser.browser_controls;
tedchocb026e9942016-11-16 01:36:446
7import android.os.Handler;
tedchocb026e9942016-11-16 01:36:448import android.os.SystemClock;
9
Henrique Nakashimaae72a582019-11-13 16:56:1610import androidx.annotation.VisibleForTesting;
11
jonrosse6852d52019-07-12 17:42:4912import org.chromium.base.CommandLine;
Theresa Wellington66957512020-01-08 03:24:2513import org.chromium.base.supplier.ObservableSupplier;
14import org.chromium.base.supplier.Supplier;
Dave Tapuskafc4adbae2020-12-02 22:08:5915import org.chromium.cc.input.BrowserControlsState;
Henrique Nakashimab64e1782020-03-06 06:21:1816import org.chromium.chrome.browser.flags.ChromeSwitches;
Evan Staded26b27c2020-02-07 19:46:2017import org.chromium.components.browser_ui.util.BrowserControlsVisibilityDelegate;
Matt Jones672eebc2019-10-14 23:03:0018import org.chromium.ui.util.TokenHolder;
tedchocb026e9942016-11-16 01:36:4419
tedchocb026e9942016-11-16 01:36:4420/**
21 * Determines the desired visibility of the browser controls based on the current state of the
22 * running activity.
23 */
24public class BrowserStateBrowserControlsVisibilityDelegate
Ted Chocd4dab4b2019-12-09 21:41:2625 extends BrowserControlsVisibilityDelegate {
tedchocb026e9942016-11-16 01:36:4426 /** Minimum duration (in milliseconds) that the controls are shown when requested. */
Pavel Shmakov760c5a942018-10-12 17:09:5127 @VisibleForTesting
28 static final long MINIMUM_SHOW_DURATION_MS = 3000;
tedchocb026e9942016-11-16 01:36:4429
30 private static boolean sDisableOverridesForTesting;
31
Pavel Shmakov760c5a942018-10-12 17:09:5132 private final TokenHolder mTokenHolder;
tedchocb026e9942016-11-16 01:36:4433
Pavel Shmakov760c5a942018-10-12 17:09:5134 private final Handler mHandler = new Handler();
tedchocb026e9942016-11-16 01:36:4435
Jinsuk Kimb7177202019-06-13 12:52:3336 /** Predicate that tells if we're in persistent fullscreen mode. */
37 private final Supplier<Boolean> mPersistentFullscreenMode;
38
Pavel Shmakov760c5a942018-10-12 17:09:5139 private long mCurrentShowingStartTime;
tedchocb026e9942016-11-16 01:36:4440
41 /**
42 * Constructs a BrowserControlsVisibilityDelegate designed to deal with overrides driven by
43 * the browser UI (as opposed to the state of the tab).
tedchocc5be519e2017-01-20 20:54:1744 *
Jinsuk Kimb7177202019-06-13 12:52:3345 * @param persistentFullscreenMode Predicate that tells if we're in persistent fullscreen mode.
tedchocb026e9942016-11-16 01:36:4446 */
Jinsuk Kimb7177202019-06-13 12:52:3347 public BrowserStateBrowserControlsVisibilityDelegate(
Ted Chocd4dab4b2019-12-09 21:41:2648 ObservableSupplier<Boolean> persistentFullscreenMode) {
49 super(BrowserControlsState.BOTH);
50 mTokenHolder = new TokenHolder(this::updateVisibilityConstraints);
Jinsuk Kimb7177202019-06-13 12:52:3351 mPersistentFullscreenMode = persistentFullscreenMode;
Ted Chocd4dab4b2019-12-09 21:41:2652 persistentFullscreenMode.addObserver((persistentMode) -> updateVisibilityConstraints());
53 updateVisibilityConstraints();
tedchocb026e9942016-11-16 01:36:4454 }
55
56 private void ensureControlsVisibleForMinDuration() {
jonrosse6852d52019-07-12 17:42:4957 // Do not lock the controls as visible. Such as in testing.
Matt Jones672eebc2019-10-14 23:03:0058 if (CommandLine.getInstance().hasSwitch(ChromeSwitches.DISABLE_MINIMUM_SHOW_DURATION)) {
jonrosse6852d52019-07-12 17:42:4959 return;
Matt Jones672eebc2019-10-14 23:03:0060 }
Pavel Shmakov760c5a942018-10-12 17:09:5161 if (mHandler.hasMessages(0)) return; // Messages sent via post/postDelayed have what=0
tedchocb026e9942016-11-16 01:36:4462
Pavel Shmakov760c5a942018-10-12 17:09:5163 long currentShowingTime = SystemClock.uptimeMillis() - mCurrentShowingStartTime;
64 if (currentShowingTime >= MINIMUM_SHOW_DURATION_MS) return;
tedchocb026e9942016-11-16 01:36:4465
Pavel Shmakov760c5a942018-10-12 17:09:5166 final int temporaryToken = mTokenHolder.acquireToken();
Theresa Wellington33a92382020-06-19 19:23:0067 mHandler.postDelayed(()
68 -> mTokenHolder.releaseToken(temporaryToken),
Pavel Shmakov760c5a942018-10-12 17:09:5169 MINIMUM_SHOW_DURATION_MS - currentShowingTime);
tedchocb026e9942016-11-16 01:36:4470 }
71
72 /**
73 * Trigger a temporary showing of the browser controls.
74 */
75 public void showControlsTransient() {
Pavel Shmakov760c5a942018-10-12 17:09:5176 if (!mTokenHolder.hasTokens()) mCurrentShowingStartTime = SystemClock.uptimeMillis();
tedchocb026e9942016-11-16 01:36:4477 ensureControlsVisibleForMinDuration();
78 }
79
80 /**
81 * Trigger a permanent showing of the browser controls until requested otherwise.
82 *
83 * @return The token that determines whether the requester still needs persistent controls to
84 * be present on the screen.
Pavel Shmakov760c5a942018-10-12 17:09:5185 * @see #releasePersistentShowingToken(int)
tedchocb026e9942016-11-16 01:36:4486 */
87 public int showControlsPersistent() {
Pavel Shmakov760c5a942018-10-12 17:09:5188 if (!mTokenHolder.hasTokens()) mCurrentShowingStartTime = SystemClock.uptimeMillis();
89 return mTokenHolder.acquireToken();
tedchocb026e9942016-11-16 01:36:4490 }
91
92 /**
93 * Same behavior as {@link #showControlsPersistent()} but also handles removing a previously
94 * requested token if necessary.
95 *
96 * @param oldToken The old fullscreen token to be cleared.
97 * @return The fullscreen token as defined in {@link #showControlsPersistent()}.
98 */
99 public int showControlsPersistentAndClearOldToken(int oldToken) {
100 int newToken = showControlsPersistent();
Pavel Shmakov760c5a942018-10-12 17:09:51101 mTokenHolder.releaseToken(oldToken);
tedchocb026e9942016-11-16 01:36:44102 return newToken;
103 }
104
105 /**
106 * Notify the manager that the browser controls are no longer required for the given token.
107 *
108 * @param token The fullscreen token returned from {@link #showControlsPersistent()}.
109 */
Pavel Shmakov760c5a942018-10-12 17:09:51110 public void releasePersistentShowingToken(int token) {
111 if (mTokenHolder.containsOnly(token)) {
tedchocb026e9942016-11-16 01:36:44112 ensureControlsVisibleForMinDuration();
113 }
Pavel Shmakov760c5a942018-10-12 17:09:51114 mTokenHolder.releaseToken(token);
tedchocb026e9942016-11-16 01:36:44115 }
116
Ted Chocd4dab4b2019-12-09 21:41:26117 @BrowserControlsState
118 private int calculateVisibilityConstraints() {
119 if (mPersistentFullscreenMode.get()) {
120 return BrowserControlsState.HIDDEN;
121 } else if (mTokenHolder.hasTokens() && !sDisableOverridesForTesting) {
122 return BrowserControlsState.SHOWN;
123 }
124 return BrowserControlsState.BOTH;
tedchocb026e9942016-11-16 01:36:44125 }
126
Ted Chocd4dab4b2019-12-09 21:41:26127 private void updateVisibilityConstraints() {
128 set(calculateVisibilityConstraints());
tedchocb026e9942016-11-16 01:36:44129 }
130
131 /**
132 * Disable any browser visibility overrides for testing.
133 */
134 public static void disableForTesting() {
135 sDisableOverridesForTesting = true;
136 }
Pavel Shmakov760c5a942018-10-12 17:09:51137
138 /**
139 * Performs clean-up.
140 */
141 public void destroy() {
142 mHandler.removeCallbacksAndMessages(null);
143 }
tedchocb026e9942016-11-16 01:36:44144}