Home | History | Annotate | Download | only in os
      1 /*
      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 
     17 package com.example.android.apis.os;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.provider.Telephony;
     23 import android.util.Log;
     24 
     25 import com.google.android.mms.ContentType;
     26 import com.google.android.mms.pdu.GenericPdu;
     27 import com.google.android.mms.pdu.NotificationInd;
     28 import com.google.android.mms.pdu.PduHeaders;
     29 import com.google.android.mms.pdu.PduParser;
     30 
     31 /**
     32  * Receiver for MMS WAP push
     33  */
     34 public 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");
     42             final PduParser parser = new PduParser(
     43                     data, PduParserUtil.shouldParseContentDisposition());
     44             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 }
     79