From 17acda976236a3cd3ca90ec8ba288fdde5ac875d Mon Sep 17 00:00:00 2001 From: Marco Castelluccio Date: Sun, 1 Feb 2015 12:40:48 +0100 Subject: [PATCH 1/5] Remove TextEditorThread::sleep --- java/custom/com/nokia/mid/ui/TextEditor.java | 2 -- jit/analyze.ts | 2 +- midp/gfx.js | 21 +++++++------------- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/java/custom/com/nokia/mid/ui/TextEditor.java b/java/custom/com/nokia/mid/ui/TextEditor.java index f8ec09ce..ad71b9c8 100644 --- a/java/custom/com/nokia/mid/ui/TextEditor.java +++ b/java/custom/com/nokia/mid/ui/TextEditor.java @@ -5,7 +5,6 @@ import java.util.Hashtable; class TextEditorThread implements Runnable { // We need a thread to be able to wake from js when there is an async js keyboard event. - native void sleep(); native int getNextDirtyEditor(); Hashtable listeners; @@ -16,7 +15,6 @@ class TextEditorThread implements Runnable { public void run() { while (true) { - sleep(); int dirty = getNextDirtyEditor(); TextEditor t = (TextEditor)listeners.get("" + dirty); t.myListener.inputAction(t, TextEditorListener.ACTION_CONTENT_CHANGE); diff --git a/jit/analyze.ts b/jit/analyze.ts index a69213bb..11ee390f 100644 --- a/jit/analyze.ts +++ b/jit/analyze.ts @@ -39,7 +39,7 @@ module J2ME { "com/ibm/oti/connection/file/FCOutputStream.openOffsetImpl.([BJ)I": YieldReason.Root, "com/sun/midp/io/j2me/storage/RandomAccessStream.open.(Ljava/lang/String;I)I": YieldReason.Root, "javax/microedition/lcdui/ImageDataFactory.createImmutableImageDecodeImage.(Ljavax/microedition/lcdui/ImageData;[BII)V": YieldReason.Root, - "com/nokia/mid/ui/TextEditorThread.sleep.()V": YieldReason.Root, + "com/nokia/mid/ui/TextEditorThread.getNextDirtyEditor.()I": YieldReason.Root, "com/nokia/mid/ui/TextEditor.setFocus.(Z)V": YieldReason.Root, "com/nokia/mid/ui/VKVisibilityNotificationRunnable.sleepUntilVKVisibilityChange.()Z": YieldReason.Root, "com/nokia/mid/s40/bg/BGUtils.waitUserInteraction.()V": YieldReason.Root, diff --git a/midp/gfx.js b/midp/gfx.js index cb0ed164..faa0d1c6 100644 --- a/midp/gfx.js +++ b/midp/gfx.js @@ -1718,23 +1718,16 @@ var currentlyFocusedTextEditor; this.textEditor.setFont(font); }; - Native["com/nokia/mid/ui/TextEditorThread.sleep.()V"] = function() { - asyncImpl("V", new Promise(function(resolve, reject) { - if (!dirtyEditors.length) { - textEditorResolve = resolve; - } else { - resolve(); - } - })); - }; - Native["com/nokia/mid/ui/TextEditorThread.getNextDirtyEditor.()I"] = function() { - if (!dirtyEditors.length) { - console.error("ERROR: getNextDirtyEditor called but no dirty editors"); - return 0; + if (dirtyEditors.length) { + return dirtyEditors.shift(); } - return dirtyEditors.shift(); + asyncImpl("I", new Promise(function(resolve, reject) { + textEditorResolve = function() { + resolve(dirtyEditors.shift()); + } + })); }; var curDisplayableId = 0; From 9ea323d87052af96a8e6ec82eea0a2531ed9760c Mon Sep 17 00:00:00 2001 From: Marco Castelluccio Date: Sun, 1 Feb 2015 12:41:52 +0100 Subject: [PATCH 2/5] Raise priority of the TextEditorThread thread --- java/custom/com/nokia/mid/ui/TextEditor.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/java/custom/com/nokia/mid/ui/TextEditor.java b/java/custom/com/nokia/mid/ui/TextEditor.java index ad71b9c8..e035744d 100644 --- a/java/custom/com/nokia/mid/ui/TextEditor.java +++ b/java/custom/com/nokia/mid/ui/TextEditor.java @@ -3,13 +3,14 @@ package com.nokia.mid.ui; import javax.microedition.lcdui.Font; import java.util.Hashtable; -class TextEditorThread implements Runnable { +class TextEditorThread extends Thread { // We need a thread to be able to wake from js when there is an async js keyboard event. native int getNextDirtyEditor(); Hashtable listeners; TextEditorThread() { + setPriority(Thread.MAX_PRIORITY); listeners = new Hashtable(); } @@ -42,8 +43,7 @@ public class TextEditor extends CanvasItem { if (textEditorThread == null) { textEditorThread = new TextEditorThread(); - Thread t = new Thread(textEditorThread); - t.start(); + textEditorThread.start(); } } From f92ef977923841af4f60d09c2cba60b6746949e1 Mon Sep 17 00:00:00 2001 From: Marco Castelluccio Date: Sun, 1 Feb 2015 12:54:39 +0100 Subject: [PATCH 3/5] Remove myId from TextEditor; make TextEditorListener get the dirty TextEditor without a Hashtable --- java/custom/com/nokia/mid/ui/TextEditor.java | 20 +++++--------------- midp/gfx.js | 11 +++++------ 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/java/custom/com/nokia/mid/ui/TextEditor.java b/java/custom/com/nokia/mid/ui/TextEditor.java index e035744d..eb199a01 100644 --- a/java/custom/com/nokia/mid/ui/TextEditor.java +++ b/java/custom/com/nokia/mid/ui/TextEditor.java @@ -5,26 +5,18 @@ import java.util.Hashtable; class TextEditorThread extends Thread { // We need a thread to be able to wake from js when there is an async js keyboard event. - native int getNextDirtyEditor(); - - Hashtable listeners; + native TextEditor getNextDirtyEditor(); TextEditorThread() { setPriority(Thread.MAX_PRIORITY); - listeners = new Hashtable(); } public void run() { while (true) { - int dirty = getNextDirtyEditor(); - TextEditor t = (TextEditor)listeners.get("" + dirty); - t.myListener.inputAction(t, TextEditorListener.ACTION_CONTENT_CHANGE); + TextEditor dirty = getNextDirtyEditor(); + dirty.myListener.inputAction(dirty, TextEditorListener.ACTION_CONTENT_CHANGE); } } - - void register(int id, TextEditor t) { - listeners.put("" + id, t); - } } public class TextEditor extends CanvasItem { @@ -34,12 +26,11 @@ public class TextEditor extends CanvasItem { protected TextEditorListener myListener; private boolean multiline = false; - private int myId; private static TextEditorThread textEditorThread; private Font font = Font.getDefaultFont(); protected TextEditor(String text, int maxSize, int constraints, int width, int height) { - myId = init(text, maxSize, constraints, width, height); + init(text, maxSize, constraints, width, height); if (textEditorThread == null) { textEditorThread = new TextEditorThread(); @@ -48,7 +39,7 @@ public class TextEditor extends CanvasItem { } // Initialize the native representation. - native private int init(String text, int maxSize, int constraints, int width, int height); + native private void init(String text, int maxSize, int constraints, int width, int height); // Creates a new TextEditor object with the given initial contents, maximum size in characters, constraints and editor size in pixels. public static TextEditor createTextEditor(String text, int maxSize, int constraints, int width, int height) { @@ -184,7 +175,6 @@ public class TextEditor extends CanvasItem { // Sets a listener for content changes in this TextEditor, replacing any previous TextEditorListener. public void setTextEditorListener(TextEditorListener listener) { myListener = listener; - textEditorThread.register(myId, this); } // Returns the multiline state of the TextEditor. diff --git a/midp/gfx.js b/midp/gfx.js index faa0d1c6..562b7dc1 100644 --- a/midp/gfx.js +++ b/midp/gfx.js @@ -1493,15 +1493,15 @@ var currentlyFocusedTextEditor; textEditorResolve = null, dirtyEditors = []; - function wakeTextEditorThread(id) { - dirtyEditors.push(id); + function wakeTextEditorThread(textEditor) { + dirtyEditors.push(textEditor); if (textEditorResolve) { textEditorResolve(); textEditorResolve = null; } } - Native["com/nokia/mid/ui/TextEditor.init.(Ljava/lang/String;IIII)I"] = + Native["com/nokia/mid/ui/TextEditor.init.(Ljava/lang/String;IIII)V"] = function(text, maxSize, constraints, width, height) { if (constraints != 0) { console.warn("TextEditor.constraints not implemented"); @@ -1541,9 +1541,8 @@ var currentlyFocusedTextEditor; this.setCaretPosition(this.textEditor.getContentSize()); this.textEditor.oninput(function(e) { - wakeTextEditorThread(this.textEditorId); + wakeTextEditorThread(this); }.bind(this)); - return textEditorId; }; Native["com/nokia/mid/ui/CanvasItem.attachNativeImpl.()V"] = function() { @@ -1718,7 +1717,7 @@ var currentlyFocusedTextEditor; this.textEditor.setFont(font); }; - Native["com/nokia/mid/ui/TextEditorThread.getNextDirtyEditor.()I"] = function() { + Native["com/nokia/mid/ui/TextEditorThread.getNextDirtyEditor.()Lcom/nokia/mid/ui/TextEditor;"] = function() { if (dirtyEditors.length) { return dirtyEditors.shift(); } From 737ffc9ff66397d6c8fb0c408afbb07dd1047987 Mon Sep 17 00:00:00 2001 From: Marco Castelluccio Date: Sun, 1 Feb 2015 13:15:43 +0100 Subject: [PATCH 4/5] Update getNextDirtyEditor signature --- jit/analyze.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jit/analyze.ts b/jit/analyze.ts index 11ee390f..8dd3d5c7 100644 --- a/jit/analyze.ts +++ b/jit/analyze.ts @@ -39,7 +39,7 @@ module J2ME { "com/ibm/oti/connection/file/FCOutputStream.openOffsetImpl.([BJ)I": YieldReason.Root, "com/sun/midp/io/j2me/storage/RandomAccessStream.open.(Ljava/lang/String;I)I": YieldReason.Root, "javax/microedition/lcdui/ImageDataFactory.createImmutableImageDecodeImage.(Ljavax/microedition/lcdui/ImageData;[BII)V": YieldReason.Root, - "com/nokia/mid/ui/TextEditorThread.getNextDirtyEditor.()I": YieldReason.Root, + "com/nokia/mid/ui/TextEditorThread.getNextDirtyEditor.()Lcom/nokia/mid/ui/TextEditor;": YieldReason.Root, "com/nokia/mid/ui/TextEditor.setFocus.(Z)V": YieldReason.Root, "com/nokia/mid/ui/VKVisibilityNotificationRunnable.sleepUntilVKVisibilityChange.()Z": YieldReason.Root, "com/nokia/mid/s40/bg/BGUtils.waitUserInteraction.()V": YieldReason.Root, From e070f981c44e2d947d001791caca99410ea27735 Mon Sep 17 00:00:00 2001 From: Marco Castelluccio Date: Mon, 2 Feb 2015 12:55:11 +0100 Subject: [PATCH 5/5] Check that dirty.myListener is not null before using it --- java/custom/com/nokia/mid/ui/TextEditor.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/java/custom/com/nokia/mid/ui/TextEditor.java b/java/custom/com/nokia/mid/ui/TextEditor.java index eb199a01..2358e8cc 100644 --- a/java/custom/com/nokia/mid/ui/TextEditor.java +++ b/java/custom/com/nokia/mid/ui/TextEditor.java @@ -14,7 +14,9 @@ class TextEditorThread extends Thread { public void run() { while (true) { TextEditor dirty = getNextDirtyEditor(); - dirty.myListener.inputAction(dirty, TextEditorListener.ACTION_CONTENT_CHANGE); + if (dirty.myListener != null) { + dirty.myListener.inputAction(dirty, TextEditorListener.ACTION_CONTENT_CHANGE); + } } } }