• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Portions of this code came from frameworks/base/core/java/android/view/ViewConfiguration.java,
3  * which contains the following license text:
4  *
5  * Copyright (C) 2006 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20 
21 package org.robolectric.shadows;
22 
23 import android.content.Context;
24 import android.util.DisplayMetrics;
25 import android.view.ViewConfiguration;
26 import org.robolectric.annotation.Implementation;
27 import org.robolectric.annotation.Implements;
28 import org.robolectric.annotation.RealObject;
29 import org.robolectric.shadow.api.Shadow;
30 
31 @SuppressWarnings({"UnusedDeclaration"})
32 @Implements(ViewConfiguration.class)
33 public class ShadowViewConfiguration {
34 
35   private static final int SCROLL_BAR_SIZE = 10;
36   private static final int SCROLL_BAR_FADE_DURATION = 250;
37   private static final int SCROLL_BAR_DEFAULT_DELAY = 300;
38   private static final int FADING_EDGE_LENGTH = 12;
39   private static final int PRESSED_STATE_DURATION = 125;
40   private static final int LONG_PRESS_TIMEOUT = 500;
41   private static final int GLOBAL_ACTIONS_KEY_TIMEOUT = 500;
42   private static final int TAP_TIMEOUT = 115;
43   private static final int JUMP_TAP_TIMEOUT = 500;
44   private static final int DOUBLE_TAP_TIMEOUT = 300;
45   private static final int ZOOM_CONTROLS_TIMEOUT = 3000;
46   private static final int EDGE_SLOP = 12;
47   private static final int TOUCH_SLOP = 16;
48   private static final int PAGING_TOUCH_SLOP = TOUCH_SLOP * 2;
49   private static final int DOUBLE_TAP_SLOP = 100;
50   private static final int WINDOW_TOUCH_SLOP = 16;
51   private static final int MINIMUM_FLING_VELOCITY = 50;
52   private static final int MAXIMUM_FLING_VELOCITY = 4000;
53   private static final int MAXIMUM_DRAWING_CACHE_SIZE = 320 * 480 * 4;
54   private static final int OVERSCROLL_DISTANCE = 0;
55   private static final int OVERFLING_DISTANCE = 4;
56   private static final float SCROLL_FRICTION = 0.015f;
57 
58   private int edgeSlop;
59   private int fadingEdgeLength;
60   private int minimumFlingVelocity;
61   private int maximumFlingVelocity;
62   private int scrollbarSize;
63   private int touchSlop;
64   private int pagingTouchSlop;
65   private int doubleTapSlop;
66   private int windowTouchSlop;
67   private boolean hasPermanentMenuKey = false;
68 
69   @RealObject
70   private ViewConfiguration realViewConfiguration;
71 
setup(Context context)72   private void setup(Context context) {
73     DisplayMetrics metrics = context.getResources().getDisplayMetrics();
74     float density = metrics.density;
75 
76     edgeSlop = (int) (density * EDGE_SLOP + 0.5f);
77     fadingEdgeLength = (int) (density * FADING_EDGE_LENGTH + 0.5f);
78     minimumFlingVelocity = (int) (density * MINIMUM_FLING_VELOCITY + 0.5f);
79     maximumFlingVelocity = (int) (density * MAXIMUM_FLING_VELOCITY + 0.5f);
80     scrollbarSize = (int) (density * SCROLL_BAR_SIZE + 0.5f);
81     touchSlop = (int) (density * TOUCH_SLOP + 0.5f);
82     pagingTouchSlop = (int) (density * PAGING_TOUCH_SLOP + 0.5f);
83     doubleTapSlop = (int) (density * DOUBLE_TAP_SLOP + 0.5f);
84     windowTouchSlop = (int) (density * WINDOW_TOUCH_SLOP + 0.5f);
85   }
86 
87   @Implementation
get(Context context)88   protected static ViewConfiguration get(Context context) {
89     ViewConfiguration viewConfiguration = Shadow.newInstanceOf(ViewConfiguration.class);
90     ShadowViewConfiguration shadowViewConfiguration = Shadow.extract(viewConfiguration);
91     shadowViewConfiguration.setup(context);
92     return viewConfiguration;
93   }
94 
95   @Implementation
getScrollBarSize()96   protected static int getScrollBarSize() {
97     return SCROLL_BAR_SIZE;
98   }
99 
100   @Implementation
getScaledScrollBarSize()101   protected int getScaledScrollBarSize() {
102     return scrollbarSize;
103   }
104 
105   @Implementation
getScrollBarFadeDuration()106   protected static int getScrollBarFadeDuration() {
107     return SCROLL_BAR_FADE_DURATION;
108   }
109 
110   @Implementation
getScrollDefaultDelay()111   protected static int getScrollDefaultDelay() {
112     return SCROLL_BAR_DEFAULT_DELAY;
113   }
114 
115   @Implementation
getFadingEdgeLength()116   protected static int getFadingEdgeLength() {
117     return FADING_EDGE_LENGTH;
118   }
119 
120   @Implementation
getScaledFadingEdgeLength()121   protected int getScaledFadingEdgeLength() {
122     return fadingEdgeLength;
123   }
124 
125   @Implementation
getPressedStateDuration()126   protected static int getPressedStateDuration() {
127     return PRESSED_STATE_DURATION;
128   }
129 
130   @Implementation
getLongPressTimeout()131   protected static int getLongPressTimeout() {
132     return LONG_PRESS_TIMEOUT;
133   }
134 
135   @Implementation
getTapTimeout()136   protected static int getTapTimeout() {
137     return TAP_TIMEOUT;
138   }
139 
140   @Implementation
getJumpTapTimeout()141   protected static int getJumpTapTimeout() {
142     return JUMP_TAP_TIMEOUT;
143   }
144 
145   @Implementation
getDoubleTapTimeout()146   protected static int getDoubleTapTimeout() {
147     return DOUBLE_TAP_TIMEOUT;
148   }
149 
150   @Implementation
getEdgeSlop()151   protected static int getEdgeSlop() {
152     return EDGE_SLOP;
153   }
154 
155   @Implementation
getScaledEdgeSlop()156   protected int getScaledEdgeSlop() {
157     return edgeSlop;
158   }
159 
160   @Implementation
getTouchSlop()161   protected static int getTouchSlop() {
162     return TOUCH_SLOP;
163   }
164 
165   @Implementation
getScaledTouchSlop()166   protected int getScaledTouchSlop() {
167     return touchSlop;
168   }
169 
170   @Implementation
getScaledPagingTouchSlop()171   protected int getScaledPagingTouchSlop() {
172     return pagingTouchSlop;
173   }
174 
175   @Implementation
getScaledDoubleTapSlop()176   protected int getScaledDoubleTapSlop() {
177     return doubleTapSlop;
178   }
179 
180   @Implementation
getWindowTouchSlop()181   protected static int getWindowTouchSlop() {
182     return WINDOW_TOUCH_SLOP;
183   }
184 
185   @Implementation
getScaledWindowTouchSlop()186   protected int getScaledWindowTouchSlop() {
187     return windowTouchSlop;
188   }
189 
190   @Implementation
getMinimumFlingVelocity()191   protected static int getMinimumFlingVelocity() {
192     return MINIMUM_FLING_VELOCITY;
193   }
194 
195   @Implementation
getScaledMinimumFlingVelocity()196   protected int getScaledMinimumFlingVelocity() {
197     return minimumFlingVelocity;
198   }
199 
200   @Implementation
getMaximumFlingVelocity()201   protected static int getMaximumFlingVelocity() {
202     return MAXIMUM_FLING_VELOCITY;
203   }
204 
205   @Implementation
getScaledMaximumFlingVelocity()206   protected int getScaledMaximumFlingVelocity() {
207     return maximumFlingVelocity;
208   }
209 
210   @Implementation
getMaximumDrawingCacheSize()211   protected static int getMaximumDrawingCacheSize() {
212     return MAXIMUM_DRAWING_CACHE_SIZE;
213   }
214 
215   @Implementation
getZoomControlsTimeout()216   protected static long getZoomControlsTimeout() {
217     return ZOOM_CONTROLS_TIMEOUT;
218   }
219 
220   @Implementation
getGlobalActionKeyTimeout()221   protected static long getGlobalActionKeyTimeout() {
222     return GLOBAL_ACTIONS_KEY_TIMEOUT;
223   }
224 
225   @Implementation
getScrollFriction()226   protected static float getScrollFriction() {
227     return SCROLL_FRICTION;
228   }
229 
230   @Implementation
hasPermanentMenuKey()231   protected boolean hasPermanentMenuKey() {
232     return hasPermanentMenuKey;
233   }
234 
setHasPermanentMenuKey(boolean value)235   public void setHasPermanentMenuKey(boolean value) {
236     this.hasPermanentMenuKey = value;
237   }
238 }
239