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