| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/android/view_configuration.h" | 5 #include "ui/gfx/android/view_configuration.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/threading/non_thread_safe.h" | 9 #include "base/threading/non_thread_safe.h" |
| 10 #include "jni/ViewConfigurationHelper_jni.h" | 10 #include "jni/ViewConfigurationHelper_jni.h" |
| 11 | 11 |
| 12 using base::android::AttachCurrentThread; | 12 using base::android::AttachCurrentThread; |
| 13 using base::android::GetApplicationContext; | 13 using base::android::GetApplicationContext; |
| 14 | 14 |
| 15 namespace gfx { | 15 namespace gfx { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 struct ViewConfigurationData { | 19 struct ViewConfigurationData { |
| 20 ViewConfigurationData() | 20 ViewConfigurationData() |
| 21 : double_tap_timeout_in_ms_(0), | 21 : double_tap_timeout_in_ms_(0), |
| 22 long_press_timeout_in_ms_(0), | 22 long_press_timeout_in_ms_(0), |
| 23 tap_timeout_in_ms_(0), | 23 tap_timeout_in_ms_(0), |
| 24 scroll_friction_(1.f), | 24 scroll_friction_(1.f), |
| 25 max_fling_velocity_in_pixels_s_(0), | 25 max_fling_velocity_in_dips_s_(0), |
| 26 min_fling_velocity_in_pixels_s_(0), | 26 min_fling_velocity_in_dips_s_(0), |
| 27 touch_slop_in_pixels_(0), | 27 touch_slop_in_dips_(0), |
| 28 double_tap_slop_in_pixels_(0), | 28 double_tap_slop_in_dips_(0), |
| 29 min_scaling_span_in_pixels_(0), | 29 min_scaling_span_in_dips_(0), |
| 30 min_scaling_touch_major_in_pixels_(0) { | 30 min_scaling_touch_major_in_dips_(0) { |
| 31 JNIEnv* env = AttachCurrentThread(); | 31 JNIEnv* env = AttachCurrentThread(); |
| 32 j_view_configuration_helper_.Reset( | 32 j_view_configuration_helper_.Reset( |
| 33 Java_ViewConfigurationHelper_createWithListener( | 33 Java_ViewConfigurationHelper_createWithListener( |
| 34 env, base::android::GetApplicationContext())); | 34 env, base::android::GetApplicationContext())); |
| 35 | 35 |
| 36 double_tap_timeout_in_ms_ = | 36 double_tap_timeout_in_ms_ = |
| 37 Java_ViewConfigurationHelper_getDoubleTapTimeout(env); | 37 Java_ViewConfigurationHelper_getDoubleTapTimeout(env); |
| 38 long_press_timeout_in_ms_ = | 38 long_press_timeout_in_ms_ = |
| 39 Java_ViewConfigurationHelper_getLongPressTimeout(env); | 39 Java_ViewConfigurationHelper_getLongPressTimeout(env); |
| 40 tap_timeout_in_ms_ = Java_ViewConfigurationHelper_getTapTimeout(env); | 40 tap_timeout_in_ms_ = Java_ViewConfigurationHelper_getTapTimeout(env); |
| 41 scroll_friction_ = Java_ViewConfigurationHelper_getScrollFriction(env); | 41 scroll_friction_ = Java_ViewConfigurationHelper_getScrollFriction(env); |
| 42 | 42 |
| 43 jobject obj = j_view_configuration_helper_.obj(); | 43 jobject obj = j_view_configuration_helper_.obj(); |
| 44 Update( | 44 Update(Java_ViewConfigurationHelper_getMaximumFlingVelocity(env, obj), |
| 45 Java_ViewConfigurationHelper_getScaledMaximumFlingVelocity(env, obj), | 45 Java_ViewConfigurationHelper_getMinimumFlingVelocity(env, obj), |
| 46 Java_ViewConfigurationHelper_getScaledMinimumFlingVelocity(env, obj), | 46 Java_ViewConfigurationHelper_getTouchSlop(env, obj), |
| 47 Java_ViewConfigurationHelper_getScaledTouchSlop(env, obj), | 47 Java_ViewConfigurationHelper_getDoubleTapSlop(env, obj), |
| 48 Java_ViewConfigurationHelper_getScaledDoubleTapSlop(env, obj), | 48 Java_ViewConfigurationHelper_getMinScalingSpan(env, obj), |
| 49 Java_ViewConfigurationHelper_getScaledMinScalingSpan(env, obj), | 49 Java_ViewConfigurationHelper_getMinScalingTouchMajor(env, obj)); |
| 50 Java_ViewConfigurationHelper_getScaledMinScalingTouchMajor(env, obj)); | |
| 51 } | 50 } |
| 52 | 51 |
| 53 ~ViewConfigurationData() {} | 52 ~ViewConfigurationData() {} |
| 54 | 53 |
| 55 void SynchronizedUpdate(int scaled_maximum_fling_velocity, | 54 void SynchronizedUpdate(float maximum_fling_velocity, |
| 56 int scaled_minimum_fling_velocity, | 55 float minimum_fling_velocity, |
| 57 int scaled_touch_slop, | 56 float touch_slop, |
| 58 int scaled_double_tap_slop, | 57 float double_tap_slop, |
| 59 int scaled_min_scaling_span, | 58 float min_scaling_span, |
| 60 int scaled_min_scaling_touch_major) { | 59 float min_scaling_touch_major) { |
| 61 base::AutoLock autolock(lock_); | 60 base::AutoLock autolock(lock_); |
| 62 Update(scaled_maximum_fling_velocity, | 61 Update(maximum_fling_velocity, minimum_fling_velocity, touch_slop, |
| 63 scaled_minimum_fling_velocity, | 62 double_tap_slop, min_scaling_span, min_scaling_touch_major); |
| 64 scaled_touch_slop, | |
| 65 scaled_double_tap_slop, | |
| 66 scaled_min_scaling_span, | |
| 67 scaled_min_scaling_touch_major); | |
| 68 } | 63 } |
| 69 | 64 |
| 70 int double_tap_timeout_in_ms() const { return double_tap_timeout_in_ms_; } | 65 int double_tap_timeout_in_ms() const { return double_tap_timeout_in_ms_; } |
| 71 int long_press_timeout_in_ms() const { return long_press_timeout_in_ms_; } | 66 int long_press_timeout_in_ms() const { return long_press_timeout_in_ms_; } |
| 72 int tap_timeout_in_ms() const { return tap_timeout_in_ms_; } | 67 int tap_timeout_in_ms() const { return tap_timeout_in_ms_; } |
| 73 float scroll_friction() const { return scroll_friction_; } | 68 float scroll_friction() const { return scroll_friction_; } |
| 74 | 69 |
| 75 int max_fling_velocity_in_pixels_s() { | 70 int max_fling_velocity_in_dips_s() { |
| 76 base::AutoLock autolock(lock_); | 71 base::AutoLock autolock(lock_); |
| 77 return max_fling_velocity_in_pixels_s_; | 72 return max_fling_velocity_in_dips_s_; |
| 78 } | 73 } |
| 79 | 74 |
| 80 int min_fling_velocity_in_pixels_s() { | 75 int min_fling_velocity_in_dips_s() { |
| 81 base::AutoLock autolock(lock_); | 76 base::AutoLock autolock(lock_); |
| 82 return min_fling_velocity_in_pixels_s_; | 77 return min_fling_velocity_in_dips_s_; |
| 83 } | 78 } |
| 84 | 79 |
| 85 int touch_slop_in_pixels() { | 80 int touch_slop_in_dips() { |
| 86 base::AutoLock autolock(lock_); | 81 base::AutoLock autolock(lock_); |
| 87 return touch_slop_in_pixels_; | 82 return touch_slop_in_dips_; |
| 88 } | 83 } |
| 89 | 84 |
| 90 int double_tap_slop_in_pixels() { | 85 int double_tap_slop_in_dips() { |
| 91 base::AutoLock autolock(lock_); | 86 base::AutoLock autolock(lock_); |
| 92 return double_tap_slop_in_pixels_; | 87 return double_tap_slop_in_dips_; |
| 93 } | 88 } |
| 94 | 89 |
| 95 int min_scaling_span_in_pixels() { | 90 int min_scaling_span_in_dips() { |
| 96 base::AutoLock autolock(lock_); | 91 base::AutoLock autolock(lock_); |
| 97 return min_scaling_span_in_pixels_; | 92 return min_scaling_span_in_dips_; |
| 98 } | 93 } |
| 99 | 94 |
| 100 int min_scaling_touch_major_in_pixels() { | 95 int min_scaling_touch_major_in_dips() { |
| 101 base::AutoLock autolock(lock_); | 96 base::AutoLock autolock(lock_); |
| 102 return min_scaling_touch_major_in_pixels_; | 97 return min_scaling_touch_major_in_dips_; |
| 103 } | 98 } |
| 104 | 99 |
| 105 private: | 100 private: |
| 106 void Update(int scaled_maximum_fling_velocity, | 101 void Update(float maximum_fling_velocity, |
| 107 int scaled_minimum_fling_velocity, | 102 float minimum_fling_velocity, |
| 108 int scaled_touch_slop, | 103 float touch_slop, |
| 109 int scaled_double_tap_slop, | 104 float double_tap_slop, |
| 110 int scaled_min_scaling_span, | 105 float min_scaling_span, |
| 111 int scaled_min_scaling_touch_major) { | 106 float min_scaling_touch_major) { |
| 112 DCHECK_LE(scaled_minimum_fling_velocity, scaled_maximum_fling_velocity); | 107 DCHECK_LE(minimum_fling_velocity, maximum_fling_velocity); |
| 113 max_fling_velocity_in_pixels_s_ = scaled_maximum_fling_velocity; | 108 max_fling_velocity_in_dips_s_ = maximum_fling_velocity; |
| 114 min_fling_velocity_in_pixels_s_ = scaled_minimum_fling_velocity; | 109 min_fling_velocity_in_dips_s_ = minimum_fling_velocity; |
| 115 touch_slop_in_pixels_ = scaled_touch_slop; | 110 touch_slop_in_dips_ = touch_slop; |
| 116 double_tap_slop_in_pixels_ = scaled_double_tap_slop; | 111 double_tap_slop_in_dips_ = double_tap_slop; |
| 117 min_scaling_span_in_pixels_ = scaled_min_scaling_span; | 112 min_scaling_span_in_dips_ = min_scaling_span; |
| 118 min_scaling_touch_major_in_pixels_ = scaled_min_scaling_touch_major; | 113 min_scaling_touch_major_in_dips_ = min_scaling_touch_major; |
| 119 } | 114 } |
| 120 | 115 |
| 121 base::Lock lock_; | 116 base::Lock lock_; |
| 122 base::android::ScopedJavaGlobalRef<jobject> j_view_configuration_helper_; | 117 base::android::ScopedJavaGlobalRef<jobject> j_view_configuration_helper_; |
| 123 | 118 |
| 124 // These values will remain constant throughout the lifetime of the app, so | 119 // These values will remain constant throughout the lifetime of the app, so |
| 125 // read-access needn't be synchronized. | 120 // read-access needn't be synchronized. |
| 126 int double_tap_timeout_in_ms_; | 121 int double_tap_timeout_in_ms_; |
| 127 int long_press_timeout_in_ms_; | 122 int long_press_timeout_in_ms_; |
| 128 int tap_timeout_in_ms_; | 123 int tap_timeout_in_ms_; |
| 129 float scroll_friction_; | 124 float scroll_friction_; |
| 130 | 125 |
| 131 // These values may vary as view-specific parameters (DPI scale) are changed, | 126 // These values may vary as view-specific parameters change, so read/write |
| 132 // so read/write access must be synchronized. | 127 // access must be synchronized. |
| 133 int max_fling_velocity_in_pixels_s_; | 128 int max_fling_velocity_in_dips_s_; |
| 134 int min_fling_velocity_in_pixels_s_; | 129 int min_fling_velocity_in_dips_s_; |
| 135 int touch_slop_in_pixels_; | 130 int touch_slop_in_dips_; |
| 136 int double_tap_slop_in_pixels_; | 131 int double_tap_slop_in_dips_; |
| 137 int min_scaling_span_in_pixels_; | 132 int min_scaling_span_in_dips_; |
| 138 int min_scaling_touch_major_in_pixels_; | 133 int min_scaling_touch_major_in_dips_; |
| 139 | 134 |
| 140 private: | 135 private: |
| 141 DISALLOW_COPY_AND_ASSIGN(ViewConfigurationData); | 136 DISALLOW_COPY_AND_ASSIGN(ViewConfigurationData); |
| 142 }; | 137 }; |
| 143 | 138 |
| 144 // Leaky to allow access from any thread. | 139 // Leaky to allow access from any thread. |
| 145 base::LazyInstance<ViewConfigurationData>::Leaky g_view_configuration = | 140 base::LazyInstance<ViewConfigurationData>::Leaky g_view_configuration = |
| 146 LAZY_INSTANCE_INITIALIZER; | 141 LAZY_INSTANCE_INITIALIZER; |
| 147 | 142 |
| 148 } // namespace | 143 } // namespace |
| 149 | 144 |
| 150 static void UpdateSharedViewConfiguration(JNIEnv* env, | 145 static void UpdateSharedViewConfiguration(JNIEnv* env, |
| 151 jobject obj, | 146 jobject obj, |
| 152 jint scaled_maximum_fling_velocity, | 147 jfloat maximum_fling_velocity, |
| 153 jint scaled_minimum_fling_velocity, | 148 jfloat minimum_fling_velocity, |
| 154 jint scaled_touch_slop, | 149 jfloat touch_slop, |
| 155 jint scaled_double_tap_slop, | 150 jfloat double_tap_slop, |
| 156 jint scaled_min_scaling_span, | 151 jfloat min_scaling_span, |
| 157 jint scaled_min_scaling_touch_major) { | 152 jfloat min_scaling_touch_major) { |
| 158 g_view_configuration.Get().SynchronizedUpdate(scaled_maximum_fling_velocity, | 153 g_view_configuration.Get().SynchronizedUpdate( |
| 159 scaled_minimum_fling_velocity, | 154 maximum_fling_velocity, minimum_fling_velocity, touch_slop, |
| 160 scaled_touch_slop, | 155 double_tap_slop, min_scaling_span, min_scaling_touch_major); |
| 161 scaled_double_tap_slop, | |
| 162 scaled_min_scaling_span, | |
| 163 scaled_min_scaling_touch_major); | |
| 164 } | 156 } |
| 165 | 157 |
| 166 int ViewConfiguration::GetDoubleTapTimeoutInMs() { | 158 int ViewConfiguration::GetDoubleTapTimeoutInMs() { |
| 167 return g_view_configuration.Get().double_tap_timeout_in_ms(); | 159 return g_view_configuration.Get().double_tap_timeout_in_ms(); |
| 168 } | 160 } |
| 169 | 161 |
| 170 int ViewConfiguration::GetLongPressTimeoutInMs() { | 162 int ViewConfiguration::GetLongPressTimeoutInMs() { |
| 171 return g_view_configuration.Get().long_press_timeout_in_ms(); | 163 return g_view_configuration.Get().long_press_timeout_in_ms(); |
| 172 } | 164 } |
| 173 | 165 |
| 174 int ViewConfiguration::GetTapTimeoutInMs() { | 166 int ViewConfiguration::GetTapTimeoutInMs() { |
| 175 return g_view_configuration.Get().tap_timeout_in_ms(); | 167 return g_view_configuration.Get().tap_timeout_in_ms(); |
| 176 } | 168 } |
| 177 | 169 |
| 178 float ViewConfiguration::GetScrollFriction() { | 170 float ViewConfiguration::GetScrollFriction() { |
| 179 return g_view_configuration.Get().scroll_friction(); | 171 return g_view_configuration.Get().scroll_friction(); |
| 180 } | 172 } |
| 181 | 173 |
| 182 int ViewConfiguration::GetMaximumFlingVelocityInPixelsPerSecond() { | 174 int ViewConfiguration::GetMaximumFlingVelocityInDipsPerSecond() { |
| 183 return g_view_configuration.Get().max_fling_velocity_in_pixels_s(); | 175 return g_view_configuration.Get().max_fling_velocity_in_dips_s(); |
| 184 } | 176 } |
| 185 | 177 |
| 186 int ViewConfiguration::GetMinimumFlingVelocityInPixelsPerSecond() { | 178 int ViewConfiguration::GetMinimumFlingVelocityInDipsPerSecond() { |
| 187 return g_view_configuration.Get().min_fling_velocity_in_pixels_s(); | 179 return g_view_configuration.Get().min_fling_velocity_in_dips_s(); |
| 188 } | 180 } |
| 189 | 181 |
| 190 int ViewConfiguration::GetTouchSlopInPixels() { | 182 int ViewConfiguration::GetTouchSlopInDips() { |
| 191 return g_view_configuration.Get().touch_slop_in_pixels(); | 183 return g_view_configuration.Get().touch_slop_in_dips(); |
| 192 } | 184 } |
| 193 | 185 |
| 194 int ViewConfiguration::GetDoubleTapSlopInPixels() { | 186 int ViewConfiguration::GetDoubleTapSlopInDips() { |
| 195 return g_view_configuration.Get().double_tap_slop_in_pixels(); | 187 return g_view_configuration.Get().double_tap_slop_in_dips(); |
| 196 } | 188 } |
| 197 | 189 |
| 198 int ViewConfiguration::GetMinScalingSpanInPixels() { | 190 int ViewConfiguration::GetMinScalingSpanInDips() { |
| 199 return g_view_configuration.Get().min_scaling_span_in_pixels(); | 191 return g_view_configuration.Get().min_scaling_span_in_dips(); |
| 200 } | 192 } |
| 201 | 193 |
| 202 int ViewConfiguration::GetMinScalingTouchMajorInPixels() { | 194 int ViewConfiguration::GetMinScalingTouchMajorInDips() { |
| 203 return g_view_configuration.Get().min_scaling_touch_major_in_pixels(); | 195 return g_view_configuration.Get().min_scaling_touch_major_in_dips(); |
| 204 } | 196 } |
| 205 | 197 |
| 206 bool ViewConfiguration::RegisterViewConfiguration(JNIEnv* env) { | 198 bool ViewConfiguration::RegisterViewConfiguration(JNIEnv* env) { |
| 207 return RegisterNativesImpl(env); | 199 return RegisterNativesImpl(env); |
| 208 } | 200 } |
| 209 | 201 |
| 210 } // namespace gfx | 202 } // namespace gfx |
| OLD | NEW |