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