ViewConfiguration.java revision a3dcd0a3f1b48fc0d675db7e32c27415a14f6d08
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 duration in milliseconds we want to display zoom controls in response 117 * to a user panning within an application. 118 */ 119 private static final int ZOOM_CONTROLS_TIMEOUT = 3000; 120 121 /** 122 * Inset in pixels to look for touchable content when the user touches the edge of the screen 123 */ 124 private static final int EDGE_SLOP = 12; 125 126 /** 127 * Distance a touch can wander before we think the user is scrolling in pixels 128 */ 129 private static final int TOUCH_SLOP = 16; 130 131 /** 132 * Distance a touch can wander before we think the user is the first touch 133 * in a sequence of double tap 134 */ 135 private static final int LARGE_TOUCH_SLOP = 18; 136 137 /** 138 * Distance a touch can wander before we think the user is attempting a paged scroll 139 * (in dips) 140 */ 141 private static final int PAGING_TOUCH_SLOP = TOUCH_SLOP * 2; 142 143 /** 144 * Distance between the first touch and second touch to still be considered a double tap 145 */ 146 private static final int DOUBLE_TAP_SLOP = 100; 147 148 /** 149 * Distance a touch needs to be outside of a window's bounds for it to 150 * count as outside for purposes of dismissing the window. 151 */ 152 private static final int WINDOW_TOUCH_SLOP = 16; 153 154 /** 155 * Minimum velocity to initiate a fling, as measured in pixels per second 156 */ 157 private static final int MINIMUM_FLING_VELOCITY = 50; 158 159 /** 160 * Maximum velocity to initiate a fling, as measured in pixels per second 161 */ 162 private static final int MAXIMUM_FLING_VELOCITY = 8000; 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 mLargeTouchSlop; 193 private final int mPagingTouchSlop; 194 private final int mDoubleTapSlop; 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 mLargeTouchSlop = LARGE_TOUCH_SLOP; 215 mPagingTouchSlop = PAGING_TOUCH_SLOP; 216 mDoubleTapSlop = DOUBLE_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 mLargeTouchSlop = (int) (sizeAndDensity * LARGE_TOUCH_SLOP + 0.5f); 252 mPagingTouchSlop = (int) (sizeAndDensity * PAGING_TOUCH_SLOP + 0.5f); 253 mDoubleTapSlop = (int) (sizeAndDensity * DOUBLE_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 the first touch 429 * in a sequence of double tap 430 */ 431 public int getScaledLargeTouchSlop() { 432 return mLargeTouchSlop; 433 } 434 435 /** 436 * @return Distance a touch can wander before we think the user is scrolling a full page 437 * in dips 438 */ 439 public int getScaledPagingTouchSlop() { 440 return mPagingTouchSlop; 441 } 442 443 /** 444 * @return Distance between the first touch and second touch to still be 445 * considered a double tap 446 * @deprecated Use {@link #getScaledDoubleTapSlop()} instead. 447 * @hide The only client of this should be GestureDetector, which needs this 448 * for clients that still use its deprecated constructor. 449 */ 450 @Deprecated 451 public static int getDoubleTapSlop() { 452 return DOUBLE_TAP_SLOP; 453 } 454 455 /** 456 * @return Distance between the first touch and second touch to still be 457 * considered a double tap 458 */ 459 public int getScaledDoubleTapSlop() { 460 return mDoubleTapSlop; 461 } 462 463 /** 464 * @return Distance a touch must be outside the bounds of a window for it 465 * to be counted as outside the window for purposes of dismissing that 466 * window. 467 * 468 * @deprecated Use {@link #getScaledWindowTouchSlop()} instead. 469 */ 470 @Deprecated 471 public static int getWindowTouchSlop() { 472 return WINDOW_TOUCH_SLOP; 473 } 474 475 /** 476 * @return Distance a touch must be outside the bounds of a window for it 477 * to be counted as outside the window for purposes of dismissing that 478 * window. 479 */ 480 public int getScaledWindowTouchSlop() { 481 return mWindowTouchSlop; 482 } 483 484 /** 485 * @return Minimum velocity to initiate a fling, as measured in pixels per second. 486 * 487 * @deprecated Use {@link #getScaledMinimumFlingVelocity()} instead. 488 */ 489 @Deprecated 490 public static int getMinimumFlingVelocity() { 491 return MINIMUM_FLING_VELOCITY; 492 } 493 494 /** 495 * @return Minimum velocity to initiate a fling, as measured in pixels per second. 496 */ 497 public int getScaledMinimumFlingVelocity() { 498 return mMinimumFlingVelocity; 499 } 500 501 /** 502 * @return Maximum velocity to initiate a fling, as measured in pixels per second. 503 * 504 * @deprecated Use {@link #getScaledMaximumFlingVelocity()} instead. 505 */ 506 @Deprecated 507 public static int getMaximumFlingVelocity() { 508 return MAXIMUM_FLING_VELOCITY; 509 } 510 511 /** 512 * @return Maximum velocity to initiate a fling, as measured in pixels per second. 513 */ 514 public int getScaledMaximumFlingVelocity() { 515 return mMaximumFlingVelocity; 516 } 517 518 /** 519 * The maximum drawing cache size expressed in bytes. 520 * 521 * @return the maximum size of View's drawing cache expressed in bytes 522 * 523 * @deprecated Use {@link #getScaledMaximumDrawingCacheSize()} instead. 524 */ 525 @Deprecated 526 public static int getMaximumDrawingCacheSize() { 527 //noinspection deprecation 528 return MAXIMUM_DRAWING_CACHE_SIZE; 529 } 530 531 /** 532 * The maximum drawing cache size expressed in bytes. 533 * 534 * @return the maximum size of View's drawing cache expressed in bytes 535 */ 536 public int getScaledMaximumDrawingCacheSize() { 537 return mMaximumDrawingCacheSize; 538 } 539 540 /** 541 * @return The maximum distance a View should overscroll by when showing edge effects. 542 */ 543 public int getScaledOverscrollDistance() { 544 return mOverscrollDistance; 545 } 546 547 /** 548 * @return The maximum distance a View should overfling by when showing edge effects. 549 */ 550 public int getScaledOverflingDistance() { 551 return mOverflingDistance; 552 } 553 554 /** 555 * The amount of time that the zoom controls should be 556 * displayed on the screen expressed in milliseconds. 557 * 558 * @return the time the zoom controls should be visible expressed 559 * in milliseconds. 560 */ 561 public static long getZoomControlsTimeout() { 562 return ZOOM_CONTROLS_TIMEOUT; 563 } 564 565 /** 566 * The amount of time a user needs to press the relevant key to bring up 567 * the global actions dialog. 568 * 569 * @return how long a user needs to press the relevant key to bring up 570 * the global actions dialog. 571 */ 572 public static long getGlobalActionKeyTimeout() { 573 return GLOBAL_ACTIONS_KEY_TIMEOUT; 574 } 575 576 /** 577 * The amount of friction applied to scrolls and flings. 578 * 579 * @return A scalar dimensionless value representing the coefficient of 580 * friction. 581 */ 582 public static float getScrollFriction() { 583 return SCROLL_FRICTION; 584 } 585} 586