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 com.google.android.mms.ContentType;
     20 import com.google.android.mms.pdu.GenericPdu;
     21 import com.google.android.mms.pdu.NotificationInd;
     22 import com.google.android.mms.pdu.PduHeaders;
     23 import com.google.android.mms.pdu.PduParser;
     24 
     25 import android.content.BroadcastReceiver;
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.provider.Telephony;
     29 import android.util.Log;
     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(data);
     43             GenericPdu pdu = null;
     44             try {
     45                 pdu = parser.parse();
     46             } catch (final RuntimeException e) {
     47                 Log.e(TAG, "Invalid MMS WAP push", e);
     48             }
     49             if (pdu == null) {
     50                 Log.e(TAG, "Invalid WAP push data");
     51                 return;
     52             }
     53             switch (pdu.getMessageType()) {
     54                 case PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND: {
     55                     final NotificationInd nInd = (NotificationInd) pdu;
     56                     final String location = new String(nInd.getContentLocation());
     57                     Log.v(TAG, "Received MMS notification: " + location);
     58                     final Intent di = new Intent();
     59                     di.setClass(context, MmsMessagingDemo.class);
     60                     di.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
     61                     di.putExtra(MmsMessagingDemo.EXTRA_NOTIFICATION_URL, location);
     62                     context.startActivity(di);
     63                     break;
     64                 }
     65                 // FLAG (ywen): impl. handling of the following push
     66                 case PduHeaders.MESSAGE_TYPE_DELIVERY_IND: {
     67                     Log.v(TAG, "Received delivery report");
     68                     break;
     69                 }
     70                 case PduHeaders.MESSAGE_TYPE_READ_ORIG_IND: {
     71                     Log.v(TAG, "Received read report");
     72                     break;
     73                 }
     74             }
     75         }
     76     }
     77 }
     78