Search in sources :

Example 26 with State

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());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Device(com.android.sdklib.devices.Device) State(com.android.sdklib.devices.State) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) IAndroidTarget(com.android.sdklib.IAndroidTarget) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 27 with State

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);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Configuration(com.android.tools.idea.configurations.Configuration) Device(com.android.sdklib.devices.Device) Screen(com.android.sdklib.devices.Screen) NlEditor(com.android.tools.idea.uibuilder.editor.NlEditor) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) State(com.android.sdklib.devices.State) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 28 with State

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;
    }
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) GradleSyncState(com.android.tools.idea.gradle.project.sync.GradleSyncState) State(com.android.sdklib.devices.State) Device(com.android.sdklib.devices.Device) Screen(com.android.sdklib.devices.Screen) Density(com.android.resources.Density)

Example 29 with State

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);
}
Also used : ScreenOrientation(com.android.resources.ScreenOrientation) State(com.android.sdklib.devices.State) Screen(com.android.sdklib.devices.Screen) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with State

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();
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) State(com.android.sdklib.devices.State) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

State (com.android.sdklib.devices.State)30 Device (com.android.sdklib.devices.Device)20 IAndroidTarget (com.android.sdklib.IAndroidTarget)8 Configuration (com.android.tools.idea.configurations.Configuration)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 Nullable (org.jetbrains.annotations.Nullable)7 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)6 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)5 NotNull (org.jetbrains.annotations.NotNull)5 Module (com.intellij.openapi.module.Module)4 Screen (com.android.sdklib.devices.Screen)3 Locale (com.android.tools.idea.rendering.Locale)3 NightMode (com.android.resources.NightMode)2 UiMode (com.android.resources.UiMode)2 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)2 PsiFile (com.intellij.psi.PsiFile)2 HardwareConfigHelper (com.android.ide.common.rendering.HardwareConfigHelper)1 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)1 HardwareConfig (com.android.ide.common.rendering.api.HardwareConfig)1 DensityQualifier (com.android.ide.common.resources.configuration.DensityQualifier)1