1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/android/overscroll_controller_android.h"
#include "base/android/build_info.h"
#include "base/command_line.h"
#include "cc/layers/layer.h"
#include "cc/output/compositor_frame_metadata.h"
#include "content/browser/android/edge_effect.h"
#include "content/browser/android/edge_effect_l.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/input/did_overscroll_params.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/common/content_switches.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
#include "ui/android/resources/resource_manager.h"
#include "ui/base/android/window_android_compositor.h"
#include "ui/base/l10n/l10n_util_android.h"
namespace content {
namespace {
// Used for conditional creation of EdgeEffect types for the overscroll glow.
const int kAndroidLSDKVersion = 21;
// Default offset in dips from the top of the view beyond which the refresh
// action will be activated.
const int kDefaultRefreshDragTargetDips = 64;
bool IsAndroidLOrNewer() {
static bool android_l_or_newer =
base::android::BuildInfo::GetInstance()->sdk_int() >= kAndroidLSDKVersion;
return android_l_or_newer;
}
bool ShouldDisableRefreshWhenGlowActive() {
// Suppressing refresh detection when the glow is still animating prevents
// visual confusion and accidental activation after repeated scrolls. However,
// only the L effect is guaranteed to be both sufficiently brief and prominent
// to provide a meaningful "wait" signal. The refresh effect on previous
// Android releases can be quite faint, depending on the OEM-supplied
// overscroll resources, and lasts nearly twice as long.
return IsAndroidLOrNewer();
}
scoped_ptr<EdgeEffectBase> CreateGlowEdgeEffect(
ui::ResourceManager* resource_manager,
float dpi_scale) {
DCHECK(resource_manager);
if (IsAndroidLOrNewer())
return scoped_ptr<EdgeEffectBase>(new EdgeEffectL(resource_manager));
return scoped_ptr<EdgeEffectBase>(
new EdgeEffect(resource_manager, dpi_scale));
}
scoped_ptr<OverscrollGlow> CreateGlowEffect(OverscrollGlowClient* client,
float dpi_scale) {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableOverscrollEdgeEffect)) {
return nullptr;
}
return make_scoped_ptr(new OverscrollGlow(client));
}
scoped_ptr<OverscrollRefresh> CreateRefreshEffect(
ui::WindowAndroidCompositor* compositor,
OverscrollRefreshClient* client,
float dpi_scale) {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisablePullToRefreshEffect)) {
return nullptr;
}
return make_scoped_ptr(new OverscrollRefresh(
&compositor->GetResourceManager(), client,
kDefaultRefreshDragTargetDips * dpi_scale, l10n_util::IsLayoutRtl()));
}
} // namespace
OverscrollControllerAndroid::OverscrollControllerAndroid(
WebContents* web_contents,
ui::WindowAndroidCompositor* compositor,
float dpi_scale)
: compositor_(compositor),
dpi_scale_(dpi_scale),
enabled_(true),
glow_effect_(CreateGlowEffect(this, dpi_scale)),
refresh_effect_(CreateRefreshEffect(compositor, this, dpi_scale)),
triggered_refresh_active_(false),
is_fullscreen_(static_cast<WebContentsImpl*>(web_contents)
->IsFullscreenForCurrentTab()) {
DCHECK(web_contents);
DCHECK(compositor);
if (refresh_effect_)
Observe(web_contents);
}
OverscrollControllerAndroid::~OverscrollControllerAndroid() {
}
bool OverscrollControllerAndroid::WillHandleGestureEvent(
const blink::WebGestureEvent& event) {
if (!enabled_)
return false;
if (!refresh_effect_)
return false;
// Suppress refresh detection for fullscreen HTML5 scenarios, e.g., video.
if (is_fullscreen_)
return false;
if (glow_effect_) {
if (glow_effect_->IsActive() && ShouldDisableRefreshWhenGlowActive())
return false;
}
bool handled = false;
bool maybe_needs_animate = false;
switch (event.type) {
case blink::WebInputEvent::GestureScrollBegin:
refresh_effect_->OnScrollBegin();
break;
case blink::WebInputEvent::GestureScrollUpdate: {
gfx::Vector2dF scroll_delta(event.data.scrollUpdate.deltaX,
event.data.scrollUpdate.deltaY);
scroll_delta.Scale(dpi_scale_);
maybe_needs_animate = true;
handled = refresh_effect_->WillHandleScrollUpdate(scroll_delta);
} break;
case blink::WebInputEvent::GestureScrollEnd:
refresh_effect_->OnScrollEnd(gfx::Vector2dF());
maybe_needs_animate = true;
break;
case blink::WebInputEvent::GestureFlingStart: {
gfx::Vector2dF scroll_velocity(event.data.flingStart.velocityX,
event.data.flingStart.velocityY);
scroll_velocity.Scale(dpi_scale_);
refresh_effect_->OnScrollEnd(scroll_velocity);
if (refresh_effect_->IsActive()) {
// TODO(jdduke): Figure out a cleaner way of suppressing a fling.
// It's important that the any downstream code sees a scroll-ending
// event (in this case GestureFlingStart) if it has seen a scroll begin.
// Thus, we cannot simply consume the fling. Changing the event type to
// a GestureScrollEnd might work in practice, but could lead to
// unexpected results. For now, simply truncate the fling velocity, but
// not to zero as downstream code may not expect a zero-velocity fling.
blink::WebGestureEvent& modified_event =
const_cast<blink::WebGestureEvent&>(event);
modified_event.data.flingStart.velocityX = .01f;
modified_event.data.flingStart.velocityY = .01f;
}
maybe_needs_animate = true;
} break;
case blink::WebInputEvent::GesturePinchBegin:
refresh_effect_->ReleaseWithoutActivation();
break;
default:
break;
}
if (maybe_needs_animate && refresh_effect_->IsActive())
SetNeedsAnimate();
return handled;
}
void OverscrollControllerAndroid::OnGestureEventAck(
const blink::WebGestureEvent& event,
InputEventAckState ack_result) {
if (!enabled_)
return;
// The overscroll effect requires an explicit release signal that may not be
// sent from the renderer compositor.
if (event.type == blink::WebInputEvent::GestureScrollEnd ||
event.type == blink::WebInputEvent::GestureFlingStart) {
OnOverscrolled(DidOverscrollParams());
}
if (event.type == blink::WebInputEvent::GestureScrollUpdate &&
refresh_effect_) {
// The effect should only be allowed if both the causal touch events go
// unconsumed and the generated scroll events go unconsumed.
bool consumed = ack_result == INPUT_EVENT_ACK_STATE_CONSUMED ||
event.data.scrollUpdate.previousUpdateInSequencePrevented;
refresh_effect_->OnScrollUpdateAck(consumed);
}
}
void OverscrollControllerAndroid::OnOverscrolled(
const DidOverscrollParams& params) {
if (!enabled_)
return;
if (refresh_effect_ && (refresh_effect_->IsActive() ||
refresh_effect_->IsAwaitingScrollUpdateAck())) {
// An active (or potentially active) refresh effect should always pre-empt
// the passive glow effect.
return;
}
if (glow_effect_ &&
glow_effect_->OnOverscrolled(
base::TimeTicks::Now(),
gfx::ScaleVector2d(params.accumulated_overscroll, dpi_scale_),
gfx::ScaleVector2d(params.latest_overscroll_delta, dpi_scale_),
gfx::ScaleVector2d(params.current_fling_velocity, dpi_scale_),
gfx::ScaleVector2d(
params.causal_event_viewport_point.OffsetFromOrigin(),
dpi_scale_))) {
SetNeedsAnimate();
}
}
bool OverscrollControllerAndroid::Animate(base::TimeTicks current_time,
cc::Layer* parent_layer) {
DCHECK(parent_layer);
if (!enabled_)
return false;
bool needs_animate = false;
if (refresh_effect_)
needs_animate |= refresh_effect_->Animate(current_time, parent_layer);
if (glow_effect_)
needs_animate |= glow_effect_->Animate(current_time, parent_layer);
return needs_animate;
}
void OverscrollControllerAndroid::OnFrameMetadataUpdated(
const cc::CompositorFrameMetadata& frame_metadata) {
if (!refresh_effect_ && !glow_effect_)
return;
const float scale_factor =
frame_metadata.page_scale_factor * frame_metadata.device_scale_factor;
gfx::SizeF viewport_size =
gfx::ScaleSize(frame_metadata.scrollable_viewport_size, scale_factor);
gfx::SizeF content_size =
gfx::ScaleSize(frame_metadata.root_layer_size, scale_factor);
gfx::Vector2dF content_scroll_offset =
gfx::ScaleVector2d(frame_metadata.root_scroll_offset, scale_factor);
if (refresh_effect_) {
refresh_effect_->UpdateDisplay(viewport_size, content_scroll_offset,
frame_metadata.root_overflow_y_hidden);
}
if (glow_effect_) {
glow_effect_->UpdateDisplay(viewport_size, content_size,
content_scroll_offset);
}
}
void OverscrollControllerAndroid::Enable() {
enabled_ = true;
}
void OverscrollControllerAndroid::Disable() {
if (!enabled_)
return;
enabled_ = false;
if (!enabled_) {
if (refresh_effect_)
refresh_effect_->Reset();
if (glow_effect_)
glow_effect_->Reset();
}
}
void OverscrollControllerAndroid::DidNavigateMainFrame(
const LoadCommittedDetails& details,
const FrameNavigateParams& params) {
// Once the main frame has navigated, there's little need to further animate
// the reload effect. Note that the effect will naturally time out should the
// reload be interruped for any reason.
triggered_refresh_active_ = false;
}
void OverscrollControllerAndroid::DidToggleFullscreenModeForTab(
bool entered_fullscreen) {
if (is_fullscreen_ == entered_fullscreen)
return;
is_fullscreen_ = entered_fullscreen;
if (is_fullscreen_)
refresh_effect_->ReleaseWithoutActivation();
}
void OverscrollControllerAndroid::TriggerRefresh() {
triggered_refresh_active_ = false;
if (!web_contents())
return;
triggered_refresh_active_ = true;
RecordAction(base::UserMetricsAction("MobilePullGestureReload"));
web_contents()->GetController().Reload(true);
}
bool OverscrollControllerAndroid::IsStillRefreshing() const {
return triggered_refresh_active_;
}
scoped_ptr<EdgeEffectBase> OverscrollControllerAndroid::CreateEdgeEffect() {
return CreateGlowEdgeEffect(&compositor_->GetResourceManager(), dpi_scale_);
}
void OverscrollControllerAndroid::SetNeedsAnimate() {
compositor_->SetNeedsAnimate();
}
} // namespace content
|