summaryrefslogtreecommitdiff
path: root/src/com/android/cellbroadcastreceiver/CellBroadcastSettings.java
blob: 9f609d42efecc11f0483258ad111ecfa477ee0bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.android.cellbroadcastreceiver;

import android.annotation.NonNull;
import android.app.ActionBar;
import android.app.ActivityManager;
import android.app.Fragment;
import android.app.backup.BackupManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.UserManager;
import android.os.Vibrator;
import android.telephony.SubscriptionManager;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Switch;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragment;
import androidx.preference.PreferenceManager;
import androidx.preference.TwoStatePreference;

import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.utils.build.SdkLevel;
import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity;
import com.android.settingslib.widget.MainSwitchPreference;
import com.android.settingslib.widget.OnMainSwitchChangeListener;

import java.util.HashMap;
import java.util.Map;

/**
 * Settings activity for the cell broadcast receiver.
 */
public class CellBroadcastSettings extends CollapsingToolbarBaseActivity {

    private static final String TAG = "CellBroadcastSettings";

    private static final boolean DBG = false;

    /**
     * Keys for user preferences.
     * When adding a new preference, make sure to clear its value in resetAllPreferences.
     */
    // Preference key for alert header (A text view, not clickable).
    public static final String KEY_ALERTS_HEADER = "alerts_header";

    // Preference key for a main toggle to enable/disable all alerts message (default enabled).
    public static final String KEY_ENABLE_ALERTS_MASTER_TOGGLE = "enable_alerts_master_toggle";

    // Preference key for whether to enable public safety messages (default enabled).
    public static final String KEY_ENABLE_PUBLIC_SAFETY_MESSAGES = "enable_public_safety_messages";

    // Preference key for whether to show full-screen public safety message (pop-up dialog), If set
    // to false, only display from message history and sms inbox if enabled. A foreground
    // notification might also be shown if enabled.
    public static final String KEY_ENABLE_PUBLIC_SAFETY_MESSAGES_FULL_SCREEN =
            "enable_public_safety_messages_full_screen";

    // Preference key for whether to enable emergency alerts (default enabled).
    public static final String KEY_ENABLE_EMERGENCY_ALERTS = "enable_emergency_alerts";

    // Enable vibration on alert (unless main volume is silent).
    public static final String KEY_ENABLE_ALERT_VIBRATE = "enable_alert_vibrate";

    // Speak contents of alert after playing the alert sound.
    public static final String KEY_ENABLE_ALERT_SPEECH = "enable_alert_speech";

    // Play alert sound in full volume regardless Do Not Disturb is on.
    public static final String KEY_OVERRIDE_DND = "override_dnd";

    public static final String KEY_OVERRIDE_DND_SETTINGS_CHANGED =
            "override_dnd_settings_changed";

    // Preference category for emergency alert and CMAS settings.
    public static final String KEY_CATEGORY_EMERGENCY_ALERTS = "category_emergency_alerts";

    // Preference category for alert preferences.
    public static final String KEY_CATEGORY_ALERT_PREFERENCES = "category_alert_preferences";

    // Show checkbox for Presidential alerts in settings
    // Whether to display CMAS presidential alert notifications (always enabled).
    public static final String KEY_ENABLE_CMAS_PRESIDENTIAL_ALERTS =
            "enable_cmas_presidential_alerts";

    // Whether to display CMAS extreme threat notifications (default is enabled).
    public static final String KEY_ENABLE_CMAS_EXTREME_THREAT_ALERTS =
            "enable_cmas_extreme_threat_alerts";

    // Whether to display CMAS severe threat notifications (default is enabled).
    public static final String KEY_ENABLE_CMAS_SEVERE_THREAT_ALERTS =
            "enable_cmas_severe_threat_alerts";

    // Whether to display CMAS amber alert messages (default is enabled).
    public static final String KEY_ENABLE_CMAS_AMBER_ALERTS = "enable_cmas_amber_alerts";

    // Whether to display monthly test messages (default is disabled).
    public static final String KEY_ENABLE_TEST_ALERTS = "enable_test_alerts";

    // Whether to display exercise test alerts.
    public static final String KEY_ENABLE_EXERCISE_ALERTS = "enable_exercise_alerts";

    // Whether to display operator defined test alerts
    public static final String KEY_OPERATOR_DEFINED_ALERTS = "enable_operator_defined_alerts";

    // Whether to display state/local test messages (default disabled).
    public static final String KEY_ENABLE_STATE_LOCAL_TEST_ALERTS =
            "enable_state_local_test_alerts";

    // Preference key for whether to enable area update information notifications
    // Enabled by default for phones sold in Brazil and India, otherwise this setting may be hidden.
    public static final String KEY_ENABLE_AREA_UPDATE_INFO_ALERTS =
            "enable_area_update_info_alerts";

    // Preference key for initial opt-in/opt-out dialog.
    public static final String KEY_SHOW_CMAS_OPT_OUT_DIALOG = "show_cmas_opt_out_dialog";

    // Alert reminder interval ("once" = single 2 minute reminder).
    public static final String KEY_ALERT_REMINDER_INTERVAL = "alert_reminder_interval";

    // Preference key for emergency alerts history
    public static final String KEY_EMERGENCY_ALERT_HISTORY = "emergency_alert_history";

    // For watch layout
    private static final String KEY_WATCH_ALERT_REMINDER = "watch_alert_reminder";

    // For top introduction info
    private static final String KEY_PREFS_TOP_INTRO = "alert_prefs_top_intro";

    // Whether to receive alert in second language code
    public static final String KEY_RECEIVE_CMAS_IN_SECOND_LANGUAGE =
            "receive_cmas_in_second_language";

    /* End of user preferences keys section. */

    // Resource cache per operator
    private static final Map<String, Resources> sResourcesCacheByOperator = new HashMap<>();
    private static final Object sCacheLock = new Object();

    // Intent sent from cellbroadcastreceiver to notify cellbroadcastservice that area info update
    // is disabled/enabled.
    private static final String AREA_INFO_UPDATE_ACTION =
            "com.android.cellbroadcastreceiver.action.AREA_UPDATE_INFO_ENABLED";
    private static final String AREA_INFO_UPDATE_ENABLED_EXTRA = "enable";

    /**
     * This permission is only granted to the cellbroadcast mainline module and thus can be
     * used for permission check within CBR and CBS.
     */
    private static final String CBR_MODULE_PERMISSION =
            "com.android.cellbroadcastservice.FULL_ACCESS_CELL_BROADCAST_HISTORY";

    // Key for shared preference which represents whether user has changed any preference
    private static final String ANY_PREFERENCE_CHANGED_BY_USER = "any_preference_changed_by_user";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // for backward compatibility on R devices
        if (!SdkLevel.isAtLeastS()) {
            setCustomizeContentView(R.layout.cell_broadcast_list_collapsing_no_toobar);
        }
        super.onCreate(savedInstanceState);

        // for backward compatibility on R devices
        if (!SdkLevel.isAtLeastS()) {
            ActionBar actionBar = getActionBar();
            if (actionBar != null) {
                // android.R.id.home will be triggered in onOptionsItemSelected()
                actionBar.setDisplayHomeAsUpEnabled(true);
            }
        }

        UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
        if (userManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_CELL_BROADCASTS)) {
            setContentView(R.layout.cell_broadcast_disallowed_preference_screen);
            return;
        }

        // We only add new CellBroadcastSettingsFragment if no fragment is restored.
        Fragment fragment = getFragmentManager().findFragmentById(
                com.android.settingslib.collapsingtoolbar.R.id.content_frame);
        if (fragment == null) {
            fragment = new CellBroadcastSettingsFragment();
            getFragmentManager()
                    .beginTransaction()
                    .add(com.android.settingslib.collapsingtoolbar.R.id.content_frame, fragment)
                    .commit();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        getWindow().addSystemFlags(
                android.view.WindowManager.LayoutParams
                        .SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Reset all user values for preferences (stored in shared preferences).
     *
     * @param c the application context
     */
    public static void resetAllPreferences(Context c) {
        SharedPreferences.Editor e = PreferenceManager.getDefaultSharedPreferences(c).edit();
        e.remove(KEY_ENABLE_CMAS_EXTREME_THREAT_ALERTS)
                .remove(KEY_ENABLE_CMAS_SEVERE_THREAT_ALERTS)
                .remove(KEY_ENABLE_CMAS_AMBER_ALERTS)
                .remove(KEY_ENABLE_PUBLIC_SAFETY_MESSAGES)
                .remove(KEY_ENABLE_PUBLIC_SAFETY_MESSAGES_FULL_SCREEN)
                .remove(KEY_ENABLE_EMERGENCY_ALERTS)
                .remove(KEY_ALERT_REMINDER_INTERVAL)
                .remove(KEY_ENABLE_ALERT_SPEECH)
                .remove(KEY_OVERRIDE_DND)
                .remove(KEY_ENABLE_AREA_UPDATE_INFO_ALERTS)
                .remove(KEY_ENABLE_TEST_ALERTS)
                .remove(KEY_ENABLE_STATE_LOCAL_TEST_ALERTS)
                .remove(KEY_ENABLE_ALERT_VIBRATE)
                .remove(KEY_ENABLE_CMAS_PRESIDENTIAL_ALERTS)
                .remove(KEY_RECEIVE_CMAS_IN_SECOND_LANGUAGE)
                .remove(KEY_ENABLE_EXERCISE_ALERTS)
                .remove(KEY_OPERATOR_DEFINED_ALERTS);
        // If the device is in test harness mode, reset main toggle should only happen on the
        // first boot.
        if (!ActivityManager.isRunningInUserTestHarness()) {
          Log.d(TAG, "In not test harness mode. reset main toggle.");
          e.remove(KEY_ENABLE_ALERTS_MASTER_TOGGLE);
        }
        e.commit();

        PackageManager pm = c.getPackageManager();
        if (pm.hasSystemFeature(PackageManager.FEATURE_WATCH)) {
            PreferenceManager.setDefaultValues(c, R.xml.watch_preferences, true);
        } else {
            PreferenceManager.setDefaultValues(c, R.xml.preferences, true);
        }
        setPreferenceChanged(c, false);
    }

    /**
     * Return true if user has modified any preference manually.
     * @param c the application context
     * @return
     */
    public static boolean hasAnyPreferenceChanged(Context c) {
        return PreferenceManager.getDefaultSharedPreferences(c)
                .getBoolean(ANY_PREFERENCE_CHANGED_BY_USER, false);
    }

    private static void setPreferenceChanged(Context c, boolean changed) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c);
        sp.edit().putBoolean(ANY_PREFERENCE_CHANGED_BY_USER, changed).apply();
    }

    /**
     * New fragment-style implementation of preferences.
     */
    public static class CellBroadcastSettingsFragment extends PreferenceFragment {

        private TwoStatePreference mExtremeCheckBox;
        private TwoStatePreference mSevereCheckBox;
        private TwoStatePreference mAmberCheckBox;
        private MainSwitchPreference mMasterToggle;
        private TwoStatePreference mPublicSafetyMessagesChannelCheckBox;
        private TwoStatePreference mPublicSafetyMessagesChannelFullScreenCheckBox;
        private TwoStatePreference mEmergencyAlertsCheckBox;
        private ListPreference mReminderInterval;
        private TwoStatePreference mSpeechCheckBox;
        private TwoStatePreference mOverrideDndCheckBox;
        private TwoStatePreference mAreaUpdateInfoCheckBox;
        private TwoStatePreference mTestCheckBox;
        private TwoStatePreference mExerciseTestCheckBox;
        private TwoStatePreference mOperatorDefinedCheckBox;
        private TwoStatePreference mStateLocalTestCheckBox;
        private TwoStatePreference mEnableVibrateCheckBox;
        private Preference mAlertHistory;
        private Preference mAlertsHeader;
        private PreferenceCategory mAlertCategory;
        private PreferenceCategory mAlertPreferencesCategory;
        private boolean mDisableSevereWhenExtremeDisabled = true;

        // Show checkbox for Presidential alerts in settings
        private TwoStatePreference mPresidentialCheckBox;

        // on/off switch in settings for receiving alert in second language code
        private TwoStatePreference mReceiveCmasInSecondLanguageCheckBox;

        // Show the top introduction
        private Preference mTopIntroPreference;

        private final BroadcastReceiver mTestingModeChangedReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                switch (intent.getAction()) {
                    case CellBroadcastReceiver.ACTION_TESTING_MODE_CHANGED:
                        updatePreferenceVisibility();
                        break;
                }
            }
        };

        private void initPreferences() {
            mExtremeCheckBox = (TwoStatePreference)
                    findPreference(KEY_ENABLE_CMAS_EXTREME_THREAT_ALERTS);
            mSevereCheckBox = (TwoStatePreference)
                    findPreference(KEY_ENABLE_CMAS_SEVERE_THREAT_ALERTS);
            mAmberCheckBox = (TwoStatePreference)
                    findPreference(KEY_ENABLE_CMAS_AMBER_ALERTS);
            mMasterToggle = (MainSwitchPreference)
                    findPreference(KEY_ENABLE_ALERTS_MASTER_TOGGLE);
            mPublicSafetyMessagesChannelCheckBox = (TwoStatePreference)
                    findPreference(KEY_ENABLE_PUBLIC_SAFETY_MESSAGES);
            mPublicSafetyMessagesChannelFullScreenCheckBox = (TwoStatePreference)
                    findPreference(KEY_ENABLE_PUBLIC_SAFETY_MESSAGES_FULL_SCREEN);
            mEmergencyAlertsCheckBox = (TwoStatePreference)
                    findPreference(KEY_ENABLE_EMERGENCY_ALERTS);
            mReminderInterval = (ListPreference)
                    findPreference(KEY_ALERT_REMINDER_INTERVAL);
            mSpeechCheckBox = (TwoStatePreference)
                    findPreference(KEY_ENABLE_ALERT_SPEECH);
            mOverrideDndCheckBox = (TwoStatePreference)
                    findPreference(KEY_OVERRIDE_DND);
            mAreaUpdateInfoCheckBox = (TwoStatePreference)
                    findPreference(KEY_ENABLE_AREA_UPDATE_INFO_ALERTS);
            mTestCheckBox = (TwoStatePreference)
                    findPreference(KEY_ENABLE_TEST_ALERTS);
            mExerciseTestCheckBox = (TwoStatePreference) findPreference(KEY_ENABLE_EXERCISE_ALERTS);
            mOperatorDefinedCheckBox = (TwoStatePreference)
                    findPreference(KEY_OPERATOR_DEFINED_ALERTS);
            mStateLocalTestCheckBox = (TwoStatePreference)
                    findPreference(KEY_ENABLE_STATE_LOCAL_TEST_ALERTS);
            mAlertHistory = findPreference(KEY_EMERGENCY_ALERT_HISTORY);
            mAlertsHeader = findPreference(KEY_ALERTS_HEADER);
            mReceiveCmasInSecondLanguageCheckBox = (TwoStatePreference) findPreference
                    (KEY_RECEIVE_CMAS_IN_SECOND_LANGUAGE);
            mEnableVibrateCheckBox = findPreference(KEY_ENABLE_ALERT_VIBRATE);

            // Show checkbox for Presidential alerts in settings
            mPresidentialCheckBox = (TwoStatePreference)
                    findPreference(KEY_ENABLE_CMAS_PRESIDENTIAL_ALERTS);

            PackageManager pm = getActivity().getPackageManager();
            if (!pm.hasSystemFeature(PackageManager.FEATURE_WATCH)) {
                mAlertPreferencesCategory = (PreferenceCategory)
                        findPreference(KEY_CATEGORY_ALERT_PREFERENCES);
                mAlertCategory = (PreferenceCategory)
                        findPreference(KEY_CATEGORY_EMERGENCY_ALERTS);
            }
            mTopIntroPreference = findPreference(KEY_PREFS_TOP_INTRO);
        }

        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

            LocalBroadcastManager.getInstance(getContext())
                    .registerReceiver(mTestingModeChangedReceiver, new IntentFilter(
                            CellBroadcastReceiver.ACTION_TESTING_MODE_CHANGED));

            // Load the preferences from an XML resource
            PackageManager pm = getActivity().getPackageManager();
            if (pm.hasSystemFeature(PackageManager.FEATURE_WATCH)) {
                addPreferencesFromResource(R.xml.watch_preferences);
            } else {
                addPreferencesFromResource(R.xml.preferences);
            }

            initPreferences();

            Resources res = CellBroadcastSettings.getResourcesForDefaultSubId(getContext());

            mDisableSevereWhenExtremeDisabled = res.getBoolean(
                    R.bool.disable_severe_when_extreme_disabled);

            final OnMainSwitchChangeListener mainSwitchListener = new OnMainSwitchChangeListener() {
                @Override
                public void onSwitchChanged(Switch switchView, boolean isChecked) {
                    setAlertsEnabled(isChecked);
                    onPreferenceChangedByUser(getContext());
                }
            };

            // Handler for settings that require us to reconfigure enabled channels in radio
            Preference.OnPreferenceChangeListener startConfigServiceListener =
                    new Preference.OnPreferenceChangeListener() {
                        @Override
                        public boolean onPreferenceChange(Preference pref, Object newValue) {
                            if (mDisableSevereWhenExtremeDisabled) {
                                if (pref.getKey().equals(KEY_ENABLE_CMAS_EXTREME_THREAT_ALERTS)) {
                                    boolean isExtremeAlertChecked = (Boolean) newValue;
                                    if (mSevereCheckBox != null) {
                                        mSevereCheckBox.setEnabled(isExtremeAlertChecked);
                                        mSevereCheckBox.setChecked(false);
                                    }
                                }
                            }

                            // check if area update was disabled
                            if (pref.getKey().equals(KEY_ENABLE_AREA_UPDATE_INFO_ALERTS)) {
                                boolean isEnabledAlert = (Boolean) newValue;
                                notifyAreaInfoUpdate(isEnabledAlert);
                            }

                            onPreferenceChangedByUser(getContext());
                            return true;
                        }
                    };

            initReminderIntervalList();

            if (mMasterToggle != null) {
                mMasterToggle.addOnSwitchChangeListener(mainSwitchListener);
                // If allow alerts are disabled, we turn all sub-alerts off. If it's enabled, we
                // leave them as they are.
                if (!mMasterToggle.isChecked()) {
                    setAlertsEnabled(false);
                }
            }
            // note that mPresidentialCheckBox does not use the startConfigServiceListener because
            // the user is never allowed to change the preference
            if (mAreaUpdateInfoCheckBox != null) {
                mAreaUpdateInfoCheckBox.setOnPreferenceChangeListener(startConfigServiceListener);
            }
            if (mExtremeCheckBox != null) {
                mExtremeCheckBox.setOnPreferenceChangeListener(startConfigServiceListener);
            }
            if (mPublicSafetyMessagesChannelCheckBox != null) {
                mPublicSafetyMessagesChannelCheckBox.setOnPreferenceChangeListener(
                        startConfigServiceListener);
            }
            if (mPublicSafetyMessagesChannelFullScreenCheckBox != null) {
                mPublicSafetyMessagesChannelFullScreenCheckBox.setOnPreferenceChangeListener(
                        startConfigServiceListener);
            }
            if (mEmergencyAlertsCheckBox != null) {
                mEmergencyAlertsCheckBox.setOnPreferenceChangeListener(startConfigServiceListener);
            }
            if (mSevereCheckBox != null) {
                mSevereCheckBox.setOnPreferenceChangeListener(startConfigServiceListener);
                if (mDisableSevereWhenExtremeDisabled) {
                    if (mExtremeCheckBox != null) {
                        mSevereCheckBox.setEnabled(mExtremeCheckBox.isChecked());
                    }
                }
            }
            if (mAmberCheckBox != null) {
                mAmberCheckBox.setOnPreferenceChangeListener(startConfigServiceListener);
            }
            if (mTestCheckBox != null) {
                mTestCheckBox.setOnPreferenceChangeListener(startConfigServiceListener);
            }
            if (mExerciseTestCheckBox != null) {
                mExerciseTestCheckBox.setOnPreferenceChangeListener(startConfigServiceListener);
            }
            if (mOperatorDefinedCheckBox != null) {
                mOperatorDefinedCheckBox.setOnPreferenceChangeListener(startConfigServiceListener);
            }
            if (mStateLocalTestCheckBox != null) {
                mStateLocalTestCheckBox.setOnPreferenceChangeListener(
                        startConfigServiceListener);
            }

            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());

            if (mOverrideDndCheckBox != null) {
                if (!sp.getBoolean(KEY_OVERRIDE_DND_SETTINGS_CHANGED, false)) {
                    // If the user hasn't changed this settings yet, use the default settings
                    // from resource overlay.
                    mOverrideDndCheckBox.setChecked(res.getBoolean(R.bool.override_dnd_default));
                }
                mOverrideDndCheckBox.setOnPreferenceChangeListener(
                        (pref, newValue) -> {
                            sp.edit().putBoolean(KEY_OVERRIDE_DND_SETTINGS_CHANGED,
                                    true).apply();
                            updateVibrationPreference((boolean) newValue);
                            return true;
                        });
            }

            if (mAlertHistory != null) {
                mAlertHistory.setOnPreferenceClickListener(
                        preference -> {
                            final Intent intent = new Intent(getContext(),
                                    CellBroadcastListActivity.class);
                            startActivity(intent);
                            return true;
                        });
            }

            updateVibrationPreference(sp.getBoolean(CellBroadcastSettings.KEY_OVERRIDE_DND,
                    false));
            updatePreferenceVisibility();
        }

        /**
         * Update the vibration preference based on override DND. If DND is overridden, then do
         * not allow users to turn off vibration.
         *
         * @param overrideDnd {@code true} if the alert will be played at full volume, regardless
         * DND settings.
         */
        private void updateVibrationPreference(boolean overrideDnd) {
            if (mEnableVibrateCheckBox != null) {
                if (overrideDnd) {
                    // If DND is enabled, always enable vibration.
                    mEnableVibrateCheckBox.setChecked(true);
                }
                // Grey out the preference if DND is overridden.
                mEnableVibrateCheckBox.setEnabled(!overrideDnd);
            }
        }

        /**
         * Dynamically update each preference's visibility based on configuration.
         */
        private void updatePreferenceVisibility() {
            Resources res = CellBroadcastSettings.getResourcesForDefaultSubId(getContext());

            // The settings should be based on the config by the subscription
            CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(
                    getContext(), SubscriptionManager.getDefaultSubscriptionId(), null);

            if (mMasterToggle != null) {
                mMasterToggle.setVisible(res.getBoolean(R.bool.show_main_switch_settings));
            }

            if (mPresidentialCheckBox != null) {
                mPresidentialCheckBox.setVisible(
                        res.getBoolean(R.bool.show_presidential_alerts_settings));
            }

            if (mExtremeCheckBox != null) {
                mExtremeCheckBox.setVisible(res.getBoolean(R.bool.show_extreme_alert_settings)
                        && !channelManager.getCellBroadcastChannelRanges(
                                R.array.cmas_alert_extreme_channels_range_strings).isEmpty());
            }

            if (mSevereCheckBox != null) {
                mSevereCheckBox.setVisible(res.getBoolean(R.bool.show_severe_alert_settings)
                        && !channelManager.getCellBroadcastChannelRanges(
                                R.array.cmas_alerts_severe_range_strings).isEmpty());
            }

            if (mAmberCheckBox != null) {
                mAmberCheckBox.setVisible(res.getBoolean(R.bool.show_amber_alert_settings)
                        && !channelManager.getCellBroadcastChannelRanges(
                                R.array.cmas_amber_alerts_channels_range_strings).isEmpty());
            }

            if (mPublicSafetyMessagesChannelCheckBox != null) {
                mPublicSafetyMessagesChannelCheckBox.setVisible(
                        res.getBoolean(R.bool.show_public_safety_settings)
                                && !channelManager.getCellBroadcastChannelRanges(
                                        R.array.public_safety_messages_channels_range_strings)
                                .isEmpty());
            }
            // this is the matching full screen settings for public safety toggle. shown only if
            // public safety toggle is displayed.
            if (mPublicSafetyMessagesChannelFullScreenCheckBox != null) {
                mPublicSafetyMessagesChannelFullScreenCheckBox.setVisible(
                        res.getBoolean(R.bool.show_public_safety_full_screen_settings)
                                && (mPublicSafetyMessagesChannelCheckBox != null
                                && mPublicSafetyMessagesChannelCheckBox.isVisible()));
            }

            if (mTestCheckBox != null) {
                mTestCheckBox.setVisible(isTestAlertsToggleVisible(getContext()));
            }

            if (mExerciseTestCheckBox != null) {
                boolean visible = false;
                if (res.getBoolean(R.bool.show_separate_exercise_settings)) {
                    if (res.getBoolean(R.bool.show_exercise_settings)
                            || CellBroadcastReceiver.isTestingMode(getContext())) {
                        if (!channelManager.getCellBroadcastChannelRanges(
                                R.array.exercise_alert_range_strings).isEmpty()) {
                            visible = true;
                        }
                    }
                }
                mExerciseTestCheckBox.setVisible(visible);
            }

            if (mOperatorDefinedCheckBox != null) {
                boolean visible = false;
                if (res.getBoolean(R.bool.show_separate_operator_defined_settings)) {
                    if (res.getBoolean(R.bool.show_operator_defined_settings)
                            || CellBroadcastReceiver.isTestingMode(getContext())) {
                        if (!channelManager.getCellBroadcastChannelRanges(
                                R.array.operator_defined_alert_range_strings).isEmpty()) {
                            visible = true;
                        }
                    }
                }
                mOperatorDefinedCheckBox.setVisible(visible);
            }

            if (mEmergencyAlertsCheckBox != null) {
                mEmergencyAlertsCheckBox.setVisible(!channelManager.getCellBroadcastChannelRanges(
                        R.array.emergency_alerts_channels_range_strings).isEmpty());
            }

            if (mStateLocalTestCheckBox != null) {
                mStateLocalTestCheckBox.setVisible(
                        res.getBoolean(R.bool.show_state_local_test_settings)
                                && !channelManager.getCellBroadcastChannelRanges(
                                        R.array.state_local_test_alert_range_strings).isEmpty());
            }

            if (mReceiveCmasInSecondLanguageCheckBox != null) {
                mReceiveCmasInSecondLanguageCheckBox.setVisible(!res.getString(
                        R.string.emergency_alert_second_language_code).isEmpty());
            }

            if (mAreaUpdateInfoCheckBox != null) {
                mAreaUpdateInfoCheckBox.setVisible(
                        res.getBoolean(R.bool.config_showAreaUpdateInfoSettings));
            }

            if (mOverrideDndCheckBox != null) {
                mOverrideDndCheckBox.setVisible(res.getBoolean(R.bool.show_override_dnd_settings));
            }

            if (mEnableVibrateCheckBox != null) {
                // Only show vibrate toggle when override DND toggle is available to users, or when
                // override DND default is turned off.
                // In some countries, override DND is always on, which means vibration is always on.
                // In that case, no need to show vibration toggle for users.
                Vibrator vibrator = getContext().getSystemService(Vibrator.class);
                boolean supportVibration = (vibrator != null) && vibrator.hasVibrator();
                mEnableVibrateCheckBox.setVisible(supportVibration
                        && (res.getBoolean(R.bool.show_override_dnd_settings) ||
                        !res.getBoolean(R.bool.override_dnd)));
            }
            if (mAlertsHeader != null) {
                mAlertsHeader.setVisible(
                        !getContext().getString(R.string.alerts_header_summary).isEmpty());
            }

            if (mSpeechCheckBox != null) {
                mSpeechCheckBox.setVisible(res.getBoolean(R.bool.show_alert_speech_setting)
                        || getActivity().getPackageManager()
                        .hasSystemFeature(PackageManager.FEATURE_WATCH));
            }

            if (mTopIntroPreference != null) {
                mTopIntroPreference.setTitle(getTopIntroduction());
            }
        }

        private int getTopIntroduction() {
            // Only set specific top introduction for roaming support now
            if (!CellBroadcastReceiver.getRoamingOperatorSupported(getContext()).isEmpty()) {
                return R.string.top_intro_roaming_text;
            }
            return R.string.top_intro_default_text;
        }

        private void initReminderIntervalList() {
            Resources res = CellBroadcastSettings.getResourcesForDefaultSubId(getContext());

            String[] activeValues =
                    res.getStringArray(R.array.alert_reminder_interval_active_values);
            String[] allEntries = res.getStringArray(R.array.alert_reminder_interval_entries);
            String[] newEntries = new String[activeValues.length];

            // Only add active interval to the list
            for (int i = 0; i < activeValues.length; i++) {
                int index = mReminderInterval.findIndexOfValue(activeValues[i]);
                if (index != -1) {
                    newEntries[i] = allEntries[index];
                    if (DBG) Log.d(TAG, "Added " + allEntries[index]);
                } else {
                    Log.e(TAG, "Can't find " + activeValues[i]);
                }
            }

            mReminderInterval.setEntries(newEntries);
            mReminderInterval.setEntryValues(activeValues);
            mReminderInterval.setSummary(mReminderInterval.getEntry());
            mReminderInterval.setOnPreferenceChangeListener(
                    new Preference.OnPreferenceChangeListener() {
                        @Override
                        public boolean onPreferenceChange(Preference pref, Object newValue) {
                            final ListPreference listPref = (ListPreference) pref;
                            final int idx = listPref.findIndexOfValue((String) newValue);
                            listPref.setSummary(listPref.getEntries()[idx]);
                            return true;
                        }
                    });
        }


        private void setAlertsEnabled(boolean alertsEnabled) {
            if (mSevereCheckBox != null) {
                mSevereCheckBox.setEnabled(alertsEnabled);
                mSevereCheckBox.setChecked(alertsEnabled);
            }
            if (mExtremeCheckBox != null) {
                mExtremeCheckBox.setEnabled(alertsEnabled);
                mExtremeCheckBox.setChecked(alertsEnabled);
            }
            if (mAmberCheckBox != null) {
                mAmberCheckBox.setEnabled(alertsEnabled);
                mAmberCheckBox.setChecked(alertsEnabled);
            }
            if (mAreaUpdateInfoCheckBox != null) {
                mAreaUpdateInfoCheckBox.setEnabled(alertsEnabled);
                mAreaUpdateInfoCheckBox.setChecked(alertsEnabled);
                notifyAreaInfoUpdate(alertsEnabled);
            }
            if (mEmergencyAlertsCheckBox != null) {
                mEmergencyAlertsCheckBox.setEnabled(alertsEnabled);
                mEmergencyAlertsCheckBox.setChecked(alertsEnabled);
            }
            if (mPublicSafetyMessagesChannelCheckBox != null) {
                mPublicSafetyMessagesChannelCheckBox.setEnabled(alertsEnabled);
                mPublicSafetyMessagesChannelCheckBox.setChecked(alertsEnabled);
            }
            if (mStateLocalTestCheckBox != null) {
                mStateLocalTestCheckBox.setEnabled(alertsEnabled);
                mStateLocalTestCheckBox.setChecked(alertsEnabled);
            }
            if (mTestCheckBox != null) {
                mTestCheckBox.setEnabled(alertsEnabled);
                mTestCheckBox.setChecked(alertsEnabled);
            }
            if (mExerciseTestCheckBox != null) {
                mExerciseTestCheckBox.setEnabled(alertsEnabled);
                mExerciseTestCheckBox.setChecked(alertsEnabled);
            }
            if (mOperatorDefinedCheckBox != null) {
                mOperatorDefinedCheckBox.setEnabled(alertsEnabled);
                mOperatorDefinedCheckBox.setChecked(alertsEnabled);
            }
        }

        private void notifyAreaInfoUpdate(boolean enabled) {
            Intent areaInfoIntent = new Intent(AREA_INFO_UPDATE_ACTION);
            areaInfoIntent.putExtra(AREA_INFO_UPDATE_ENABLED_EXTRA, enabled);
            // sending broadcast protected by the permission which is only
            // granted for CBR mainline module.
            getContext().sendBroadcast(areaInfoIntent, CBR_MODULE_PERMISSION);
        }


        @Override
        public void onResume() {
            super.onResume();
            updatePreferenceVisibility();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            LocalBroadcastManager.getInstance(getContext())
                    .unregisterReceiver(mTestingModeChangedReceiver);
        }

        /**
         * Callback to be called when preference or master toggle is changed by user
         *
         * @param context Context to use
         */
        public void onPreferenceChangedByUser(Context context) {
            CellBroadcastReceiver.startConfigService(context,
                    CellBroadcastConfigService.ACTION_ENABLE_CHANNELS);
            setPreferenceChanged(context, true);

            // Notify backup manager a backup pass is needed.
            new BackupManager(context).dataChanged();
        }
    }

    public static boolean isTestAlertsToggleVisible(Context context) {
        return isTestAlertsToggleVisible(context, null);
    }

    /**
     * Check whether test alert toggle is visible
     * @param context Context
     * @param operator Opeator numeric
     */
    public static boolean isTestAlertsToggleVisible(Context context, String operator) {
        CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(context,
                SubscriptionManager.getDefaultSubscriptionId(), operator);
        Resources res = operator == null ? getResourcesForDefaultSubId(context)
                : getResourcesByOperator(context,
                        SubscriptionManager.getDefaultSubscriptionId(), operator);
        boolean isTestAlertsAvailable = !channelManager.getCellBroadcastChannelRanges(
                R.array.required_monthly_test_range_strings).isEmpty()
                || (!channelManager.getCellBroadcastChannelRanges(
                R.array.exercise_alert_range_strings).isEmpty()
                /** exercise toggle is controlled under the main test toggle */
                && (!res.getBoolean(R.bool.show_separate_exercise_settings)))
                || (!channelManager.getCellBroadcastChannelRanges(
                R.array.operator_defined_alert_range_strings).isEmpty()
                /** operator defined toggle is controlled under the main test toggle */
                && (!res.getBoolean(R.bool.show_separate_operator_defined_settings)))
                || !channelManager.getCellBroadcastChannelRanges(
                R.array.etws_test_alerts_range_strings).isEmpty();

        return (res.getBoolean(R.bool.show_test_settings)
                || CellBroadcastReceiver.isTestingMode(context))
                && isTestAlertsAvailable;
    }

    /**
     * Get the device resource based on SIM
     *
     * @param context Context
     * @param subId Subscription index
     *
     * @return The resource
     */
    public static @NonNull Resources getResources(@NonNull Context context, int subId) {

        if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID
                || !SubscriptionManager.isValidSubscriptionId(subId)) {
            return context.getResources();
        }

        return SubscriptionManager.getResourcesForSubId(context, subId);
    }

    /**
     * Get the resources using the default subscription ID.
     * @param context Context
     * @return the Resources for the default subscription ID, or if there is no default subscription
     * from SubscriptionManager, the resources for the latest loaded SIM.
     */
    public static @NonNull Resources getResourcesForDefaultSubId(@NonNull Context context) {
        return getResources(context, SubscriptionManager.getDefaultSubscriptionId());
    }

    /**
     * Get the resources per network operator
     * @param context Context
     * @param operator Opeator numeric
     * @return the Resources based on network operator
     */
    public static @NonNull Resources getResourcesByOperator(
            @NonNull Context context, int subId, @NonNull String operator) {
        if (operator == null || operator.isEmpty()) {
            return getResources(context, subId);
        }

        synchronized (sCacheLock) {
            Resources res = sResourcesCacheByOperator.get(operator);
            if (res != null) {
                return res;
            }

            Configuration overrideConfig = new Configuration();
            try {
                int mcc = Integer.parseInt(operator.substring(0, 3));
                int mnc = operator.length() > 3 ? Integer.parseInt(operator.substring(3))
                        : Configuration.MNC_ZERO;

                overrideConfig.mcc = mcc;
                overrideConfig.mnc = mnc;
            } catch (NumberFormatException e) {
                // should not happen
                Log.e(TAG, "invalid operator: " + operator);
                return context.getResources();
            }

            Context newContext = context.createConfigurationContext(overrideConfig);
            res = newContext.getResources();

            sResourcesCacheByOperator.put(operator, res);
            return res;
        }
    }

    /**
     * Get the resources id which is used for the default value of the preference
     * @param key the preference key
     * @return a valid resources id if the key is valid and the default value is
     * defined, otherwise 0
     */
    public static int getResourcesIdForDefaultPrefValue(String key) {
        switch (key) {
            case KEY_ENABLE_ALERTS_MASTER_TOGGLE:
                return R.bool.master_toggle_enabled_default;
            case KEY_ENABLE_PUBLIC_SAFETY_MESSAGES:
                return R.bool.public_safety_messages_enabled_default;
            case KEY_ENABLE_PUBLIC_SAFETY_MESSAGES_FULL_SCREEN:
                return R.bool.public_safety_messages_full_screen_enabled_default;
            case KEY_ENABLE_EMERGENCY_ALERTS:
                return R.bool.emergency_alerts_enabled_default;
            case KEY_ENABLE_ALERT_SPEECH:
                return R.bool.enable_alert_speech_default;
            case KEY_OVERRIDE_DND:
                return R.bool.override_dnd_default;
            case KEY_ENABLE_CMAS_EXTREME_THREAT_ALERTS:
                return R.bool.extreme_threat_alerts_enabled_default;
            case KEY_ENABLE_CMAS_SEVERE_THREAT_ALERTS:
                return R.bool.severe_threat_alerts_enabled_default;
            case KEY_ENABLE_CMAS_AMBER_ALERTS:
                return R.bool.amber_alerts_enabled_default;
            case KEY_ENABLE_TEST_ALERTS:
                return R.bool.test_alerts_enabled_default;
            case KEY_ENABLE_EXERCISE_ALERTS:
                return R.bool.test_exercise_alerts_enabled_default;
            case KEY_OPERATOR_DEFINED_ALERTS:
                return R.bool.test_operator_defined_alerts_enabled_default;
            case KEY_ENABLE_STATE_LOCAL_TEST_ALERTS:
                return R.bool.state_local_test_alerts_enabled_default;
            case KEY_ENABLE_AREA_UPDATE_INFO_ALERTS:
                return R.bool.area_update_info_alerts_enabled_default;
            default:
                return 0;
        }
    }

    /**
     * Reset the resources cache.
     */
    @VisibleForTesting
    public static void resetResourcesCache() {
        synchronized (sCacheLock) {
            sResourcesCacheByOperator.clear();
        }
    }
}