ViewConfiguration.java revision 8b2c00144ac262a6fbdbbac1ed8dfe813b883f81
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.app.AppGlobals;
20import android.content.Context;
21import android.content.res.Configuration;
22import android.os.Bundle;
23import android.provider.Settings;
24import android.util.DisplayMetrics;
25import android.util.SparseArray;
26
27/**
28 * Contains methods to standard constants used in the UI for timeouts, sizes, and distances.
29 */
30public class ViewConfiguration {
31    /**
32     * Expected bit depth of the display panel.
33     *
34     * @hide
35     */
36    public static final float PANEL_BIT_DEPTH = 24;
37
38    /**
39     * Minimum alpha required for a view to draw.
40     *
41     * @hide
42     */
43    public static final float ALPHA_THRESHOLD = 0.5f / PANEL_BIT_DEPTH;
44    /**
45     * @hide
46     */
47    public static final float ALPHA_THRESHOLD_INT = 0x7f / PANEL_BIT_DEPTH;
48
49    /**
50     * Defines the width of the horizontal scrollbar and the height of the vertical scrollbar in
51     * pixels
52     */
53    private static final int SCROLL_BAR_SIZE = 10;
54
55    /**
56     * Duration of the fade when scrollbars fade away in milliseconds
57     */
58    private static final int SCROLL_BAR_FADE_DURATION = 250;
59
60    /**
61     * Default delay before the scrollbars fade in milliseconds
62     */
63    private static final int SCROLL_BAR_DEFAULT_DELAY = 300;
64
65    /**
66     * Defines the length of the fading edges in pixels
67     */
68    private static final int FADING_EDGE_LENGTH = 12;
69
70    /**
71     * Defines the duration in milliseconds of the pressed state in child
72     * components.
73     */
74    private static final int PRESSED_STATE_DURATION = 125;
75
76    /**
77     * Defines the default duration in milliseconds before a press turns into
78     * a long press
79     */
80    private static final int DEFAULT_LONG_PRESS_TIMEOUT = 500;
81
82    /**
83     * Defines the time between successive key repeats in milliseconds.
84     */
85    private static final int KEY_REPEAT_DELAY = 50;
86
87    /**
88     * Defines the duration in milliseconds a user needs to hold down the
89     * appropriate button to bring up the global actions dialog (power off,
90     * lock screen, etc).
91     */
92    private static final int GLOBAL_ACTIONS_KEY_TIMEOUT = 500;
93
94    /**
95     * Defines the duration in milliseconds we will wait to see if a touch event
96     * is a tap or a scroll. If the user does not move within this interval, it is
97     * considered to be a tap.
98     */
99    private static final int TAP_TIMEOUT = 115;
100
101    /**
102     * Defines the duration in milliseconds we will wait to see if a touch event
103     * is a jump tap. If the user does not complete the jump tap within this interval, it is
104     * considered to be a tap.
105     */
106    private static final int JUMP_TAP_TIMEOUT = 500;
107
108    /**
109     * Defines the duration in milliseconds between the first tap's up event and
110     * the second tap's down event for an interaction to be considered a
111     * double-tap.
112     */
113    private static final int DOUBLE_TAP_TIMEOUT = 300;
114
115    /**
116     * Defines the maximum duration in milliseconds between a touch pad
117     * touch and release for a given touch to be considered a tap (click) as
118     * opposed to a hover movement gesture.
119     */
120    private static final int HOVER_TAP_TIMEOUT = 150;
121
122    /**
123     * Defines the maximum distance in pixels that a touch pad touch can move
124     * before being released for it to be considered a tap (click) as opposed
125     * to a hover movement gesture.
126     */
127    private static final int HOVER_TAP_SLOP = 20;
128
129    /**
130     * Defines the duration in milliseconds we want to display zoom controls in response
131     * to a user panning within an application.
132     */
133    private static final int ZOOM_CONTROLS_TIMEOUT = 3000;
134
135    /**
136     * Inset in pixels to look for touchable content when the user touches the edge of the screen
137     */
138    private static final int EDGE_SLOP = 12;
139
140    /**
141     * Distance a touch can wander before we think the user is scrolling in pixels
142     */
143    private static final int TOUCH_SLOP = 16;
144
145    /**
146     * Distance a touch can wander before we think the user is the first touch
147     * in a sequence of double tap
148     */
149    private static final int LARGE_TOUCH_SLOP = 18;
150
151    /**
152     * Distance a touch can wander before we think the user is attempting a paged scroll
153     * (in dips)
154     */
155    private static final int PAGING_TOUCH_SLOP = TOUCH_SLOP * 2;
156
157    /**
158     * Distance between the first touch and second touch to still be considered a double tap
159     */
160    private static final int DOUBLE_TAP_SLOP = 100;
161
162    /**
163     * Distance a touch needs to be outside of a window's bounds for it to
164     * count as outside for purposes of dismissing the window.
165     */
166    private static final int WINDOW_TOUCH_SLOP = 16;
167
168    /**
169     * Minimum velocity to initiate a fling, as measured in pixels per second
170     */
171    private static final int MINIMUM_FLING_VELOCITY = 50;
172
173    /**
174     * Maximum velocity to initiate a fling, as measured in pixels per second
175     */
176    private static final int MAXIMUM_FLING_VELOCITY = 8000;
177
178    /**
179     * The maximum size of View's drawing cache, expressed in bytes. This size
180     * should be at least equal to the size of the screen in ARGB888 format.
181     */
182    @Deprecated
183    private static final int MAXIMUM_DRAWING_CACHE_SIZE = 480 * 800 * 4; // ARGB8888
184
185    /**
186     * The coefficient of friction applied to flings/scrolls.
187     */
188    private static final float SCROLL_FRICTION = 0.015f;
189
190    /**
191     * Max distance to overscroll for edge effects
192     */
193    private static final int OVERSCROLL_DISTANCE = 0;
194
195    /**
196     * Max distance to overfling for edge effects
197     */
198    private static final int OVERFLING_DISTANCE = 6;
199
200    private final int mEdgeSlop;
201    private final int mFadingEdgeLength;
202    private final int mMinimumFlingVelocity;
203    private final int mMaximumFlingVelocity;
204    private final int mScrollbarSize;
205    private final int mTouchSlop;
206    private final int mLargeTouchSlop;
207    private final int mPagingTouchSlop;
208    private final int mDoubleTapSlop;
209    private final int mWindowTouchSlop;
210    private final int mMaximumDrawingCacheSize;
211    private final int mOverscrollDistance;
212    private final int mOverflingDistance;
213
214    private static final SparseArray<ViewConfiguration> sConfigurations =
215            new SparseArray<ViewConfiguration>(2);
216
217    /**
218     * @deprecated Use {@link android.view.ViewConfiguration#get(android.content.Context)} instead.
219     */
220    @Deprecated
221    public ViewConfiguration() {
222        mEdgeSlop = EDGE_SLOP;
223        mFadingEdgeLength = FADING_EDGE_LENGTH;
224        mMinimumFlingVelocity = MINIMUM_FLING_VELOCITY;
225        mMaximumFlingVelocity = MAXIMUM_FLING_VELOCITY;
226        mScrollbarSize = SCROLL_BAR_SIZE;
227        mTouchSlop = TOUCH_SLOP;
228        mLargeTouchSlop = LARGE_TOUCH_SLOP;
229        mPagingTouchSlop = PAGING_TOUCH_SLOP;
230        mDoubleTapSlop = DOUBLE_TAP_SLOP;
231        mWindowTouchSlop = WINDOW_TOUCH_SLOP;
232        //noinspection deprecation
233        mMaximumDrawingCacheSize = MAXIMUM_DRAWING_CACHE_SIZE;
234        mOverscrollDistance = OVERSCROLL_DISTANCE;
235        mOverflingDistance = OVERFLING_DISTANCE;
236    }
237
238    /**
239     * Creates a new configuration for the specified context. The configuration depends on
240     * various parameters of the context, like the dimension of the display or the density
241     * of the display.
242     *
243     * @param context The application context used to initialize this view configuration.
244     *
245     * @see #get(android.content.Context)
246     * @see android.util.DisplayMetrics
247     */
248    private ViewConfiguration(Context context) {
249        final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
250        final float density = metrics.density;
251        final float sizeAndDensity;
252        if (context.getResources().getConfiguration().isLayoutSizeAtLeast(
253                Configuration.SCREENLAYOUT_SIZE_XLARGE)) {
254            sizeAndDensity = density * 1.5f;
255        } else {
256            sizeAndDensity = density;
257        }
258
259        mEdgeSlop = (int) (sizeAndDensity * EDGE_SLOP + 0.5f);
260        mFadingEdgeLength = (int) (sizeAndDensity * FADING_EDGE_LENGTH + 0.5f);
261        mMinimumFlingVelocity = (int) (density * MINIMUM_FLING_VELOCITY + 0.5f);
262        mMaximumFlingVelocity = (int) (density * MAXIMUM_FLING_VELOCITY + 0.5f);
263        mScrollbarSize = (int) (density * SCROLL_BAR_SIZE + 0.5f);
264        mTouchSlop = (int) (sizeAndDensity * TOUCH_SLOP + 0.5f);
265        mLargeTouchSlop =  (int) (sizeAndDensity * LARGE_TOUCH_SLOP + 0.5f);
266        mPagingTouchSlop = (int) (sizeAndDensity * PAGING_TOUCH_SLOP + 0.5f);
267        mDoubleTapSlop = (int) (sizeAndDensity * DOUBLE_TAP_SLOP + 0.5f);
268        mWindowTouchSlop = (int) (sizeAndDensity * WINDOW_TOUCH_SLOP + 0.5f);
269
270        // Size of the screen in bytes, in ARGB_8888 format
271        mMaximumDrawingCacheSize = 4 * metrics.widthPixels * metrics.heightPixels;
272
273        mOverscrollDistance = (int) (sizeAndDensity * OVERSCROLL_DISTANCE + 0.5f);
274        mOverflingDistance = (int) (sizeAndDensity * OVERFLING_DISTANCE + 0.5f);
275    }
276
277    /**
278     * Returns a configuration for the specified context. The configuration depends on
279     * various parameters of the context, like the dimension of the display or the
280     * density of the display.
281     *
282     * @param context The application context used to initialize the view configuration.
283     */
284    public static ViewConfiguration get(Context context) {
285        final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
286        final int density = (int) (100.0f * metrics.density);
287
288        ViewConfiguration configuration = sConfigurations.get(density);
289        if (configuration == null) {
290            configuration = new ViewConfiguration(context);
291            sConfigurations.put(density, configuration);
292        }
293
294        return configuration;
295    }
296
297    /**
298     * @return The width of the horizontal scrollbar and the height of the vertical
299     *         scrollbar in pixels
300     *
301     * @deprecated Use {@link #getScaledScrollBarSize()} instead.
302     */
303    @Deprecated
304    public static int getScrollBarSize() {
305        return SCROLL_BAR_SIZE;
306    }
307
308    /**
309     * @return The width of the horizontal scrollbar and the height of the vertical
310     *         scrollbar in pixels
311     */
312    public int getScaledScrollBarSize() {
313        return mScrollbarSize;
314    }
315
316    /**
317     * @return Duration of the fade when scrollbars fade away in milliseconds
318     */
319    public static int getScrollBarFadeDuration() {
320        return SCROLL_BAR_FADE_DURATION;
321    }
322
323    /**
324     * @return Default delay before the scrollbars fade in milliseconds
325     */
326    public static int getScrollDefaultDelay() {
327        return SCROLL_BAR_DEFAULT_DELAY;
328    }
329
330    /**
331     * @return the length of the fading edges in pixels
332     *
333     * @deprecated Use {@link #getScaledFadingEdgeLength()} instead.
334     */
335    @Deprecated
336    public static int getFadingEdgeLength() {
337        return FADING_EDGE_LENGTH;
338    }
339
340    /**
341     * @return the length of the fading edges in pixels
342     */
343    public int getScaledFadingEdgeLength() {
344        return mFadingEdgeLength;
345    }
346
347    /**
348     * @return the duration in milliseconds of the pressed state in child
349     * components.
350     */
351    public static int getPressedStateDuration() {
352        return PRESSED_STATE_DURATION;
353    }
354
355    /**
356     * @return the duration in milliseconds before a press turns into
357     * a long press
358     */
359    public static int getLongPressTimeout() {
360        return AppGlobals.getIntCoreSetting(Settings.Secure.LONG_PRESS_TIMEOUT,
361                DEFAULT_LONG_PRESS_TIMEOUT);
362    }
363
364    /**
365     * @return the time before the first key repeat in milliseconds.
366     */
367    public static int getKeyRepeatTimeout() {
368        return getLongPressTimeout();
369    }
370
371    /**
372     * @return the time between successive key repeats in milliseconds.
373     */
374    public static int getKeyRepeatDelay() {
375        return KEY_REPEAT_DELAY;
376    }
377
378    /**
379     * @return the duration in milliseconds we will wait to see if a touch event
380     * is a tap or a scroll. If the user does not move within this interval, it is
381     * considered to be a tap.
382     */
383    public static int getTapTimeout() {
384        return TAP_TIMEOUT;
385    }
386
387    /**
388     * @return the duration in milliseconds we will wait to see if a touch event
389     * is a jump tap. If the user does not move within this interval, it is
390     * considered to be a tap.
391     */
392    public static int getJumpTapTimeout() {
393        return JUMP_TAP_TIMEOUT;
394    }
395
396    /**
397     * @return the duration in milliseconds between the first tap's up event and
398     * the second tap's down event for an interaction to be considered a
399     * double-tap.
400     */
401    public static int getDoubleTapTimeout() {
402        return DOUBLE_TAP_TIMEOUT;
403    }
404
405    /**
406     * @return the maximum duration in milliseconds between a touch pad
407     * touch and release for a given touch to be considered a tap (click) as
408     * opposed to a hover movement gesture.
409     * @hide
410     */
411    public static int getHoverTapTimeout() {
412        return HOVER_TAP_TIMEOUT;
413    }
414
415    /**
416     * @return the maximum distance in pixels that a touch pad touch can move
417     * before being released for it to be considered a tap (click) as opposed
418     * to a hover movement gesture.
419     * @hide
420     */
421    public static int getHoverTapSlop() {
422        return HOVER_TAP_SLOP;
423    }
424
425    /**
426     * @return Inset in pixels to look for touchable content when the user touches the edge of the
427     *         screen
428     *
429     * @deprecated Use {@link #getScaledEdgeSlop()} instead.
430     */
431    @Deprecated
432    public static int getEdgeSlop() {
433        return EDGE_SLOP;
434    }
435
436    /**
437     * @return Inset in pixels to look for touchable content when the user touches the edge of the
438     *         screen
439     */
440    public int getScaledEdgeSlop() {
441        return mEdgeSlop;
442    }
443
444    /**
445     * @return Distance a touch can wander before we think the user is scrolling in pixels
446     *
447     * @deprecated Use {@link #getScaledTouchSlop()} instead.
448     */
449    @Deprecated
450    public static int getTouchSlop() {
451        return TOUCH_SLOP;
452    }
453
454    /**
455     * @return Distance a touch can wander before we think the user is scrolling in pixels
456     */
457    public int getScaledTouchSlop() {
458        return mTouchSlop;
459    }
460
461    /**
462     * @return Distance a touch can wander before we think the user is the first touch
463     *         in a sequence of double tap
464     */
465    public int getScaledLargeTouchSlop() {
466        return mLargeTouchSlop;
467    }
468
469    /**
470     * @return Distance a touch can wander before we think the user is scrolling a full page
471     *         in dips
472     */
473    public int getScaledPagingTouchSlop() {
474        return mPagingTouchSlop;
475    }
476
477    /**
478     * @return Distance between the first touch and second touch to still be
479     *         considered a double tap
480     * @deprecated Use {@link #getScaledDoubleTapSlop()} instead.
481     * @hide The only client of this should be GestureDetector, which needs this
482     *       for clients that still use its deprecated constructor.
483     */
484    @Deprecated
485    public static int getDoubleTapSlop() {
486        return DOUBLE_TAP_SLOP;
487    }
488
489    /**
490     * @return Distance between the first touch and second touch to still be
491     *         considered a double tap
492     */
493    public int getScaledDoubleTapSlop() {
494        return mDoubleTapSlop;
495    }
496
497    /**
498     * @return Distance a touch must be outside the bounds of a window for it
499     * to be counted as outside the window for purposes of dismissing that
500     * window.
501     *
502     * @deprecated Use {@link #getScaledWindowTouchSlop()} instead.
503     */
504    @Deprecated
505    public static int getWindowTouchSlop() {
506        return WINDOW_TOUCH_SLOP;
507    }
508
509    /**
510     * @return Distance a touch must be outside the bounds of a window for it
511     * to be counted as outside the window for purposes of dismissing that
512     * window.
513     */
514    public int getScaledWindowTouchSlop() {
515        return mWindowTouchSlop;
516    }
517
518    /**
519     * @return Minimum velocity to initiate a fling, as measured in pixels per second.
520     *
521     * @deprecated Use {@link #getScaledMinimumFlingVelocity()} instead.
522     */
523    @Deprecated
524    public static int getMinimumFlingVelocity() {
525        return MINIMUM_FLING_VELOCITY;
526    }
527
528    /**
529     * @return Minimum velocity to initiate a fling, as measured in pixels per second.
530     */
531    public int getScaledMinimumFlingVelocity() {
532        return mMinimumFlingVelocity;
533    }
534
535    /**
536     * @return Maximum velocity to initiate a fling, as measured in pixels per second.
537     *
538     * @deprecated Use {@link #getScaledMaximumFlingVelocity()} instead.
539     */
540    @Deprecated
541    public static int getMaximumFlingVelocity() {
542        return MAXIMUM_FLING_VELOCITY;
543    }
544
545    /**
546     * @return Maximum velocity to initiate a fling, as measured in pixels per second.
547     */
548    public int getScaledMaximumFlingVelocity() {
549        return mMaximumFlingVelocity;
550    }
551
552    /**
553     * The maximum drawing cache size expressed in bytes.
554     *
555     * @return the maximum size of View's drawing cache expressed in bytes
556     *
557     * @deprecated Use {@link #getScaledMaximumDrawingCacheSize()} instead.
558     */
559    @Deprecated
560    public static int getMaximumDrawingCacheSize() {
561        //noinspection deprecation
562        return MAXIMUM_DRAWING_CACHE_SIZE;
563    }
564
565    /**
566     * The maximum drawing cache size expressed in bytes.
567     *
568     * @return the maximum size of View's drawing cache expressed in bytes
569     */
570    public int getScaledMaximumDrawingCacheSize() {
571        return mMaximumDrawingCacheSize;
572    }
573
574    /**
575     * @return The maximum distance a View should overscroll by when showing edge effects.
576     */
577    public int getScaledOverscrollDistance() {
578        return mOverscrollDistance;
579    }
580
581    /**
582     * @return The maximum distance a View should overfling by when showing edge effects.
583     */
584    public int getScaledOverflingDistance() {
585        return mOverflingDistance;
586    }
587
588    /**
589     * The amount of time that the zoom controls should be
590     * displayed on the screen expressed in milliseconds.
591     *
592     * @return the time the zoom controls should be visible expressed
593     * in milliseconds.
594     */
595    public static long getZoomControlsTimeout() {
596        return ZOOM_CONTROLS_TIMEOUT;
597    }
598
599    /**
600     * The amount of time a user needs to press the relevant key to bring up
601     * the global actions dialog.
602     *
603     * @return how long a user needs to press the relevant key to bring up
604     *   the global actions dialog.
605     */
606    public static long getGlobalActionKeyTimeout() {
607        return GLOBAL_ACTIONS_KEY_TIMEOUT;
608    }
609
610    /**
611     * The amount of friction applied to scrolls and flings.
612     *
613     * @return A scalar dimensionless value representing the coefficient of
614     *         friction.
615     */
616    public static float getScrollFriction() {
617        return SCROLL_FRICTION;
618    }
619}
620