Search in sources :

Example 71 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class NlModel method inflate.

/**
   * Synchronously inflates the model and updates the view hierarchy
   *
   * @param force forces the model to be re-inflated even if a previous version was already inflated
   * @returns whether the model was inflated in this call or not
   */
private boolean inflate(boolean force) {
    Configuration configuration = myConfiguration;
    if (configuration == null) {
        return false;
    }
    ResourceNotificationManager resourceNotificationManager = ResourceNotificationManager.getInstance(myFile.getProject());
    // Some types of files must be saved to disk first, because layoutlib doesn't
    // delegate XML parsers for non-layout files (meaning layoutlib will read the
    // disk contents, so we have to push any edits to disk before rendering)
    LayoutPullParserFactory.saveFileIfNecessary(myFile);
    RenderResult result = null;
    synchronized (RENDERING_LOCK) {
        if (myRenderTask != null && !force) {
            // No need to inflate
            return false;
        }
        // Record the current version we're rendering from; we'll use that in #activate to make sure we're picking up any
        // external changes
        myRenderedVersion = resourceNotificationManager.getCurrentVersion(myFacet, myFile, myConfiguration);
        RenderService renderService = RenderService.get(myFacet);
        RenderLogger logger = renderService.createLogger();
        if (myRenderTask != null) {
            myRenderTask.dispose();
        }
        myRenderTask = renderService.createTask(myFile, configuration, logger, mySurface);
        setupRenderTask(myRenderTask);
        if (myRenderTask != null) {
            if (!isRenderViewPort()) {
                myRenderTask.useDesignMode(myFile);
            }
            result = myRenderTask.inflate();
            if (result == null || !result.getRenderResult().isSuccess()) {
                myRenderTask.dispose();
                myRenderTask = null;
                if (result == null) {
                    result = RenderResult.createBlank(myFile);
                }
            }
        }
        updateHierarchy(result);
        myRenderResultLock.writeLock().lock();
        try {
            myRenderResult = result;
        } finally {
            myRenderResultLock.writeLock().unlock();
        }
        return myRenderTask != null;
    }
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) ResourceNotificationManager(com.android.tools.idea.res.ResourceNotificationManager)

Example 72 with Configuration

use of com.android.tools.idea.configurations.Configuration 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 73 with Configuration

use of com.android.tools.idea.configurations.Configuration 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 74 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class NlPreviewImagePanel method getBackgroundColor.

@NotNull
private Color getBackgroundColor() {
    Configuration configuration = myDesignSurface != null ? myDesignSurface.getConfiguration() : null;
    ResourceResolver resolver = configuration != null ? configuration.getResourceResolver() : null;
    if (resolver == null) {
        return UIUtil.getPanelBackground();
    }
    ResourceValue windowBackground = resolver.findItemInTheme("colorBackground", true);
    Color background = ResourceHelper.resolveColor(resolver, windowBackground, myDesignSurface.getProject());
    return background != null ? background : UIUtil.getPanelBackground();
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) NotNull(org.jetbrains.annotations.NotNull)

Example 75 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class NlProperties method currentActivityIfFoundIsDerivedFromAppCompatActivity.

private static boolean currentActivityIfFoundIsDerivedFromAppCompatActivity(@NotNull List<NlComponent> components) {
    assert !components.isEmpty();
    NlModel model = components.get(0).getModel();
    Configuration configuration = model.getConfiguration();
    String activityClassName = configuration.getActivity();
    if (activityClassName == null) {
        // Assume we are since this is how the default activities are created.
        return true;
    }
    if (activityClassName.startsWith(".")) {
        MergedManifest manifest = MergedManifest.get(model.getModule());
        String pkg = StringUtil.notNullize(manifest.getPackage());
        activityClassName = pkg + activityClassName;
    }
    JavaPsiFacade facade = JavaPsiFacade.getInstance(model.getProject());
    PsiClass activityClass = facade.findClass(activityClassName, model.getModule().getModuleScope());
    while (activityClass != null && !CLASS_APP_COMPAT_ACTIVITY.equals(activityClass.getQualifiedName())) {
        activityClass = activityClass.getSuperClass();
    }
    return activityClass != null;
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) Configuration(com.android.tools.idea.configurations.Configuration) MergedManifest(com.android.tools.idea.model.MergedManifest) PsiClass(com.intellij.psi.PsiClass) NlModel(com.android.tools.idea.uibuilder.model.NlModel)

Aggregations

Configuration (com.android.tools.idea.configurations.Configuration)95 VirtualFile (com.intellij.openapi.vfs.VirtualFile)38 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)23 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)21 ResourceResolver (com.android.ide.common.resources.ResourceResolver)16 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)14 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 Module (com.intellij.openapi.module.Module)9 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)8 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 Device (com.android.sdklib.devices.Device)7 State (com.android.sdklib.devices.State)7 NotNull (org.jetbrains.annotations.NotNull)7 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)6 EditedStyleItem (com.android.tools.idea.editors.theme.datamodels.EditedStyleItem)5 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)5 NlModel (com.android.tools.idea.uibuilder.model.NlModel)4 DesignSurface (com.android.tools.idea.uibuilder.surface.DesignSurface)4 PsiFile (com.intellij.psi.PsiFile)4 ResourceFolderType (com.android.resources.ResourceFolderType)3