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