Search in sources :

Example 21 with State

use of com.android.sdklib.devices.State in project android by JetBrains.

the class Configuration method getScreenSize.

/** Returns the screen size required for this configuration */
@Nullable
public ScreenSize getScreenSize() {
    // Look up the screen size for the current state
    State deviceState = getDeviceState();
    if (deviceState != null) {
        FolderConfiguration folderConfig = DeviceConfigHelper.getFolderConfig(deviceState);
        if (folderConfig != null) {
            ScreenSizeQualifier qualifier = folderConfig.getScreenSizeQualifier();
            assert qualifier != null;
            return qualifier.getValue();
        }
    }
    ScreenSize screenSize = null;
    Device device = getDevice();
    if (device != null) {
        List<State> states = device.getAllStates();
        for (State state : states) {
            FolderConfiguration folderConfig = DeviceConfigHelper.getFolderConfig(state);
            if (folderConfig != null) {
                ScreenSizeQualifier qualifier = folderConfig.getScreenSizeQualifier();
                assert qualifier != null;
                screenSize = qualifier.getValue();
                break;
            }
        }
    }
    return screenSize;
}
Also used : State(com.android.sdklib.devices.State) Device(com.android.sdklib.devices.Device) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with State

use of com.android.sdklib.devices.State in project android by JetBrains.

the class Configuration method computeBestDevice.

@Nullable
private Device computeBestDevice() {
    for (Device device : myManager.getRecentDevices()) {
        String stateName = myStateName;
        if (stateName == null) {
            stateName = device.getDefaultState().getName();
        }
        State selectedState = ConfigurationFileState.getState(device, stateName);
        Module module = myManager.getModule();
        FolderConfiguration currentConfig = getFolderConfig(module, selectedState, getLocale(), getTarget());
        if (currentConfig != null) {
            if (myEditedConfig.isMatchFor(currentConfig)) {
                LocalResourceRepository resources = AppResourceRepository.getAppResources(module, true);
                if (resources != null && myFile != null) {
                    ResourceFolderType folderType = ResourceHelper.getFolderType(myFile);
                    if (folderType != null) {
                        if (ResourceFolderType.VALUES.equals(folderType)) {
                            // If it's a file in the values folder, ResourceRepository.getMatchingFiles won't work.
                            // We get instead all the available folders and check that there is one compatible.
                            LocalResourceManager resourceManager = LocalResourceManager.getInstance(module);
                            if (resourceManager != null) {
                                for (PsiFile resourceFile : resourceManager.findResourceFiles("values")) {
                                    if (myFile.equals(resourceFile.getVirtualFile()) && resourceFile.getParent() != null) {
                                        FolderConfiguration folderConfiguration = FolderConfiguration.getConfigForFolder(resourceFile.getParent().getName());
                                        if (currentConfig.isMatchFor(folderConfiguration)) {
                                            return device;
                                        }
                                    }
                                }
                            }
                        } else {
                            List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
                            if (!types.isEmpty()) {
                                ResourceType type = types.get(0);
                                List<VirtualFile> matches = resources.getMatchingFiles(myFile, type, currentConfig);
                                if (matches.contains(myFile)) {
                                    return device;
                                }
                            }
                        }
                    } else if ("Kotlin".equals(myFile.getFileType().getName())) {
                        return device;
                    } else if (myFile.equals(myManager.getProject().getProjectFile())) {
                        // takes care of correct device selection for Theme Editor
                        return device;
                    }
                }
            }
        }
    }
    return myManager.getDefaultDevice();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) Device(com.android.sdklib.devices.Device) State(com.android.sdklib.devices.State) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with State

use of com.android.sdklib.devices.State in project android by JetBrains.

the class NlUsageTrackerManagerTest method getConfigurationMock.

private static Configuration getConfigurationMock() {
    IAndroidTarget target = mock(IAndroidTarget.class);
    when(target.getVersion()).thenReturn(new AndroidVersion(0, "mock"));
    State state = mock(State.class);
    when(state.getOrientation()).thenReturn(PORTRAIT);
    Configuration configuration = mock(Configuration.class);
    when(configuration.getTarget()).thenReturn(target);
    when(configuration.getDeviceState()).thenReturn(state);
    return configuration;
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) State(com.android.sdklib.devices.State) LayoutEditorState(com.google.wireless.android.sdk.stats.LayoutEditorState) IAndroidTarget(com.android.sdklib.IAndroidTarget) AndroidVersion(com.android.sdklib.AndroidVersion)

Example 24 with State

use of com.android.sdklib.devices.State in project android by JetBrains.

the class ScreenView method toggleOrientation.

public void toggleOrientation() {
    Configuration configuration = getConfiguration();
    configuration.getDeviceState();
    State current = configuration.getDeviceState();
    State flip = configuration.getNextDeviceState(current);
    if (flip != null) {
        configuration.setDeviceState(flip);
    }
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) State(com.android.sdklib.devices.State)

Example 25 with State

use of com.android.sdklib.devices.State in project android by JetBrains.

the class ScreenView method getPreferredSize.

/**
   * Returns the current preferred size for the view.
   * @param dimension optional existing {@link Dimension} instance to be reused. If not null, the values will be set and this instance
   *                  returned.
   */
@NotNull
public Dimension getPreferredSize(@Nullable Dimension dimension) {
    if (dimension == null) {
        dimension = new Dimension();
    }
    Configuration configuration = getConfiguration();
    Device device = configuration.getDevice();
    State state = configuration.getDeviceState();
    if (device != null && state != null) {
        HardwareConfig config = new HardwareConfigHelper(device).setOrientation(state.getOrientation()).getConfig();
        dimension.setSize(config.getScreenWidth(), config.getScreenHeight());
    }
    return dimension;
}
Also used : HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) Configuration(com.android.tools.idea.configurations.Configuration) Device(com.android.sdklib.devices.Device) State(com.android.sdklib.devices.State) HardwareConfigHelper(com.android.ide.common.rendering.HardwareConfigHelper) 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