blob: f2ca090b0d0f362fffb1c4ba65bba17ffac5587f [file] [log] [blame]
Ye Wen69291d62014-05-15 13:06:12 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.apis.os;
18
Ye Wen69291d62014-05-15 13:06:12 -070019import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.provider.Telephony;
23import android.util.Log;
24
Ye Wen03f9acc2014-11-17 17:19:50 -080025import com.google.android.mms.ContentType;
26import com.google.android.mms.pdu.GenericPdu;
27import com.google.android.mms.pdu.NotificationInd;
28import com.google.android.mms.pdu.PduHeaders;
29import com.google.android.mms.pdu.PduParser;
30
Ye Wen69291d62014-05-15 13:06:12 -070031/**
32 * Receiver for MMS WAP push
33 */
34public class MmsWapPushReceiver extends BroadcastReceiver {
35 private static final String TAG = "MmsMessagingDemo";
36
37 @Override
38 public void onReceive(Context context, Intent intent) {
39 if (Telephony.Sms.Intents.WAP_PUSH_RECEIVED_ACTION.equals(intent.getAction())
40 && ContentType.MMS_MESSAGE.equals(intent.getType())) {
41 final byte[] data = intent.getByteArrayExtra("data");
Ye Wen03f9acc2014-11-17 17:19:50 -080042 final PduParser parser = new PduParser(
43 data, PduParserUtil.shouldParseContentDisposition());
Ye Wen69291d62014-05-15 13:06:12 -070044 GenericPdu pdu = null;
45 try {
46 pdu = parser.parse();
47 } catch (final RuntimeException e) {
48 Log.e(TAG, "Invalid MMS WAP push", e);
49 }
50 if (pdu == null) {
51 Log.e(TAG, "Invalid WAP push data");
52 return;
53 }
54 switch (pdu.getMessageType()) {
55 case PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND: {
56 final NotificationInd nInd = (NotificationInd) pdu;
57 final String location = new String(nInd.getContentLocation());
58 Log.v(TAG, "Received MMS notification: " + location);
59 final Intent di = new Intent();
60 di.setClass(context, MmsMessagingDemo.class);
61 di.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
62 di.putExtra(MmsMessagingDemo.EXTRA_NOTIFICATION_URL, location);
63 context.startActivity(di);
64 break;
65 }
66 // FLAG (ywen): impl. handling of the following push
67 case PduHeaders.MESSAGE_TYPE_DELIVERY_IND: {
68 Log.v(TAG, "Received delivery report");
69 break;
70 }
71 case PduHeaders.MESSAGE_TYPE_READ_ORIG_IND: {
72 Log.v(TAG, "Received read report");
73 break;
74 }
75 }
76 }
77 }
78}