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