2014-09-06 04:32:39 +04:00
|
|
|
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set shiftwidth=4 tabstop=4 autoindent cindent expandtab: */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
MIDP.lastSMSConnection = -1;
|
2014-09-10 05:45:20 +04:00
|
|
|
MIDP.lastSMSID = -1;
|
2014-09-11 01:01:42 +04:00
|
|
|
MIDP.smsConnections = {};
|
|
|
|
MIDP.j2meSMSMessages = [];
|
|
|
|
MIDP.j2meSMSWaiting = null;
|
|
|
|
MIDP.nokiaSMSMessages = [];
|
2014-09-10 05:45:20 +04:00
|
|
|
|
2014-09-22 19:29:21 +04:00
|
|
|
/**
|
|
|
|
* Simulate a received SMS with the given text, sent to the specified addr.
|
|
|
|
* (It appears the value of `addr` is unimportant for most apps.)
|
|
|
|
*/
|
2014-09-20 02:49:57 +04:00
|
|
|
function receiveSms(text, addr) {
|
2014-09-10 05:45:20 +04:00
|
|
|
var sms = {
|
|
|
|
text: text,
|
|
|
|
addr: addr,
|
|
|
|
id: ++MIDP.lastSMSID,
|
|
|
|
};
|
|
|
|
|
2014-09-11 01:01:42 +04:00
|
|
|
MIDP.nokiaSMSMessages.push(sms);
|
|
|
|
MIDP.j2meSMSMessages.push(sms);
|
2014-09-10 05:45:20 +04:00
|
|
|
|
2014-11-27 11:36:10 +03:00
|
|
|
window.dispatchEvent(new CustomEvent("nokia.messaging", {
|
|
|
|
detail: sms
|
|
|
|
}));
|
2014-09-10 05:45:20 +04:00
|
|
|
|
2014-09-11 01:01:42 +04:00
|
|
|
if (MIDP.j2meSMSWaiting) {
|
|
|
|
MIDP.j2meSMSWaiting();
|
2014-09-10 05:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
2014-09-06 04:32:39 +04:00
|
|
|
|
2014-09-20 02:49:57 +04:00
|
|
|
/**
|
|
|
|
* This app is listening for SMS messages; for most apps, that means
|
|
|
|
* they're looking for the content of a message the app's servers just
|
|
|
|
* sent. Prompt the user to enter that code here, and forward it to
|
|
|
|
* the app.
|
|
|
|
*/
|
|
|
|
function promptForMessageText() {
|
|
|
|
var el = document.getElementById('sms-listener-prompt').cloneNode(true);
|
|
|
|
el.style.display = 'block';
|
2014-09-22 20:28:54 +04:00
|
|
|
el.classList.add('visible');
|
2014-09-20 02:49:57 +04:00
|
|
|
|
2014-12-21 02:36:56 +03:00
|
|
|
el.querySelector('p.verificationText').textContent = MIDlet.SMSDialogVerificationText;
|
|
|
|
|
2014-09-20 02:49:57 +04:00
|
|
|
var input = el.querySelector('input');
|
|
|
|
var btnCancel = el.querySelector('button.cancel');
|
|
|
|
var btnDone = el.querySelector('button.recommend');
|
|
|
|
|
|
|
|
btnDone.disabled = true; // Wait for input before enabling.
|
|
|
|
input.addEventListener('input', function() {
|
2014-12-18 17:03:03 +03:00
|
|
|
btnDone.disabled = (input.value.length === 0);
|
2014-09-20 02:49:57 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
btnCancel.addEventListener('click', function() {
|
2014-12-18 17:03:03 +03:00
|
|
|
console.warn('SMS prompt canceled.');
|
2014-12-18 17:28:45 +03:00
|
|
|
clearInterval(intervalID);
|
|
|
|
clearTimeout(timeoutID);
|
2014-12-18 17:03:03 +03:00
|
|
|
el.parentElement.removeChild(el);
|
2014-09-20 02:49:57 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
btnDone.addEventListener('click', function() {
|
2014-12-18 17:28:45 +03:00
|
|
|
clearInterval(intervalID);
|
|
|
|
clearTimeout(timeoutID);
|
2014-12-18 17:03:03 +03:00
|
|
|
el.parentElement.removeChild(el);
|
|
|
|
console.log('SMS prompt filled out:', input.value);
|
|
|
|
// We don't have easy access to our own phone number; use a
|
|
|
|
// dummy unknown value instead.
|
2014-12-18 17:35:26 +03:00
|
|
|
receiveSms(MIDlet.SMSDialogReceiveFilter(input.value), 'unknown');
|
2014-09-20 02:49:57 +04:00
|
|
|
});
|
|
|
|
|
2014-12-18 17:28:45 +03:00
|
|
|
function toTimeText(ms) {
|
|
|
|
var seconds = ms / 1000;
|
|
|
|
var minutes = Math.floor(seconds / 60);
|
|
|
|
seconds -= minutes * 60;
|
|
|
|
|
|
|
|
var text = minutes + ":";
|
|
|
|
|
|
|
|
if (seconds > 10) {
|
|
|
|
text += seconds;
|
|
|
|
} else {
|
|
|
|
text += "0" + seconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
el.querySelector('p.timeLeft').textContent = toTimeText(MIDlet.SMSDialogTimeout) +
|
|
|
|
" " + MIDlet.SMSDialogTimeoutText;
|
|
|
|
|
2014-09-20 02:49:57 +04:00
|
|
|
document.body.appendChild(el);
|
2015-01-09 13:42:06 +03:00
|
|
|
if (currentlyFocusedTextEditor) {
|
|
|
|
currentlyFocusedTextEditor.blur();
|
|
|
|
currentlyFocusedTextEditor = null;
|
|
|
|
}
|
2014-12-18 17:03:03 +03:00
|
|
|
|
2014-12-18 17:28:45 +03:00
|
|
|
var elapsedMS = 0;
|
|
|
|
var intervalID = setInterval(function() {
|
|
|
|
elapsedMS += 1000;
|
|
|
|
el.querySelector('p.timeLeft').textContent = toTimeText(MIDlet.SMSDialogTimeout - elapsedMS) +
|
|
|
|
" " + MIDlet.SMSDialogTimeoutText;
|
|
|
|
el.querySelector('progress.timeLeftBar').value = elapsedMS / MIDlet.SMSDialogTimeout * 100;
|
|
|
|
}, 1000);
|
|
|
|
|
2014-12-18 17:03:03 +03:00
|
|
|
// Remove the dialog after a timeout
|
2014-12-18 17:28:45 +03:00
|
|
|
var timeoutID = setTimeout(function() {
|
|
|
|
clearInterval(intervalID);
|
2014-12-18 17:03:03 +03:00
|
|
|
el.parentElement.removeChild(el);
|
|
|
|
}, MIDlet.SMSDialogTimeout);
|
2014-09-20 02:49:57 +04:00
|
|
|
}
|
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Native["com/sun/midp/io/j2me/sms/Protocol.open0.(Ljava/lang/String;II)I"] = function(host, msid, port) {
|
2014-09-11 01:01:42 +04:00
|
|
|
MIDP.smsConnections[++MIDP.lastSMSConnection] = {
|
2014-09-09 01:43:46 +04:00
|
|
|
port: port,
|
|
|
|
msid: msid,
|
2014-10-15 01:51:40 +04:00
|
|
|
host: util.fromJavaString(host),
|
2014-09-09 01:43:46 +04:00
|
|
|
};
|
2014-09-06 04:32:39 +04:00
|
|
|
|
2014-10-15 01:51:40 +04:00
|
|
|
return ++MIDP.lastSMSConnection;
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-09-06 04:32:39 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Native["com/sun/midp/io/j2me/sms/Protocol.receive0.(IIILcom/sun/midp/io/j2me/sms/Protocol$SMSPacket;)I"] =
|
2014-10-27 15:13:54 +03:00
|
|
|
function(port, msid, handle, smsPacket) {
|
2014-12-19 00:03:59 +03:00
|
|
|
asyncImpl("I", new Promise(function(resolve, reject) {
|
2014-11-21 00:52:16 +03:00
|
|
|
promptForMessageText();
|
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
function receiveSMS() {
|
|
|
|
var sms = MIDP.j2meSMSMessages.shift();
|
|
|
|
var text = sms.text;
|
|
|
|
var addr = sms.addr;
|
|
|
|
|
2015-01-28 02:36:13 +03:00
|
|
|
var message = J2ME.newByteArray(text.length);
|
2014-10-17 02:04:31 +04:00
|
|
|
for (var i = 0; i < text.length; i++) {
|
|
|
|
message[i] = text.charCodeAt(i);
|
|
|
|
}
|
|
|
|
|
2015-01-28 02:36:13 +03:00
|
|
|
var address = J2ME.newByteArray(addr.length);
|
2014-10-17 02:04:31 +04:00
|
|
|
for (var i = 0; i < addr.length; i++) {
|
|
|
|
address[i] = addr.charCodeAt(i);
|
|
|
|
}
|
|
|
|
|
2014-11-27 05:40:02 +03:00
|
|
|
smsPacket.klass.classInfo.getField("I.message.[B").set(smsPacket, message);
|
|
|
|
smsPacket.klass.classInfo.getField("I.address.[B").set(smsPacket, address);
|
|
|
|
smsPacket.klass.classInfo.getField("I.port.I").set(smsPacket, port);
|
|
|
|
smsPacket.klass.classInfo.getField("I.sentAt.J").set(smsPacket, Long.fromNumber(Date.now()));
|
|
|
|
smsPacket.klass.classInfo.getField("I.messageType.I").set(smsPacket, 0); // GSM_TEXT
|
2014-10-17 02:04:31 +04:00
|
|
|
|
|
|
|
return text.length;
|
2014-09-09 01:43:46 +04:00
|
|
|
}
|
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
if (MIDP.j2meSMSMessages.length > 0) {
|
|
|
|
resolve(receiveSMS());
|
|
|
|
} else {
|
|
|
|
MIDP.j2meSMSWaiting = function() {
|
|
|
|
MIDP.j2meSMSWaiting = null;
|
|
|
|
resolve(receiveSMS());
|
|
|
|
}
|
2014-09-09 01:43:46 +04:00
|
|
|
}
|
2014-12-19 00:03:59 +03:00
|
|
|
}));
|
|
|
|
};
|
2014-09-06 04:32:39 +04:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Native["com/sun/midp/io/j2me/sms/Protocol.close0.(III)I"] = function(port, handle, deRegister) {
|
2014-09-11 01:01:42 +04:00
|
|
|
delete MIDP.smsConnections[handle];
|
2014-10-15 01:51:40 +04:00
|
|
|
return 0;
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-11-21 00:49:52 +03:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Native["com/sun/midp/io/j2me/sms/Protocol.numberOfSegments0.([BIIZ)I"] = function(msgBuffer, msgLen, msgType, hasPort) {
|
2014-11-21 22:35:26 +03:00
|
|
|
console.warn("com/sun/midp/io/j2me/sms/Protocol.numberOfSegments0.([BIIZ)I not implemented");
|
2014-11-21 00:49:52 +03:00
|
|
|
return 1;
|
2014-12-19 00:03:59 +03:00
|
|
|
};
|
2014-11-21 00:49:52 +03:00
|
|
|
|
2014-12-19 00:03:59 +03:00
|
|
|
Native["com/sun/midp/io/j2me/sms/Protocol.send0.(IILjava/lang/String;II[B)I"] =
|
2014-11-21 00:49:52 +03:00
|
|
|
function(handle, type, host, destPort, sourcePort, message) {
|
2014-12-18 05:12:21 +03:00
|
|
|
var ctx = $.ctx;
|
2014-12-19 00:03:59 +03:00
|
|
|
asyncImpl("I", new Promise(function(resolve, reject) {
|
2014-11-21 00:49:52 +03:00
|
|
|
var activity = new MozActivity({
|
|
|
|
name: "new",
|
|
|
|
data: {
|
|
|
|
type: "websms/sms",
|
2014-11-21 23:28:29 +03:00
|
|
|
number: util.fromJavaString(host),
|
|
|
|
body: new TextDecoder('utf-16be').decode(message),
|
2014-11-21 00:49:52 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
activity.onsuccess = function() {
|
|
|
|
resolve(message.byteLength);
|
|
|
|
};
|
|
|
|
|
|
|
|
activity.onerror = function() {
|
2014-12-18 05:12:21 +03:00
|
|
|
ctx.setAsCurrentContext();
|
|
|
|
reject($.newIOException("Error while sending SMS message"));
|
2014-11-21 00:49:52 +03:00
|
|
|
};
|
2014-12-19 00:03:59 +03:00
|
|
|
}));
|
|
|
|
};
|