Revision: 731
http://swingme.svn.sourceforge.net/swingme/?rev=731&view=rev
Author: janeroski
Date: 2010-03-24 13:39:04 +0000 (Wed, 24 Mar 2010)
Log Message:
-----------
Check for more than one MIDlet
Modified Paths:
--------------
AndroidME/src_Android/net/yura/android/AndroidMeMIDlet.java
AndroidME/src_MIDP/javax/microedition/lcdui/Canvas.java
Modified: AndroidME/src_Android/net/yura/android/AndroidMeMIDlet.java
===================================================================
--- AndroidME/src_Android/net/yura/android/AndroidMeMIDlet.java 2010-03-23 17:32:26 UTC (rev 730)
+++ AndroidME/src_Android/net/yura/android/AndroidMeMIDlet.java 2010-03-24 13:39:04 UTC (rev 731)
@@ -1,9 +1,7 @@
package net.yura.android;
-import java.io.IOException;
import java.io.InputStream;
-import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Properties;
@@ -16,7 +14,6 @@
import javax.microedition.midlet.MIDlet;
import net.yura.android.lcdui.Toolkit;
-import net.yura.android.log.LogOutputStream;
import android.app.Activity;
import android.os.Bundle;
@@ -26,337 +23,402 @@
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+import android.widget.AdapterView.OnItemClickListener;
-public class AndroidMeMIDlet extends Activity implements Toolkit {
- private static final String MIDLET_PROPERTY = "midlet";
+public class AndroidMeMIDlet extends Activity implements Toolkit, OnItemClickListener {
- private MIDlet midlet;
- private View defaultView;
- private Handler handler;
- private Thread eventThread;
- private Object lock = new Object();
+ private MIDlet midlet;
+ private Vector<String[]> jadMidlets;
+ private View defaultView;
+ private Handler handler;
+ private Thread eventThread;
+ private Object lock = new Object();
- public static AndroidMeMIDlet DEFAULT_ACTIVITY;
+ public static AndroidMeMIDlet DEFAULT_ACTIVITY;
- public AndroidMeMIDlet() {
- DEFAULT_ACTIVITY = this;
- }
+ public AndroidMeMIDlet() {
+ DEFAULT_ACTIVITY = this;
+ }
- public MIDlet getMIDlet() {
- return this.midlet;
- }
+ public MIDlet getMIDlet() {
+ return this.midlet;
+ }
- public Handler getHandler() {
- return handler;
- }
+ public Handler getHandler() {
+ return handler;
+ }
- public void invokeAndWait(final Runnable runnable) {
- if (Thread.currentThread() == this.eventThread) {
- runnable.run();
- } else {
- Runnable r = new Runnable() {
- public void run() {
- synchronized (AndroidMeMIDlet.this.lock) {
- runnable.run();
- AndroidMeMIDlet.this.lock.notify();
- }
- }
- };
- synchronized (this.lock) {
- this.handler.post(r);
- try {
- this.lock.wait();
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- }
- }
- }
- }
+ public void invokeAndWait(final Runnable runnable) {
+ if (Thread.currentThread() == this.eventThread) {
+ runnable.run();
+ } else {
+ Runnable r = new Runnable() {
+ public void run() {
+ synchronized (AndroidMeMIDlet.this.lock) {
+ runnable.run();
+ AndroidMeMIDlet.this.lock.notify();
+ }
+ }
+ };
+ synchronized (this.lock) {
+ this.handler.post(r);
+ try {
+ this.lock.wait();
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ }
+ }
+ }
+ }
- @Override
- protected void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- this.handler = new Handler();
- this.eventThread = Thread.currentThread();
+ @Override
+ protected void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+ this.handler = new Handler();
+ this.eventThread = Thread.currentThread();
- View splash = this.getLayoutInflater().inflate(R.layout.main, null, false);
- this.defaultView = splash;
- setContentView(splash);
+ View splash = this.getLayoutInflater().inflate(R.layout.main, null, false);
+ this.defaultView = splash;
+ setContentView(splash);
-// PrintStream log = new PrintStream(new LogOutputStream("AndroidMe"));
-// System.setErr(log);
-// System.setOut(log);
- }
+// PrintStream log = new PrintStream(new LogOutputStream("AndroidMe"));
+// System.setErr(log);
+// System.setOut(log);
+ }
- private MIDlet createMIDlet() {
- System.setProperty("microedition.platform", "microemulator-android");
- System.setProperty("microedition.locale", Locale.getDefault().toString());
- System.setProperty("microedition.configuration", "CLDC-1.1");
- System.setProperty("microedition.profiles", "MIDP-2.0");
+ private void startMIDlet(final String midletClassName) {
+ Thread thread = new Thread() {
+ public void run() {
+ while (defaultView.getWidth() == 0) {
+ try {
+ System.out.println("Waiting for view...");
+ Thread.sleep(100);
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
- Properties properties = new Properties();
+ try {
+ MIDlet midlet = createMIDlet(midletClassName);
+ AndroidMeMIDlet.this.midlet = midlet;
- try {
- InputStream is = getAssets().open("BadooMobile.jad");
- properties.load(is);
+ if (midlet != null) {
+ midlet.doStartApp();
+ }
+ } catch (Throwable ex) {
+ ex.printStackTrace();
+ }
+ }
+ };
- } catch (Throwable e) {
- e.printStackTrace();
- }
+ thread.start();
+ }
- System.out.println(">>>1 " + System.getProperty("microedition.platform"));
+ private MIDlet createMIDlet(String midletClassName) {
- // create a new class loader that correctly handles getResourceAsStream!
+ Properties properties = new Properties();
+ System.setProperty("microedition.platform", "microemulator-android");
+ System.setProperty("microedition.locale", Locale.getDefault().toString());
+ System.setProperty("microedition.configuration", "CLDC-1.1");
+ System.setProperty("microedition.profiles", "MIDP-2.0");
+ try {
+ InputStream is = getAssets().open("BadooMobile.jad");
+ properties.load(is);
- properties.put(MIDLET_PROPERTY, "net.yura.mobile.test.MyMidlet");
- // properties.put(MIDLET_PROPERTY, "com.badoo.locator.BadooMidlet");
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
-// System.out.println(">>> " + net.java.dev.lwuit.speed.SpeedMIDlet.class);
-// properties.put(MIDLET_PROPERTY, "net.java.dev.lwuit.speed.SpeedMIDlet");
+ System.out.println(">>>1 " + System.getProperty("microedition.platform"));
- String midletClassName = properties.getProperty(MIDLET_PROPERTY);
- ClassLoader classLoader;
- classLoader = this.getClassLoader();
- MIDlet midlet;
- MIDlet.DEFAULT_ACTIVITY = this;
- MIDlet.DEFAULT_TOOLKIT = this;
- MIDlet.DEFAULT_APPLICATION_PROPERTIES = properties;
- try {
- Class midletClass = Class.forName(midletClassName, true, classLoader);
- midlet = (MIDlet) midletClass.newInstance();
- } catch (Exception ex) {
- throw new RuntimeException("unable to load class "
- + midletClassName, ex);
- }
- return midlet;
- }
+ // midletClassName = "net.yura.mobile.test.MyMidlet";
+ // midletClassName = "com.badoo.locator.BadooMidlet";
+ // midletClassName = "net.java.dev.lwuit.speed.SpeedMIDlet";
- @Override
- protected void onDestroy() {
- try {
- if (this.midlet != null) {
- this.midlet.doDestroyApp(true);
- this.midlet = null;
- }
- } catch (Exception ex) {
- throw new RuntimeException("unable to destroy", ex);
- }
- // this.resources.getAssets().release();
- super.onDestroy();
- }
- @Override
- protected void onPause() {
- try {
- if (this.midlet != null) {
- this.midlet.doPauseApp();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- throw new RuntimeException("unable to freeze app", ex);
- }
- super.onPause();
- }
+ jadMidlets = new Vector<String[]>();
+ if (midletClassName == null) {
- @Override
- public void onStop() {
- // TODO: JP
- onDestroy();
- }
+ int count = 0;
+ while (true) {
+ count++;
+ String midletProp = properties.getProperty("MIDlet-" + count);
+ System.out.println(">>>>>>>>> ----------- " + midletProp);
+ if (midletProp == null) {
+ break;
+ }
+ try {
+ int firstComma = midletProp.indexOf(',');
+ int lastComma = midletProp.lastIndexOf(',');
- @Override
- protected void onResume() {
- super.onResume();
+ String midletName = midletProp.substring(0, firstComma).trim();
+ String iconName = midletProp.substring(firstComma + 1, lastComma).trim();
+ String className = midletProp.substring(lastComma + 1).trim();
- if (this.midlet == null) {
- Thread thread = new Thread() {
- public void run() {
- while (defaultView.getWidth() == 0) {
- try {
- Thread.sleep(500);
- System.out.println("W:" + defaultView.getWidth()
- + ",H:" + defaultView.getHeight() + ",MW:"
- + defaultView.getMeasuredWidth() + ",MH:"
- + defaultView.getMeasuredHeight());
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- MIDlet midlet = createMIDlet();
- AndroidMeMIDlet.this.midlet = midlet;
- try {
- if (midlet != null) {
- midlet.doStartApp();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- };
- thread.start();
- } else {
- try {
- this.midlet.doStartApp();
- } catch (Exception ex) {
- ex.printStackTrace();
- throw new RuntimeException("couldn't start MIDlet");
- }
- }
+ String[] midletEntry = {midletName, iconName, className};
+ jadMidlets.add(midletEntry);
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
- }
+ if (jadMidlets.size() == 1) {
+ midletClassName = jadMidlets.elementAt(0)[2];
+ }
+ else if (jadMidlets.size() > 1) {
+ ListView listView = new ListView(this);
+ listView.setOnItemClickListener(this);
- @Override
- public boolean onPrepareOptionsMenu(Menu menu) {
+ String[] listValues = new String[jadMidlets.size()];
+ for (int i = 0; i < jadMidlets.size(); i++) {
+ listValues[i] = jadMidlets.elementAt(i)[0];
+ }
- boolean result = false;
- Display display = Display.getDisplay(midlet);
- Displayable current = display.getCurrent();
- if (current != null) {
- // load the menu items
- menu.clear();
- Vector<Command> commands = current.getCommands();
- for (int i = 0; i < commands.size(); i++) {
- result = true;
- Command cmd = commands.get(i);
- if (cmd.getCommandType() != Command.BACK && cmd.getCommandType() != Command.EXIT) {
- menu.addSubMenu(Menu.NONE, i + Menu.FIRST, Menu.NONE, cmd.getLabel());
- }
- }
- }
- return result;
- }
+ listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listValues));
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- Display display = Display.getDisplay(midlet);
- Displayable current = display.getCurrent();
- if (current != null) {
- Vector<Command> commands = current.getCommands();
+ this.defaultView = listView;
+ invokeAndWait(new Runnable() {
+ public void run() {
+ setContentView(defaultView);
+ }
+ });
+ }
+ }
- int commandIndex = item.getItemId() - Menu.FIRST;
- Command c = commands.get(commandIndex);
- CommandListener l = current.getCommandListener();
+ MIDlet midlet = null;
+ if (midletClassName != null) {
+ // create a new class loader that correctly handles getResourceAsStream!
+ ClassLoader classLoader = this.getClassLoader();
- if (c != null && l != null) {
- l.commandAction(c, current);
- return true;
- }
- }
+ MIDlet.DEFAULT_ACTIVITY = this;
+ MIDlet.DEFAULT_TOOLKIT = this;
+ MIDlet.DEFAULT_APPLICATION_PROPERTIES = properties;
- return false;
- }
+ try {
+ Class midletClass = Class.forName(midletClassName, true, classLoader);
+ midlet = (MIDlet) midletClass.newInstance();
+ } catch (Exception ex) {
+ throw new RuntimeException("unable to load class "
+ + midletClassName, ex);
+ }
+ }
-// @Override
-// public boolean onCreateOptionsMenu(Menu menu) {
-// if (this.midlet != null) {
-// this.midlet.setMenu(menu);
-// } else {
-// this.menu = menu;
-// }
-// return super.onCreateOptionsMenu(menu);
-// }
+ return midlet;
+ }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
+ @Override
+ protected void onDestroy() {
+ try {
+ if (this.midlet != null) {
+ this.midlet.doDestroyApp(true);
+ this.midlet = null;
+ }
+ } catch (Exception ex) {
+ throw new RuntimeException("unable to destroy", ex);
+ }
+ // this.resources.getAssets().release();
+ super.onDestroy();
+ }
- Displayable displayable = Display.getDisplay(this.midlet).getCurrent();
+ @Override
+ protected void onPause() {
+ try {
+ if (this.midlet != null) {
+ this.midlet.doPauseApp();
+ }
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ throw new RuntimeException("unable to freeze app", ex);
+ }
+ super.onPause();
+ }
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- // we need to see if there is a back option in the current display
- for (Command c : displayable.getCommands()) {
- if (c.getCommandType() == Command.BACK) {
- // manually call the back operation: YECH!
- displayable.getCommandListener().commandAction(c,
- displayable);
- return true;
- }
- }
- }
+ @Override
+ public void onStop() {
+ // TODO: JP
+ onDestroy();
+ }
- if (displayable instanceof OnKeyListener) {
- OnKeyListener keyListener = (OnKeyListener) displayable;
- keyListener.onKey(null, keyCode, event);
- }
- return super.onKeyDown(keyCode, event);
- }
+ @Override
+ protected void onResume() {
+ super.onResume();
- public int getResourceId(String resourceName) {
- // slow, slow, slow (cache?)
- // mmmmm, introspection
- String[] splits = resourceName.split("\\.");
- Class c = R.class;
- for (int i = 0; i < splits.length; i++) {
- String name = splits[i];
- if (i < splits.length - 1) {
- Class[] childClasses = c.getDeclaredClasses();
- Class found = null;
- for (int j = 0; j < childClasses.length; j++) {
- Class childClass = childClasses[j];
- String separator;
+ try {
+ if (midlet == null) {
+ startMIDlet(null);
+ }
+ else {
+ midlet.doStartApp();
+ }
+ } catch (Throwable ex) {
+ ex.printStackTrace();
+ }
+ }
- if (childClass.getName().endsWith("$" + name)) {
- found = childClass;
- break;
- }
- }
- if (found == null) {
- throw new RuntimeException("no class " + resourceName + "("
- + name + ") in " + c.getName());
- } else {
- c = found;
- }
- } else {
- try {
- Field field = c.getField(name);
- return field.getInt(null);
- } catch (Exception ex) {
- throw new RuntimeException("no field " + name + " in "
- + c.getName());
- }
- }
- }
- throw new RuntimeException("no resource " + resourceName);
- }
+ @Override
+ public boolean onPrepareOptionsMenu(Menu menu) {
- public int getScreenHeight() {
- return this.defaultView.getHeight();
- }
+ boolean result = false;
+ Display display = Display.getDisplay(midlet);
+ Displayable current = display.getCurrent();
+ if (current != null) {
+ // load the menu items
+ menu.clear();
+ Vector<Command> commands = current.getCommands();
+ for (int i = 0; i < commands.size(); i++) {
+ result = true;
+ Command cmd = commands.get(i);
+ if (cmd.getCommandType() != Command.BACK && cmd.getCommandType() != Command.EXIT) {
+ menu.addSubMenu(Menu.NONE, i + Menu.FIRST, Menu.NONE, cmd.getLabel());
+ }
+ }
+ }
+ return result;
+ }
- public int getScreenWidth() {
- return this.defaultView.getWidth();
- }
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ Display display = Display.getDisplay(midlet);
+ Displayable current = display.getCurrent();
+ if (current != null) {
+ Vector<Command> commands = current.getCommands();
- public View inflate(int resourceId) {
- return this.getLayoutInflater().inflate(resourceId, null, false);
- }
+ int commandIndex = item.getItemId() - Menu.FIRST;
+ Command c = commands.get(commandIndex);
+ CommandListener l = current.getCommandListener();
- public static InputStream getResourceAsStream(Class origClass, String name)
- throws IOException {
+ if (c != null && l != null) {
+ l.commandAction(c, current);
+ return true;
+ }
+ }
- long time = System.currentTimeMillis();
- System.out.println(">>> getResourceAsStream (" + origClass.getName() + ")" + name);
- long time2 = System.currentTimeMillis() - time;
+ return false;
+ }
- InputStream is = null;
- try {
- if (name.startsWith("/")) {
- name = name.substring(1);
- }
+// @Override
+// public boolean onCreateOptionsMenu(Menu menu) {
+// if (this.midlet != null) {
+// this.midlet.setMenu(menu);
+// } else {
+// this.menu = menu;
+// }
+// return super.onCreateOptionsMenu(menu);
+// }
- is = DEFAULT_ACTIVITY.getAssets().open(name);
- } catch (Throwable e) {
- // e.printStackTrace();
- }
+ @Override
+ public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (is == null) {
- long elapsed = System.currentTimeMillis() - time;
- System.out.println(">>> getResourceAsStream: NOT FOUND. " + time2 + " " + elapsed);
- }
+ Displayable displayable = Display.getDisplay(this.midlet).getCurrent();
- return is;
- }
+ if (keyCode == KeyEvent.KEYCODE_BACK) {
+ // we need to see if there is a back option in the current display
+ for (Command c : displayable.getCommands()) {
+ if (c.getCommandType() == Command.BACK) {
+ // manually call the back operation: YECH!
+ displayable.getCommandListener().commandAction(c,
+ displayable);
+ return true;
+ }
+ }
+ }
+ if (displayable instanceof OnKeyListener) {
+ OnKeyListener keyListener = (OnKeyListener) displayable;
+ keyListener.onKey(null, keyCode, event);
+ }
+
+ return super.onKeyDown(keyCode, event);
+ }
+
+ public int getResourceId(String resourceName) {
+ // slow, slow, slow (cache?)
+ // mmmmm, introspection
+ String[] splits = resourceName.split("\\.");
+ Class c = R.class;
+ for (int i = 0; i < splits.length; i++) {
+ String name = splits[i];
+ if (i < splits.length - 1) {
+ Class[] childClasses = c.getDeclaredClasses();
+ Class found = null;
+ for (int j = 0; j < childClasses.length; j++) {
+ Class childClass = childClasses[j];
+ String separator;
+
+ if (childClass.getName().endsWith("$" + name)) {
+ found = childClass;
+ break;
+ }
+ }
+ if (found == null) {
+ throw new RuntimeException("no class " + resourceName + "("
+ + name + ") in " + c.getName());
+ } else {
+ c = found;
+ }
+ } else {
+ try {
+ Field field = c.getField(name);
+ return field.getInt(null);
+ } catch (Exception ex) {
+ throw new RuntimeException("no field " + name + " in "
+ + c.getName());
+ }
+ }
+ }
+ throw new RuntimeException("no resource " + resourceName);
+ }
+
+ public int getScreenHeight() {
+ return this.defaultView.getHeight();
+ }
+
+ public int getScreenWidth() {
+ return this.defaultView.getWidth();
+ }
+
+ public View inflate(int resourceId) {
+ return this.getLayoutInflater().inflate(resourceId, null, false);
+ }
+
+ public static InputStream getResourceAsStream(Class origClass, String name)
+ {
+
+ long time = System.currentTimeMillis();
+ System.out.println(">>> getResourceAsStream (" + origClass.getName() + ")" + name);
+ long time2 = System.currentTimeMillis() - time;
+
+ InputStream is = null;
+ try {
+ if (name.startsWith("/")) {
+ name = name.substring(1);
+ }
+
+ is = DEFAULT_ACTIVITY.getAssets().open(name);
+ } catch (Throwable e) {
+ // e.printStackTrace();
+ }
+
+ if (is == null) {
+ long elapsed = System.currentTimeMillis() - time;
+ System.out.println(">>> getResourceAsStream: NOT FOUND. " + time2 + " " + elapsed);
+ }
+
+ return is;
+ }
+
+ //@Override
+ public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
+ if (position >= 0) {
+ String midletClassName = jadMidlets.elementAt(position)[2];
+ startMIDlet(midletClassName);
+ }
+ }
+
}
Modified: AndroidME/src_MIDP/javax/microedition/lcdui/Canvas.java
===================================================================
--- AndroidME/src_MIDP/javax/microedition/lcdui/Canvas.java 2010-03-23 17:32:26 UTC (rev 730)
+++ AndroidME/src_MIDP/javax/microedition/lcdui/Canvas.java 2010-03-24 13:39:04 UTC (rev 731)
@@ -40,7 +40,7 @@
private Bitmap graphicsBitmap;
protected Canvas() {
- this.canvasView = new CanvasView((AndroidMeMIDlet.DEFAULT_ACTIVITY.getMIDlet()).getActivity());
+ this.canvasView = new CanvasView(AndroidMeMIDlet.DEFAULT_ACTIVITY);
}
public void setFullScreenMode(boolean fullScreen) {
@@ -81,7 +81,7 @@
public void repaint(int x, int y, int w, int h) {
if (this.canvasView != null) {
- this.canvasView.postInvalidate(x, y, x + w, y + h);
+ this.canvasView.postInvalidate();
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|