blob: cc4e89cce417a01c2a91cb09499741ab530fb79f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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;
Dianne Hackbornff801ec2011-01-22 18:05:38 -080020import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import 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 /**
Romain Guydbc26d22010-10-11 17:58:29 -070029 * 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;
Romain Guy909cbaf2010-10-13 18:19:48 -070041 /**
42 * @hide
43 */
44 public static final float ALPHA_THRESHOLD_INT = 0x7f / PANEL_BIT_DEPTH;
45
Romain Guydbc26d22010-10-11 17:58:29 -070046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 * 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 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070053 * 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 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 * 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 */
Adam Powelle14579b2009-12-16 18:39:52 -080071 private static final int PRESSED_STATE_DURATION = 125;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
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 */
Adam Powelle14579b2009-12-16 18:39:52 -080091 private static final int TAP_TIMEOUT = 115;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
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 */
Dianne Hackborn3e2ac882009-09-25 01:34:28 -0700121 private static final int TOUCH_SLOP = 16;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
123 /**
Adam Powellde8d0832010-03-09 17:11:30 -0800124 * Distance a touch can wander before we think the user is attempting a paged scroll
125 * (in dips)
126 */
Adam Powellb7ef1d92010-03-11 10:25:24 -0800127 private static final int PAGING_TOUCH_SLOP = TOUCH_SLOP * 2;
Adam Powellde8d0832010-03-09 17:11:30 -0800128
129 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 * 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;
Romain Guy4296fc42009-07-06 11:48:52 -0700144
145 /**
146 * Maximum velocity to initiate a fling, as measured in pixels per second
147 */
Adam Powell637d3372010-08-25 14:37:03 -0700148 private static final int MAXIMUM_FLING_VELOCITY = 8000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
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
Romain Guy0211a0a2011-02-14 16:34:59 -0800155 private static final int MAXIMUM_DRAWING_CACHE_SIZE = 480 * 800 * 4; // ARGB8888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
157 /**
158 * The coefficient of friction applied to flings/scrolls.
159 */
Romain Guy0211a0a2011-02-14 16:34:59 -0800160 private static final float SCROLL_FRICTION = 0.015f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Adam Powell637d3372010-08-25 14:37:03 -0700162 /**
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 */
Gilles Debunne8ce7aab2011-01-25 11:23:30 -0800170 private static final int OVERFLING_DISTANCE = 6;
Adam Powell637d3372010-08-25 14:37:03 -0700171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 private final int mEdgeSlop;
173 private final int mFadingEdgeLength;
174 private final int mMinimumFlingVelocity;
Romain Guy4296fc42009-07-06 11:48:52 -0700175 private final int mMaximumFlingVelocity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 private final int mScrollbarSize;
177 private final int mTouchSlop;
Adam Powellde8d0832010-03-09 17:11:30 -0800178 private final int mPagingTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 private final int mDoubleTapSlop;
180 private final int mWindowTouchSlop;
181 private final int mMaximumDrawingCacheSize;
Adam Powell637d3372010-08-25 14:37:03 -0700182 private final int mOverscrollDistance;
183 private final int mOverflingDistance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184
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;
Romain Guy4296fc42009-07-06 11:48:52 -0700196 mMaximumFlingVelocity = MAXIMUM_FLING_VELOCITY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 mScrollbarSize = SCROLL_BAR_SIZE;
198 mTouchSlop = TOUCH_SLOP;
Adam Powellde8d0832010-03-09 17:11:30 -0800199 mPagingTouchSlop = PAGING_TOUCH_SLOP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 mDoubleTapSlop = DOUBLE_TAP_SLOP;
201 mWindowTouchSlop = WINDOW_TOUCH_SLOP;
202 //noinspection deprecation
203 mMaximumDrawingCacheSize = MAXIMUM_DRAWING_CACHE_SIZE;
Adam Powell637d3372010-08-25 14:37:03 -0700204 mOverscrollDistance = OVERSCROLL_DISTANCE;
205 mOverflingDistance = OVERFLING_DISTANCE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 }
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;
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800221 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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800229 mEdgeSlop = (int) (sizeAndDensity * EDGE_SLOP + 0.5f);
230 mFadingEdgeLength = (int) (sizeAndDensity * FADING_EDGE_LENGTH + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 mMinimumFlingVelocity = (int) (density * MINIMUM_FLING_VELOCITY + 0.5f);
Romain Guy4296fc42009-07-06 11:48:52 -0700232 mMaximumFlingVelocity = (int) (density * MAXIMUM_FLING_VELOCITY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 mScrollbarSize = (int) (density * SCROLL_BAR_SIZE + 0.5f);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800234 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
239 // Size of the screen in bytes, in ARGB_8888 format
240 mMaximumDrawingCacheSize = 4 * metrics.widthPixels * metrics.heightPixels;
Adam Powell637d3372010-08-25 14:37:03 -0700241
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800242 mOverscrollDistance = (int) (sizeAndDensity * OVERSCROLL_DISTANCE + 0.5f);
243 mOverflingDistance = (int) (sizeAndDensity * OVERFLING_DISTANCE + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 }
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 /**
Mike Cleronf116bf82009-09-27 19:14:12 -0700286 * @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 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 * @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.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 */
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 }
Adam Powellde8d0832010-03-09 17:11:30 -0800394
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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 */
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 /**
Romain Guy4296fc42009-07-06 11:48:52 -0700462 * @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 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 * 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 /**
Adam Powell637d3372010-08-25 14:37:03 -0700501 * @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 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 * 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}