Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Add custom renderer for actions and actionlayouts #1655

Merged
merged 3 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package io.adaptivecards.renderer;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import io.adaptivecards.objectmodel.ActionAlignment;
import io.adaptivecards.objectmodel.ActionsOrientation;
import io.adaptivecards.objectmodel.BaseActionElement;
import io.adaptivecards.objectmodel.BaseActionElementVector;
import io.adaptivecards.objectmodel.HostConfig;
import io.adaptivecards.objectmodel.IconPlacement;
import io.adaptivecards.objectmodel.Spacing;
import io.adaptivecards.renderer.AdaptiveWarning;
import io.adaptivecards.renderer.BaseCardElementRenderer;
import io.adaptivecards.renderer.IActionLayoutRenderer;
import io.adaptivecards.renderer.IBaseActionElementRenderer;
import io.adaptivecards.renderer.RenderedAdaptiveCard;
import io.adaptivecards.renderer.actionhandler.ICardActionHandler;
import io.adaptivecards.renderer.registration.CardRendererRegistration;

public class ActionLayoutRenderer implements IActionLayoutRenderer {

protected ActionLayoutRenderer()
{
}

public static ActionLayoutRenderer getInstance()
{
if (s_instance == null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new to this codebase -- is it safe to assume that this won't be called on two threads at once?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the renderers work the same so I just kept the style for consistency

{
s_instance = new ActionLayoutRenderer();
}

return s_instance;
}

public void renderActions(RenderedAdaptiveCard renderedCard, Context context, FragmentManager fragmentManager, ViewGroup viewGroup, BaseActionElementVector baseActionElementList, ICardActionHandler cardActionHandler, HostConfig hostConfig) {
long size;
if (baseActionElementList == null || (size = baseActionElementList.size()) <= 0)
{
return;
}

LinearLayout actionButtonsLayout = new LinearLayout(context);
actionButtonsLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
int alignment = hostConfig.getActions().getActionAlignment().swigValue();
if (alignment == ActionAlignment.Right.swigValue())
{
actionButtonsLayout.setGravity(Gravity.RIGHT);
}
else if (alignment == ActionAlignment.Center.swigValue())
{
actionButtonsLayout.setGravity(Gravity.CENTER_HORIZONTAL);
}

int actionButtonsLayoutOrientation = hostConfig.getActions().getActionsOrientation().swigValue();
if (actionButtonsLayoutOrientation == ActionsOrientation.Vertical.swigValue())
{
actionButtonsLayout.setOrientation(LinearLayout.VERTICAL);
}
else
{
actionButtonsLayout.setOrientation(LinearLayout.HORIZONTAL);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: unneeded newline

Spacing spacing = hostConfig.getActions().getSpacing();
/* Passing false for separator since we do not have any configuration for separator in actionsConfig */
BaseCardElementRenderer.setSpacingAndSeparator(context, viewGroup, spacing, false, hostConfig, true /* Horizontal Line */);

if (viewGroup != null)
{
if(actionButtonsLayoutOrientation == ActionsOrientation.Horizontal.swigValue())
{
HorizontalScrollView actionButtonsContainer = new HorizontalScrollView(context);
actionButtonsContainer.setHorizontalScrollBarEnabled(false);
actionButtonsContainer.addView(actionButtonsLayout);
viewGroup.addView(actionButtonsContainer);
}
else
{
viewGroup.addView(actionButtonsLayout);
}
}

int i = 0;
Copy link
Member

@paulcam206 paulcam206 Jun 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason why we wouldn't want to just declare i independently in each for loop below? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because of the check on line 118

if (i >= maxActions && size != maxActions)

long maxActions = hostConfig.getActions().getMaxActions();

boolean allActionsHaveIcons = true;
for(; i < size && i < maxActions; ++i)
{
BaseActionElement actionElement = baseActionElementList.get(i);
if(actionElement.GetIconUrl().isEmpty())
{
allActionsHaveIcons = false;
break;
}
}

for (i = 0; i < size && i < maxActions; i++)
{
BaseActionElement actionElement = baseActionElementList.get(i);

IconPlacement originalIconPlacement = hostConfig.getActions().getIconPlacement();
if(!allActionsHaveIcons)
{
hostConfig.getActions().setIconPlacement(IconPlacement.LeftOfTitle);
}

IBaseActionElementRenderer actionRenderer = CardRendererRegistration.getInstance().getActionRenderer();
actionRenderer.render(renderedCard, context, fragmentManager, actionButtonsLayout, actionElement, cardActionHandler, hostConfig);
hostConfig.getActions().setIconPlacement(originalIconPlacement);
}

if (i >= maxActions && size != maxActions)
{
renderedCard.addWarning(new AdaptiveWarning(AdaptiveWarning.MAX_ACTIONS_EXCEEDED, "A maximum of " + maxActions + " actions are allowed"));
}
}

private static ActionLayoutRenderer s_instance = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ public View internalRender(RenderedAdaptiveCard renderedCard,
showCardsLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
rootLayout.addView(showCardsLayout);

renderActions(renderedCard, context, fragmentManager, layout, baseActionElementList, cardActionHandler, hostConfig);
IActionLayoutRenderer actionLayoutRenderer = CardRendererRegistration.getInstance().getActionLayoutRenderer();
if(actionLayoutRenderer != null) {
actionLayoutRenderer.renderActions(renderedCard, context, fragmentManager, layout, baseActionElementList, cardActionHandler, hostConfig);
}
}
}
else
Expand All @@ -203,88 +206,6 @@ public View internalRender(RenderedAdaptiveCard renderedCard,
return rootLayout;
}

private void renderActions(RenderedAdaptiveCard renderedCard, Context context, FragmentManager fragmentManager, ViewGroup viewGroup, BaseActionElementVector baseActionElementList, ICardActionHandler cardActionHandler, HostConfig hostConfig) {
long size;
if (baseActionElementList == null || (size = baseActionElementList.size()) <= 0)
{
return;
}

LinearLayout actionButtonsLayout = new LinearLayout(context);
actionButtonsLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
int alignment = hostConfig.getActions().getActionAlignment().swigValue();
if (alignment == ActionAlignment.Right.swigValue())
{
actionButtonsLayout.setGravity(Gravity.RIGHT);
}
else if (alignment == ActionAlignment.Center.swigValue())
{
actionButtonsLayout.setGravity(Gravity.CENTER_HORIZONTAL);
}

int actionButtonsLayoutOrientation = hostConfig.getActions().getActionsOrientation().swigValue();
if (actionButtonsLayoutOrientation == ActionsOrientation.Vertical.swigValue())
{
actionButtonsLayout.setOrientation(LinearLayout.VERTICAL);
}
else
{
actionButtonsLayout.setOrientation(LinearLayout.HORIZONTAL);
}


Spacing spacing = hostConfig.getActions().getSpacing();
/* Passing false for seperator since we do not have any configuration for seperator in actionsConfig */
BaseCardElementRenderer.setSpacingAndSeparator(context, viewGroup, spacing, false, hostConfig, true /* Horizontal Line */);

if (viewGroup != null)
{
if(actionButtonsLayoutOrientation == ActionsOrientation.Horizontal.swigValue())
{
HorizontalScrollView actionButtonsContainer = new HorizontalScrollView(context);
actionButtonsContainer.setHorizontalScrollBarEnabled(false);
actionButtonsContainer.addView(actionButtonsLayout);
viewGroup.addView(actionButtonsContainer);
}
else
{
viewGroup.addView(actionButtonsLayout);
}
}

int i = 0;
long maxActions = hostConfig.getActions().getMaxActions();

boolean allActionsHaveIcons = true;
for(; i < size && i < maxActions; ++i)
{
BaseActionElement actionElement = baseActionElementList.get(i);
if(actionElement.GetIconUrl().isEmpty())
{
allActionsHaveIcons = false;
break;
}
}

for (i = 0; i < size && i < maxActions; i++)
{
BaseActionElement actionElement = baseActionElementList.get(i);

IconPlacement originalIconPlacement = hostConfig.getActions().getIconPlacement();
if(!allActionsHaveIcons)
{
hostConfig.getActions().setIconPlacement(IconPlacement.LeftOfTitle);
}
ActionElementRenderer.getInstance().render(renderedCard, context, fragmentManager, actionButtonsLayout, actionElement, cardActionHandler, hostConfig);
hostConfig.getActions().setIconPlacement(originalIconPlacement);
}

if (i >= maxActions && size != maxActions)
{
renderedCard.addWarning(new AdaptiveWarning(AdaptiveWarning.MAX_ACTIONS_EXCEEDED, "A maximum of " + maxActions + " actions are allowed"));
}
}

private static AdaptiveCardRenderer s_instance = null;

private HostConfig defaultHostConfig = new HostConfig();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.adaptivecards.renderer;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.view.ViewGroup;

import io.adaptivecards.objectmodel.BaseActionElementVector;
import io.adaptivecards.objectmodel.HostConfig;
import io.adaptivecards.renderer.actionhandler.ICardActionHandler;

public interface IActionLayoutRenderer {
public void renderActions(RenderedAdaptiveCard renderedCard, Context context, FragmentManager fragmentManager, ViewGroup viewGroup, BaseActionElementVector baseActionElementList, ICardActionHandler cardActionHandler, HostConfig hostConfig);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

import io.adaptivecards.objectmodel.ContainerStyle;
import io.adaptivecards.renderer.AdaptiveWarning;
import io.adaptivecards.renderer.IActionLayoutRenderer;
import io.adaptivecards.renderer.IBaseActionElementRenderer;
import io.adaptivecards.renderer.RenderedAdaptiveCard;
import io.adaptivecards.renderer.action.ActionElementRenderer;
import io.adaptivecards.renderer.ActionLayoutRenderer;
import io.adaptivecards.renderer.actionhandler.ICardActionHandler;
import io.adaptivecards.objectmodel.BaseCardElement;
import io.adaptivecards.objectmodel.BaseCardElementVector;
Expand Down Expand Up @@ -54,6 +58,10 @@ private CardRendererRegistration()
registerRenderer(CardElementTypeToString(CardElementType.TimeInput), TimeInputRenderer.getInstance());
registerRenderer(CardElementTypeToString(CardElementType.ToggleInput), ToggleInputRenderer.getInstance());
registerRenderer(CardElementTypeToString(CardElementType.ChoiceSetInput), ChoiceSetInputRenderer.getInstance());

// Register Action Renderer
m_actionRenderer = ActionElementRenderer.getInstance();
m_actionLayoutRenderer = ActionLayoutRenderer.getInstance();
}

public static CardRendererRegistration getInstance()
Expand Down Expand Up @@ -85,6 +93,26 @@ public IBaseCardElementRenderer getRenderer(String cardElementType)
return m_typeToRendererMap.get(cardElementType);
}

public void registerActionRenderer(IBaseActionElementRenderer actionRenderer)
{
m_actionRenderer = actionRenderer;
}

public IBaseActionElementRenderer getActionRenderer()
{
return m_actionRenderer;
}

public void registerActionLayoutRenderer(IActionLayoutRenderer actionLayoutRenderer)
{
m_actionLayoutRenderer = actionLayoutRenderer;
}

public IActionLayoutRenderer getActionLayoutRenderer()
{
return m_actionLayoutRenderer;
}

public View render(
RenderedAdaptiveCard renderedCard,
Context context,
Expand Down Expand Up @@ -131,4 +159,6 @@ public View render(
private static CardRendererRegistration s_instance = null;

private HashMap<String, IBaseCardElementRenderer> m_typeToRendererMap = new HashMap<String, IBaseCardElementRenderer>();
private IBaseActionElementRenderer m_actionRenderer = null;
private IActionLayoutRenderer m_actionLayoutRenderer = null;
}