Here is my patch, finally

I threw together a quick static logging class, and switched my new logging to use it. I thought it might be easier to change it to a logging framework later on when done like this. Not sure how big these frameworks are, and if we want to add it to the MO though. We could probably disable them at build time however...
Index: Makefile
===================================================================
--- Makefile (revision 21508)
+++ Makefile (working copy)
@@ -28,6 +28,8 @@
${JAVAC} ${CFLAGS} com/linuxmce/javamo/BD_CP_ShowImage.java
${JAVAC} ${CFLAGS} com/linuxmce/javamo/BD_CP_ShowList.java
${JAVAC} ${CFLAGS} com/linuxmce/javamo/BD_CP_Dummy.java
+ ${JAVAC} ${CFLAGS} com/linuxmce/javamo/log/MOLogger.java
+ ${JAVAC} ${CFLAGS} com/linuxmce/javamo/log/Logger.java
${WTKPATH}/bin/preverify -classpath ${API} com.linuxmce.javamo.${MIDLETNAME}
${WTKPATH}/bin/preverify -classpath ${API} com.linuxmce.javamo.${MIDLETNAME}\$$DrawImageCanvas
${WTKPATH}/bin/preverify -classpath ${API} com.linuxmce.javamo.${MIDLETNAME}\$$ListCommandListener
@@ -47,6 +49,8 @@
${WTKPATH}/bin/preverify -classpath ${API} com.linuxmce.javamo.BD_CP_ShowImage
${WTKPATH}/bin/preverify -classpath ${API} com.linuxmce.javamo.BD_CP_ShowList
${WTKPATH}/bin/preverify -classpath ${API} com.linuxmce.javamo.BD_CP_Dummy
+ ${WTKPATH}/bin/preverify -classpath ${API} com.linuxmce.javamo.log.MOLogger
+ ${WTKPATH}/bin/preverify -classpath ${API} com.linuxmce.javamo.log.Logger
mkdir -p output/com/linuxmce/javamo/res
cp res/icon.png output/com/linuxmce/javamo/res
cd output && jar cvfm ${MIDLETNAME}.jar Manifest.mf ./com
Index: com/linuxmce/javamo/BDCommandProcessor.java
===================================================================
--- com/linuxmce/javamo/BDCommandProcessor.java (revision 21508)
+++ com/linuxmce/javamo/BDCommandProcessor.java (working copy)
@@ -19,11 +19,14 @@
package com.linuxmce.javamo;
-import javax.microedition.lcdui.*;
-import javax.microedition.midlet.*;
import javax.microedition.io.StreamConnection;
+
+import com.linuxmce.javamo.log.MOLogger;
+
import java.util.*;
-import java.io.*;
+import java.io.OutputStream;
+import java.io.InputStream;
+import java.io.IOException;
/**
* Class to process BDCommands
@@ -46,12 +49,13 @@
this.mJavaMO = javamo;
this.m_connection = connection;
connected = true;
- commandQueue = new Vector();
+ commandQueue = new Vector(20);
try {
m_os = m_connection.openOutputStream();
m_is = m_connection.openInputStream();
} catch (IOException e) {
- System.err.println(e);
+ if (MOLogger.DEBUG)
+ MOLogger.log(0, e.toString());
}
processorThread = new Thread(this);
processorThread.start();
@@ -92,29 +96,25 @@
m_os.flush();
System.out.println("SendLong:" + output);
} catch (IOException e) {
- System.err.println(e);
+ if (MOLogger.DEBUG)
+ MOLogger.log(0, e.toString());
connected = false;
}
}
- private long ReceiveLong() {
+ private long ReceiveLong() throws IOException {
long output = 0;
int a1,a2,a3,a4;
- try {
- a1 = (int)m_is.read();
- // System.out.println("ReceiveLong: got byte: " + a1);
- a2 = (int)m_is.read();
- // System.out.println("ReceiveLong: got byte: " + a2);
- a3 = (int)m_is.read();
- // System.out.println("ReceiveLong: got byte: " + a3);
- a4 = (int)m_is.read();
- // System.out.println("ReceiveLong: got byte: " + a4);
- output = (a1) + (a2*0x100) + (a3*0x10000) + (a4*0x1000000);
- System.out.println("ReceiveLong: long result: " + output);
- } catch (IOException e) {
- System.err.println(e);
- connected = false;
- }
+ a1 = (int)m_is.read();
+ // System.out.println("ReceiveLong: got byte: " + a1);
+ a2 = (int)m_is.read();
+ // System.out.println("ReceiveLong: got byte: " + a2);
+ a3 = (int)m_is.read();
+ // System.out.println("ReceiveLong: got byte: " + a3);
+ a4 = (int)m_is.read();
+ // System.out.println("ReceiveLong: got byte: " + a4);
+ output = (a1) + (a2*0x100) + (a3*0x10000) + (a4*0x1000000);
+ System.out.println("ReceiveLong: long result: " + output);
return output;
}
@@ -126,27 +126,41 @@
}
m_os.flush();
} catch (IOException e) {
- System.err.println(e);
+ if (MOLogger.DEBUG)
+ MOLogger.log(0, e.toString());
connected = false;
}
}
+ private void readFully(byte[] data) throws IOException {
+ int i = 0;
+ while(i < data.length) {
+ int count = m_is.read(data, i, data.length - i);
+ if (count > 0) {
+ i += count;
+ }
+ }
+ }
+
private byte[] ReceiveData() {
int size;
- size = (int)ReceiveLong();
+ try {
+ size = (int)ReceiveLong();
- if (size < 0) {
- size=0;
+ if (size < 0) {
+ size=0;
+ connected = false;
+ }
+ } catch (IOException e) {
connected = false;
+ size = 0;
}
-
byte[] data = new byte[size];
try {
- for (int i=0;i<size;i++) {
- data[i] = (byte)m_is.read();
- }
+ readFully(data);
} catch (IOException e) {
- System.err.println(e);
+ if (MOLogger.DEBUG)
+ MOLogger.log(0, "BDCommandProcessor.ReceiveData() : " + e.toString());
connected = false;
}
return data;
@@ -182,7 +196,6 @@
public void run() {
BDCommand l_BDResponseCommand;
- int data;
int counter = 0;
long commandtype;
BD_PC_WhatDoYouHave l_BD_PC_WhatDoYouHave;
@@ -199,7 +212,7 @@
l_BD_PC_ReportMyVersion = null;
/* set quality to 100 (png) */
- commandToSend = new BD_PC_SetImageQuality(100);
+ commandToSend = new BD_PC_SetImageQuality(30);
commandToSend.ConvertCommandToBinary();
sendCommand(commandToSend);
commandToSend.ReceiveData(ReceiveData());
@@ -207,57 +220,75 @@
commandToSend = null;
while (connected) {
-
- if (commandQueue.isEmpty()) {
- if (counter++ == 5) {
- counter = 0;
- /* no outgoing commands in the queue, send BD_PC_WHAT_DO_YOU_HAVE */
- l_BD_PC_WhatDoYouHave = new BD_PC_WhatDoYouHave();
- l_BD_PC_WhatDoYouHave.ConvertCommandToBinary();
- sendCommand(l_BD_PC_WhatDoYouHave);
-
- /* if commandtype != 100 (BD_CP_WE_HAVE_NOTHING) build command and handle it */
- commandtype = ReceiveLong();
- if (commandtype != 100) {
- l_BDResponseCommand = buildCommandFromData(commandtype);
- l_BDResponseCommand.ReceiveData(ReceiveData());
- l_BDResponseCommand.parseCommand();
- l_BDResponseCommand.processCommand(mJavaMO);
- l_BDResponseCommand.ConvertAckToBinary();
- sendAck(l_BDResponseCommand);
- l_BDResponseCommand = null;
- } else {
- /* handle BD_CP_WE_HAVE_NOTHING */
- ReceiveLong();
- sendAck(l_BD_PC_WhatDoYouHave);
+ try {
+ if (commandQueue.isEmpty()) {
+ if (counter++ == 5) {
+ counter = 0;
+ /* no outgoing commands in the queue, send BD_PC_WHAT_DO_YOU_HAVE */
+ l_BD_PC_WhatDoYouHave = new BD_PC_WhatDoYouHave();
+ l_BD_PC_WhatDoYouHave.ConvertCommandToBinary();
+ sendCommand(l_BD_PC_WhatDoYouHave);
+
+ commandtype = ReceiveLong();
+ // If read return -1 (EOF) we should not try to do anything
+ if (commandtype >= 0) {
+ /* if commandtype != 100 (BD_CP_WE_HAVE_NOTHING) build command and handle it */
+ if (commandtype != 100) {
+ mJavaMO.addMessage("Got command " + commandtype);
+ l_BDResponseCommand = buildCommandFromData(commandtype);
+ l_BDResponseCommand.ReceiveData(ReceiveData());
+ l_BDResponseCommand.parseCommand();
+ l_BDResponseCommand.processCommand(mJavaMO);
+ l_BDResponseCommand.ConvertAckToBinary();
+ sendAck(l_BDResponseCommand);
+ l_BDResponseCommand = null;
+
+ // We got one command, lets see if there is anything else right away
+ counter = 5;
+ } else {
+ /* handle BD_CP_WE_HAVE_NOTHING */
+ ReceiveLong();
+ sendAck(l_BD_PC_WhatDoYouHave);
+ }
+ }
+ l_BD_PC_WhatDoYouHave = null;
}
- l_BD_PC_WhatDoYouHave = null;
- // this.currentThread().sleep(100);
- }
- /* go to sleep to save power */
- try {
- processorThread.sleep(100);
- } catch (InterruptedException e) {
+ /* go to sleep to save power */
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ if (MOLogger.DEBUG)
+ MOLogger.log(0, "BDCommandProcessor.run(): " + e.getMessage());
+ }
+ } else {
+ /* pop command from the queue */
+ commandToSend = (BDCommand) commandQueue.firstElement();
+ commandQueue.removeElement((Object) commandToSend);
+
+ /* send the command */
+ commandToSend.ConvertCommandToBinary();
+ sendCommand(commandToSend);
+ commandToSend.ReceiveData(ReceiveData());
+ commandToSend.parseAck();
+ commandToSend = null;
+
+ /* send what do you have without further sleeping after sending a command */
+ counter = 5;
}
-
- } else {
- /* pop command from the queue */
- commandToSend = (BDCommand) commandQueue.firstElement();
- commandQueue.removeElement((Object) commandToSend);
-
- /* send the command */
- commandToSend.ConvertCommandToBinary();
- sendCommand(commandToSend);
- commandToSend.ReceiveData(ReceiveData());
- commandToSend.parseAck();
- commandToSend = null;
-
- /* send what do you have without further sleeping after sending a command */
- counter = 5;
+ } catch (IOException e) {
+ // Catch IOException, so we skip processing relying on received data
+ if (MOLogger.DEBUG)
+ MOLogger.log(0, "BTCommandProcessor.run() : " + e.toString());
+ connected = false;
+ } catch (Exception e) {
+ if (MOLogger.DEBUG)
+ MOLogger.log(0, "BTCommandProcessor.run() : " + e.toString());
+ // Not necessarily disconnected, but we should probably restart
+ connected = false;
}
-
-
}
+ if (MOLogger.DEBUG)
+ MOLogger.log(0, "BDCommandProcessor.run(): Not connected anymore");
}
Index: com/linuxmce/javamo/BTServer.java
===================================================================
--- com/linuxmce/javamo/BTServer.java (revision 21508)
+++ com/linuxmce/javamo/BTServer.java (working copy)
@@ -37,6 +37,8 @@
import javax.bluetooth.*;
+import com.linuxmce.javamo.log.MOLogger;
+
/**
* Bluetooth server for J2ME using JSR82
*/
@@ -119,7 +121,7 @@
/* listen for connections */
conn = notifier.acceptAndOpen();
- parent.showAlert("CONNECT",3000);
+ parent.addMessage("Connected");
/* BD Command processor to handle commands */
System.out.println("BTServer: starting BDCommandProcessor...");
@@ -132,16 +134,27 @@
System.out.println("BTServer: waiting for BDCommandProcessor to finish...");
m_BDCommandProcessor.processorThread.join();
} catch (InterruptedException e) {
+ if (MOLogger.DEBUG)
+ MOLogger.log(0, "BTServer.run() : " + e.getMessage());
}
- m_BDCommandProcessor = null;
- conn.close();
- parent.showAlert("DISCONNECT",3000);
- parent.showForm();
-
} catch (IOException e) {
// wrong client or interrupted - continue anyway
- continue;
+ if (MOLogger.DEBUG)
+ MOLogger.log(0, "BTServer.run() : " + e.getMessage());
+ } finally {
+ if (MOLogger.DEBUG)
+ MOLogger.log(0, "Disconnect!");
+ parent.showForm();
+ m_BDCommandProcessor = null;
+ if (conn != null) {
+ try {
+ conn.close();
+ } catch (Exception e) {
+ if (MOLogger.DEBUG)
+ MOLogger.log(0, "BTServer.run() : " + e.toString());
+ }
+ }
}
// processor.addConnection(conn);
Index: com/linuxmce/javamo/JavaMO.java
===================================================================
--- com/linuxmce/javamo/JavaMO.java (revision 21508)
+++ com/linuxmce/javamo/JavaMO.java (working copy)
@@ -22,15 +22,8 @@
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-
-import javax.microedition.io.Connector;
-import javax.microedition.io.StreamConnection;
-import javax.microedition.io.StreamConnectionNotifier;
-import javax.microedition.rms.*;
+import com.linuxmce.javamo.log.Logger;
+import com.linuxmce.javamo.log.MOLogger;
/**
* Java Mobile Orbiter for LinuxMCE/Plutohome
@@ -40,7 +33,7 @@
*/
public class JavaMO
extends MIDlet
- implements CommandListener {
+ implements CommandListener, Logger {
private Canvas mCanvas;
private Image mImage;
@@ -57,6 +50,8 @@
mMainForm = new Form("JavaMO");
+ MOLogger.setLogger(this);
+
/* check for jsr082 */
try {
Class.forName("javax.bluetooth.LocalDevice");
@@ -129,22 +124,22 @@
} else {
listActive = false;
}
+ turnBackLightOn();
}
/**
* Shows a list with media elements
*/
public void showList(String[] list) {
- mList = new List ("Media", List.IMPLICIT);
- for (int i=0; i < list.length; i++) {
- mList.append(list[i], null);
- }
- // Command selectCommand = new Command("Select", Command.ITEM, 1);
- // mList.setSelectCommand(selectCommand);
+ mList = new List ("Media", List.IMPLICIT, list, null);
+ Command selectCommand = new Command("Select", Command.ITEM, 1);
+ mList.setSelectCommand(selectCommand);
mList.setCommandListener(new ListCommandListener());
+ mList.addCommand(new Command("Exit", Command.BACK, 2));
+
Display.getDisplay(this).setCurrent(mList);
listActive = true;
-
+ turnBackLightOn();
}
/**
@@ -176,6 +171,8 @@
return 32; /* BUTTON_Asterisk_CONST */
case Canvas.KEY_POUND:
return 33; /* BUTTON_Pound_CONST */
+ case -6: // Sony Ericsson left soft key
+ return 30; // Soft left key
case -7: /* mpp sim right soft key */
case -8: /* Nokia N73 C key */
return 26; /* BUTTON_Phone_C_CONST */
@@ -247,6 +244,8 @@
* Canvas class to show the image on the screen and report keystrokes back
*/
private class DrawImageCanvas extends Canvas {
+ private int lastKeyCode = 0;
+
public void paint(Graphics g) {
g.drawImage(mImage, 0, 0, Graphics.TOP | Graphics.LEFT);
}
@@ -260,18 +259,24 @@
}
protected void keyReleased(int keyCode) {
+ if (lastKeyCode == 0) {
//if (keyCode > 0) {
// commandProcessor.addCommandToQueue(new BD_PC_KeyWasPressed(translateKey(keyCode),0));
commandProcessor.addCommandToQueue(new BD_PC_KeyWasPressed(translateKey(keyCode),2));
System.out.println("keyReleased " + ((int)keyCode));
//} else {
// System.out.println("keyReleased action " + getGameAction(keyCode));
- //}
+ //}
+ }
+ lastKeyCode = 0;
}
protected void keyRepeated(int keyCode) {
- commandProcessor.addCommandToQueue(new BD_PC_KeyWasPressed(translateRepeatedKey(keyCode),1));
- System.out.println("repeatedKeyReleased " + ((int)keyCode));
+ if (keyCode != lastKeyCode) {
+ lastKeyCode = keyCode;
+ commandProcessor.addCommandToQueue(new BD_PC_KeyWasPressed(translateRepeatedKey(keyCode),1));
+ System.out.println("repeatedKeyReleased " + ((int)keyCode));
+ }
}
protected void pointerPressed(int x, int y) {
@@ -281,6 +286,24 @@
}
}
+ public void addMessage(String message) {
+ while (mMainForm.size() > 20) {
+ mMainForm.delete(mMainForm.size()-1);
+ }
+ mMainForm.insert(0, new StringItem(null, message));
+ }
+
+ public void log(int level, String message) {
+ addMessage(message);
+ }
+
+ /**
+ * Turns backlign on
+ */
+ public void turnBackLightOn() {
+ Display.getDisplay(this).flashBacklight(1);
+ }
+
/**
* Command listener for the list
*/
@@ -289,7 +312,7 @@
public void commandAction (Command c, Displayable d) {
long item = -1;
List list;
- if (c == List.SELECT_COMMAND) {
+ if (c == List.SELECT_COMMAND || c.getLabel().equals("Select")) {
listActive = false;
list = (List) d;
item = list.getSelectedIndex();
@@ -299,6 +322,10 @@
commandProcessor.addCommandToQueue(new BD_PC_SelectedFromList(item));
// send enter
commandProcessor.addCommandToQueue(new BD_PC_KeyWasPressed(5,2));
+ } else if (c.getLabel().equals("Exit")) {
+ listActive = false;
+ // Send C key
+ commandProcessor.addCommandToQueue(new BD_PC_KeyWasPressed(26,2));
}
}
}
Index: com/linuxmce/javamo/log/Logger.java
===================================================================
--- com/linuxmce/javamo/log/Logger.java (revision 0)
+++ com/linuxmce/javamo/log/Logger.java (revision 0)
@@ -0,0 +1,6 @@
+package com.linuxmce.javamo.log;
+
+public interface Logger {
+
+ public void log(int level, String message);
+}
Index: com/linuxmce/javamo/log/MOLogger.java
===================================================================
--- com/linuxmce/javamo/log/MOLogger.java (revision 0)
+++ com/linuxmce/javamo/log/MOLogger.java (revision 0)
@@ -0,0 +1,20 @@
+package com.linuxmce.javamo.log;
+
+public class MOLogger {
+
+ static public final boolean DEBUG = true;
+ static Logger logger;
+
+ static public void setLogger(Logger logr) {
+ logger = logr;
+ }
+
+ static public void log(int level, String message) {
+ if (logger != null) {
+ logger.log(level, message);
+ } else {
+ System.out.println(message);
+ }
+ }
+
+}
Hari, just ask if you see anything fishy.
crudpuppy, the UI displayed is mostly just images sent over bluetooth from the MD. As such, you need to use the HADesigner to change the orbiter GUI. If that was what you were thinking of?
br, sambuca