Upstream version 8.36.156.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / extension / api / messaging / MessagingManager.java
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.
4
5 package org.xwalk.core.extension.api.messaging;
6
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; 
21
22 import java.text.SimpleDateFormat;
23 import java.text.ParseException;
24 import java.util.Date;
25
26 import org.json.JSONArray;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29
30 import org.xwalk.core.extension.api.messaging.Messaging;
31 import org.xwalk.core.extension.api.messaging.MessagingHelpers;
32 import org.xwalk.core.extension.api.messaging.MessagingSmsConstMaps;
33 import org.xwalk.core.extension.api.messaging.MessagingSmsConsts;
34
35 public class MessagingManager {
36     private final static String TAG = "MessagingManager"; 
37     private final Activity mMainActivity;
38     private final Messaging mMessagingHandler;
39
40     MessagingManager(Activity activity, Messaging messaging) {
41         mMainActivity = activity;
42         mMessagingHandler = messaging;
43     }
44
45     public void onMsgFindMessages(int instanceID, JSONObject jsonMsg) {
46         queryMessage(instanceID, jsonMsg);
47     }
48
49     public void onMsgGetMessage(int instanceID, JSONObject jsonMsg) {
50         queryMessage(instanceID, jsonMsg);
51     }
52
53     public void onMsgDeleteMessage(int instanceID, JSONObject jsonMsg) {
54         operation(instanceID, jsonMsg);
55     }
56
57     public void onMsgDeleteConversation(int instanceID, JSONObject jsonMsg) {
58         operation(instanceID, jsonMsg);
59     }
60
61     public void onMsgMarkMessageRead(int instanceID, JSONObject jsonMsg) {
62         operation(instanceID, jsonMsg);
63     }
64
65     public void onMsgMarkConversationRead(int instanceID, JSONObject jsonMsg) {
66         operation(instanceID, jsonMsg);
67     }
68
69     private Uri getUri(String type) {
70         if (type.equals("mms")) {
71             return Uri.parse("content://mms");
72         } else {
73             return Uri.parse("content://sms");
74         }
75     }
76
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;
80         
81         try {
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");
87             }
88             if (eventBody.has("filter")) {
89                 filter = eventBody.getJSONObject("filter");
90             }
91             if (eventBody.has("options")) {
92                 filterOption = eventBody.getJSONObject("options");
93             }
94             if (null != filter) {
95                 msgType = filter.getString("type");
96             } else {
97                 msgType = eventBody.getString("type");
98             }
99         } catch (JSONException e) {
100             e.printStackTrace();
101             return;
102         }
103
104         if (!msgType.equals("sms") && !msgType.equals("mms")) {
105             Log.e(TAG, "Invalidate message type: " + msgType);
106             return;
107         }
108
109         ContentResolver cr = mMainActivity.getContentResolver();
110         Uri contentUri = getUri(msgType);
111         String sqlString = null;
112         String[] sqlArgs = null;
113         String sqlOption = null;
114
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);
120         } else {
121             sqlString = String.format("%s = ?", MessagingSmsConsts.ID);
122             sqlArgs = new String[]{messageID};
123         }
124
125         Cursor cursor = cr.query(contentUri, null, sqlString, sqlArgs, sqlOption);
126
127         JSONObject jsonMsgRet = null;
128         JSONArray results = null;
129         try {
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) {
141             e.printStackTrace();
142             return;
143         }
144         
145         try {
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);
156                     }
157                 }
158             }
159         } finally {
160             cursor.close();
161         }
162         
163         mMessagingHandler.postMessage(instanceID, jsonMsgRet.toString());
164     }
165
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;
170
171         try {
172             promise_id = jsonMsg.getString("_promise_id");
173             eventBody = jsonMsg.getJSONObject("data");
174             if (eventBody.has("messageID")) {
175                 id = eventBody.getString("messageID");
176             } else {
177                 id = eventBody.getString("conversationID");
178             }
179             cmd = jsonMsg.getString("cmd");
180             if (eventBody.has("value")) {
181                 isRead = eventBody.getBoolean("value");
182             }
183             msgType = eventBody.getString("type");
184         } catch (JSONException e) {
185             e.printStackTrace();
186             return;
187         }
188
189         String selString = null;
190         if (eventBody.has("messageID")) {
191             selString = String.format("%s = ?", MessagingSmsConsts.ID);
192         } else {
193             selString = String.format("%s = ?", MessagingSmsConsts.THREAD_ID);
194         }
195
196         String[] selArgs = new String[]{id};
197         ContentResolver cr = mMainActivity.getContentResolver();
198         Uri contentUri = getUri(msgType);
199
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);
206         }
207
208         JSONObject jsonMsgRet = null;
209         try {
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);
219             } else {
220                 jsBody.put("conversationID", id);
221             }
222             jsonMsgRet.put("cmd", cmd + "_ret");
223         } catch (JSONException e) {
224             e.printStackTrace();
225             return;
226         }
227
228         mMessagingHandler.postMessage(instanceID, jsonMsgRet.toString());
229     }
230 }