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