Search in sources :

Example 6 with ResourceReference

use of com.android.ide.common.rendering.api.ResourceReference in project android_frameworks_base by DirtyUnicorns.

the class AdapterHelper method fillView.

private static void fillView(BridgeContext context, View view, AdapterItem item, AdapterItem parentItem, LayoutlibCallback callback, ResourceReference adapterRef) {
    if (view instanceof ViewGroup) {
        ViewGroup group = (ViewGroup) view;
        final int count = group.getChildCount();
        for (int i = 0; i < count; i++) {
            fillView(context, group.getChildAt(i), item, parentItem, callback, adapterRef);
        }
    } else {
        int id = view.getId();
        if (id != 0) {
            ResourceReference resolvedRef = context.resolveId(id);
            if (resolvedRef != null) {
                int fullPosition = item.getFullPosition();
                int positionPerType = item.getPositionPerType();
                int fullParentPosition = parentItem != null ? parentItem.getFullPosition() : 0;
                int parentPositionPerType = parentItem != null ? parentItem.getPositionPerType() : 0;
                if (view instanceof TextView) {
                    TextView tv = (TextView) view;
                    Object value = callback.getAdapterItemValue(adapterRef, context.getViewKey(view), item.getDataBindingItem().getViewReference(), fullPosition, positionPerType, fullParentPosition, parentPositionPerType, resolvedRef, ViewAttribute.TEXT, tv.getText().toString());
                    if (value != null) {
                        if (value.getClass() != ViewAttribute.TEXT.getAttributeClass()) {
                            Bridge.getLog().error(LayoutLog.TAG_BROKEN, String.format("Wrong Adapter Item value class for TEXT. Expected String, got %s", value.getClass().getName()), null);
                        } else {
                            tv.setText((String) value);
                        }
                    }
                }
                if (view instanceof Checkable) {
                    Checkable cb = (Checkable) view;
                    Object value = callback.getAdapterItemValue(adapterRef, context.getViewKey(view), item.getDataBindingItem().getViewReference(), fullPosition, positionPerType, fullParentPosition, parentPositionPerType, resolvedRef, ViewAttribute.IS_CHECKED, cb.isChecked());
                    if (value != null) {
                        if (value.getClass() != ViewAttribute.IS_CHECKED.getAttributeClass()) {
                            Bridge.getLog().error(LayoutLog.TAG_BROKEN, String.format("Wrong Adapter Item value class for IS_CHECKED. Expected Boolean, got %s", value.getClass().getName()), null);
                        } else {
                            cb.setChecked((Boolean) value);
                        }
                    }
                }
                if (view instanceof ImageView) {
                    ImageView iv = (ImageView) view;
                    Object value = callback.getAdapterItemValue(adapterRef, context.getViewKey(view), item.getDataBindingItem().getViewReference(), fullPosition, positionPerType, fullParentPosition, parentPositionPerType, resolvedRef, ViewAttribute.SRC, iv.getDrawable());
                    if (value != null) {
                        if (value.getClass() != ViewAttribute.SRC.getAttributeClass()) {
                            Bridge.getLog().error(LayoutLog.TAG_BROKEN, String.format("Wrong Adapter Item value class for SRC. Expected Boolean, got %s", value.getClass().getName()), null);
                        } else {
                        // FIXME
                        }
                    }
                }
            }
        }
    }
}
Also used : ViewGroup(android.view.ViewGroup) TextView(android.widget.TextView) ResourceReference(com.android.ide.common.rendering.api.ResourceReference) Checkable(android.widget.Checkable) ImageView(android.widget.ImageView)

Example 7 with ResourceReference

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

the class LayoutMetadata method getNodeBinding.

@Nullable
private static AdapterBinding getNodeBinding(@Nullable Object viewObject, @Nullable String header, @Nullable String footer, @Nullable String layout, int count) {
    if (layout != null || header != null || footer != null) {
        AdapterBinding binding = new AdapterBinding(count);
        if (header != null) {
            boolean isFramework = header.startsWith(ANDROID_LAYOUT_RESOURCE_PREFIX);
            binding.addHeader(new ResourceReference(stripLayoutPrefix(header), isFramework));
        }
        if (footer != null) {
            boolean isFramework = footer.startsWith(ANDROID_LAYOUT_RESOURCE_PREFIX);
            binding.addFooter(new ResourceReference(stripLayoutPrefix(footer), isFramework));
        }
        if (layout != null) {
            boolean isFramework = layout.startsWith(ANDROID_LAYOUT_RESOURCE_PREFIX);
            if (isFramework) {
                layout = layout.substring(ANDROID_LAYOUT_RESOURCE_PREFIX.length());
            } else if (layout.startsWith(LAYOUT_RESOURCE_PREFIX)) {
                layout = layout.substring(LAYOUT_RESOURCE_PREFIX.length());
            }
            binding.addItem(new DataBindingItem(layout, isFramework, 1));
        } else if (viewObject != null) {
            String listFqcn = LayoutlibCallbackImpl.getListAdapterViewFqcn(viewObject.getClass());
            if (listFqcn != null) {
                if (listFqcn.endsWith(EXPANDABLE_LIST_VIEW)) {
                    binding.addItem(new DataBindingItem(DEFAULT_EXPANDABLE_LIST_ITEM, true, /* isFramework */
                    1));
                } else {
                    binding.addItem(new DataBindingItem(DEFAULT_LIST_ITEM, true, /* isFramework */
                    1));
                }
            }
        } else {
            binding.addItem(new DataBindingItem(DEFAULT_LIST_ITEM, true, /* isFramework */
            1));
        }
        return binding;
    }
    return null;
}
Also used : AdapterBinding(com.android.ide.common.rendering.api.AdapterBinding) DataBindingItem(com.android.ide.common.rendering.api.DataBindingItem) ResourceReference(com.android.ide.common.rendering.api.ResourceReference) Nullable(com.android.annotations.Nullable)

Example 8 with ResourceReference

use of com.android.ide.common.rendering.api.ResourceReference in project platform_frameworks_base by android.

the class Resources_Theme_Delegate method resolveStyle.

@Nullable
private static StyleResourceValue resolveStyle(int nativeResid) {
    if (nativeResid == 0) {
        return null;
    }
    BridgeContext context = RenderSessionImpl.getCurrentContext();
    ResourceReference theme = context.resolveId(nativeResid);
    if (theme.isFramework()) {
        return (StyleResourceValue) context.getRenderResources().getFrameworkResource(ResourceType.STYLE, theme.getName());
    } else {
        return (StyleResourceValue) context.getRenderResources().getProjectResource(ResourceType.STYLE, theme.getName());
    }
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceReference(com.android.ide.common.rendering.api.ResourceReference) Nullable(android.annotation.Nullable)

Example 9 with ResourceReference

use of com.android.ide.common.rendering.api.ResourceReference in project platform_frameworks_base by android.

the class RenderSessionImpl method postInflateProcess.

/**
     * Post process on a view hierarchy that was just inflated.
     * <p/>
     * At the moment this only supports TabHost: If {@link TabHost} is detected, look for the
     * {@link TabWidget}, and the corresponding {@link FrameLayout} and make new tabs automatically
     * based on the content of the {@link FrameLayout}.
     * @param view the root view to process.
     * @param layoutlibCallback callback to the project.
     * @param skip the view and it's children are not processed.
     */
// For the use of Pair
@SuppressWarnings("deprecation")
private void postInflateProcess(View view, LayoutlibCallback layoutlibCallback, View skip) throws PostInflateException {
    if (view == skip) {
        return;
    }
    if (view instanceof TabHost) {
        setupTabHost((TabHost) view, layoutlibCallback);
    } else if (view instanceof QuickContactBadge) {
        QuickContactBadge badge = (QuickContactBadge) view;
        badge.setImageToDefault();
    } else if (view instanceof AdapterView<?>) {
        // get the view ID.
        int id = view.getId();
        BridgeContext context = getContext();
        // get a ResourceReference from the integer ID.
        ResourceReference listRef = context.resolveId(id);
        if (listRef != null) {
            SessionParams params = getParams();
            AdapterBinding binding = params.getAdapterBindings().get(listRef);
            // if there was no adapter binding, trying to get it from the call back.
            if (binding == null) {
                binding = layoutlibCallback.getAdapterBinding(listRef, context.getViewKey(view), view);
            }
            if (binding != null) {
                if (view instanceof AbsListView) {
                    if ((binding.getFooterCount() > 0 || binding.getHeaderCount() > 0) && view instanceof ListView) {
                        ListView list = (ListView) view;
                        boolean skipCallbackParser = false;
                        int count = binding.getHeaderCount();
                        for (int i = 0; i < count; i++) {
                            Pair<View, Boolean> pair = context.inflateView(binding.getHeaderAt(i), list, false, skipCallbackParser);
                            if (pair.getFirst() != null) {
                                list.addHeaderView(pair.getFirst());
                            }
                            skipCallbackParser |= pair.getSecond();
                        }
                        count = binding.getFooterCount();
                        for (int i = 0; i < count; i++) {
                            Pair<View, Boolean> pair = context.inflateView(binding.getFooterAt(i), list, false, skipCallbackParser);
                            if (pair.getFirst() != null) {
                                list.addFooterView(pair.getFirst());
                            }
                            skipCallbackParser |= pair.getSecond();
                        }
                    }
                    if (view instanceof ExpandableListView) {
                        ((ExpandableListView) view).setAdapter(new FakeExpandableAdapter(listRef, binding, layoutlibCallback));
                    } else {
                        ((AbsListView) view).setAdapter(new FakeAdapter(listRef, binding, layoutlibCallback));
                    }
                } else if (view instanceof AbsSpinner) {
                    ((AbsSpinner) view).setAdapter(new FakeAdapter(listRef, binding, layoutlibCallback));
                }
            }
        }
    } else if (view instanceof ViewGroup) {
        mInflater.postInflateProcess(view);
        ViewGroup group = (ViewGroup) view;
        final int count = group.getChildCount();
        for (int c = 0; c < count; c++) {
            View child = group.getChildAt(c);
            postInflateProcess(child, layoutlibCallback, skip);
        }
    }
}
Also used : SessionParams(com.android.ide.common.rendering.api.SessionParams) TabHost(android.widget.TabHost) ViewGroup(android.view.ViewGroup) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) AbsListView(android.widget.AbsListView) FakeAdapter(com.android.layoutlib.bridge.impl.binding.FakeAdapter) FakeExpandableAdapter(com.android.layoutlib.bridge.impl.binding.FakeExpandableAdapter) MenuView(com.android.internal.view.menu.MenuView) View(android.view.View) AdapterView(android.widget.AdapterView) ActionMenuItemView(com.android.internal.view.menu.ActionMenuItemView) IconMenuItemView(com.android.internal.view.menu.IconMenuItemView) ListView(android.widget.ListView) ListMenuItemView(com.android.internal.view.menu.ListMenuItemView) AbsListView(android.widget.AbsListView) ActionMenuView(android.widget.ActionMenuView) ExpandableListView(android.widget.ExpandableListView) ListView(android.widget.ListView) AbsListView(android.widget.AbsListView) ExpandableListView(android.widget.ExpandableListView) QuickContactBadge(android.widget.QuickContactBadge) AdapterBinding(com.android.ide.common.rendering.api.AdapterBinding) AbsSpinner(android.widget.AbsSpinner) AdapterView(android.widget.AdapterView) ResourceReference(com.android.ide.common.rendering.api.ResourceReference) ExpandableListView(android.widget.ExpandableListView)

Example 10 with ResourceReference

use of com.android.ide.common.rendering.api.ResourceReference in project platform_frameworks_base by android.

the class AdapterHelper method fillView.

private static void fillView(BridgeContext context, View view, AdapterItem item, AdapterItem parentItem, LayoutlibCallback callback, ResourceReference adapterRef) {
    if (view instanceof ViewGroup) {
        ViewGroup group = (ViewGroup) view;
        final int count = group.getChildCount();
        for (int i = 0; i < count; i++) {
            fillView(context, group.getChildAt(i), item, parentItem, callback, adapterRef);
        }
    } else {
        int id = view.getId();
        if (id != 0) {
            ResourceReference resolvedRef = context.resolveId(id);
            if (resolvedRef != null) {
                int fullPosition = item.getFullPosition();
                int positionPerType = item.getPositionPerType();
                int fullParentPosition = parentItem != null ? parentItem.getFullPosition() : 0;
                int parentPositionPerType = parentItem != null ? parentItem.getPositionPerType() : 0;
                if (view instanceof TextView) {
                    TextView tv = (TextView) view;
                    Object value = callback.getAdapterItemValue(adapterRef, context.getViewKey(view), item.getDataBindingItem().getViewReference(), fullPosition, positionPerType, fullParentPosition, parentPositionPerType, resolvedRef, ViewAttribute.TEXT, tv.getText().toString());
                    if (value != null) {
                        if (value.getClass() != ViewAttribute.TEXT.getAttributeClass()) {
                            Bridge.getLog().error(LayoutLog.TAG_BROKEN, String.format("Wrong Adapter Item value class for TEXT. Expected String, got %s", value.getClass().getName()), null);
                        } else {
                            tv.setText((String) value);
                        }
                    }
                }
                if (view instanceof Checkable) {
                    Checkable cb = (Checkable) view;
                    Object value = callback.getAdapterItemValue(adapterRef, context.getViewKey(view), item.getDataBindingItem().getViewReference(), fullPosition, positionPerType, fullParentPosition, parentPositionPerType, resolvedRef, ViewAttribute.IS_CHECKED, cb.isChecked());
                    if (value != null) {
                        if (value.getClass() != ViewAttribute.IS_CHECKED.getAttributeClass()) {
                            Bridge.getLog().error(LayoutLog.TAG_BROKEN, String.format("Wrong Adapter Item value class for IS_CHECKED. Expected Boolean, got %s", value.getClass().getName()), null);
                        } else {
                            cb.setChecked((Boolean) value);
                        }
                    }
                }
                if (view instanceof ImageView) {
                    ImageView iv = (ImageView) view;
                    Object value = callback.getAdapterItemValue(adapterRef, context.getViewKey(view), item.getDataBindingItem().getViewReference(), fullPosition, positionPerType, fullParentPosition, parentPositionPerType, resolvedRef, ViewAttribute.SRC, iv.getDrawable());
                    if (value != null) {
                        if (value.getClass() != ViewAttribute.SRC.getAttributeClass()) {
                            Bridge.getLog().error(LayoutLog.TAG_BROKEN, String.format("Wrong Adapter Item value class for SRC. Expected Boolean, got %s", value.getClass().getName()), null);
                        } else {
                        // FIXME
                        }
                    }
                }
            }
        }
    }
}
Also used : ViewGroup(android.view.ViewGroup) TextView(android.widget.TextView) ResourceReference(com.android.ide.common.rendering.api.ResourceReference) Checkable(android.widget.Checkable) ImageView(android.widget.ImageView)

Aggregations

ResourceReference (com.android.ide.common.rendering.api.ResourceReference)28 ViewGroup (android.view.ViewGroup)12 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)10 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)9 AdapterBinding (com.android.ide.common.rendering.api.AdapterBinding)7 DataBindingItem (com.android.ide.common.rendering.api.DataBindingItem)7 View (android.view.View)6 AbsListView (android.widget.AbsListView)6 AbsSpinner (android.widget.AbsSpinner)6 AdapterView (android.widget.AdapterView)6 Checkable (android.widget.Checkable)6 ExpandableListView (android.widget.ExpandableListView)6 ImageView (android.widget.ImageView)6 ListView (android.widget.ListView)6 QuickContactBadge (android.widget.QuickContactBadge)6 TabHost (android.widget.TabHost)6 TextView (android.widget.TextView)6 SessionParams (com.android.ide.common.rendering.api.SessionParams)6 FakeAdapter (com.android.layoutlib.bridge.impl.binding.FakeAdapter)6 FakeExpandableAdapter (com.android.layoutlib.bridge.impl.binding.FakeExpandableAdapter)6