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