ViewConfiguration.java revision 3fb3d7c4e756bd32d5abde0abca9ab52d559bc84
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 duration in milliseconds we want to display zoom controls in response
117     * to a user panning within an application.
118     */
119    private static final int ZOOM_CONTROLS_TIMEOUT = 3000;
120
121    /**
122     * Inset in pixels to look for touchable content when the user touches the edge of the screen
123     */
124    private static final int EDGE_SLOP = 12;
125
126    /**
127     * Distance a touch can wander before we think the user is scrolling in pixels
128     */
129    private static final int TOUCH_SLOP = 16;
130
131    /**
132     * Distance a touch can wander before we think the user is attempting a paged scroll
133     * (in dips)
134     */
135    private static final int PAGING_TOUCH_SLOP = TOUCH_SLOP * 2;
136
137    /**
138     * Distance between the first touch and second touch to still be considered a double tap
139     */
140    private static final int DOUBLE_TAP_SLOP = 100;
141
142    /**
143     * Distance a touch needs to be outside of a window's bounds for it to
144     * count as outside for purposes of dismissing the window.
145     */
146    private static final int WINDOW_TOUCH_SLOP = 16;
147
148    /**
149     * Minimum velocity to initiate a fling, as measured in pixels per second
150     */
151    private static final int MINIMUM_FLING_VELOCITY = 50;
152
153    /**
154     * Maximum velocity to initiate a fling, as measured in pixels per second
155     */
156    private static final int MAXIMUM_FLING_VELOCITY = 8000;
157
158    /**
159     * The maximum size of View's drawing cache, expressed in bytes. This size
160     * should be at least equal to the size of the screen in ARGB888 format.
161     */
162    @Deprecated
163    private static final int MAXIMUM_DRAWING_CACHE_SIZE = 480 * 800 * 4; // ARGB8888
164
165    /**
166     * The coefficient of friction applied to flings/scrolls.
167     */
168    private static final float SCROLL_FRICTION = 0.015f;
169
170    /**
171     * Max distance to overscroll for edge effects
172     */
173    private static final int OVERSCROLL_DISTANCE = 0;
174
175    /**
176     * Max distance to overfling for edge effects
177     */
178    private static final int OVERFLING_DISTANCE = 6;
179
180    private final int mEdgeSlop;
181    private final int mFadingEdgeLength;
182    private final int mMinimumFlingVelocity;
183    private final int mMaximumFlingVelocity;
184    private final int mScrollbarSize;
185    private final int mTouchSlop;
186    private final int mPagingTouchSlop;
187    private final int mDoubleTapSlop;
188    private final int mWindowTouchSlop;
189    private final int mMaximumDrawingCacheSize;
190    private final int mOverscrollDistance;
191    private final int mOverflingDistance;
192
193    private static final SparseArray<ViewConfiguration> sConfigurations =
194            new SparseArray<ViewConfiguration>(2);
195
196    /**
197     * @deprecated Use {@link android.view.ViewConfiguration#get(android.content.Context)} instead.
198     */
199    @Deprecated
200    public ViewConfiguration() {
201        mEdgeSlop = EDGE_SLOP;
202        mFadingEdgeLength = FADING_EDGE_LENGTH;
203        mMinimumFlingVelocity = MINIMUM_FLING_VELOCITY;
204        mMaximumFlingVelocity = MAXIMUM_FLING_VELOCITY;
205        mScrollbarSize = SCROLL_BAR_SIZE;
206        mTouchSlop = TOUCH_SLOP;
207        mPagingTouchSlop = PAGING_TOUCH_SLOP;
208        mDoubleTapSlop = DOUBLE_TAP_SLOP;
209        mWindowTouchSlop = WINDOW_TOUCH_SLOP;
210        //noinspection deprecation
211        mMaximumDrawingCacheSize = MAXIMUM_DRAWING_CACHE_SIZE;
212        mOverscrollDistance = OVERSCROLL_DISTANCE;
213        mOverflingDistance = OVERFLING_DISTANCE;
214    }
215
216    /**
217     * Creates a new configuration for the specified context. The configuration depends on
218     * various parameters of the context, like the dimension of the display or the density
219     * of the display.
220     *
221     * @param context The application context used to initialize this view configuration.
222     *
223     * @see #get(android.content.Context)
224     * @see android.util.DisplayMetrics
225     */
226    private ViewConfiguration(Context context) {
227        final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
228        final float density = metrics.density;
229        final float sizeAndDensity;
230        if (context.getResources().getConfiguration().isLayoutSizeAtLeast(
231                Configuration.SCREENLAYOUT_SIZE_XLARGE)) {
232            sizeAndDensity = density * 1.5f;
233        } else {
234            sizeAndDensity = density;
235        }
236
237        mEdgeSlop = (int) (sizeAndDensity * EDGE_SLOP + 0.5f);
238        mFadingEdgeLength = (int) (sizeAndDensity * FADING_EDGE_LENGTH + 0.5f);
239        mMinimumFlingVelocity = (int) (density * MINIMUM_FLING_VELOCITY + 0.5f);
240        mMaximumFlingVelocity = (int) (density * MAXIMUM_FLING_VELOCITY + 0.5f);
241        mScrollbarSize = (int) (density * SCROLL_BAR_SIZE + 0.5f);
242        mTouchSlop = (int) (sizeAndDensity * TOUCH_SLOP + 0.5f);
243        mPagingTouchSlop = (int) (sizeAndDensity * PAGING_TOUCH_SLOP + 0.5f);
244        mDoubleTapSlop = (int) (sizeAndDensity * DOUBLE_TAP_SLOP + 0.5f);
245        mWindowTouchSlop = (int) (sizeAndDensity * WINDOW_TOUCH_SLOP + 0.5f);
246
247        // Size of the screen in bytes, in ARGB_8888 format
248        mMaximumDrawingCacheSize = 4 * metrics.widthPixels * metrics.heightPixels;
249
250        mOverscrollDistance = (int) (sizeAndDensity * OVERSCROLL_DISTANCE + 0.5f);
251        mOverflingDistance = (int) (sizeAndDensity * OVERFLING_DISTANCE + 0.5f);
252    }
253
254    /**
255     * Returns a configuration for the specified context. The configuration depends on
256     * various parameters of the context, like the dimension of the display or the
257     * density of the display.
258     *
259     * @param context The application context used to initialize the view configuration.
260     */
261    public static ViewConfiguration get(Context context) {
262        final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
263        final int density = (int) (100.0f * metrics.density);
264
265        ViewConfiguration configuration = sConfigurations.get(density);
266        if (configuration == null) {
267            configuration = new ViewConfiguration(context);
268            sConfigurations.put(density, configuration);
269        }
270
271        return configuration;
272    }
273
274    /**
275     * @return The width of the horizontal scrollbar and the height of the vertical
276     *         scrollbar in pixels
277     *
278     * @deprecated Use {@link #getScaledScrollBarSize()} instead.
279     */
280    @Deprecated
281    public static int getScrollBarSize() {
282        return SCROLL_BAR_SIZE;
283    }
284
285    /**
286     * @return The width of the horizontal scrollbar and the height of the vertical
287     *         scrollbar in pixels
288     */
289    public int getScaledScrollBarSize() {
290        return mScrollbarSize;
291    }
292
293    /**
294     * @return Duration of the fade when scrollbars fade away in milliseconds
295     */
296    public static int getScrollBarFadeDuration() {
297        return SCROLL_BAR_FADE_DURATION;
298    }
299
300    /**
301     * @return Default delay before the scrollbars fade in milliseconds
302     */
303    public static int getScrollDefaultDelay() {
304        return SCROLL_BAR_DEFAULT_DELAY;
305    }
306
307    /**
308     * @return the length of the fading edges in pixels
309     *
310     * @deprecated Use {@link #getScaledFadingEdgeLength()} instead.
311     */
312    @Deprecated
313    public static int getFadingEdgeLength() {
314        return FADING_EDGE_LENGTH;
315    }
316
317    /**
318     * @return the length of the fading edges in pixels
319     */
320    public int getScaledFadingEdgeLength() {
321        return mFadingEdgeLength;
322    }
323
324    /**
325     * @return the duration in milliseconds of the pressed state in child
326     * components.
327     */
328    public static int getPressedStateDuration() {
329        return PRESSED_STATE_DURATION;
330    }
331
332    /**
333     * @return the duration in milliseconds before a press turns into
334     * a long press
335     */
336    public static int getLongPressTimeout() {
337        return AppGlobals.getIntCoreSetting(Settings.Secure.LONG_PRESS_TIMEOUT,
338                DEFAULT_LONG_PRESS_TIMEOUT);
339    }
340
341    /**
342     * @return the time before the first key repeat in milliseconds.
343     */
344    public static int getKeyRepeatTimeout() {
345        return getLongPressTimeout();
346    }
347
348    /**
349     * @return the time between successive key repeats in milliseconds.
350     */
351    public static int getKeyRepeatDelay() {
352        return KEY_REPEAT_DELAY;
353    }
354
355    /**
356     * @return the duration in milliseconds we will wait to see if a touch event
357     * is a tap or a scroll. If the user does not move within this interval, it is
358     * considered to be a tap.
359     */
360    public static int getTapTimeout() {
361        return TAP_TIMEOUT;
362    }
363
364    /**
365     * @return the duration in milliseconds we will wait to see if a touch event
366     * is a jump tap. If the user does not move within this interval, it is
367     * considered to be a tap.
368     */
369    public static int getJumpTapTimeout() {
370        return JUMP_TAP_TIMEOUT;
371    }
372
373    /**
374     * @return the duration in milliseconds between the first tap's up event and
375     * the second tap's down event for an interaction to be considered a
376     * double-tap.
377     */
378    public static int getDoubleTapTimeout() {
379        return DOUBLE_TAP_TIMEOUT;
380    }
381
382    /**
383     * @return Inset in pixels to look for touchable content when the user touches the edge of the
384     *         screen
385     *
386     * @deprecated Use {@link #getScaledEdgeSlop()} instead.
387     */
388    @Deprecated
389    public static int getEdgeSlop() {
390        return EDGE_SLOP;
391    }
392
393    /**
394     * @return Inset in pixels to look for touchable content when the user touches the edge of the
395     *         screen
396     */
397    public int getScaledEdgeSlop() {
398        return mEdgeSlop;
399    }
400
401    /**
402     * @return Distance a touch can wander before we think the user is scrolling in pixels
403     *
404     * @deprecated Use {@link #getScaledTouchSlop()} instead.
405     */
406    @Deprecated
407    public static int getTouchSlop() {
408        return TOUCH_SLOP;
409    }
410
411    /**
412     * @return Distance a touch can wander before we think the user is scrolling in pixels
413     */
414    public int getScaledTouchSlop() {
415        return mTouchSlop;
416    }
417
418    /**
419     * @return Distance a touch can wander before we think the user is scrolling a full page
420     *         in dips
421     */
422    public int getScaledPagingTouchSlop() {
423        return mPagingTouchSlop;
424    }
425
426    /**
427     * @return Distance between the first touch and second touch to still be
428     *         considered a double tap
429     * @deprecated Use {@link #getScaledDoubleTapSlop()} instead.
430     * @hide The only client of this should be GestureDetector, which needs this
431     *       for clients that still use its deprecated constructor.
432     */
433    @Deprecated
434    public static int getDoubleTapSlop() {
435        return DOUBLE_TAP_SLOP;
436    }
437
438    /**
439     * @return Distance between the first touch and second touch to still be
440     *         considered a double tap
441     */
442    public int getScaledDoubleTapSlop() {
443        return mDoubleTapSlop;
444    }
445
446    /**
447     * @return Distance a touch must be outside the bounds of a window for it
448     * to be counted as outside the window for purposes of dismissing that
449     * window.
450     *
451     * @deprecated Use {@link #getScaledWindowTouchSlop()} instead.
452     */
453    @Deprecated
454    public static int getWindowTouchSlop() {
455        return WINDOW_TOUCH_SLOP;
456    }
457
458    /**
459     * @return Distance a touch must be outside the bounds of a window for it
460     * to be counted as outside the window for purposes of dismissing that
461     * window.
462     */
463    public int getScaledWindowTouchSlop() {
464        return mWindowTouchSlop;
465    }
466
467    /**
468     * @return Minimum velocity to initiate a fling, as measured in pixels per second.
469     *
470     * @deprecated Use {@link #getScaledMinimumFlingVelocity()} instead.
471     */
472    @Deprecated
473    public static int getMinimumFlingVelocity() {
474        return MINIMUM_FLING_VELOCITY;
475    }
476
477    /**
478     * @return Minimum velocity to initiate a fling, as measured in pixels per second.
479     */
480    public int getScaledMinimumFlingVelocity() {
481        return mMinimumFlingVelocity;
482    }
483
484    /**
485     * @return Maximum velocity to initiate a fling, as measured in pixels per second.
486     *
487     * @deprecated Use {@link #getScaledMaximumFlingVelocity()} instead.
488     */
489    @Deprecated
490    public static int getMaximumFlingVelocity() {
491        return MAXIMUM_FLING_VELOCITY;
492    }
493
494    /**
495     * @return Maximum velocity to initiate a fling, as measured in pixels per second.
496     */
497    public int getScaledMaximumFlingVelocity() {
498        return mMaximumFlingVelocity;
499    }
500
501    /**
502     * The maximum drawing cache size expressed in bytes.
503     *
504     * @return the maximum size of View's drawing cache expressed in bytes
505     *
506     * @deprecated Use {@link #getScaledMaximumDrawingCacheSize()} instead.
507     */
508    @Deprecated
509    public static int getMaximumDrawingCacheSize() {
510        //noinspection deprecation
511        return MAXIMUM_DRAWING_CACHE_SIZE;
512    }
513
514    /**
515     * The maximum drawing cache size expressed in bytes.
516     *
517     * @return the maximum size of View's drawing cache expressed in bytes
518     */
519    public int getScaledMaximumDrawingCacheSize() {
520        return mMaximumDrawingCacheSize;
521    }
522
523    /**
524     * @return The maximum distance a View should overscroll by when showing edge effects.
525     */
526    public int getScaledOverscrollDistance() {
527        return mOverscrollDistance;
528    }
529
530    /**
531     * @return The maximum distance a View should overfling by when showing edge effects.
532     */
533    public int getScaledOverflingDistance() {
534        return mOverflingDistance;
535    }
536
537    /**
538     * The amount of time that the zoom controls should be
539     * displayed on the screen expressed in milliseconds.
540     *
541     * @return the time the zoom controls should be visible expressed
542     * in milliseconds.
543     */
544    public static long getZoomControlsTimeout() {
545        return ZOOM_CONTROLS_TIMEOUT;
546    }
547
548    /**
549     * The amount of time a user needs to press the relevant key to bring up
550     * the global actions dialog.
551     *
552     * @return how long a user needs to press the relevant key to bring up
553     *   the global actions dialog.
554     */
555    public static long getGlobalActionKeyTimeout() {
556        return GLOBAL_ACTIONS_KEY_TIMEOUT;
557    }
558
559    /**
560     * The amount of friction applied to scrolls and flings.
561     *
562     * @return A scalar dimensionless value representing the coefficient of
563     *         friction.
564     */
565    public static float getScrollFriction() {
566        return SCROLL_FRICTION;
567    }
568}
569