Home | History | Annotate | Download | only in shadows
      1 /*
      2  * Portions of this code came from frameworks/base/core/java/android/view/ViewConfiguration.java,
      3  * which contains the following license text:
      4  *
      5  * Copyright (C) 2006 The Android Open Source Project
      6  *
      7  * Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *      http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  *
     19  */
     20 
     21 package com.xtremelabs.robolectric.shadows;
     22 
     23 import android.content.Context;
     24 import android.util.DisplayMetrics;
     25 import android.view.ViewConfiguration;
     26 import com.xtremelabs.robolectric.Robolectric;
     27 import com.xtremelabs.robolectric.internal.Implementation;
     28 import com.xtremelabs.robolectric.internal.Implements;
     29 import com.xtremelabs.robolectric.internal.RealObject;
     30 
     31 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
     32 
     33 @SuppressWarnings({"UnusedDeclaration"})
     34 @Implements(ViewConfiguration.class)
     35 public class ShadowViewConfiguration {
     36 
     37     private static final int SCROLL_BAR_SIZE = 10;
     38     private static final int SCROLL_BAR_FADE_DURATION = 250;
     39     private static final int SCROLL_BAR_DEFAULT_DELAY = 300;
     40     private static final int FADING_EDGE_LENGTH = 12;
     41     private static final int PRESSED_STATE_DURATION = 125;
     42     private static final int LONG_PRESS_TIMEOUT = 500;
     43     private static final int GLOBAL_ACTIONS_KEY_TIMEOUT = 500;
     44     private static final int TAP_TIMEOUT = 115;
     45     private static final int JUMP_TAP_TIMEOUT = 500;
     46     private static final int DOUBLE_TAP_TIMEOUT = 300;
     47     private static final int ZOOM_CONTROLS_TIMEOUT = 3000;
     48     private static final int EDGE_SLOP = 12;
     49     private static final int TOUCH_SLOP = 16;
     50     private static final int PAGING_TOUCH_SLOP = TOUCH_SLOP * 2;
     51     private static final int DOUBLE_TAP_SLOP = 100;
     52     private static final int WINDOW_TOUCH_SLOP = 16;
     53     private static final int MINIMUM_FLING_VELOCITY = 50;
     54     private static final int MAXIMUM_FLING_VELOCITY = 4000;
     55     private static final int MAXIMUM_DRAWING_CACHE_SIZE = 320 * 480 * 4;
     56     private static float SCROLL_FRICTION = 0.015f;
     57     private static final int OVERSCROLL_DISTANCE = 0;
     58     private static final int OVERFLING_DISTANCE = 4;
     59 
     60     private int edgeSlop;
     61     private int fadingEdgeLength;
     62     private int minimumFlingVelocity;
     63     private int maximumFlingVelocity;
     64     private int scrollbarSize;
     65     private int touchSlop;
     66     private int pagingTouchSlop;
     67     private int doubleTapSlop;
     68     private int windowTouchSlop;
     69 
     70     @RealObject
     71     private ViewConfiguration realViewConfiguration;
     72 
     73     private void setup(Context context) {
     74         DisplayMetrics metrics = context.getResources().getDisplayMetrics();
     75         float density = metrics.density;
     76 
     77         edgeSlop = (int) (density * EDGE_SLOP + 0.5f);
     78         fadingEdgeLength = (int) (density * FADING_EDGE_LENGTH + 0.5f);
     79         minimumFlingVelocity = (int) (density * MINIMUM_FLING_VELOCITY + 0.5f);
     80         maximumFlingVelocity = (int) (density * MAXIMUM_FLING_VELOCITY + 0.5f);
     81         scrollbarSize = (int) (density * SCROLL_BAR_SIZE + 0.5f);
     82         touchSlop = (int) (density * TOUCH_SLOP + 0.5f);
     83         pagingTouchSlop = (int) (density * PAGING_TOUCH_SLOP + 0.5f);
     84         doubleTapSlop = (int) (density * DOUBLE_TAP_SLOP + 0.5f);
     85         windowTouchSlop = (int) (density * WINDOW_TOUCH_SLOP + 0.5f);
     86     }
     87 
     88     @Implementation
     89     public static ViewConfiguration get(Context context) {
     90         ViewConfiguration viewConfiguration = Robolectric.newInstanceOf(ViewConfiguration.class);
     91         shadowOf(viewConfiguration).setup(context);
     92         return viewConfiguration;
     93     }
     94 
     95     @Implementation
     96     public static int getScrollBarSize() {
     97         return SCROLL_BAR_SIZE;
     98     }
     99 
    100     @Implementation
    101     public int getScaledScrollBarSize() {
    102         return scrollbarSize;
    103     }
    104 
    105     @Implementation
    106     public static int getScrollBarFadeDuration() {
    107         return SCROLL_BAR_FADE_DURATION;
    108     }
    109 
    110     @Implementation
    111     public static int getScrollDefaultDelay() {
    112         return SCROLL_BAR_DEFAULT_DELAY;
    113     }
    114 
    115     @Implementation
    116     public static int getFadingEdgeLength() {
    117         return FADING_EDGE_LENGTH;
    118     }
    119 
    120     @Implementation
    121     public int getScaledFadingEdgeLength() {
    122         return fadingEdgeLength;
    123     }
    124 
    125     @Implementation
    126     public static int getPressedStateDuration() {
    127         return PRESSED_STATE_DURATION;
    128     }
    129 
    130     @Implementation
    131     public static int getLongPressTimeout() {
    132         return LONG_PRESS_TIMEOUT;
    133     }
    134 
    135     @Implementation
    136     public static int getTapTimeout() {
    137         return TAP_TIMEOUT;
    138     }
    139 
    140     @Implementation
    141     public static int getJumpTapTimeout() {
    142         return JUMP_TAP_TIMEOUT;
    143     }
    144 
    145     @Implementation
    146     public static int getDoubleTapTimeout() {
    147         return DOUBLE_TAP_TIMEOUT;
    148     }
    149 
    150     @Implementation
    151     public static int getEdgeSlop() {
    152         return EDGE_SLOP;
    153     }
    154 
    155     @Implementation
    156     public int getScaledEdgeSlop() {
    157         return edgeSlop;
    158     }
    159 
    160     @Implementation
    161     public static int getTouchSlop() {
    162         return TOUCH_SLOP;
    163     }
    164 
    165     @Implementation
    166     public int getScaledTouchSlop() {
    167         return touchSlop;
    168     }
    169 
    170     @Implementation
    171     public int getScaledPagingTouchSlop() {
    172         return pagingTouchSlop;
    173     }
    174 
    175     @Implementation
    176     public int getScaledDoubleTapSlop() {
    177         return doubleTapSlop;
    178     }
    179 
    180     @Implementation
    181     public static int getWindowTouchSlop() {
    182         return WINDOW_TOUCH_SLOP;
    183     }
    184 
    185     @Implementation
    186     public int getScaledWindowTouchSlop() {
    187         return windowTouchSlop;
    188     }
    189 
    190     @Implementation
    191     public static int getMinimumFlingVelocity() {
    192         return MINIMUM_FLING_VELOCITY;
    193     }
    194 
    195     @Implementation
    196     public int getScaledMinimumFlingVelocity() {
    197         return minimumFlingVelocity;
    198     }
    199 
    200     @Implementation
    201     public static int getMaximumFlingVelocity() {
    202         return MAXIMUM_FLING_VELOCITY;
    203     }
    204 
    205     @Implementation
    206     public int getScaledMaximumFlingVelocity() {
    207         return maximumFlingVelocity;
    208     }
    209 
    210     @Implementation
    211     public static int getMaximumDrawingCacheSize() {
    212         return MAXIMUM_DRAWING_CACHE_SIZE;
    213     }
    214 
    215     @Implementation
    216     public static long getZoomControlsTimeout() {
    217         return ZOOM_CONTROLS_TIMEOUT;
    218     }
    219 
    220     @Implementation
    221     public static long getGlobalActionKeyTimeout() {
    222         return GLOBAL_ACTIONS_KEY_TIMEOUT;
    223     }
    224 
    225     @Implementation
    226     public static float getScrollFriction() {
    227         return SCROLL_FRICTION;
    228     }
    229 
    230 }
    231