pluotsorbet/midp/sms.js

198 строки
6.3 KiB
JavaScript
Исходник Обычный вид История

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;
MIDP.lastSMSID = -1;
MIDP.smsConnections = {};
MIDP.j2meSMSMessages = [];
MIDP.j2meSMSWaiting = null;
MIDP.nokiaSMSMessages = [];
/**
* Simulate a received SMS with the given text, sent to the specified addr.
* (It appears the value of `addr` is unimportant for most apps.)
*/
function receiveSms(text, addr) {
var sms = {
text: text,
addr: addr,
id: ++MIDP.lastSMSID,
};
MIDP.nokiaSMSMessages.push(sms);
MIDP.j2meSMSMessages.push(sms);
2014-11-27 11:36:10 +03:00
window.dispatchEvent(new CustomEvent("nokia.messaging", {
detail: sms
}));
if (MIDP.j2meSMSWaiting) {
MIDP.j2meSMSWaiting();
}
}
2014-09-06 04:32:39 +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';
el.classList.add('visible');
el.querySelector('p.verificationText').textContent = MIDlet.SMSDialogVerificationText;
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);
});
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);
});
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.
receiveSms(MIDlet.SMSDialogReceiveFilter(input.value), 'unknown');
});
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;
document.body.appendChild(el);
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-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) {
MIDP.smsConnections[++MIDP.lastSMSConnection] = {
port: port,
msid: msid,
host: util.fromJavaString(host),
};
2014-09-06 04:32:39 +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"] =
function(port, msid, handle, smsPacket) {
2014-12-19 00:03:59 +03:00
asyncImpl("I", new Promise(function(resolve, reject) {
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-10-17 02:04:31 +04:00
if (MIDP.j2meSMSMessages.length > 0) {
resolve(receiveSMS());
} else {
MIDP.j2meSMSWaiting = function() {
MIDP.j2meSMSWaiting = null;
resolve(receiveSMS());
}
}
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) {
delete MIDP.smsConnections[handle];
return 0;
2014-12-19 00:03:59 +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) {
console.warn("com/sun/midp/io/j2me/sms/Protocol.numberOfSegments0.([BIIZ)I not implemented");
return 1;
2014-12-19 00:03:59 +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"] =
function(handle, type, host, destPort, sourcePort, message) {
var ctx = $.ctx;
2014-12-19 00:03:59 +03:00
asyncImpl("I", new Promise(function(resolve, reject) {
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),
},
});
activity.onsuccess = function() {
resolve(message.byteLength);
};
activity.onerror = function() {
ctx.setAsCurrentContext();
reject($.newIOException("Error while sending SMS message"));
};
2014-12-19 00:03:59 +03:00
}));
};