Home | History | Annotate | Download | only in receiver
      1 /*
      2  * Copyright (C) 2013 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.smssample.receiver;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.provider.Telephony.Sms.Intents;
     22 import android.support.v4.content.WakefulBroadcastReceiver;
     23 
     24 import com.example.android.smssample.service.MessagingService;
     25 import com.example.android.smssample.Utils;
     26 
     27 /**
     28  * The main messaging receiver class. Note that this is not directly included in
     29  * AndroidManifest.xml, instead, subclassed versions of this are included which allows
     30  * them to be enabled/disabled independently as they will have a unique component name.
     31  */
     32 public class MessagingReceiver extends WakefulBroadcastReceiver {
     33 
     34     @Override
     35     public void onReceive(Context context, Intent intent) {
     36         String action = intent == null ? null : intent.getAction();
     37 
     38         // If on KitKat+ and default messaging app then look for new deliver actions actions.
     39         if (Utils.hasKitKat() && Utils.isDefaultSmsApp(context)) {
     40             if (Intents.SMS_DELIVER_ACTION.equals(action)) {
     41                 handleIncomingSms(context, intent);
     42             } else if (Intents.WAP_PUSH_DELIVER_ACTION.equals(action)) {
     43                 handleIncomingMms(context, intent);
     44             }
     45         } else { // Otherwise look for old pre-KitKat actions
     46             if (Intents.SMS_RECEIVED_ACTION.equals(action)) {
     47                 handleIncomingSms(context, intent);
     48             } else if (Intents.WAP_PUSH_RECEIVED_ACTION.equals(action)) {
     49                 handleIncomingMms(context, intent);
     50             }
     51         }
     52     }
     53 
     54     private void handleIncomingSms(Context context, Intent intent) {
     55         // TODO: Handle SMS here
     56         // As an example, we'll start a wakeful service to handle the SMS
     57         intent.setAction(MessagingService.ACTION_MY_RECEIVE_SMS);
     58         intent.setClass(context, MessagingService.class);
     59         startWakefulService(context, intent);
     60     }
     61 
     62     private void handleIncomingMms(Context context, Intent intent) {
     63         // TODO: Handle MMS here
     64         // As an example, we'll start a wakeful service to handle the MMS
     65         intent.setAction(MessagingService.ACTION_MY_RECEIVE_MMS);
     66         intent.setClass(context, MessagingService.class);
     67         startWakefulService(context, intent);
     68     }
     69 }
     70