Search in sources :

Example 1 with PushActionsProvider

use of com.codename1.push.PushActionsProvider 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 PushActionsProvider

use of com.codename1.push.PushActionsProvider 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 PushActionsProvider

use of com.codename1.push.PushActionsProvider 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)

Aggregations

PushAction (com.codename1.push.PushAction)3 PushActionCategory (com.codename1.push.PushActionCategory)3 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)2 RemoteException (android.os.RemoteException)2 MediaException (com.codename1.media.AsyncMedia.MediaException)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URISyntaxException (java.net.URISyntaxException)2 ParseException (java.text.ParseException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 JSONException (org.json.JSONException)2 SAXException (org.xml.sax.SAXException)2 Paint (android.graphics.Paint)1 Element (android.renderscript.Element)1 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 PushActionsProvider (com.codename1.push.PushActionsProvider)1 File (java.io.File)1