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