1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core.internal.extension.api.messaging;
7 import android.app.Activity;
8 import android.app.PendingIntent;
9 import android.content.BroadcastReceiver;
10 import android.content.Context;
11 import android.content.ContentResolver;
12 import android.content.ContentValues;
13 import android.content.Intent;
14 import android.content.IntentFilter;
15 import android.database.Cursor;
16 import android.net.Uri;
17 import android.os.Bundle;
18 import android.telephony.SmsManager;
19 import android.telephony.SmsMessage;
20 import android.util.Log;
22 import java.text.SimpleDateFormat;
23 import java.text.ParseException;
24 import java.util.Date;
26 import org.json.JSONArray;
27 import org.json.JSONException;
28 import org.json.JSONObject;
30 import org.xwalk.core.internal.extension.api.messaging.Messaging;
31 import org.xwalk.core.internal.extension.api.messaging.MessagingHelpers;
32 import org.xwalk.core.internal.extension.api.messaging.MessagingSmsConstMaps;
33 import org.xwalk.core.internal.extension.api.messaging.MessagingSmsConsts;
35 public class MessagingManager {
36 private final static String TAG = "MessagingManager";
37 private final Activity mMainActivity;
38 private final Messaging mMessagingHandler;
40 MessagingManager(Activity activity, Messaging messaging) {
41 mMainActivity = activity;
42 mMessagingHandler = messaging;
45 public void onMsgFindMessages(int instanceID, JSONObject jsonMsg) {
46 queryMessage(instanceID, jsonMsg);
49 public void onMsgGetMessage(int instanceID, JSONObject jsonMsg) {
50 queryMessage(instanceID, jsonMsg);
53 public void onMsgDeleteMessage(int instanceID, JSONObject jsonMsg) {
54 operation(instanceID, jsonMsg);
57 public void onMsgDeleteConversation(int instanceID, JSONObject jsonMsg) {
58 operation(instanceID, jsonMsg);
61 public void onMsgMarkMessageRead(int instanceID, JSONObject jsonMsg) {
62 operation(instanceID, jsonMsg);
65 public void onMsgMarkConversationRead(int instanceID, JSONObject jsonMsg) {
66 operation(instanceID, jsonMsg);
69 private Uri getUri(String type) {
70 if (type.equals("mms")) {
71 return Uri.parse("content://mms");
73 return Uri.parse("content://sms");
77 private void queryMessage(int instanceID, JSONObject jsonMsg) {
78 String promise_id = null, msgType = null, cmd = null, messageID = null;
79 JSONObject filter = null, filterOption = null;
82 promise_id = jsonMsg.getString("_promise_id");
83 cmd = jsonMsg.getString("cmd");
84 JSONObject eventBody = jsonMsg.getJSONObject("data");
85 if (eventBody.has("messageID")) {
86 messageID = eventBody.getString("messageID");
88 if (eventBody.has("filter")) {
89 filter = eventBody.getJSONObject("filter");
91 if (eventBody.has("options")) {
92 filterOption = eventBody.getJSONObject("options");
95 msgType = filter.getString("type");
97 msgType = eventBody.getString("type");
99 } catch (JSONException e) {
104 if (!msgType.equals("sms") && !msgType.equals("mms")) {
105 Log.e(TAG, "Invalidate message type: " + msgType);
109 ContentResolver cr = mMainActivity.getContentResolver();
110 Uri contentUri = getUri(msgType);
111 String sqlString = null;
112 String[] sqlArgs = null;
113 String sqlOption = null;
115 if (cmd.equals("msg_findMessages")) {
116 Object[] retValue = MessagingHelpers.buildSqlFilterString(filter);
117 sqlString = (String)retValue[0];
118 sqlArgs = (String[])retValue[1];
119 sqlOption = MessagingHelpers.buildSqlFilterOptionString(filterOption);
121 sqlString = String.format("%s = ?", MessagingSmsConsts.ID);
122 sqlArgs = new String[]{messageID};
125 Cursor cursor = cr.query(contentUri, null, sqlString, sqlArgs, sqlOption);
127 JSONObject jsonMsgRet = null;
128 JSONArray results = null;
130 jsonMsgRet = new JSONObject();
131 results = new JSONArray();
132 jsonMsgRet.put("_promise_id", promise_id);
133 jsonMsgRet.put("cmd", cmd + "_ret");
134 JSONObject jsData = new JSONObject();
135 jsonMsgRet.put("data", jsData);
136 jsData.put("error", false);
137 JSONObject jsBody = new JSONObject();
138 jsData.put("body", jsBody);
139 jsBody.put("results", results);
140 } catch (JSONException e) {
146 if (msgType.equals("mms")) {
147 // TODO:(shawn) Pending on Android MMS related api get public.
148 // MMS is implemented in native messaging app, but they are not exposed as public APIs.
149 // We ever tired to backport ~60 files with MMS feature supporting. Considering the pros and
150 // cons, we would rather break the MMS feature than doing the ugly backport.
151 } else if (cursor.getCount() > 0) {
152 while (cursor.moveToNext()) {
153 JSONObject jsonSmsObj = MessagingHelpers.SmsMessageCursor2Json(cursor);
154 if (null != jsonSmsObj) {
155 results.put(jsonSmsObj);
163 mMessagingHandler.postMessage(instanceID, jsonMsgRet.toString());
166 private void operation(int instanceID, JSONObject jsonMsg) {
167 JSONObject eventBody = null;
168 String promise_id = null, msgType = null, id = null, cmd = null;
169 boolean isRead = false;
172 promise_id = jsonMsg.getString("_promise_id");
173 eventBody = jsonMsg.getJSONObject("data");
174 if (eventBody.has("messageID")) {
175 id = eventBody.getString("messageID");
177 id = eventBody.getString("conversationID");
179 cmd = jsonMsg.getString("cmd");
180 if (eventBody.has("value")) {
181 isRead = eventBody.getBoolean("value");
183 msgType = eventBody.getString("type");
184 } catch (JSONException e) {
189 String selString = null;
190 if (eventBody.has("messageID")) {
191 selString = String.format("%s = ?", MessagingSmsConsts.ID);
193 selString = String.format("%s = ?", MessagingSmsConsts.THREAD_ID);
196 String[] selArgs = new String[]{id};
197 ContentResolver cr = mMainActivity.getContentResolver();
198 Uri contentUri = getUri(msgType);
200 if (cmd.equals("msg_deleteMessage") || cmd.equals("msg_deleteConversation")) {
201 cr.delete(contentUri, selString, selArgs);
202 } else if (cmd.equals("msg_markMessageRead") || cmd.equals("msg_markConversationRead")) {
203 ContentValues values = new ContentValues();
204 values.put("read", isRead ? "1" : "0");
205 cr.update(contentUri, values, selString, selArgs);
208 JSONObject jsonMsgRet = null;
210 jsonMsgRet = new JSONObject();
211 jsonMsgRet.put("_promise_id", promise_id);
212 JSONObject jsData = new JSONObject();
213 jsonMsgRet.put("data", jsData);
214 jsData.put("error", false);
215 JSONObject jsBody = new JSONObject();
216 jsData.put("body", jsBody);
217 if (eventBody.has("messageID")) {
218 jsBody.put("messageID", id);
220 jsBody.put("conversationID", id);
222 jsonMsgRet.put("cmd", cmd + "_ret");
223 } catch (JSONException e) {
228 mMessagingHandler.postMessage(instanceID, jsonMsgRet.toString());