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