Search in sources :

Example 1 with State

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

the class Configuration method setDevice.

/**
   * Sets the device
   *
   * @param device        the device
   * @param preserveState if true, attempt to preserve the state associated with the config
   */
public void setDevice(Device device, boolean preserveState) {
    if (mySpecificDevice != device) {
        Device prevDevice = mySpecificDevice;
        State prevState = myState;
        myDevice = mySpecificDevice = device;
        int updateFlags = CFG_DEVICE;
        if (device != null) {
            State state = null;
            // Attempt to preserve the device state?
            if (preserveState && prevDevice != null) {
                if (prevState != null) {
                    FolderConfiguration oldConfig = DeviceConfigHelper.getFolderConfig(prevState);
                    if (oldConfig != null) {
                        String stateName = getClosestMatch(oldConfig, device.getAllStates());
                        state = device.getState(stateName);
                    } else {
                        state = device.getState(prevState.getName());
                    }
                }
            } else if (preserveState && myStateName != null) {
                state = device.getState(myStateName);
            }
            if (state == null) {
                state = device.getDefaultState();
            }
            if (myState != state) {
                setDeviceStateName(state.getName());
                myState = state;
                updateFlags |= CFG_DEVICE_STATE;
            }
        }
        // TODO: Is this redundant with the stuff above?
        if (mySpecificDevice != null && myState == null) {
            setDeviceStateName(mySpecificDevice.getDefaultState().getName());
            myState = mySpecificDevice.getDefaultState();
            updateFlags |= CFG_DEVICE_STATE;
        }
        updated(updateFlags);
    }
}
Also used : Device(com.android.sdklib.devices.Device) State(com.android.sdklib.devices.State)

Example 2 with State

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

the class Configuration method syncFolderConfig.

/**
   * Updates the folder configuration such that it reflects changes in
   * configuration state such as the device orientation, the UI mode, the
   * rendering target, etc.
   */
protected void syncFolderConfig() {
    Device device = getDevice();
    if (device == null) {
        return;
    }
    // get the device config from the device/state combos.
    State deviceState = getDeviceState();
    if (deviceState == null) {
        deviceState = device.getDefaultState();
    }
    FolderConfiguration config = getFolderConfig(getModule(), deviceState, getLocale(), getTarget());
    // replace the config with the one from the device
    myFullConfig.set(config);
    // sync the selected locale
    Locale locale = getLocale();
    myFullConfig.setLocaleQualifier(locale.qualifier);
    if (myEditedConfig.getLayoutDirectionQualifier() != null) {
        myFullConfig.setLayoutDirectionQualifier(myEditedConfig.getLayoutDirectionQualifier());
    } else if (!locale.hasLanguage()) {
        // Avoid getting the layout library if the locale doesn't have any language.
        myFullConfig.setLayoutDirectionQualifier(new LayoutDirectionQualifier(LayoutDirection.LTR));
    } else {
        LayoutLibrary layoutLib = RenderService.getLayoutLibrary(getModule(), getTarget());
        if (layoutLib != null) {
            if (layoutLib.isRtl(locale.toLocaleId())) {
                myFullConfig.setLayoutDirectionQualifier(new LayoutDirectionQualifier(LayoutDirection.RTL));
            } else {
                myFullConfig.setLayoutDirectionQualifier(new LayoutDirectionQualifier(LayoutDirection.LTR));
            }
        }
    }
    // Replace the UiMode with the selected one, if one is selected
    UiMode uiMode = getUiMode();
    myFullConfig.setUiModeQualifier(new UiModeQualifier(uiMode));
    // Replace the NightMode with the selected one, if one is selected
    NightMode nightMode = getNightMode();
    myFullConfig.setNightModeQualifier(new NightModeQualifier(nightMode));
    // replace the API level by the selection of the combo
    IAndroidTarget target = getTarget();
    if (target != null) {
        int apiLevel = target.getVersion().getFeatureLevel();
        myFullConfig.setVersionQualifier(new VersionQualifier(apiLevel));
    }
    myFolderConfigDirty = 0;
    myProjectStateVersion = myManager.getStateVersion();
}
Also used : Locale(com.android.tools.idea.rendering.Locale) LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) Device(com.android.sdklib.devices.Device) IAndroidTarget(com.android.sdklib.IAndroidTarget) State(com.android.sdklib.devices.State)

Example 3 with State

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

the class OrientationMenuAction method handleIconClicked.

@Override
protected boolean handleIconClicked() {
    Configuration configuration = myRenderContext.getConfiguration();
    if (configuration == null) {
        return false;
    }
    State current = configuration.getDeviceState();
    State flip = configuration.getNextDeviceState(current);
    if (flip != null) {
        SetDeviceStateAction action = new SetDeviceStateAction(myRenderContext, flip.getName(), flip, false, false);
        action.perform();
    }
    return true;
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) State(com.android.sdklib.devices.State)

Example 4 with State

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

the class ConfigurationMatcher method findAndSetCompatibleConfig.

/**
   * Finds a device/config that can display a configuration.
   * <p/>
   * Once found the device and config combos are set to the config.
   * <p/>
   * If there is no compatible configuration, a custom one is created.
   *
   * @param favorCurrentConfig if true, and no best match is found, don't
   *                           change the current config. This must only be true if the
   *                           current config is compatible.
   */
void findAndSetCompatibleConfig(boolean favorCurrentConfig) {
    List<Locale> localeList = getPrioritizedLocales();
    List<Device> deviceList = myManager.getDevices();
    FolderConfiguration editedConfig = myConfiguration.getEditedConfig();
    FolderConfiguration currentConfig = myConfiguration.getFullConfig();
    // list of compatible device/state/locale
    List<ConfigMatch> anyMatches = new ArrayList<ConfigMatch>();
    // list of actual best match (ie the file is a best match for the
    // device/state)
    List<ConfigMatch> bestMatches = new ArrayList<ConfigMatch>();
    // get a locale that matches the host locale roughly (may not be exact match on the region.)
    int localeHostMatch = getLocaleMatch();
    // build a list of combinations of non standard qualifiers to add to each device's
    // qualifier set when testing for a match.
    // These qualifiers are: locale, night-mode, car dock.
    List<ConfigBundle> configBundles = new ArrayList<ConfigBundle>(200);
    // If the edited file has locales, then we have to select a matching locale from
    // the list.
    // However, if it doesn't, we don't randomly take the first locale, we take one
    // matching the current host locale (making sure it actually exist in the project)
    int start, max;
    if (editedConfig.getLocaleQualifier() != null || localeHostMatch == -1) {
        // add all the locales
        start = 0;
        max = localeList.size();
    } else {
        // only add the locale host match
        start = localeHostMatch;
        // test is <
        max = localeHostMatch + 1;
    }
    for (int i = start; i < max; i++) {
        Locale l = localeList.get(i);
        ConfigBundle bundle = new ConfigBundle();
        bundle.config.setLocaleQualifier(l.qualifier);
        bundle.localeIndex = i;
        configBundles.add(bundle);
    }
    // add the dock mode to the bundle combinations.
    addDockModeToBundles(configBundles);
    // add the night mode to the bundle combinations.
    addNightModeToBundles(configBundles);
    addRenderTargetToBundles(configBundles);
    Locale currentLocale = myConfiguration.getLocale();
    IAndroidTarget currentTarget = myConfiguration.getTarget();
    Module module = myConfiguration.getModule();
    for (Device device : deviceList) {
        for (State state : device.getAllStates()) {
            // loop on the list of config bundles to create full
            // configurations.
            FolderConfiguration stateConfig = Configuration.getFolderConfig(module, state, currentLocale, currentTarget);
            for (ConfigBundle bundle : configBundles) {
                // create a new config with device config
                FolderConfiguration testConfig = new FolderConfiguration();
                testConfig.set(stateConfig);
                // add on top of it, the extra qualifiers from the bundle
                testConfig.add(bundle.config);
                if (editedConfig.isMatchFor(testConfig)) {
                    // this is a basic match. record it in case we don't
                    // find a match
                    // where the edited file is a best config.
                    anyMatches.add(new ConfigMatch(testConfig, device, state, bundle));
                    if (isCurrentFileBestMatchFor(testConfig)) {
                        // this is what we want.
                        bestMatches.add(new ConfigMatch(testConfig, device, state, bundle));
                    }
                }
            }
        }
    }
    if (bestMatches.size() == 0) {
        if (favorCurrentConfig) {
            // quick check
            if (!editedConfig.isMatchFor(currentConfig)) {
                LOG.warn("favorCurrentConfig can only be true if the current config is compatible");
            }
            // just display the warning
            LOG.warn(String.format("'%1$s' is not a best match for any device/locale combination for %2$s.\n" + "Displaying it with '%3$s'.", editedConfig.toDisplayString(), myConfiguration.getFile(), currentConfig.toDisplayString()));
        } else if (anyMatches.size() > 0) {
            // select the best device anyway.
            ConfigMatch match = selectConfigMatch(anyMatches);
            myConfiguration.startBulkEditing();
            myConfiguration.setEffectiveDevice(match.device, match.state);
            myConfiguration.setUiMode(UiMode.getByIndex(match.bundle.dockModeIndex));
            myConfiguration.setNightMode(NightMode.getByIndex(match.bundle.nightModeIndex));
            myConfiguration.finishBulkEditing();
            // TODO: display a better warning!
            LOG.warn(String.format("'%1$s' is not a best match for any device/locale combination for %2$s.\n" + "Displaying it with\n" + "  %3$s\n" + "which is compatible, but will actually be displayed with " + "another more specific version of the layout.", editedConfig.toDisplayString(), myConfiguration.getFile(), currentConfig.toDisplayString()));
        } else {
        // TODO: there is no device/config able to display the layout, create one.
        // For the base config values, we'll take the first device and state,
        // and replace whatever qualifier required by the layout file.
        }
    } else {
        ConfigMatch match = selectConfigMatch(bestMatches);
        myConfiguration.startBulkEditing();
        myConfiguration.setEffectiveDevice(match.device, match.state);
        myConfiguration.setUiMode(UiMode.getByIndex(match.bundle.dockModeIndex));
        myConfiguration.setNightMode(NightMode.getByIndex(match.bundle.nightModeIndex));
        myConfiguration.finishBulkEditing();
    }
}
Also used : Locale(com.android.tools.idea.rendering.Locale) Device(com.android.sdklib.devices.Device) IAndroidTarget(com.android.sdklib.IAndroidTarget) State(com.android.sdklib.devices.State) Module(com.intellij.openapi.module.Module)

Example 5 with State

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

the class Configuration method getClosestMatch.

/**
   * Attempts to find a close state among a list
   *
   * @param oldConfig the reference config.
   * @param states    the list of states to search through
   * @return the name of the closest state match, or possibly null if no states are compatible
   *         (this can only happen if the states don't have a single qualifier that is the same).
   */
@Nullable
private static String getClosestMatch(@NotNull FolderConfiguration oldConfig, @NotNull List<State> states) {
    // create 2 lists as we're going to go through one and put the
    // candidates in the other.
    List<State> list1 = new ArrayList<>(states.size());
    List<State> list2 = new ArrayList<>(states.size());
    list1.addAll(states);
    final int count = FolderConfiguration.getQualifierCount();
    for (int i = 0; i < count; i++) {
        // the same i-th qualifier as the old state
        for (State s : list1) {
            ResourceQualifier oldQualifier = oldConfig.getQualifier(i);
            FolderConfiguration folderConfig = DeviceConfigHelper.getFolderConfig(s);
            ResourceQualifier newQualifier = folderConfig != null ? folderConfig.getQualifier(i) : null;
            if (oldQualifier == null) {
                if (newQualifier == null) {
                    list2.add(s);
                }
            } else if (oldQualifier.equals(newQualifier)) {
                list2.add(s);
            }
        }
        // is returned.
        if (list2.size() == 1) {
            return list2.get(0).getName();
        }
        // all new states it is simply ignored.
        if (list2.size() != 0) {
            // move the candidates back into list1.
            list1.clear();
            list1.addAll(list2);
            list2.clear();
        }
    }
    // we take the first one).
    if (list1.size() > 0) {
        return list1.get(0).getName();
    }
    return null;
}
Also used : State(com.android.sdklib.devices.State) ArrayList(java.util.ArrayList) 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