use of com.android.ide.common.rendering.api.ViewInfo in project android by JetBrains.
the class AndroidPreviewPanelTest method testSimpleRender.
public void testSimpleRender() throws ParserConfigurationException, IOException, SAXException, InterruptedException {
VirtualFile layout = myFixture.copyFileToProject("themeEditor/theme_preview_layout.xml", "res/layout/theme_preview_layout.xml");
Configuration configuration = myFacet.getConfigurationManager().getConfiguration(layout);
AtomicBoolean executorCalled = new AtomicBoolean(false);
ExecutorService threadPool = Executors.newFixedThreadPool(1);
Executor executor = (r) -> {
// Run in a separate thread and wait for the result
try {
threadPool.submit(r).get(60, TimeUnit.SECONDS);
executorCalled.set(true);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new AssertionError("Unexpected exception", e);
}
};
AndroidPreviewPanel.GraphicsLayoutRendererFactory graphicsFactory = (configuration1, parser, background) -> {
Module module = configuration.getModule();
AndroidFacet facet = AndroidFacet.getInstance(configuration.getModule());
assertNotNull(facet);
AndroidPlatform platform = AndroidPlatform.getInstance(module);
assertNotNull(platform);
return GraphicsLayoutRenderer.create(facet, platform, getProject(), configuration, parser, background, SessionParams.RenderingMode.V_SCROLL, false);
};
AndroidPreviewPanel panel = new AndroidPreviewPanel(configuration, executor, graphicsFactory) {
@Override
public void invalidateGraphicsRenderer() {
myInvalidateRunnable.run();
}
};
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader("<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"" + " android:layout_width=\"match_parent\"" + " android:layout_height=\"match_parent\"" + " android:background=\"#0F0\">" + " <TextView" + " android:layout_width=\"wrap_content\"" + " android:layout_height=\"wrap_content\"" + " android:text=\"Hello world\"" + " android:background=\"#F00\"/>" + "</LinearLayout>")));
panel.setDocument(document);
panel.setBounds(0, 0, 400, 100);
//noinspection UndesirableClassUsage (we don't want to use HDPI images here)
BufferedImage output = new BufferedImage(400, 100, BufferedImage.TYPE_INT_ARGB);
panel.paintComponent(output.getGraphics());
// Make sure that the executor got called since we were trying to render on the UI thread.
assertTrue(executorCalled.get());
BufferedImage goldenImage = ImageIO.read(new File(getTestDataPath() + "/themeEditor/previewPanel/golden.png"));
assertImageSimilar("layout", goldenImage, output, 3);
ViewInfo textView = panel.findViewAtPoint(new Point(0, 0));
assertEquals("android.widget.TextView", textView.getClassName());
threadPool.shutdownNow();
threadPool.awaitTermination(30, TimeUnit.SECONDS);
Disposer.dispose(panel);
}
use of com.android.ide.common.rendering.api.ViewInfo in project android by JetBrains.
the class PreferenceGroupDragHandler method initDividerHeight.
private void initDividerHeight() {
ViewInfo view = ViewInfoUtils.findListView(editor.getRootViews());
assert view != null;
myDividerHeight = ((ListView) view.getViewObject()).getDividerHeight();
}
use of com.android.ide.common.rendering.api.ViewInfo in project android_frameworks_base by crdroidandroid.
the class RenderSessionImpl method visit.
/**
* Visits a {@link View} and its children and generate a {@link ViewInfo} containing the
* bounds of all the views.
*
* @param view the root View
* @param offset an offset for the view bounds.
* @param setExtendedInfo whether to set the extended view info in the {@link ViewInfo} object.
* @param isContentFrame {@code true} if the {@code ViewInfo} to be created is part of the
* content frame.
*
* @return {@code ViewInfo} containing the bounds of the view and it children otherwise.
*/
private ViewInfo visit(View view, int offset, boolean setExtendedInfo, boolean isContentFrame) {
ViewInfo result = createViewInfo(view, offset, setExtendedInfo, isContentFrame);
if (view instanceof ViewGroup) {
ViewGroup group = ((ViewGroup) view);
result.setChildren(visitAllChildren(group, isContentFrame ? 0 : offset, setExtendedInfo, isContentFrame));
}
return result;
}
use of com.android.ide.common.rendering.api.ViewInfo in project android_frameworks_base by ParanoidAndroid.
the class RenderSessionImpl method visit.
/**
* Visits a View and its children and generate a {@link ViewInfo} containing the
* bounds of all the views.
* @param view the root View
* @param offset an offset for the view bounds.
* @param setExtendedInfo whether to set the extended view info in the {@link ViewInfo} object.
*/
private ViewInfo visit(View view, int offset, boolean setExtendedInfo) {
if (view == null) {
return null;
}
ViewInfo result = new ViewInfo(view.getClass().getName(), getContext().getViewKey(view), view.getLeft(), view.getTop() + offset, view.getRight(), view.getBottom() + offset, view, view.getLayoutParams());
if (setExtendedInfo) {
MarginLayoutParams marginParams = null;
LayoutParams params = view.getLayoutParams();
if (params instanceof MarginLayoutParams) {
marginParams = (MarginLayoutParams) params;
}
result.setExtendedInfo(view.getBaseline(), marginParams != null ? marginParams.leftMargin : 0, marginParams != null ? marginParams.topMargin : 0, marginParams != null ? marginParams.rightMargin : 0, marginParams != null ? marginParams.bottomMargin : 0);
}
if (view instanceof ViewGroup) {
ViewGroup group = ((ViewGroup) view);
result.setChildren(visitAllChildren(group, 0, /*offset*/
setExtendedInfo));
}
return result;
}
use of com.android.ide.common.rendering.api.ViewInfo in project android_frameworks_base by AOSPA.
the class RenderSessionImpl method createViewInfo.
/**
* Creates a {@link ViewInfo} for the view. The {@code ViewInfo} corresponding to the children
* of the {@code view} are not created. Consequently, the children of {@code ViewInfo} is not
* set.
* @param offset an offset for the view bounds. Used only if view is part of the content frame.
*/
private ViewInfo createViewInfo(View view, int offset, boolean setExtendedInfo, boolean isContentFrame) {
if (view == null) {
return null;
}
ViewParent parent = view.getParent();
ViewInfo result;
if (isContentFrame) {
// Account for parent scroll values when calculating the bounding box
int scrollX = parent != null ? ((View) parent).getScrollX() : 0;
int scrollY = parent != null ? ((View) parent).getScrollY() : 0;
// The view is part of the layout added by the user. Hence,
// the ViewCookie may be obtained only through the Context.
result = new ViewInfo(view.getClass().getName(), getContext().getViewKey(view), -scrollX + view.getLeft(), -scrollY + view.getTop() + offset, -scrollX + view.getRight(), -scrollY + view.getBottom() + offset, view, view.getLayoutParams());
} else {
// We are part of the system decor.
SystemViewInfo r = new SystemViewInfo(view.getClass().getName(), getViewKey(view), view.getLeft(), view.getTop(), view.getRight(), view.getBottom(), view, view.getLayoutParams());
result = r;
// 3. The overflow popup button.
if (view instanceof ListMenuItemView) {
// Mark 2.
// All menus in the popup are of type ListMenuItemView.
r.setViewType(ViewType.ACTION_BAR_OVERFLOW_MENU);
} else {
// Mark 3.
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp instanceof ActionMenuView.LayoutParams && ((ActionMenuView.LayoutParams) lp).isOverflowButton) {
r.setViewType(ViewType.ACTION_BAR_OVERFLOW);
} else {
// actionProviderClass.
while (parent != mViewRoot && parent instanceof ViewGroup) {
if (parent instanceof ActionMenuView) {
r.setViewType(ViewType.ACTION_BAR_MENU);
break;
}
parent = parent.getParent();
}
}
}
}
if (setExtendedInfo) {
MarginLayoutParams marginParams = null;
LayoutParams params = view.getLayoutParams();
if (params instanceof MarginLayoutParams) {
marginParams = (MarginLayoutParams) params;
}
result.setExtendedInfo(view.getBaseline(), marginParams != null ? marginParams.leftMargin : 0, marginParams != null ? marginParams.topMargin : 0, marginParams != null ? marginParams.rightMargin : 0, marginParams != null ? marginParams.bottomMargin : 0);
}
return result;
}
Aggregations