Home | History | Annotate | Download | only in telephony
      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.android.internal.telephony;
     18 
     19 import android.Manifest;
     20 import android.app.Activity;
     21 import android.app.AppOpsManager;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.os.Message;
     25 import android.provider.Telephony;
     26 import android.telephony.SubscriptionManager;
     27 import android.telephony.SmsCbMessage;
     28 
     29 /**
     30  * Dispatch new Cell Broadcasts to receivers. Acquires a private wakelock until the broadcast
     31  * completes and our result receiver is called.
     32  */
     33 public class CellBroadcastHandler extends WakeLockStateMachine {
     34 
     35     private CellBroadcastHandler(Context context, PhoneBase phone) {
     36         this("CellBroadcastHandler", context, phone);
     37     }
     38 
     39     protected CellBroadcastHandler(String debugTag, Context context, PhoneBase phone) {
     40         super(debugTag, context, phone);
     41     }
     42 
     43     /**
     44      * Create a new CellBroadcastHandler.
     45      * @param context the context to use for dispatching Intents
     46      * @return the new handler
     47      */
     48     public static CellBroadcastHandler makeCellBroadcastHandler(Context context, PhoneBase phone) {
     49         CellBroadcastHandler handler = new CellBroadcastHandler(context, phone);
     50         handler.start();
     51         return handler;
     52     }
     53 
     54     /**
     55      * Handle Cell Broadcast messages from {@code CdmaInboundSmsHandler}.
     56      * 3GPP-format Cell Broadcast messages sent from radio are handled in the subclass.
     57      *
     58      * @param message the message to process
     59      * @return true if an ordered broadcast was sent; false on failure
     60      */
     61     @Override
     62     protected boolean handleSmsMessage(Message message) {
     63         if (message.obj instanceof SmsCbMessage) {
     64             handleBroadcastSms((SmsCbMessage) message.obj);
     65             return true;
     66         } else {
     67             loge("handleMessage got object of type: " + message.obj.getClass().getName());
     68             return false;
     69         }
     70     }
     71 
     72     /**
     73      * Dispatch a Cell Broadcast message to listeners.
     74      * @param message the Cell Broadcast to broadcast
     75      */
     76     protected void handleBroadcastSms(SmsCbMessage message) {
     77         String receiverPermission;
     78         int appOp;
     79         Intent intent;
     80         if (message.isEmergencyMessage()) {
     81             log("Dispatching emergency SMS CB");
     82             intent = new Intent(Telephony.Sms.Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION);
     83             receiverPermission = Manifest.permission.RECEIVE_EMERGENCY_BROADCAST;
     84             appOp = AppOpsManager.OP_RECEIVE_EMERGECY_SMS;
     85         } else {
     86             log("Dispatching SMS CB");
     87             intent = new Intent(Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION);
     88             receiverPermission = Manifest.permission.RECEIVE_SMS;
     89             appOp = AppOpsManager.OP_RECEIVE_SMS;
     90         }
     91         intent.putExtra("message", message);
     92         SubscriptionManager.putPhoneIdAndSubIdExtra(intent, mPhone.getPhoneId());
     93         mContext.sendOrderedBroadcast(intent, receiverPermission, appOp, mReceiver,
     94                 getHandler(), Activity.RESULT_OK, null, null);
     95     }
     96 }
     97