ViewConfiguration.java revision de8d0834fc2bc31f011fb3316757883e1b731440
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
19import android.content.Context;
20import android.util.DisplayMetrics;
21import android.util.SparseArray;
22
23/**
24 * Contains methods to standard constants used in the UI for timeouts, sizes, and distances.
25 */
26public class ViewConfiguration {
27    /**
28     * Defines the width of the horizontal scrollbar and the height of the vertical scrollbar in
29     * pixels
30     */
31    private static final int SCROLL_BAR_SIZE = 10;
32
33    /**
34     * Duration of the fade when scrollbars fade away in milliseconds
35     */
36    private static final int SCROLL_BAR_FADE_DURATION = 250;
37
38    /**
39     * Default delay before the scrollbars fade in milliseconds
40     */
41    private static final int SCROLL_BAR_DEFAULT_DELAY = 300;
42
43    /**
44     * Defines the length of the fading edges in pixels
45     */
46    private static final int FADING_EDGE_LENGTH = 12;
47
48    /**
49     * Defines the duration in milliseconds of the pressed state in child
50     * components.
51     */
52    private static final int PRESSED_STATE_DURATION = 125;
53
54    /**
55     * Defines the duration in milliseconds before a press turns into
56     * a long press
57     */
58    private static final int LONG_PRESS_TIMEOUT = 500;
59
60    /**
61     * Defines the duration in milliseconds a user needs to hold down the
62     * appropriate button to bring up the global actions dialog (power off,
63     * lock screen, etc).
64     */
65    private static final int GLOBAL_ACTIONS_KEY_TIMEOUT = 500;
66
67    /**
68     * Defines the duration in milliseconds we will wait to see if a touch event
69     * is a tap or a scroll. If the user does not move within this interval, it is
70     * considered to be a tap.
71     */
72    private static final int TAP_TIMEOUT = 115;
73
74    /**
75     * Defines the duration in milliseconds we will wait to see if a touch event
76     * is a jump tap. If the user does not complete the jump tap within this interval, it is
77     * considered to be a tap.
78     */
79    private static final int JUMP_TAP_TIMEOUT = 500;
80
81    /**
82     * Defines the duration in milliseconds between the first tap's up event and
83     * the second tap's down event for an interaction to be considered a
84     * double-tap.
85     */
86    private static final int DOUBLE_TAP_TIMEOUT = 300;
87
88    /**
89     * Defines the duration in milliseconds we want to display zoom controls in response
90     * to a user panning within an application.
91     */
92    private static final int ZOOM_CONTROLS_TIMEOUT = 3000;
93
94    /**
95     * Inset in pixels to look for touchable content when the user touches the edge of the screen
96     */
97    private static final int EDGE_SLOP = 12;
98
99    /**
100     * Distance a touch can wander before we think the user is scrolling in pixels
101     */
102    private static final int TOUCH_SLOP = 16;
103
104    /**
105     * Distance a touch can wander before we think the user is attempting a paged scroll
106     * (in dips)
107     */
108    private static final int PAGING_TOUCH_SLOP = TOUCH_SLOP * 3;
109
110    /**
111     * Distance between the first touch and second touch to still be considered a double tap
112     */
113    private static final int DOUBLE_TAP_SLOP = 100;
114
115    /**
116     * Distance a touch needs to be outside of a window's bounds for it to
117     * count as outside for purposes of dismissing the window.
118     */
119    private static final int WINDOW_TOUCH_SLOP = 16;
120
121    /**
122     * Minimum velocity to initiate a fling, as measured in pixels per second
123     */
124    private static final int MINIMUM_FLING_VELOCITY = 50;
125
126    /**
127     * Maximum velocity to initiate a fling, as measured in pixels per second
128     */
129    private static final int MAXIMUM_FLING_VELOCITY = 4000;
130
131    /**
132     * The maximum size of View's drawing cache, expressed in bytes. This size
133     * should be at least equal to the size of the screen in ARGB888 format.
134     */
135    @Deprecated
136    private static final int MAXIMUM_DRAWING_CACHE_SIZE = 320 * 480 * 4; // HVGA screen, ARGB8888
137
138    /**
139     * The coefficient of friction applied to flings/scrolls.
140     */
141    private static float SCROLL_FRICTION = 0.015f;
142
143    private final int mEdgeSlop;
144    private final int mFadingEdgeLength;
145    private final int mMinimumFlingVelocity;
146    private final int mMaximumFlingVelocity;
147    private final int mScrollbarSize;
148    private final int mTouchSlop;
149    private final int mPagingTouchSlop;
150    private final int mDoubleTapSlop;
151    private final int mWindowTouchSlop;
152    private final int mMaximumDrawingCacheSize;
153
154    private static final SparseArray<ViewConfiguration> sConfigurations =
155            new SparseArray<ViewConfiguration>(2);
156
157    /**
158     * @deprecated Use {@link android.view.ViewConfiguration#get(android.content.Context)} instead.
159     */
160    @Deprecated
161    public ViewConfiguration() {
162        mEdgeSlop = EDGE_SLOP;
163        mFadingEdgeLength = FADING_EDGE_LENGTH;
164        mMinimumFlingVelocity = MINIMUM_FLING_VELOCITY;
165        mMaximumFlingVelocity = MAXIMUM_FLING_VELOCITY;
166        mScrollbarSize = SCROLL_BAR_SIZE;
167        mTouchSlop = TOUCH_SLOP;
168        mPagingTouchSlop = PAGING_TOUCH_SLOP;
169        mDoubleTapSlop = DOUBLE_TAP_SLOP;
170        mWindowTouchSlop = WINDOW_TOUCH_SLOP;
171        //noinspection deprecation
172        mMaximumDrawingCacheSize = MAXIMUM_DRAWING_CACHE_SIZE;
173    }
174
175    /**
176     * Creates a new configuration for the specified context. The configuration depends on
177     * various parameters of the context, like the dimension of the display or the density
178     * of the display.
179     *
180     * @param context The application context used to initialize this view configuration.
181     *
182     * @see #get(android.content.Context)
183     * @see android.util.DisplayMetrics
184     */
185    private ViewConfiguration(Context context) {
186        final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
187        final float density = metrics.density;
188
189        mEdgeSlop = (int) (density * EDGE_SLOP + 0.5f);
190        mFadingEdgeLength = (int) (density * FADING_EDGE_LENGTH + 0.5f);
191        mMinimumFlingVelocity = (int) (density * MINIMUM_FLING_VELOCITY + 0.5f);
192        mMaximumFlingVelocity = (int) (density * MAXIMUM_FLING_VELOCITY + 0.5f);
193        mScrollbarSize = (int) (density * SCROLL_BAR_SIZE + 0.5f);
194        mTouchSlop = (int) (density * TOUCH_SLOP + 0.5f);
195        mPagingTouchSlop = (int) (density * PAGING_TOUCH_SLOP + 0.5f);
196        mDoubleTapSlop = (int) (density * DOUBLE_TAP_SLOP + 0.5f);
197        mWindowTouchSlop = (int) (density * WINDOW_TOUCH_SLOP + 0.5f);
198
199        // Size of the screen in bytes, in ARGB_8888 format
200        mMaximumDrawingCacheSize = 4 * metrics.widthPixels * metrics.heightPixels;
201    }
202
203    /**
204     * Returns a configuration for the specified context. The configuration depends on
205     * various parameters of the context, like the dimension of the display or the
206     * density of the display.
207     *
208     * @param context The application context used to initialize the view configuration.
209     */
210    public static ViewConfiguration get(Context context) {
211        final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
212        final int density = (int) (100.0f * metrics.density);
213
214        ViewConfiguration configuration = sConfigurations.get(density);
215        if (configuration == null) {
216            configuration = new ViewConfiguration(context);
217            sConfigurations.put(density, configuration);
218        }
219
220        return configuration;
221    }
222
223    /**
224     * @return The width of the horizontal scrollbar and the height of the vertical
225     *         scrollbar in pixels
226     *
227     * @deprecated Use {@link #getScaledScrollBarSize()} instead.
228     */
229    @Deprecated
230    public static int getScrollBarSize() {
231        return SCROLL_BAR_SIZE;
232    }
233
234    /**
235     * @return The width of the horizontal scrollbar and the height of the vertical
236     *         scrollbar in pixels
237     */
238    public int getScaledScrollBarSize() {
239        return mScrollbarSize;
240    }
241
242    /**
243     * @return Duration of the fade when scrollbars fade away in milliseconds
244     */
245    public static int getScrollBarFadeDuration() {
246        return SCROLL_BAR_FADE_DURATION;
247    }
248
249    /**
250     * @return Default delay before the scrollbars fade in milliseconds
251     */
252    public static int getScrollDefaultDelay() {
253        return SCROLL_BAR_DEFAULT_DELAY;
254    }
255
256    /**
257     * @return the length of the fading edges in pixels
258     *
259     * @deprecated Use {@link #getScaledFadingEdgeLength()} instead.
260     */
261    @Deprecated
262    public static int getFadingEdgeLength() {
263        return FADING_EDGE_LENGTH;
264    }
265
266    /**
267     * @return the length of the fading edges in pixels
268     */
269    public int getScaledFadingEdgeLength() {
270        return mFadingEdgeLength;
271    }
272
273    /**
274     * @return the duration in milliseconds of the pressed state in child
275     * components.
276     */
277    public static int getPressedStateDuration() {
278        return PRESSED_STATE_DURATION;
279    }
280
281    /**
282     * @return the duration in milliseconds before a press turns into
283     * a long press
284     */
285    public static int getLongPressTimeout() {
286        return LONG_PRESS_TIMEOUT;
287    }
288
289    /**
290     * @return the duration in milliseconds we will wait to see if a touch event
291     * is a tap or a scroll. If the user does not move within this interval, it is
292     * considered to be a tap.
293     */
294    public static int getTapTimeout() {
295        return TAP_TIMEOUT;
296    }
297
298    /**
299     * @return the duration in milliseconds we will wait to see if a touch event
300     * is a jump tap. If the user does not move within this interval, it is
301     * considered to be a tap.
302     */
303    public static int getJumpTapTimeout() {
304        return JUMP_TAP_TIMEOUT;
305    }
306
307    /**
308     * @return the duration in milliseconds between the first tap's up event and
309     * the second tap's down event for an interaction to be considered a
310     * double-tap.
311     */
312    public static int getDoubleTapTimeout() {
313        return DOUBLE_TAP_TIMEOUT;
314    }
315
316    /**
317     * @return Inset in pixels to look for touchable content when the user touches the edge of the
318     *         screen
319     *
320     * @deprecated Use {@link #getScaledEdgeSlop()} instead.
321     */
322    @Deprecated
323    public static int getEdgeSlop() {
324        return EDGE_SLOP;
325    }
326
327    /**
328     * @return Inset in pixels to look for touchable content when the user touches the edge of the
329     *         screen
330     */
331    public int getScaledEdgeSlop() {
332        return mEdgeSlop;
333    }
334
335    /**
336     * @return Distance a touch can wander before we think the user is scrolling in pixels
337     *
338     * @deprecated Use {@link #getScaledTouchSlop()} instead.
339     */
340    @Deprecated
341    public static int getTouchSlop() {
342        return TOUCH_SLOP;
343    }
344
345    /**
346     * @return Distance a touch can wander before we think the user is scrolling in pixels
347     */
348    public int getScaledTouchSlop() {
349        return mTouchSlop;
350    }
351
352    /**
353     * @return Distance a touch can wander before we think the user is scrolling a full page
354     *         in dips
355     */
356    public int getScaledPagingTouchSlop() {
357        return mPagingTouchSlop;
358    }
359
360    /**
361     * @return Distance between the first touch and second touch to still be
362     *         considered a double tap
363     * @deprecated Use {@link #getScaledDoubleTapSlop()} instead.
364     * @hide The only client of this should be GestureDetector, which needs this
365     *       for clients that still use its deprecated constructor.
366     */
367    @Deprecated
368    public static int getDoubleTapSlop() {
369        return DOUBLE_TAP_SLOP;
370    }
371
372    /**
373     * @return Distance between the first touch and second touch to still be
374     *         considered a double tap
375     */
376    public int getScaledDoubleTapSlop() {
377        return mDoubleTapSlop;
378    }
379
380    /**
381     * @return Distance a touch must be outside the bounds of a window for it
382     * to be counted as outside the window for purposes of dismissing that
383     * window.
384     *
385     * @deprecated Use {@link #getScaledWindowTouchSlop()} instead.
386     */
387    @Deprecated
388    public static int getWindowTouchSlop() {
389        return WINDOW_TOUCH_SLOP;
390    }
391
392    /**
393     * @return Distance a touch must be outside the bounds of a window for it
394     * to be counted as outside the window for purposes of dismissing that
395     * window.
396     */
397    public int getScaledWindowTouchSlop() {
398        return mWindowTouchSlop;
399    }
400
401    /**
402     * @return Minimum velocity to initiate a fling, as measured in pixels per second.
403     *
404     * @deprecated Use {@link #getScaledMinimumFlingVelocity()} instead.
405     */
406    @Deprecated
407    public static int getMinimumFlingVelocity() {
408        return MINIMUM_FLING_VELOCITY;
409    }
410
411    /**
412     * @return Minimum velocity to initiate a fling, as measured in pixels per second.
413     */
414    public int getScaledMinimumFlingVelocity() {
415        return mMinimumFlingVelocity;
416    }
417
418    /**
419     * @return Maximum velocity to initiate a fling, as measured in pixels per second.
420     *
421     * @deprecated Use {@link #getScaledMaximumFlingVelocity()} instead.
422     */
423    @Deprecated
424    public static int getMaximumFlingVelocity() {
425        return MAXIMUM_FLING_VELOCITY;
426    }
427
428    /**
429     * @return Maximum velocity to initiate a fling, as measured in pixels per second.
430     */
431    public int getScaledMaximumFlingVelocity() {
432        return mMaximumFlingVelocity;
433    }
434
435    /**
436     * The maximum drawing cache size expressed in bytes.
437     *
438     * @return the maximum size of View's drawing cache expressed in bytes
439     *
440     * @deprecated Use {@link #getScaledMaximumDrawingCacheSize()} instead.
441     */
442    @Deprecated
443    public static int getMaximumDrawingCacheSize() {
444        //noinspection deprecation
445        return MAXIMUM_DRAWING_CACHE_SIZE;
446    }
447
448    /**
449     * The maximum drawing cache size expressed in bytes.
450     *
451     * @return the maximum size of View's drawing cache expressed in bytes
452     */
453    public int getScaledMaximumDrawingCacheSize() {
454        return mMaximumDrawingCacheSize;
455    }
456
457    /**
458     * The amount of time that the zoom controls should be
459     * displayed on the screen expressed in milliseconds.
460     *
461     * @return the time the zoom controls should be visible expressed
462     * in milliseconds.
463     */
464    public static long getZoomControlsTimeout() {
465        return ZOOM_CONTROLS_TIMEOUT;
466    }
467
468    /**
469     * The amount of time a user needs to press the relevant key to bring up
470     * the global actions dialog.
471     *
472     * @return how long a user needs to press the relevant key to bring up
473     *   the global actions dialog.
474     */
475    public static long getGlobalActionKeyTimeout() {
476        return GLOBAL_ACTIONS_KEY_TIMEOUT;
477    }
478
479    /**
480     * The amount of friction applied to scrolls and flings.
481     *
482     * @return A scalar dimensionless value representing the coefficient of
483     *         friction.
484     */
485    public static float getScrollFriction() {
486        return SCROLL_FRICTION;
487    }
488}
489