Search in sources :

Example 1 with PushCallback

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

the class Executor method main.

public static void main(final String[] argv) throws Exception {
    setProxySettings();
    final Properties p = new Properties();
    String currentDir = System.getProperty("user.dir");
    File props = new File(currentDir, "codenameone_settings.properties");
    if (props.exists()) {
        FileInputStream f = null;
        try {
            f = new FileInputStream(props);
            p.load(f);
            f.close();
        } catch (Exception ex) {
        } finally {
            try {
                f.close();
            } catch (IOException ex) {
            }
        }
    }
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            try {
                String packageName = p.getProperty("codename1.packageName");
                String mainName = p.getProperty("codename1.mainName");
                if (argv.length > 1) {
                    if (argv[1].equalsIgnoreCase("-force") || packageName == null) {
                        c = Class.forName(argv[0]);
                    } else {
                        c = Class.forName(packageName + "." + mainName);
                    }
                } else {
                    if (packageName == null || System.getenv("FORCE_CLASS") != null) {
                        c = Class.forName(argv[0]);
                    } else {
                        c = Class.forName(packageName + "." + mainName);
                    }
                }
                try {
                    Method m = c.getDeclaredMethod("main", String[].class);
                    m.invoke(null, new Object[] { null });
                } catch (NoSuchMethodException noMain) {
                    try {
                        Method m = c.getDeclaredMethod("startApp");
                        m.invoke(c.newInstance());
                    } catch (NoSuchMethodException noStartApp) {
                        if (Display.isInitialized()) {
                            Display.deinitialize();
                        }
                        final Method m = c.getDeclaredMethod("init", Object.class);
                        if (m.getExceptionTypes() != null && m.getExceptionTypes().length > 0) {
                            System.err.println("ERROR: the init method can't declare a throws clause");
                            System.exit(1);
                        }
                        app = c.newInstance();
                        if (app instanceof PushCallback) {
                            CodenameOneImplementation.setPushCallback((PushCallback) app);
                        }
                        if (app instanceof PurchaseCallback) {
                            CodenameOneImplementation.setPurchaseCallback((PurchaseCallback) app);
                        }
                        Display.init(null);
                        Display.getInstance().callSerially(new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    m.invoke(app, new Object[] { null });
                                    Method start = c.getDeclaredMethod("start", new Class[0]);
                                    if (start.getExceptionTypes() != null && start.getExceptionTypes().length > 0) {
                                        System.err.println("ERROR: the start method can't declare a throws clause");
                                        System.exit(1);
                                    }
                                    start.invoke(app, new Object[0]);
                                } catch (NoSuchMethodException err) {
                                    System.out.println("Couldn't find a main or a startup in " + argv[0]);
                                } catch (InvocationTargetException err) {
                                    err.getTargetException().printStackTrace();
                                    System.exit(1);
                                } catch (Exception err) {
                                    err.printStackTrace();
                                    System.exit(1);
                                }
                            }
                        });
                    }
                }
            } catch (Exception err) {
                err.printStackTrace();
                System.exit(1);
            }
        }
    });
}
Also used : IOException(java.io.IOException) Method(java.lang.reflect.Method) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PushCallback(com.codename1.push.PushCallback) PurchaseCallback(com.codename1.payment.PurchaseCallback) File(java.io.File)

Example 2 with PushCallback

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

the class AndroidImplementation method firePendingPushes.

public static void firePendingPushes(final PushCallback c, Context a) {
    try {
        if (c != null) {
            InputStream i = a.openFileInput("CN1$AndroidPendingNotifications");
            if (i == null) {
                return;
            }
            DataInputStream is = new DataInputStream(i);
            int count = is.readByte();
            for (int iter = 0; iter < count; iter++) {
                boolean hasType = is.readBoolean();
                String actualType = null;
                if (hasType) {
                    actualType = is.readUTF();
                }
                final String t = actualType;
                final String b = is.readUTF();
                long s = is.readLong();
                Display.getInstance().callSerially(new Runnable() {

                    @Override
                    public void run() {
                        Display.getInstance().setProperty("pendingPush", "true");
                        Display.getInstance().setProperty("pushType", t);
                        if (t != null && ("3".equals(t) || "6".equals(t))) {
                            String[] a = b.split(";");
                            c.push(a[0]);
                            c.push(a[1]);
                        } else {
                            c.push(b);
                        }
                        Display.getInstance().setProperty("pendingPush", null);
                    }
                });
            }
            a.deleteFile("CN1$AndroidPendingNotifications");
        }
    } catch (IOException err) {
    }
}
Also used : BufferedInputStream(com.codename1.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Paint(android.graphics.Paint)

Example 3 with PushCallback

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

the class PushNotificationService method push.

@Override
public void push(final String value) {
    final PushCallback callback = getPushCallbackInstance();
    if (callback != null) {
        Display.getInstance().callSerially(new Runnable() {

            public void run() {
                callback.push(value);
            }
        });
    } else {
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent newIntent = new Intent(this, getStubClass());
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, newIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        Notification.Builder builder = new Notification.Builder(this).setContentIntent(contentIntent).setSmallIcon(android.R.drawable.stat_notify_sync).setTicker(value).setAutoCancel(true).setWhen(System.currentTimeMillis()).setContentTitle(value).setDefaults(Notification.DEFAULT_ALL);
        Notification notif = builder.build();
        nm.notify((int) System.currentTimeMillis(), notif);
    }
}
Also used : NotificationManager(android.app.NotificationManager) PushCallback(com.codename1.push.PushCallback) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification)

Aggregations

PushCallback (com.codename1.push.PushCallback)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 Notification (android.app.Notification)1 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 Paint (android.graphics.Paint)1 BufferedInputStream (com.codename1.io.BufferedInputStream)1 PurchaseCallback (com.codename1.payment.PurchaseCallback)1 File (java.io.File)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Properties (java.util.Properties)1