From 06efc4ec6b55683d2cb5e93a5eff02516c398e4f Mon Sep 17 00:00:00 2001 From: Tim Abraldes Date: Thu, 12 Feb 2015 18:01:03 -0800 Subject: [PATCH 1/4] Convert MIDP to module pattern. This change attempts to have the smallest diff possible so that it is obvious what has actually changed. Hence, whitespace will look funny in this file after this change is applied. A follow-up whitespace-only change will be made. MIDP has been converted from an object to a module. `MIDP.Context2D` has been converted from a module to the member variable `MIDP.context2D`. Invocations of `MIDP.sendNativeEvent` have been replaced with `MIDP.send*Event` function invocations. This provides a dual benefit: Those functions now do a little bit of validation that we weren't doing before, and we don't have to expose `MIDP.sendNativeEvent` or `MIDP.foregroundIsolateId` --- midp/gfx.js | 26 +- midp/media.js | 18 +- midp/midp.js | 587 ++++++++++++++++++++++++-------------------- midp/sensor.js | 4 +- midp/text_editor.js | 2 +- native.js | 4 +- 6 files changed, 338 insertions(+), 303 deletions(-) diff --git a/midp/gfx.js b/midp/gfx.js index 48b55701..5dfc9c38 100644 --- a/midp/gfx.js +++ b/midp/gfx.js @@ -65,11 +65,11 @@ var currentlyFocusedTextEditor; }; Native["com/sun/midp/lcdui/DisplayDevice.getScreenWidth0.(I)I"] = function(id) { - return MIDP.Context2D.canvas.width; + return MIDP.context2D.canvas.width; }; Native["com/sun/midp/lcdui/DisplayDevice.getScreenHeight0.(I)I"] = function(id) { - return MIDP.Context2D.canvas.height; + return MIDP.context2D.canvas.height; }; Native["com/sun/midp/lcdui/DisplayDevice.displayStateChanged0.(II)V"] = function(hardwareId, state) { @@ -323,7 +323,7 @@ var currentlyFocusedTextEditor; var SIZE_LARGE = 16; Native["javax/microedition/lcdui/Font.init.(III)V"] = function(face, style, size) { - var defaultSize = config.fontSize ? config.fontSize : Math.max(19, (MIDP.Context2D.canvas.height / 35) | 0); + var defaultSize = config.fontSize ? config.fontSize : Math.max(19, (MIDP.context2D.canvas.height / 35) | 0); if (size & SIZE_SMALL) size = defaultSize / 1.25; else if (size & SIZE_LARGE) @@ -351,10 +351,10 @@ var currentlyFocusedTextEditor; // Note: // When a css string, such as ` 10 pt Arial, Helvetica`, is set to - // MIDP.Context2D.font, it will be formatted to `10 pt Arial,Helvetica` + // MIDP.context2D.font, it will be formatted to `10 pt Arial,Helvetica` // with some spaces removed. // We need this css string to have the same format as that of the - // MIDP.Context2D.font to do comparison in withFont() function. + // MIDP.context2D.font to do comparison in withFont() function. this.css = style + size + "px " + face; this.size = size; this.style = style; @@ -364,8 +364,8 @@ var currentlyFocusedTextEditor; function calcStringWidth(font, str) { var emojiLen = 0; - withFont(font, MIDP.Context2D); - var len = measureWidth(MIDP.Context2D, str.replace(emoji.regEx, function() { + withFont(font, MIDP.context2D); + var len = measureWidth(MIDP.context2D, str.replace(emoji.regEx, function() { emojiLen += font.size; return ""; })); @@ -393,8 +393,8 @@ var currentlyFocusedTextEditor; }; Native["javax/microedition/lcdui/Font.charWidth.(C)I"] = function(char) { - withFont(this, MIDP.Context2D); - return measureWidth(MIDP.Context2D, String.fromCharCode(char)); + withFont(this, MIDP.context2D); + return measureWidth(MIDP.context2D, String.fromCharCode(char)); }; Native["javax/microedition/lcdui/Font.charsWidth.([CII)I"] = function(str, offset, len) { @@ -418,7 +418,7 @@ var currentlyFocusedTextEditor; c = null; if (img === null) { - c = MIDP.Context2D; + c = MIDP.context2D; } else { var imgData = img.imageData, c = imgData.context; @@ -1900,11 +1900,7 @@ var currentlyFocusedTextEditor; }); function sendEvent(command) { - MIDP.sendNativeEvent({ - type: MIDP.COMMAND_EVENT, - intParam1: command.klass.classInfo.getField("I.id.I").get(command), - intParam4: MIDP.displayId, - }, MIDP.foregroundIsolateId); + MIDP.sendCommandEvent(command.klass.classInfo.getField("I.id.I").get(command)); } if (el) { diff --git a/midp/media.js b/midp/media.js index aba269d1..1839f639 100644 --- a/midp/media.js +++ b/midp/media.js @@ -213,13 +213,7 @@ AudioPlayer.prototype.realize = function() { AudioPlayer.prototype.play = function() { this.audio.play(); this.audio.onended = function() { - MIDP.sendNativeEvent({ - type: MIDP.MMAPI_EVENT, - intParam1: this.playerContainer.pId, - intParam2: this.getDuration(), - intParam3: 0, - intParam4: Media.EVENT_MEDIA_END_OF_MEDIA - }, MIDP.foregroundIsolateId); + MIDP.sendEndOfMediaEvent(this.playerContainer.pId, this.getDuration()); }.bind(this); }; @@ -434,15 +428,7 @@ ImageRecorder.prototype.recipient = function(message) { case "snapshot": this.snapshotData = new Int8Array(message.data); - - MIDP.sendNativeEvent({ - type: MIDP.MMAPI_EVENT, - intParam1: this.playerContainer.pId, - intParam2: 0, - intParam3: 0, - intParam4: Media.EVENT_MEDIA_SNAPSHOT_FINISHED, - }, MIDP.foregroundIsolateId); - + MIDP.sendMediaSnapshotFinishedEvent(this.playerContainer.pId); break; } } diff --git a/midp/midp.js b/midp/midp.js index 465be9d8..ab076eac 100644 --- a/midp/midp.js +++ b/midp/midp.js @@ -1,26 +1,25 @@ -/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* vim: set shiftwidth=4 tabstop=4 autoindent cindent expandtab: */ +/* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ 'use strict'; -var MIDP = { +var MIDP = (function() { + var canvas = document.getElementById("canvas"); + + var isFullScreen = true; +function setFullScreen(isFS) { + isFullScreen = isFS; + updateCanvas(); }; -MIDP.isFullScreen = true; -MIDP.setFullScreen = function(isFS) { - MIDP.isFullScreen = isFS; - MIDP.updateCanvas(); -}; - -MIDP.updateCanvas = function() { + function updateCanvas() { var sidebar = document.getElementById("sidebar"); var header = document.getElementById("drawer").querySelector("header"); - var canvas = document.getElementById("canvas"); sidebar.style.display = header.style.display = - MIDP.isFullScreen ? "none" : "block"; - var headerHeight = MIDP.isFullScreen ? 0 : header.offsetHeight; - var newHeight = MIDP.physicalScreenHeight - headerHeight; - var newWidth = MIDP.physicalScreenWidth; + isFullScreen ? "none" : "block"; + var headerHeight = isFullScreen ? 0 : header.offsetHeight; + var newHeight = physicalScreenHeight - headerHeight; + var newWidth = physicalScreenWidth; if (newHeight != canvas.height || newWidth != canvas.width) { canvas.height = newHeight; @@ -30,26 +29,26 @@ MIDP.updateCanvas = function() { canvas.style.top = headerHeight + "px"; canvas.dispatchEvent(new Event("canvasresize")); } -}; + }; -MIDP.onWindowResize = function(evt) { - var newPhysicalScreenWidth = window.outerWidth - MIDP.horizontalChrome; - var newPhysicalScreenHeight = window.outerHeight - MIDP.verticalChrome; + function onWindowResize(evt) { + var newPhysicalScreenWidth = window.outerWidth - horizontalChrome; + var newPhysicalScreenHeight = window.outerHeight - verticalChrome; - if (newPhysicalScreenWidth != MIDP.physicalScreenWidth || newPhysicalScreenHeight != MIDP.physicalScreenHeight) { - MIDP.physicalScreenWidth = newPhysicalScreenWidth; - MIDP.physicalScreenHeight = newPhysicalScreenHeight; - MIDP.lastWindowInnerHeight = window.innerHeight; - MIDP.updateCanvas(); - } else if (MIDP.lastWindowInnerHeight != window.innerHeight) { - MIDP.lastWindowInnerHeight = window.innerHeight; - MIDP.sendVirtualKeyboardEvent(); + if (newPhysicalScreenWidth != physicalScreenWidth || newPhysicalScreenHeight != physicalScreenHeight) { + physicalScreenWidth = newPhysicalScreenWidth; + physicalScreenHeight = newPhysicalScreenHeight; + lastWindowInnerHeight = window.innerHeight; + updateCanvas(); + } else if (lastWindowInnerHeight != window.innerHeight) { + lastWindowInnerHeight = window.innerHeight; + sendVirtualKeyboardEvent(); } else { console.warn("Unhandled resize event!"); } }; -MIDP.manifest = {}; + var manifest = {}; Native["com/sun/midp/jarutil/JarReader.readJarEntry0.(Ljava/lang/String;Ljava/lang/String;)[B"] = function(jar, entryName) { var bytes = CLASSES.loadFileFromJar(util.fromJavaString(jar), util.fromJavaString(entryName)); @@ -67,7 +66,7 @@ Native["com/sun/midp/log/LoggingBase.report.(IILjava/lang/String;)V"] = function console.info(util.fromJavaString(message)); }; -MIDP.groupTBL = [ + var groupTBL = [ "net_access", "low_level_net_access", "call_control", @@ -84,17 +83,17 @@ MIDP.groupTBL = [ "authentication", "smart_card", "satsa" -]; + ]; Native["com/sun/midp/security/Permissions.loadGroupList.()[Ljava/lang/String;"] = function() { - var list = J2ME.newStringArray(MIDP.groupTBL.length); - MIDP.groupTBL.forEach(function (e, n) { + var list = J2ME.newStringArray(groupTBL.length); + groupTBL.forEach(function (e, n) { list[n] = J2ME.newString(e); }); return list; }; -MIDP.messagesTBL = [ + var messagesTBL = [ ["Airtime", "How often should %1 ask for permission to use airtime? Using airtime may result in charges.", "Don't use airtime and don't ask", @@ -187,9 +186,9 @@ MIDP.messagesTBL = [ Native["com/sun/midp/security/Permissions.getGroupMessages.(Ljava/lang/String;)[Ljava/lang/String;"] = function(jName) { var name = util.fromJavaString(jName); var list = null; - MIDP.groupTBL.forEach(function(e, n) { + groupTBL.forEach(function(e, n) { if (e === name) { - var messages = MIDP.messagesTBL[n]; + var messages = messagesTBL[n]; list = J2ME.newStringArray(messages.length); messages.forEach(function (e, n) { list[n] = J2ME.newString(e); @@ -199,7 +198,7 @@ Native["com/sun/midp/security/Permissions.getGroupMessages.(Ljava/lang/String;)[ return list; }; -MIDP.membersTBL = [ + var membersTBL = [ ["javax.microedition.io.Connector.http", "javax.microedition.io.Connector.https", "javax.microedition.io.Connector.obex.client.tcp", @@ -255,9 +254,9 @@ MIDP.membersTBL = [ Native["com/sun/midp/security/Permissions.loadGroupPermissions.(Ljava/lang/String;)[Ljava/lang/String;"] = function(jName) { var name = util.fromJavaString(jName); var list = null; - MIDP.groupTBL.forEach(function(e, n) { + groupTBL.forEach(function(e, n) { if (e === name) { - var members = MIDP.membersTBL[n]; + var members = membersTBL[n]; list = J2ME.newStringArray(members.length); members.forEach(function (e, n) { list[n] = J2ME.newString(e); @@ -272,7 +271,7 @@ Native["com/sun/midp/main/CldcPlatformRequest.dispatchPlatformRequest.(Ljava/lan if (request.startsWith("http://") || request.startsWith("https://")) { if (request.endsWith(".jad")) { // The download will start after the MIDlet has terminated its execution. - MIDP.pendingMIDletUpdate = request; + pendingMIDletUpdate = request; return 1; } else { DumbPipe.close(DumbPipe.open("windowOpen", request)); @@ -304,7 +303,7 @@ Native["com/sun/midp/main/CommandState.restoreCommandState.(Lcom/sun/midp/main/C state.klass.classInfo.getField("I.arg2.Ljava/lang/String;").set(state, J2ME.newString((args.length > 2) ? args[2] : "")); }; -MIDP.domainTBL = [ + var domainTBL = [ "manufacturer", "operator", "identified_third_party", @@ -314,88 +313,88 @@ MIDP.domainTBL = [ ]; Native["com/sun/midp/security/Permissions.loadDomainList.()[Ljava/lang/String;"] = function() { - var list = J2ME.newStringArray(MIDP.domainTBL.length); - MIDP.domainTBL.forEach(function (e, n) { + var list = J2ME.newStringArray(domainTBL.length); + domainTBL.forEach(function (e, n) { list[n] = J2ME.newString(e); }); return list; }; -MIDP.NEVER = 0; -MIDP.ALLOW = 1; -MIDP.BLANKET = 4; -MIDP.SESSION = 8; -MIDP.ONESHOT = 16; +var NEVER = 0; +var ALLOW = 1; +var BLANKET = 4; +var SESSION = 8; +var ONESHOT = 16; -MIDP.identifiedTBL = { - net_access: { max: MIDP.BLANKET, default: MIDP.SESSION}, - low_level_net_access: { max: MIDP.BLANKET, default: MIDP.SESSION}, - call_control: { max: MIDP.BLANKET, default: MIDP.ONESHOT}, - application_auto_invocation: { max: MIDP.BLANKET, default: MIDP.ONESHOT}, - local_connectivity: { max: MIDP.BLANKET, default: MIDP.SESSION}, - messaging: { max: MIDP.BLANKET, default: MIDP.ONESHOT}, - restricted_messaging: { max: MIDP.BLANKET, default: MIDP.ONESHOT}, - multimedia_recording: { max: MIDP.BLANKET, default: MIDP.SESSION}, - read_user_data_access: { max: MIDP.BLANKET, default: MIDP.ONESHOT}, - write_user_data_access: { max: MIDP.BLANKET, default: MIDP.ONESHOT}, - location: { max: MIDP.BLANKET, default: MIDP.SESSION}, - landmark: { max: MIDP.BLANKET, default: MIDP.SESSION}, - payment: { max: MIDP.ALLOW, default: MIDP.ALLOW}, - authentication: { max: MIDP.BLANKET, default: MIDP.SESSION}, - smart_card: { max: MIDP.BLANKET, default: MIDP.SESSION}, - satsa: { max: MIDP.NEVER, default: MIDP.NEVER}, -}; + var identifiedTBL = { + net_access: { max: BLANKET, default: SESSION}, + low_level_net_access: { max: BLANKET, default: SESSION}, + call_control: { max: BLANKET, default: ONESHOT}, + application_auto_invocation: { max: BLANKET, default: ONESHOT}, + local_connectivity: { max: BLANKET, default: SESSION}, + messaging: { max: BLANKET, default: ONESHOT}, + restricted_messaging: { max: BLANKET, default: ONESHOT}, + multimedia_recording: { max: BLANKET, default: SESSION}, + read_user_data_access: { max: BLANKET, default: ONESHOT}, + write_user_data_access: { max: BLANKET, default: ONESHOT}, + location: { max: BLANKET, default: SESSION}, + landmark: { max: BLANKET, default: SESSION}, + payment: { max: ALLOW, default: ALLOW}, + authentication: { max: BLANKET, default: SESSION}, + smart_card: { max: BLANKET, default: SESSION}, + satsa: { max: NEVER, default: NEVER}, + }; -MIDP.unidentifiedTBL = { - net_access: { max: MIDP.SESSION, default: MIDP.ONESHOT}, - low_level_net_access: { max: MIDP.SESSION, default: MIDP.ONESHOT}, - call_control: { max: MIDP.ONESHOT, default: MIDP.ONESHOT}, - application_auto_invocation: { max: MIDP.SESSION, default: MIDP.ONESHOT}, - local_connectivity: { max: MIDP.BLANKET, default: MIDP.ONESHOT}, - messaging: { max: MIDP.ONESHOT, default: MIDP.ONESHOT}, - restricted_messaging: { max: MIDP.ONESHOT, default: MIDP.ONESHOT}, - multimedia_recording: { max: MIDP.SESSION, default: MIDP.ONESHOT}, - read_user_data_access: { max: MIDP.ONESHOT, default: MIDP.ONESHOT}, - write_user_data_access: { max: MIDP.ONESHOT, default: MIDP.ONESHOT}, - location: { max: MIDP.SESSION, default: MIDP.ONESHOT}, - landmark: { max: MIDP.SESSION, default: MIDP.ONESHOT}, - payment: { max: MIDP.NEVER, default: MIDP.NEVER}, - authentication: { max: MIDP.NEVER, default: MIDP.NEVER}, - smart_card: { max: MIDP.NEVER, default: MIDP.NEVER}, - satsa: { max: MIDP.NEVER, default: MIDP.NEVER}, -}; + var unidentifiedTBL = { + net_access: { max: SESSION, default: ONESHOT}, + low_level_net_access: { max: SESSION, default: ONESHOT}, + call_control: { max: ONESHOT, default: ONESHOT}, + application_auto_invocation: { max: SESSION, default: ONESHOT}, + local_connectivity: { max: BLANKET, default: ONESHOT}, + messaging: { max: ONESHOT, default: ONESHOT}, + restricted_messaging: { max: ONESHOT, default: ONESHOT}, + multimedia_recording: { max: SESSION, default: ONESHOT}, + read_user_data_access: { max: ONESHOT, default: ONESHOT}, + write_user_data_access: { max: ONESHOT, default: ONESHOT}, + location: { max: SESSION, default: ONESHOT}, + landmark: { max: SESSION, default: ONESHOT}, + payment: { max: NEVER, default: NEVER}, + authentication: { max: NEVER, default: NEVER}, + smart_card: { max: NEVER, default: NEVER}, + satsa: { max: NEVER, default: NEVER}, + }; Native["com/sun/midp/security/Permissions.getDefaultValue.(Ljava/lang/String;Ljava/lang/String;)B"] = function(domain, group) { - var allow = MIDP.NEVER; + var allow = NEVER; switch (util.fromJavaString(domain)) { case "manufacturer": case "maximum": case "operator": - allow = MIDP.ALLOW; + allow = ALLOW; break; case "identified_third_party": - allow = MIDP.identifiedTBL[util.fromJavaString(group)].default; + allow = identifiedTBL[util.fromJavaString(group)].default; break; case "unidentified_third_party": - allow = MIDP.unidentifiedTBL[util.fromJavaString(group)].default; + allow = unidentifiedTBL[util.fromJavaString(group)].default; break; } return allow; }; Native["com/sun/midp/security/Permissions.getMaxValue.(Ljava/lang/String;Ljava/lang/String;)B"] = function(domain, group) { - var allow = MIDP.NEVER; + var allow = NEVER; switch (util.fromJavaString(domain)) { case "manufacturer": case "maximum": case "operator": - allow = MIDP.ALLOW; + allow = ALLOW; break; case "identified_third_party": - allow = MIDP.identifiedTBL[util.fromJavaString(group)].max; + allow = identifiedTBL[util.fromJavaString(group)].max; break; case "unidentified_third_party": - allow = MIDP.unidentifiedTBL[util.fromJavaString(group)].max; + allow = unidentifiedTBL[util.fromJavaString(group)].max; break; } return allow; @@ -409,16 +408,17 @@ Native["com/sun/midp/main/MIDletSuiteUtils.getIsolateId.()I"] = function() { return $.ctx.runtime.isolate.id; }; + var AMSIsolateId; Native["com/sun/midp/main/MIDletSuiteUtils.registerAmsIsolateId.()V"] = function() { - MIDP.AMSIsolateId = $.ctx.runtime.isolate.id; + AMSIsolateId = $.ctx.runtime.isolate.id; }; Native["com/sun/midp/main/MIDletSuiteUtils.getAmsIsolateId.()I"] = function() { - return MIDP.AMSIsolateId; + return AMSIsolateId; }; Native["com/sun/midp/main/MIDletSuiteUtils.isAmsIsolate.()Z"] = function() { - return MIDP.AMSIsolateId == $.ctx.runtime.isolate.id ? 1 : 0; + return AMSIsolateId == $.ctx.runtime.isolate.id ? 1 : 0; }; Native["com/sun/midp/main/MIDletSuiteUtils.vmBeginStartUp.(I)V"] = function(midletIsolateId) { @@ -427,7 +427,7 @@ Native["com/sun/midp/main/MIDletSuiteUtils.vmBeginStartUp.(I)V"] = function(midl // The formula depens on the ID of the isolate that calls createDisplayId, that is // the same isolate that calls vmBeginStartUp. So this is a good place to calculate // the display ID. - MIDP.displayId = ((midletIsolateId & 0xff)<<24) | (1 & 0x00ffffff); + displayId = ((midletIsolateId & 0xff)<<24) | (1 & 0x00ffffff); }; Native["com/sun/midp/main/MIDletSuiteUtils.vmEndStartUp.(I)V"] = function(midletIsolateId) { @@ -500,9 +500,12 @@ Native["com/sun/midp/util/ResourceHandler.loadRomizedResource0.(Ljava/lang/Strin return bytes; }; -MIDP.Context2D = (function() { - var c = document.getElementById("canvas"); - + var verticalChrome; + var horizontalChrome; + var physicalScreenWidth; + var physicalScreenHeight; + var lastWindowInnerHeight; + var isVKVisible; if (config.autosize && !/no|0/.test(config.autosize)) { document.documentElement.classList.add('autosize'); @@ -513,8 +516,8 @@ MIDP.Context2D = (function() { // which has been 30px in testing. These are assumed to be static // throughout the lifetime of the app, and things will break if that // assumption is violated. - MIDP.verticalChrome = window.outerHeight - window.innerHeight; - MIDP.horizontalChrome = window.outerWidth - window.innerWidth; + verticalChrome = window.outerHeight - window.innerHeight; + horizontalChrome = window.outerWidth - window.innerWidth; // "Physical" dimensions: // The amount of space available to J2ME. This is always the @@ -531,17 +534,17 @@ MIDP.Context2D = (function() { // the header, which might shrink our canvas. To find out how much // space is actually available to the current MIDlet, check // `document.getElementById("canvas").[width|height]`. - MIDP.physicalScreenWidth = window.outerWidth - MIDP.horizontalChrome; - MIDP.physicalScreenHeight = window.outerHeight - MIDP.verticalChrome; + physicalScreenWidth = window.outerWidth - horizontalChrome; + physicalScreenHeight = window.outerHeight - verticalChrome; // Cached value of `window.innerHeight` so that we can tell when it // changes. This is useful for determining when to send keyboard // visibility events. - MIDP.lastWindowInnerHeight = window.innerHeight; + lastWindowInnerHeight = window.innerHeight; - MIDP.updateCanvas(); - MIDP.isVKVisible = function() { - var expectedHeightWithNoKeyboard = window.outerHeight - MIDP.verticalChrome; + updateCanvas(); + isVKVisible = function() { + var expectedHeightWithNoKeyboard = window.outerHeight - verticalChrome; if (window.innerHeight == expectedHeightWithNoKeyboard) { return false; } else if (window.innerHeight < expectedHeightWithNoKeyboard) { @@ -551,35 +554,35 @@ MIDP.Context2D = (function() { return false; } }; - window.addEventListener("resize", MIDP.onWindowResize); + window.addEventListener("resize", onWindowResize); } else { document.documentElement.classList.add('debug-mode'); - MIDP.physicalScreenWidth = 240; - MIDP.physicalScreenHeight = 320; + physicalScreenWidth = 240; + physicalScreenHeight = 320; - MIDP.updateCanvas(); - MIDP.isVKVisible = function() { + updateCanvas(); + isVKVisible = function() { return false; }; } function sendPenEvent(pt, whichType) { - MIDP.sendNativeEvent({ - type: MIDP.PEN_EVENT, + sendNativeEvent({ + type: PEN_EVENT, intParam1: whichType, intParam2: pt.x, intParam3: pt.y, - intParam4: MIDP.displayId - }, MIDP.foregroundIsolateId); + intParam4: displayId + }, foregroundIsolateId); } function sendGestureEvent(pt, distancePt, whichType, aFloatParam1, aIntParam7, aIntParam8, aIntParam9) { - MIDP.sendNativeEvent({ - type: MIDP.GESTURE_EVENT, + sendNativeEvent({ + type: GESTURE_EVENT, intParam1: whichType, intParam2: distancePt && distancePt.x || 0, intParam3: distancePt && distancePt.y || 0, - intParam4: MIDP.displayId, + intParam4: displayId, intParam5: pt.x, intParam6: pt.y, floatParam1: Math.fround(aFloatParam1 || 0.0), @@ -593,7 +596,7 @@ MIDP.Context2D = (function() { intParam14: 0, intParam15: 0, intParam16: 0 - }, MIDP.foregroundIsolateId); + }, foregroundIsolateId); } // In the simulator and on device, use touch events; in desktop @@ -602,10 +605,10 @@ MIDP.Context2D = (function() { var supportsTouch = ("ontouchstart" in document.documentElement); // Cache the canvas position for future computation. - var canvasRect = c.getBoundingClientRect(); - c.addEventListener("canvasresize", function() { - canvasRect = c.getBoundingClientRect(); - MIDP.sendRotationEvent(); + var canvasRect = canvas.getBoundingClientRect(); + canvas.addEventListener("canvasresize", function() { + canvasRect = canvas.getBoundingClientRect(); + sendRotationEvent(); }); function getEventPoint(event) { @@ -629,20 +632,20 @@ MIDP.Context2D = (function() { var longPressTimeoutID = null; var longPressDetected = false; - c.addEventListener(supportsTouch ? "touchstart" : "mousedown", function(event) { + canvas.addEventListener(supportsTouch ? "touchstart" : "mousedown", function(event) { event.preventDefault(); // Prevent unnecessary fake mouse events. var pt = getEventPoint(event); - sendPenEvent(pt, MIDP.PRESSED); + sendPenEvent(pt, PRESSED); mouseDownInfo = pt; longPressDetected = false; longPressTimeoutID = setTimeout(function() { longPressDetected = true; - sendGestureEvent(pt, null, MIDP.GESTURE_LONG_PRESS); + sendGestureEvent(pt, null, GESTURE_LONG_PRESS); }, LONG_PRESS_TIMEOUT); }); - c.addEventListener(supportsTouch ? "touchmove" : "mousemove", function(event) { + canvas.addEventListener(supportsTouch ? "touchmove" : "mousemove", function(event) { if (!mouseDownInfo) { return; // Mousemove on desktop; ignored. } @@ -654,7 +657,7 @@ MIDP.Context2D = (function() { } var pt = getEventPoint(event); - sendPenEvent(pt, MIDP.DRAGGED); + sendPenEvent(pt, DRAGGED); var distance = { x: pt.x - mouseDownInfo.x, y: pt.y - mouseDownInfo.y @@ -668,7 +671,7 @@ MIDP.Context2D = (function() { mouseDownInfo.x = pt.x; mouseDownInfo.y = pt.y; if (!longPressDetected) { - sendGestureEvent(pt, distance, MIDP.GESTURE_DRAG); + sendGestureEvent(pt, distance, GESTURE_DRAG); } } @@ -734,7 +737,7 @@ MIDP.Context2D = (function() { } var pt = getEventPoint(event); - sendPenEvent(pt, MIDP.RELEASED); + sendPenEvent(pt, RELEASED); if (!longPressDetected) { if (mouseDownInfo.isDragging) { @@ -748,28 +751,25 @@ MIDP.Context2D = (function() { // between touchend event and the last touchmove event is // larger than a threshold. if (deltaTime > 300 || flickSpeed.speed == 0) { - sendGestureEvent(pt, null, MIDP.GESTURE_DROP); + sendGestureEvent(pt, null, GESTURE_DROP); } else { - sendGestureEvent(pt, null, MIDP.GESTURE_FLICK, + sendGestureEvent(pt, null, GESTURE_FLICK, flickSpeed.direction, flickSpeed.speed, flickSpeed.speedX, flickSpeed.speedY); } } else { - sendGestureEvent(pt, null, MIDP.GESTURE_DROP); + sendGestureEvent(pt, null, GESTURE_DROP); } } else { - sendGestureEvent(pt, null, MIDP.GESTURE_TAP); + sendGestureEvent(pt, null, GESTURE_TAP); } } mouseDownInfo = null; // Clear the way for the next gesture. }); - return c.getContext("2d"); -})(); - Native["com/sun/midp/midletsuite/MIDletSuiteStorage.loadSuitesIcons0.()I"] = function() { return 0; }; @@ -816,12 +816,12 @@ Native["com/sun/midp/midletsuite/InstallInfo.load.()V"] = function() { }; Native["com/sun/midp/midletsuite/SuiteProperties.load.()[Ljava/lang/String;"] = function() { - var keys = Object.keys(MIDP.manifest); + var keys = Object.keys(manifest); var arr = J2ME.newStringArray(keys.length * 2); var i = 0; keys.forEach(function(key) { arr[i++] = J2ME.newString(key); - arr[i++] = J2ME.newString(MIDP.manifest[key]); + arr[i++] = J2ME.newString(manifest[key]); }); return arr; }; @@ -832,27 +832,27 @@ Native["javax/microedition/lcdui/SuiteImageCacheImpl.loadAndCreateImmutableImage return 0; }; -MIDP.InterIsolateMutexes = []; -MIDP.LastInterIsolateMutexID = -1; +var interIsolateMutexes = []; +var lastInterIsolateMutexID = -1; Native["com/sun/midp/util/isolate/InterIsolateMutex.getID0.(Ljava/lang/String;)I"] = function(jName) { var name = util.fromJavaString(jName); var mutex; - for (var i = 0; i < MIDP.InterIsolateMutexes.length; i++) { - if (MIDP.InterIsolateMutexes[i].name === name) { - mutex = MIDP.InterIsolateMutexes[i]; + for (var i = 0; i < interIsolateMutexes.length; i++) { + if (interIsolateMutexes[i].name === name) { + mutex = interIsolateMutexes[i]; } } if (!mutex) { mutex = { name: name, - id: ++MIDP.LastInterIsolateMutexID, + id: ++lastInterIsolateMutexID, locked: false, waiting: [], }; - MIDP.InterIsolateMutexes.push(mutex); + interIsolateMutexes.push(mutex); } return mutex.id; @@ -861,9 +861,9 @@ Native["com/sun/midp/util/isolate/InterIsolateMutex.getID0.(Ljava/lang/String;)I Native["com/sun/midp/util/isolate/InterIsolateMutex.lock0.(I)V"] = function(id) { var ctx = $.ctx; var mutex; - for (var i = 0; i < MIDP.InterIsolateMutexes.length; i++) { - if (MIDP.InterIsolateMutexes[i].id == id) { - mutex = MIDP.InterIsolateMutexes[i]; + for (var i = 0; i < interIsolateMutexes.length; i++) { + if (interIsolateMutexes[i].id == id) { + mutex = interIsolateMutexes[i]; break; } } @@ -893,9 +893,9 @@ Native["com/sun/midp/util/isolate/InterIsolateMutex.lock0.(I)V"] = function(id) Native["com/sun/midp/util/isolate/InterIsolateMutex.unlock0.(I)V"] = function(id) { var mutex; - for (var i = 0; i < MIDP.InterIsolateMutexes.length; i++) { - if (MIDP.InterIsolateMutexes[i].id == id) { - mutex = MIDP.InterIsolateMutexes[i]; + for (var i = 0; i < interIsolateMutexes.length; i++) { + if (interIsolateMutexes[i].id == id) { + mutex = interIsolateMutexes[i]; break; } } @@ -920,18 +920,18 @@ Native["com/sun/midp/util/isolate/InterIsolateMutex.unlock0.(I)V"] = function(id } }; -MIDP.exit = function(code) { + function exit(code) { $.stop(); DumbPipe.open("exit", null, function(message) {}); document.getElementById("exit-screen").style.display = "block"; -}; + }; -MIDP.pendingMIDletUpdate = null; + var pendingMIDletUpdate = null; Native["com/sun/cldc/isolate/Isolate.stop.(II)V"] = function(code, reason) { console.info("Isolate stops with code " + code + " and reason " + reason); - if (!MIDP.pendingMIDletUpdate) { - MIDP.exit(); + if (!pendingMIDletUpdate) { + exit(); return; } @@ -941,7 +941,7 @@ Native["com/sun/cldc/isolate/Isolate.stop.(II)V"] = function(code, reason) { dialog.classList.add('visible'); document.body.appendChild(dialog); - performDownload(MIDP.pendingMIDletUpdate, dialog, function(data) { + performDownload(pendingMIDletUpdate, dialog, function(data) { dialog.parentElement.removeChild(dialog); fs.remove("/midlet.jad"); @@ -955,7 +955,7 @@ Native["com/sun/cldc/isolate/Isolate.stop.(II)V"] = function(code, reason) { }), CompiledMethodCache.clear(), ]).then(function() { - MIDP.pendingMIDletUpdate = null; + pendingMIDletUpdate = null; DumbPipe.close(DumbPipe.open("alert", "Update completed!")); DumbPipe.close(DumbPipe.open("reload", {})); }); @@ -963,95 +963,133 @@ Native["com/sun/cldc/isolate/Isolate.stop.(II)V"] = function(code, reason) { }; // The foreground isolate will get the user events (keypresses, etc.) -MIDP.foregroundIsolateId; -MIDP.nativeEventQueues = {}; -MIDP.waitingNativeEventQueue = {}; + var foregroundIsolateId; + var displayId; + var nativeEventQueues = {}; + var waitingNativeEventQueue = {}; -MIDP.copyEvent = function(e, obj) { + function copyEvent(e, obj) { var keys = Object.keys(e); for (var i = 0; i < keys.length; i++) { obj[keys[i]] = e[keys[i]]; } -} + } -MIDP.sendNativeEvent = function(e, isolateId) { - var elem = MIDP.waitingNativeEventQueue[isolateId]; + function sendNativeEvent(e, isolateId) { + var elem = waitingNativeEventQueue[isolateId]; if (!elem) { - MIDP.nativeEventQueues[isolateId].push(e); + nativeEventQueues[isolateId].push(e); return; } - MIDP.copyEvent(e, elem.nativeEvent); - elem.resolve(MIDP.nativeEventQueues[isolateId].length); + copyEvent(e, elem.nativeEvent); + elem.resolve(nativeEventQueues[isolateId].length); - delete MIDP.waitingNativeEventQueue[isolateId]; -} + delete waitingNativeEventQueue[isolateId]; + } -MIDP.sendVirtualKeyboardEvent = function() { - if (-1 != MIDP.displayId) { - MIDP.sendNativeEvent({ - type: MIDP.VIRTUAL_KEYBOARD_EVENT, + function sendVirtualKeyboardEvent() { + if (undefined != displayId && undefined != foregroundIsolateId) { + sendNativeEvent({ + type: VIRTUAL_KEYBOARD_EVENT, intParam1: 0, intParam2: 0, intParam3: 0, - intParam4: MIDP.displayId, - }, MIDP.foregroundIsolateId); + intParam4: displayId, + }, foregroundIsolateId); } -}; + }; -MIDP.sendRotationEvent = function() { - if (-1 != MIDP.displayId) { - MIDP.sendNativeEvent({ - type: MIDP.ROTATION_EVENT, + function sendRotationEvent() { + if (undefined != displayId && undefined != foregroundIsolateId) { + sendNativeEvent({ + type: ROTATION_EVENT, intParam1: 0, intParam2: 0, intParam3: 0, - intParam4: MIDP.displayId, - }, MIDP.foregroundIsolateId); + intParam4: displayId, + }, foregroundIsolateId); } -} + } -MIDP.KEY_EVENT = 1; -MIDP.PEN_EVENT = 2; -MIDP.PRESSED = 1; -MIDP.RELEASED = 2; -MIDP.DRAGGED = 3; -MIDP.COMMAND_EVENT = 3; -MIDP.EVENT_QUEUE_SHUTDOWN = 31; -MIDP.ROTATION_EVENT = 43; -MIDP.MMAPI_EVENT = 45; -MIDP.SCREEN_REPAINT_EVENT = 47; -MIDP.VIRTUAL_KEYBOARD_EVENT = 58, -MIDP.GESTURE_EVENT = 71; -MIDP.GESTURE_TAP = 0x1; -MIDP.GESTURE_LONG_PRESS = 0x2; -MIDP.GESTURE_DRAG = 0x4; -MIDP.GESTURE_DROP = 0x8; -MIDP.GESTURE_FLICK = 0x10; -MIDP.GESTURE_LONG_PRESS_REPEATED = 0x20; -MIDP.GESTURE_PINCH = 0x40; -MIDP.GESTURE_DOUBLE_TAP = 0x80; -MIDP.GESTURE_RECOGNITION_START = 0x4000; -MIDP.GESTURE_RECOGNITION_END = 0x8000; + function sendCommandEvent(id) { + if (undefined != displayId && undefined != foregroundIsolateId) { + sendNativeEvent({ + type: COMMAND_EVENT, + intParam1: id, + intParam2: 0, + intParam3: 0, + intParam4: displayId, + }, foregroundIsolateId); + } + } -MIDP.suppressKeyEvents = false; + function sendEndOfMediaEvent(pId, duration) { + if (undefined != foregroundIsolateId) { + sendNativeEvent({ + type: MMAPI_EVENT, + intParam1: pId, + intParam2: duration, + intParam3: 0, + intParam4: Media.EVENT_MEDIA_END_OF_MEDIA + }, foregroundIsolateId); + } + } -MIDP.keyPress = function(keyCode) { - if (!MIDP.suppressKeyEvents) - MIDP.sendNativeEvent({ type: MIDP.KEY_EVENT, intParam1: MIDP.PRESSED, intParam2: keyCode, intParam3: 0, intParam4: MIDP.displayId }, MIDP.foregroundIsolateId); + function sendMediaSnapshotFinishedEvent(pId) { + if (undefined != foregroundIsolateId) { + sendNativeEvent({ + type: MMAPI_EVENT, + intParam1: pId, + intParam2: 0, + intParam3: 0, + intParam4: Media.EVENT_MEDIA_SNAPSHOT_FINISHED, + }, foregroundIsolateId); + } + } + + var KEY_EVENT = 1; + var PEN_EVENT = 2; + var PRESSED = 1; + var RELEASED = 2; + var DRAGGED = 3; + var COMMAND_EVENT = 3; + var EVENT_QUEUE_SHUTDOWN = 31; + var ROTATION_EVENT = 43; + var MMAPI_EVENT = 45; + var SCREEN_REPAINT_EVENT = 47; + var VIRTUAL_KEYBOARD_EVENT = 58; + var GESTURE_EVENT = 71; + var GESTURE_TAP = 0x1; + var GESTURE_LONG_PRESS = 0x2; + var GESTURE_DRAG = 0x4; + var GESTURE_DROP = 0x8; + var GESTURE_FLICK = 0x10; + var GESTURE_LONG_PRESS_REPEATED = 0x20; + var GESTURE_PINCH = 0x40; + var GESTURE_DOUBLE_TAP = 0x80; + var GESTURE_RECOGNITION_START = 0x4000; + var GESTURE_RECOGNITION_END = 0x8000; + + var suppressKeyEvents = false; + + function keyPress(keyCode) { + if (!suppressKeyEvents) { + sendNativeEvent({ type: KEY_EVENT, intParam1: PRESSED, intParam2: keyCode, intParam3: 0, intParam4: displayId }, foregroundIsolateId); + } }; -MIDP.keyRelease = function(keyCode) { - if (!MIDP.suppressKeyEvents) - MIDP.sendNativeEvent({ type: MIDP.KEY_EVENT, intParam1: MIDP.RELEASED, intParam2: keyCode, intParam3: 0, intParam4: MIDP.displayId }, MIDP.foregroundIsolateId); +function keyRelease(keyCode) { + if (!suppressKeyEvents) + sendNativeEvent({ type: KEY_EVENT, intParam1: RELEASED, intParam2: keyCode, intParam3: 0, intParam4: displayId }, foregroundIsolateId); }; window.addEventListener("keypress", function(ev) { - MIDP.keyPress(ev.which); + keyPress(ev.which); }); window.addEventListener("keyup", function(ev) { - MIDP.keyRelease(ev.which); + keyRelease(ev.which); }); Native["com/sun/midp/events/EventQueue.getNativeEventQueueHandle.()I"] = function() { @@ -1059,7 +1097,7 @@ Native["com/sun/midp/events/EventQueue.getNativeEventQueueHandle.()I"] = functio }; Native["com/sun/midp/events/EventQueue.resetNativeEventQueue.()V"] = function() { - MIDP.nativeEventQueues[$.ctx.runtime.isolate.id] = []; + nativeEventQueues[$.ctx.runtime.isolate.id] = []; }; Native["com/sun/midp/events/EventQueue.sendNativeEventToIsolate.(Lcom/sun/midp/events/NativeEvent;I)V"] = @@ -1072,21 +1110,21 @@ function(obj, isolateId) { e[field.name] = field.get(obj); } - MIDP.sendNativeEvent(e, isolateId); + sendNativeEvent(e, isolateId); }; Native["com/sun/midp/events/NativeEventMonitor.waitForNativeEvent.(Lcom/sun/midp/events/NativeEvent;)I"] = function(nativeEvent) { var isolateId = $.ctx.runtime.isolate.id; - var nativeEventQueue = MIDP.nativeEventQueues[isolateId]; + var nativeEventQueue = nativeEventQueues[isolateId]; if (nativeEventQueue.length !== 0) { - MIDP.copyEvent(nativeEventQueue.shift(), nativeEvent); + copyEvent(nativeEventQueue.shift(), nativeEvent); return nativeEventQueue.length; } asyncImpl("I", new Promise(function(resolve, reject) { - MIDP.waitingNativeEventQueue[isolateId] = { + waitingNativeEventQueue[isolateId] = { resolve: resolve, nativeEvent: nativeEvent, }; @@ -1096,18 +1134,18 @@ function(nativeEvent) { Native["com/sun/midp/events/NativeEventMonitor.readNativeEvent.(Lcom/sun/midp/events/NativeEvent;)Z"] = function(obj) { var isolateId = $.ctx.runtime.isolate.id; - var nativeEventQueue = MIDP.nativeEventQueues[isolateId]; + var nativeEventQueue = nativeEventQueues[isolateId]; if (!nativeEventQueue.length) { return 0; } - MIDP.copyEvent(nativeEventQueue.shift(), obj); + copyEvent(nativeEventQueue.shift(), obj); return 1; }; -MIDP.localizedStrings = new Map(); +var localizedStrings = new Map(); Native["com/sun/midp/l10n/LocalizedStringsBase.getContent.(I)Ljava/lang/String;"] = function(id) { - if (MIDP.localizedStrings.size === 0) { + if (localizedStrings.size === 0) { // First build up a mapping of field names to field IDs var classInfo = CLASSES.getClass("com/sun/midp/i18n/ResourceConstants"); var constantsMap = new Map(); @@ -1127,11 +1165,11 @@ Native["com/sun/midp/l10n/LocalizedStringsBase.getContent.(I)Ljava/lang/String;" var attrs = entries[n].attributes; // map the key value to a field ID var id = constantsMap.get(attrs.Key.value); - MIDP.localizedStrings.set(id, attrs.Value.value); + localizedStrings.set(id, attrs.Value.value); } } - var value = MIDP.localizedStrings.get(id); + var value = localizedStrings.get(id); if (!value) { throw $.newIllegalStateException(); @@ -1140,12 +1178,12 @@ Native["com/sun/midp/l10n/LocalizedStringsBase.getContent.(I)Ljava/lang/String;" return J2ME.newString(value); }; -Native["javax/microedition/lcdui/Display.drawTrustedIcon0.(IZ)V"] = function(displayId, drawTrusted) { - console.warn("Display.drawTrustedIcon0.(IZ)V not implemented (" + displayId + ", " + drawTrusted + ")"); +Native["javax/microedition/lcdui/Display.drawTrustedIcon0.(IZ)V"] = function(dispId, drawTrusted) { + console.warn("Display.drawTrustedIcon0.(IZ)V not implemented (" + dispId + ", " + drawTrusted + ")"); }; Native["com/sun/midp/events/EventQueue.sendShutdownEvent.()V"] = function() { - MIDP.sendNativeEvent({ type: MIDP.EVENT_QUEUE_SHUTDOWN }, $.ctx.runtime.isolate.id); + sendNativeEvent({ type: EVENT_QUEUE_SHUTDOWN }, $.ctx.runtime.isolate.id); }; Native["com/sun/midp/main/CommandState.saveCommandState.(Lcom/sun/midp/main/CommandState;)V"] = function(commandState) { @@ -1154,7 +1192,7 @@ Native["com/sun/midp/main/CommandState.saveCommandState.(Lcom/sun/midp/main/Comm Native["com/sun/midp/main/CommandState.exitInternal.(I)V"] = function(exit) { console.info("Exit: " + exit); - MIDP.exit(); + exit(); }; Native["com/sun/midp/suspend/SuspendSystem$MIDPSystem.allMidletsKilled.()Z"] = function() { @@ -1167,23 +1205,23 @@ Native["com/sun/midp/suspend/SuspendSystem$MIDPSystem.allMidletsKilled.()Z"] = f but we do care about SYSTEM_KEY_CLEAR, so send it when the delete key is pressed. */ -MIDP.SYSTEM_KEY_POWER = 1; -MIDP.SYSTEM_KEY_SEND = 2; -MIDP.SYSTEM_KEY_END = 3; -MIDP.SYSTEM_KEY_CLEAR = 4; +var SYSTEM_KEY_POWER = 1; +var SYSTEM_KEY_SEND = 2; +var SYSTEM_KEY_END = 3; +var SYSTEM_KEY_CLEAR = 4; -MIDP.systemKeyMap = { - 8: MIDP.SYSTEM_KEY_CLEAR, // Backspace - 112: MIDP.SYSTEM_KEY_POWER, // F1 - 116: MIDP.SYSTEM_KEY_SEND, // F5 - 114: MIDP.SYSTEM_KEY_END, // F3 +var systemKeyMap = { + 8: SYSTEM_KEY_CLEAR, // Backspace + 112: SYSTEM_KEY_POWER, // F1 + 116: SYSTEM_KEY_SEND, // F5 + 114: SYSTEM_KEY_END, // F3 }; Native["javax/microedition/lcdui/KeyConverter.getSystemKey.(I)I"] = function(key) { - return MIDP.systemKeyMap[key] || 0; + return systemKeyMap[key] || 0; }; -MIDP.keyMap = { +var keyMap = { 1: 119, // UP 2: 97, // LEFT 5: 100, // RIGHT @@ -1196,10 +1234,10 @@ MIDP.keyMap = { }; Native["javax/microedition/lcdui/KeyConverter.getKeyCode.(I)I"] = function(key) { - return MIDP.keyMap[key] || 0; + return keyMap[key] || 0; }; -MIDP.keyNames = { +var keyNames = { 119: "Up", 97: "Left", 100: "Right", @@ -1212,10 +1250,10 @@ MIDP.keyNames = { }; Native["javax/microedition/lcdui/KeyConverter.getKeyName.(I)Ljava/lang/String;"] = function(keyCode) { - return J2ME.newString((keyCode in MIDP.keyNames) ? MIDP.keyNames[keyCode] : String.fromCharCode(keyCode)); + return J2ME.newString((keyCode in keyNames) ? keyNames[keyCode] : String.fromCharCode(keyCode)); }; -MIDP.gameKeys = { +var gameKeys = { 119: 1, // UP 97: 2, // LEFT 115: 6, // DOWN @@ -1228,23 +1266,23 @@ MIDP.gameKeys = { }; Native["javax/microedition/lcdui/KeyConverter.getGameAction.(I)I"] = function(keyCode) { - return MIDP.gameKeys[keyCode] || 0; + return gameKeys[keyCode] || 0; }; Native["javax/microedition/lcdui/game/GameCanvas.setSuppressKeyEvents.(Ljavax/microedition/lcdui/Canvas;Z)V"] = function(canvas, suppressKeyEvents) { - MIDP.suppressKeyEvents = suppressKeyEvents; + suppressKeyEvents = suppressKeyEvents; }; Native["com/sun/midp/main/MIDletProxyList.resetForegroundInNativeState.()V"] = function() { - MIDP.displayId = -1; + displayId = undefined; }; -Native["com/sun/midp/main/MIDletProxyList.setForegroundInNativeState.(II)V"] = function(isolateId, displayId) { - MIDP.displayId = displayId; - MIDP.foregroundIsolateId = isolateId; +Native["com/sun/midp/main/MIDletProxyList.setForegroundInNativeState.(II)V"] = function(isolateId, dispId) { + displayId = dispId; + foregroundIsolateId = isolateId; }; -MIDP.ConnectionRegistry = { +var connectionRegistry = { // The lastRegistrationId is in common between alarms and push notifications lastRegistrationId: -1, pushRegistrations: [], @@ -1290,7 +1328,7 @@ MIDP.ConnectionRegistry = { Native["com/sun/midp/io/j2me/push/ConnectionRegistry.poll0.(J)I"] = function(time) { asyncImpl("I", new Promise(function(resolve, reject) { - MIDP.ConnectionRegistry.waitForRegistration(function(id) { + connectionRegistry.waitForRegistration(function(id) { resolve(id); }); })); @@ -1301,7 +1339,7 @@ Native["com/sun/midp/io/j2me/push/ConnectionRegistry.add0.(Ljava/lang/String;)I" console.warn("ConnectionRegistry.add0.(IL...String;)I isn't completely implemented"); - MIDP.ConnectionRegistry.addConnection({ + connectionRegistry.addConnection({ connection: values[0], midlet: values[1], filter: values[2], @@ -1316,7 +1354,7 @@ Native["com/sun/midp/io/j2me/push/ConnectionRegistry.addAlarm0.([BJ)J"] = functi var lastAlarm = 0; var id = null; - var alarms = MIDP.ConnectionRegistry.alarms; + var alarms = connectionRegistry.alarms; for (var i = 0; i < alarms.length; i++) { if (alarms[i].midlet == midlet) { if (time != 0) { @@ -1332,7 +1370,7 @@ Native["com/sun/midp/io/j2me/push/ConnectionRegistry.addAlarm0.([BJ)J"] = functi } if (lastAlarm == 0 && time != 0) { - id = MIDP.ConnectionRegistry.addAlarm({ + id = connectionRegistry.addAlarm({ midlet: midlet, time: time }); @@ -1345,7 +1383,7 @@ Native["com/sun/midp/io/j2me/push/ConnectionRegistry.addAlarm0.([BJ)J"] = functi } setTimeout(function() { - MIDP.ConnectionRegistry.addReadyRegistration(id); + connectionRegistry.addReadyRegistration(id); }, relativeTime); } @@ -1354,7 +1392,7 @@ Native["com/sun/midp/io/j2me/push/ConnectionRegistry.addAlarm0.([BJ)J"] = functi Native["com/sun/midp/io/j2me/push/ConnectionRegistry.getMIDlet0.(I[BI)I"] = function(handle, regentry, entrysz) { var reg; - var alarms = MIDP.ConnectionRegistry.alarms; + var alarms = connectionRegistry.alarms; for (var i = 0; i < alarms.length; i++) { if (alarms[i].id == handle) { reg = alarms[i]; @@ -1362,7 +1400,7 @@ Native["com/sun/midp/io/j2me/push/ConnectionRegistry.getMIDlet0.(I[BI)I"] = func } if (!reg) { - var pushRegistrations = MIDP.ConnectionRegistry.pushRegistrations; + var pushRegistrations = connectionRegistry.pushRegistrations; for (var i = 0; i < pushRegistrations.length; i++) { if (pushRegistrations[i].id == handle) { reg = pushRegistrations[i]; @@ -1446,13 +1484,13 @@ Native["com/nokia/mid/ui/VirtualKeyboard.getCustomKeyboardControl.()Lcom/nokia/m throw $.newIllegalArgumentException("VirtualKeyboard::getCustomKeyboardControl() not implemented") }; -MIDP.keyboardVisibilityListener = null; +var keyboardVisibilityListener = null; Native["com/nokia/mid/ui/VirtualKeyboard.setVisibilityListener.(Lcom/nokia/mid/ui/KeyboardVisibilityListener;)V"] = function(listener) { - MIDP.keyboardVisibilityListener = listener; + keyboardVisibilityListener = listener; }; Native["javax/microedition/lcdui/Display.getKeyboardVisibilityListener.()Lcom/nokia/mid/ui/KeyboardVisibilityListener;"] = function() { - return MIDP.keyboardVisibilityListener; + return keyboardVisibilityListener; }; Native["com/nokia/mid/ui/VirtualKeyboard.isVisible.()Z"] = function() { @@ -1466,7 +1504,7 @@ Native["com/nokia/mid/ui/VirtualKeyboard.getXPosition.()I"] = function() { Native["com/nokia/mid/ui/VirtualKeyboard.getYPosition.()I"] = function() { // We should return the number of pixels between the top of the // screen and the top of the keyboard - return MIDP.Context2D.canvas.height - MIDP.getKeyboardHeight(); + return canvas.height - getKeyboardHeight(); }; Native["com/nokia/mid/ui/VirtualKeyboard.getWidth.()I"] = function() { @@ -1475,9 +1513,24 @@ Native["com/nokia/mid/ui/VirtualKeyboard.getWidth.()I"] = function() { }; Native["com/nokia/mid/ui/VirtualKeyboard.getHeight.()I"] = function() { - return MIDP.getKeyboardHeight(); + return getKeyboardHeight(); }; -MIDP.getKeyboardHeight = function() { - return MIDP.physicalScreenHeight - window.innerHeight; +function getKeyboardHeight() { + return physicalScreenHeight - window.innerHeight; }; + + return { + isVKVisible: isVKVisible, + setFullScreen: setFullScreen, + manifest: manifest, + sendCommandEvent: sendCommandEvent, + sendVirtualKeyboardEvent: sendVirtualKeyboardEvent, + sendEndOfMediaEvent: sendEndOfMediaEvent, + sendMediaSnapshotFinishedEvent: sendMediaSnapshotFinishedEvent, + keyPress: keyPress, + keyRelease: keyRelease, + displayId: displayId, + context2D: canvas.getContext("2d"), + }; +})(); diff --git a/midp/sensor.js b/midp/sensor.js index de5ede13..2907e4c7 100644 --- a/midp/sensor.js +++ b/midp/sensor.js @@ -78,7 +78,7 @@ AccelerometerSensor.simulator = { start: function() { var currentMouseX = -1; var currentMouseY = -1; - var c = MIDP.Context2D.canvas; + var c = MIDP.context2D.canvas; c.onmousemove = function(ev) { currentMouseX =ev.layerX; currentMouseY =ev.layerY; @@ -113,7 +113,7 @@ AccelerometerSensor.simulator = { }, stop: function() { - MIDP.Context2D.canvas.onmousemove = null; + MIDP.context2D.canvas.onmousemove = null; clearInterval(this._interalId); } }; diff --git a/midp/text_editor.js b/midp/text_editor.js index 816c416f..49d655ba 100644 --- a/midp/text_editor.js +++ b/midp/text_editor.js @@ -184,7 +184,7 @@ var TextEditorProvider = (function() { setPosition: function(left, top) { this.left = left; this.top = top; - var t = MIDP.Context2D.canvas.offsetTop + top; + var t = MIDP.context2D.canvas.offsetTop + top; this._setStyle("left", left + "px"); this._setStyle("top", t + "px"); }, diff --git a/native.js b/native.js index 94c80d5a..dbcb6b29 100644 --- a/native.js +++ b/native.js @@ -1008,8 +1008,8 @@ Native["com/nokia/mid/impl/jms/core/Launcher.handleContent.(Ljava/lang/String;)V mask.style.position = "absolute"; mask.style.top = 0; mask.style.left = 0; - mask.style.height = MIDP.Context2D.canvas.height + "px"; - mask.style.width = MIDP.Context2D.canvas.width + "px"; + mask.style.height = MIDP.context2D.canvas.height + "px"; + mask.style.width = MIDP.context2D.canvas.width + "px"; mask.style.backgroundColor = "#000"; mask.style.backgroundPosition = "center center"; mask.style.backgroundRepeat = "no-repeat"; From 7fdd1f9acd240f5034d0f63df1bd8024b4fd4d11 Mon Sep 17 00:00:00 2001 From: Tim Abraldes Date: Thu, 12 Feb 2015 18:03:20 -0800 Subject: [PATCH 2/4] Whitespace-only change. Fix up midp.js --- midp/midp.js | 1576 +++++++++++++++++++++++++------------------------- 1 file changed, 788 insertions(+), 788 deletions(-) diff --git a/midp/midp.js b/midp/midp.js index ab076eac..86129957 100644 --- a/midp/midp.js +++ b/midp/midp.js @@ -7,10 +7,10 @@ var MIDP = (function() { var canvas = document.getElementById("canvas"); var isFullScreen = true; -function setFullScreen(isFS) { + function setFullScreen(isFS) { isFullScreen = isFS; updateCanvas(); -}; + }; function updateCanvas() { var sidebar = document.getElementById("sidebar"); @@ -46,25 +46,27 @@ function setFullScreen(isFS) { } else { console.warn("Unhandled resize event!"); } -}; + }; var manifest = {}; -Native["com/sun/midp/jarutil/JarReader.readJarEntry0.(Ljava/lang/String;Ljava/lang/String;)[B"] = function(jar, entryName) { + Native["com/sun/midp/jarutil/JarReader.readJarEntry0.(Ljava/lang/String;Ljava/lang/String;)[B"] = function(jar, entryName) { var bytes = CLASSES.loadFileFromJar(util.fromJavaString(jar), util.fromJavaString(entryName)); - if (!bytes) - throw $.newIOException(); + if (!bytes) { + throw $.newIOException(); + } var length = bytes.byteLength; var data = new Int8Array(bytes); var array = J2ME.newByteArray(length); - for (var n = 0; n < length; ++n) - array[n] = data[n]; + for (var n = 0; n < length; ++n) { + array[n] = data[n]; + } return array; -}; + }; -Native["com/sun/midp/log/LoggingBase.report.(IILjava/lang/String;)V"] = function(severity, channelID, message) { + Native["com/sun/midp/log/LoggingBase.report.(IILjava/lang/String;)V"] = function(severity, channelID, message) { console.info(util.fromJavaString(message)); -}; + }; var groupTBL = [ "net_access", @@ -85,118 +87,118 @@ Native["com/sun/midp/log/LoggingBase.report.(IILjava/lang/String;)V"] = function "satsa" ]; -Native["com/sun/midp/security/Permissions.loadGroupList.()[Ljava/lang/String;"] = function() { + Native["com/sun/midp/security/Permissions.loadGroupList.()[Ljava/lang/String;"] = function() { var list = J2ME.newStringArray(groupTBL.length); groupTBL.forEach(function (e, n) { - list[n] = J2ME.newString(e); + list[n] = J2ME.newString(e); }); return list; -}; + }; var messagesTBL = [ - ["Airtime", - "How often should %1 ask for permission to use airtime? Using airtime may result in charges.", - "Don't use airtime and don't ask", - "Is it OK to Use Airtime?", - "%1 wants to send and receive data using the network. This will use airtime and may result in charges.\n\nIs it OK to use airtime?", - ], - ["Network", - "How often should %1 ask for permission to use network? Using network may result in charges.", - "Don't use network and don't ask", - "Is it OK to Use Network?", - "%1 wants to send and receive data using the network. This will use airtime and may result in charges.\n\nIs it OK to use network?" - ], - ["Restricted Network Connections", - "How often should %1 ask for permission to open a restricted network connection?", - "Don't open any restricted connections and don't ask", - "Is it OK to open a restricted network connection?", - "%1 wants to open a restricted network connection.\n\nIs it OK to open a restricted network connection?" - ], - ["Auto-Start Registration", - "How often should %1 ask for permission to register itself to automatically start?", - "Don't register and don't ask", - "Is it OK to automatically start the application?", - "%1 wants to register itself to be automatically started.\n\nIs it OK to register to be automatically started?" - ], - ["Computer Connection", - "How often should %1 ask for permission to connect to a computer? This may require a data cable that came with your phone.", - "Don't connect and don't ask", - "Is it OK to Connect?", - "%1 wants to connect to a computer. This may require a data cable.\n\nIs it OK to make a connection?" - ], - ["Messaging", - "How often should %1 ask for permission before sending or receiving text messages?", - "Don't send or receive messages and don't ask", - "Is it OK to Send Messages?", - "%1 wants to send text message(s). This could result in charges.%3 message(s) will be sent to %2.\n\nIs it OK to send messages?" - ], - ["Secured Messaging", - "How often should %1 ask for permission before sending or receiving secured text messages?", - "Don't send or receive secured messages and don't ask", - "Is it OK to Send secured Messages?", - "%1 wants to send text secured message(s). This could result in charges.%3 message(s) will be sent to %2.\n\nIs it OK to send messages?" - ], - ["Recording", - "How often should %1 ask for permission to record audio and images? This will use space on your phone.", - "Don't record and don't ask", - "Is it OK to Record?", - "%1 wants to record an image or audio clip.\n\nIs it OK to record?" - ], - ["Read Personal Data", - "How often should %1 ask for permission to read your personal data (contacts, appointments, etc)?", - "Don't read my data and don't ask", - "Is it OK to read your personal data?", - "%1 wants to read your personal data (contacts, appointments, etc)\n\nIs it OK to read your personal data?" - ], - ["Update Personal Data", - "How often should %1 ask for permission to update your personal data (contacts, appointments, etc)?", - "Don't update my data and don't ask", - "Is it OK to update your personal data?", - "%1 wants to update your personal data (contacts, appointments, etc)\n\nIs it OK to update your personal data?", - "%1 wants to update %2\n\nIs it OK to update this data?" - ], - ["Obtain Current Location", - "How often should %1 ask for permission to obtain your location?", - "Don't give my location and don't ask", - "Is it OK to obtain your location?", - "Application %1 wants to obtain your the location.\n\nIs it OK to obtain your location?" - ], - ["Access Landmark Database", - "How often should %1 ask for permission to access your landmark database?", - "Don't access my landmark database and don't ask", - "Is it OK to access your landmark database?", - "Application %1 wants to access your landmark database.\n\nIs it OK to access your landmark database?" - ], - ["payment"], - ["Personal Indentification", - "How often should %1 ask for permission to use your smart card to identify you?", - "Don't sign and don't ask", - "Is it OK to obtain your personal signature?", - "%1 wants to obtain your personal digital signature.\n\nIs it OK to obtain your personal signature?\nContent to be signed:\n\n%3" - ], - ["Smart Card Communication", - "How often should %1 ask for permission to access your smart card?", - "Don't access my smart card and don't ask", - "Is it OK to access your smart card?", - "Application %1 wants to access your smart card.\n\nIs it OK to access your smart card?" - ], - ["satsa"] -]; + ["Airtime", + "How often should %1 ask for permission to use airtime? Using airtime may result in charges.", + "Don't use airtime and don't ask", + "Is it OK to Use Airtime?", + "%1 wants to send and receive data using the network. This will use airtime and may result in charges.\n\nIs it OK to use airtime?", + ], + ["Network", + "How often should %1 ask for permission to use network? Using network may result in charges.", + "Don't use network and don't ask", + "Is it OK to Use Network?", + "%1 wants to send and receive data using the network. This will use airtime and may result in charges.\n\nIs it OK to use network?" + ], + ["Restricted Network Connections", + "How often should %1 ask for permission to open a restricted network connection?", + "Don't open any restricted connections and don't ask", + "Is it OK to open a restricted network connection?", + "%1 wants to open a restricted network connection.\n\nIs it OK to open a restricted network connection?" + ], + ["Auto-Start Registration", + "How often should %1 ask for permission to register itself to automatically start?", + "Don't register and don't ask", + "Is it OK to automatically start the application?", + "%1 wants to register itself to be automatically started.\n\nIs it OK to register to be automatically started?" + ], + ["Computer Connection", + "How often should %1 ask for permission to connect to a computer? This may require a data cable that came with your phone.", + "Don't connect and don't ask", + "Is it OK to Connect?", + "%1 wants to connect to a computer. This may require a data cable.\n\nIs it OK to make a connection?" + ], + ["Messaging", + "How often should %1 ask for permission before sending or receiving text messages?", + "Don't send or receive messages and don't ask", + "Is it OK to Send Messages?", + "%1 wants to send text message(s). This could result in charges.%3 message(s) will be sent to %2.\n\nIs it OK to send messages?" + ], + ["Secured Messaging", + "How often should %1 ask for permission before sending or receiving secured text messages?", + "Don't send or receive secured messages and don't ask", + "Is it OK to Send secured Messages?", + "%1 wants to send text secured message(s). This could result in charges.%3 message(s) will be sent to %2.\n\nIs it OK to send messages?" + ], + ["Recording", + "How often should %1 ask for permission to record audio and images? This will use space on your phone.", + "Don't record and don't ask", + "Is it OK to Record?", + "%1 wants to record an image or audio clip.\n\nIs it OK to record?" + ], + ["Read Personal Data", + "How often should %1 ask for permission to read your personal data (contacts, appointments, etc)?", + "Don't read my data and don't ask", + "Is it OK to read your personal data?", + "%1 wants to read your personal data (contacts, appointments, etc)\n\nIs it OK to read your personal data?" + ], + ["Update Personal Data", + "How often should %1 ask for permission to update your personal data (contacts, appointments, etc)?", + "Don't update my data and don't ask", + "Is it OK to update your personal data?", + "%1 wants to update your personal data (contacts, appointments, etc)\n\nIs it OK to update your personal data?", + "%1 wants to update %2\n\nIs it OK to update this data?" + ], + ["Obtain Current Location", + "How often should %1 ask for permission to obtain your location?", + "Don't give my location and don't ask", + "Is it OK to obtain your location?", + "Application %1 wants to obtain your the location.\n\nIs it OK to obtain your location?" + ], + ["Access Landmark Database", + "How often should %1 ask for permission to access your landmark database?", + "Don't access my landmark database and don't ask", + "Is it OK to access your landmark database?", + "Application %1 wants to access your landmark database.\n\nIs it OK to access your landmark database?" + ], + ["payment"], + ["Personal Indentification", + "How often should %1 ask for permission to use your smart card to identify you?", + "Don't sign and don't ask", + "Is it OK to obtain your personal signature?", + "%1 wants to obtain your personal digital signature.\n\nIs it OK to obtain your personal signature?\nContent to be signed:\n\n%3" + ], + ["Smart Card Communication", + "How often should %1 ask for permission to access your smart card?", + "Don't access my smart card and don't ask", + "Is it OK to access your smart card?", + "Application %1 wants to access your smart card.\n\nIs it OK to access your smart card?" + ], + ["satsa"] + ]; -Native["com/sun/midp/security/Permissions.getGroupMessages.(Ljava/lang/String;)[Ljava/lang/String;"] = function(jName) { + Native["com/sun/midp/security/Permissions.getGroupMessages.(Ljava/lang/String;)[Ljava/lang/String;"] = function(jName) { var name = util.fromJavaString(jName); var list = null; groupTBL.forEach(function(e, n) { - if (e === name) { - var messages = messagesTBL[n]; - list = J2ME.newStringArray(messages.length); - messages.forEach(function (e, n) { - list[n] = J2ME.newString(e); - }); - } + if (e === name) { + var messages = messagesTBL[n]; + list = J2ME.newStringArray(messages.length); + messages.forEach(function (e, n) { + list[n] = J2ME.newString(e); + }); + } }); return list; -}; + }; var membersTBL = [ ["javax.microedition.io.Connector.http", @@ -249,51 +251,51 @@ Native["com/sun/midp/security/Permissions.getGroupMessages.(Ljava/lang/String;)[ ["javax.microedition.apdu.aid", "javax.microedition.jcrmi"], ["javax.microedition.apdu.sat"], -]; + ]; -Native["com/sun/midp/security/Permissions.loadGroupPermissions.(Ljava/lang/String;)[Ljava/lang/String;"] = function(jName) { + Native["com/sun/midp/security/Permissions.loadGroupPermissions.(Ljava/lang/String;)[Ljava/lang/String;"] = function(jName) { var name = util.fromJavaString(jName); var list = null; groupTBL.forEach(function(e, n) { - if (e === name) { - var members = membersTBL[n]; - list = J2ME.newStringArray(members.length); - members.forEach(function (e, n) { - list[n] = J2ME.newString(e); - }); - } + if (e === name) { + var members = membersTBL[n]; + list = J2ME.newStringArray(members.length); + members.forEach(function (e, n) { + list[n] = J2ME.newString(e); + }); + } }); return list; -}; + }; -Native["com/sun/midp/main/CldcPlatformRequest.dispatchPlatformRequest.(Ljava/lang/String;)Z"] = function(request) { + Native["com/sun/midp/main/CldcPlatformRequest.dispatchPlatformRequest.(Ljava/lang/String;)Z"] = function(request) { request = util.fromJavaString(request); if (request.startsWith("http://") || request.startsWith("https://")) { - if (request.endsWith(".jad")) { - // The download will start after the MIDlet has terminated its execution. - pendingMIDletUpdate = request; - return 1; - } else { - DumbPipe.close(DumbPipe.open("windowOpen", request)); - } + if (request.endsWith(".jad")) { + // The download will start after the MIDlet has terminated its execution. + pendingMIDletUpdate = request; + return 1; + } else { + DumbPipe.close(DumbPipe.open("windowOpen", request)); + } } else if (request.startsWith("x-contacts:add?number=")) { - new MozActivity({ - name: "new", - data: { - type: "webcontacts/contact", - params: { - tel: request.substring(22), - }, - }, - }); + new MozActivity({ + name: "new", + data: { + type: "webcontacts/contact", + params: { + tel: request.substring(22), + }, + }, + }); } else { console.warn("com/sun/midp/main/CldcPlatformRequest.dispatchPlatformRequest.(Ljava/lang/String;)Z not implemented for: " + request); } return 0; -}; + }; -Native["com/sun/midp/main/CommandState.restoreCommandState.(Lcom/sun/midp/main/CommandState;)V"] = function(state) { + Native["com/sun/midp/main/CommandState.restoreCommandState.(Lcom/sun/midp/main/CommandState;)V"] = function(state) { var suiteId = (config.midletClassName === "internal") ? -1 : 1; state.klass.classInfo.getField("I.suiteId.I").set(state, suiteId); state.klass.classInfo.getField("I.midletClassName.Ljava/lang/String;").set(state, J2ME.newString(config.midletClassName)); @@ -301,7 +303,7 @@ Native["com/sun/midp/main/CommandState.restoreCommandState.(Lcom/sun/midp/main/C state.klass.classInfo.getField("I.arg0.Ljava/lang/String;").set(state, J2ME.newString((args.length > 0) ? args[0] : "")); state.klass.classInfo.getField("I.arg1.Ljava/lang/String;").set(state, J2ME.newString((args.length > 1) ? args[1] : "")); state.klass.classInfo.getField("I.arg2.Ljava/lang/String;").set(state, J2ME.newString((args.length > 2) ? args[2] : "")); -}; + }; var domainTBL = [ "manufacturer", @@ -310,21 +312,21 @@ Native["com/sun/midp/main/CommandState.restoreCommandState.(Lcom/sun/midp/main/C "unidentified_third_party,unsecured", "minimum,unsecured", "maximum,unsecured", -]; + ]; -Native["com/sun/midp/security/Permissions.loadDomainList.()[Ljava/lang/String;"] = function() { + Native["com/sun/midp/security/Permissions.loadDomainList.()[Ljava/lang/String;"] = function() { var list = J2ME.newStringArray(domainTBL.length); domainTBL.forEach(function (e, n) { - list[n] = J2ME.newString(e); + list[n] = J2ME.newString(e); }); return list; -}; + }; -var NEVER = 0; -var ALLOW = 1; -var BLANKET = 4; -var SESSION = 8; -var ONESHOT = 16; + var NEVER = 0; + var ALLOW = 1; + var BLANKET = 4; + var SESSION = 8; + var ONESHOT = 16; var identifiedTBL = { net_access: { max: BLANKET, default: SESSION}, @@ -364,141 +366,142 @@ var ONESHOT = 16; satsa: { max: NEVER, default: NEVER}, }; -Native["com/sun/midp/security/Permissions.getDefaultValue.(Ljava/lang/String;Ljava/lang/String;)B"] = function(domain, group) { + Native["com/sun/midp/security/Permissions.getDefaultValue.(Ljava/lang/String;Ljava/lang/String;)B"] = function(domain, group) { var allow = NEVER; switch (util.fromJavaString(domain)) { - case "manufacturer": - case "maximum": - case "operator": + case "manufacturer": + case "maximum": + case "operator": allow = ALLOW; break; - case "identified_third_party": + case "identified_third_party": allow = identifiedTBL[util.fromJavaString(group)].default; break; - case "unidentified_third_party": + case "unidentified_third_party": allow = unidentifiedTBL[util.fromJavaString(group)].default; break; } return allow; -}; + }; -Native["com/sun/midp/security/Permissions.getMaxValue.(Ljava/lang/String;Ljava/lang/String;)B"] = function(domain, group) { + Native["com/sun/midp/security/Permissions.getMaxValue.(Ljava/lang/String;Ljava/lang/String;)B"] = function(domain, group) { var allow = NEVER; switch (util.fromJavaString(domain)) { - case "manufacturer": - case "maximum": - case "operator": + case "manufacturer": + case "maximum": + case "operator": allow = ALLOW; break; - case "identified_third_party": + case "identified_third_party": allow = identifiedTBL[util.fromJavaString(group)].max; break; - case "unidentified_third_party": + case "unidentified_third_party": allow = unidentifiedTBL[util.fromJavaString(group)].max; break; } return allow; -}; + }; -Native["com/sun/midp/security/Permissions.loadingFinished.()V"] = function() { + Native["com/sun/midp/security/Permissions.loadingFinished.()V"] = function() { console.warn("Permissions.loadingFinished.()V not implemented"); -}; + }; -Native["com/sun/midp/main/MIDletSuiteUtils.getIsolateId.()I"] = function() { + Native["com/sun/midp/main/MIDletSuiteUtils.getIsolateId.()I"] = function() { return $.ctx.runtime.isolate.id; -}; + }; var AMSIsolateId; -Native["com/sun/midp/main/MIDletSuiteUtils.registerAmsIsolateId.()V"] = function() { + Native["com/sun/midp/main/MIDletSuiteUtils.registerAmsIsolateId.()V"] = function() { AMSIsolateId = $.ctx.runtime.isolate.id; -}; + }; -Native["com/sun/midp/main/MIDletSuiteUtils.getAmsIsolateId.()I"] = function() { + Native["com/sun/midp/main/MIDletSuiteUtils.getAmsIsolateId.()I"] = function() { return AMSIsolateId; -}; + }; -Native["com/sun/midp/main/MIDletSuiteUtils.isAmsIsolate.()Z"] = function() { + Native["com/sun/midp/main/MIDletSuiteUtils.isAmsIsolate.()Z"] = function() { return AMSIsolateId == $.ctx.runtime.isolate.id ? 1 : 0; -}; + }; -Native["com/sun/midp/main/MIDletSuiteUtils.vmBeginStartUp.(I)V"] = function(midletIsolateId) { + Native["com/sun/midp/main/MIDletSuiteUtils.vmBeginStartUp.(I)V"] = function(midletIsolateId) { // See DisplayContainer::createDisplayId, called by the LCDUIEnvironment constructor, // called by CldcMIDletSuiteLoader::createSuiteEnvironment. // The formula depens on the ID of the isolate that calls createDisplayId, that is // the same isolate that calls vmBeginStartUp. So this is a good place to calculate // the display ID. displayId = ((midletIsolateId & 0xff)<<24) | (1 & 0x00ffffff); -}; + }; -Native["com/sun/midp/main/MIDletSuiteUtils.vmEndStartUp.(I)V"] = function(midletIsolateId) { -}; + Native["com/sun/midp/main/MIDletSuiteUtils.vmEndStartUp.(I)V"] = function(midletIsolateId) { + }; -Native["com/sun/midp/main/AppIsolateMIDletSuiteLoader.allocateReservedResources0.()Z"] = function() { - return 1; -}; + Native["com/sun/midp/main/AppIsolateMIDletSuiteLoader.allocateReservedResources0.()Z"] = function() { + return 1; + }; -Native["com/sun/midp/main/Configuration.getProperty0.(Ljava/lang/String;)Ljava/lang/String;"] = function(key) { + Native["com/sun/midp/main/Configuration.getProperty0.(Ljava/lang/String;)Ljava/lang/String;"] = function(key) { var value; switch (util.fromJavaString(key)) { - case "com.sun.midp.publickeystore.WebPublicKeyStore": + case "com.sun.midp.publickeystore.WebPublicKeyStore": if (config.midletClassName == "RunTests") { value = "_test.ks"; } else { value = "_main.ks"; } break; - case "com.sun.midp.events.dispatchTableInitSize": + case "com.sun.midp.events.dispatchTableInitSize": value = "71"; break; - case "microedition.locale": + case "microedition.locale": value = navigator.language; break; - case "datagram": + case "datagram": value = "com.sun.midp.io.j2me.datagram.ProtocolPushImpl"; break; - case "com.sun.midp.io.j2me.socket.buffersize": + case "com.sun.midp.io.j2me.socket.buffersize": value = null; break; - case "com.sun.midp.io.http.proxy": + case "com.sun.midp.io.http.proxy": value = null; break; - case "com.sun.midp.io.http.force_non_persistent": + case "com.sun.midp.io.http.force_non_persistent": value = null; break; - case "com.sun.midp.io.http.max_persistent_connections": + case "com.sun.midp.io.http.max_persistent_connections": value = null; break; - case "com.sun.midp.io.http.persistent_connection_linger_time": + case "com.sun.midp.io.http.persistent_connection_linger_time": value = null; break; - case "com.sun.midp.io.http.input_buffer_size": + case "com.sun.midp.io.http.input_buffer_size": value = null; break; - case "com.sun.midp.io.http.output_buffer_size": + case "com.sun.midp.io.http.output_buffer_size": value = null; break; - default: + default: console.warn("UNKNOWN PROPERTY (com/sun/midp/main/Configuration): " + util.fromJavaString(key)); value = null; break; } return J2ME.newString(value); -}; + }; -Native["com/sun/midp/util/ResourceHandler.loadRomizedResource0.(Ljava/lang/String;)[B"] = function(file) { + Native["com/sun/midp/util/ResourceHandler.loadRomizedResource0.(Ljava/lang/String;)[B"] = function(file) { var fileName = "assets/0/" + util.fromJavaString(file).replace("_", ".").replace("_png", ".png").replace("_raw", ".raw"); var data = CLASSES.loadFile(fileName); if (!data) { - console.warn("ResourceHandler::loadRomizedResource0: file " + fileName + " not found"); - return null; + console.warn("ResourceHandler::loadRomizedResource0: file " + fileName + " not found"); + return null; } var len = data.byteLength; var bytes = J2ME.newByteArray(len); var src = new Int8Array(data); - for (var n = 0; n < bytes.byteLength; ++n) - bytes[n] = src[n]; + for (var n = 0; n < bytes.byteLength; ++n) { + bytes[n] = src[n]; + } return bytes; -}; + }; var verticalChrome; var horizontalChrome; @@ -506,316 +509,314 @@ Native["com/sun/midp/util/ResourceHandler.loadRomizedResource0.(Ljava/lang/Strin var physicalScreenHeight; var lastWindowInnerHeight; var isVKVisible; - if (config.autosize && !/no|0/.test(config.autosize)) { - document.documentElement.classList.add('autosize'); + if (config.autosize && !/no|0/.test(config.autosize)) { + document.documentElement.classList.add('autosize'); - // Chrome amounts: - // The difference between the outer[Height|Width] and the actual - // amount of space we have available. So far, horizontalChrome is - // always 0 and verticalChrome is always the size of the status bar, - // which has been 30px in testing. These are assumed to be static - // throughout the lifetime of the app, and things will break if that - // assumption is violated. - verticalChrome = window.outerHeight - window.innerHeight; - horizontalChrome = window.outerWidth - window.innerWidth; + // Chrome amounts: + // The difference between the outer[Height|Width] and the actual + // amount of space we have available. So far, horizontalChrome is + // always 0 and verticalChrome is always the size of the status bar, + // which has been 30px in testing. These are assumed to be static + // throughout the lifetime of the app, and things will break if that + // assumption is violated. + verticalChrome = window.outerHeight - window.innerHeight; + horizontalChrome = window.outerWidth - window.innerWidth; - // "Physical" dimensions: - // The amount of space available to J2ME. This is always the - // outer[Height|Width] minus the [vertical|horizontal]Chrome amount. - // - // Note that these values will not always equal the size of our window. - // Specifically, when the FxOS keyboard is visible, the window shrinks, - // so `window.inner[Height|Width]` will be - // smaller than these values. J2ME apps expect that the keyboard - // overlaps the window rather than squishing it, so we simulate that - // by keeping track of these "physical" values. - // - // Note also that these values do not take into account the size of - // the header, which might shrink our canvas. To find out how much - // space is actually available to the current MIDlet, check - // `document.getElementById("canvas").[width|height]`. - physicalScreenWidth = window.outerWidth - horizontalChrome; - physicalScreenHeight = window.outerHeight - verticalChrome; + // "Physical" dimensions: + // The amount of space available to J2ME. This is always the + // outer[Height|Width] minus the [vertical|horizontal]Chrome amount. + // + // Note that these values will not always equal the size of our window. + // Specifically, when the FxOS keyboard is visible, the window shrinks, + // so `window.inner[Height|Width]` will be + // smaller than these values. J2ME apps expect that the keyboard + // overlaps the window rather than squishing it, so we simulate that + // by keeping track of these "physical" values. + // + // Note also that these values do not take into account the size of + // the header, which might shrink our canvas. To find out how much + // space is actually available to the current MIDlet, check + // `document.getElementById("canvas").[width|height]`. + physicalScreenWidth = window.outerWidth - horizontalChrome; + physicalScreenHeight = window.outerHeight - verticalChrome; - // Cached value of `window.innerHeight` so that we can tell when it - // changes. This is useful for determining when to send keyboard - // visibility events. - lastWindowInnerHeight = window.innerHeight; + // Cached value of `window.innerHeight` so that we can tell when it + // changes. This is useful for determining when to send keyboard + // visibility events. + lastWindowInnerHeight = window.innerHeight; - updateCanvas(); - isVKVisible = function() { - var expectedHeightWithNoKeyboard = window.outerHeight - verticalChrome; - if (window.innerHeight == expectedHeightWithNoKeyboard) { - return false; - } else if (window.innerHeight < expectedHeightWithNoKeyboard) { - return true; + updateCanvas(); + isVKVisible = function() { + var expectedHeightWithNoKeyboard = window.outerHeight - verticalChrome; + if (window.innerHeight == expectedHeightWithNoKeyboard) { + return false; + } else if (window.innerHeight < expectedHeightWithNoKeyboard) { + return true; + } else { + console.warn("window is taller than expected in isVKVisible!"); + return false; + } + }; + window.addEventListener("resize", onWindowResize); + } else { + document.documentElement.classList.add('debug-mode'); + physicalScreenWidth = 240; + physicalScreenHeight = 320; + + updateCanvas(); + isVKVisible = function() { + return false; + }; + } + + function sendPenEvent(pt, whichType) { + sendNativeEvent({ + type: PEN_EVENT, + intParam1: whichType, + intParam2: pt.x, + intParam3: pt.y, + intParam4: displayId + }, foregroundIsolateId); + } + + function sendGestureEvent(pt, distancePt, whichType, aFloatParam1, aIntParam7, aIntParam8, aIntParam9) { + sendNativeEvent({ + type: GESTURE_EVENT, + intParam1: whichType, + intParam2: distancePt && distancePt.x || 0, + intParam3: distancePt && distancePt.y || 0, + intParam4: displayId, + intParam5: pt.x, + intParam6: pt.y, + floatParam1: Math.fround(aFloatParam1 || 0.0), + intParam7: aIntParam7 || 0, + intParam8: aIntParam8 || 0, + intParam9: aIntParam9 || 0, + intParam10: 0, + intParam11: 0, + intParam12: 0, + intParam13: 0, + intParam14: 0, + intParam15: 0, + intParam16: 0 + }, foregroundIsolateId); + } + + // In the simulator and on device, use touch events; in desktop + // mode, we must use mouse events (unless you enable touch events + // in devtools). + var supportsTouch = ("ontouchstart" in document.documentElement); + + // Cache the canvas position for future computation. + var canvasRect = canvas.getBoundingClientRect(); + canvas.addEventListener("canvasresize", function() { + canvasRect = canvas.getBoundingClientRect(); + sendRotationEvent(); + }); + + function getEventPoint(event) { + var item = ((event.touches && event.touches[0]) || // touchstart, touchmove + (event.changedTouches && event.changedTouches[0]) || // touchend + event); // mousedown, mouseup, mousemove + return { + x: item.pageX - (canvasRect.left | 0), + y: item.pageY - (canvasRect.top | 0) + }; + } + + // Input Handling: Some MIDlets (usually older ones) respond to + // "pen" events; others respond to "gesture" events. We must fire + // both. A distance threshold ensures that touches with an "intent + // to tap" will likely result in a tap. + + var LONG_PRESS_TIMEOUT = 1000; + var MIN_DRAG_DISTANCE_SQUARED = 5 * 5; + var mouseDownInfo = null; + var longPressTimeoutID = null; + var longPressDetected = false; + + canvas.addEventListener(supportsTouch ? "touchstart" : "mousedown", function(event) { + event.preventDefault(); // Prevent unnecessary fake mouse events. + var pt = getEventPoint(event); + sendPenEvent(pt, PRESSED); + mouseDownInfo = pt; + + longPressDetected = false; + longPressTimeoutID = setTimeout(function() { + longPressDetected = true; + sendGestureEvent(pt, null, GESTURE_LONG_PRESS); + }, LONG_PRESS_TIMEOUT); + }); + + canvas.addEventListener(supportsTouch ? "touchmove" : "mousemove", function(event) { + if (!mouseDownInfo) { + return; // Mousemove on desktop; ignored. + } + event.preventDefault(); + + if (longPressTimeoutID) { + clearTimeout(longPressTimeoutID); + longPressTimeoutID = null; + } + + var pt = getEventPoint(event); + sendPenEvent(pt, DRAGGED); + var distance = { + x: pt.x - mouseDownInfo.x, + y: pt.y - mouseDownInfo.y + }; + // If this gesture is dragging, or we've moved a substantial + // amount since the original "down" event, begin or continue a + // drag event. Using squared distance to avoid needing sqrt. + if (mouseDownInfo.isDragging || (distance.x * distance.x + distance.y * distance.y > MIN_DRAG_DISTANCE_SQUARED)) { + mouseDownInfo.isDragging = true; + mouseDownInfo.x = pt.x; + mouseDownInfo.y = pt.y; + if (!longPressDetected) { + sendGestureEvent(pt, distance, GESTURE_DRAG); + } + } + + // Just store the dragging event info here, then calc the speed and + // determine whether the gesture is GESTURE_DROP or GESTURE_FLICK in + // the mouseup event listener. + if (!mouseDownInfo.draggingPts) { + mouseDownInfo.draggingPts = []; + } + + // Only store the latest two drag events. + if (mouseDownInfo.draggingPts.length > 1) { + mouseDownInfo.draggingPts.shift(); + } + + mouseDownInfo.draggingPts.push({ + pt: getEventPoint(event), + time: new Date().getTime() + }); + }); + + function calcFlickSpeed() { + var currentDragPT = mouseDownInfo.draggingPts[1]; + var lastDragPT = mouseDownInfo.draggingPts[0]; + + var deltaX = currentDragPT.pt.x - lastDragPT.pt.x; + var deltaY = currentDragPT.pt.y - lastDragPT.pt.y; + var deltaTimeInMs = currentDragPT.time - lastDragPT.time; + + var speedX = Math.round(deltaX * 1000 / deltaTimeInMs); + var speedY = Math.round(deltaY * 1000 / deltaTimeInMs); + var speed = Math.round(Math.sqrt(speedX * speedX + speedY * speedY)); + + var direction = 0; + if (deltaX >= 0 && deltaY >=0) { + direction = Math.atan(deltaY / deltaX); + } else if (deltaX < 0 && deltaY >= 0) { + direction = Math.PI + Math.atan(deltaY / deltaX); + } else if (deltaX < 0 && deltaY < 0) { + direction = Math.atan(deltaY / deltaX) - Math.PI; + } else if (deltaX >= 0 && deltaY < 0) { + direction = Math.atan(deltaY / deltaX); + } + + return { + direction: direction, + speed: speed, + speedX: speedX, + speedY: speedY + }; + } + + // The end listener goes on `document` so that we properly detect touchend/mouseup anywhere. + document.addEventListener(supportsTouch ? "touchend" : "mouseup", function(event) { + if (!mouseDownInfo) { + return; // Touchstart wasn't on the canvas. + } + event.preventDefault(); + + if (longPressTimeoutID) { + clearTimeout(longPressTimeoutID); + longPressTimeoutID = null; + } + + var pt = getEventPoint(event); + sendPenEvent(pt, RELEASED); + + if (!longPressDetected) { + if (mouseDownInfo.isDragging) { + if (mouseDownInfo.draggingPts && mouseDownInfo.draggingPts.length == 2) { + var deltaTime = new Date().getTime() - mouseDownInfo.draggingPts[1].time; + var flickSpeed = calcFlickSpeed(); + // On the real Nokia device, if user touch on the screen and + // move the finger, then stop moving for a while and lift + // the finger, it will trigger a normal GESTURE_DROP instead + // of GESTURE_FLICK event, so let's check if the time gap + // between touchend event and the last touchmove event is + // larger than a threshold. + if (deltaTime > 300 || flickSpeed.speed == 0) { + sendGestureEvent(pt, null, GESTURE_DROP); } else { - console.warn("window is taller than expected in isVKVisible!"); - return false; + sendGestureEvent(pt, null, GESTURE_FLICK, + flickSpeed.direction, + flickSpeed.speed, + flickSpeed.speedX, + flickSpeed.speedY); } - }; - window.addEventListener("resize", onWindowResize); - } else { - document.documentElement.classList.add('debug-mode'); - physicalScreenWidth = 240; - physicalScreenHeight = 320; - - updateCanvas(); - isVKVisible = function() { - return false; - }; + } else { + sendGestureEvent(pt, null, GESTURE_DROP); + } + } else { + sendGestureEvent(pt, null, GESTURE_TAP); + } } - function sendPenEvent(pt, whichType) { - sendNativeEvent({ - type: PEN_EVENT, - intParam1: whichType, - intParam2: pt.x, - intParam3: pt.y, - intParam4: displayId - }, foregroundIsolateId); - } + mouseDownInfo = null; // Clear the way for the next gesture. + }); - function sendGestureEvent(pt, distancePt, whichType, aFloatParam1, aIntParam7, aIntParam8, aIntParam9) { - sendNativeEvent({ - type: GESTURE_EVENT, - intParam1: whichType, - intParam2: distancePt && distancePt.x || 0, - intParam3: distancePt && distancePt.y || 0, - intParam4: displayId, - intParam5: pt.x, - intParam6: pt.y, - floatParam1: Math.fround(aFloatParam1 || 0.0), - intParam7: aIntParam7 || 0, - intParam8: aIntParam8 || 0, - intParam9: aIntParam9 || 0, - intParam10: 0, - intParam11: 0, - intParam12: 0, - intParam13: 0, - intParam14: 0, - intParam15: 0, - intParam16: 0 - }, foregroundIsolateId); - } - - // In the simulator and on device, use touch events; in desktop - // mode, we must use mouse events (unless you enable touch events - // in devtools). - var supportsTouch = ("ontouchstart" in document.documentElement); - - // Cache the canvas position for future computation. - var canvasRect = canvas.getBoundingClientRect(); - canvas.addEventListener("canvasresize", function() { - canvasRect = canvas.getBoundingClientRect(); - sendRotationEvent(); - }); - - function getEventPoint(event) { - var item = ((event.touches && event.touches[0]) || // touchstart, touchmove - (event.changedTouches && event.changedTouches[0]) || // touchend - event); // mousedown, mouseup, mousemove - return { - x: item.pageX - (canvasRect.left | 0), - y: item.pageY - (canvasRect.top | 0) - }; - } - - // Input Handling: Some MIDlets (usually older ones) respond to - // "pen" events; others respond to "gesture" events. We must fire - // both. A distance threshold ensures that touches with an "intent - // to tap" will likely result in a tap. - - var LONG_PRESS_TIMEOUT = 1000; - var MIN_DRAG_DISTANCE_SQUARED = 5 * 5; - var mouseDownInfo = null; - var longPressTimeoutID = null; - var longPressDetected = false; - - canvas.addEventListener(supportsTouch ? "touchstart" : "mousedown", function(event) { - event.preventDefault(); // Prevent unnecessary fake mouse events. - var pt = getEventPoint(event); - sendPenEvent(pt, PRESSED); - mouseDownInfo = pt; - - longPressDetected = false; - longPressTimeoutID = setTimeout(function() { - longPressDetected = true; - sendGestureEvent(pt, null, GESTURE_LONG_PRESS); - }, LONG_PRESS_TIMEOUT); - }); - - canvas.addEventListener(supportsTouch ? "touchmove" : "mousemove", function(event) { - if (!mouseDownInfo) { - return; // Mousemove on desktop; ignored. - } - event.preventDefault(); - - if (longPressTimeoutID) { - clearTimeout(longPressTimeoutID); - longPressTimeoutID = null; - } - - var pt = getEventPoint(event); - sendPenEvent(pt, DRAGGED); - var distance = { - x: pt.x - mouseDownInfo.x, - y: pt.y - mouseDownInfo.y - }; - // If this gesture is dragging, or we've moved a substantial - // amount since the original "down" event, begin or continue a - // drag event. Using squared distance to avoid needing sqrt. - if (mouseDownInfo.isDragging || - (distance.x * distance.x + distance.y * distance.y > MIN_DRAG_DISTANCE_SQUARED)) { - mouseDownInfo.isDragging = true; - mouseDownInfo.x = pt.x; - mouseDownInfo.y = pt.y; - if (!longPressDetected) { - sendGestureEvent(pt, distance, GESTURE_DRAG); - } - } - - // Just store the dragging event info here, then calc the speed and - // determine whether the gesture is GESTURE_DROP or GESTURE_FLICK in - // the mouseup event listener. - if (!mouseDownInfo.draggingPts) { - mouseDownInfo.draggingPts = []; - } - - // Only store the latest two drag events. - if (mouseDownInfo.draggingPts.length > 1) { - mouseDownInfo.draggingPts.shift(); - } - - mouseDownInfo.draggingPts.push({ - pt: getEventPoint(event), - time: new Date().getTime() - }); - }); - - function calcFlickSpeed() { - var currentDragPT = mouseDownInfo.draggingPts[1]; - var lastDragPT = mouseDownInfo.draggingPts[0]; - - var deltaX = currentDragPT.pt.x - lastDragPT.pt.x; - var deltaY = currentDragPT.pt.y - lastDragPT.pt.y; - var deltaTimeInMs = currentDragPT.time - lastDragPT.time; - - var speedX = Math.round(deltaX * 1000 / deltaTimeInMs); - var speedY = Math.round(deltaY * 1000 / deltaTimeInMs); - var speed = Math.round(Math.sqrt(speedX * speedX + speedY * speedY)); - - var direction = 0; - if (deltaX >= 0 && deltaY >=0) { - direction = Math.atan(deltaY / deltaX); - } else if (deltaX < 0 && deltaY >= 0) { - direction = Math.PI + Math.atan(deltaY / deltaX); - } else if (deltaX < 0 && deltaY < 0) { - direction = Math.atan(deltaY / deltaX) - Math.PI; - } else if (deltaX >= 0 && deltaY < 0) { - direction = Math.atan(deltaY / deltaX); - } - - return { - direction: direction, - speed: speed, - speedX: speedX, - speedY: speedY - }; - } - - // The end listener goes on `document` so that we properly detect touchend/mouseup anywhere. - document.addEventListener(supportsTouch ? "touchend" : "mouseup", function(event) { - if (!mouseDownInfo) { - return; // Touchstart wasn't on the canvas. - } - event.preventDefault(); - - if (longPressTimeoutID) { - clearTimeout(longPressTimeoutID); - longPressTimeoutID = null; - } - - var pt = getEventPoint(event); - sendPenEvent(pt, RELEASED); - - if (!longPressDetected) { - if (mouseDownInfo.isDragging) { - if (mouseDownInfo.draggingPts && mouseDownInfo.draggingPts.length == 2) { - var deltaTime = new Date().getTime() - mouseDownInfo.draggingPts[1].time; - var flickSpeed = calcFlickSpeed(); - // On the real Nokia device, if user touch on the screen and - // move the finger, then stop moving for a while and lift - // the finger, it will trigger a normal GESTURE_DROP instead - // of GESTURE_FLICK event, so let's check if the time gap - // between touchend event and the last touchmove event is - // larger than a threshold. - if (deltaTime > 300 || flickSpeed.speed == 0) { - sendGestureEvent(pt, null, GESTURE_DROP); - } else { - sendGestureEvent(pt, null, GESTURE_FLICK, - flickSpeed.direction, - flickSpeed.speed, - flickSpeed.speedX, - flickSpeed.speedY); - } - } else { - sendGestureEvent(pt, null, GESTURE_DROP); - } - } else { - sendGestureEvent(pt, null, GESTURE_TAP); - } - } - - mouseDownInfo = null; // Clear the way for the next gesture. - }); - -Native["com/sun/midp/midletsuite/MIDletSuiteStorage.loadSuitesIcons0.()I"] = function() { + Native["com/sun/midp/midletsuite/MIDletSuiteStorage.loadSuitesIcons0.()I"] = function() { return 0; -}; + }; -Native["com/sun/midp/midletsuite/MIDletSuiteStorage.suiteExists.(I)Z"] = function(id) { + Native["com/sun/midp/midletsuite/MIDletSuiteStorage.suiteExists.(I)Z"] = function(id) { return id <= 1 ? 1 : 0; -}; + }; -Native["com/sun/midp/midletsuite/MIDletSuiteStorage.suiteIdToString.(I)Ljava/lang/String;"] = function(id) { + Native["com/sun/midp/midletsuite/MIDletSuiteStorage.suiteIdToString.(I)Ljava/lang/String;"] = function(id) { return J2ME.newString(id.toString()); -}; + }; -Native["com/sun/midp/midletsuite/MIDletSuiteStorage.getMidletSuiteStorageId.(I)I"] = function(suiteId) { + Native["com/sun/midp/midletsuite/MIDletSuiteStorage.getMidletSuiteStorageId.(I)I"] = function(suiteId) { // We should be able to use the same storage ID for all MIDlet suites. return 0; // storageId -}; + }; -Native["com/sun/midp/midletsuite/MIDletSuiteStorage.getMidletSuiteJarPath.(I)Ljava/lang/String;"] = function(id) { + Native["com/sun/midp/midletsuite/MIDletSuiteStorage.getMidletSuiteJarPath.(I)Ljava/lang/String;"] = function(id) { return J2ME.newString(""); -}; + }; -Native["com/sun/midp/midletsuite/MIDletSuiteImpl.lockMIDletSuite.(IZ)V"] = function(id, lock) { + Native["com/sun/midp/midletsuite/MIDletSuiteImpl.lockMIDletSuite.(IZ)V"] = function(id, lock) { console.warn("MIDletSuiteImpl.lockMIDletSuite.(IZ)V not implemented (" + id + ", " + lock + ")"); -}; + }; -Native["com/sun/midp/midletsuite/MIDletSuiteImpl.unlockMIDletSuite.(I)V"] = function(suiteId) { + Native["com/sun/midp/midletsuite/MIDletSuiteImpl.unlockMIDletSuite.(I)V"] = function(suiteId) { console.warn("MIDletSuiteImpl.unlockMIDletSuite.(I)V not implemented (" + suiteId + ")"); -}; + }; -Native["com/sun/midp/midletsuite/SuiteSettings.load.()V"] = function() { + Native["com/sun/midp/midletsuite/SuiteSettings.load.()V"] = function() { this.klass.classInfo.getField("I.pushInterruptSetting.B").set(this, 1); console.warn("com/sun/midp/midletsuite/SuiteSettings.load.()V incomplete"); -}; + }; -Native["com/sun/midp/midletsuite/SuiteSettings.save0.(IBI[B)V"] = function(suiteId, pushInterruptSetting, pushOptions, permissions) { - console.warn("SuiteSettings.save0.(IBI[B)V not implemented (" + - suiteId + ", " + pushInterruptSetting + ", " + pushOptions + ", " + permissions + ")"); -}; + Native["com/sun/midp/midletsuite/SuiteSettings.save0.(IBI[B)V"] = function(suiteId, pushInterruptSetting, pushOptions, permissions) { + console.warn("SuiteSettings.save0.(IBI[B)V not implemented (" + suiteId + ", " + pushInterruptSetting + ", " + pushOptions + ", " + permissions + ")"); + }; -Native["com/sun/midp/midletsuite/InstallInfo.load.()V"] = function() { + Native["com/sun/midp/midletsuite/InstallInfo.load.()V"] = function() { // The MIDlet has to be trusted for opening SSL connections using port 443. this.klass.classInfo.getField("I.trusted.Z").set(this, 1); console.warn("com/sun/midp/midletsuite/InstallInfo.load.()V incomplete"); -}; + }; -Native["com/sun/midp/midletsuite/SuiteProperties.load.()[Ljava/lang/String;"] = function() { + Native["com/sun/midp/midletsuite/SuiteProperties.load.()[Ljava/lang/String;"] = function() { var keys = Object.keys(manifest); var arr = J2ME.newStringArray(keys.length * 2); var i = 0; @@ -824,25 +825,25 @@ Native["com/sun/midp/midletsuite/SuiteProperties.load.()[Ljava/lang/String;"] = arr[i++] = J2ME.newString(manifest[key]); }); return arr; -}; + }; -Native["javax/microedition/lcdui/SuiteImageCacheImpl.loadAndCreateImmutableImageDataFromCache0.(Ljavax/microedition/lcdui/ImageData;ILjava/lang/String;)Z"] = function(imageData, suiteId, fileName) { + Native["javax/microedition/lcdui/SuiteImageCacheImpl.loadAndCreateImmutableImageDataFromCache0.(Ljavax/microedition/lcdui/ImageData;ILjava/lang/String;)Z"] = function(imageData, suiteId, fileName) { // We're not implementing the cache because looks like it isn't used much. // In a MIDlet I've been testing for a few minutes, there's been only one hit. return 0; -}; + }; -var interIsolateMutexes = []; -var lastInterIsolateMutexID = -1; + var interIsolateMutexes = []; + var lastInterIsolateMutexID = -1; -Native["com/sun/midp/util/isolate/InterIsolateMutex.getID0.(Ljava/lang/String;)I"] = function(jName) { + Native["com/sun/midp/util/isolate/InterIsolateMutex.getID0.(Ljava/lang/String;)I"] = function(jName) { var name = util.fromJavaString(jName); var mutex; for (var i = 0; i < interIsolateMutexes.length; i++) { - if (interIsolateMutexes[i].name === name) { - mutex = interIsolateMutexes[i]; - } + if (interIsolateMutexes[i].name === name) { + mutex = interIsolateMutexes[i]; + } } if (!mutex) { @@ -856,69 +857,69 @@ Native["com/sun/midp/util/isolate/InterIsolateMutex.getID0.(Ljava/lang/String;)I } return mutex.id; -}; + }; -Native["com/sun/midp/util/isolate/InterIsolateMutex.lock0.(I)V"] = function(id) { + Native["com/sun/midp/util/isolate/InterIsolateMutex.lock0.(I)V"] = function(id) { var ctx = $.ctx; var mutex; for (var i = 0; i < interIsolateMutexes.length; i++) { - if (interIsolateMutexes[i].id == id) { - mutex = interIsolateMutexes[i]; - break; - } + if (interIsolateMutexes[i].id == id) { + mutex = interIsolateMutexes[i]; + break; + } } if (!mutex) { - throw $.newIllegalStateException("Invalid mutex ID"); + throw $.newIllegalStateException("Invalid mutex ID"); } if (!mutex.locked) { - mutex.locked = true; - mutex.holder = ctx.runtime.isolate.id; - return; + mutex.locked = true; + mutex.holder = ctx.runtime.isolate.id; + return; } if (mutex.holder == ctx.runtime.isolate.id) { - throw $.newRuntimeException("Attempting to lock mutex twice within the same Isolate"); + throw $.newRuntimeException("Attempting to lock mutex twice within the same Isolate"); } asyncImpl("V", new Promise(function(resolve, reject) { - mutex.waiting.push(function() { - mutex.locked = true; - mutex.holder = ctx.runtime.isolate.id; - resolve(); - }); + mutex.waiting.push(function() { + mutex.locked = true; + mutex.holder = ctx.runtime.isolate.id; + resolve(); + }); })); -}; + }; -Native["com/sun/midp/util/isolate/InterIsolateMutex.unlock0.(I)V"] = function(id) { + Native["com/sun/midp/util/isolate/InterIsolateMutex.unlock0.(I)V"] = function(id) { var mutex; for (var i = 0; i < interIsolateMutexes.length; i++) { - if (interIsolateMutexes[i].id == id) { - mutex = interIsolateMutexes[i]; - break; - } + if (interIsolateMutexes[i].id == id) { + mutex = interIsolateMutexes[i]; + break; + } } if (!mutex) { - throw $.newIllegalStateException("Invalid mutex ID"); + throw $.newIllegalStateException("Invalid mutex ID"); } if (!mutex.locked) { - throw $.newRuntimeException("Mutex is not locked"); + throw $.newRuntimeException("Mutex is not locked"); } if (mutex.holder !== $.ctx.runtime.isolate.id) { - throw $.newRuntimeException("Mutex is locked by different Isolate"); + throw $.newRuntimeException("Mutex is locked by different Isolate"); } mutex.locked = false; var firstWaiting = mutex.waiting.shift(); if (firstWaiting) { - firstWaiting(); + firstWaiting(); } -}; + }; function exit(code) { $.stop(); @@ -928,11 +929,11 @@ Native["com/sun/midp/util/isolate/InterIsolateMutex.unlock0.(I)V"] = function(id var pendingMIDletUpdate = null; -Native["com/sun/cldc/isolate/Isolate.stop.(II)V"] = function(code, reason) { + Native["com/sun/cldc/isolate/Isolate.stop.(II)V"] = function(code, reason) { console.info("Isolate stops with code " + code + " and reason " + reason); if (!pendingMIDletUpdate) { - exit(); - return; + exit(); + return; } // Perform updating. @@ -942,27 +943,27 @@ Native["com/sun/cldc/isolate/Isolate.stop.(II)V"] = function(code, reason) { document.body.appendChild(dialog); performDownload(pendingMIDletUpdate, dialog, function(data) { - dialog.parentElement.removeChild(dialog); + dialog.parentElement.removeChild(dialog); - fs.remove("/midlet.jad"); - fs.create("/midlet.jad", new Blob([ data.jadData ])); - fs.remove("/midlet.jar"); - fs.create("/midlet.jar", new Blob([ data.jarData ])); + fs.remove("/midlet.jad"); + fs.create("/midlet.jad", new Blob([ data.jadData ])); + fs.remove("/midlet.jar"); + fs.create("/midlet.jar", new Blob([ data.jarData ])); - Promise.all([ - new Promise(function(resolve, reject) { - fs.syncStore(resolve); - }), - CompiledMethodCache.clear(), + Promise.all([ + new Promise(function(resolve, reject) { + fs.syncStore(resolve); + }), + CompiledMethodCache.clear(), ]).then(function() { - pendingMIDletUpdate = null; - DumbPipe.close(DumbPipe.open("alert", "Update completed!")); - DumbPipe.close(DumbPipe.open("reload", {})); + pendingMIDletUpdate = null; + DumbPipe.close(DumbPipe.open("alert", "Update completed!")); + DumbPipe.close(DumbPipe.open("reload", {})); }); }); -}; + }; -// The foreground isolate will get the user events (keypresses, etc.) + // The foreground isolate will get the user events (keypresses, etc.) var foregroundIsolateId; var displayId; var nativeEventQueues = {}; @@ -978,8 +979,8 @@ Native["com/sun/cldc/isolate/Isolate.stop.(II)V"] = function(code, reason) { function sendNativeEvent(e, isolateId) { var elem = waitingNativeEventQueue[isolateId]; if (!elem) { - nativeEventQueues[isolateId].push(e); - return; + nativeEventQueues[isolateId].push(e); + return; } copyEvent(e, elem.nativeEvent); @@ -990,25 +991,25 @@ Native["com/sun/cldc/isolate/Isolate.stop.(II)V"] = function(code, reason) { function sendVirtualKeyboardEvent() { if (undefined != displayId && undefined != foregroundIsolateId) { - sendNativeEvent({ - type: VIRTUAL_KEYBOARD_EVENT, - intParam1: 0, - intParam2: 0, - intParam3: 0, - intParam4: displayId, - }, foregroundIsolateId); + sendNativeEvent({ + type: VIRTUAL_KEYBOARD_EVENT, + intParam1: 0, + intParam2: 0, + intParam3: 0, + intParam4: displayId, + }, foregroundIsolateId); } }; function sendRotationEvent() { if (undefined != displayId && undefined != foregroundIsolateId) { - sendNativeEvent({ - type: ROTATION_EVENT, - intParam1: 0, - intParam2: 0, - intParam3: 0, - intParam4: displayId, - }, foregroundIsolateId); + sendNativeEvent({ + type: ROTATION_EVENT, + intParam1: 0, + intParam2: 0, + intParam3: 0, + intParam4: displayId, + }, foregroundIsolateId); } } @@ -1077,151 +1078,152 @@ Native["com/sun/cldc/isolate/Isolate.stop.(II)V"] = function(code, reason) { if (!suppressKeyEvents) { sendNativeEvent({ type: KEY_EVENT, intParam1: PRESSED, intParam2: keyCode, intParam3: 0, intParam4: displayId }, foregroundIsolateId); } -}; + }; -function keyRelease(keyCode) { - if (!suppressKeyEvents) + function keyRelease(keyCode) { + if (!suppressKeyEvents) { sendNativeEvent({ type: KEY_EVENT, intParam1: RELEASED, intParam2: keyCode, intParam3: 0, intParam4: displayId }, foregroundIsolateId); -}; - -window.addEventListener("keypress", function(ev) { - keyPress(ev.which); -}); - -window.addEventListener("keyup", function(ev) { - keyRelease(ev.which); -}); - -Native["com/sun/midp/events/EventQueue.getNativeEventQueueHandle.()I"] = function() { - return 0; -}; - -Native["com/sun/midp/events/EventQueue.resetNativeEventQueue.()V"] = function() { - nativeEventQueues[$.ctx.runtime.isolate.id] = []; -}; - -Native["com/sun/midp/events/EventQueue.sendNativeEventToIsolate.(Lcom/sun/midp/events/NativeEvent;I)V"] = -function(obj, isolateId) { - var e = { type: obj.type }; - - var fields = obj.klass.classInfo.fields; - for (var i = 0; i < fields.length; i++) { - var field = fields[i]; - e[field.name] = field.get(obj); } + }; - sendNativeEvent(e, isolateId); -}; + window.addEventListener("keypress", function(ev) { + keyPress(ev.which); + }); -Native["com/sun/midp/events/NativeEventMonitor.waitForNativeEvent.(Lcom/sun/midp/events/NativeEvent;)I"] = -function(nativeEvent) { - var isolateId = $.ctx.runtime.isolate.id; - var nativeEventQueue = nativeEventQueues[isolateId]; + window.addEventListener("keyup", function(ev) { + keyRelease(ev.which); + }); - if (nativeEventQueue.length !== 0) { + Native["com/sun/midp/events/EventQueue.getNativeEventQueueHandle.()I"] = function() { + return 0; + }; + + Native["com/sun/midp/events/EventQueue.resetNativeEventQueue.()V"] = function() { + nativeEventQueues[$.ctx.runtime.isolate.id] = []; + }; + + Native["com/sun/midp/events/EventQueue.sendNativeEventToIsolate.(Lcom/sun/midp/events/NativeEvent;I)V"] = + function(obj, isolateId) { + var e = { type: obj.type }; + + var fields = obj.klass.classInfo.fields; + for (var i = 0; i < fields.length; i++) { + var field = fields[i]; + e[field.name] = field.get(obj); + } + + sendNativeEvent(e, isolateId); + }; + + Native["com/sun/midp/events/NativeEventMonitor.waitForNativeEvent.(Lcom/sun/midp/events/NativeEvent;)I"] = + function(nativeEvent) { + var isolateId = $.ctx.runtime.isolate.id; + var nativeEventQueue = nativeEventQueues[isolateId]; + + if (nativeEventQueue.length !== 0) { copyEvent(nativeEventQueue.shift(), nativeEvent); return nativeEventQueue.length; - } + } - asyncImpl("I", new Promise(function(resolve, reject) { + asyncImpl("I", new Promise(function(resolve, reject) { waitingNativeEventQueue[isolateId] = { - resolve: resolve, - nativeEvent: nativeEvent, + resolve: resolve, + nativeEvent: nativeEvent, }; - })); -}; + })); + }; -Native["com/sun/midp/events/NativeEventMonitor.readNativeEvent.(Lcom/sun/midp/events/NativeEvent;)Z"] = -function(obj) { - var isolateId = $.ctx.runtime.isolate.id; - var nativeEventQueue = nativeEventQueues[isolateId]; - if (!nativeEventQueue.length) { + Native["com/sun/midp/events/NativeEventMonitor.readNativeEvent.(Lcom/sun/midp/events/NativeEvent;)Z"] = + function(obj) { + var isolateId = $.ctx.runtime.isolate.id; + var nativeEventQueue = nativeEventQueues[isolateId]; + if (!nativeEventQueue.length) { return 0; - } - copyEvent(nativeEventQueue.shift(), obj); - return 1; -}; + } + copyEvent(nativeEventQueue.shift(), obj); + return 1; + }; -var localizedStrings = new Map(); + var localizedStrings = new Map(); -Native["com/sun/midp/l10n/LocalizedStringsBase.getContent.(I)Ljava/lang/String;"] = function(id) { + Native["com/sun/midp/l10n/LocalizedStringsBase.getContent.(I)Ljava/lang/String;"] = function(id) { if (localizedStrings.size === 0) { - // First build up a mapping of field names to field IDs - var classInfo = CLASSES.getClass("com/sun/midp/i18n/ResourceConstants"); - var constantsMap = new Map(); - classInfo.fields.forEach(function(field) { - constantsMap.set(field.name, classInfo.constant_pool[field.constantValue].integer); - }); + // First build up a mapping of field names to field IDs + var classInfo = CLASSES.getClass("com/sun/midp/i18n/ResourceConstants"); + var constantsMap = new Map(); + classInfo.fields.forEach(function(field) { + constantsMap.set(field.name, classInfo.constant_pool[field.constantValue].integer); + }); - var data = CLASSES.loadFileFromJar("java/classes.jar", "assets/0/en-US.xml"); - if (!data) - throw $.newIOException(); + var data = CLASSES.loadFileFromJar("java/classes.jar", "assets/0/en-US.xml"); + if (!data) + throw $.newIOException(); - var text = util.decodeUtf8(data); - var xml = new window.DOMParser().parseFromString(text, "text/xml"); - var entries = xml.getElementsByTagName("localized_string"); + var text = util.decodeUtf8(data); + var xml = new window.DOMParser().parseFromString(text, "text/xml"); + var entries = xml.getElementsByTagName("localized_string"); - for (var n = 0; n < entries.length; ++n) { - var attrs = entries[n].attributes; - // map the key value to a field ID - var id = constantsMap.get(attrs.Key.value); - localizedStrings.set(id, attrs.Value.value); - } + for (var n = 0; n < entries.length; ++n) { + var attrs = entries[n].attributes; + // map the key value to a field ID + var id = constantsMap.get(attrs.Key.value); + localizedStrings.set(id, attrs.Value.value); + } } var value = localizedStrings.get(id); if (!value) { - throw $.newIllegalStateException(); + throw $.newIllegalStateException(); } return J2ME.newString(value); -}; + }; -Native["javax/microedition/lcdui/Display.drawTrustedIcon0.(IZ)V"] = function(dispId, drawTrusted) { + Native["javax/microedition/lcdui/Display.drawTrustedIcon0.(IZ)V"] = function(dispId, drawTrusted) { console.warn("Display.drawTrustedIcon0.(IZ)V not implemented (" + dispId + ", " + drawTrusted + ")"); -}; + }; -Native["com/sun/midp/events/EventQueue.sendShutdownEvent.()V"] = function() { + Native["com/sun/midp/events/EventQueue.sendShutdownEvent.()V"] = function() { sendNativeEvent({ type: EVENT_QUEUE_SHUTDOWN }, $.ctx.runtime.isolate.id); -}; + }; -Native["com/sun/midp/main/CommandState.saveCommandState.(Lcom/sun/midp/main/CommandState;)V"] = function(commandState) { + Native["com/sun/midp/main/CommandState.saveCommandState.(Lcom/sun/midp/main/CommandState;)V"] = function(commandState) { console.warn("CommandState.saveCommandState.(L...CommandState;)V not implemented (" + commandState + ")"); -}; + }; -Native["com/sun/midp/main/CommandState.exitInternal.(I)V"] = function(exit) { + Native["com/sun/midp/main/CommandState.exitInternal.(I)V"] = function(exit) { console.info("Exit: " + exit); exit(); -}; + }; -Native["com/sun/midp/suspend/SuspendSystem$MIDPSystem.allMidletsKilled.()Z"] = function() { + Native["com/sun/midp/suspend/SuspendSystem$MIDPSystem.allMidletsKilled.()Z"] = function() { console.warn("SuspendSystem$MIDPSystem.allMidletsKilled.()Z not implemented"); return 0; -}; + }; -/* We don't care about the system keys SELECT, - SOFT_BUTTON1, SOFT_BUTTON2, DEBUG_TRACE1, CLAMSHELL_OPEN, CLAMSHELL_CLOSE, - but we do care about SYSTEM_KEY_CLEAR, so send it when the delete key is pressed. -*/ + /* We don't care about the system keys SELECT, + SOFT_BUTTON1, SOFT_BUTTON2, DEBUG_TRACE1, CLAMSHELL_OPEN, CLAMSHELL_CLOSE, + but we do care about SYSTEM_KEY_CLEAR, so send it when the delete key is pressed. + */ -var SYSTEM_KEY_POWER = 1; -var SYSTEM_KEY_SEND = 2; -var SYSTEM_KEY_END = 3; -var SYSTEM_KEY_CLEAR = 4; + var SYSTEM_KEY_POWER = 1; + var SYSTEM_KEY_SEND = 2; + var SYSTEM_KEY_END = 3; + var SYSTEM_KEY_CLEAR = 4; -var systemKeyMap = { - 8: SYSTEM_KEY_CLEAR, // Backspace - 112: SYSTEM_KEY_POWER, // F1 - 116: SYSTEM_KEY_SEND, // F5 - 114: SYSTEM_KEY_END, // F3 -}; + var systemKeyMap = { + 8: SYSTEM_KEY_CLEAR, // Backspace + 112: SYSTEM_KEY_POWER, // F1 + 116: SYSTEM_KEY_SEND, // F5 + 114: SYSTEM_KEY_END, // F3 + }; -Native["javax/microedition/lcdui/KeyConverter.getSystemKey.(I)I"] = function(key) { + Native["javax/microedition/lcdui/KeyConverter.getSystemKey.(I)I"] = function(key) { return systemKeyMap[key] || 0; -}; + }; -var keyMap = { + var keyMap = { 1: 119, // UP 2: 97, // LEFT 5: 100, // RIGHT @@ -1231,13 +1233,13 @@ var keyMap = { 10: 101, // GAME_B 11: 122, // GAME_C 12: 99, // GAME_D -}; + }; -Native["javax/microedition/lcdui/KeyConverter.getKeyCode.(I)I"] = function(key) { + Native["javax/microedition/lcdui/KeyConverter.getKeyCode.(I)I"] = function(key) { return keyMap[key] || 0; -}; + }; -var keyNames = { + var keyNames = { 119: "Up", 97: "Left", 100: "Right", @@ -1247,13 +1249,13 @@ var keyNames = { 101: "Addressbook", 122: "Menu", 99: "Mail", -}; + }; -Native["javax/microedition/lcdui/KeyConverter.getKeyName.(I)Ljava/lang/String;"] = function(keyCode) { + Native["javax/microedition/lcdui/KeyConverter.getKeyName.(I)Ljava/lang/String;"] = function(keyCode) { return J2ME.newString((keyCode in keyNames) ? keyNames[keyCode] : String.fromCharCode(keyCode)); -}; + }; -var gameKeys = { + var gameKeys = { 119: 1, // UP 97: 2, // LEFT 115: 6, // DOWN @@ -1263,262 +1265,260 @@ var gameKeys = { 101: 10, // GAME_B 122: 11, // GAME_C 99: 12 // GAME_D -}; + }; -Native["javax/microedition/lcdui/KeyConverter.getGameAction.(I)I"] = function(keyCode) { + Native["javax/microedition/lcdui/KeyConverter.getGameAction.(I)I"] = function(keyCode) { return gameKeys[keyCode] || 0; -}; + }; -Native["javax/microedition/lcdui/game/GameCanvas.setSuppressKeyEvents.(Ljavax/microedition/lcdui/Canvas;Z)V"] = function(canvas, suppressKeyEvents) { + Native["javax/microedition/lcdui/game/GameCanvas.setSuppressKeyEvents.(Ljavax/microedition/lcdui/Canvas;Z)V"] = function(canvas, suppressKeyEvents) { suppressKeyEvents = suppressKeyEvents; -}; + }; -Native["com/sun/midp/main/MIDletProxyList.resetForegroundInNativeState.()V"] = function() { + Native["com/sun/midp/main/MIDletProxyList.resetForegroundInNativeState.()V"] = function() { displayId = undefined; -}; + }; -Native["com/sun/midp/main/MIDletProxyList.setForegroundInNativeState.(II)V"] = function(isolateId, dispId) { + Native["com/sun/midp/main/MIDletProxyList.setForegroundInNativeState.(II)V"] = function(isolateId, dispId) { displayId = dispId; foregroundIsolateId = isolateId; -}; + }; -var connectionRegistry = { + var connectionRegistry = { // The lastRegistrationId is in common between alarms and push notifications lastRegistrationId: -1, pushRegistrations: [], alarms: [], readyRegistrations: [], addReadyRegistration: function(id) { - this.readyRegistrations.push(id); - this.notify(); + this.readyRegistrations.push(id); + this.notify(); }, notify: function() { - if (!this.readyRegistrations.length || !this.pendingPollCallback) { - return; - } - var cb = this.pendingPollCallback; - this.pendingPollCallback = null; - cb(this.readyRegistrations.pop()); + if (!this.readyRegistrations.length || !this.pendingPollCallback) { + return; + } + var cb = this.pendingPollCallback; + this.pendingPollCallback = null; + cb(this.readyRegistrations.pop()); }, pushNotify: function(protocolName) { - for (var i = 0; i < this.pushRegistrations.length; i++) { - if (protocolName == this.pushRegistrations[i].connection) { - this.addReadyRegistration(this.pushRegistrations[i].id); - } + for (var i = 0; i < this.pushRegistrations.length; i++) { + if (protocolName == this.pushRegistrations[i].connection) { + this.addReadyRegistration(this.pushRegistrations[i].id); } + } }, waitForRegistration: function(cb) { - if (this.pendingPollCallback) { - throw new Error("There can only be one waiter."); - } - this.pendingPollCallback = cb; - this.notify(); + if (this.pendingPollCallback) { + throw new Error("There can only be one waiter."); + } + this.pendingPollCallback = cb; + this.notify(); }, addConnection: function(connection) { - connection.id = ++this.lastRegistrationId; - this.pushRegistrations.push(connection); - return connection.id; + connection.id = ++this.lastRegistrationId; + this.pushRegistrations.push(connection); + return connection.id; }, addAlarm: function(alarm) { - alarm.id = ++this.lastRegistrationId; - this.alarms.push(alarm); - return alarm.id; + alarm.id = ++this.lastRegistrationId; + this.alarms.push(alarm); + return alarm.id; } -}; + }; -Native["com/sun/midp/io/j2me/push/ConnectionRegistry.poll0.(J)I"] = function(time) { + Native["com/sun/midp/io/j2me/push/ConnectionRegistry.poll0.(J)I"] = function(time) { asyncImpl("I", new Promise(function(resolve, reject) { - connectionRegistry.waitForRegistration(function(id) { - resolve(id); - }); + connectionRegistry.waitForRegistration(function(id) { + resolve(id); + }); })); -}; + }; -Native["com/sun/midp/io/j2me/push/ConnectionRegistry.add0.(Ljava/lang/String;)I"] = function(connection) { + Native["com/sun/midp/io/j2me/push/ConnectionRegistry.add0.(Ljava/lang/String;)I"] = function(connection) { var values = util.fromJavaString(connection).split(','); console.warn("ConnectionRegistry.add0.(IL...String;)I isn't completely implemented"); connectionRegistry.addConnection({ - connection: values[0], - midlet: values[1], - filter: values[2], - suiteId: values[3] + connection: values[0], + midlet: values[1], + filter: values[2], + suiteId: values[3] }); return 0; -}; + }; -Native["com/sun/midp/io/j2me/push/ConnectionRegistry.addAlarm0.([BJ)J"] = function(jMidlet, jTime) { + Native["com/sun/midp/io/j2me/push/ConnectionRegistry.addAlarm0.([BJ)J"] = function(jMidlet, jTime) { var time = jTime.toNumber(), midlet = util.decodeUtf8(jMidlet); var lastAlarm = 0; var id = null; var alarms = connectionRegistry.alarms; for (var i = 0; i < alarms.length; i++) { - if (alarms[i].midlet == midlet) { - if (time != 0) { - id = alarms[i].id; - lastAlarm = alarms[i].time; - alarms[i].time = time; - } else { - alarms[i].splice(i, 1); - } - - break; + if (alarms[i].midlet == midlet) { + if (time != 0) { + id = alarms[i].id; + lastAlarm = alarms[i].time; + alarms[i].time = time; + } else { + alarms[i].splice(i, 1); } + + break; + } } if (lastAlarm == 0 && time != 0) { - id = connectionRegistry.addAlarm({ - midlet: midlet, - time: time - }); + id = connectionRegistry.addAlarm({ + midlet: midlet, + time: time + }); } if (id !== null) { - var relativeTime = time - Date.now(); - if (relativeTime < 0) { - relativeTime = 0; - } + var relativeTime = time - Date.now(); + if (relativeTime < 0) { + relativeTime = 0; + } - setTimeout(function() { - connectionRegistry.addReadyRegistration(id); - }, relativeTime); + setTimeout(function() { + connectionRegistry.addReadyRegistration(id); + }, relativeTime); } return Long.fromNumber(lastAlarm); -}; + }; -Native["com/sun/midp/io/j2me/push/ConnectionRegistry.getMIDlet0.(I[BI)I"] = function(handle, regentry, entrysz) { + Native["com/sun/midp/io/j2me/push/ConnectionRegistry.getMIDlet0.(I[BI)I"] = function(handle, regentry, entrysz) { var reg; var alarms = connectionRegistry.alarms; for (var i = 0; i < alarms.length; i++) { - if (alarms[i].id == handle) { - reg = alarms[i]; - } + if (alarms[i].id == handle) { + reg = alarms[i]; + } } if (!reg) { - var pushRegistrations = connectionRegistry.pushRegistrations; - for (var i = 0; i < pushRegistrations.length; i++) { - if (pushRegistrations[i].id == handle) { - reg = pushRegistrations[i]; - } + var pushRegistrations = connectionRegistry.pushRegistrations; + for (var i = 0; i < pushRegistrations.length; i++) { + if (pushRegistrations[i].id == handle) { + reg = pushRegistrations[i]; } + } } if (!reg) { - console.error("getMIDlet0 returns -1, this should never happen"); - return -1; + console.error("getMIDlet0 returns -1, this should never happen"); + return -1; } var str; if (reg.time) { - str = reg.midlet + ", 0, 1"; + str = reg.midlet + ", 0, 1"; } else { - str = reg.connection + ", " + reg.midlet + ", " + reg.filter + ", " + reg.suiteId; + str = reg.connection + ", " + reg.midlet + ", " + reg.filter + ", " + reg.suiteId; } for (var i = 0; i < str.length; i++) { - regentry[i] = str.charCodeAt(i); + regentry[i] = str.charCodeAt(i); } regentry[str.length] = 0; return 0; -}; + }; -Native["com/sun/midp/io/j2me/push/ConnectionRegistry.checkInByMidlet0.(ILjava/lang/String;)V"] = function(suiteId, className) { - console.warn("ConnectionRegistry.checkInByMidlet0.(IL...String;)V not implemented (" + - suiteId + ", " + util.fromJavaString(className) + ")"); -}; + Native["com/sun/midp/io/j2me/push/ConnectionRegistry.checkInByMidlet0.(ILjava/lang/String;)V"] = function(suiteId, className) { + console.warn("ConnectionRegistry.checkInByMidlet0.(IL...String;)V not implemented (" + suiteId + ", " + util.fromJavaString(className) + ")"); + }; -Native["com/sun/midp/io/j2me/push/ConnectionRegistry.checkInByName0.([B)I"] = function(name) { - console.warn("ConnectionRegistry.checkInByName0.([B)V not implemented (" + - util.decodeUtf8(name) + ")"); + Native["com/sun/midp/io/j2me/push/ConnectionRegistry.checkInByName0.([B)I"] = function(name) { + console.warn("ConnectionRegistry.checkInByName0.([B)V not implemented (" + util.decodeUtf8(name) + ")"); return 0; -}; + }; -Native["com/nokia/mid/ui/gestures/GestureInteractiveZone.isSupported.(I)Z"] = function(gestureEventIdentity) { + Native["com/nokia/mid/ui/gestures/GestureInteractiveZone.isSupported.(I)Z"] = function(gestureEventIdentity) { console.warn("GestureInteractiveZone.isSupported.(I)Z not implemented (" + gestureEventIdentity + ")"); return 0; -}; + }; -Native["com/sun/midp/security/SecurityHandler.checkPermission0.(II)Z"] = function(suiteId, permission) { + Native["com/sun/midp/security/SecurityHandler.checkPermission0.(II)Z"] = function(suiteId, permission) { return 1; -}; + }; -Native["com/sun/midp/security/SecurityHandler.checkPermissionStatus0.(II)I"] = function(suiteId, permission) { + Native["com/sun/midp/security/SecurityHandler.checkPermissionStatus0.(II)I"] = function(suiteId, permission) { return 1; -}; + }; -Native["com/sun/midp/io/NetworkConnectionBase.initializeInternal.()V"] = function() { + Native["com/sun/midp/io/NetworkConnectionBase.initializeInternal.()V"] = function() { console.warn("NetworkConnectionBase.initializeInternal.()V not implemented"); -}; + }; -Native["com/sun/j2me/content/RegistryStore.init.()Z"] = function() { + Native["com/sun/j2me/content/RegistryStore.init.()Z"] = function() { console.warn("com/sun/j2me/content/RegistryStore.init.()Z not implemented"); return 1; -}; + }; -Native["com/sun/j2me/content/RegistryStore.forSuite0.(I)Ljava/lang/String;"] = function(suiteID) { + Native["com/sun/j2me/content/RegistryStore.forSuite0.(I)Ljava/lang/String;"] = function(suiteID) { console.warn("com/sun/j2me/content/RegistryStore.forSuite0.(I)Ljava/lang/String; not implemented"); return J2ME.newString(""); -}; + }; -Native["com/sun/j2me/content/AppProxy.isInSvmMode.()Z"] = function() { + Native["com/sun/j2me/content/AppProxy.isInSvmMode.()Z"] = function() { console.warn("com/sun/j2me/content/AppProxy.isInSvmMode.()Z not implemented"); return 0; -}; + }; -addUnimplementedNative("com/sun/j2me/content/InvocationStore.setCleanup0.(ILjava/lang/String;Z)V"); -addUnimplementedNative("com/sun/j2me/content/InvocationStore.get0.(Lcom/sun/j2me/content/InvocationImpl;ILjava/lang/String;IZ)I", 0); -addUnimplementedNative("com/sun/j2me/content/InvocationStore.getByTid0.(Lcom/sun/j2me/content/InvocationImpl;II)I", 0); -addUnimplementedNative("com/sun/j2me/content/InvocationStore.resetFlags0.(I)V"); -addUnimplementedNative("com/sun/j2me/content/AppProxy.midletIsRemoved.(ILjava/lang/String;)V"); -addUnimplementedNative("com/nokia/mid/ui/VirtualKeyboard.hideOpenKeypadCommand.(Z)V"); -addUnimplementedNative("com/nokia/mid/ui/VirtualKeyboard.suppressSizeChanged.(Z)V"); + addUnimplementedNative("com/sun/j2me/content/InvocationStore.setCleanup0.(ILjava/lang/String;Z)V"); + addUnimplementedNative("com/sun/j2me/content/InvocationStore.get0.(Lcom/sun/j2me/content/InvocationImpl;ILjava/lang/String;IZ)I", 0); + addUnimplementedNative("com/sun/j2me/content/InvocationStore.getByTid0.(Lcom/sun/j2me/content/InvocationImpl;II)I", 0); + addUnimplementedNative("com/sun/j2me/content/InvocationStore.resetFlags0.(I)V"); + addUnimplementedNative("com/sun/j2me/content/AppProxy.midletIsRemoved.(ILjava/lang/String;)V"); + addUnimplementedNative("com/nokia/mid/ui/VirtualKeyboard.hideOpenKeypadCommand.(Z)V"); + addUnimplementedNative("com/nokia/mid/ui/VirtualKeyboard.suppressSizeChanged.(Z)V"); -Native["com/nokia/mid/ui/VirtualKeyboard.getCustomKeyboardControl.()Lcom/nokia/mid/ui/CustomKeyboardControl;"] = function() { + Native["com/nokia/mid/ui/VirtualKeyboard.getCustomKeyboardControl.()Lcom/nokia/mid/ui/CustomKeyboardControl;"] = function() { throw $.newIllegalArgumentException("VirtualKeyboard::getCustomKeyboardControl() not implemented") -}; + }; -var keyboardVisibilityListener = null; -Native["com/nokia/mid/ui/VirtualKeyboard.setVisibilityListener.(Lcom/nokia/mid/ui/KeyboardVisibilityListener;)V"] = function(listener) { + var keyboardVisibilityListener = null; + Native["com/nokia/mid/ui/VirtualKeyboard.setVisibilityListener.(Lcom/nokia/mid/ui/KeyboardVisibilityListener;)V"] = function(listener) { keyboardVisibilityListener = listener; -}; + }; -Native["javax/microedition/lcdui/Display.getKeyboardVisibilityListener.()Lcom/nokia/mid/ui/KeyboardVisibilityListener;"] = function() { + Native["javax/microedition/lcdui/Display.getKeyboardVisibilityListener.()Lcom/nokia/mid/ui/KeyboardVisibilityListener;"] = function() { return keyboardVisibilityListener; -}; + }; -Native["com/nokia/mid/ui/VirtualKeyboard.isVisible.()Z"] = function() { + Native["com/nokia/mid/ui/VirtualKeyboard.isVisible.()Z"] = function() { return MIDP.isVKVisible() ? 1 : 0; -}; + }; -Native["com/nokia/mid/ui/VirtualKeyboard.getXPosition.()I"] = function() { + Native["com/nokia/mid/ui/VirtualKeyboard.getXPosition.()I"] = function() { return 0; -}; + }; -Native["com/nokia/mid/ui/VirtualKeyboard.getYPosition.()I"] = function() { + Native["com/nokia/mid/ui/VirtualKeyboard.getYPosition.()I"] = function() { // We should return the number of pixels between the top of the // screen and the top of the keyboard return canvas.height - getKeyboardHeight(); -}; + }; -Native["com/nokia/mid/ui/VirtualKeyboard.getWidth.()I"] = function() { + Native["com/nokia/mid/ui/VirtualKeyboard.getWidth.()I"] = function() { // The keyboard is always the same width as our window return window.innerWidth; -}; + }; -Native["com/nokia/mid/ui/VirtualKeyboard.getHeight.()I"] = function() { + Native["com/nokia/mid/ui/VirtualKeyboard.getHeight.()I"] = function() { return getKeyboardHeight(); -}; + }; -function getKeyboardHeight() { + function getKeyboardHeight() { return physicalScreenHeight - window.innerHeight; -}; + }; return { isVKVisible: isVKVisible, From ae0b3d70d76f8a56bc5245b094491979723e1380 Mon Sep 17 00:00:00 2001 From: Tim Abraldes Date: Wed, 18 Feb 2015 08:32:48 -0800 Subject: [PATCH 3/4] Fix typo: `Context2D` -> `context2D` --- midp/gfx.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/midp/gfx.js b/midp/gfx.js index 67823a6d..944100a3 100644 --- a/midp/gfx.js +++ b/midp/gfx.js @@ -928,7 +928,7 @@ var currentlyFocusedTextEditor; }; Native["javax/microedition/lcdui/Graphics.initScreen0.(III)V"] = function(displayId, w, h) { - this.context2D = MIDP.Context2D; + this.context2D = MIDP.context2D; this.displayId = displayId; setDimensions(this, w, h); resetGC(this); From 890d04ef0b1fcc5deb76b32eb17b167fe663ddf9 Mon Sep 17 00:00:00 2001 From: Tim Abraldes Date: Tue, 24 Feb 2015 11:25:16 -0800 Subject: [PATCH 4/4] Review comments Make `displayId` default to -1. Rename the argument `suppressKeyEvents` so it doesn't obscure the module variable of the same name. --- midp/midp.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/midp/midp.js b/midp/midp.js index dbc409a5..3860576d 100644 --- a/midp/midp.js +++ b/midp/midp.js @@ -967,7 +967,7 @@ var MIDP = (function() { // The foreground isolate will get the user events (keypresses, etc.) var foregroundIsolateId; - var displayId; + var displayId = -1; var nativeEventQueues = {}; var waitingNativeEventQueue = {}; @@ -992,7 +992,7 @@ var MIDP = (function() { } function sendVirtualKeyboardEvent() { - if (undefined != displayId && undefined != foregroundIsolateId) { + if (-1 != displayId && undefined != foregroundIsolateId) { sendNativeEvent({ type: VIRTUAL_KEYBOARD_EVENT, intParam1: 0, @@ -1004,7 +1004,7 @@ var MIDP = (function() { }; function sendRotationEvent() { - if (undefined != displayId && undefined != foregroundIsolateId) { + if (-1 != displayId && undefined != foregroundIsolateId) { sendNativeEvent({ type: ROTATION_EVENT, intParam1: 0, @@ -1016,7 +1016,7 @@ var MIDP = (function() { } function sendCommandEvent(id) { - if (undefined != displayId && undefined != foregroundIsolateId) { + if (-1 != displayId && undefined != foregroundIsolateId) { sendNativeEvent({ type: COMMAND_EVENT, intParam1: id, @@ -1273,12 +1273,12 @@ var MIDP = (function() { return gameKeys[keyCode] || 0; }; - Native["javax/microedition/lcdui/game/GameCanvas.setSuppressKeyEvents.(Ljavax/microedition/lcdui/Canvas;Z)V"] = function(canvas, suppressKeyEvents) { - suppressKeyEvents = suppressKeyEvents; + Native["javax/microedition/lcdui/game/GameCanvas.setSuppressKeyEvents.(Ljavax/microedition/lcdui/Canvas;Z)V"] = function(canvas, shouldSuppress) { + suppressKeyEvents = shouldSuppress; }; Native["com/sun/midp/main/MIDletProxyList.resetForegroundInNativeState.()V"] = function() { - displayId = undefined; + displayId = -1; }; Native["com/sun/midp/main/MIDletProxyList.setForegroundInNativeState.(II)V"] = function(isolateId, dispId) {