/** * Changes the density for this display. * * Any registered {@link android.hardware.display.DisplayManager.DisplayListener}s will be * notified of the change. */ public void setDensityDpi(int densityDpi) { if (isJB()) { this.densityDpi = densityDpi; } else { ShadowDisplayManager.changeDisplay(realObject.getDisplayId(), di -> di.logicalDensityDpi = densityDpi); } }
public static void setUpDisplay(Configuration configuration, DisplayMetrics displayMetrics) { if (Build.VERSION.SDK_INT == VERSION_CODES.JELLY_BEAN) { ShadowWindowManagerImpl.configureDefaultDisplayForJBOnly(configuration, displayMetrics); } else { ShadowDisplayManager.configureDefaultDisplay(configuration, displayMetrics); } }
/** * Adds a simulated display. * * @param qualifiersStr the {@link Qualifiers} string representing characteristics of the new * display. * @return the new display's ID */ public static int addDisplay(String qualifiersStr) { return getShadowDisplayManagerGlobal().addDisplay(createDisplayInfo(qualifiersStr, null)); }
/** * Changes properties of a simulated display. The original properties will be passed to the * `consumer`, which may modify them in place. The display will be updated with the new * properties. * * @param displayId the display id to change * @param consumer a function which modifies the display properties */ static void changeDisplay(int displayId, Consumer<DisplayConfig> consumer) { DisplayInfo displayInfo = DisplayManagerGlobal.getInstance().getDisplayInfo(displayId); if (displayInfo != null) { DisplayConfig displayConfig = new DisplayConfig(displayInfo); consumer.accept(displayConfig); displayConfig.copyTo(displayInfo); fixNominalDimens(displayInfo); } getShadowDisplayManagerGlobal().changeDisplay(displayId, displayInfo); }
@Test @Config(minSdk = P) public void getSaturationLevel_setToOneViaShadow_shouldReturnOne() { shadowOf(instance).setSaturationLevel(0.0f); shadowOf(instance).setSaturationLevel(1.0f); assertThat(shadowOf(instance).getSaturationLevel()).isEqualTo(1.0f); }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void changeAndRemoveDisplay_shouldNotifyListeners() throws Exception { List<String> events = new ArrayList<>(); instance.registerDisplayListener(new MyDisplayListener(events), null); int displayId = ShadowDisplayManager.addDisplay("w100dp-h200dp"); ShadowDisplayManager.changeDisplay(displayId, "w300dp-h400dp"); Display display = getGlobal().getRealDisplay(displayId); assertThat(display.getWidth()).isEqualTo(300); assertThat(display.getHeight()).isEqualTo(400); assertThat(display.getOrientation()).isEqualTo(Surface.ROTATION_0); ShadowDisplayManager.removeDisplay(displayId); assertThat(events).containsExactly( "Added " + displayId, "Changed " + displayId, "Removed " + displayId); }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void changeDisplay_shouldAllowPartialChanges() throws Exception { List<String> events = new ArrayList<>(); instance.registerDisplayListener(new MyDisplayListener(events), null); int displayId = ShadowDisplayManager.addDisplay("w100dp-h200dp"); ShadowDisplayManager.changeDisplay(displayId, "+h201dp-land"); Display display = getGlobal().getRealDisplay(displayId); assertThat(display.getWidth()).isEqualTo(201); assertThat(display.getHeight()).isEqualTo(100); assertThat(display.getOrientation()).isEqualTo(Surface.ROTATION_90); assertThat(events).containsExactly( "Added " + displayId, "Changed " + displayId); }
/** * Returns the current display saturation level set via {@link * android.hardware.display.DisplayManager#setSaturationLevel(float)}. */ public float getSaturationLevel() { return getShadowDisplayManagerGlobal().getSaturationLevel(); }
private static DisplayInfo createDisplayInfo(String qualifiersStr, DisplayInfo baseDisplayInfo) { Configuration configuration = new Configuration(); DisplayMetrics displayMetrics = new DisplayMetrics(); if (qualifiersStr.startsWith("+") && baseDisplayInfo != null) { configuration.orientation = (baseDisplayInfo.rotation == Surface.ROTATION_0 || baseDisplayInfo.rotation == Surface.ROTATION_180) ? Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE; configuration.screenWidthDp = baseDisplayInfo.logicalWidth * DisplayMetrics.DENSITY_DEFAULT / baseDisplayInfo.logicalDensityDpi; configuration.screenHeightDp = baseDisplayInfo.logicalHeight * DisplayMetrics.DENSITY_DEFAULT / baseDisplayInfo.logicalDensityDpi; configuration.densityDpi = baseDisplayInfo.logicalDensityDpi; displayMetrics.densityDpi = baseDisplayInfo.logicalDensityDpi; displayMetrics.density = baseDisplayInfo.logicalDensityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE; } Bootstrap.applyQualifiers(qualifiersStr, RuntimeEnvironment.getApiLevel(), configuration, displayMetrics); return createDisplayInfo(configuration, displayMetrics); }
fixNominalDimens(displayInfo); displayInfo.logicalWidth = widthPx; displayInfo.logicalHeight = heightPx;
@Test @Config(minSdk = P) public void getSaturationLevel_defaultValue_shouldReturnOne() { assertThat(shadowOf(instance).getSaturationLevel()).isEqualTo(1.0f); }
@Test @Config(maxSdk = JELLY_BEAN) public void notSupportedInJellyBean() throws Exception { try { ShadowDisplayManager.removeDisplay(0); fail("Expected Exception thrown"); } catch (UnsupportedOperationException e) { assertThat(e).hasMessageThat().contains("displays not supported in Jelly Bean"); } }
@Test @Config(minSdk = P) public void setSaturationLevel_setToValueGreaterThanOneViaShadow_shouldThrow() { try { shadowOf(instance).setSaturationLevel(1.1f); fail("Expected IllegalArgumentException thrown"); } catch (IllegalArgumentException expected) {} }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void addDisplay_shouldNotifyListeners() throws Exception { List<String> events = new ArrayList<>(); instance.registerDisplayListener(new MyDisplayListener(events), null); int displayId = ShadowDisplayManager.addDisplay("w100dp-h200dp"); assertThat(events).containsExactly("Added " + displayId); }
/** * Changes properties of a simulated display. The original properties will be passed to the * `consumer`, which may modify them in place. The display will be updated with the new * properties. * * @param displayId the display id to change * @param consumer a function which modifies the display properties */ static void changeDisplay(int displayId, Consumer<DisplayConfig> consumer) { DisplayInfo displayInfo = DisplayManagerGlobal.getInstance().getDisplayInfo(displayId); if (displayInfo != null) { DisplayConfig displayConfig = new DisplayConfig(displayInfo); consumer.accept(displayConfig); displayConfig.copyTo(displayInfo); fixNominalDimens(displayInfo); } getShadowDisplayManagerGlobal().changeDisplay(displayId, displayInfo); }
/** * Removes a simulated display. * * @param displayId the display id to remove */ public static void removeDisplay(int displayId) { getShadowDisplayManagerGlobal().removeDisplay(displayId); }
@Test @Config(minSdk = P) public void getSaturationLevel_setToZeroViaShadow_shouldReturnZero() { shadowOf(instance).setSaturationLevel(0.0f); assertThat(shadowOf(instance).getSaturationLevel()).isEqualTo(0.0f); }
private static DisplayInfo createDisplayInfo(String qualifiersStr, DisplayInfo baseDisplayInfo) { Configuration configuration = new Configuration(); DisplayMetrics displayMetrics = new DisplayMetrics(); if (qualifiersStr.startsWith("+") && baseDisplayInfo != null) { configuration.orientation = (baseDisplayInfo.rotation == Surface.ROTATION_0 || baseDisplayInfo.rotation == Surface.ROTATION_180) ? Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE; configuration.screenWidthDp = baseDisplayInfo.logicalWidth * DisplayMetrics.DENSITY_DEFAULT / baseDisplayInfo.logicalDensityDpi; configuration.screenHeightDp = baseDisplayInfo.logicalHeight * DisplayMetrics.DENSITY_DEFAULT / baseDisplayInfo.logicalDensityDpi; configuration.densityDpi = baseDisplayInfo.logicalDensityDpi; displayMetrics.densityDpi = baseDisplayInfo.logicalDensityDpi; displayMetrics.density = baseDisplayInfo.logicalDensityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE; } Bootstrap.applyQualifiers(qualifiersStr, RuntimeEnvironment.getApiLevel(), configuration, displayMetrics); return createDisplayInfo(configuration, displayMetrics); }
fixNominalDimens(displayInfo); displayInfo.logicalWidth = widthPx; displayInfo.logicalHeight = heightPx;
@Test @Config(minSdk = P) public void getSaturationLevel_setToHalf_shouldReturnHalf() { instance.setSaturationLevel(0.5f); assertThat(shadowOf(instance).getSaturationLevel()).isEqualTo(0.5f); }