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