Merge pull request #917 from marco-c/background_not_running

Allow MIDlets to run in the background
This commit is contained in:
Myk Melez 2015-01-26 15:08:19 -08:00
Родитель f3f5bdd401 4588eb891b
Коммит b56642a22e
11 изменённых файлов: 146 добавлений и 26 удалений

Просмотреть файл

@ -12,6 +12,8 @@ var MIDlet = {
SMSDialogReceiveFilter: function(message) {
return message;
},
wakeUpAlarm: 60000, // ms, i.e. 60 seconds
};
var config = {

Просмотреть файл

@ -22,8 +22,7 @@
* network_mnc
* platform
* profile
* pushConn
* pushMidlet
* fgMidletClass
* autosize
* fontSize
*

Просмотреть файл

@ -540,4 +540,14 @@ DumbPipe.registerOpener("reload", function(message, sender) {
DumbPipe.registerOpener("exit", function(message, sender) {
window.close();
});
});
DumbPipe.registerOpener("backgroundCheck", function(message, sender) {
if (navigator.mozAlarms) {
navigator.mozAlarms.add(new Date(Date.now() + MIDlet.wakeUpAlarm), 'ignoreTimezone', {});
navigator.mozSetMessageHandler('alarm', function() {
navigator.mozAlarms.add(new Date(Date.now() + MIDlet.wakeUpAlarm), 'ignoreTimezone', {});
});
}
});

Просмотреть файл

@ -1,13 +1,55 @@
package com.nokia.mid.s40.bg;
public class BGUtils {
public static void setBGMIDletResident(boolean param) {
System.out.println("warning: BGUtils.setBGMIDletResident(Z)V not implemented (" + param + ")");
}
import com.sun.midp.main.MIDletSuiteUtils;
public static boolean launchIEMIDlet(String string1, String string2, int int1, String string3, String string4) {
throw new RuntimeException("BGUtils.launchIEMIdlet(L...String;L...String;IL...String;L...String;)Z " +
"not implemented (" + string1 + ", " + string2 + ", " + int1 + ", " + string3 +
", " + string4 + ")");
class WaitUserInteractionThread extends Thread {
public void run() {
BGUtils.waitUserInteraction();
BGUtils.startMIDlet();
}
}
public class BGUtils {
private static boolean launchMIDletCalled = false;
private static native int getFGMIDletNumber();
private static native String getFGMIDletClass();
static native void waitUserInteraction();
/* Start the FG MIDlet when the page becomes visible and if
launchIEMIDlet hasn't been called (we want launchIEMIDlet
to launch the MIDlet if possible) */
public static void setBGMIDletResident(boolean param) {
new WaitUserInteractionThread().start();
}
public static void startMIDlet() {
if (BGUtils.launchMIDletCalled) {
return;
}
int midletNumber = BGUtils.getFGMIDletNumber();
String midletClass = BGUtils.getFGMIDletClass();
MIDletSuiteUtils.execute(midletNumber, midletClass, null);
}
private static native void addSystemProperties(String args);
public static boolean launchIEMIDlet(String midletSuiteVendor, String midletName, int midletNumber, String startupNoteText, String args) {
BGUtils.launchMIDletCalled = true;
BGUtils.waitUserInteraction();
try {
BGUtils.addSystemProperties(args);
MIDletSuiteUtils.execute(midletNumber, BGUtils.getFGMIDletClass(), null);
} catch (Exception e) {
System.out.println("Unexpected exception: " + e);
e.printStackTrace();
}
return true;
}
}

Просмотреть файл

@ -52,6 +52,7 @@ module J2ME {
"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/VKVisibilityNotificationRunnable.sleepUntilVKVisibilityChange.()Z": YieldReason.Root,
"com/nokia/mid/s40/bg/BGUtils.waitUserInteraction.()V": YieldReason.Root,
"org/mozilla/io/LocalMsgConnection.init.(Ljava/lang/String;)V": YieldReason.Root,
"org/mozilla/io/LocalMsgConnection.receiveData.([B)I": YieldReason.Root,
"com/sun/mmedia/PlayerImpl.nRealize.(ILjava/lang/String;)Z": YieldReason.Root,

Просмотреть файл

@ -57,6 +57,7 @@
<script type="text/javascript" src="midp/codec.js" defer></script>
<script type="text/javascript" src="midp/pim.js" defer></script>
<script type="text/javascript" src="midp/device_control.js" defer></script>
<script type="text/javascript" src="midp/background.js" defer></script>
<script type="text/javascript" src="game-ui.js" defer></script>
<!-- run make aot to generate these -->

13
main.js
Просмотреть файл

@ -19,15 +19,6 @@ if (config.jars) {
jars = jars.concat(config.jars.split(":"));
}
if (config.pushConn && config.pushMidlet) {
MIDP.ConnectionRegistry.addConnection({
connection: config.pushConn,
midlet: config.pushMidlet,
filter: "*",
suiteId: "1"
});
}
// Mobile info gets accessed a lot, so we cache it on startup.
var mobileInfo;
var getMobileInfo = new Promise(function(resolve, reject) {
@ -67,7 +58,7 @@ function processJAD(data) {
}
if (config.jad) {
loadingPromises.push(load(config.jad, "text").then(processJAD));
loadingPromises.push(load(config.jad, "text").then(processJAD).then(backgroundCheck));
}
function performDownload(url, dialog, callback) {
@ -162,7 +153,7 @@ if (config.downloadJAD) {
}
});
});
}));
}).then(backgroundCheck));
}
if (config.midletClassName == "RunTests") {

Просмотреть файл

@ -40,7 +40,13 @@
},
"storage": {
"description": "Required to store files without limitations"
},
"alarms": {
"description": "Required to perform tasks while the app isn't running"
}
},
"messages": [
{ "alarm": "/index.html" }
],
"type": "privileged"
}

59
midp/background.js Normal file
Просмотреть файл

@ -0,0 +1,59 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
function backgroundCheck() {
if (!MIDP.manifest["Nokia-MIDlet-bg-server"]) {
return;
}
DumbPipe.close(DumbPipe.open("backgroundCheck", {}));
}
Native["com/nokia/mid/s40/bg/BGUtils.getFGMIDletClass.()Ljava/lang/String;"] = function() {
return J2ME.newString(config.fgMidletClass);
};
Native["com/nokia/mid/s40/bg/BGUtils.getFGMIDletNumber.()I"] = function() {
return 1;
};
MIDP.additionalProperties = {};
Native["com/nokia/mid/s40/bg/BGUtils.addSystemProperties.(Ljava/lang/String;)V"] = function(args) {
util.fromJavaString(args).split(";").splice(1).forEach(function(arg) {
var elems = arg.split("=");
MIDP.additionalProperties[elems[0]] = elems[1];
});
};
var localmsgServerCreated = false;
var localmsgServerWait = null;
Native["com/nokia/mid/s40/bg/BGUtils.waitUserInteraction.()V"] = function() {
asyncImpl("V", new Promise(function(resolve, reject) {
// If the page is visible, just start the FG MIDlet
if (!document.hidden) {
resolve();
return;
}
// Otherwise, wait until the page becomes visible, then start the FG MIDlet
document.addEventListener("visibilitychange", function onVisible() {
if (!document.hidden) {
document.removeEventListener("visibilitychange", onVisible, false);
resolve();
}
}, false);
}).then(function() {
return new Promise(function(resolve, reject) {
if (localmsgServerCreated) {
resolve();
return;
}
localmsgServerWait = resolve;
});
}));
};

Просмотреть файл

@ -1082,7 +1082,10 @@ Native["org/mozilla/io/LocalMsgConnection.init.(Ljava/lang/String;)V"] = functio
// It seems that one server only serves on client at a time, let's
// store an object instead of the constructor.
this.connection = MIDP.LocalMsgConnections[this.protocolName] = new LocalMsgConnection();
MIDP.ConnectionRegistry.pushNotify("localmsg:" + this.protocolName);
localmsgServerCreated = true;
if (localmsgServerWait) {
localmsgServerWait();
}
} else {
// Actually, there should always be a server, but we need this check
// for apps that use the Nokia built-in servers (because we haven't

Просмотреть файл

@ -70,8 +70,9 @@ Native["java/lang/System.arraycopy.(Ljava/lang/Object;ILjava/lang/Object;II)V"]
};
Native["java/lang/System.getProperty0.(Ljava/lang/String;)Ljava/lang/String;"] = function(key) {
key = util.fromJavaString(key);
var value;
switch (util.fromJavaString(key)) {
switch (key) {
case "microedition.encoding":
// The value of this property is different than the value on a real Nokia Asha 503 phone.
// On the phone, it is: ISO8859_1.
@ -235,10 +236,15 @@ Native["java/lang/System.getProperty0.(Ljava/lang/String;)Ljava/lang/String;"] =
value = "encoding=jpeg&quality=80&progressive=true&type=jfif&width=400&height=400";
break;
default:
console.warn("UNKNOWN PROPERTY (java/lang/System): " + util.fromJavaString(key));
value = null;
if (MIDP.additionalProperties[key]) {
value = MIDP.additionalProperties[key];
} else {
console.warn("UNKNOWN PROPERTY (java/lang/System): " + key);
value = null;
}
break;
}
return J2ME.newString(value);
};