-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPushReceiver.java
More file actions
executable file
·273 lines (236 loc) · 11.2 KB
/
PushReceiver.java
File metadata and controls
executable file
·273 lines (236 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright 2014 Jacob Klinker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.mms.transaction;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SqliteWrapper;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.Telephony.Mms;
import android.provider.Telephony.Mms.Inbox;
import com.android.mms.MmsConfig;
import com.google.android.mms.ContentType;
import com.google.android.mms.MmsException;
import com.google.android.mms.pdu_alt.DeliveryInd;
import com.google.android.mms.pdu_alt.GenericPdu;
import com.google.android.mms.pdu_alt.NotificationInd;
import com.google.android.mms.pdu_alt.PduHeaders;
import com.google.android.mms.pdu_alt.PduParser;
import com.google.android.mms.pdu_alt.PduPersister;
import com.google.android.mms.pdu_alt.ReadOrigInd;
import com.klinker.android.send_message.Utils;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import timber.log.Timber;
import static android.provider.Telephony.Sms.Intents.WAP_PUSH_DELIVER_ACTION;
import static android.provider.Telephony.Sms.Intents.WAP_PUSH_RECEIVED_ACTION;
import static com.google.android.mms.pdu_alt.PduHeaders.MESSAGE_TYPE_DELIVERY_IND;
import static com.google.android.mms.pdu_alt.PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND;
import static com.google.android.mms.pdu_alt.PduHeaders.MESSAGE_TYPE_READ_ORIG_IND;
/**
* Receives Intent.WAP_PUSH_RECEIVED_ACTION intents and starts the
* TransactionService by passing the push-data to it.
*/
public class PushReceiver extends BroadcastReceiver {
static final String[] PROJECTION = new String[]{Mms.CONTENT_LOCATION, Mms.LOCKED};
static final int COLUMN_CONTENT_LOCATION = 0;
private static Set<String> downloadedUrls = new HashSet<String>();
private static final ExecutorService PUSH_RECEIVER_EXECUTOR = Executors.newSingleThreadExecutor();
private class ReceivePushTask extends AsyncTask<Intent, Void, Void> {
private Context mContext;
private PendingResult pendingResult;
private ReceivePushTask(Context context, PendingResult pendingResult) {
mContext = context;
this.pendingResult = pendingResult;
}
@Override
protected Void doInBackground(Intent... intents) {
Timber.v("receiving a new mms message");
Intent intent = intents[0];
// Get raw PDU push-data from the message and parse it
byte[] pushData = intent.getByteArrayExtra("data");
PduParser parser = new PduParser(pushData);
GenericPdu pdu = parser.parse();
if (pdu == null) {
Timber.e("Invalid PUSH data");
return null;
}
PduPersister p = PduPersister.getPduPersister(mContext);
ContentResolver cr = mContext.getContentResolver();
int type = pdu.getMessageType();
long threadId;
try {
switch (type) {
case MESSAGE_TYPE_DELIVERY_IND:
case MESSAGE_TYPE_READ_ORIG_IND: {
threadId = findThreadId(mContext, pdu, type);
if (threadId == -1) {
// The associated SendReq isn't found, therefore skip
// processing this PDU.
break;
}
Uri uri = p.persist(pdu, Uri.parse("content://mms/inbox"),
PduPersister.DUMMY_THREAD_ID, true, true, null);
// Update thread ID for ReadOrigInd & DeliveryInd.
ContentValues values = new ContentValues(1);
values.put(Mms.THREAD_ID, threadId);
SqliteWrapper.update(mContext, cr, uri, values, null, null);
break;
}
case MESSAGE_TYPE_NOTIFICATION_IND: {
NotificationInd nInd = (NotificationInd) pdu;
if (MmsConfig.getTransIdEnabled()) {
byte[] contentLocation = nInd.getContentLocation();
if ('=' == contentLocation[contentLocation.length - 1]) {
byte[] transactionId = nInd.getTransactionId();
byte[] contentLocationWithId = new byte[contentLocation.length
+ transactionId.length];
System.arraycopy(contentLocation, 0, contentLocationWithId,
0, contentLocation.length);
System.arraycopy(transactionId, 0, contentLocationWithId,
contentLocation.length, transactionId.length);
nInd.setContentLocation(contentLocationWithId);
}
}
if (!isDuplicateNotification(mContext, nInd)) {
// Save the pdu. If we can start downloading the real pdu immediately,
// don't allow persist() to create a thread for the notificationInd
// because it causes UI jank.
Uri uri = p.persist(pdu, Inbox.CONTENT_URI,
PduPersister.DUMMY_THREAD_ID, true, true, null);
String location = getContentLocation(mContext, uri);
if (downloadedUrls.contains(location)) {
Timber.v("already added this download, don't download again");
return null;
} else {
downloadedUrls.add(location);
}
int subId = intent.getIntExtra("subscription", Utils.getDefaultSubscriptionId());
Timber.d("PushReceiver: MMS notification received, triggering download. location=" + location + " notifUri=" + uri + " subId=" + subId);
DownloadManager.getInstance().downloadMultimediaMessage(mContext, location, uri, true, subId);
} else {
Timber.v("Skip downloading duplicate message: " + new String(nInd.getContentLocation()));
}
break;
}
default:
Timber.e("Received unrecognized PDU.");
}
} catch (MmsException e) {
Timber.e(e, "Failed to save the data from PUSH: type=" + type);
} catch (RuntimeException e) {
Timber.e(e, "Unexpected RuntimeException.");
}
Timber.v("PUSH Intent processed.");
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
pendingResult.finish();
}
}
@Override
public void onReceive(Context context, Intent intent) {
Timber.v(intent.getAction() + " " + intent.getType());
if ((intent.getAction().equals(WAP_PUSH_DELIVER_ACTION) || intent.getAction().equals(WAP_PUSH_RECEIVED_ACTION))
&& ContentType.MMS_MESSAGE.equals(intent.getType())) {
Timber.v("Received PUSH Intent: " + intent);
MmsConfig.init(context);
new ReceivePushTask(context, goAsync()).executeOnExecutor(PUSH_RECEIVER_EXECUTOR, intent);
Timber.v(context.getPackageName() + " received and aborted");
}
}
public static String getContentLocation(Context context, Uri uri) throws MmsException {
Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
uri, PROJECTION, null, null, null);
if (cursor != null) {
try {
if ((cursor.getCount() == 1) && cursor.moveToFirst()) {
String location = cursor.getString(COLUMN_CONTENT_LOCATION);
cursor.close();
return location;
}
} finally {
cursor.close();
}
}
throw new MmsException("Cannot get X-Mms-Content-Location from: " + uri);
}
private static long findThreadId(Context context, GenericPdu pdu, int type) {
String messageId;
if (type == MESSAGE_TYPE_DELIVERY_IND) {
messageId = new String(((DeliveryInd) pdu).getMessageId());
} else {
messageId = new String(((ReadOrigInd) pdu).getMessageId());
}
StringBuilder sb = new StringBuilder('(');
sb.append(Mms.MESSAGE_ID);
sb.append('=');
sb.append(DatabaseUtils.sqlEscapeString(messageId));
sb.append(" AND ");
sb.append(Mms.MESSAGE_TYPE);
sb.append('=');
sb.append(PduHeaders.MESSAGE_TYPE_SEND_REQ);
// TODO ContentResolver.query() appends closing ')' to the selection argument
// sb.append(')');
Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
Mms.CONTENT_URI, new String[]{Mms.THREAD_ID},
sb.toString(), null, null);
if (cursor != null) {
try {
if ((cursor.getCount() == 1) && cursor.moveToFirst()) {
long id = cursor.getLong(0);
cursor.close();
return id;
}
} finally {
cursor.close();
}
}
return -1;
}
private static boolean isDuplicateNotification(Context context, NotificationInd nInd) {
byte[] rawLocation = nInd.getContentLocation();
if (rawLocation != null) {
String location = new String(rawLocation);
String selection = Mms.CONTENT_LOCATION + " = ?";
String[] selectionArgs = new String[]{location};
Cursor cursor = SqliteWrapper.query(
context, context.getContentResolver(),
Mms.CONTENT_URI, new String[]{Mms._ID},
selection, selectionArgs, null);
if (cursor != null) {
try {
if (cursor.getCount() > 0) {
// We already received the same notification before.
cursor.close();
//return true;
}
} finally {
cursor.close();
}
}
}
return false;
}
}