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 17 package com.android.cellbroadcastreceiver; 18 19 import android.app.IntentService; 20 import android.content.Intent; 21 import android.content.SharedPreferences; 22 import android.content.res.Resources; 23 import android.os.SystemProperties; 24 import android.preference.PreferenceManager; 25 import android.telephony.SmsManager; 26 import android.text.TextUtils; 27 import android.util.Log; 28 29 import com.android.internal.telephony.gsm.SmsCbConstants; 30 31 import static com.android.cellbroadcastreceiver.CellBroadcastReceiver.DBG; 32 33 /** 34 * This service manages enabling and disabling ranges of message identifiers 35 * that the radio should listen for. It operates independently of the other 36 * services and runs at boot time and after exiting airplane mode. 37 * 38 * Note that the entire range of emergency channels is enabled. Test messages 39 * and lower priority broadcasts are filtered out in CellBroadcastAlertService 40 * if the user has not enabled them in settings. 41 * 42 * TODO: add notification to re-enable channels after a radio reset. 43 */ 44 public class CellBroadcastConfigService extends IntentService { 45 private static final String TAG = "CellBroadcastConfigService"; 46 47 static final String ACTION_ENABLE_CHANNELS = "ACTION_ENABLE_CHANNELS"; 48 49 public CellBroadcastConfigService() { 50 super(TAG); // use class name for worker thread name 51 } 52 53 private static void setChannelRange(SmsManager manager, String ranges, boolean enable) { 54 try { 55 for (String channelRange : ranges.split(",")) { 56 int dashIndex = channelRange.indexOf('-'); 57 if (dashIndex != -1) { 58 int startId = Integer.decode(channelRange.substring(0, dashIndex)); 59 int endId = Integer.decode(channelRange.substring(dashIndex + 1)); 60 if (enable) { 61 if (DBG) log("enabling emergency IDs " + startId + '-' + endId); 62 manager.enableCellBroadcastRange(startId, endId); 63 } else { 64 if (DBG) log("disabling emergency IDs " + startId + '-' + endId); 65 manager.disableCellBroadcastRange(startId, endId); 66 } 67 } else { 68 int messageId = Integer.decode(channelRange); 69 if (enable) { 70 if (DBG) log("enabling emergency message ID " + messageId); 71 manager.enableCellBroadcast(messageId); 72 } else { 73 if (DBG) log("disabling emergency message ID " + messageId); 74 manager.disableCellBroadcast(messageId); 75 } 76 } 77 } 78 } catch (NumberFormatException e) { 79 Log.e(TAG, "Number Format Exception parsing emergency channel range", e); 80 } 81 } 82 83 static boolean isOperatorDefinedEmergencyId(int messageId) { 84 // Check for system property defining the emergency channel ranges to enable 85 String emergencyIdRange = SystemProperties.get("ro.cellbroadcast.emergencyids"); 86 if (TextUtils.isEmpty(emergencyIdRange)) { 87 return false; 88 } 89 try { 90 for (String channelRange : emergencyIdRange.split(",")) { 91 int dashIndex = channelRange.indexOf('-'); 92 if (dashIndex != -1) { 93 int startId = Integer.decode(channelRange.substring(0, dashIndex)); 94 int endId = Integer.decode(channelRange.substring(dashIndex + 1)); 95 if (messageId >= startId && messageId <= endId) { 96 return true; 97 } 98 } else { 99 int emergencyMessageId = Integer.decode(channelRange); 100 if (emergencyMessageId == messageId) { 101 return true; 102 } 103 } 104 } 105 } catch (NumberFormatException e) { 106 Log.e(TAG, "Number Format Exception parsing emergency channel range", e); 107 } 108 return false; 109 } 110 111 @Override 112 protected void onHandleIntent(Intent intent) { 113 if (ACTION_ENABLE_CHANNELS.equals(intent.getAction())) { 114 try { 115 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 116 Resources res = getResources(); 117 118 // Check for system property defining the emergency channel ranges to enable 119 String emergencyIdRange = SystemProperties.get("ro.cellbroadcast.emergencyids"); 120 121 boolean enableEmergencyAlerts = prefs.getBoolean( 122 CellBroadcastSettings.KEY_ENABLE_EMERGENCY_ALERTS, true); 123 124 boolean enableChannel50Alerts = res.getBoolean(R.bool.show_brazil_settings) && 125 prefs.getBoolean(CellBroadcastSettings.KEY_ENABLE_CHANNEL_50_ALERTS, true); 126 127 SmsManager manager = SmsManager.getDefault(); 128 if (enableEmergencyAlerts) { 129 if (DBG) log("enabling emergency cell broadcast channels"); 130 if (!TextUtils.isEmpty(emergencyIdRange)) { 131 setChannelRange(manager, emergencyIdRange, true); 132 } else { 133 // No emergency channel system property, enable all emergency channels 134 manager.enableCellBroadcastRange( 135 SmsCbConstants.MESSAGE_ID_PWS_FIRST_IDENTIFIER, 136 SmsCbConstants.MESSAGE_ID_PWS_LAST_IDENTIFIER); 137 } 138 if (DBG) log("enabled emergency cell broadcast channels"); 139 } else { 140 // we may have enabled these channels previously, so try to disable them 141 if (DBG) log("disabling emergency cell broadcast channels"); 142 if (!TextUtils.isEmpty(emergencyIdRange)) { 143 setChannelRange(manager, emergencyIdRange, false); 144 } else { 145 // No emergency channel system property, disable all emergency channels 146 manager.disableCellBroadcastRange( 147 SmsCbConstants.MESSAGE_ID_PWS_FIRST_IDENTIFIER, 148 SmsCbConstants.MESSAGE_ID_PWS_LAST_IDENTIFIER); 149 } 150 if (DBG) log("disabled emergency cell broadcast channels"); 151 } 152 153 if (enableChannel50Alerts) { 154 if (DBG) log("enabling cell broadcast channel 50"); 155 manager.enableCellBroadcast(50); 156 if (DBG) log("enabled cell broadcast channel 50"); 157 } else { 158 if (DBG) log("disabling cell broadcast channel 50"); 159 manager.disableCellBroadcast(50); 160 if (DBG) log("disabled cell broadcast channel 50"); 161 } 162 } catch (Exception ex) { 163 Log.e(TAG, "exception enabling cell broadcast channels", ex); 164 } 165 } 166 } 167 168 private static void log(String msg) { 169 Log.d(TAG, msg); 170 } 171 } 172