The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 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 | * 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 | * @hide pending API council |
| 271 | */ |
| 272 | public static int getDoubleTapTimeout() { |
| 273 | return DOUBLE_TAP_TIMEOUT; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * @return Inset in pixels to look for touchable content when the user touches the edge of the |
| 278 | * screen |
| 279 | * |
| 280 | * @deprecated Use {@link #getScaledEdgeSlop()} instead. |
| 281 | */ |
| 282 | @Deprecated |
| 283 | public static int getEdgeSlop() { |
| 284 | return EDGE_SLOP; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * @return Inset in pixels to look for touchable content when the user touches the edge of the |
| 289 | * screen |
| 290 | */ |
| 291 | public int getScaledEdgeSlop() { |
| 292 | return mEdgeSlop; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * @return Distance a touch can wander before we think the user is scrolling in pixels |
| 297 | * |
| 298 | * @deprecated Use {@link #getScaledTouchSlop()} instead. |
| 299 | */ |
| 300 | @Deprecated |
| 301 | public static int getTouchSlop() { |
| 302 | return TOUCH_SLOP; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * @return Distance a touch can wander before we think the user is scrolling in pixels |
| 307 | */ |
| 308 | public int getScaledTouchSlop() { |
| 309 | return mTouchSlop; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * @return Distance between the first touch and second touch to still be |
| 314 | * considered a double tap |
| 315 | * @deprecated Use {@link #getScaledDoubleTapSlop()} instead. |
| 316 | * @hide The only client of this should be GestureDetector, which needs this |
| 317 | * for clients that still use its deprecated constructor. |
| 318 | */ |
| 319 | @Deprecated |
| 320 | public static int getDoubleTapSlop() { |
| 321 | return DOUBLE_TAP_SLOP; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * @return Distance between the first touch and second touch to still be |
| 326 | * considered a double tap |
| 327 | * @hide pending API council |
| 328 | */ |
| 329 | public int getScaledDoubleTapSlop() { |
| 330 | return mDoubleTapSlop; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * @return Distance a touch must be outside the bounds of a window for it |
| 335 | * to be counted as outside the window for purposes of dismissing that |
| 336 | * window. |
| 337 | * |
| 338 | * @deprecated Use {@link #getScaledWindowTouchSlop()} instead. |
| 339 | */ |
| 340 | @Deprecated |
| 341 | public static int getWindowTouchSlop() { |
| 342 | return WINDOW_TOUCH_SLOP; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * @return Distance a touch must be outside the bounds of a window for it |
| 347 | * to be counted as outside the window for purposes of dismissing that |
| 348 | * window. |
| 349 | */ |
| 350 | public int getScaledWindowTouchSlop() { |
| 351 | return mWindowTouchSlop; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * @return Minimum velocity to initiate a fling, as measured in pixels per second. |
| 356 | * |
| 357 | * @deprecated Use {@link #getScaledMinimumFlingVelocity()} instead. |
| 358 | */ |
| 359 | @Deprecated |
| 360 | public static int getMinimumFlingVelocity() { |
| 361 | return MINIMUM_FLING_VELOCITY; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * @return Minimum velocity to initiate a fling, as measured in pixels per second. |
| 366 | */ |
| 367 | public int getScaledMinimumFlingVelocity() { |
| 368 | return mMinimumFlingVelocity; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * The maximum drawing cache size expressed in bytes. |
| 373 | * |
| 374 | * @return the maximum size of View's drawing cache expressed in bytes |
| 375 | * |
| 376 | * @deprecated Use {@link #getScaledMaximumDrawingCacheSize()} instead. |
| 377 | */ |
| 378 | @Deprecated |
| 379 | public static int getMaximumDrawingCacheSize() { |
| 380 | //noinspection deprecation |
| 381 | return MAXIMUM_DRAWING_CACHE_SIZE; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * The maximum drawing cache size expressed in bytes. |
| 386 | * |
| 387 | * @return the maximum size of View's drawing cache expressed in bytes |
| 388 | */ |
| 389 | public int getScaledMaximumDrawingCacheSize() { |
| 390 | return mMaximumDrawingCacheSize; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * The amount of time that the zoom controls should be |
| 395 | * displayed on the screen expressed in milliseconds. |
| 396 | * |
| 397 | * @return the time the zoom controls should be visible expressed |
| 398 | * in milliseconds. |
| 399 | */ |
| 400 | public static long getZoomControlsTimeout() { |
| 401 | return ZOOM_CONTROLS_TIMEOUT; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * The amount of time a user needs to press the relevant key to bring up |
| 406 | * the global actions dialog. |
| 407 | * |
| 408 | * @return how long a user needs to press the relevant key to bring up |
| 409 | * the global actions dialog. |
| 410 | */ |
| 411 | public static long getGlobalActionKeyTimeout() { |
| 412 | return GLOBAL_ACTIONS_KEY_TIMEOUT; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * The amount of friction applied to scrolls and flings. |
| 417 | * |
| 418 | * @return A scalar dimensionless value representing the coefficient of |
| 419 | * friction. |
| 420 | */ |
| 421 | public static float getScrollFriction() { |
| 422 | return SCROLL_FRICTION; |
| 423 | } |
| 424 | } |