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 17 package android.view; 18 19 import android.content.Context; 20 import android.util.DisplayMetrics; 21 import android.util.SparseArray; 22 23 /** 24 * Contains methods to standard constants used in the UI for timeouts, sizes, and distances. 25 */ 26 public class ViewConfiguration { 27 /** 28 * Defines the width of the horizontal scrollbar and the height of the vertical scrollbar in 29 * pixels 30 */ 31 private static final int SCROLL_BAR_SIZE = 10; 32 33 /** 34 * Duration of the fade when scrollbars fade away in milliseconds 35 */ 36 private static final int SCROLL_BAR_FADE_DURATION = 250; 37 38 /** 39 * Default delay before the scrollbars fade in milliseconds 40 */ 41 private static final int SCROLL_BAR_DEFAULT_DELAY = 300; 42 43 /** 44 * Defines the length of the fading edges in pixels 45 */ 46 private static final int FADING_EDGE_LENGTH = 12; 47 48 /** 49 * Defines the duration in milliseconds of the pressed state in child 50 * components. 51 */ 52 private static final int PRESSED_STATE_DURATION = 85; 53 54 /** 55 * Defines the duration in milliseconds before a press turns into 56 * a long press 57 */ 58 private static final int LONG_PRESS_TIMEOUT = 500; 59 60 /** 61 * Defines the duration in milliseconds a user needs to hold down the 62 * appropriate button to bring up the global actions dialog (power off, 63 * lock screen, etc). 64 */ 65 private static final int GLOBAL_ACTIONS_KEY_TIMEOUT = 500; 66 67 /** 68 * Defines the duration in milliseconds we will wait to see if a touch event 69 * is a tap or a scroll. If the user does not move within this interval, it is 70 * considered to be a tap. 71 */ 72 private static final int TAP_TIMEOUT = 100; 73 74 /** 75 * Defines the duration in milliseconds we will wait to see if a touch event 76 * is a jump tap. If the user does not complete the jump tap within this interval, it is 77 * considered to be a tap. 78 */ 79 private static final int JUMP_TAP_TIMEOUT = 500; 80 81 /** 82 * Defines the duration in milliseconds between the first tap's up event and 83 * the second tap's down event for an interaction to be considered a 84 * double-tap. 85 */ 86 private static final int DOUBLE_TAP_TIMEOUT = 300; 87 88 /** 89 * Defines the duration in milliseconds we want to display zoom controls in response 90 * to a user panning within an application. 91 */ 92 private static final int ZOOM_CONTROLS_TIMEOUT = 3000; 93 94 /** 95 * Inset in pixels to look for touchable content when the user touches the edge of the screen 96 */ 97 private static final int EDGE_SLOP = 12; 98 99 /** 100 * Distance a touch can wander before we think the user is scrolling in pixels 101 */ 102 private static final int TOUCH_SLOP = 16; 103 104 /** 105 * Distance between the first touch and second touch to still be considered a double tap 106 */ 107 private static final int DOUBLE_TAP_SLOP = 100; 108 109 /** 110 * Distance a touch needs to be outside of a window's bounds for it to 111 * count as outside for purposes of dismissing the window. 112 */ 113 private static final int WINDOW_TOUCH_SLOP = 16; 114 115 /** 116 * Minimum velocity to initiate a fling, as measured in pixels per second 117 */ 118 private static final int MINIMUM_FLING_VELOCITY = 50; 119 120 /** 121 * Maximum velocity to initiate a fling, as measured in pixels per second 122 */ 123 private static final int MAXIMUM_FLING_VELOCITY = 4000; 124 125 /** 126 * The maximum size of View's drawing cache, expressed in bytes. This size 127 * should be at least equal to the size of the screen in ARGB888 format. 128 */ 129 @Deprecated 130 private static final int MAXIMUM_DRAWING_CACHE_SIZE = 320 * 480 * 4; // HVGA screen, ARGB8888 131 132 /** 133 * The coefficient of friction applied to flings/scrolls. 134 */ 135 private static float SCROLL_FRICTION = 0.015f; 136 137 private final int mEdgeSlop; 138 private final int mFadingEdgeLength; 139 private final int mMinimumFlingVelocity; 140 private final int mMaximumFlingVelocity; 141 private final int mScrollbarSize; 142 private final int mTouchSlop; 143 private final int mDoubleTapSlop; 144 private final int mWindowTouchSlop; 145 private final int mMaximumDrawingCacheSize; 146 147 private static final SparseArray<ViewConfiguration> sConfigurations = 148 new SparseArray<ViewConfiguration>(2); 149 150 /** 151 * @deprecated Use {@link android.view.ViewConfiguration#get(android.content.Context)} instead. 152 */ 153 @Deprecated ViewConfiguration()154 public ViewConfiguration() { 155 mEdgeSlop = EDGE_SLOP; 156 mFadingEdgeLength = FADING_EDGE_LENGTH; 157 mMinimumFlingVelocity = MINIMUM_FLING_VELOCITY; 158 mMaximumFlingVelocity = MAXIMUM_FLING_VELOCITY; 159 mScrollbarSize = SCROLL_BAR_SIZE; 160 mTouchSlop = TOUCH_SLOP; 161 mDoubleTapSlop = DOUBLE_TAP_SLOP; 162 mWindowTouchSlop = WINDOW_TOUCH_SLOP; 163 //noinspection deprecation 164 mMaximumDrawingCacheSize = MAXIMUM_DRAWING_CACHE_SIZE; 165 } 166 167 /** 168 * Creates a new configuration for the specified context. The configuration depends on 169 * various parameters of the context, like the dimension of the display or the density 170 * of the display. 171 * 172 * @param context The application context used to initialize this view configuration. 173 * 174 * @see #get(android.content.Context) 175 * @see android.util.DisplayMetrics 176 */ ViewConfiguration(Context context)177 private ViewConfiguration(Context context) { 178 final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); 179 final float density = metrics.density; 180 181 mEdgeSlop = (int) (density * EDGE_SLOP + 0.5f); 182 mFadingEdgeLength = (int) (density * FADING_EDGE_LENGTH + 0.5f); 183 mMinimumFlingVelocity = (int) (density * MINIMUM_FLING_VELOCITY + 0.5f); 184 mMaximumFlingVelocity = (int) (density * MAXIMUM_FLING_VELOCITY + 0.5f); 185 mScrollbarSize = (int) (density * SCROLL_BAR_SIZE + 0.5f); 186 mTouchSlop = (int) (density * TOUCH_SLOP + 0.5f); 187 mDoubleTapSlop = (int) (density * DOUBLE_TAP_SLOP + 0.5f); 188 mWindowTouchSlop = (int) (density * WINDOW_TOUCH_SLOP + 0.5f); 189 190 // Size of the screen in bytes, in ARGB_8888 format 191 mMaximumDrawingCacheSize = 4 * metrics.widthPixels * metrics.heightPixels; 192 } 193 194 /** 195 * Returns a configuration for the specified context. The configuration depends on 196 * various parameters of the context, like the dimension of the display or the 197 * density of the display. 198 * 199 * @param context The application context used to initialize the view configuration. 200 */ get(Context context)201 public static ViewConfiguration get(Context context) { 202 final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); 203 final int density = (int) (100.0f * metrics.density); 204 205 ViewConfiguration configuration = sConfigurations.get(density); 206 if (configuration == null) { 207 configuration = new ViewConfiguration(context); 208 sConfigurations.put(density, configuration); 209 } 210 211 return configuration; 212 } 213 214 /** 215 * @return The width of the horizontal scrollbar and the height of the vertical 216 * scrollbar in pixels 217 * 218 * @deprecated Use {@link #getScaledScrollBarSize()} instead. 219 */ 220 @Deprecated getScrollBarSize()221 public static int getScrollBarSize() { 222 return SCROLL_BAR_SIZE; 223 } 224 225 /** 226 * @return The width of the horizontal scrollbar and the height of the vertical 227 * scrollbar in pixels 228 */ getScaledScrollBarSize()229 public int getScaledScrollBarSize() { 230 return mScrollbarSize; 231 } 232 233 /** 234 * @return Duration of the fade when scrollbars fade away in milliseconds 235 */ getScrollBarFadeDuration()236 public static int getScrollBarFadeDuration() { 237 return SCROLL_BAR_FADE_DURATION; 238 } 239 240 /** 241 * @return Default delay before the scrollbars fade in milliseconds 242 */ getScrollDefaultDelay()243 public static int getScrollDefaultDelay() { 244 return SCROLL_BAR_DEFAULT_DELAY; 245 } 246 247 /** 248 * @return the length of the fading edges in pixels 249 * 250 * @deprecated Use {@link #getScaledFadingEdgeLength()} instead. 251 */ 252 @Deprecated getFadingEdgeLength()253 public static int getFadingEdgeLength() { 254 return FADING_EDGE_LENGTH; 255 } 256 257 /** 258 * @return the length of the fading edges in pixels 259 */ getScaledFadingEdgeLength()260 public int getScaledFadingEdgeLength() { 261 return mFadingEdgeLength; 262 } 263 264 /** 265 * @return the duration in milliseconds of the pressed state in child 266 * components. 267 */ getPressedStateDuration()268 public static int getPressedStateDuration() { 269 return PRESSED_STATE_DURATION; 270 } 271 272 /** 273 * @return the duration in milliseconds before a press turns into 274 * a long press 275 */ getLongPressTimeout()276 public static int getLongPressTimeout() { 277 return LONG_PRESS_TIMEOUT; 278 } 279 280 /** 281 * @return the duration in milliseconds we will wait to see if a touch event 282 * is a tap or a scroll. If the user does not move within this interval, it is 283 * considered to be a tap. 284 */ getTapTimeout()285 public static int getTapTimeout() { 286 return TAP_TIMEOUT; 287 } 288 289 /** 290 * @return the duration in milliseconds we will wait to see if a touch event 291 * is a jump tap. If the user does not move within this interval, it is 292 * considered to be a tap. 293 */ getJumpTapTimeout()294 public static int getJumpTapTimeout() { 295 return JUMP_TAP_TIMEOUT; 296 } 297 298 /** 299 * @return the duration in milliseconds between the first tap's up event and 300 * the second tap's down event for an interaction to be considered a 301 * double-tap. 302 */ getDoubleTapTimeout()303 public static int getDoubleTapTimeout() { 304 return DOUBLE_TAP_TIMEOUT; 305 } 306 307 /** 308 * @return Inset in pixels to look for touchable content when the user touches the edge of the 309 * screen 310 * 311 * @deprecated Use {@link #getScaledEdgeSlop()} instead. 312 */ 313 @Deprecated getEdgeSlop()314 public static int getEdgeSlop() { 315 return EDGE_SLOP; 316 } 317 318 /** 319 * @return Inset in pixels to look for touchable content when the user touches the edge of the 320 * screen 321 */ getScaledEdgeSlop()322 public int getScaledEdgeSlop() { 323 return mEdgeSlop; 324 } 325 326 /** 327 * @return Distance a touch can wander before we think the user is scrolling in pixels 328 * 329 * @deprecated Use {@link #getScaledTouchSlop()} instead. 330 */ 331 @Deprecated getTouchSlop()332 public static int getTouchSlop() { 333 return TOUCH_SLOP; 334 } 335 336 /** 337 * @return Distance a touch can wander before we think the user is scrolling in pixels 338 */ getScaledTouchSlop()339 public int getScaledTouchSlop() { 340 return mTouchSlop; 341 } 342 343 /** 344 * @return Distance between the first touch and second touch to still be 345 * considered a double tap 346 * @deprecated Use {@link #getScaledDoubleTapSlop()} instead. 347 * @hide The only client of this should be GestureDetector, which needs this 348 * for clients that still use its deprecated constructor. 349 */ 350 @Deprecated getDoubleTapSlop()351 public static int getDoubleTapSlop() { 352 return DOUBLE_TAP_SLOP; 353 } 354 355 /** 356 * @return Distance between the first touch and second touch to still be 357 * considered a double tap 358 */ getScaledDoubleTapSlop()359 public int getScaledDoubleTapSlop() { 360 return mDoubleTapSlop; 361 } 362 363 /** 364 * @return Distance a touch must be outside the bounds of a window for it 365 * to be counted as outside the window for purposes of dismissing that 366 * window. 367 * 368 * @deprecated Use {@link #getScaledWindowTouchSlop()} instead. 369 */ 370 @Deprecated getWindowTouchSlop()371 public static int getWindowTouchSlop() { 372 return WINDOW_TOUCH_SLOP; 373 } 374 375 /** 376 * @return Distance a touch must be outside the bounds of a window for it 377 * to be counted as outside the window for purposes of dismissing that 378 * window. 379 */ getScaledWindowTouchSlop()380 public int getScaledWindowTouchSlop() { 381 return mWindowTouchSlop; 382 } 383 384 /** 385 * @return Minimum velocity to initiate a fling, as measured in pixels per second. 386 * 387 * @deprecated Use {@link #getScaledMinimumFlingVelocity()} instead. 388 */ 389 @Deprecated getMinimumFlingVelocity()390 public static int getMinimumFlingVelocity() { 391 return MINIMUM_FLING_VELOCITY; 392 } 393 394 /** 395 * @return Minimum velocity to initiate a fling, as measured in pixels per second. 396 */ getScaledMinimumFlingVelocity()397 public int getScaledMinimumFlingVelocity() { 398 return mMinimumFlingVelocity; 399 } 400 401 /** 402 * @return Maximum velocity to initiate a fling, as measured in pixels per second. 403 * 404 * @deprecated Use {@link #getScaledMaximumFlingVelocity()} instead. 405 */ 406 @Deprecated getMaximumFlingVelocity()407 public static int getMaximumFlingVelocity() { 408 return MAXIMUM_FLING_VELOCITY; 409 } 410 411 /** 412 * @return Maximum velocity to initiate a fling, as measured in pixels per second. 413 */ getScaledMaximumFlingVelocity()414 public int getScaledMaximumFlingVelocity() { 415 return mMaximumFlingVelocity; 416 } 417 418 /** 419 * The maximum drawing cache size expressed in bytes. 420 * 421 * @return the maximum size of View's drawing cache expressed in bytes 422 * 423 * @deprecated Use {@link #getScaledMaximumDrawingCacheSize()} instead. 424 */ 425 @Deprecated getMaximumDrawingCacheSize()426 public static int getMaximumDrawingCacheSize() { 427 //noinspection deprecation 428 return MAXIMUM_DRAWING_CACHE_SIZE; 429 } 430 431 /** 432 * The maximum drawing cache size expressed in bytes. 433 * 434 * @return the maximum size of View's drawing cache expressed in bytes 435 */ getScaledMaximumDrawingCacheSize()436 public int getScaledMaximumDrawingCacheSize() { 437 return mMaximumDrawingCacheSize; 438 } 439 440 /** 441 * The amount of time that the zoom controls should be 442 * displayed on the screen expressed in milliseconds. 443 * 444 * @return the time the zoom controls should be visible expressed 445 * in milliseconds. 446 */ getZoomControlsTimeout()447 public static long getZoomControlsTimeout() { 448 return ZOOM_CONTROLS_TIMEOUT; 449 } 450 451 /** 452 * The amount of time a user needs to press the relevant key to bring up 453 * the global actions dialog. 454 * 455 * @return how long a user needs to press the relevant key to bring up 456 * the global actions dialog. 457 */ getGlobalActionKeyTimeout()458 public static long getGlobalActionKeyTimeout() { 459 return GLOBAL_ACTIONS_KEY_TIMEOUT; 460 } 461 462 /** 463 * The amount of friction applied to scrolls and flings. 464 * 465 * @return A scalar dimensionless value representing the coefficient of 466 * friction. 467 */ getScrollFriction()468 public static float getScrollFriction() { 469 return SCROLL_FRICTION; 470 } 471 } 472