org.dmfs.android.xmlmagic.builder.NotificationObjectBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of android-xml-magic Show documentation
Show all versions of android-xml-magic Show documentation
Unleashing the power of XML on Android
/*
* Copyright (C) 2015 Marten Gajda
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dmfs.android.xmlmagic.builder;
import org.dmfs.android.xmlmagic.AndroidParserContext;
import org.dmfs.android.xmlmagic.Model;
import org.dmfs.xmlobjects.ElementDescriptor;
import org.dmfs.xmlobjects.QualifiedName;
import org.dmfs.xmlobjects.pull.ParserContext;
import org.dmfs.xmlobjects.pull.XmlObjectPullParserException;
import android.app.Notification;
import android.app.PendingIntent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
/**
* Created by marten on 10.05.15.
*/
public class NotificationObjectBuilder extends BaseAndroidObjectBuilder
{
public final static NotificationObjectBuilder INSTANCE = new NotificationObjectBuilder();
private final static QualifiedName ATTR_ICON = QualifiedName.get("icon");
private final static QualifiedName ATTR_TICKER = QualifiedName.get("ticker");
private final static QualifiedName ATTR_TITLE = QualifiedName.get("title");
private final static QualifiedName ATTR_SUB_TEXT = QualifiedName.get("sub-text");
private final static QualifiedName ATTR_ONGOING = QualifiedName.get("ongoing");
private final static QualifiedName ATTR_AUTO_CANCEL = QualifiedName.get("auto-cancel");
private final static QualifiedName ATTR_CONTENT = QualifiedName.get("content");
private final static QualifiedName ATTR_NUMBER = QualifiedName.get("number");
private final static QualifiedName ATTR_ALERT_ONLY_ONCE = QualifiedName.get("alert-only-once");
private final static QualifiedName ATTR_SHOW_WHEN = QualifiedName.get("show-when");
private final static QualifiedName ATTR_SORT_KEY = QualifiedName.get("sort-key");
/**
*
*/
private final static QualifiedName ATTR_COLOR = QualifiedName.get("color");
private final static QualifiedName ATTR_ON_MS = QualifiedName.get("on-ms");
private final static QualifiedName ATTR_OFF_MS = QualifiedName.get("off-ms");
private final static QualifiedName ATTR_SOUND = QualifiedName.get("sound");
private final static QualifiedName ATTR_MAX = QualifiedName.get("max");
private final static QualifiedName ATTR_PROGRESS = QualifiedName.get("progress");
private final static QualifiedName ATTR_VISIBLE = QualifiedName.get("visible");
private final static QualifiedName ATTR_INDETERMINANTE = QualifiedName.get("indeterminante");
private final static QualifiedName ATTR_SMALL_ICON = QualifiedName.get("small-icon");
private final static ElementDescriptor ADD_ACTION = ElementDescriptor.register(QualifiedName.get(Model.NAMESPACE, "add-action"),
new BaseAndroidObjectBuilder()
{
@Override
public NotificationCompat.Action get(ElementDescriptor descriptor, NotificationCompat.Action recycle,
ParserContext context) throws XmlObjectPullParserException
{
if (!(context instanceof AndroidParserContext))
{
throw new IllegalArgumentException("ParserContext must be an AndroidParserContext to build a Notification");
}
context.setState(new Action());
return null;
}
@Override
public NotificationCompat.Action update(ElementDescriptor descriptor, NotificationCompat.Action object,
QualifiedName attribute, String value, ParserContext context) throws XmlObjectPullParserException
{
if (ATTR_ICON == attribute)
{
((Action) context.getState()).icon = getIntegerAttr(attribute, false, context);
}
else if (ATTR_TITLE == attribute)
{
((Action) context.getState()).title = getCharSequenceAttr(attribute, value, context);
}
return object;
}
@Override
public NotificationCompat.Action update(ElementDescriptor descriptor, NotificationCompat.Action object,
ElementDescriptor childDescriptor, V child, ParserContext context) throws XmlObjectPullParserException
{
if (childDescriptor == Model.PENDING_INTENT)
{
((Action) context.getState()).intent = (PendingIntent) child;
}
return object;
}
@Override
public NotificationCompat.Action finish(ElementDescriptor descriptor, NotificationCompat.Action object,
ParserContext context) throws XmlObjectPullParserException
{
Action action = (Action) context.getState();
if (action.intent == null)
{
return null;
}
return new NotificationCompat.Action(action.icon, action.title, action.intent);
}
});
private final static ElementDescriptor CONTENT_ACTION = ElementDescriptor.register(QualifiedName.get(Model.NAMESPACE, "content-intent"),
PendingIntentObjectBuilder.INSTANCE);
private final static ElementDescriptor DELETE_ACTION = ElementDescriptor.register(QualifiedName.get(Model.NAMESPACE, "delete-intent"),
PendingIntentObjectBuilder.INSTANCE);
private final static ElementDescriptor LIGHTS = ElementDescriptor.register(QualifiedName.get(Model.NAMESPACE, "lights"),
new BaseAndroidObjectBuilder()
{
@Override
public Lights get(ElementDescriptor descriptor, Lights recycle, ParserContext context) throws XmlObjectPullParserException
{
if (recycle != null)
{
recycle.color = Color.GREEN;
recycle.on = recycle.off = 500;
return recycle;
}
return new Lights();
}
@Override
public Lights update(ElementDescriptor descriptor, Lights object, QualifiedName attribute, String value, ParserContext context)
throws XmlObjectPullParserException
{
if (attribute == ATTR_COLOR)
{
object.color = Color.parseColor(getCharSequenceAttr(attribute, value, context).toString());
}
else if (attribute == ATTR_ON_MS)
{
object.on = getIntegerAttr(attribute, true, context);
}
else if (attribute == ATTR_OFF_MS)
{
object.off = getIntegerAttr(attribute, true, context);
}
return object;
}
});
private final static ElementDescriptor
© 2015 - 2025 Weber Informatics LLC | Privacy Policy