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.content.res.Resources;
23import android.graphics.Point;
24import android.os.RemoteException;
25import android.provider.Settings;
26import android.util.DisplayMetrics;
27import android.util.SparseArray;
28
29/**
30 * Contains methods to standard constants used in the UI for timeouts, sizes, and distances.
31 */
32public class ViewConfiguration {
33    /**
34     * Defines the width of the horizontal scrollbar and the height of the vertical scrollbar in
35     * dips
36     */
37    private static final int SCROLL_BAR_SIZE = 10;
38
39    /**
40     * Duration of the fade when scrollbars fade away in milliseconds
41     */
42    private static final int SCROLL_BAR_FADE_DURATION = 250;
43
44    /**
45     * Default delay before the scrollbars fade in milliseconds
46     */
47    private static final int SCROLL_BAR_DEFAULT_DELAY = 300;
48
49    /**
50     * Defines the length of the fading edges in dips
51     */
52    private static final int FADING_EDGE_LENGTH = 12;
53
54    /**
55     * Defines the duration in milliseconds of the pressed state in child
56     * components.
57     */
58    private static final int PRESSED_STATE_DURATION = 64;
59
60    /**
61     * Defines the default duration in milliseconds before a press turns into
62     * a long press
63     */
64    private static final int DEFAULT_LONG_PRESS_TIMEOUT = 500;
65
66    /**
67     * Defines the time between successive key repeats in milliseconds.
68     */
69    private static final int KEY_REPEAT_DELAY = 50;
70
71    /**
72     * Defines the duration in milliseconds a user needs to hold down the
73     * appropriate button to bring up the global actions dialog (power off,
74     * lock screen, etc).
75     */
76    private static final int GLOBAL_ACTIONS_KEY_TIMEOUT = 500;
77
78    /**
79     * Defines the duration in milliseconds we will wait to see if a touch event
80     * is a tap or a scroll. If the user does not move within this interval, it is
81     * considered to be a tap.
82     */
83    private static final int TAP_TIMEOUT = 180;
84
85    /**
86     * Defines the duration in milliseconds we will wait to see if a touch event
87     * is a jump tap. If the user does not complete the jump tap within this interval, it is
88     * considered to be a tap.
89     */
90    private static final int JUMP_TAP_TIMEOUT = 500;
91
92    /**
93     * Defines the duration in milliseconds between the first tap's up event and
94     * the second tap's down event for an interaction to be considered a
95     * double-tap.
96     */
97    private static final int DOUBLE_TAP_TIMEOUT = 300;
98
99    /**
100     * Defines the minimum duration in milliseconds between the first tap's up event and
101     * the second tap's down event for an interaction to be considered a
102     * double-tap.
103     */
104    private static final int DOUBLE_TAP_MIN_TIME = 40;
105
106    /**
107     * Defines the maximum duration in milliseconds between a touch pad
108     * touch and release for a given touch to be considered a tap (click) as
109     * opposed to a hover movement gesture.
110     */
111    private static final int HOVER_TAP_TIMEOUT = 150;
112
113    /**
114     * Defines the maximum distance in pixels that a touch pad touch can move
115     * before being released for it to be considered a tap (click) as opposed
116     * to a hover movement gesture.
117     */
118    private static final int HOVER_TAP_SLOP = 20;
119
120    /**
121     * Defines the duration in milliseconds we want to display zoom controls in response
122     * to a user panning within an application.
123     */
124    private static final int ZOOM_CONTROLS_TIMEOUT = 3000;
125
126    /**
127     * Inset in dips to look for touchable content when the user touches the edge of the screen
128     */
129    private static final int EDGE_SLOP = 12;
130
131    /**
132     * Distance a touch can wander before we think the user is scrolling in dips.
133     * Note that this value defined here is only used as a fallback by legacy/misbehaving
134     * applications that do not provide a Context for determining density/configuration-dependent
135     * values.
136     *
137     * To alter this value, see the configuration resource config_viewConfigurationTouchSlop
138     * in frameworks/base/core/res/res/values/config.xml or the appropriate device resource overlay.
139     * It may be appropriate to tweak this on a device-specific basis in an overlay based on
140     * the characteristics of the touch panel and firmware.
141     */
142    private static final int TOUCH_SLOP = 8;
143
144    /**
145     * Distance the first touch can wander before we stop considering this event a double tap
146     * (in dips)
147     */
148    private static final int DOUBLE_TAP_TOUCH_SLOP = TOUCH_SLOP;
149
150    /**
151     * Distance a touch can wander before we think the user is attempting a paged scroll
152     * (in dips)
153     *
154     * Note that this value defined here is only used as a fallback by legacy/misbehaving
155     * applications that do not provide a Context for determining density/configuration-dependent
156     * values.
157     *
158     * See the note above on {@link #TOUCH_SLOP} regarding the dimen resource
159     * config_viewConfigurationTouchSlop. ViewConfiguration will report a paging touch slop of
160     * config_viewConfigurationTouchSlop * 2 when provided with a Context.
161     */
162    private static final int PAGING_TOUCH_SLOP = TOUCH_SLOP * 2;
163
164    /**
165     * Distance in dips between the first touch and second touch to still be considered a double tap
166     */
167    private static final int DOUBLE_TAP_SLOP = 100;
168
169    /**
170     * Distance in dips a touch needs to be outside of a window's bounds for it to
171     * count as outside for purposes of dismissing the window.
172     */
173    private static final int WINDOW_TOUCH_SLOP = 16;
174
175    /**
176     * Minimum velocity to initiate a fling, as measured in dips per second
177     */
178    private static final int MINIMUM_FLING_VELOCITY = 50;
179
180    /**
181     * Maximum velocity to initiate a fling, as measured in dips per second
182     */
183    private static final int MAXIMUM_FLING_VELOCITY = 8000;
184
185    /**
186     * Delay before dispatching a recurring accessibility event in milliseconds.
187     * This delay guarantees that a recurring event will be send at most once
188     * during the {@link #SEND_RECURRING_ACCESSIBILITY_EVENTS_INTERVAL_MILLIS} time
189     * frame.
190     */
191    private static final long SEND_RECURRING_ACCESSIBILITY_EVENTS_INTERVAL_MILLIS = 100;
192
193    /**
194     * The maximum size of View's drawing cache, expressed in bytes. This size
195     * should be at least equal to the size of the screen in ARGB888 format.
196     */
197    @Deprecated
198    private static final int MAXIMUM_DRAWING_CACHE_SIZE = 480 * 800 * 4; // ARGB8888
199
200    /**
201     * The coefficient of friction applied to flings/scrolls.
202     */
203    private static final float SCROLL_FRICTION = 0.015f;
204
205    /**
206     * Max distance in dips to overscroll for edge effects
207     */
208    private static final int OVERSCROLL_DISTANCE = 0;
209
210    /**
211     * Max distance in dips to overfling for edge effects
212     */
213    private static final int OVERFLING_DISTANCE = 6;
214
215    private final int mEdgeSlop;
216    private final int mFadingEdgeLength;
217    private final int mMinimumFlingVelocity;
218    private final int mMaximumFlingVelocity;
219    private final int mScrollbarSize;
220    private final int mTouchSlop;
221    private final int mDoubleTapTouchSlop;
222    private final int mPagingTouchSlop;
223    private final int mDoubleTapSlop;
224    private final int mWindowTouchSlop;
225    private final int mMaximumDrawingCacheSize;
226    private final int mOverscrollDistance;
227    private final int mOverflingDistance;
228    private final boolean mFadingMarqueeEnabled;
229
230    private boolean sHasPermanentMenuKey;
231    private boolean sHasPermanentMenuKeySet;
232
233    static final SparseArray<ViewConfiguration> sConfigurations =
234            new SparseArray<ViewConfiguration>(2);
235
236    /**
237     * @deprecated Use {@link android.view.ViewConfiguration#get(android.content.Context)} instead.
238     */
239    @Deprecated
240    public ViewConfiguration() {
241        mEdgeSlop = EDGE_SLOP;
242        mFadingEdgeLength = FADING_EDGE_LENGTH;
243        mMinimumFlingVelocity = MINIMUM_FLING_VELOCITY;
244        mMaximumFlingVelocity = MAXIMUM_FLING_VELOCITY;
245        mScrollbarSize = SCROLL_BAR_SIZE;
246        mTouchSlop = TOUCH_SLOP;
247        mDoubleTapTouchSlop = DOUBLE_TAP_TOUCH_SLOP;
248        mPagingTouchSlop = PAGING_TOUCH_SLOP;
249        mDoubleTapSlop = DOUBLE_TAP_SLOP;
250        mWindowTouchSlop = WINDOW_TOUCH_SLOP;
251        //noinspection deprecation
252        mMaximumDrawingCacheSize = MAXIMUM_DRAWING_CACHE_SIZE;
253        mOverscrollDistance = OVERSCROLL_DISTANCE;
254        mOverflingDistance = OVERFLING_DISTANCE;
255        mFadingMarqueeEnabled = true;
256    }
257
258    /**
259     * Creates a new configuration for the specified context. The configuration depends on
260     * various parameters of the context, like the dimension of the display or the density
261     * of the display.
262     *
263     * @param context The application context used to initialize this view configuration.
264     *
265     * @see #get(android.content.Context)
266     * @see android.util.DisplayMetrics
267     */
268    private ViewConfiguration(Context context) {
269        final Resources res = context.getResources();
270        final DisplayMetrics metrics = res.getDisplayMetrics();
271        final Configuration config = res.getConfiguration();
272        final float density = metrics.density;
273        final float sizeAndDensity;
274        if (config.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_XLARGE)) {
275            sizeAndDensity = density * 1.5f;
276        } else {
277            sizeAndDensity = density;
278        }
279
280        mEdgeSlop = (int) (sizeAndDensity * EDGE_SLOP + 0.5f);
281        mFadingEdgeLength = (int) (sizeAndDensity * FADING_EDGE_LENGTH + 0.5f);
282        mMinimumFlingVelocity = (int) (density * MINIMUM_FLING_VELOCITY + 0.5f);
283        mMaximumFlingVelocity = (int) (density * MAXIMUM_FLING_VELOCITY + 0.5f);
284        mScrollbarSize = (int) (density * SCROLL_BAR_SIZE + 0.5f);
285        mDoubleTapSlop = (int) (sizeAndDensity * DOUBLE_TAP_SLOP + 0.5f);
286        mWindowTouchSlop = (int) (sizeAndDensity * WINDOW_TOUCH_SLOP + 0.5f);
287
288        // Size of the screen in bytes, in ARGB_8888 format
289        final WindowManager win = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
290        final Display display = win.getDefaultDisplay();
291        final Point size = new Point();
292        display.getRealSize(size);
293        mMaximumDrawingCacheSize = 4 * size.x * size.y;
294
295        mOverscrollDistance = (int) (sizeAndDensity * OVERSCROLL_DISTANCE + 0.5f);
296        mOverflingDistance = (int) (sizeAndDensity * OVERFLING_DISTANCE + 0.5f);
297
298        if (!sHasPermanentMenuKeySet) {
299            IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
300            try {
301                sHasPermanentMenuKey = !wm.hasNavigationBar();
302                sHasPermanentMenuKeySet = true;
303            } catch (RemoteException ex) {
304                sHasPermanentMenuKey = false;
305            }
306        }
307
308        mFadingMarqueeEnabled = res.getBoolean(
309                com.android.internal.R.bool.config_ui_enableFadingMarquee);
310        mTouchSlop = res.getDimensionPixelSize(
311                com.android.internal.R.dimen.config_viewConfigurationTouchSlop);
312        mPagingTouchSlop = mTouchSlop * 2;
313
314        mDoubleTapTouchSlop = mTouchSlop;
315    }
316
317    /**
318     * Returns a configuration for the specified context. The configuration depends on
319     * various parameters of the context, like the dimension of the display or the
320     * density of the display.
321     *
322     * @param context The application context used to initialize the view configuration.
323     */
324    public static ViewConfiguration get(Context context) {
325        final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
326        final int density = (int) (100.0f * metrics.density);
327
328        ViewConfiguration configuration = sConfigurations.get(density);
329        if (configuration == null) {
330            configuration = new ViewConfiguration(context);
331            sConfigurations.put(density, configuration);
332        }
333
334        return configuration;
335    }
336
337    /**
338     * @return The width of the horizontal scrollbar and the height of the vertical
339     *         scrollbar in dips
340     *
341     * @deprecated Use {@link #getScaledScrollBarSize()} instead.
342     */
343    @Deprecated
344    public static int getScrollBarSize() {
345        return SCROLL_BAR_SIZE;
346    }
347
348    /**
349     * @return The width of the horizontal scrollbar and the height of the vertical
350     *         scrollbar in pixels
351     */
352    public int getScaledScrollBarSize() {
353        return mScrollbarSize;
354    }
355
356    /**
357     * @return Duration of the fade when scrollbars fade away in milliseconds
358     */
359    public static int getScrollBarFadeDuration() {
360        return SCROLL_BAR_FADE_DURATION;
361    }
362
363    /**
364     * @return Default delay before the scrollbars fade in milliseconds
365     */
366    public static int getScrollDefaultDelay() {
367        return SCROLL_BAR_DEFAULT_DELAY;
368    }
369
370    /**
371     * @return the length of the fading edges in dips
372     *
373     * @deprecated Use {@link #getScaledFadingEdgeLength()} instead.
374     */
375    @Deprecated
376    public static int getFadingEdgeLength() {
377        return FADING_EDGE_LENGTH;
378    }
379
380    /**
381     * @return the length of the fading edges in pixels
382     */
383    public int getScaledFadingEdgeLength() {
384        return mFadingEdgeLength;
385    }
386
387    /**
388     * @return the duration in milliseconds of the pressed state in child
389     * components.
390     */
391    public static int getPressedStateDuration() {
392        return PRESSED_STATE_DURATION;
393    }
394
395    /**
396     * @return the duration in milliseconds before a press turns into
397     * a long press
398     */
399    public static int getLongPressTimeout() {
400        return AppGlobals.getIntCoreSetting(Settings.Secure.LONG_PRESS_TIMEOUT,
401                DEFAULT_LONG_PRESS_TIMEOUT);
402    }
403
404    /**
405     * @return the time before the first key repeat in milliseconds.
406     */
407    public static int getKeyRepeatTimeout() {
408        return getLongPressTimeout();
409    }
410
411    /**
412     * @return the time between successive key repeats in milliseconds.
413     */
414    public static int getKeyRepeatDelay() {
415        return KEY_REPEAT_DELAY;
416    }
417
418    /**
419     * @return the duration in milliseconds we will wait to see if a touch event
420     * is a tap or a scroll. If the user does not move within this interval, it is
421     * considered to be a tap.
422     */
423    public static int getTapTimeout() {
424        return TAP_TIMEOUT;
425    }
426
427    /**
428     * @return the duration in milliseconds we will wait to see if a touch event
429     * is a jump tap. If the user does not move within this interval, it is
430     * considered to be a tap.
431     */
432    public static int getJumpTapTimeout() {
433        return JUMP_TAP_TIMEOUT;
434    }
435
436    /**
437     * @return the duration in milliseconds between the first tap's up event and
438     * the second tap's down event for an interaction to be considered a
439     * double-tap.
440     */
441    public static int getDoubleTapTimeout() {
442        return DOUBLE_TAP_TIMEOUT;
443    }
444
445    /**
446     * @return the minimum duration in milliseconds between the first tap's
447     * up event and the second tap's down event for an interaction to be considered a
448     * double-tap.
449     *
450     * @hide
451     */
452    public static int getDoubleTapMinTime() {
453        return DOUBLE_TAP_MIN_TIME;
454    }
455
456    /**
457     * @return the maximum duration in milliseconds between a touch pad
458     * touch and release for a given touch to be considered a tap (click) as
459     * opposed to a hover movement gesture.
460     * @hide
461     */
462    public static int getHoverTapTimeout() {
463        return HOVER_TAP_TIMEOUT;
464    }
465
466    /**
467     * @return the maximum distance in pixels that a touch pad touch can move
468     * before being released for it to be considered a tap (click) as opposed
469     * to a hover movement gesture.
470     * @hide
471     */
472    public static int getHoverTapSlop() {
473        return HOVER_TAP_SLOP;
474    }
475
476    /**
477     * @return Inset in dips to look for touchable content when the user touches the edge of the
478     *         screen
479     *
480     * @deprecated Use {@link #getScaledEdgeSlop()} instead.
481     */
482    @Deprecated
483    public static int getEdgeSlop() {
484        return EDGE_SLOP;
485    }
486
487    /**
488     * @return Inset in pixels to look for touchable content when the user touches the edge of the
489     *         screen
490     */
491    public int getScaledEdgeSlop() {
492        return mEdgeSlop;
493    }
494
495    /**
496     * @return Distance in dips a touch can wander before we think the user is scrolling
497     *
498     * @deprecated Use {@link #getScaledTouchSlop()} instead.
499     */
500    @Deprecated
501    public static int getTouchSlop() {
502        return TOUCH_SLOP;
503    }
504
505    /**
506     * @return Distance in pixels a touch can wander before we think the user is scrolling
507     */
508    public int getScaledTouchSlop() {
509        return mTouchSlop;
510    }
511
512    /**
513     * @return Distance in pixels the first touch can wander before we do not consider this a
514     * potential double tap event
515     * @hide
516     */
517    public int getScaledDoubleTapTouchSlop() {
518        return mDoubleTapTouchSlop;
519    }
520
521    /**
522     * @return Distance in pixels a touch can wander before we think the user is scrolling a full
523     * page
524     */
525    public int getScaledPagingTouchSlop() {
526        return mPagingTouchSlop;
527    }
528
529    /**
530     * @return Distance in dips between the first touch and second touch to still be
531     *         considered a double tap
532     * @deprecated Use {@link #getScaledDoubleTapSlop()} instead.
533     * @hide The only client of this should be GestureDetector, which needs this
534     *       for clients that still use its deprecated constructor.
535     */
536    @Deprecated
537    public static int getDoubleTapSlop() {
538        return DOUBLE_TAP_SLOP;
539    }
540
541    /**
542     * @return Distance in pixels between the first touch and second touch to still be
543     *         considered a double tap
544     */
545    public int getScaledDoubleTapSlop() {
546        return mDoubleTapSlop;
547    }
548
549    /**
550     * Interval for dispatching a recurring accessibility event in milliseconds.
551     * This interval guarantees that a recurring event will be send at most once
552     * during the {@link #getSendRecurringAccessibilityEventsInterval()} time frame.
553     *
554     * @return The delay in milliseconds.
555     *
556     * @hide
557     */
558    public static long getSendRecurringAccessibilityEventsInterval() {
559        return SEND_RECURRING_ACCESSIBILITY_EVENTS_INTERVAL_MILLIS;
560    }
561
562    /**
563     * @return Distance in dips a touch must be outside the bounds of a window for it
564     * to be counted as outside the window for purposes of dismissing that
565     * window.
566     *
567     * @deprecated Use {@link #getScaledWindowTouchSlop()} instead.
568     */
569    @Deprecated
570    public static int getWindowTouchSlop() {
571        return WINDOW_TOUCH_SLOP;
572    }
573
574    /**
575     * @return Distance in pixels a touch must be outside the bounds of a window for it
576     * to be counted as outside the window for purposes of dismissing that window.
577     */
578    public int getScaledWindowTouchSlop() {
579        return mWindowTouchSlop;
580    }
581
582    /**
583     * @return Minimum velocity to initiate a fling, as measured in dips per second.
584     *
585     * @deprecated Use {@link #getScaledMinimumFlingVelocity()} instead.
586     */
587    @Deprecated
588    public static int getMinimumFlingVelocity() {
589        return MINIMUM_FLING_VELOCITY;
590    }
591
592    /**
593     * @return Minimum velocity to initiate a fling, as measured in pixels per second.
594     */
595    public int getScaledMinimumFlingVelocity() {
596        return mMinimumFlingVelocity;
597    }
598
599    /**
600     * @return Maximum velocity to initiate a fling, as measured in dips per second.
601     *
602     * @deprecated Use {@link #getScaledMaximumFlingVelocity()} instead.
603     */
604    @Deprecated
605    public static int getMaximumFlingVelocity() {
606        return MAXIMUM_FLING_VELOCITY;
607    }
608
609    /**
610     * @return Maximum velocity to initiate a fling, as measured in pixels per second.
611     */
612    public int getScaledMaximumFlingVelocity() {
613        return mMaximumFlingVelocity;
614    }
615
616    /**
617     * The maximum drawing cache size expressed in bytes.
618     *
619     * @return the maximum size of View's drawing cache expressed in bytes
620     *
621     * @deprecated Use {@link #getScaledMaximumDrawingCacheSize()} instead.
622     */
623    @Deprecated
624    public static int getMaximumDrawingCacheSize() {
625        //noinspection deprecation
626        return MAXIMUM_DRAWING_CACHE_SIZE;
627    }
628
629    /**
630     * The maximum drawing cache size expressed in bytes.
631     *
632     * @return the maximum size of View's drawing cache expressed in bytes
633     */
634    public int getScaledMaximumDrawingCacheSize() {
635        return mMaximumDrawingCacheSize;
636    }
637
638    /**
639     * @return The maximum distance a View should overscroll by when showing edge effects (in
640     * pixels).
641     */
642    public int getScaledOverscrollDistance() {
643        return mOverscrollDistance;
644    }
645
646    /**
647     * @return The maximum distance a View should overfling by when showing edge effects (in
648     * pixels).
649     */
650    public int getScaledOverflingDistance() {
651        return mOverflingDistance;
652    }
653
654    /**
655     * The amount of time that the zoom controls should be
656     * displayed on the screen expressed in milliseconds.
657     *
658     * @return the time the zoom controls should be visible expressed
659     * in milliseconds.
660     */
661    public static long getZoomControlsTimeout() {
662        return ZOOM_CONTROLS_TIMEOUT;
663    }
664
665    /**
666     * The amount of time a user needs to press the relevant key to bring up
667     * the global actions dialog.
668     *
669     * @return how long a user needs to press the relevant key to bring up
670     *   the global actions dialog.
671     */
672    public static long getGlobalActionKeyTimeout() {
673        return GLOBAL_ACTIONS_KEY_TIMEOUT;
674    }
675
676    /**
677     * The amount of friction applied to scrolls and flings.
678     *
679     * @return A scalar dimensionless value representing the coefficient of
680     *         friction.
681     */
682    public static float getScrollFriction() {
683        return SCROLL_FRICTION;
684    }
685
686    /**
687     * Report if the device has a permanent menu key available to the user.
688     *
689     * <p>As of Android 3.0, devices may not have a permanent menu key available.
690     * Apps should use the action bar to present menu options to users.
691     * However, there are some apps where the action bar is inappropriate
692     * or undesirable. This method may be used to detect if a menu key is present.
693     * If not, applications should provide another on-screen affordance to access
694     * functionality.
695     *
696     * @return true if a permanent menu key is present, false otherwise.
697     */
698    public boolean hasPermanentMenuKey() {
699        return sHasPermanentMenuKey;
700    }
701
702    /**
703     * @hide
704     * @return Whether or not marquee should use fading edges.
705     */
706    public boolean isFadingMarqueeEnabled() {
707        return mFadingMarqueeEnabled;
708    }
709}
710