use of com.android.sdklib.devices.State in project android by JetBrains.
the class VaryingConfigurationTest method test.
public void test() throws Exception {
VirtualFile file = myFixture.copyFileToProject(TEST_FILE, "res/layout/layout1.xml");
final AndroidFacet facet = AndroidFacet.getInstance(myModule);
assertNotNull(facet);
ConfigurationManager manager = facet.getConfigurationManager();
assertNotNull(manager);
assertSame(manager, facet.getConfigurationManager());
Configuration parent = Configuration.create(manager, file, new FolderConfiguration());
assertNotNull(parent);
parent.startBulkEditing();
parent.setDisplayName("myconfig");
parent.setTheme("@style/Theme1");
parent.setNightMode(NightMode.NIGHT);
parent.setActivity("tes.tpkg.MyActivity1");
parent.setUiMode(UiMode.TELEVISION);
IAndroidTarget target = parent.getConfigurationManager().getTarget();
Device device = parent.getConfigurationManager().getDefaultDevice();
State deviceState = device != null ? device.getState("Portrait") : null;
if (target != null) {
parent.setTarget(target);
}
if (device != null) {
parent.setDevice(device, false);
assertNotNull(deviceState);
parent.setDeviceState(deviceState);
}
parent.setLocale(Locale.create("en-rUS"));
parent.finishBulkEditing();
VaryingConfiguration configuration = VaryingConfiguration.create(parent);
configuration.setAlternateUiMode(true);
assertEquals(UiMode.TELEVISION, parent.getUiMode());
assertEquals(UiMode.APPLIANCE, configuration.getUiMode());
parent.setUiMode(UiMode.APPLIANCE);
assertEquals(UiMode.WATCH, configuration.getUiMode());
assertNotNull(device);
State portrait = device.getState("Portrait");
State landscape = device.getState("Landscape");
assertNotNull(portrait);
assertNotNull(landscape);
configuration.setAlternateDeviceState(true);
assertTrue(configuration.isAlternatingDeviceState());
assertEquals(ScreenOrientation.LANDSCAPE, configuration.getFullConfig().getScreenOrientationQualifier().getValue());
parent.setDeviceState(landscape);
assertEquals(ScreenOrientation.PORTRAIT, configuration.getFullConfig().getScreenOrientationQualifier().getValue());
assertEquals(portrait, configuration.getDeviceState());
parent.setDeviceState(portrait);
assertEquals(ScreenOrientation.LANDSCAPE, configuration.getFullConfig().getScreenOrientationQualifier().getValue());
assertEquals(landscape, configuration.getDeviceState());
}
use of com.android.sdklib.devices.State in project android by JetBrains.
the class NlModel method overrideConfigurationScreenSize.
/**
* Changes the configuration to use a custom device with screen size defined by xDimension and yDimension.
*/
public void overrideConfigurationScreenSize(@AndroidCoordinate int xDimension, @AndroidCoordinate int yDimension) {
Device original = myConfiguration.getDevice();
// doesn't copy tag id
Device.Builder deviceBuilder = new Device.Builder(original);
if (original != null) {
deviceBuilder.setTagId(original.getTagId());
}
deviceBuilder.setName("Custom");
deviceBuilder.setId(Configuration.CUSTOM_DEVICE_ID);
Device device = deviceBuilder.build();
for (State state : device.getAllStates()) {
Screen screen = state.getHardware().getScreen();
screen.setXDimension(xDimension);
screen.setYDimension(yDimension);
double dpi = screen.getPixelDensity().getDpiValue();
double width = xDimension / dpi;
double height = yDimension / dpi;
double diagonalLength = Math.sqrt(width * width + height * height);
screen.setDiagonalLength(diagonalLength);
screen.setSize(AvdScreenData.getScreenSize(diagonalLength));
screen.setRatio(AvdScreenData.getScreenRatio(xDimension, yDimension));
screen.setScreenRound(device.getDefaultHardware().getScreen().getScreenRound());
screen.setChin(device.getDefaultHardware().getScreen().getChin());
}
// If a custom device already exists, remove it before adding the latest one
List<Device> devices = myConfiguration.getConfigurationManager().getDevices();
boolean customDeviceReplaced = false;
for (int i = 0; i < devices.size(); i++) {
if ("Custom".equals(devices.get(i).getId())) {
devices.set(i, device);
customDeviceReplaced = true;
break;
}
}
if (!customDeviceReplaced) {
devices.add(device);
}
VirtualFile better;
State newState;
//Change the orientation of the device depending on the shape of the canvas
if (xDimension > yDimension) {
better = ConfigurationMatcher.getBetterMatch(myConfiguration, device, "Landscape", null, null);
newState = device.getState("Landscape");
} else {
better = ConfigurationMatcher.getBetterMatch(myConfiguration, device, "Portrait", null, null);
newState = device.getState("Portrait");
}
if (better != null) {
VirtualFile old = myConfiguration.getFile();
assert old != null;
Project project = mySurface.getProject();
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, better, -1);
FileEditorManager manager = FileEditorManager.getInstance(project);
FileEditor selectedEditor = manager.getSelectedEditor(old);
manager.openEditor(descriptor, true);
// Switch to the same type of editor (XML or Layout Editor) in the target file
if (selectedEditor instanceof NlEditor) {
manager.setSelectedEditor(better, NlEditorProvider.DESIGNER_ID);
} else if (selectedEditor != null) {
manager.setSelectedEditor(better, TextEditorProvider.getInstance().getEditorTypeId());
}
AndroidFacet facet = AndroidFacet.getInstance(myConfiguration.getModule());
assert facet != null;
Configuration configuration = facet.getConfigurationManager().getConfiguration(better);
configuration.setEffectiveDevice(device, newState);
} else {
myConfiguration.setEffectiveDevice(device, newState);
}
}
use of com.android.sdklib.devices.State in project android by JetBrains.
the class NlOldPalettePanel method updateConfiguration.
private void updateConfiguration() {
myConfiguration = null;
if (myDesignSurface == null) {
return;
}
Configuration designConfiguration = myDesignSurface.getConfiguration();
if (designConfiguration == null) {
return;
}
State designState = designConfiguration.getDeviceState();
Configuration configuration = designConfiguration.clone();
Device device = configuration.getDevice();
if (device == null) {
return;
}
// Override to a predefined density that closest matches the screens resolution
Density override = null;
int monitorResolution = Toolkit.getDefaultToolkit().getScreenResolution();
for (Density density : Density.values()) {
if (density.getDpiValue() > 0) {
if (override == null || Math.abs(density.getDpiValue() - monitorResolution) < Math.abs(override.getDpiValue() - monitorResolution)) {
override = density;
}
}
}
if (override != null) {
device = new Device.Builder(device).build();
for (State state : device.getAllStates()) {
Screen screen = state.getHardware().getScreen();
screen.setXDimension((int) (screen.getXDimension() * override.getDpiValue() / screen.getXdpi()));
screen.setYDimension((int) (screen.getYDimension() * override.getDpiValue() / screen.getYdpi()));
screen.setXdpi(override.getDpiValue());
screen.setYdpi(override.getDpiValue());
screen.setPixelDensity(override);
}
configuration.setDevice(device, false);
if (designState != null) {
configuration.setDeviceStateName(designState.getName());
}
myConfiguration = configuration;
}
}
use of com.android.sdklib.devices.State in project android by JetBrains.
the class IconPreviewFactory method getPreviewCacheDirForConfiguration.
@NotNull
private static File getPreviewCacheDirForConfiguration(@NotNull Configuration configuration) {
int density = configuration.getDensity().getDpiValue();
State state = configuration.getDeviceState();
Screen screen = state != null ? state.getHardware().getScreen() : null;
int xDimension = DEFAULT_X_DIMENSION;
int yDimension = DEFAULT_Y_DIMENSION;
if (screen != null) {
xDimension = screen.getXDimension();
yDimension = screen.getYDimension();
density = screen.getPixelDensity().getDpiValue();
}
ScreenOrientation orientation = state != null ? state.getOrientation() : ScreenOrientation.PORTRAIT;
if ((orientation == ScreenOrientation.LANDSCAPE && xDimension < yDimension) || (orientation == ScreenOrientation.PORTRAIT && xDimension > yDimension)) {
int temp = xDimension;
//noinspection SuspiciousNameCombination
xDimension = yDimension;
yDimension = temp;
}
String theme = getTheme(configuration);
String apiVersion = getApiVersion(configuration);
String cacheFolder = theme + File.separator + xDimension + "x" + yDimension + "-" + density + "-" + apiVersion;
return new File(getPreviewCacheDir(), cacheFolder);
}
use of com.android.sdklib.devices.State in project android by JetBrains.
the class NlUsageTrackerManager method getState.
/**
* Generates a {@link LayoutEditorState} containing all the state of the layout editor from the given surface.
*/
@NotNull
static LayoutEditorState getState(@Nullable DesignSurface surface) {
LayoutEditorState.Builder builder = LayoutEditorState.newBuilder();
if (surface == null) {
return builder.build();
}
builder.setMode(surface.isPreviewSurface() ? LayoutEditorState.Mode.PREVIEW_MODE : LayoutEditorState.Mode.DESIGN_MODE);
switch(surface.getLayoutType()) {
case DRAWABLE:
builder.setType(LayoutEditorState.Type.DRAWABLE);
break;
case LAYOUT:
builder.setType(LayoutEditorState.Type.LAYOUT);
break;
case MENU:
builder.setType(LayoutEditorState.Type.MENU);
break;
case PREFERENCE_SCREEN:
builder.setType(LayoutEditorState.Type.PREFERENCE_SCREEN);
break;
case UNKNOWN:
}
double scale = surface.getScale();
if (SystemInfo.isMac && UIUtil.isRetina()) {
scale *= 2;
}
Configuration configuration = surface.getConfiguration();
if (configuration != null) {
State deviceState = configuration.getDeviceState();
if (deviceState != null) {
switch(deviceState.getOrientation()) {
case PORTRAIT:
builder.setConfigOrientation(LayoutEditorState.Orientation.PORTRAIT);
break;
case LANDSCAPE:
builder.setConfigOrientation(LayoutEditorState.Orientation.LANDSCAPE);
break;
case SQUARE:
}
}
if (configuration.getTarget() != null) {
builder.setConfigApiLevel(configuration.getTarget().getVersion().getApiString());
}
}
if (scale >= 0) {
builder.setConfigZoomLevel((int) (scale * 100));
}
switch(surface.getScreenMode()) {
case SCREEN_ONLY:
builder.setSurfaces(LayoutEditorState.Surfaces.SCREEN_SURFACE);
break;
case BLUEPRINT_ONLY:
builder.setSurfaces(LayoutEditorState.Surfaces.BLUEPRINT_SURFACE);
break;
case BOTH:
builder.setSurfaces(LayoutEditorState.Surfaces.BOTH);
break;
}
return builder.build();
}
Aggregations