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