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