Search in sources :

Example 16 with State

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

the class ConfigureDeviceModel method createState.

@Nullable
private static State createState(ScreenOrientation orientation, Hardware hardware, boolean hasHardwareKeyboard) {
    State state = null;
    String name = "";
    String description = "";
    if (orientation == ScreenOrientation.LANDSCAPE) {
        name = "Landscape";
        description = "The device in landscape orientation";
        state = new State();
    } else if (orientation == ScreenOrientation.PORTRAIT) {
        name = "Portrait";
        description = "The device in portrait orientation";
        state = new State();
    }
    if (state != null) {
        if (hasHardwareKeyboard) {
            name += " with keyboard";
            description += " with a keyboard open";
            state.setKeyState(KeyboardState.EXPOSED);
        } else {
            if (hardware.getKeyboard() != null && hardware.getKeyboard().equals(Keyboard.NOKEY)) {
                state.setKeyState(KeyboardState.SOFT);
            } else {
                state.setKeyState(KeyboardState.HIDDEN);
            }
        }
        state.setName(name);
        state.setHardware(hardware);
        state.setOrientation(orientation);
        state.setDescription(description);
        state.setNavState(hardware.getNav().equals(Navigation.NONAV) ? NavigationState.HIDDEN : NavigationState.EXPOSED);
    }
    return state;
}
Also used : State(com.android.sdklib.devices.State) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with State

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

the class ConfigurationMatcher method adaptConfigSelection.

/**
   * Adapts the current device/config selection so that it's compatible with
   * the configuration.
   * <p/>
   * If the current selection is compatible, nothing is changed.
   * <p/>
   * If it's not compatible, configs from the current devices are tested.
   * <p/>
   * If none are compatible, it reverts to
   * {@link #findAndSetCompatibleConfig(boolean)}
   */
void adaptConfigSelection(boolean needBestMatch) {
    // check the device config (ie sans locale)
    // if still true, we need to find another config.
    boolean needConfigChange = true;
    boolean currentConfigIsCompatible = false;
    State selectedState = myConfiguration.getDeviceState();
    FolderConfiguration editedConfig = myConfiguration.getEditedConfig();
    Module module = myConfiguration.getModule();
    if (selectedState != null) {
        FolderConfiguration currentConfig = Configuration.getFolderConfig(module, selectedState, myConfiguration.getLocale(), myConfiguration.getTarget());
        if (currentConfig != null && editedConfig.isMatchFor(currentConfig)) {
            // current config is compatible
            currentConfigIsCompatible = true;
            if (!needBestMatch || isCurrentFileBestMatchFor(currentConfig)) {
                needConfigChange = false;
            }
        }
    }
    if (needConfigChange) {
        List<Locale> localeList = getPrioritizedLocales();
        // if the current state/locale isn't a correct match, then
        // look for another state/locale in the same device.
        FolderConfiguration testConfig = new FolderConfiguration();
        // first look in the current device.
        State matchState = null;
        int localeIndex = -1;
        Device device = myConfiguration.getDevice();
        IAndroidTarget target = myConfiguration.getTarget();
        if (device != null && target != null) {
            VersionQualifier versionQualifier = new VersionQualifier(target.getVersion().getFeatureLevel());
            mainloop: for (State state : device.getAllStates()) {
                testConfig.set(Configuration.getFolderConfig(module, state, myConfiguration.getLocale(), target));
                testConfig.setVersionQualifier(versionQualifier);
                // loop on the locales.
                for (int i = 0; i < localeList.size(); i++) {
                    Locale locale = localeList.get(i);
                    // update the test config with the locale qualifiers
                    testConfig.setLocaleQualifier(locale.qualifier);
                    if (editedConfig.isMatchFor(testConfig) && isCurrentFileBestMatchFor(testConfig)) {
                        matchState = state;
                        localeIndex = i;
                        break mainloop;
                    }
                }
            }
        }
        if (matchState != null) {
            myConfiguration.startBulkEditing();
            myConfiguration.setDeviceState(matchState);
            myConfiguration.setEffectiveDevice(device, matchState);
            myConfiguration.finishBulkEditing();
        } else {
            // no match in current device with any state/locale
            // attempt to find another device that can display this
            // particular state.
            findAndSetCompatibleConfig(currentConfigIsCompatible);
        }
    }
}
Also used : Locale(com.android.tools.idea.rendering.Locale) State(com.android.sdklib.devices.State) Device(com.android.sdklib.devices.Device) IAndroidTarget(com.android.sdklib.IAndroidTarget) Module(com.intellij.openapi.module.Module)

Example 18 with State

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

the class ConfigurationMatcher method getBetterMatch.

/**
   * Returns a different file which is a better match for the given device, orientation, target version, etc
   * than the current one. You can supply {@code null} for all parameters; in that case, the current value
   * in the configuration is used.
   */
@Nullable
public static VirtualFile getBetterMatch(@NotNull Configuration configuration, @Nullable Device device, @Nullable String stateName, @Nullable Locale locale, @Nullable IAndroidTarget target) {
    VirtualFile file = configuration.getFile();
    Module module = configuration.getModule();
    if (file != null && module != null) {
        if (device == null) {
            device = configuration.getDevice();
        }
        if (stateName == null) {
            State deviceState = configuration.getDeviceState();
            stateName = deviceState != null ? deviceState.getName() : null;
        }
        State selectedState = ConfigurationFileState.getState(device, stateName);
        if (selectedState == null) {
            // Invalid state name passed in for the current device
            return null;
        }
        if (locale == null) {
            locale = configuration.getLocale();
        }
        if (target == null) {
            target = configuration.getTarget();
        }
        FolderConfiguration currentConfig = Configuration.getFolderConfig(module, selectedState, locale, target);
        if (currentConfig != null) {
            LocalResourceRepository resources = AppResourceRepository.getAppResources(module, true);
            if (resources != null) {
                ResourceFolderType folderType = ResourceHelper.getFolderType(file);
                if (folderType != null) {
                    List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
                    if (!types.isEmpty()) {
                        ResourceType type = types.get(0);
                        List<VirtualFile> matches = resources.getMatchingFiles(file, type, currentConfig);
                        if (!matches.contains(file) && !matches.isEmpty()) {
                            return matches.get(0);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) State(com.android.sdklib.devices.State) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with State

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

the class ConfigurationFileState method saveState.

public void saveState(@NotNull Configuration configuration) {
    Device device = configuration.getDevice();
    myDeviceState = null;
    if (device != null) {
        State deviceState = configuration.getDeviceState();
        if (deviceState != null && deviceState != device.getDefaultState()) {
            myDeviceState = deviceState.getName();
        }
    }
    // Null out if same as the default
    UiMode dockMode = configuration.getUiMode();
    if (dockMode != UiMode.NORMAL) {
        myDockMode = dockMode.getResourceValue();
    } else {
        myDockMode = null;
    }
    myDockMode = StringUtil.nullize(dockMode.getResourceValue());
    NightMode nightMode = configuration.getNightMode();
    if (nightMode != NightMode.NOTNIGHT) {
        myNightMode = nightMode.getResourceValue();
    } else {
        myNightMode = null;
    }
    myTheme = StringUtil.nullize(configuration.getTheme());
}
Also used : Device(com.android.sdklib.devices.Device) State(com.android.sdklib.devices.State) NightMode(com.android.resources.NightMode) UiMode(com.android.resources.UiMode)

Example 20 with State

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

the class Configuration method getNextDeviceState.

/**
   * Get the next cyclical state after the given state
   *
   * @param from the state to start with
   * @return the following state following
   */
@Nullable
public State getNextDeviceState(@Nullable State from) {
    Device device = getDevice();
    if (device == null) {
        return null;
    }
    List<State> states = device.getAllStates();
    for (int i = 0; i < states.size(); i++) {
        if (states.get(i) == from) {
            return states.get((i + 1) % states.size());
        }
    }
    // Search by name instead
    if (from != null) {
        String name = from.getName();
        for (int i = 0; i < states.size(); i++) {
            if (states.get(i).getName().equals(name)) {
                return states.get((i + 1) % states.size());
            }
        }
    }
    return null;
}
Also used : Device(com.android.sdklib.devices.Device) State(com.android.sdklib.devices.State) Nullable(org.jetbrains.annotations.Nullable)

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