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; }
/** Initialize the bridge and the resource maps. */ @Before public void setUp() { File data_dir = new File(PLATFORM_DIR, "data"); File res = new File(data_dir, "res"); mFrameworkRepo = new FrameworkResources(new FolderWrapper(res)); mFrameworkRepo.loadResources(); mFrameworkRepo.loadPublicResources(getLogger()); mProjectResources = new ResourceRepository(new FolderWrapper(TEST_RES_DIR + APP_TEST_RES), false) { @NonNull @Override protected ResourceItem createResourceItem(String name) { return new ResourceItem(name); } }; mProjectResources.loadResources(); File fontLocation = new File(data_dir, "fonts"); File buildProp = new File(PLATFORM_DIR, "build.prop"); File attrs = new File(res, "values" + File.separator + "attrs.xml"); mBridge = new Bridge(); mBridge.init( ConfigGenerator.loadProperties(buildProp), fontLocation, ConfigGenerator.getEnumMap(attrs), getLayoutLog()); }
/** Uses Theme.Material and Target sdk version as 21. */ private SessionParams getSessionParams( LayoutPullParser layoutParser, ConfigGenerator configGenerator, LayoutLibTestCallback layoutLibCallback) { FolderConfiguration config = configGenerator.getFolderConfig(); ResourceResolver resourceResolver = ResourceResolver.create( mProjectResources.getConfiguredResources(config), mFrameworkRepo.getConfiguredResources(config), "Theme.Material", false); return new SessionParams( layoutParser, RenderingMode.NORMAL, null /*used for caching*/, configGenerator.getHardwareConfig(), resourceResolver, layoutLibCallback, 0, 21, // TODO: Make it more configurable to run tests for various versions. getLayoutLog()); }