• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007-2008 Esmertec AG.
3  * Copyright (C) 2007-2008 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.mms.transaction;
19 
20 import static android.provider.Telephony.Sms.Intents.WAP_PUSH_RECEIVED_ACTION;
21 import static com.google.android.mms.pdu.PduHeaders.MESSAGE_TYPE_DELIVERY_IND;
22 import static com.google.android.mms.pdu.PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND;
23 import static com.google.android.mms.pdu.PduHeaders.MESSAGE_TYPE_READ_ORIG_IND;
24 
25 import com.android.mms.MmsConfig;
26 import com.google.android.mms.ContentType;
27 import com.google.android.mms.MmsException;
28 import com.google.android.mms.pdu.DeliveryInd;
29 import com.google.android.mms.pdu.GenericPdu;
30 import com.google.android.mms.pdu.NotificationInd;
31 import com.google.android.mms.pdu.PduHeaders;
32 import com.google.android.mms.pdu.PduParser;
33 import com.google.android.mms.pdu.PduPersister;
34 import com.google.android.mms.pdu.ReadOrigInd;
35 import android.database.sqlite.SqliteWrapper;
36 
37 import android.content.BroadcastReceiver;
38 import android.content.ContentResolver;
39 import android.content.ContentValues;
40 import android.content.Context;
41 import android.content.Intent;
42 import android.database.Cursor;
43 import android.database.DatabaseUtils;
44 import android.net.Uri;
45 import android.os.AsyncTask;
46 import android.os.PowerManager;
47 import android.provider.Telephony.Mms;
48 import android.provider.Telephony.Mms.Inbox;
49 import android.util.Config;
50 import android.util.Log;
51 
52 /**
53  * Receives Intent.WAP_PUSH_RECEIVED_ACTION intents and starts the
54  * TransactionService by passing the push-data to it.
55  */
56 public class PushReceiver extends BroadcastReceiver {
57     private static final String TAG = "PushReceiver";
58     private static final boolean DEBUG = false;
59     private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV;
60 
61     private class ReceivePushTask extends AsyncTask<Intent,Void,Void> {
62         private Context mContext;
ReceivePushTask(Context context)63         public ReceivePushTask(Context context) {
64             mContext = context;
65         }
66 
67         @Override
doInBackground(Intent... intents)68         protected Void doInBackground(Intent... intents) {
69             Intent intent = intents[0];
70 
71             // Get raw PDU push-data from the message and parse it
72             byte[] pushData = intent.getByteArrayExtra("data");
73             PduParser parser = new PduParser(pushData);
74             GenericPdu pdu = parser.parse();
75 
76             if (null == pdu) {
77                 Log.e(TAG, "Invalid PUSH data");
78                 return null;
79             }
80 
81             PduPersister p = PduPersister.getPduPersister(mContext);
82             ContentResolver cr = mContext.getContentResolver();
83             int type = pdu.getMessageType();
84             long threadId = -1;
85 
86             try {
87                 switch (type) {
88                     case MESSAGE_TYPE_DELIVERY_IND:
89                     case MESSAGE_TYPE_READ_ORIG_IND: {
90                         threadId = findThreadId(mContext, pdu, type);
91                         if (threadId == -1) {
92                             // The associated SendReq isn't found, therefore skip
93                             // processing this PDU.
94                             break;
95                         }
96 
97                         Uri uri = p.persist(pdu, Inbox.CONTENT_URI);
98                         // Update thread ID for ReadOrigInd & DeliveryInd.
99                         ContentValues values = new ContentValues(1);
100                         values.put(Mms.THREAD_ID, threadId);
101                         SqliteWrapper.update(mContext, cr, uri, values, null, null);
102                         break;
103                     }
104                     case MESSAGE_TYPE_NOTIFICATION_IND: {
105                         NotificationInd nInd = (NotificationInd) pdu;
106 
107                         if (MmsConfig.getTransIdEnabled()) {
108                             byte [] contentLocation = nInd.getContentLocation();
109                             if ('=' == contentLocation[contentLocation.length - 1]) {
110                                 byte [] transactionId = nInd.getTransactionId();
111                                 byte [] contentLocationWithId = new byte [contentLocation.length
112                                                                           + transactionId.length];
113                                 System.arraycopy(contentLocation, 0, contentLocationWithId,
114                                         0, contentLocation.length);
115                                 System.arraycopy(transactionId, 0, contentLocationWithId,
116                                         contentLocation.length, transactionId.length);
117                                 nInd.setContentLocation(contentLocationWithId);
118                             }
119                         }
120 
121                         if (!isDuplicateNotification(mContext, nInd)) {
122                             Uri uri = p.persist(pdu, Inbox.CONTENT_URI);
123                             // Start service to finish the notification transaction.
124                             Intent svc = new Intent(mContext, TransactionService.class);
125                             svc.putExtra(TransactionBundle.URI, uri.toString());
126                             svc.putExtra(TransactionBundle.TRANSACTION_TYPE,
127                                     Transaction.NOTIFICATION_TRANSACTION);
128                             mContext.startService(svc);
129                         } else if (LOCAL_LOGV) {
130                             Log.v(TAG, "Skip downloading duplicate message: "
131                                     + new String(nInd.getContentLocation()));
132                         }
133                         break;
134                     }
135                     default:
136                         Log.e(TAG, "Received unrecognized PDU.");
137                 }
138             } catch (MmsException e) {
139                 Log.e(TAG, "Failed to save the data from PUSH: type=" + type, e);
140             } catch (RuntimeException e) {
141                 Log.e(TAG, "Unexpected RuntimeException.", e);
142             }
143 
144             if (LOCAL_LOGV) {
145                 Log.v(TAG, "PUSH Intent processed.");
146             }
147 
148             return null;
149         }
150     }
151 
152     @Override
onReceive(Context context, Intent intent)153     public void onReceive(Context context, Intent intent) {
154         if (intent.getAction().equals(WAP_PUSH_RECEIVED_ACTION)
155                 && ContentType.MMS_MESSAGE.equals(intent.getType())) {
156             if (LOCAL_LOGV) {
157                 Log.v(TAG, "Received PUSH Intent: " + intent);
158             }
159 
160             // Hold a wake lock for 5 seconds, enough to give any
161             // services we start time to take their own wake locks.
162             PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
163             PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
164                                             "MMS PushReceiver");
165             wl.acquire(5000);
166             new ReceivePushTask(context).execute(intent);
167         }
168     }
169 
findThreadId(Context context, GenericPdu pdu, int type)170     private static long findThreadId(Context context, GenericPdu pdu, int type) {
171         String messageId;
172 
173         if (type == MESSAGE_TYPE_DELIVERY_IND) {
174             messageId = new String(((DeliveryInd) pdu).getMessageId());
175         } else {
176             messageId = new String(((ReadOrigInd) pdu).getMessageId());
177         }
178 
179         StringBuilder sb = new StringBuilder('(');
180         sb.append(Mms.MESSAGE_ID);
181         sb.append('=');
182         sb.append(DatabaseUtils.sqlEscapeString(messageId));
183         sb.append(" AND ");
184         sb.append(Mms.MESSAGE_TYPE);
185         sb.append('=');
186         sb.append(PduHeaders.MESSAGE_TYPE_SEND_REQ);
187         // TODO ContentResolver.query() appends closing ')' to the selection argument
188         // sb.append(')');
189 
190         Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
191                             Mms.CONTENT_URI, new String[] { Mms.THREAD_ID },
192                             sb.toString(), null, null);
193         if (cursor != null) {
194             try {
195                 if ((cursor.getCount() == 1) && cursor.moveToFirst()) {
196                     return cursor.getLong(0);
197                 }
198             } finally {
199                 cursor.close();
200             }
201         }
202 
203         return -1;
204     }
205 
isDuplicateNotification( Context context, NotificationInd nInd)206     private static boolean isDuplicateNotification(
207             Context context, NotificationInd nInd) {
208         byte[] rawLocation = nInd.getContentLocation();
209         if (rawLocation != null) {
210             String location = new String(rawLocation);
211             String selection = Mms.CONTENT_LOCATION + " = ?";
212             String[] selectionArgs = new String[] { location };
213             Cursor cursor = SqliteWrapper.query(
214                     context, context.getContentResolver(),
215                     Mms.CONTENT_URI, new String[] { Mms._ID },
216                     selection, selectionArgs, null);
217             if (cursor != null) {
218                 try {
219                     if (cursor.getCount() > 0) {
220                         // We already received the same notification before.
221                         return true;
222                     }
223                 } finally {
224                     cursor.close();
225                 }
226             }
227         }
228         return false;
229     }
230 }
231