Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / android / view_configuration.cc
blobdf9581be135c4cf7656a58719609f4dbba92e383
1 // Copyright 2014 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.
5 #include "ui/gfx/android/view_configuration.h"
7 #include "base/android/jni_android.h"
8 #include "base/lazy_instance.h"
9 #include "base/threading/non_thread_safe.h"
10 #include "jni/ViewConfigurationHelper_jni.h"
12 using base::android::AttachCurrentThread;
13 using base::android::GetApplicationContext;
15 namespace gfx {
17 namespace {
19 struct ViewConfigurationData {
20 ViewConfigurationData()
21 : double_tap_timeout_in_ms_(0),
22 long_press_timeout_in_ms_(0),
23 tap_timeout_in_ms_(0),
24 scroll_friction_(1.f),
25 max_fling_velocity_in_dips_s_(0),
26 min_fling_velocity_in_dips_s_(0),
27 touch_slop_in_dips_(0),
28 double_tap_slop_in_dips_(0),
29 min_scaling_span_in_dips_(0),
30 min_scaling_touch_major_in_dips_(0) {
31 JNIEnv* env = AttachCurrentThread();
32 j_view_configuration_helper_.Reset(
33 Java_ViewConfigurationHelper_createWithListener(
34 env, base::android::GetApplicationContext()));
36 double_tap_timeout_in_ms_ =
37 Java_ViewConfigurationHelper_getDoubleTapTimeout(env);
38 long_press_timeout_in_ms_ =
39 Java_ViewConfigurationHelper_getLongPressTimeout(env);
40 tap_timeout_in_ms_ = Java_ViewConfigurationHelper_getTapTimeout(env);
41 scroll_friction_ = Java_ViewConfigurationHelper_getScrollFriction(env);
43 jobject obj = j_view_configuration_helper_.obj();
44 Update(Java_ViewConfigurationHelper_getMaximumFlingVelocity(env, obj),
45 Java_ViewConfigurationHelper_getMinimumFlingVelocity(env, obj),
46 Java_ViewConfigurationHelper_getTouchSlop(env, obj),
47 Java_ViewConfigurationHelper_getDoubleTapSlop(env, obj),
48 Java_ViewConfigurationHelper_getMinScalingSpan(env, obj),
49 Java_ViewConfigurationHelper_getMinScalingTouchMajor(env, obj));
52 ~ViewConfigurationData() {}
54 void SynchronizedUpdate(float maximum_fling_velocity,
55 float minimum_fling_velocity,
56 float touch_slop,
57 float double_tap_slop,
58 float min_scaling_span,
59 float min_scaling_touch_major) {
60 base::AutoLock autolock(lock_);
61 Update(maximum_fling_velocity, minimum_fling_velocity, touch_slop,
62 double_tap_slop, min_scaling_span, min_scaling_touch_major);
65 int double_tap_timeout_in_ms() const { return double_tap_timeout_in_ms_; }
66 int long_press_timeout_in_ms() const { return long_press_timeout_in_ms_; }
67 int tap_timeout_in_ms() const { return tap_timeout_in_ms_; }
68 float scroll_friction() const { return scroll_friction_; }
70 int max_fling_velocity_in_dips_s() {
71 base::AutoLock autolock(lock_);
72 return max_fling_velocity_in_dips_s_;
75 int min_fling_velocity_in_dips_s() {
76 base::AutoLock autolock(lock_);
77 return min_fling_velocity_in_dips_s_;
80 int touch_slop_in_dips() {
81 base::AutoLock autolock(lock_);
82 return touch_slop_in_dips_;
85 int double_tap_slop_in_dips() {
86 base::AutoLock autolock(lock_);
87 return double_tap_slop_in_dips_;
90 int min_scaling_span_in_dips() {
91 base::AutoLock autolock(lock_);
92 return min_scaling_span_in_dips_;
95 int min_scaling_touch_major_in_dips() {
96 base::AutoLock autolock(lock_);
97 return min_scaling_touch_major_in_dips_;
100 private:
101 void Update(float maximum_fling_velocity,
102 float minimum_fling_velocity,
103 float touch_slop,
104 float double_tap_slop,
105 float min_scaling_span,
106 float min_scaling_touch_major) {
107 DCHECK_LE(minimum_fling_velocity, maximum_fling_velocity);
108 max_fling_velocity_in_dips_s_ = maximum_fling_velocity;
109 min_fling_velocity_in_dips_s_ = minimum_fling_velocity;
110 touch_slop_in_dips_ = touch_slop;
111 double_tap_slop_in_dips_ = double_tap_slop;
112 min_scaling_span_in_dips_ = min_scaling_span;
113 min_scaling_touch_major_in_dips_ = min_scaling_touch_major;
116 base::Lock lock_;
117 base::android::ScopedJavaGlobalRef<jobject> j_view_configuration_helper_;
119 // These values will remain constant throughout the lifetime of the app, so
120 // read-access needn't be synchronized.
121 int double_tap_timeout_in_ms_;
122 int long_press_timeout_in_ms_;
123 int tap_timeout_in_ms_;
124 float scroll_friction_;
126 // These values may vary as view-specific parameters change, so read/write
127 // access must be synchronized.
128 int max_fling_velocity_in_dips_s_;
129 int min_fling_velocity_in_dips_s_;
130 int touch_slop_in_dips_;
131 int double_tap_slop_in_dips_;
132 int min_scaling_span_in_dips_;
133 int min_scaling_touch_major_in_dips_;
135 private:
136 DISALLOW_COPY_AND_ASSIGN(ViewConfigurationData);
139 // Leaky to allow access from any thread.
140 base::LazyInstance<ViewConfigurationData>::Leaky g_view_configuration =
141 LAZY_INSTANCE_INITIALIZER;
143 } // namespace
145 static void UpdateSharedViewConfiguration(JNIEnv* env,
146 const JavaParamRef<jobject>& obj,
147 jfloat maximum_fling_velocity,
148 jfloat minimum_fling_velocity,
149 jfloat touch_slop,
150 jfloat double_tap_slop,
151 jfloat min_scaling_span,
152 jfloat min_scaling_touch_major) {
153 g_view_configuration.Get().SynchronizedUpdate(
154 maximum_fling_velocity, minimum_fling_velocity, touch_slop,
155 double_tap_slop, min_scaling_span, min_scaling_touch_major);
158 int ViewConfiguration::GetDoubleTapTimeoutInMs() {
159 return g_view_configuration.Get().double_tap_timeout_in_ms();
162 int ViewConfiguration::GetLongPressTimeoutInMs() {
163 return g_view_configuration.Get().long_press_timeout_in_ms();
166 int ViewConfiguration::GetTapTimeoutInMs() {
167 return g_view_configuration.Get().tap_timeout_in_ms();
170 float ViewConfiguration::GetScrollFriction() {
171 return g_view_configuration.Get().scroll_friction();
174 int ViewConfiguration::GetMaximumFlingVelocityInDipsPerSecond() {
175 return g_view_configuration.Get().max_fling_velocity_in_dips_s();
178 int ViewConfiguration::GetMinimumFlingVelocityInDipsPerSecond() {
179 return g_view_configuration.Get().min_fling_velocity_in_dips_s();
182 int ViewConfiguration::GetTouchSlopInDips() {
183 return g_view_configuration.Get().touch_slop_in_dips();
186 int ViewConfiguration::GetDoubleTapSlopInDips() {
187 return g_view_configuration.Get().double_tap_slop_in_dips();
190 int ViewConfiguration::GetMinScalingSpanInDips() {
191 return g_view_configuration.Get().min_scaling_span_in_dips();
194 int ViewConfiguration::GetMinScalingTouchMajorInDips() {
195 return g_view_configuration.Get().min_scaling_touch_major_in_dips();
198 bool ViewConfiguration::RegisterViewConfiguration(JNIEnv* env) {
199 return RegisterNativesImpl(env);
202 } // namespace gfx