platformservices_20_specific.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Messaging service object based on com.nokia.device.messaging Interface
  3. */
  4. var messaging = null;
  5. /*
  6. * Initializes messaging service object
  7. */
  8. function initService(){
  9. try {
  10. messaging = nokia.device.load("messaging");
  11. var transactionid = messaging.setNotifier(setNotifierCb, setNotifierCbErr);
  12. }
  13. catch (e) {
  14. alert(e.toString());
  15. }
  16. }
  17. /*
  18. * Retrieves message by specified id
  19. * id - message id
  20. */
  21. function getMessage(id){
  22. var message = null;
  23. try{
  24. //id = parseInt(id);
  25. message = messaging.getMessage(id);
  26. }catch(e){
  27. alert("getMessage: " + e.toString());
  28. }
  29. return message;
  30. }
  31. /*
  32. * Callback function that called asynchronously when new message arrives.
  33. * It shows UI notification with message sender infornation
  34. */
  35. function setNotifierCb(messageid){
  36. var message = getMessage(messageid);
  37. if (message != null) {
  38. alert("New message from " + message.sender);
  39. listMessages();
  40. }
  41. }
  42. /*
  43. * Error Callback function for setNotifier.
  44. */
  45. function setNotifierCbErr(err){
  46. alert(err.message +" "+ err.code);
  47. }
  48. /*
  49. * Callback handler for messaging.send
  50. * displays notification about completed sent operation
  51. */
  52. function sendCb(){
  53. alert("Message was sent succesfully");
  54. }
  55. /*
  56. * Error Callback handler for messaging.send
  57. */
  58. function sendCbErr(err){
  59. alert(err.toString());
  60. }
  61. /*
  62. * Sends SMS message
  63. * phoneNumber - recipients phone number
  64. * text - message text
  65. */
  66. function sendSMS(phoneNumber, text){
  67. var message = new Object();
  68. //Setting the type of the message
  69. message.type = "SMS";
  70. if (phoneNumber != null && phoneNumber != "") {
  71. message.to = new Array();
  72. message.to.push(phoneNumber);
  73. }
  74. else {
  75. alert("Phone number is empty");
  76. return;
  77. }
  78. if (text != null && text != "") {
  79. message.body = text;
  80. }
  81. else {
  82. alert("Text is empty");
  83. return;
  84. }
  85. try {
  86. messaging.send(sendCb, message, null, sendCbErr);
  87. }catch(e){
  88. alert("sendSMS:" + e.toString());
  89. }
  90. }
  91. /*
  92. * Starts the device native message editor with predefined recipient number and message text
  93. * number - recipient number
  94. * message - message text to be sent
  95. * Message object is based in the following schema:
  96. * {
  97. * "type": String, //Supported values are "SMS" and "MMS". Defaults to "SMS".
  98. * "to": [String, String, . . .],
  99. * "body": String,
  100. * "subject: String, //Supported by MMS only. In case of SMS this field is supported for output where in the first 64 bytes of the bodytext will be returned as subject //If used for SMS input it will be ignored.
  101. * "cc": [String, String, . . .], //For SMS the contents of this field gets appended to tolist
  102. * "attachments": [{attachment data}, {attachment data}, . . .] //Supported by MMS only
  103. * }
  104. */
  105. function startEditor(number, message){
  106. var message = {
  107. type: 'SMS',
  108. to: [number],
  109. body: message
  110. };
  111. try{
  112. messaging.startEditor(message);
  113. }catch(e){
  114. alert(e.toString());
  115. }
  116. }
  117. /*
  118. * Sets specified status to message
  119. * id - message id
  120. * status - status to be set: read, unread, delete
  121. * delete is considered as message status, but on practice message is removed from database
  122. */
  123. function setMessageStatus(id, status){
  124. try{
  125. //var msgId = parseInt(id);
  126. if (status == 'delete') {
  127. messaging.deleteMessage(id);
  128. }
  129. else {
  130. var msgStatus = (status == "read" ? messaging.STATUS_READ : messaging.STATUS_UNREAD);
  131. messaging.setStatus(id, msgStatus);
  132. }
  133. }catch(e){
  134. alert(e.toString());
  135. }
  136. }
  137. /*
  138. * Callback handler for getList
  139. * Populates message list container with retrieved message data
  140. */
  141. function listMessagesCb(retval){
  142. var message;
  143. var content = "<table border='1' width='100%'><tr><th scope='colgroup' colspan='2'><input type='checkbox' name='chAll' onClick='chALL(this.checked)'/></th><th>Sender</th><th>Message</th></tr>";
  144. while ((message = retval.next()) != null) {
  145. content += showMessage(message);
  146. }
  147. content += "</table>";
  148. document.getElementById("msgList").innerHTML = content;
  149. }
  150. /*
  151. * Error Callback handler for getList
  152. */
  153. function listMessagesCbErr(err){
  154. alert(err.toString());
  155. }
  156. /*
  157. * Gets list of all messages in inbox
  158. */
  159. function listMessages(){
  160. /* It is possible to retrieve messages based on pattern
  161. Message Matching Pattern
  162. {
  163. "senders": ['sender1', 'sender2', . . .],
  164. "subject": 'subject heading',
  165. "start": Date, // JS Date object
  166. "end": Date
  167. }
  168. */
  169. //create filter
  170. var match = {};
  171. try {
  172. messaging.getList(listMessagesCb, match, messaging.SORT_BY_DATE, messaging.SORT_ASCENDING, listMessagesCbErr);
  173. }catch(e){
  174. alert(e.toString());
  175. }
  176. }
  177. /*
  178. * Handles single message info and produces HTML table row
  179. * Message Info schema:
  180. * {
  181. * "message": {message data},
  182. * "sender": String,
  183. * "time": Date,
  184. * "unread": Boolean,
  185. * "id": String // Identifies message in the message store
  186. * }
  187. */
  188. function showMessage(obj){
  189. var txt = "<tr>";
  190. try {
  191. txt += "<td width='5'><input type='checkbox' onclick='checkMain()' name='ch' value='" + obj["id"] + "'/></td>";
  192. txt += "<td width='5'>" + (obj["unread"] == true ? "<img src='images/img-link-email.png' border='0'/>" : "<img src='images/img-link-sms.png' border='0'/>") + "</td>";
  193. txt += "<td width='5'><nobr><a href='javascript:openMessage(\"" + obj["id"] + "\")'>" + obj["sender"] + "</a></nobr></td>";
  194. txt += "<td>" + obj["message"]["subject"] + "</td>";
  195. }
  196. catch (e) {
  197. alert("showMessage: " + e.toString());
  198. }
  199. txt += "</tr>";
  200. return txt;
  201. }