Skip to content

Commit

Permalink
[Android] Add a native pull-to-refresh overscroll effect
Browse files Browse the repository at this point in the history
Add a restricted pull-to-refresh effect to Android. The effect is
triggered only when 1) the scroll starts when the page has no vertical
offset, 2) the scroll direction is upward and 3) the initial scroll
is not consumed by the page. Page reloads are triggered only when
the user pulls and releases beyond a certain threshold.

BUG=428429

Review URL: https://codereview.chromium.org/679493002

Cr-Commit-Position: refs/heads/master@{#304320}
  • Loading branch information
jdduke authored and Commit bot committed Nov 15, 2014
1 parent 2140408 commit 9db1b9d
Show file tree
Hide file tree
Showing 44 changed files with 1,495 additions and 415 deletions.
3 changes: 2 additions & 1 deletion android_webview/java_library_common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ LOCAL_SRC_FILES += \
$(call all-java-files-under, java/generated_src)

# Java files generated from .template rules. This list should match list of java dependencies in
# android_webview/android_webview.gyp
# android_webview/libwebviewchromium.gyp
LOCAL_GENERATED_SOURCES := \
$(call intermediates-dir-for,GYP,shared)/enums/bitmap_format_java/org/chromium/ui/gfx/BitmapFormat.java \
$(call intermediates-dir-for,GYP,shared)/enums/cert_verify_status_android_java/org/chromium/net/CertVerifyStatusAndroid.java \
Expand All @@ -63,6 +63,7 @@ $(call intermediates-dir-for,GYP,shared)/enums/base_java_library_load_from_apk_s
$(call intermediates-dir-for,GYP,shared)/enums/base_java_memory_pressure_level/org/chromium/base/MemoryPressureLevel.java \
$(call intermediates-dir-for,GYP,shared)/enums/media_android_imageformat/org/chromium/media/AndroidImageFormat.java \
$(call intermediates-dir-for,GYP,shared)/enums/page_transition_types_java/org/chromium/ui/base/PageTransition.java \
$(call intermediates-dir-for,GYP,shared)/enums/system_ui_resource_type_java/org/chromium/ui/base/SystemUIResourceType.java \
$(call intermediates-dir-for,GYP,shared)/templates/net_errors_java/org/chromium/net/NetError.java \

# content dependencies on java components that are provided by the system on
Expand Down
1 change: 1 addition & 0 deletions android_webview/libwebviewchromium.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'../net/net.gyp:private_key_types_java',
'../ui/android/ui_android.gyp:bitmap_format_java',
'../ui/android/ui_android.gyp:page_transition_types_java',
'../ui/android/ui_android.gyp:system_ui_resource_type_java',
'../ui/android/ui_android.gyp:window_open_disposition_java',
],
# Enable feedback-directed optimisation for the library when building in
Expand Down
33 changes: 33 additions & 0 deletions content/browser/android/animation_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.

#ifndef CONTENT_BROWSER_ANDROID_ANIMATION_UTILS_H_
#define CONTENT_BROWSER_ANDROID_ANIMATION_UTILS_H_

namespace content {

template <typename T>
T Lerp(T a, T b, T t) {
return a + (b - a) * t;
}

template <typename T>
T Clamp(T value, T low, T high) {
return value < low ? low : (value > high ? high : value);
}

template <typename T>
T Damp(T input, T factor) {
T result;
if (factor == 1) {
result = 1 - (1 - input) * (1 - input);
} else {
result = 1 - std::pow(1 - input, 2 * factor);
}
return result;
}

} // namespace content

#endif // CONTENT_BROWSER_ANDROID_ANIMATION_UTILS_H_
8 changes: 6 additions & 2 deletions content/browser/android/browser_jni_registrar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "content/browser/android/interstitial_page_delegate_android.h"
#include "content/browser/android/load_url_params.h"
#include "content/browser/android/popup_touch_handle_drawable.h"
#include "content/browser/android/system_ui_resource_manager_impl.h"
#include "content/browser/android/tracing_controller_android.h"
#include "content/browser/android/web_contents_observer_android.h"
#include "content/browser/device_sensors/sensor_manager_android.h"
Expand Down Expand Up @@ -61,8 +62,9 @@ base::android::RegistrationMethod kContentRegisteredMethods[] = {
{"DateTimePickerAndroid", content::RegisterDateTimeChooserAndroid},
{"DownloadControllerAndroidImpl",
content::DownloadControllerAndroidImpl::RegisterDownloadController},
{"GamepadList", content::GamepadPlatformDataFetcherAndroid::
RegisterGamepadPlatformDataFetcherAndroid},
{"GamepadList",
content::GamepadPlatformDataFetcherAndroid::
RegisterGamepadPlatformDataFetcherAndroid},
{"HandleViewResources",
content::CompositedTouchHandleDrawable::RegisterHandleViewResources},
{"InterstitialPageDelegateAndroid",
Expand All @@ -87,6 +89,8 @@ base::android::RegistrationMethod kContentRegisteredMethods[] = {
{"ServiceRegistryAndroid", content::ServiceRegistryAndroid::Register},
{"SpeechRecognizerImplAndroid",
content::SpeechRecognizerImplAndroid::RegisterSpeechRecognizer},
{"SystemUIResourceManagerImpl",
content::SystemUIResourceManagerImpl::RegisterUIResources},
{"TimeZoneMonitorAndroid", content::TimeZoneMonitorAndroid::Register},
{"TouchEventSynthesizer",
content::SyntheticGestureTargetAndroid::RegisterTouchEventSynthesizer},
Expand Down
6 changes: 3 additions & 3 deletions content/browser/android/content_view_core_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void ContentViewCoreImpl::RenderViewHostChanged(RenderViewHost* old_host,
}

RenderWidgetHostViewAndroid*
ContentViewCoreImpl::GetRenderWidgetHostViewAndroid() {
ContentViewCoreImpl::GetRenderWidgetHostViewAndroid() const {
RenderWidgetHostView* rwhv = NULL;
if (web_contents_) {
rwhv = web_contents_->GetRenderWidgetHostView();
Expand Down Expand Up @@ -798,8 +798,8 @@ ui::WindowAndroid* ContentViewCoreImpl::GetWindowAndroid() const {
return window_android_;
}

scoped_refptr<cc::Layer> ContentViewCoreImpl::GetLayer() const {
return root_layer_.get();
const scoped_refptr<cc::Layer>& ContentViewCoreImpl::GetLayer() const {
return root_layer_;
}

// ----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions content/browser/android/content_view_core_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ContentViewCoreImpl : public ContentViewCore,
virtual WebContents* GetWebContents() const override;
virtual ui::ViewAndroid* GetViewAndroid() const override;
virtual ui::WindowAndroid* GetWindowAndroid() const override;
virtual scoped_refptr<cc::Layer> GetLayer() const override;
virtual const scoped_refptr<cc::Layer>& GetLayer() const override;
virtual void ShowPastePopup(int x, int y) override;
virtual void GetScaledContentBitmap(
float scale,
Expand Down Expand Up @@ -295,7 +295,7 @@ class ContentViewCoreImpl : public ContentViewCore,

void InitWebContents();

RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid() const;

blink::WebGestureEvent MakeGestureEvent(
blink::WebInputEvent::Type type, int64 time_ms, float x, float y) const;
Expand Down
38 changes: 7 additions & 31 deletions content/browser/android/edge_effect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "cc/layers/layer.h"
#include "cc/layers/ui_resource_layer.h"
#include "content/browser/android/animation_utils.h"
#include "ui/base/android/system_ui_resource_manager.h"

namespace content {
Expand Down Expand Up @@ -52,32 +53,11 @@ const int kVelocityGlowFactor = 12;
const float kEdgeHeightAtMdpi = 12.f;
const float kGlowHeightAtMdpi = 128.f;

template <typename T>
T Lerp(T a, T b, T t) {
return a + (b - a) * t;
}

template <typename T>
T Clamp(T value, T low, T high) {
return value < low ? low : (value > high ? high : value);
}

template <typename T>
T Damp(T input, T factor) {
T result;
if (factor == 1) {
result = 1 - (1 - input) * (1 - input);
} else {
result = 1 - std::pow(1 - input, 2 * factor);
}
return result;
}

} // namespace

class EdgeEffect::EffectLayer {
public:
EffectLayer(ui::SystemUIResourceManager::ResourceType resource_type,
EffectLayer(ui::SystemUIResourceType resource_type,
ui::SystemUIResourceManager* resource_manager)
: ui_resource_layer_(cc::UIResourceLayer::Create()),
resource_type_(resource_type),
Expand Down Expand Up @@ -108,18 +88,16 @@ class EdgeEffect::EffectLayer {
}

scoped_refptr<cc::UIResourceLayer> ui_resource_layer_;
ui::SystemUIResourceManager::ResourceType resource_type_;
ui::SystemUIResourceType resource_type_;
ui::SystemUIResourceManager* resource_manager_;

DISALLOW_COPY_AND_ASSIGN(EffectLayer);
};

EdgeEffect::EdgeEffect(ui::SystemUIResourceManager* resource_manager,
float device_scale_factor)
: edge_(new EffectLayer(ui::SystemUIResourceManager::OVERSCROLL_EDGE,
resource_manager)),
glow_(new EffectLayer(ui::SystemUIResourceManager::OVERSCROLL_GLOW,
resource_manager)),
: edge_(new EffectLayer(ui::OVERSCROLL_EDGE, resource_manager)),
glow_(new EffectLayer(ui::OVERSCROLL_GLOW, resource_manager)),
base_edge_height_(kEdgeHeightAtMdpi * device_scale_factor),
base_glow_height_(kGlowHeightAtMdpi * device_scale_factor),
edge_alpha_(0),
Expand Down Expand Up @@ -362,10 +340,8 @@ void EdgeEffect::SetParent(cc::Layer* parent) {
void EdgeEffect::PreloadResources(
ui::SystemUIResourceManager* resource_manager) {
DCHECK(resource_manager);
resource_manager->PreloadResource(
ui::SystemUIResourceManager::OVERSCROLL_EDGE);
resource_manager->PreloadResource(
ui::SystemUIResourceManager::OVERSCROLL_GLOW);
resource_manager->PreloadResource(ui::OVERSCROLL_EDGE);
resource_manager->PreloadResource(ui::OVERSCROLL_GLOW);
}

} // namespace content
25 changes: 2 additions & 23 deletions content/browser/android/edge_effect_l.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "content/browser/android/edge_effect_l.h"

#include "cc/layers/ui_resource_layer.h"
#include "content/browser/android/animation_utils.h"
#include "ui/base/android/system_ui_resource_manager.h"

namespace content {
Expand Down Expand Up @@ -40,29 +41,7 @@ const float kPullDistanceAlphaGlowFactor = 0.8f;

const int kVelocityGlowFactor = 6;

const ui::SystemUIResourceManager::ResourceType kResourceType =
ui::SystemUIResourceManager::OVERSCROLL_GLOW_L;

template <typename T>
T Lerp(T a, T b, T t) {
return a + (b - a) * t;
}

template <typename T>
T Clamp(T value, T low, T high) {
return value < low ? low : (value > high ? high : value);
}

template <typename T>
T Damp(T input, T factor) {
T result;
if (factor == 1) {
result = 1 - (1 - input) * (1 - input);
} else {
result = 1 - std::pow(1 - input, 2 * factor);
}
return result;
}
const ui::SystemUIResourceType kResourceType = ui::OVERSCROLL_GLOW_L;

} // namespace

Expand Down
Loading

0 comments on commit 9db1b9d

Please sign in to comment.