Search in sources :

Example 16 with AppResourceRepository

use of com.android.tools.idea.res.AppResourceRepository 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 17 with AppResourceRepository

use of com.android.tools.idea.res.AppResourceRepository in project android by JetBrains.

the class ResourceDrawablePanel method updateResolutionChain.

private void updateResolutionChain(@NotNull ResourceChooserItem item) {
    // Resource resolver
    myResolvedPanel.removeAll();
    ResourceValue resourceValue = item.getResourceValue();
    Configuration configuration = myDialog.getConfiguration();
    ResourceRepository frameworkResources = configuration.getFrameworkResources();
    if (frameworkResources != null) {
        AppResourceRepository appResources = AppResourceRepository.getAppResources(myDialog.geFacet(), true);
        ResourceItemResolver resolver = new ResourceItemResolver(configuration.getFullConfig(), frameworkResources, appResources, null);
        List<ResourceValue> lookupChain = Lists.newArrayList();
        lookupChain.add(resourceValue);
        resolver.setLookupChainList(lookupChain);
        resolver.resolveResValue(resourceValue);
        String prev = null;
        int indent = 0;
        if (lookupChain.size() >= 2) {
            for (ResourceValue element : lookupChain) {
                if (element == null) {
                    continue;
                }
                String value = element.getValue();
                if (value == null) {
                    continue;
                }
                String text = value;
                if (text.equals(prev)) {
                    continue;
                }
                // Strip paths
                if (!(text.startsWith(PREFIX_THEME_REF) || text.startsWith(PREFIX_RESOURCE_REF))) {
                    if (indent == 0) {
                        break;
                    }
                    int end = Math.max(text.lastIndexOf('/'), text.lastIndexOf('\\'));
                    if (end != -1) {
                        text = text.substring(end + 1);
                    }
                }
                if (indent > 0) {
                    // 21D2: Rightwards arrow
                    text = "⇒ " + text;
                }
                JBLabel label = new JBLabel(text);
                label.setBorder(IdeBorderFactory.createEmptyBorder(0, JBUI.scale(indent * 12), 0, 0));
                myResolvedPanel.add(label);
                indent++;
                prev = value;
            }
        }
    }
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) JBLabel(com.intellij.ui.components.JBLabel) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceItemResolver(com.android.ide.common.resources.ResourceItemResolver) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceRepository(com.android.ide.common.resources.ResourceRepository)

Aggregations

AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)17 NotNull (org.jetbrains.annotations.NotNull)5 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)4 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)3 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)3 ResourceType (com.android.resources.ResourceType)3 Module (com.intellij.openapi.module.Module)3 File (java.io.File)3 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)2 ResourceItem (com.android.ide.common.res2.ResourceItem)2 ResourceItem (com.android.ide.common.resources.ResourceItem)2 ResourceRepository (com.android.ide.common.resources.ResourceRepository)2 IAndroidTarget (com.android.sdklib.IAndroidTarget)2 ResourceClassRegistry (com.android.tools.idea.res.ResourceClassRegistry)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 XmlTag (com.intellij.psi.xml.XmlTag)2 VisibleForTesting (com.android.annotations.VisibleForTesting)1 HardwareConfigHelper (com.android.ide.common.rendering.HardwareConfigHelper)1