private ResourceResolver getResourceResolver(Configuration configuration) { ResourceResolver resourceResolver = mResourceResolver.get(); if (resourceResolver != null) { return resourceResolver; } GraphicalEditorPart graphicalEditor = mCanvas.getEditorDelegate().getGraphicalEditor(); String theme = configuration.getTheme(); if (theme == null) { return null; } Map<ResourceType, Map<String, ResourceValue>> configuredFrameworkRes = null; Map<ResourceType, Map<String, ResourceValue>> configuredProjectRes = null; FolderConfiguration config = configuration.getFullConfig(); IAndroidTarget target = graphicalEditor.getRenderingTarget(); ResourceRepository frameworkRes = null; if (target != null) { Sdk sdk = Sdk.getCurrent(); if (sdk == null) { return null; } AndroidTargetData data = sdk.getTargetData(target); if (data != null) { // TODO: SHARE if possible frameworkRes = data.getFrameworkResources(); configuredFrameworkRes = frameworkRes.getConfiguredResources(config); } else { return null; } } else { return null; } assert configuredFrameworkRes != null; // get the resources of the file's project. ProjectResources projectRes = ResourceManager.getInstance().getProjectResources(graphicalEditor.getProject()); configuredProjectRes = projectRes.getConfiguredResources(config); if (!theme.startsWith(PREFIX_RESOURCE_REF)) { if (frameworkRes.hasResourceItem(ANDROID_STYLE_RESOURCE_PREFIX + theme)) { theme = ANDROID_STYLE_RESOURCE_PREFIX + theme; } else { theme = STYLE_RESOURCE_PREFIX + theme; } } resourceResolver = ResourceResolver.create( configuredProjectRes, configuredFrameworkRes, ResourceHelper.styleToTheme(theme), ResourceHelper.isProjectStyle(theme)); mResourceResolver = new SoftReference<ResourceResolver>(resourceResolver); return resourceResolver; }
/** Render immediately */ private void renderSync() { GraphicalEditorPart editor = mCanvas.getEditorDelegate().getGraphicalEditor(); if (editor.getReadyLayoutLib(false /*displayError*/) == null) { // Don't attempt to render when there is no ready layout library: most likely // the targets are loading/reloading. return; } disposeThumbnail(); Configuration configuration = mAlternateInput != null && mAlternateConfiguration != null ? mAlternateConfiguration : mConfiguration; ResourceResolver resolver = getResourceResolver(configuration); RenderService renderService = RenderService.create(editor, configuration, resolver); if (mIncludedWithin != null) { renderService.setIncludedWithin(mIncludedWithin); } if (mAlternateInput != null) { IAndroidTarget target = editor.getRenderingTarget(); AndroidTargetData data = null; if (target != null) { Sdk sdk = Sdk.getCurrent(); if (sdk != null) { data = sdk.getTargetData(target); } } // Construct UI model from XML DocumentDescriptor documentDescriptor; if (data == null) { documentDescriptor = new DocumentDescriptor("temp", null); // $NON-NLS-1$ } else { documentDescriptor = data.getLayoutDescriptors().getDescriptor(); } UiDocumentNode model = (UiDocumentNode) documentDescriptor.createUiNode(); model.setEditor(mCanvas.getEditorDelegate().getEditor()); model.setUnknownDescriptorProvider(editor.getModel().getUnknownDescriptorProvider()); Document document = DomUtilities.getDocument(mAlternateInput); if (document == null) { mError = "No document"; createErrorThumbnail(); return; } model.loadFromXmlNode(document); renderService.setModel(model); } else { renderService.setModel(editor.getModel()); } RenderLogger log = new RenderLogger(getDisplayName()); renderService.setLog(log); RenderSession session = renderService.createRenderSession(); Result render = session.render(1000); if (DUMP_RENDER_DIAGNOSTICS) { if (log.hasProblems() || !render.isSuccess()) { AdtPlugin.log( IStatus.ERROR, "Found problems rendering preview " + getDisplayName() + ": " + render.getErrorMessage() + " : " + log.getProblems(false)); Throwable exception = render.getException(); if (exception != null) { AdtPlugin.log(exception, "Failure rendering preview " + getDisplayName()); } } } if (render.isSuccess()) { mError = null; } else { mError = render.getErrorMessage(); if (mError == null) { mError = ""; } } if (render.getStatus() == Status.ERROR_TIMEOUT) { // TODO: Special handling? schedule update again later return; } if (render.isSuccess()) { BufferedImage image = session.getImage(); if (image != null) { createThumbnail(image); } } if (mError != null) { createErrorThumbnail(); } }