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