summaryrefslogtreecommitdiff
path: root/src/com/android/cellbroadcastreceiver/CellBroadcastAlertDialog.java
blob: eef1825b92d84dfb4d7cd7e130e20571afd1386e (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
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
/*
 * Copyright (C) 2016 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 static com.android.cellbroadcastreceiver.CellBroadcastReceiver.VDBG;
import static com.android.cellbroadcastservice.CellBroadcastMetrics.ERRSRC_CBR;
import static com.android.cellbroadcastservice.CellBroadcastMetrics.ERRTYPE_ICONRESOURCE;
import static com.android.cellbroadcastservice.CellBroadcastMetrics.ERRTYPE_STATUSBAR;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.KeyguardManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.RemoteAction;
import android.app.StatusBarManager;
import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.provider.Telephony;
import android.telephony.SmsCbCmasInfo;
import android.telephony.SmsCbMessage;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.textclassifier.TextClassification;
import android.view.textclassifier.TextClassification.Request;
import android.view.textclassifier.TextClassifier;
import android.view.textclassifier.TextLinks;
import android.view.textclassifier.TextLinks.TextLink;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.cellbroadcastreceiver.CellBroadcastChannelManager.CellBroadcastChannelRange;
import com.android.internal.annotations.VisibleForTesting;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Custom alert dialog with optional flashing warning icon.
 * Alert audio and text-to-speech handled by {@link CellBroadcastAlertAudio}.
 */
public class CellBroadcastAlertDialog extends Activity {

    private static final String TAG = "CellBroadcastAlertDialog";

    /** Intent extra indicate this intent should not dismiss the notification */
    @VisibleForTesting
    public static final String DISMISS_NOTIFICATION_EXTRA = "dismiss_notification";

    // Intent extra to identify if notification was sent while trying to move away from the dialog
    //  without acknowledging the dialog
    static final String FROM_SAVE_STATE_NOTIFICATION_EXTRA = "from_save_state_notification";

    /** Not link any text. */
    private static final int LINK_METHOD_NONE = 0;

    private static final String LINK_METHOD_NONE_STRING = "none";

    /** Use {@link android.text.util.Linkify} to generate links. */
    private static final int LINK_METHOD_LEGACY_LINKIFY = 1;

    private static final String LINK_METHOD_LEGACY_LINKIFY_STRING = "legacy_linkify";

    /**
     * Use the machine learning based {@link TextClassifier} to generate links. Will fallback to
     * {@link #LINK_METHOD_LEGACY_LINKIFY} if not enabled.
     */
    private static final int LINK_METHOD_SMART_LINKIFY = 2;

    private static final String LINK_METHOD_SMART_LINKIFY_STRING = "smart_linkify";

    /**
     * Use the machine learning based {@link TextClassifier} to generate links but hiding copy
     * option. Will fallback to
     * {@link #LINK_METHOD_LEGACY_LINKIFY} if not enabled.
     */
    private static final int LINK_METHOD_SMART_LINKIFY_NO_COPY = 3;

    private static final String LINK_METHOD_SMART_LINKIFY_NO_COPY_STRING = "smart_linkify_no_copy";


    /**
     * Text link method
     * @hide
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(prefix = "LINK_METHOD_",
            value = {LINK_METHOD_NONE, LINK_METHOD_LEGACY_LINKIFY,
                    LINK_METHOD_SMART_LINKIFY, LINK_METHOD_SMART_LINKIFY_NO_COPY})
    private @interface LinkMethod {}


    /** List of cell broadcast messages to display (oldest to newest). */
    protected ArrayList<SmsCbMessage> mMessageList;

    /** Whether a CMAS alert other than Presidential Alert was displayed. */
    private boolean mShowOptOutDialog;

    /** Length of time for the warning icon to be visible. */
    private static final int WARNING_ICON_ON_DURATION_MSEC = 800;

    /** Length of time for the warning icon to be off. */
    private static final int WARNING_ICON_OFF_DURATION_MSEC = 800;

    /** Default interval for the highlight color of the pulsation. */
    private static final int PULSATION_ON_DURATION_MSEC = 1000;
    /** Default interval for the normal color of the pulsation. */
    private static final int PULSATION_OFF_DURATION_MSEC = 1000;
    /** Max value for the interval of the color change. */
    private static final int PULSATION_MAX_ON_OFF_DURATION_MSEC = 120000;
    /** Default time for the pulsation */
    private static final int PULSATION_DURATION_MSEC = 10000;
    /** Max time for the pulsation */
    private static final int PULSATION_MAX_DURATION_MSEC = 86400000;

    /** Length of time to keep the screen turned on. */
    private static final int KEEP_SCREEN_ON_DURATION_MSEC = 60000;

    /** Animation handler for the flashing warning icon (emergency alerts only). */
    @VisibleForTesting
    public AnimationHandler mAnimationHandler = new AnimationHandler();

    /** Handler to add and remove screen on flags for emergency alerts. */
    private final ScreenOffHandler mScreenOffHandler = new ScreenOffHandler();

    /** Pulsation handler for the alert background color. */
    @VisibleForTesting
    public PulsationHandler mPulsationHandler = new PulsationHandler();

    // Show the opt-out dialog
    private AlertDialog mOptOutDialog;

    /** BroadcastReceiver for screen off events. When screen was off, remove FLAG_TURN_SCREEN_ON to
     * start from a clean state. Otherwise, the window flags from the first alert will be
     * automatically applied to the following alerts handled at onNewIntent.
     */
    private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent){
            Log.d(TAG, "onSreenOff: remove FLAG_TURN_SCREEN_ON flag");
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        }
    };

    /**
     * Animation handler for the flashing warning icon (emergency alerts only).
     */
    @VisibleForTesting
    public class AnimationHandler extends Handler {
        /** Latest {@code message.what} value for detecting old messages. */
        @VisibleForTesting
        public final AtomicInteger mCount = new AtomicInteger();

        /** Warning icon state: visible == true, hidden == false. */
        @VisibleForTesting
        public boolean mWarningIconVisible;

        /** The warning icon Drawable. */
        private Drawable mWarningIcon;

        /** The View containing the warning icon. */
        private ImageView mWarningIconView;

        /** Package local constructor (called from outer class). */
        AnimationHandler() {}

        /** Start the warning icon animation. */
        @VisibleForTesting
        public void startIconAnimation(int subId) {
            if (!initDrawableAndImageView(subId)) {
                return;     // init failure
            }
            mWarningIconVisible = true;
            mWarningIconView.setVisibility(View.VISIBLE);
            updateIconState();
            queueAnimateMessage();
        }

        /** Stop the warning icon animation. */
        @VisibleForTesting
        public void stopIconAnimation() {
            // Increment the counter so the handler will ignore the next message.
            mCount.incrementAndGet();
        }

        /** Update the visibility of the warning icon. */
        private void updateIconState() {
            mWarningIconView.setImageAlpha(mWarningIconVisible ? 255 : 0);
            mWarningIconView.invalidateDrawable(mWarningIcon);
        }

        /** Queue a message to animate the warning icon. */
        private void queueAnimateMessage() {
            int msgWhat = mCount.incrementAndGet();
            sendEmptyMessageDelayed(msgWhat, mWarningIconVisible ? WARNING_ICON_ON_DURATION_MSEC
                    : WARNING_ICON_OFF_DURATION_MSEC);
        }

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == mCount.get()) {
                mWarningIconVisible = !mWarningIconVisible;
                updateIconState();
                queueAnimateMessage();
            }
        }

        /**
         * Initialize the Drawable and ImageView fields.
         *
         * @param subId Subscription index
         *
         * @return true if successful; false if any field failed to initialize
         */
        private boolean initDrawableAndImageView(int subId) {
            if (mWarningIcon == null) {
                try {
                    mWarningIcon = CellBroadcastSettings.getResourcesByOperator(
                            getApplicationContext(), subId,
                            CellBroadcastReceiver
                                    .getRoamingOperatorSupported(getApplicationContext()))
                            .getDrawable(R.drawable.ic_warning_googred);
                } catch (Resources.NotFoundException e) {
                    CellBroadcastReceiverMetrics.getInstance().logModuleError(
                            ERRSRC_CBR, ERRTYPE_ICONRESOURCE);
                    Log.e(TAG, "warning icon resource not found", e);
                    return false;
                }
            }
            if (mWarningIconView == null) {
                mWarningIconView = (ImageView) findViewById(R.id.icon);
                if (mWarningIconView != null) {
                    mWarningIconView.setImageDrawable(mWarningIcon);
                } else {
                    Log.e(TAG, "failed to get ImageView for warning icon");
                    return false;
                }
            }
            return true;
        }
    }

    /**
     * Handler to add {@code FLAG_KEEP_SCREEN_ON} for emergency alerts. After a short delay,
     * remove the flag so the screen can turn off to conserve the battery.
     */
    private class ScreenOffHandler extends Handler {
        /** Latest {@code message.what} value for detecting old messages. */
        private final AtomicInteger mCount = new AtomicInteger();

        /** Package local constructor (called from outer class). */
        ScreenOffHandler() {}

        /** Add screen on window flags and queue a delayed message to remove them later. */
        void startScreenOnTimer(@NonNull SmsCbMessage message) {
            // if screenOnDuration in milliseconds. if set to 0, do not turn screen on.
            int screenOnDuration = KEEP_SCREEN_ON_DURATION_MSEC;
            CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(
                    getApplicationContext(), message.getSubscriptionId());
            CellBroadcastChannelRange range = channelManager
                    .getCellBroadcastChannelRangeFromMessage(message);
            if (range!= null) {
                screenOnDuration = range.mScreenOnDuration;
            }
            if (screenOnDuration == 0) {
                Log.d(TAG, "screenOnDuration set to 0, do not turn screen on");
                return;
            }
            addWindowFlags();
            int msgWhat = mCount.incrementAndGet();
            removeMessages(msgWhat - 1);    // Remove previous message, if any.
            sendEmptyMessageDelayed(msgWhat, screenOnDuration);
            Log.d(TAG, "added FLAG_KEEP_SCREEN_ON, queued screen off message id " + msgWhat);
        }

        /** Remove the screen on window flags and any queued screen off message. */
        void stopScreenOnTimer() {
            removeMessages(mCount.get());
            clearWindowFlags();
        }

        /** Set the screen on window flags. */
        private void addWindowFlags() {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }

        /**
         * Clear the keep screen on window flags in order for powersaving but keep TURN_ON_SCREEN_ON
         * to make sure next wake up still turn screen on without unintended onStop triggered at
         * the beginning.
         */
        private void clearWindowFlags() {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }

        @Override
        public void handleMessage(Message msg) {
            int msgWhat = msg.what;
            if (msgWhat == mCount.get()) {
                clearWindowFlags();
                Log.d(TAG, "removed FLAG_KEEP_SCREEN_ON with id " + msgWhat);
            } else {
                Log.e(TAG, "discarding screen off message with id " + msgWhat);
            }
        }
    }

    /**
     * Pulsation handler for the alert window background color.
     */
    @VisibleForTesting
    public static class PulsationHandler extends Handler {
        /** Latest {@code message.what} value for detecting old messages. */
        @VisibleForTesting
        public final AtomicInteger mCount = new AtomicInteger();

        @VisibleForTesting
        public int mBackgroundColor = Color.TRANSPARENT;
        @VisibleForTesting
        public int mHighlightColor = Color.TRANSPARENT;
        @VisibleForTesting
        public int mOnInterval;
        @VisibleForTesting
        public int mOffInterval;
        @VisibleForTesting
        public int mDuration;
        @VisibleForTesting
        public boolean mIsPulsationOn;
        @VisibleForTesting
        public View mLayout;

        /** Package local constructor (called from outer class). */
        PulsationHandler() {
        }

        /** Start the pulsation. */
        @VisibleForTesting
        public void start(View layout, int[] pattern) {
            if (layout == null || pattern == null || pattern.length == 0) {
                Log.d(TAG, layout == null ? "layout is null" : "no pulsation pattern");
                return;
            }

            post(() -> {
                mLayout = layout;
                Drawable bg = mLayout.getBackground();
                if (bg instanceof ColorDrawable) {
                    mBackgroundColor = ((ColorDrawable) bg).getColor();
                }

                mHighlightColor = pattern[0];
                mDuration = PULSATION_DURATION_MSEC;
                if (pattern.length > 1) {
                    if (pattern[1] < 0 || pattern[1] > PULSATION_MAX_DURATION_MSEC) {
                        Log.wtf(TAG, "Invalid pulsation duration: " + pattern[1]);
                    } else {
                        mDuration = pattern[1];
                    }
                }

                mOnInterval = PULSATION_ON_DURATION_MSEC;
                if (pattern.length > 2) {
                    if (pattern[2] < 0 || pattern[2] > PULSATION_MAX_ON_OFF_DURATION_MSEC) {
                        Log.wtf(TAG, "Invalid pulsation on interval: " + pattern[2]);
                    } else {
                        mOnInterval = pattern[2];
                    }
                }

                mOffInterval = PULSATION_OFF_DURATION_MSEC;
                if (pattern.length > 3) {
                    if (pattern[3] < 0 || pattern[3] > PULSATION_MAX_ON_OFF_DURATION_MSEC) {
                        Log.wtf(TAG, "Invalid pulsation off interval: " + pattern[3]);
                    } else {
                        mOffInterval = pattern[3];
                    }
                }

                if (VDBG) {
                    Log.d(TAG, "start pulsation, highlight color=" + mHighlightColor
                            + ", background color=" + mBackgroundColor
                            + ", duration=" + mDuration
                            + ", on=" + mOnInterval + ", off=" + mOffInterval);
                }

                mCount.set(0);
                queuePulsationMessage();
                postDelayed(() -> onPulsationStopped(), mDuration);
            });
        }

        /** Stop the pulsation. */
        @VisibleForTesting
        public void stop() {
            post(() -> onPulsationStopped());
        }

        private void onPulsationStopped() {
            // Increment the counter so the handler will ignore the next message.
            mCount.incrementAndGet();
            if (mLayout != null) {
                mLayout.setBackgroundColor(mBackgroundColor);
            }
            mLayout = null;
            mIsPulsationOn = false;
            if (VDBG) {
                Log.d(TAG, "pulsation stopped");
            }
        }

        /** Queue a message to pulsate the background color of the alert. */
        private void queuePulsationMessage() {
            int msgWhat = mCount.incrementAndGet();
            sendEmptyMessageDelayed(msgWhat, mIsPulsationOn ? mOnInterval : mOffInterval);
        }

        @Override
        public void handleMessage(Message msg) {
            if (mLayout == null) {
                return;
            }

            if (msg.what == mCount.get()) {
                mIsPulsationOn = !mIsPulsationOn;
                mLayout.setBackgroundColor(mIsPulsationOn ? mHighlightColor
                        : mBackgroundColor);
                queuePulsationMessage();
            }
        }
    }

    Comparator<SmsCbMessage> mPriorityBasedComparator = (Comparator) (o1, o2) -> {
        boolean isPresidentialAlert1 =
                ((SmsCbMessage) o1).isCmasMessage()
                        && ((SmsCbMessage) o1).getCmasWarningInfo()
                        .getMessageClass() == SmsCbCmasInfo
                        .CMAS_CLASS_PRESIDENTIAL_LEVEL_ALERT;
        boolean isPresidentialAlert2 =
                ((SmsCbMessage) o2).isCmasMessage()
                        && ((SmsCbMessage) o2).getCmasWarningInfo()
                        .getMessageClass() == SmsCbCmasInfo
                        .CMAS_CLASS_PRESIDENTIAL_LEVEL_ALERT;
        if (isPresidentialAlert1 ^ isPresidentialAlert2) {
            return isPresidentialAlert1 ? 1 : -1;
        }
        Long time1 = new Long(((SmsCbMessage) o1).getReceivedTime());
        Long time2 = new Long(((SmsCbMessage) o2).getReceivedTime());
        return time2.compareTo(time1);
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // if this is only to dismiss any pending alert dialog
        if (getIntent().getBooleanExtra(CellBroadcastAlertService.DISMISS_DIALOG, false)) {
            dismissAllFromNotification(getIntent());
            return;
        }

        final Window win = getWindow();

        // We use a custom title, so remove the standard dialog title bar
        win.requestFeature(Window.FEATURE_NO_TITLE);

        // Full screen alerts display above the keyguard and when device is locked.
        win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

        // Disable home button when alert dialog is showing if mute_by_physical_button is false.
        if (!CellBroadcastSettings.getResourcesForDefaultSubId(getApplicationContext())
                .getBoolean(R.bool.mute_by_physical_button) && !CellBroadcastSettings
                .getResourcesForDefaultSubId(getApplicationContext())
                .getBoolean(R.bool.disable_status_bar)) {
            final View decorView = win.getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }

        // Initialize the view.
        LayoutInflater inflater = LayoutInflater.from(this);
        setContentView(inflater.inflate(R.layout.cell_broadcast_alert, null));

        findViewById(R.id.dismissButton).setOnClickListener(v -> dismiss());

        // Get message list from saved Bundle or from Intent.
        if (savedInstanceState != null) {
            Log.d(TAG, "onCreate getting message list from saved instance state");
            mMessageList = savedInstanceState.getParcelableArrayList(
                    CellBroadcastAlertService.SMS_CB_MESSAGE_EXTRA);
        } else {
            Log.d(TAG, "onCreate getting message list from intent");
            Intent intent = getIntent();
            mMessageList = intent.getParcelableArrayListExtra(
                    CellBroadcastAlertService.SMS_CB_MESSAGE_EXTRA);

            // If we were started from a notification, dismiss it.
            clearNotification(intent);
        }

        registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));

        if (mMessageList == null || mMessageList.size() == 0) {
            Log.e(TAG, "onCreate failed as message list is null or empty");
            finish();
        } else {
            Log.d(TAG, "onCreate loaded message list of size " + mMessageList.size());

            // For emergency alerts, keep screen on so the user can read it
            SmsCbMessage message = getLatestMessage();

            if (message == null) {
                Log.e(TAG, "message is null");
                finish();
                return;
            }

            CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(
                    this, message.getSubscriptionId());
            if (channelManager.isEmergencyMessage(message)) {
                Log.d(TAG, "onCreate setting screen on timer for emergency alert for sub "
                        + message.getSubscriptionId());
                mScreenOffHandler.startScreenOnTimer(message);
            }

            setFinishAlertOnTouchOutside();

            updateAlertText(message);

            Resources res = CellBroadcastSettings.getResourcesByOperator(getApplicationContext(),
                    message.getSubscriptionId(),
                    CellBroadcastReceiver.getRoamingOperatorSupported(getApplicationContext()));
            if (res.getBoolean(R.bool.enable_text_copy)) {
                TextView textView = findViewById(R.id.message);
                if (textView != null) {
                    textView.setOnLongClickListener(v -> copyMessageToClipboard(message,
                            getApplicationContext()));
                }
            }

            if (res.getBoolean(R.bool.disable_capture_alert_dialog)) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
            }
            startPulsatingAsNeeded(channelManager
                    .getCellBroadcastChannelRangeFromMessage(message));
        }
    }

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

    /**
     * Start animating warning icon.
     */
    @Override
    @VisibleForTesting
    public void onResume() {
        super.onResume();
        setWindowBottom();
        setMaxHeightScrollView();
        SmsCbMessage message = getLatestMessage();
        if (message != null) {
            int subId = message.getSubscriptionId();
            CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(this,
                    subId);
            CellBroadcastChannelRange range = channelManager
                    .getCellBroadcastChannelRangeFromMessage(message);
            if (channelManager.isEmergencyMessage(message)
                    && (range!= null && range.mDisplayIcon)) {
                mAnimationHandler.startIconAnimation(subId);
            }
        }
        // Some LATAM carriers mandate to disable navigation bars, quick settings etc when alert
        // dialog is showing. This is to make sure users to ack the alert before switching to
        // other activities.
        setStatusBarDisabledIfNeeded(true);
    }

    /**
     * Stop animating warning icon.
     */
    @Override
    @VisibleForTesting
    public void onPause() {
        Log.d(TAG, "onPause called");
        mAnimationHandler.stopIconAnimation();
        setStatusBarDisabledIfNeeded(false);
        super.onPause();
    }

    @Override
    protected void onUserLeaveHint() {
        Log.d(TAG, "onUserLeaveHint called");
        // When the activity goes in background (eg. clicking Home button, dismissed by outside
        // touch if enabled), send notification.
        // Avoid doing this when activity will be recreated because of orientation change or if
        // screen goes off
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        ArrayList<SmsCbMessage> messageList = getNewMessageListIfNeeded(mMessageList,
                CellBroadcastReceiverApp.getNewMessageList());
        SmsCbMessage latestMessage = (messageList == null || (messageList.size() < 1)) ? null
                : messageList.get(messageList.size() - 1);

        if (!(isChangingConfigurations() || latestMessage == null) && pm.isScreenOn()) {
            Log.d(TAG, "call addToNotificationBar when activity goes in background");
            CellBroadcastAlertService.addToNotificationBar(latestMessage, messageList,
                    getApplicationContext(), true, true, false);
        }
        super.onUserLeaveHint();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if (hasFocus) {
            Configuration config = getResources().getConfiguration();
            setPictogramAreaLayout(config.orientation);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setPictogramAreaLayout(newConfig.orientation);
    }

    private void setWindowBottom() {
        // some OEMs require that the alert window is moved to the bottom of the screen to avoid
        // blocking other screen content
        if (getResources().getBoolean(R.bool.alert_dialog_bottom)) {
            Window window = getWindow();
            WindowManager.LayoutParams params = window.getAttributes();
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.gravity = params.gravity | Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
            params.verticalMargin = 0;
            window.setAttributes(params);
        }
    }

    /** Returns the currently displayed message. */
    SmsCbMessage getLatestMessage() {
        int index = mMessageList.size() - 1;
        if (index >= 0) {
            return mMessageList.get(index);
        } else {
            Log.d(TAG, "getLatestMessage returns null");
            return null;
        }
    }

    /** Removes and returns the currently displayed message. */
    private SmsCbMessage removeLatestMessage() {
        int index = mMessageList.size() - 1;
        if (index >= 0) {
            return mMessageList.remove(index);
        } else {
            return null;
        }
    }

    /**
     * Save the list of messages so the state can be restored later.
     * @param outState Bundle in which to place the saved state.
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(
                CellBroadcastAlertService.SMS_CB_MESSAGE_EXTRA, mMessageList);
    }

    /**
     * Get link method
     *
     * @param subId Subscription index
     * @return The link method
     */
    private @LinkMethod int getLinkMethod(int subId) {
        Resources res = CellBroadcastSettings.getResourcesByOperator(getApplicationContext(),
                subId, CellBroadcastReceiver.getRoamingOperatorSupported(getApplicationContext()));
        switch (res.getString(R.string.link_method)) {
            case LINK_METHOD_NONE_STRING: return LINK_METHOD_NONE;
            case LINK_METHOD_LEGACY_LINKIFY_STRING: return LINK_METHOD_LEGACY_LINKIFY;
            case LINK_METHOD_SMART_LINKIFY_STRING: return LINK_METHOD_SMART_LINKIFY;
            case LINK_METHOD_SMART_LINKIFY_NO_COPY_STRING: return LINK_METHOD_SMART_LINKIFY_NO_COPY;
        }
        return LINK_METHOD_NONE;
    }

    /**
     * Add URL links to the applicable texts.
     *
     * @param textView Text view
     * @param messageText The text string of the message
     * @param linkMethod Link method
     */
    private void addLinks(@NonNull TextView textView, @NonNull String messageText,
            @LinkMethod int linkMethod) {
        if (linkMethod == LINK_METHOD_LEGACY_LINKIFY) {
            Spannable text = new SpannableString(messageText);
            Linkify.addLinks(text, Linkify.ALL);
            textView.setMovementMethod(LinkMovementMethod.getInstance());
            textView.setText(text);
        } else if (linkMethod == LINK_METHOD_SMART_LINKIFY
                || linkMethod == LINK_METHOD_SMART_LINKIFY_NO_COPY) {
            // Text classification cannot be run in the main thread.
            new Thread(() -> {
                final TextClassifier classifier = textView.getTextClassifier();

                TextClassifier.EntityConfig entityConfig =
                        new TextClassifier.EntityConfig.Builder()
                                .setIncludedTypes(Arrays.asList(
                                        TextClassifier.TYPE_URL,
                                        TextClassifier.TYPE_EMAIL,
                                        TextClassifier.TYPE_PHONE,
                                        TextClassifier.TYPE_ADDRESS,
                                        TextClassifier.TYPE_FLIGHT_NUMBER))
                                .setExcludedTypes(Arrays.asList(
                                        TextClassifier.TYPE_DATE,
                                        TextClassifier.TYPE_DATE_TIME))
                                .build();

                TextLinks.Request request = new TextLinks.Request.Builder(messageText)
                        .setEntityConfig(entityConfig)
                        .build();
                Spannable text;
                if (linkMethod == LINK_METHOD_SMART_LINKIFY) {
                    text = new SpannableString(messageText);
                    // Add links to the spannable text.
                    classifier.generateLinks(request).apply(
                            text, TextLinks.APPLY_STRATEGY_REPLACE, null);
                } else {
                    TextLinks textLinks = classifier.generateLinks(request);
                    // Add links to the spannable text.
                    text = applyTextLinksToSpannable(messageText, textLinks, classifier);
                }
                // UI can be only updated in the main thread.
                runOnUiThread(() -> {
                    textView.setMovementMethod(LinkMovementMethod.getInstance());
                    textView.setText(text);
                });
            }).start();
        }
    }

    private Spannable applyTextLinksToSpannable(String text, TextLinks textLinks,
            TextClassifier textClassifier) {
        Spannable result = new SpannableString(text);
        for (TextLink link : textLinks.getLinks()) {
            TextClassification textClassification = textClassifier.classifyText(
                    new Request.Builder(
                            text,
                            link.getStart(),
                            link.getEnd())
                            .build());
            if (textClassification.getActions().isEmpty()) {
                continue;
            }
            RemoteAction remoteAction = textClassification.getActions().get(0);
            result.setSpan(new RemoteActionSpan(remoteAction), link.getStart(), link.getEnd(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return result;
    }

    private static class RemoteActionSpan extends ClickableSpan {
        private final RemoteAction mRemoteAction;
        private RemoteActionSpan(RemoteAction remoteAction) {
            mRemoteAction = remoteAction;
        }
        @Override
        public void onClick(@NonNull View view) {
            try {
                mRemoteAction.getActionIntent().send();
            } catch (PendingIntent.CanceledException e) {
                Log.e(TAG, "Failed to start the pendingintent.");
            }
        }
    }

    /**
     * Update alert text when a new emergency alert arrives.
     * @param message CB message which is used to update alert text.
     */
    private void updateAlertText(@NonNull SmsCbMessage message) {
        if (message == null) {
            return;
        }
        Context context = getApplicationContext();
        int titleId = CellBroadcastResources.getDialogTitleResource(context, message);

        Resources res = CellBroadcastSettings.getResourcesByOperator(context,
                message.getSubscriptionId(),
                CellBroadcastReceiver.getRoamingOperatorSupported(context));

        CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(
                this, message.getSubscriptionId());
        CellBroadcastChannelRange range = channelManager
                .getCellBroadcastChannelRangeFromMessage(message);
        String languageCode;
        if (range != null && !TextUtils.isEmpty(range.mLanguageCode)) {
            languageCode = range.mLanguageCode;
        } else {
            languageCode = message.getLanguageCode();
        }

        if (res.getBoolean(R.bool.show_alert_title)) {
            String title = CellBroadcastResources.overrideTranslation(context, titleId, res,
                    languageCode);
            TextView titleTextView = findViewById(R.id.alertTitle);

            if (titleTextView != null) {
                String timeFormat = res.getString(R.string.date_time_format);
                if (!TextUtils.isEmpty(timeFormat)) {
                    titleTextView.setSingleLine(false);
                    title += "\n" + new SimpleDateFormat(timeFormat).format(
                            message.getReceivedTime());
                }
                setTitle(title);
                titleTextView.setText(title);
            }
        } else {
            TextView titleTextView = findViewById(R.id.alertTitle);
            setTitle("");
            titleTextView.setText("");
        }

        TextView textView = findViewById(R.id.message);
        String messageText = message.getMessageBody();
        if (textView != null && messageText != null) {
            int linkMethod = getLinkMethod(message.getSubscriptionId());
            if (linkMethod != LINK_METHOD_NONE) {
                addLinks(textView, messageText, linkMethod);
            } else {
                // Do not add any link to the message text.
                textView.setText(messageText);
            }
        }

        String dismissButtonText = getString(R.string.button_dismiss);

        if (mMessageList.size() > 1) {
            dismissButtonText += "  (1/" + mMessageList.size() + ")";
        }

        ((TextView) findViewById(R.id.dismissButton)).setText(dismissButtonText);

        setPictogram(context, message);

        if (this.hasWindowFocus()) {
            Configuration config = res.getConfiguration();
            setPictogramAreaLayout(config.orientation);
        }
    }

    /**
     * Set pictogram image
     * @param context
     * @param message
     */
    private void setPictogram(Context context, SmsCbMessage message) {
        int resId = CellBroadcastResources.getDialogPictogramResource(context, message);
        ImageView image = findViewById(R.id.pictogramImage);
        // not all layouts may have a pictogram image, e.g. watch
        if (image == null) {
            return;
        }
        if (resId != -1) {
            image.setImageResource(resId);
            image.setVisibility(View.VISIBLE);
        } else {
            image.setVisibility(View.GONE);
        }
    }

    /**
     * Set pictogram to match orientation
     *
     * @param orientation The orientation of the pictogram.
     */
    private void setPictogramAreaLayout(int orientation) {
        ImageView image = findViewById(R.id.pictogramImage);
        // not all layouts may have a pictogram image, e.g. watch
        if (image == null) {
            return;
        }
        if (image.getVisibility() == View.VISIBLE) {
            ViewGroup.LayoutParams params = image.getLayoutParams();

            if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                Display display = getWindowManager().getDefaultDisplay();
                Point point = new Point();
                display.getSize(point);
                params.width = (int) (point.x * 0.3);
                params.height = (int) (point.y * 0.3);
            } else {
                params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            }

            image.setLayoutParams(params);
        }
    }

    private void setMaxHeightScrollView() {
        int contentPanelMaxHeight = getResources().getDimensionPixelSize(
                R.dimen.alert_dialog_maxheight_content_panel);
        if (contentPanelMaxHeight > 0) {
            CustomHeightScrollView scrollView = (CustomHeightScrollView) findViewById(
                    R.id.scrollView);
            if (scrollView != null) {
                scrollView.setMaximumHeight(contentPanelMaxHeight);
            }
        }
    }

    private void startPulsatingAsNeeded(CellBroadcastChannelRange range) {
        mPulsationHandler.stop();
        if (VDBG) {
            Log.d(TAG, "start pulsation as needed for range:" + range);
        }
        if (range != null) {
            mPulsationHandler.start(findViewById(R.id.parentPanel), range.mPulsationPattern);
        }
    }

    /**
     * Called by {@link CellBroadcastAlertService} to add a new alert to the stack.
     * @param intent The new intent containing one or more {@link SmsCbMessage}.
     */
    @Override
    @VisibleForTesting
    public void onNewIntent(Intent intent) {
        if (intent.getBooleanExtra(CellBroadcastAlertService.DISMISS_DIALOG, false)) {
            dismissAllFromNotification(intent);
            return;
        }
        ArrayList<SmsCbMessage> newMessageList = intent.getParcelableArrayListExtra(
                CellBroadcastAlertService.SMS_CB_MESSAGE_EXTRA);
        if (newMessageList != null) {
            if (intent.getBooleanExtra(FROM_SAVE_STATE_NOTIFICATION_EXTRA, false)) {
                mMessageList = newMessageList;
            } else {
                // remove the duplicate messages
                for (SmsCbMessage message : newMessageList) {
                    mMessageList.removeIf(
                            msg -> msg.getReceivedTime() == message.getReceivedTime());
                }
                mMessageList.addAll(newMessageList);
                if (CellBroadcastSettings.getResourcesForDefaultSubId(getApplicationContext())
                        .getBoolean(R.bool.show_cmas_messages_in_priority_order)) {
                    // Sort message list to show messages in a different order than received by
                    // prioritizing them. Presidential Alert only has top priority.
                    Collections.sort(mMessageList, mPriorityBasedComparator);
                }
            }
            Log.d(TAG, "onNewIntent called with message list of size " + newMessageList.size());

            // For emergency alerts, keep screen on so the user can read it
            SmsCbMessage message = getLatestMessage();
            if (message != null) {
                CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(
                        this, message.getSubscriptionId());
                if (channelManager.isEmergencyMessage(message)) {
                    Log.d(TAG, "onCreate setting screen on timer for emergency alert for sub "
                            + message.getSubscriptionId());
                    mScreenOffHandler.startScreenOnTimer(message);
                }
                startPulsatingAsNeeded(channelManager
                        .getCellBroadcastChannelRangeFromMessage(message));
            }

            hideOptOutDialog(); // Hide opt-out dialog when new alert coming
            setFinishAlertOnTouchOutside();
            updateAlertText(getLatestMessage());
            // If the new intent was sent from a notification, dismiss it.
            clearNotification(intent);
        } else {
            Log.e(TAG, "onNewIntent called without SMS_CB_MESSAGE_EXTRA, ignoring");
        }
    }

    /**
     * Try to cancel any notification that may have started this activity.
     * @param intent Intent containing extras used to identify if notification needs to be cleared
     */
    private void clearNotification(Intent intent) {
        if (intent.getBooleanExtra(DISMISS_NOTIFICATION_EXTRA, false)) {
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(CellBroadcastAlertService.NOTIFICATION_ID);

            // Clear new message list when user swipe the notification
            // except dialog and notification are visible at the same time.
            if (intent.getBooleanExtra(CellBroadcastAlertService.DISMISS_DIALOG, false)) {
                CellBroadcastReceiverApp.clearNewMessageList();
            }
        }
    }

    /**
     * This will be called when users swipe away the notification, this will
     * 1. dismiss all foreground dialog, stop animating warning icon and stop the
     * {@link CellBroadcastAlertAudio} service.
     * 2. Does not mark message read.
     */
    public void dismissAllFromNotification(Intent intent) {
        Log.d(TAG, "dismissAllFromNotification");
        // Stop playing alert sound/vibration/speech (if started)
        stopService(new Intent(this, CellBroadcastAlertAudio.class));
        // Cancel any pending alert reminder
        CellBroadcastAlertReminder.cancelAlertReminder();
        // Remove the all current showing alert message from the list.
        if (mMessageList != null) {
            mMessageList.clear();
        }
        // clear notifications.
        clearNotification(intent);
        // Remove pending screen-off messages (animation messages are removed in onPause()).
        mScreenOffHandler.stopScreenOnTimer();
        finish();
    }

    /**
     * Stop animating warning icon and stop the {@link CellBroadcastAlertAudio}
     * service if necessary.
     */
    @VisibleForTesting
    public void dismiss() {
        Log.d(TAG, "dismiss");
        // Stop playing alert sound/vibration/speech (if started)
        stopService(new Intent(this, CellBroadcastAlertAudio.class));

        mPulsationHandler.stop();

        // Cancel any pending alert reminder
        CellBroadcastAlertReminder.cancelAlertReminder();

        // Remove the current alert message from the list.
        SmsCbMessage lastMessage = removeLatestMessage();
        if (lastMessage == null) {
            Log.e(TAG, "dismiss() called with empty message list!");
            finish();
            return;
        }

        // Remove the read message from the notification bar.
        // e.g, read the message from emergency alert history, need to update the notification bar.
        removeReadMessageFromNotificationBar(lastMessage, getApplicationContext());

        // Mark the alert as read.
        final long deliveryTime = lastMessage.getReceivedTime();

        // Mark broadcast as read on a background thread.
        new CellBroadcastContentProvider.AsyncCellBroadcastTask(getContentResolver())
                .execute((CellBroadcastContentProvider.CellBroadcastOperation) provider
                        -> provider.markBroadcastRead(Telephony.CellBroadcasts.DELIVERY_TIME,
                        deliveryTime));

        // Set the opt-out dialog flag if this is a CMAS alert (other than Always-on alert e.g,
        // Presidential alert).
        CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(
                getApplicationContext(),
                lastMessage.getSubscriptionId());
        CellBroadcastChannelRange range = channelManager
                .getCellBroadcastChannelRangeFromMessage(lastMessage);

        if (!neverShowOptOutDialog(lastMessage.getSubscriptionId()) && range != null
                && !range.mAlwaysOn) {
            mShowOptOutDialog = true;
        }

        // If there are older emergency alerts to display, update the alert text and return.
        SmsCbMessage nextMessage = getLatestMessage();
        if (nextMessage != null) {
            setFinishAlertOnTouchOutside();
            updateAlertText(nextMessage);
            int subId = nextMessage.getSubscriptionId();
            if (channelManager.isEmergencyMessage(nextMessage)
                    && (range!= null && range.mDisplayIcon)) {
                mAnimationHandler.startIconAnimation(subId);
            } else {
                mAnimationHandler.stopIconAnimation();
            }
            return;
        }

        // Remove pending screen-off messages (animation messages are removed in onPause()).
        mScreenOffHandler.stopScreenOnTimer();

        // Show opt-in/opt-out dialog when the first CMAS alert is received.
        if (mShowOptOutDialog) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            if (prefs.getBoolean(CellBroadcastSettings.KEY_SHOW_CMAS_OPT_OUT_DIALOG, true)) {
                // Clear the flag so the user will only see the opt-out dialog once.
                prefs.edit().putBoolean(CellBroadcastSettings.KEY_SHOW_CMAS_OPT_OUT_DIALOG, false)
                        .apply();

                KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
                if (km.inKeyguardRestrictedInputMode()) {
                    Log.d(TAG, "Showing opt-out dialog in new activity (secure keyguard)");
                    Intent intent = new Intent(this, CellBroadcastOptOutActivity.class);
                    startActivity(intent);
                } else {
                    Log.d(TAG, "Showing opt-out dialog in current activity");
                    mOptOutDialog = CellBroadcastOptOutActivity.showOptOutDialog(this);
                    return; // don't call finish() until user dismisses the dialog
                }
            }
        }
        finish();
    }

    @Override
    public void onDestroy() {
        try {
            unregisterReceiver(mScreenOffReceiver);
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "Unregister Receiver fail", e);
        }
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d(TAG, "onKeyDown: " + event);
        SmsCbMessage message = getLatestMessage();
        if (message != null && CellBroadcastSettings.getResourcesByOperator(getApplicationContext(),
                message.getSubscriptionId(),
                CellBroadcastReceiver.getRoamingOperatorSupported(getApplicationContext()))
                .getBoolean(R.bool.mute_by_physical_button)) {
            switch (event.getKeyCode()) {
                // Volume keys and camera keys mute the alert sound/vibration (except ETWS).
                case KeyEvent.KEYCODE_VOLUME_UP:
                case KeyEvent.KEYCODE_VOLUME_DOWN:
                case KeyEvent.KEYCODE_VOLUME_MUTE:
                case KeyEvent.KEYCODE_CAMERA:
                case KeyEvent.KEYCODE_FOCUS:
                    // Stop playing alert sound/vibration/speech (if started)
                    stopService(new Intent(this, CellBroadcastAlertAudio.class));
                    return true;

                default:
                    break;
            }
            return super.onKeyDown(keyCode, event);
        } else {
            if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
                // TODO: do something to prevent screen off
            }
            // Disable all physical keys if mute_by_physical_button is false
            return true;
        }
    }

    @Override
    public void onBackPressed() {
        // Disable back key
    }

    /**
     * Hide opt-out dialog.
     * In case of any emergency alert invisible, need to hide the opt-out dialog when
     * new alert coming.
     */
    private void hideOptOutDialog() {
        if (mOptOutDialog != null && mOptOutDialog.isShowing()) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            prefs.edit().putBoolean(CellBroadcastSettings.KEY_SHOW_CMAS_OPT_OUT_DIALOG, true)
                    .apply();
            mOptOutDialog.dismiss();
        }
    }

    /**
     * @return true if the device is configured to never show the opt out dialog for the mcc/mnc
     */
    private boolean neverShowOptOutDialog(int subId) {
        return CellBroadcastSettings.getResourcesByOperator(getApplicationContext(), subId,
                        CellBroadcastReceiver.getRoamingOperatorSupported(getApplicationContext()))
                .getBoolean(R.bool.disable_opt_out_dialog);
    }

    /**
     * Copy the message to clipboard.
     *
     * @param message Cell broadcast message.
     *
     * @return {@code true} if success, otherwise {@code false};
     */
    @VisibleForTesting
    public static boolean copyMessageToClipboard(SmsCbMessage message, Context context) {
        ClipboardManager cm = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
        if (cm == null) return false;

        cm.setPrimaryClip(ClipData.newPlainText("Alert Message", message.getMessageBody()));

        String msg = CellBroadcastSettings.getResourcesByOperator(context,
                message.getSubscriptionId(),
                CellBroadcastReceiver.getRoamingOperatorSupported(context))
                .getString(R.string.message_copied);
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        return true;
    }

    /**
     * Remove read message from the notification bar, update the notification text, count or cancel
     * the notification if there is no un-read messages.
     * @param message The dismissed/read message to be removed from the notification bar
     * @param context
     */
    private void removeReadMessageFromNotificationBar(SmsCbMessage message, Context context) {
        Log.d(TAG, "removeReadMessageFromNotificationBar, msg: " + message.toString());
        ArrayList<SmsCbMessage> unreadMessageList = CellBroadcastReceiverApp
                .removeReadMessage(message);
        if (unreadMessageList.isEmpty()) {
            Log.d(TAG, "removeReadMessageFromNotificationBar, cancel notification");
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.cancel(CellBroadcastAlertService.NOTIFICATION_ID);
        } else {
            Log.d(TAG, "removeReadMessageFromNotificationBar, update count to "
                    + unreadMessageList.size() );
            // do not alert if remove unread messages from the notification bar.
           CellBroadcastAlertService.addToNotificationBar(
                   CellBroadcastReceiverApp.getLatestMessage(),
                   unreadMessageList, context,false, false, false);
        }
    }

    /**
     * Finish alert dialog only if all messages are configured with DismissOnOutsideTouch.
     * When multiple messages are displayed, the message with dismissOnOutsideTouch(normally low
     * priority message) is displayed on top of other unread alerts without dismissOnOutsideTouch,
     * users can easily dismiss all messages by touching the screen. better way is to dismiss the
     * alert if and only if all messages with dismiss_on_outside_touch set true.
     */
    private void setFinishAlertOnTouchOutside() {
        if (mMessageList != null) {
            int dismissCount = 0;
            for (SmsCbMessage message : mMessageList) {
                CellBroadcastChannelManager channelManager = new CellBroadcastChannelManager(
                        this, message.getSubscriptionId());
                CellBroadcastChannelManager.CellBroadcastChannelRange range =
                        channelManager.getCellBroadcastChannelRangeFromMessage(message);
                if (range != null && range.mDismissOnOutsideTouch) {
                    dismissCount++;
                }
            }
            setFinishOnTouchOutside(mMessageList.size() > 0 && mMessageList.size() == dismissCount);
        }
    }

    /**
     * If message list of dialog does not have message which is included in newMessageList,
     * Create new list which includes both dialogMessageList and newMessageList
     * without the duplicated message, and Return the new list.
     * If not, just return dialogMessageList as default.
     * @param dialogMessageList message list which this dialog activity is having
     * @param newMessageList message list which is compared with dialogMessageList
     * @return message list which is created with dialogMessageList and newMessageList
     */
    @VisibleForTesting
    public ArrayList<SmsCbMessage> getNewMessageListIfNeeded(
            ArrayList<SmsCbMessage> dialogMessageList,
            ArrayList<SmsCbMessage> newMessageList) {
        if (newMessageList == null || dialogMessageList == null) {
            return dialogMessageList;
        }
        ArrayList<SmsCbMessage> clonedNewMessageList = new ArrayList<>(newMessageList);
        for (SmsCbMessage message : dialogMessageList) {
            clonedNewMessageList.removeIf(
                    msg -> msg.getReceivedTime() == message.getReceivedTime());
        }
        Log.d(TAG, "clonedMessageList.size()=" + clonedNewMessageList.size());
        if (clonedNewMessageList.size() > 0) {
            ArrayList<SmsCbMessage> resultList = new ArrayList<>(dialogMessageList);
            resultList.addAll(clonedNewMessageList);
            Comparator<SmsCbMessage> comparator = (Comparator) (o1, o2) -> {
                Long time1 = new Long(((SmsCbMessage) o1).getReceivedTime());
                Long time2 = new Long(((SmsCbMessage) o2).getReceivedTime());
                return time1.compareTo(time2);
            };
            if (CellBroadcastSettings.getResourcesForDefaultSubId(getApplicationContext())
                    .getBoolean(R.bool.show_cmas_messages_in_priority_order)) {
                Log.d(TAG, "Use priority order Based Comparator");
                comparator = mPriorityBasedComparator;
            }
            Collections.sort(resultList, comparator);
            return resultList;
        }
        return dialogMessageList;
    }

    /**
     * To disable navigation bars, quick settings etc. Force users to engage with the alert dialog
     * before switching to other activities.
     *
     * @param disable if set to {@code true} to disable the status bar. {@code false} otherwise.
     */
    private void setStatusBarDisabledIfNeeded(boolean disable) {
        if (!CellBroadcastSettings.getResourcesForDefaultSubId(getApplicationContext())
                .getBoolean(R.bool.disable_status_bar)) {
            return;
        }
        try {
            // TODO change to system API in future.
            StatusBarManager statusBarManager = getSystemService(StatusBarManager.class);
            Method disableMethod = StatusBarManager.class.getDeclaredMethod(
                    "disable", int.class);
            Method disableMethod2 = StatusBarManager.class.getDeclaredMethod(
                    "disable2", int.class);
            if (disable) {
                // flags to be disabled
                int disableHome = StatusBarManager.class.getDeclaredField("DISABLE_HOME")
                        .getInt(null);
                int disableRecent = StatusBarManager.class
                        .getDeclaredField("DISABLE_RECENT").getInt(null);
                int disableBack = StatusBarManager.class.getDeclaredField("DISABLE_BACK")
                        .getInt(null);
                int disableQuickSettings = StatusBarManager.class.getDeclaredField(
                        "DISABLE2_QUICK_SETTINGS").getInt(null);
                int disableNotificationShaded = StatusBarManager.class.getDeclaredField(
                        "DISABLE2_NOTIFICATION_SHADE").getInt(null);
                disableMethod.invoke(statusBarManager, disableHome | disableBack | disableRecent);
                disableMethod2.invoke(statusBarManager, disableQuickSettings
                        | disableNotificationShaded);
            } else {
                int disableNone = StatusBarManager.class.getDeclaredField("DISABLE_NONE")
                        .getInt(null);
                disableMethod.invoke(statusBarManager, disableNone);
                disableMethod2.invoke(statusBarManager, disableNone);
            }
        } catch (Exception e) {
            CellBroadcastReceiverMetrics.getInstance()
                    .logModuleError(ERRSRC_CBR, ERRTYPE_STATUSBAR);
            Log.e(TAG, "Failed to disable navigation when showing alert: ", e);
        }
    }
}