CellBroadcastConfigService.java revision 50a624a47ce645a7992e346e40a4e7ec5e0df9b7
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.preference.PreferenceManager;
24import android.telephony.SmsCbConstants;
25import android.telephony.SmsManager;
26import android.util.Log;
27
28import com.android.internal.telephony.gsm.SmsCbHeader;
29
30import static com.android.cellbroadcastreceiver.CellBroadcastReceiver.DBG;
31
32/**
33 * This service manages enabling and disabling ranges of message identifiers
34 * that the radio should listen for. It operates independently of the other
35 * services and runs at boot time and after exiting airplane mode.
36 *
37 * Note that the entire range of emergency channels is enabled. Test messages
38 * and lower priority broadcasts are filtered out in CellBroadcastAlertService
39 * if the user has not enabled them in settings.
40 *
41 * TODO: add notification to re-enable channels after a radio reset.
42 */
43public class CellBroadcastConfigService extends IntentService {
44    private static final String TAG = "CellBroadcastConfigService";
45
46    static final String ACTION_ENABLE_CHANNELS = "ACTION_ENABLE_CHANNELS";
47
48    public CellBroadcastConfigService() {
49        super(TAG);          // use class name for worker thread name
50    }
51
52    @Override
53    protected void onHandleIntent(Intent intent) {
54        if (ACTION_ENABLE_CHANNELS.equals(intent.getAction())) {
55            try {
56                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
57                Resources res = getResources();
58
59                boolean enableEmergencyAlerts = prefs.getBoolean(
60                        CellBroadcastSettings.KEY_ENABLE_EMERGENCY_ALERTS, true);
61
62                boolean enableChannel50Alerts = res.getBoolean(R.bool.show_brazil_settings) &&
63                        prefs.getBoolean(CellBroadcastSettings.KEY_ENABLE_CHANNEL_50_ALERTS, true);
64
65                SmsManager manager = SmsManager.getDefault();
66                if (enableEmergencyAlerts) {
67                    if (DBG) Log.d(TAG, "enabling emergency cell broadcast channels");
68                    manager.enableCellBroadcastRange(
69                            SmsCbConstants.MESSAGE_ID_PWS_FIRST_IDENTIFIER,
70                            SmsCbConstants.MESSAGE_ID_PWS_LAST_IDENTIFIER);
71                    if (DBG) Log.d(TAG, "enabled emergency cell broadcast channels");
72                } else {
73                    // we may have enabled these channels previously, so try to disable them
74                    if (DBG) Log.d(TAG, "disabling emergency cell broadcast channels");
75                    manager.disableCellBroadcastRange(
76                            SmsCbConstants.MESSAGE_ID_PWS_FIRST_IDENTIFIER,
77                            SmsCbConstants.MESSAGE_ID_PWS_LAST_IDENTIFIER);
78                    if (DBG) Log.d(TAG, "disabled emergency cell broadcast channels");
79                }
80
81                if (enableChannel50Alerts) {
82                    if (DBG) Log.d(TAG, "enabling cell broadcast channel 50");
83                    manager.enableCellBroadcast(50);
84                    if (DBG) Log.d(TAG, "enabled cell broadcast channel 50");
85                } else {
86                    if (DBG) Log.d(TAG, "disabling cell broadcast channel 50");
87                    manager.disableCellBroadcast(50);
88                    if (DBG) Log.d(TAG, "disabled cell broadcast channel 50");
89                }
90            } catch (Exception ex) {
91                Log.e(TAG, "exception enabling cell broadcast channels", ex);
92            }
93        }
94    }
95}
96