1/* 2 * Copyright (C) 2011 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.cellbroadcastreceiver; 18 19import android.content.BroadcastReceiver; 20import android.content.Context; 21import android.content.Intent; 22import android.os.RemoteException; 23import android.os.ServiceManager; 24import android.provider.Telephony; 25import android.telephony.TelephonyManager; 26import android.util.Log; 27 28import com.android.internal.telephony.ITelephony; 29 30public class CellBroadcastReceiver extends BroadcastReceiver { 31 private static final String TAG = "CellBroadcastReceiver"; 32 static final boolean DBG = true; // TODO: change to false before ship 33 34 @Override 35 public void onReceive(Context context, Intent intent) { 36 onReceiveWithPrivilege(context, intent, false); 37 } 38 39 protected void onReceiveWithPrivilege(Context context, Intent intent, boolean privileged) { 40 if (DBG) Log.d(TAG, "onReceive " + intent); 41 42 String action = intent.getAction(); 43 44 if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { 45 startConfigService(context); 46 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) { 47 boolean airplaneModeOn = intent.getBooleanExtra("state", false); 48 if (!airplaneModeOn) { 49 startConfigService(context); 50 } 51 } else if (Telephony.Sms.Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION.equals(action) || 52 Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION.equals(action)) { 53 // If 'privileged' is false, it means that the intent was delivered to the base 54 // no-permissions receiver class. If we get an SMS_CB_RECEIVED message that way, it 55 // means someone has tried to spoof the message by delivering it outside the normal 56 // permission-checked route, so we just ignore it. 57 if (privileged) { 58 intent.setClass(context, CellBroadcastAlertService.class); 59 context.startService(intent); 60 } else { 61 Log.e(TAG, "ignoring unprivileged action received " + action); 62 } 63 } else { 64 Log.w(TAG, "onReceive() unexpected action " + action); 65 } 66 } 67 68 /** 69 * Tell {@link CellBroadcastConfigService} to enable the CB channels. 70 * @param context the broadcast receiver context 71 */ 72 static void startConfigService(Context context) { 73 if (phoneIsCdma()) { 74 Log.d(TAG, "CDMA phone detected; doing nothing"); 75 } else { 76 Intent serviceIntent = new Intent(CellBroadcastConfigService.ACTION_ENABLE_CHANNELS, 77 null, context, CellBroadcastConfigService.class); 78 context.startService(serviceIntent); 79 } 80 } 81 82 /** 83 * @return true if the phone is a CDMA phone type 84 */ 85 private static boolean phoneIsCdma() { 86 boolean isCdma = false; 87 try { 88 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); 89 if (phone != null) { 90 isCdma = (phone.getActivePhoneType() == TelephonyManager.PHONE_TYPE_CDMA); 91 } 92 } catch (RemoteException e) { 93 Log.w(TAG, "phone.getActivePhoneType() failed", e); 94 } 95 return isCdma; 96 } 97} 98