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 package org.chromium.ui.gfx; | 5 package org.chromium.ui.gfx; |
6 | 6 |
7 import android.content.ComponentCallbacks; | 7 import android.content.ComponentCallbacks; |
8 import android.content.Context; | |
9 import android.content.res.Configuration; | 8 import android.content.res.Configuration; |
10 import android.content.res.Resources; | 9 import android.content.res.Resources; |
11 import android.util.TypedValue; | 10 import android.util.TypedValue; |
12 import android.view.ViewConfiguration; | 11 import android.view.ViewConfiguration; |
13 | 12 |
| 13 import org.chromium.base.ContextUtils; |
14 import org.chromium.base.annotations.CalledByNative; | 14 import org.chromium.base.annotations.CalledByNative; |
15 import org.chromium.base.annotations.JNINamespace; | 15 import org.chromium.base.annotations.JNINamespace; |
16 import org.chromium.ui.R; | 16 import org.chromium.ui.R; |
17 | 17 |
18 /** | 18 /** |
19 * This class facilitates access to ViewConfiguration-related properties, also | 19 * This class facilitates access to ViewConfiguration-related properties, also |
20 * providing native-code notifications when such properties have changed. | 20 * providing native-code notifications when such properties have changed. |
21 * | 21 * |
22 */ | 22 */ |
23 @JNINamespace("gfx") | 23 @JNINamespace("gfx") |
24 public class ViewConfigurationHelper { | 24 public class ViewConfigurationHelper { |
25 | 25 |
26 // Fallback constants when resource lookup fails, see | 26 // Fallback constants when resource lookup fails, see |
27 // ui/android/java/res/values/dimens.xml. | 27 // ui/android/java/res/values/dimens.xml. |
28 private static final float MIN_SCALING_SPAN_MM = 12.0f; | 28 private static final float MIN_SCALING_SPAN_MM = 12.0f; |
29 | 29 |
30 private final Context mAppContext; | |
31 private ViewConfiguration mViewConfiguration; | 30 private ViewConfiguration mViewConfiguration; |
32 private float mDensity; | 31 private float mDensity; |
33 | 32 |
34 private ViewConfigurationHelper(Context context) { | 33 private ViewConfigurationHelper() { |
35 mAppContext = context.getApplicationContext(); | 34 mViewConfiguration = ViewConfiguration.get(ContextUtils.getApplicationCo
ntext()); |
36 mViewConfiguration = ViewConfiguration.get(mAppContext); | 35 mDensity = ContextUtils.getApplicationContext().getResources().getDispla
yMetrics().density; |
37 mDensity = mAppContext.getResources().getDisplayMetrics().density; | |
38 assert mDensity > 0; | 36 assert mDensity > 0; |
39 } | 37 } |
40 | 38 |
41 private void registerListener() { | 39 private void registerListener() { |
42 mAppContext.registerComponentCallbacks( | 40 ContextUtils.getApplicationContext().registerComponentCallbacks( |
43 new ComponentCallbacks() { | 41 new ComponentCallbacks() { |
44 @Override | 42 @Override |
45 public void onConfigurationChanged(Configuration configurati
on) { | 43 public void onConfigurationChanged(Configuration configurati
on) { |
46 updateNativeViewConfigurationIfNecessary(); | 44 updateNativeViewConfigurationIfNecessary(); |
47 } | 45 } |
48 | 46 |
49 @Override | 47 @Override |
50 public void onLowMemory() { | 48 public void onLowMemory() { |
51 } | 49 } |
52 }); | 50 }); |
53 } | 51 } |
54 | 52 |
55 private void updateNativeViewConfigurationIfNecessary() { | 53 private void updateNativeViewConfigurationIfNecessary() { |
56 ViewConfiguration configuration = ViewConfiguration.get(mAppContext); | 54 ViewConfiguration configuration = |
| 55 ViewConfiguration.get(ContextUtils.getApplicationContext()); |
57 if (mViewConfiguration == configuration) { | 56 if (mViewConfiguration == configuration) { |
58 // The density should remain the same as long as the ViewConfigurati
on remains the same. | 57 // The density should remain the same as long as the ViewConfigurati
on remains the same. |
59 assert mDensity == mAppContext.getResources().getDisplayMetrics().de
nsity; | 58 assert mDensity |
| 59 == ContextUtils.getApplicationContext() |
| 60 .getResources() |
| 61 .getDisplayMetrics() |
| 62 .density; |
60 return; | 63 return; |
61 } | 64 } |
62 | 65 |
63 mViewConfiguration = configuration; | 66 mViewConfiguration = configuration; |
64 mDensity = mAppContext.getResources().getDisplayMetrics().density; | 67 mDensity = ContextUtils.getApplicationContext().getResources().getDispla
yMetrics().density; |
65 assert mDensity > 0; | 68 assert mDensity > 0; |
66 nativeUpdateSharedViewConfiguration(getMaximumFlingVelocity(), getMinimu
mFlingVelocity(), | 69 nativeUpdateSharedViewConfiguration(getMaximumFlingVelocity(), getMinimu
mFlingVelocity(), |
67 getTouchSlop(), getDoubleTapSlop(), getMinScalingSpan()); | 70 getTouchSlop(), getDoubleTapSlop(), getMinScalingSpan()); |
68 } | 71 } |
69 | 72 |
70 @CalledByNative | 73 @CalledByNative |
71 private static int getDoubleTapTimeout() { | 74 private static int getDoubleTapTimeout() { |
72 return ViewConfiguration.getDoubleTapTimeout(); | 75 return ViewConfiguration.getDoubleTapTimeout(); |
73 } | 76 } |
74 | 77 |
(...skipping 26 matching lines...) Expand all Loading... |
101 private float getDoubleTapSlop() { | 104 private float getDoubleTapSlop() { |
102 return toDips(mViewConfiguration.getScaledDoubleTapSlop()); | 105 return toDips(mViewConfiguration.getScaledDoubleTapSlop()); |
103 } | 106 } |
104 | 107 |
105 @CalledByNative | 108 @CalledByNative |
106 private float getMinScalingSpan() { | 109 private float getMinScalingSpan() { |
107 return toDips(getScaledMinScalingSpan()); | 110 return toDips(getScaledMinScalingSpan()); |
108 } | 111 } |
109 | 112 |
110 private int getScaledMinScalingSpan() { | 113 private int getScaledMinScalingSpan() { |
111 final Resources res = mAppContext.getResources(); | 114 final Resources res = ContextUtils.getApplicationContext().getResources(
); |
112 // The correct minimum scaling span depends on how we recognize scale | 115 // The correct minimum scaling span depends on how we recognize scale |
113 // gestures. Since we've deviated from Android, don't use the Android | 116 // gestures. Since we've deviated from Android, don't use the Android |
114 // system value here. | 117 // system value here. |
115 int id = R.dimen.config_min_scaling_span; | 118 int id = R.dimen.config_min_scaling_span; |
116 try { | 119 try { |
117 return res.getDimensionPixelSize(id); | 120 return res.getDimensionPixelSize(id); |
118 } catch (Resources.NotFoundException e) { | 121 } catch (Resources.NotFoundException e) { |
119 assert false : "MinScalingSpan resource lookup failed."; | 122 assert false : "MinScalingSpan resource lookup failed."; |
120 return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, M
IN_SCALING_SPAN_MM, | 123 return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, M
IN_SCALING_SPAN_MM, |
121 res.getDisplayMetrics()); | 124 res.getDisplayMetrics()); |
122 } | 125 } |
123 } | 126 } |
124 | 127 |
125 /** | 128 /** |
126 * @return the unscaled pixel quantity in DIPs. | 129 * @return the unscaled pixel quantity in DIPs. |
127 */ | 130 */ |
128 private float toDips(int pixels) { | 131 private float toDips(int pixels) { |
129 return pixels / mDensity; | 132 return pixels / mDensity; |
130 } | 133 } |
131 | 134 |
132 @CalledByNative | 135 @CalledByNative |
133 private static ViewConfigurationHelper createWithListener(Context context) { | 136 private static ViewConfigurationHelper createWithListener() { |
134 ViewConfigurationHelper viewConfigurationHelper = new ViewConfigurationH
elper(context); | 137 ViewConfigurationHelper viewConfigurationHelper = new ViewConfigurationH
elper(); |
135 viewConfigurationHelper.registerListener(); | 138 viewConfigurationHelper.registerListener(); |
136 return viewConfigurationHelper; | 139 return viewConfigurationHelper; |
137 } | 140 } |
138 | 141 |
139 private native void nativeUpdateSharedViewConfiguration(float maximumFlingVe
locity, | 142 private native void nativeUpdateSharedViewConfiguration(float maximumFlingVe
locity, |
140 float minimumFlingVelocity, float touchSlop, float doubleTapSlop, fl
oat minScalingSpan); | 143 float minimumFlingVelocity, float touchSlop, float doubleTapSlop, fl
oat minScalingSpan); |
141 } | 144 } |
OLD | NEW |