Search in sources :

Example 1 with CodenameOneThread

use of com.codename1.impl.CodenameOneThread in project CodenameOne by codenameone.

the class Log method logThrowable.

/**
 * Logs an exception to the log, by default print is called with the exception
 * details, on supported devices the stack trace is also physically written to
 * the log
 * @param t
 */
protected void logThrowable(Throwable t) {
    if (t == null) {
        p("Exception logging invoked with null exception...");
        return;
    }
    print("Exception: " + t.getClass().getName() + " - " + t.getMessage(), ERROR);
    Thread thr = Thread.currentThread();
    if (thr instanceof CodenameOneThread && ((CodenameOneThread) thr).hasStackFrame()) {
        print(((CodenameOneThread) thr).getStack(t), ERROR);
    }
    t.printStackTrace();
    try {
        synchronized (this) {
            Writer w = getWriter();
            Util.getImplementation().printStackTraceToStream(t, w);
            w.flush();
        }
    } catch (IOException err) {
        err.printStackTrace();
    }
}
Also used : CodenameOneThread(com.codename1.impl.CodenameOneThread) IOException(java.io.IOException) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) CodenameOneThread(com.codename1.impl.CodenameOneThread)

Example 2 with CodenameOneThread

use of com.codename1.impl.CodenameOneThread in project CodenameOne by codenameone.

the class CodenameOneThread method handleException.

public static void handleException(Throwable err) {
    Thread t = Thread.currentThread();
    if (t instanceof CodenameOneThread) {
        Log.p(err.toString());
        Log.p(((CodenameOneThread) t).getStack(err));
        Preferences.set("$CN1Uncaught", true);
        CrashReport r = Display.getInstance().getCrashReporter();
        if (r != null) {
            r.exception(err);
        }
    }
}
Also used : CrashReport(com.codename1.system.CrashReport)

Example 3 with CodenameOneThread

use of com.codename1.impl.CodenameOneThread in project CodenameOne by codenameone.

the class Display method scheduleBackgroundTask.

/**
 * Allows executing a background task in a separate low priority thread. Tasks are serialized
 * so they don't overload the CPU.
 *
 * @param r the task to perform in the background
 */
public void scheduleBackgroundTask(Runnable r) {
    synchronized (lock) {
        if (backgroundTasks == null) {
            backgroundTasks = new ArrayList<Runnable>();
        }
        backgroundTasks.add(r);
        if (backgroundThread == null) {
            backgroundThread = new CodenameOneThread(new Runnable() {

                public void run() {
                    // using while true to avoid double lock optimization with synchronized block
                    while (true) {
                        Runnable nextTask = null;
                        synchronized (lock) {
                            if (backgroundTasks.size() > 0) {
                                nextTask = (Runnable) backgroundTasks.get(0);
                            } else {
                                backgroundThread = null;
                                return;
                            }
                            backgroundTasks.remove(0);
                        }
                        // backgroundThread
                        try {
                            nextTask.run();
                        } catch (Throwable e) {
                            Log.e(e);
                        }
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException ex) {
                        }
                    }
                }
            }, "Task Thread");
            backgroundThread.setPriority(Thread.MIN_PRIORITY + 1);
            backgroundThread.start();
        }
    }
}
Also used : CodenameOneThread(com.codename1.impl.CodenameOneThread)

Example 4 with CodenameOneThread

use of com.codename1.impl.CodenameOneThread in project CodenameOne by codenameone.

the class Display method init.

/**
 * This is the INTERNAL Display initialization method, it will be removed in future versions of the API.
 * This method must be called before any Form is shown
 *
 * @param m platform specific object used by the implementation
 * @deprecated this method is invoked internally do not invoke it!
 */
public static void init(Object m) {
    if (!INSTANCE.codenameOneRunning) {
        INSTANCE.codenameOneRunning = true;
        INSTANCE.displayInitTime = System.currentTimeMillis();
        // restore menu state from previous run if exists
        int commandBehaviour = COMMAND_BEHAVIOR_DEFAULT;
        if (INSTANCE.impl != null) {
            commandBehaviour = INSTANCE.impl.getCommandBehavior();
        }
        INSTANCE.impl = (CodenameOneImplementation) ImplementationFactory.getInstance().createImplementation();
        INSTANCE.impl.setDisplayLock(lock);
        INSTANCE.impl.initImpl(m);
        INSTANCE.codenameOneGraphics = new Graphics(INSTANCE.impl.getNativeGraphics());
        INSTANCE.codenameOneGraphics.paintPeersBehind = INSTANCE.impl.paintNativePeersBehind();
        INSTANCE.impl.setCodenameOneGraphics(INSTANCE.codenameOneGraphics);
        // only enable but never disable the third softbutton
        if (INSTANCE.impl.isThirdSoftButton()) {
            INSTANCE.thirdSoftButton = true;
        }
        if (INSTANCE.impl.getSoftkeyCount() > 0) {
            MenuBar.leftSK = INSTANCE.impl.getSoftkeyCode(0)[0];
            if (INSTANCE.impl.getSoftkeyCount() > 1) {
                MenuBar.rightSK = INSTANCE.impl.getSoftkeyCode(1)[0];
                if (INSTANCE.impl.getSoftkeyCode(1).length > 1) {
                    MenuBar.rightSK2 = INSTANCE.impl.getSoftkeyCode(1)[1];
                }
            }
        }
        MenuBar.backSK = INSTANCE.impl.getBackKeyCode();
        MenuBar.backspaceSK = INSTANCE.impl.getBackspaceKeyCode();
        MenuBar.clearSK = INSTANCE.impl.getClearKeyCode();
        INSTANCE.PATHLENGTH = INSTANCE.impl.getDragPathLength();
        INSTANCE.dragPathX = new float[INSTANCE.PATHLENGTH];
        INSTANCE.dragPathY = new float[INSTANCE.PATHLENGTH];
        INSTANCE.dragPathTime = new long[INSTANCE.PATHLENGTH];
        com.codename1.util.StringUtil.setImplementation(INSTANCE.impl);
        com.codename1.io.Util.setImplementation(INSTANCE.impl);
        // generally its probably a bug but we can let it slide...
        if (INSTANCE.edt == null) {
            INSTANCE.touchScreen = INSTANCE.impl.isTouchDevice();
            // initialize the Codename One EDT which from now on will take all responsibility
            // for the event delivery.
            INSTANCE.edt = new CodenameOneThread(new RunnableWrapper(null, 3), "EDT");
            INSTANCE.impl.setThreadPriority(INSTANCE.edt, INSTANCE.impl.getEDTThreadPriority());
            INSTANCE.edt.start();
        }
        INSTANCE.impl.postInit();
        INSTANCE.setCommandBehavior(commandBehaviour);
    } else {
        INSTANCE.impl.confirmControlView();
    }
}
Also used : CodenameOneThread(com.codename1.impl.CodenameOneThread)

Example 5 with CodenameOneThread

use of com.codename1.impl.CodenameOneThread in project CodenameOne by codenameone.

the class CodenameOneImplementation method registerPollingFallback.

/**
 * Registers a polling thread to simulate push notification
 */
protected static void registerPollingFallback() {
    if (pollingThreadRunning || callback == null) {
        return;
    }
    pollingThreadRunning = true;
    final long pushId = Preferences.get("push_id", (long) -1);
    if (pushId > -1) {
        new CodenameOneThread(new Runnable() {

            public void run() {
                String lastReq = Preferences.get("last_push_req", "0");
                while (pollingThreadRunning) {
                    try {
                        ConnectionRequest cr = new ConnectionRequest();
                        cr.setUrl(Display.getInstance().getProperty("cloudServerURL", "https://codename-one.appspot.com/") + "pollManualPush");
                        cr.setPost(false);
                        cr.setFailSilently(true);
                        cr.addArgument("i", "" + pushId);
                        cr.addArgument("last", lastReq);
                        NetworkManager.getInstance().addToQueueAndWait(cr);
                        if (cr.getResponseCode() != 200) {
                            callback.pushRegistrationError("Server registration error", 1);
                        } else {
                            DataInputStream di = new DataInputStream(new ByteArrayInputStream(cr.getResponseData()));
                            if (di.readBoolean()) {
                                byte type = di.readByte();
                                String message = di.readUTF();
                                lastReq = "" + di.readLong();
                                Preferences.set("last_push_req", lastReq);
                                callback.push(message);
                            }
                        }
                    } catch (IOException ex) {
                        Log.e(ex);
                    }
                    try {
                        synchronized (callback) {
                            callback.wait(pollingMillis);
                        }
                    } catch (Throwable t) {
                        Log.e(t);
                    }
                }
            }
        }, "Polling Thread").start();
    }
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Aggregations

CodenameOneThread (com.codename1.impl.CodenameOneThread)3 IOException (java.io.IOException)2 ConnectionRequest (com.codename1.io.ConnectionRequest)1 CrashReport (com.codename1.system.CrashReport)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1