• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.android;
2 
3 import android.content.res.Configuration;
4 import android.os.Build;
5 import android.os.Build.VERSION_CODES;
6 import android.util.DisplayMetrics;
7 import org.robolectric.RuntimeEnvironment;
8 import org.robolectric.res.Qualifiers;
9 import org.robolectric.shadows.ShadowDisplayManager;
10 import org.robolectric.shadows.ShadowWindowManagerImpl;
11 
12 public class Bootstrap {
13 
setUpDisplay(Configuration configuration, DisplayMetrics displayMetrics)14   public static void setUpDisplay(Configuration configuration, DisplayMetrics displayMetrics) {
15     if (Build.VERSION.SDK_INT == VERSION_CODES.JELLY_BEAN) {
16       ShadowWindowManagerImpl.configureDefaultDisplayForJBOnly(configuration, displayMetrics);
17     } else {
18       ShadowDisplayManager.configureDefaultDisplay(configuration, displayMetrics);
19     }
20   }
21 
applyQualifiers(String qualifiersStrs, int apiLevel, Configuration configuration, DisplayMetrics displayMetrics)22   public static void applyQualifiers(String qualifiersStrs, int apiLevel,
23       Configuration configuration, DisplayMetrics displayMetrics) {
24 
25     String[] qualifiersParts = qualifiersStrs.split(" ", 0);
26     int i = qualifiersParts.length - 1;
27     // find the index of the left-most qualifier string that doesn't start with '+'
28     for (; i >= 0 ; i--) {
29       String qualifiersStr = qualifiersParts[i];
30       if (qualifiersStr.startsWith("+")) {
31         qualifiersParts[i] = qualifiersStr.substring(1);
32       } else {
33         break;
34       }
35     }
36 
37     for (i = (i < 0) ? 0 : i; i < qualifiersParts.length ; i++) {
38       String qualifiersStr = qualifiersParts[i];
39       int platformVersion = Qualifiers.getPlatformVersion(qualifiersStr);
40       if (platformVersion != -1 && platformVersion != apiLevel) {
41         throw new IllegalArgumentException(
42             "Cannot specify conflicting platform version in qualifiers: \"" + qualifiersStr + "\"");
43       }
44 
45       Qualifiers qualifiers = Qualifiers.parse(qualifiersStr);
46 
47       DeviceConfig.applyToConfiguration(qualifiers, apiLevel, configuration, displayMetrics);
48     }
49 
50     DeviceConfig.applyRules(configuration, displayMetrics, apiLevel);
51 
52     fixJellyBean(configuration, displayMetrics);
53   }
54 
fixJellyBean(Configuration configuration, DisplayMetrics displayMetrics)55   private static void fixJellyBean(Configuration configuration, DisplayMetrics displayMetrics) {
56     if (RuntimeEnvironment.getApiLevel() < Build.VERSION_CODES.KITKAT) {
57       int widthPx = (int) (configuration.screenWidthDp * displayMetrics.density);
58       int heightPx = (int) (configuration.screenHeightDp * displayMetrics.density);
59       displayMetrics.widthPixels = displayMetrics.noncompatWidthPixels = widthPx;
60       displayMetrics.heightPixels = displayMetrics.noncompatHeightPixels = heightPx;
61       displayMetrics.xdpi = displayMetrics.noncompatXdpi = displayMetrics.densityDpi;
62       displayMetrics.ydpi = displayMetrics.noncompatYdpi = displayMetrics.densityDpi;
63     }
64   }
65 
66 }
67