Search in sources :

Example 6 with LayoutLibrary

use of com.android.ide.common.rendering.LayoutLibrary in project android by JetBrains.

the class ModuleClassLoader method create.

@Nullable
public static ClassLoader create(IAndroidTarget target, Module module) throws Exception {
    AndroidPlatform androidPlatform = AndroidPlatform.getInstance(module);
    if (androidPlatform == null) {
        return null;
    }
    AndroidTargetData targetData = androidPlatform.getSdkData().getTargetData(target);
    LayoutLibrary library = targetData.getLayoutLibrary(module.getProject());
    if (library == null) {
        return null;
    }
    return get(library, module);
}
Also used : LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) AndroidTargetData(org.jetbrains.android.sdk.AndroidTargetData) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with LayoutLibrary

use of com.android.ide.common.rendering.LayoutLibrary 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)

Example 8 with LayoutLibrary

use of com.android.ide.common.rendering.LayoutLibrary in project android by JetBrains.

the class ConfigurationMenuAction method hasCapability.

private static boolean hasCapability(EditorDesignSurface context, int capability) {
    Configuration configuration = context.getConfiguration();
    if (configuration == null) {
        return false;
    }
    boolean enabled = false;
    Module module = configuration.getModule();
    if (module != null) {
        IAndroidTarget target = configuration.getTarget();
        if (target != null) {
            LayoutLibrary library = RenderService.getLayoutLibrary(module, target);
            enabled = library != null && library.supports(capability);
        }
    }
    return enabled;
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) IAndroidTarget(com.android.sdklib.IAndroidTarget) Module(com.intellij.openapi.module.Module)

Example 9 with LayoutLibrary

use of com.android.ide.common.rendering.LayoutLibrary in project android by JetBrains.

the class ModuleClassLoaderTest method testModuleClassLoading.

public void testModuleClassLoading() throws ClassNotFoundException, IOException {
    LayoutLibrary layoutLibrary = mock(LayoutLibrary.class);
    Module module = myFixture.getModule();
    File tmpDir = Files.createTempDir();
    File outputDir = new File(tmpDir, CompilerModuleExtension.PRODUCTION + "/" + module.getName() + "/test");
    assertTrue(FileUtil.createDirectory(outputDir));
    CompilerProjectExtension.getInstance(getProject()).setCompilerOutputUrl(pathToIdeaUrl(tmpDir));
    generateRClass("test", new File(outputDir, "R.class"));
    ApplicationManager.getApplication().runReadAction(() -> {
        ModuleClassLoader loader = ModuleClassLoader.get(layoutLibrary, module);
        try {
            Class<?> rClass = loader.loadClass("test.R");
            String value = (String) rClass.getDeclaredField("ID").get(null);
            assertEquals("FileID", value);
        } catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException e) {
            fail("Unexpected exception " + e.getLocalizedMessage());
        }
    });
}
Also used : LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) Module(com.intellij.openapi.module.Module) File(java.io.File)

Aggregations

LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)9 Module (com.intellij.openapi.module.Module)5 IAndroidTarget (com.android.sdklib.IAndroidTarget)4 Device (com.android.sdklib.devices.Device)3 RenderingException (com.android.tools.idea.layoutlib.RenderingException)3 File (java.io.File)3 IOException (java.io.IOException)3 AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)3 Nullable (org.jetbrains.annotations.Nullable)3 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)2 Project (com.intellij.openapi.project.Project)2 HardwareConfigHelper (com.android.ide.common.rendering.HardwareConfigHelper)1 LayoutLog (com.android.ide.common.rendering.api.LayoutLog)1 ResourceResolver (com.android.ide.common.resources.ResourceResolver)1 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)1 Bridge (com.android.layoutlib.bridge.Bridge)1 State (com.android.sdklib.devices.State)1 UnsupportedJavaRuntimeException (com.android.tools.idea.layoutlib.UnsupportedJavaRuntimeException)1 AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)1 Locale (com.android.tools.idea.rendering.Locale)1