Search in sources :

Example 1 with RenderingException

use of com.android.tools.idea.layoutlib.RenderingException in project android by JetBrains.

the class RenderService method supportsCapability.

public static boolean supportsCapability(@NotNull final Module module, @NotNull IAndroidTarget target, @MagicConstant(flagsFromClass = Features.class) int capability) {
    Project project = module.getProject();
    AndroidPlatform platform = AndroidPlatform.getInstance(module);
    if (platform != null) {
        try {
            LayoutLibrary library = platform.getSdkData().getTargetData(target).getLayoutLibrary(project);
            if (library != null) {
                return library.supports(capability);
            }
        } catch (RenderingException | IOException e) {
        // Ignore: if service can't be found, that capability isn't available
        }
    }
    return false;
}
Also used : Project(com.intellij.openapi.project.Project) LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) RenderingException(com.android.tools.idea.layoutlib.RenderingException) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) IOException(java.io.IOException)

Example 2 with RenderingException

use of com.android.tools.idea.layoutlib.RenderingException in project android by JetBrains.

the class RenderService method getLayoutLibrary.

@Nullable
public static LayoutLibrary getLayoutLibrary(@Nullable final Module module, @Nullable IAndroidTarget target) {
    if (module == null || target == null) {
        return null;
    }
    Project project = module.getProject();
    AndroidPlatform platform = AndroidPlatform.getInstance(module);
    if (platform != null) {
        try {
            return platform.getSdkData().getTargetData(target).getLayoutLibrary(project);
        } catch (RenderingException | IOException e) {
        // Ignore.
        }
    }
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) RenderingException(com.android.tools.idea.layoutlib.RenderingException) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with RenderingException

use of com.android.tools.idea.layoutlib.RenderingException in project android by JetBrains.

the class RenderService method createTask.

/**
   * Creates a new {@link RenderService} associated with the given editor.
   *
   * @return a {@link RenderService} which can perform rendering services
   */
@Nullable
public RenderTask createTask(@Nullable final PsiFile psiFile, @NotNull final Configuration configuration, @NotNull final RenderLogger logger, @Nullable final EditorDesignSurface surface) {
    Module module = myFacet.getModule();
    final Project project = module.getProject();
    AndroidPlatform platform = getPlatform(module, logger);
    if (platform == null) {
        return null;
    }
    IAndroidTarget target = configuration.getTarget();
    if (target == null) {
        logger.addMessage(RenderProblem.createPlain(ERROR, "No render target was chosen"));
        return null;
    }
    warnIfObsoleteLayoutLib(module, logger, surface, target);
    LayoutLibrary layoutLib;
    try {
        layoutLib = platform.getSdkData().getTargetData(target).getLayoutLibrary(project);
        if (layoutLib == null) {
            String message = AndroidBundle.message("android.layout.preview.cannot.load.library.error");
            logger.addMessage(RenderProblem.createPlain(ERROR, message));
            return null;
        }
    } catch (UnsupportedJavaRuntimeException e) {
        RenderProblem.Html javaVersionProblem = RenderProblem.create(ERROR);
        javaVersionProblem.getHtmlBuilder().add(e.getPresentableMessage()).newline().addLink("Install a supported JDK", JDK_INSTALL_URL);
        logger.addMessage(javaVersionProblem);
        return null;
    } catch (RenderingException e) {
        String message = e.getPresentableMessage();
        message = message != null ? message : AndroidBundle.message("android.layout.preview.default.error.message");
        logger.addMessage(RenderProblem.createPlain(ERROR, message, module.getProject(), logger.getLinkManager(), e));
        return null;
    } catch (IOException e) {
        final String message = e.getMessage();
        logger.error(null, "I/O error: " + (message != null ? ": " + message : ""), e);
        return null;
    }
    if (psiFile != null && TAG_PREFERENCE_SCREEN.equals(AndroidPsiUtils.getRootTagName(psiFile)) && !layoutLib.supports(Features.PREFERENCES_RENDERING)) {
        // This means that user is using an outdated version of layoutlib. A warning to update has already been
        // presented in warnIfObsoleteLayoutLib(). Just log a plain message asking users to update.
        logger.addMessage(RenderProblem.createPlain(ERROR, "This version of the rendering library does not support rendering Preferences. " + "Update it using the SDK Manager"));
        return null;
    }
    Device device = configuration.getDevice();
    if (device == null) {
        logger.addMessage(RenderProblem.createPlain(ERROR, "No device selected"));
        return null;
    }
    try {
        RenderTask task = new RenderTask(this, configuration, logger, layoutLib, device, myCredential, CrashReporter.getInstance());
        if (psiFile != null) {
            task.setPsiFile(psiFile);
        }
        task.setDesignSurface(surface);
        return task;
    } catch (IncorrectOperationException | AssertionError e) {
        // We can get this exception if the module/project is closed while we are updating the model. Ignore it.
        assert myFacet.isDisposed();
    }
    return null;
}
Also used : LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) Device(com.android.sdklib.devices.Device) UnsupportedJavaRuntimeException(com.android.tools.idea.layoutlib.UnsupportedJavaRuntimeException) RenderingException(com.android.tools.idea.layoutlib.RenderingException) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) IAndroidTarget(com.android.sdklib.IAndroidTarget) IOException(java.io.IOException) Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with RenderingException

use of com.android.tools.idea.layoutlib.RenderingException in project android by JetBrains.

the class GraphicsLayoutRenderer method create.

@VisibleForTesting
@NotNull
static GraphicsLayoutRenderer create(@NotNull AndroidFacet facet, @NotNull AndroidPlatform platform, @NotNull Project project, @NotNull Configuration configuration, @NotNull ILayoutPullParser parser, @Nullable Color backgroundColor, @NotNull SessionParams.RenderingMode renderingMode, boolean useSecurityManager) throws InitializationException {
    AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(facet);
    LayoutLibrary layoutLib;
    try {
        IAndroidTarget latestTarget = configuration.getConfigurationManager().getHighestApiTarget();
        if (latestTarget == null) {
            throw new UnsupportedLayoutlibException("GraphicsLayoutRenderer requires at least layoutlib version " + MIN_LAYOUTLIB_API_VERSION);
        }
        layoutLib = platform.getSdkData().getTargetData(latestTarget).getLayoutLibrary(project);
        if (layoutLib == null) {
            throw new InitializationException("getLayoutLibrary() returned null");
        }
    } catch (RenderingException e) {
        throw new InitializationException(e);
    } catch (IOException e) {
        throw new InitializationException(e);
    }
    if (layoutLib.getApiLevel() < MIN_LAYOUTLIB_API_VERSION) {
        throw new UnsupportedLayoutlibException("GraphicsLayoutRenderer requires at least layoutlib version " + MIN_LAYOUTLIB_API_VERSION);
    }
    AppResourceRepository appResources = AppResourceRepository.getAppResources(facet, true);
    final Module module = facet.getModule();
    // Security token used to disable the security manager. Only objects that have a reference to it are allowed to disable it.
    Object credential = new Object();
    RenderLogger logger = new RenderLogger("theme_editor", module, credential);
    final ActionBarCallback actionBarCallback = new ActionBarCallback();
    // TODO: Remove LayoutlibCallback dependency.
    //noinspection ConstantConditions
    final LayoutlibCallbackImpl layoutlibCallback = new LayoutlibCallbackImpl(null, layoutLib, appResources, module, facet, logger, credential, null) {

        @Override
        public ActionBarCallback getActionBarCallback() {
            return actionBarCallback;
        }
    };
    // Load the local project R identifiers.
    boolean loadRResult = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

        @Override
        public Boolean compute() {
            // half way.
            if (module.isDisposed()) {
                return false;
            }
            layoutlibCallback.loadAndParseRClass();
            return true;
        }
    });
    if (!loadRResult) {
        throw new AlreadyDisposedException("Module was already disposed");
    }
    IAndroidTarget target = configuration.getTarget();
    if (target == null) {
        throw new InitializationException("Unable to get IAndroidTarget");
    }
    Device device = configuration.getDevice();
    assert device != null;
    HardwareConfigHelper hardwareConfigHelper = new HardwareConfigHelper(device);
    DynamicHardwareConfig hardwareConfig = new DynamicHardwareConfig(hardwareConfigHelper.getConfig());
    List<ResourceValue> resourceLookupChain = new ArrayList<ResourceValue>();
    ResourceResolver resourceResolver = ResourceResolver.copy(configuration.getResourceResolver());
    assert resourceResolver != null;
    // Create a resource resolver that will save the lookups on the passed List<>
    ResourceResolver recordingResourceResolver = resourceResolver.createRecorder(resourceLookupChain);
    final SessionParams params = new SessionParams(parser, renderingMode, module, hardwareConfig, recordingResourceResolver, layoutlibCallback, moduleInfo.getMinSdkVersion().getApiLevel(), moduleInfo.getTargetSdkVersion().getApiLevel(), logger, target instanceof CompatibilityRenderTarget ? target.getVersion().getApiLevel() : 0);
    params.setForceNoDecor();
    params.setAssetRepository(new AssetRepositoryImpl(facet));
    // The App Label needs to be not null
    params.setAppLabel("");
    if (backgroundColor != null) {
        params.setOverrideBgColor(backgroundColor.getRGB());
    }
    RenderSecurityManager mySecurityManager = useSecurityManager ? RenderSecurityManagerFactory.create(module, platform) : null;
    return new GraphicsLayoutRenderer(layoutLib, params, mySecurityManager, hardwareConfig, resourceLookupChain, credential);
}
Also used : LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) AssetRepositoryImpl(com.android.tools.idea.res.AssetRepositoryImpl) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) HardwareConfigHelper(com.android.ide.common.rendering.HardwareConfigHelper) Device(com.android.sdklib.devices.Device) RenderingException(com.android.tools.idea.layoutlib.RenderingException) IAndroidTarget(com.android.sdklib.IAndroidTarget) IOException(java.io.IOException) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) ResourceResolver(com.android.ide.common.resources.ResourceResolver) Module(com.intellij.openapi.module.Module) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

RenderingException (com.android.tools.idea.layoutlib.RenderingException)4 IOException (java.io.IOException)4 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)3 Project (com.intellij.openapi.project.Project)3 AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)3 IAndroidTarget (com.android.sdklib.IAndroidTarget)2 Device (com.android.sdklib.devices.Device)2 Module (com.intellij.openapi.module.Module)2 Nullable (org.jetbrains.annotations.Nullable)2 HardwareConfigHelper (com.android.ide.common.rendering.HardwareConfigHelper)1 ResourceResolver (com.android.ide.common.resources.ResourceResolver)1 UnsupportedJavaRuntimeException (com.android.tools.idea.layoutlib.UnsupportedJavaRuntimeException)1 AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)1 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 AssetRepositoryImpl (com.android.tools.idea.res.AssetRepositoryImpl)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NotNull (org.jetbrains.annotations.NotNull)1