Search in sources :

Example 1 with PushActionCategory

use of com.codename1.push.PushActionCategory in project CodenameOne by codenameone.

the class IOSImplementation method initPushActionCategories.

public static void initPushActionCategories() {
    if (pushCallback instanceof PushActionsProvider) {
        PushActionsProvider actionsProvider = (PushActionsProvider) pushCallback;
        PushActionCategory[] categories = actionsProvider.getPushActionCategories();
        if (categories != null) {
            PushAction[] actions = PushActionCategory.getAllActions(categories);
            for (PushAction action : actions) {
                nativeInstance.registerPushAction(action.getId(), action.getTitle(), action.getTextInputPlaceholder(), action.getTextInputButtonText());
            }
            for (PushActionCategory category : categories) {
                nativeInstance.startPushActionCategory(category.getId());
                for (PushAction action : category.getActions()) {
                    nativeInstance.addPushActionToCategory(action.getId());
                }
                nativeInstance.endPushActionCategory();
            }
            nativeInstance.registerPushCategories();
        }
    }
}
Also used : PushActionsProvider(com.codename1.push.PushActionsProvider) PushActionCategory(com.codename1.push.PushActionCategory) PushAction(com.codename1.push.PushAction)

Example 2 with PushActionCategory

use of com.codename1.push.PushActionCategory in project CodenameOne by codenameone.

the class AndroidImplementation method installNotificationActionCategories.

/**
 * Action categories are defined on the Main class by implementing the PushActionsProvider, however
 * the main class may not be available to the push receiver, so we need to save these categories
 * to the file system when the app is installed, then the push receiver can load these actions
 * when it sends a push while the app isn't running.
 * @param provider A reference to the App's main class
 * @throws IOException
 */
public static void installNotificationActionCategories(PushActionsProvider provider) throws IOException {
    // Assume that CN1 is running... this will run when the app starts
    // up
    Context context = getContext();
    boolean requiresUpdate = false;
    File categoriesFile = new File(activity.getFilesDir().getAbsolutePath() + "/" + FILE_NAME_NOTIFICATION_CATEGORIES);
    if (!categoriesFile.exists()) {
        requiresUpdate = true;
    }
    if (!requiresUpdate) {
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getApplicationContext().getPackageName(), PackageManager.GET_PERMISSIONS);
            if (packageInfo.lastUpdateTime > categoriesFile.lastModified()) {
                requiresUpdate = true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    if (!requiresUpdate) {
        return;
    }
    OutputStream os = getContext().openFileOutput(FILE_NAME_NOTIFICATION_CATEGORIES, 0);
    PushActionCategory[] categories = provider.getPushActionCategories();
    javax.xml.parsers.DocumentBuilderFactory docFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    javax.xml.parsers.DocumentBuilder docBuilder;
    try {
        docBuilder = docFactory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        throw new IOException("Faield to create document builder for creating notification categories XML document", ex);
    }
    // root elements
    org.w3c.dom.Document doc = docBuilder.newDocument();
    org.w3c.dom.Element root = (org.w3c.dom.Element) doc.createElement("categories");
    doc.appendChild(root);
    for (PushActionCategory category : categories) {
        org.w3c.dom.Element categoryEl = (org.w3c.dom.Element) doc.createElement("category");
        org.w3c.dom.Attr idAttr = doc.createAttribute("id");
        idAttr.setValue(category.getId());
        categoryEl.setAttributeNode(idAttr);
        for (PushAction action : category.getActions()) {
            org.w3c.dom.Element actionEl = (org.w3c.dom.Element) doc.createElement("action");
            org.w3c.dom.Attr actionIdAttr = doc.createAttribute("id");
            actionIdAttr.setValue(action.getId());
            actionEl.setAttributeNode(actionIdAttr);
            org.w3c.dom.Attr actionTitleAttr = doc.createAttribute("title");
            if (action.getTitle() != null) {
                actionTitleAttr.setValue(action.getTitle());
            } else {
                actionTitleAttr.setValue(action.getId());
            }
            actionEl.setAttributeNode(actionTitleAttr);
            if (action.getIcon() != null) {
                org.w3c.dom.Attr actionIconAttr = doc.createAttribute("icon");
                String iconVal = action.getIcon();
                try {
                    // We'll store the resource IDs for the icon
                    // rather than the icon name because that is what
                    // the push notifications require.
                    iconVal = "" + context.getResources().getIdentifier(iconVal, "drawable", context.getPackageName());
                    actionIconAttr.setValue(iconVal);
                    actionEl.setAttributeNode(actionIconAttr);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (action.getTextInputPlaceholder() != null) {
                org.w3c.dom.Attr textInputPlaceholderAttr = doc.createAttribute("textInputPlaceholder");
                textInputPlaceholderAttr.setValue(action.getTextInputPlaceholder());
                actionEl.setAttributeNode(textInputPlaceholderAttr);
            }
            if (action.getTextInputButtonText() != null) {
                org.w3c.dom.Attr textInputButtonTextAttr = doc.createAttribute("textInputButtonText");
                textInputButtonTextAttr.setValue(action.getTextInputButtonText());
                actionEl.setAttributeNode(textInputButtonTextAttr);
            }
            categoryEl.appendChild(actionEl);
        }
        root.appendChild(categoryEl);
    }
    try {
        javax.xml.transform.TransformerFactory transformerFactory = javax.xml.transform.TransformerFactory.newInstance();
        javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
        javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(doc);
        javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(os);
        transformer.transform(source, result);
    } catch (Exception ex) {
        throw new IOException("Failed to save notification categories as XML.", ex);
    }
}
Also used : BufferedOutputStream(com.codename1.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) Element(android.renderscript.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) PushActionCategory(com.codename1.push.PushActionCategory) IOException(java.io.IOException) PushAction(com.codename1.push.PushAction) JSONException(org.json.JSONException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) MediaException(com.codename1.media.AsyncMedia.MediaException) ParseException(java.text.ParseException) SAXException(org.xml.sax.SAXException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 3 with PushActionCategory

use of com.codename1.push.PushActionCategory in project CodenameOne by codenameone.

the class AndroidImplementation method addActionsToNotification.

/**
 * Adds actions to a push notification.  This is called by the Push broadcast receiver probably before
 * Codename One is initialized
 * @param provider Reference to the app's main class which implements PushActionsProvider
 * @param categoryId The category ID of the push notification.
 * @param builder The builder for the push notification.
 * @param targetIntent The target intent... this should go to the app's main Activity.
 * @param context The current context (inside the Broadcast receiver).
 * @throws IOException
 */
public static void addActionsToNotification(PushActionsProvider provider, String categoryId, NotificationCompat.Builder builder, Intent targetIntent, Context context) throws IOException {
    // NOTE:  THis will likely run when the main activity isn't running so we won't have
    // access to any display properties... just native Android APIs will be accessible.
    PushActionCategory category = null;
    PushActionCategory[] categories;
    if (provider != null) {
        categories = provider.getPushActionCategories();
    } else {
        categories = getInstalledPushActionCategories(context);
    }
    for (PushActionCategory candidateCategory : categories) {
        if (categoryId.equals(candidateCategory.getId())) {
            category = candidateCategory;
            break;
        }
    }
    if (category == null) {
        return;
    }
    int requestCode = 1;
    for (PushAction action : category.getActions()) {
        Intent newIntent = (Intent) targetIntent.clone();
        newIntent.putExtra("pushActionId", action.getId());
        PendingIntent contentIntent = createPendingIntent(context, requestCode++, newIntent);
        try {
            int iconId = 0;
            try {
                iconId = Integer.parseInt(action.getIcon());
            } catch (Exception ex) {
            }
            // android.app.Notification.Action.Builder actionBuilder = new android.app.Notification.Action.Builder(iconId, action.getTitle(), contentIntent);
            System.out.println("Adding action " + action.getId() + ", " + action.getTitle() + ", icon=" + iconId);
            if (ActionWrapper.BuilderWrapper.isSupported()) {
                // We need to take this abstracted "wrapper" approach because the Action.Builder class, and RemoteInput class
                // aren't available until API 22.
                // These classes use reflection to provide support for these classes safely.
                ActionWrapper.BuilderWrapper actionBuilder = new ActionWrapper.BuilderWrapper(iconId, action.getTitle(), contentIntent);
                if (action.getTextInputPlaceholder() != null && RemoteInputWrapper.isSupported()) {
                    RemoteInputWrapper.BuilderWrapper remoteInputBuilder = new RemoteInputWrapper.BuilderWrapper(action.getId() + "$Result");
                    remoteInputBuilder.setLabel(action.getTextInputPlaceholder());
                    RemoteInputWrapper remoteInput = remoteInputBuilder.build();
                    actionBuilder.addRemoteInput(remoteInput);
                }
                ActionWrapper actionWrapper = actionBuilder.build();
                new NotificationCompatWrapper.BuilderWrapper(builder).addAction(actionWrapper);
            } else {
                builder.addAction(iconId, action.getTitle(), contentIntent);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Also used : PushActionCategory(com.codename1.push.PushActionCategory) ActionWrapper(com.codename1.impl.android.compat.app.NotificationCompatWrapper.ActionWrapper) NotificationCompatWrapper(com.codename1.impl.android.compat.app.NotificationCompatWrapper) PushAction(com.codename1.push.PushAction) Paint(android.graphics.Paint) JSONException(org.json.JSONException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) MediaException(com.codename1.media.AsyncMedia.MediaException) ParseException(java.text.ParseException) SAXException(org.xml.sax.SAXException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RemoteInputWrapper(com.codename1.impl.android.compat.app.RemoteInputWrapper)

Example 4 with PushActionCategory

use of com.codename1.push.PushActionCategory in project CodenameOne by codenameone.

the class AndroidImplementation method getInstalledPushActionCategories.

/**
 * Retrieves the app's available push action categories from the XML file in which they
 * should have been installed on the first load.
 * @param context
 * @return
 * @throws IOException
 */
private static PushActionCategory[] getInstalledPushActionCategories(Context context) throws IOException {
    // NOTE:  This method may be called from the PushReceiver when the app isn't running so we can't access
    // the main activity context, display properties, or any CN1 stuff.  Just native android
    File categoriesFile = new File(context.getFilesDir().getAbsolutePath() + "/" + FILE_NAME_NOTIFICATION_CATEGORIES);
    if (!categoriesFile.exists()) {
        return new PushActionCategory[0];
    }
    javax.xml.parsers.DocumentBuilderFactory docFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    javax.xml.parsers.DocumentBuilder docBuilder;
    try {
        docBuilder = docFactory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        throw new IOException("Faield to create document builder for creating notification categories XML document", ex);
    }
    org.w3c.dom.Document doc;
    try {
        doc = docBuilder.parse(context.openFileInput(FILE_NAME_NOTIFICATION_CATEGORIES));
    } catch (SAXException ex) {
        Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        throw new IOException("Failed to parse instaled push action categories", ex);
    }
    org.w3c.dom.Element root = doc.getDocumentElement();
    java.util.List<PushActionCategory> out = new ArrayList<PushActionCategory>();
    org.w3c.dom.NodeList l = root.getElementsByTagName("category");
    int len = l.getLength();
    for (int i = 0; i < len; i++) {
        org.w3c.dom.Element el = (org.w3c.dom.Element) l.item(i);
        java.util.List<PushAction> actions = new ArrayList<PushAction>();
        org.w3c.dom.NodeList al = el.getElementsByTagName("action");
        int alen = al.getLength();
        for (int j = 0; j < alen; j++) {
            org.w3c.dom.Element actionEl = (org.w3c.dom.Element) al.item(j);
            String textInputPlaceholder = actionEl.hasAttribute("textInputPlaceholder") ? actionEl.getAttribute("textInputPlaceholder") : null;
            String textInputButtonText = actionEl.hasAttribute("textInputButtonText") ? actionEl.getAttribute("textInputButtonText") : null;
            PushAction action = new PushAction(actionEl.getAttribute("id"), actionEl.getAttribute("title"), actionEl.getAttribute("icon"), textInputPlaceholder, textInputButtonText);
            actions.add(action);
        }
        PushActionCategory cat = new PushActionCategory((String) el.getAttribute("id"), actions.toArray(new PushAction[actions.size()]));
        out.add(cat);
    }
    return out.toArray(new PushActionCategory[out.size()]);
}
Also used : Element(android.renderscript.Element) ArrayList(java.util.ArrayList) PushActionCategory(com.codename1.push.PushActionCategory) IOException(java.io.IOException) PushAction(com.codename1.push.PushAction) Paint(android.graphics.Paint) SAXException(org.xml.sax.SAXException) java.util(java.util) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

PushAction (com.codename1.push.PushAction)4 PushActionCategory (com.codename1.push.PushActionCategory)4 IOException (java.io.IOException)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 SAXException (org.xml.sax.SAXException)3 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)2 Paint (android.graphics.Paint)2 RemoteException (android.os.RemoteException)2 Element (android.renderscript.Element)2 MediaException (com.codename1.media.AsyncMedia.MediaException)2 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URISyntaxException (java.net.URISyntaxException)2 ParseException (java.text.ParseException)2 JSONException (org.json.JSONException)2 NotificationCompatWrapper (com.codename1.impl.android.compat.app.NotificationCompatWrapper)1 ActionWrapper (com.codename1.impl.android.compat.app.NotificationCompatWrapper.ActionWrapper)1 RemoteInputWrapper (com.codename1.impl.android.compat.app.RemoteInputWrapper)1 BufferedOutputStream (com.codename1.io.BufferedOutputStream)1