CellBroadcastConfigService.java revision f88de2b37383f138b3c7d876bdc3b6b6fae7b728
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    static boolean isOperatorDefinedEmergencyId(int messageId) {
85        // Check for system property defining the emergency channel ranges to enable
86        String emergencyIdRange = SystemProperties.get("ro.cellbroadcast.emergencyids");
87        if (TextUtils.isEmpty(emergencyIdRange)) {
88            return false;
89        }
90        try {
91            for (String channelRange : emergencyIdRange.split(",")) {
92                int dashIndex = channelRange.indexOf('-');
93                if (dashIndex != -1) {
94                    int startId = Integer.decode(channelRange.substring(0, dashIndex));
95                    int endId = Integer.decode(channelRange.substring(dashIndex + 1));
96                    if (messageId >= startId && messageId <= endId) {
97                        return true;
98                    }
99                } else {
100                    int emergencyMessageId = Integer.decode(channelRange);
101                    if (emergencyMessageId == messageId) {
102                        return true;
103                    }
104                }
105            }
106        } catch (NumberFormatException e) {
107            Log.e(TAG, "Number Format Exception parsing emergency channel range", e);
108        }
109        return false;
110    }
111
112    @Override
113    protected void onHandleIntent(Intent intent) {
114        if (ACTION_ENABLE_CHANNELS.equals(intent.getAction())) {
115            try {
116                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
117                Resources res = getResources();
118
119                // Check for system property defining the emergency channel ranges to enable
120                String emergencyIdRange = SystemProperties.get("ro.cellbroadcast.emergencyids");
121
122                boolean enableEmergencyAlerts = prefs.getBoolean(
123                        CellBroadcastSettings.KEY_ENABLE_EMERGENCY_ALERTS, true);
124
125                boolean enableChannel50Alerts = res.getBoolean(R.bool.show_brazil_settings) &&
126                        prefs.getBoolean(CellBroadcastSettings.KEY_ENABLE_CHANNEL_50_ALERTS, true);
127
128                SmsManager manager = SmsManager.getDefault();
129                if (enableEmergencyAlerts) {
130                    if (DBG) Log.d(TAG, "enabling emergency cell broadcast channels");
131                    if (!TextUtils.isEmpty(emergencyIdRange)) {
132                        setChannelRange(manager, emergencyIdRange, true);
133                    } else {
134                        // No emergency channel system property, enable all emergency channels
135                        manager.enableCellBroadcastRange(
136                                SmsCbConstants.MESSAGE_ID_PWS_FIRST_IDENTIFIER,
137                                SmsCbConstants.MESSAGE_ID_PWS_LAST_IDENTIFIER);
138                    }
139                    if (DBG) Log.d(TAG, "enabled emergency cell broadcast channels");
140                } else {
141                    // we may have enabled these channels previously, so try to disable them
142                    if (DBG) Log.d(TAG, "disabling emergency cell broadcast channels");
143                    if (!TextUtils.isEmpty(emergencyIdRange)) {
144                        setChannelRange(manager, emergencyIdRange, false);
145                    } else {
146                        // No emergency channel system property, disable all emergency channels
147                        manager.disableCellBroadcastRange(
148                                SmsCbConstants.MESSAGE_ID_PWS_FIRST_IDENTIFIER,
149                                SmsCbConstants.MESSAGE_ID_PWS_LAST_IDENTIFIER);
150                    }
151                    if (DBG) Log.d(TAG, "disabled emergency cell broadcast channels");
152                }
153
154                if (enableChannel50Alerts) {
155                    if (DBG) Log.d(TAG, "enabling cell broadcast channel 50");
156                    manager.enableCellBroadcast(50);
157                    if (DBG) Log.d(TAG, "enabled cell broadcast channel 50");
158                } else {
159                    if (DBG) Log.d(TAG, "disabling cell broadcast channel 50");
160                    manager.disableCellBroadcast(50);
161                    if (DBG) Log.d(TAG, "disabled cell broadcast channel 50");
162                }
163            } catch (Exception ex) {
164                Log.e(TAG, "exception enabling cell broadcast channels", ex);
165            }
166        }
167    }
168}
169