summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/util/PhoneUtils.java
blob: 3bf784eec1cc3a66215c721ce0fb8efa2027520a (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
/*
 * Copyright (C) 2015 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.messaging.util;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.provider.Settings;
import android.provider.Telephony;
import android.support.v4.util.ArrayMap;
import android.support.v4.text.BidiFormatter;
import android.support.v4.text.TextDirectionHeuristicsCompat;
import android.telecom.TelecomManager;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.android.messaging.Factory;
import com.android.messaging.R;
import com.android.messaging.datamodel.data.ParticipantData;
import com.android.messaging.sms.MmsSmsUtils;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;

/**
 * This class abstracts away platform dependency of calling telephony related
 * platform APIs, mostly involving TelephonyManager, SubscriptionManager and
 * a bit of SmsManager.
 *
 * The class instance can only be obtained via the get(int subId) method parameterized
 * by a SIM subscription ID. On pre-L_MR1, the subId is not used and it has to be
 * the default subId (-1).
 *
 * A convenient getDefault() method is provided for default subId (-1) on any platform
 */
public abstract class PhoneUtils {
    private static final String TAG = LogUtil.BUGLE_TAG;

    private static final int MINIMUM_PHONE_NUMBER_LENGTH_TO_FORMAT = 6;

    private static final List<SubscriptionInfo> EMPTY_SUBSCRIPTION_LIST = new ArrayList<>();

    // The canonical phone number cache
    // Each country gets its own cache. The following maps from ISO country code to
    // the country's cache. Each cache maps from original phone number to canonicalized phone
    private static final ArrayMap<String, ArrayMap<String, String>> sCanonicalPhoneNumberCache =
            new ArrayMap<>();

    public static int sOverrideSendingSubId = ParticipantData.DEFAULT_SELF_SUB_ID;

    public static int getOverrideSendingSubId() {
        return sOverrideSendingSubId;
    }

    public static void setOverrideSendingSubId(int subId) {
        sOverrideSendingSubId = subId;
    }

    protected final Context mContext;
    protected final TelephonyManager mTelephonyManager;
    protected final int mSubId;

    public PhoneUtils(int subId) {
        mSubId = subId;
        mContext = Factory.get().getApplicationContext();
        mTelephonyManager =
                (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
    }

    /**
     * Get the SIM's country code
     *
     * @return the country code on the SIM
     */
    public abstract String getSimCountry();

    /**
     * Get number of SIM slots
     *
     * @return the SIM slot count
     */
    public abstract int getSimSlotCount();

    /**
     * Get SIM's carrier name
     *
     * @return the carrier name of the SIM
     */
    public abstract String getCarrierName();

    /**
     * Check if there is SIM inserted on the device
     *
     * @return true if there is SIM inserted, false otherwise
     */
    public abstract boolean hasSim();

    /**
     * Check if the SIM is roaming
     *
     * @return true if the SIM is in romaing state, false otherwise
     */
    public abstract boolean isRoaming();

    /**
     * Get the MCC and MNC in integer of the SIM's provider
     *
     * @return an array of two ints, [0] is the MCC code and [1] is the MNC code
     */
    public abstract int[] getMccMnc();

    /**
     * Get the mcc/mnc string
     *
     * @return the text of mccmnc string
     */
    public abstract String getSimOperatorNumeric();

    /**
     * Get the SIM's self raw number, i.e. not canonicalized
     *
     * @param allowOverride Whether to use the app's setting to override the self number
     * @return the original self number
     * @throws IllegalStateException if no active subscription on L-MR1+
     */
    public abstract String getSelfRawNumber(final boolean allowOverride);

    /**
     * Returns the "effective" subId, or the subId used in the context of actual messages,
     * conversations and subscription-specific settings, for the given "nominal" sub id.
     *
     * For pre-L-MR1 platform, this should always be
     * {@value com.android.messaging.datamodel.data.ParticipantData#DEFAULT_SELF_SUB_ID};
     *
     * On the other hand, for L-MR1 and above, DEFAULT_SELF_SUB_ID will be mapped to the system
     * default subscription id for SMS.
     *
     * @param subId The input subId
     * @return the real subId if we can convert
     */
    public abstract int getEffectiveSubId(int subId);

    /**
     * Returns the number of active subscriptions in the device.
     */
    public abstract int getActiveSubscriptionCount();

    /**
     * Get {@link SmsManager} instance
     *
     * @return the relevant SmsManager instance based on OS version and subId
     */
    public abstract SmsManager getSmsManager();

    /**
     * Get the default SMS subscription id
     *
     * @return the default sub ID
     */
    public abstract int getDefaultSmsSubscriptionId();

    /**
     * Returns if there's currently a system default SIM selected for sending SMS.
     */
    public abstract boolean getHasPreferredSmsSim();

    /**
     * For L_MR1, system may return a negative subId. Convert this into our own
     * subId, so that we consistently use -1 for invalid or default.
     *
     * see b/18629526 and b/18670346
     *
     * @param intent The push intent from system
     * @param extraName The name of the sub id extra
     * @return the subId that is valid and meaningful for the app
     */
    public abstract int getEffectiveIncomingSubIdFromSystem(Intent intent, String extraName);

    /**
     * Get the subscription_id column value from a telephony provider cursor
     *
     * @param cursor The database query cursor
     * @param subIdIndex The index of the subId column in the cursor
     * @return the subscription_id column value from the cursor
     */
    public abstract int getSubIdFromTelephony(Cursor cursor, int subIdIndex);

    /**
     * Check if data roaming is enabled
     *
     * @return true if data roaming is enabled, false otherwise
     */
    public abstract boolean isDataRoamingEnabled();

    /**
     * Check if mobile data is enabled
     *
     * @return true if mobile data is enabled, false otherwise
     */
    public abstract boolean isMobileDataEnabled();

    /**
     * Get the set of self phone numbers, all normalized
     *
     * @return the set of normalized self phone numbers
     */
    public abstract HashSet<String> getNormalizedSelfNumbers();

    /**
     * This interface packages methods should only compile on L_MR1.
     * This is needed to make unit tests happy when mockito tries to
     * mock these methods. Calling on these methods on L_MR1 requires
     * an extra invocation of toMr1().
     */
    public interface LMr1 {
        /**
         * Get this SIM's information. Only applies to L_MR1 above
         *
         * @return the subscription info of the SIM
         */
        public abstract SubscriptionInfo getActiveSubscriptionInfo();

        /**
         * Get the list of active SIMs in system. Only applies to L_MR1 above
         *
         * @return the list of subscription info for all inserted SIMs
         */
        public abstract List<SubscriptionInfo> getActiveSubscriptionInfoList();

        /**
         * Register subscription change listener. Only applies to L_MR1 above
         *
         * @param listener The listener to register
         */
        public abstract void registerOnSubscriptionsChangedListener(
                SubscriptionManager.OnSubscriptionsChangedListener listener);
    }

    /**
     * The PhoneUtils class for pre L_MR1
     */
    public static class PhoneUtilsPreLMR1 extends PhoneUtils {
        private final ConnectivityManager mConnectivityManager;

        public PhoneUtilsPreLMR1() {
            super(ParticipantData.DEFAULT_SELF_SUB_ID);
            mConnectivityManager =
                    (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        }

        @Override
        public String getSimCountry() {
            final String country = mTelephonyManager.getSimCountryIso();
            if (TextUtils.isEmpty(country)) {
                return null;
            }
            return country.toUpperCase();
        }

        @Override
        public int getSimSlotCount() {
            // Don't support MSIM pre-L_MR1
            return 1;
        }

        @Override
        public String getCarrierName() {
            return mTelephonyManager.getNetworkOperatorName();
        }

        @Override
        public boolean hasSim() {
            return mTelephonyManager.getSimState() != TelephonyManager.SIM_STATE_ABSENT;
        }

        @Override
        public boolean isRoaming() {
            return mTelephonyManager.isNetworkRoaming();
        }

        @Override
        public int[] getMccMnc() {
            final String mccmnc = mTelephonyManager.getSimOperator();
            int mcc = 0;
            int mnc = 0;
            try {
                mcc = Integer.parseInt(mccmnc.substring(0, 3));
                mnc = Integer.parseInt(mccmnc.substring(3));
            } catch (Exception e) {
                LogUtil.w(TAG, "PhoneUtils.getMccMnc: invalid string " + mccmnc, e);
            }
            return new int[]{mcc, mnc};
        }

        @Override
        public String getSimOperatorNumeric() {
            return mTelephonyManager.getSimOperator();
        }

        @Override
        public String getSelfRawNumber(final boolean allowOverride) {
            if (allowOverride) {
                final String userDefinedNumber = getNumberFromPrefs(mContext,
                        ParticipantData.DEFAULT_SELF_SUB_ID);
                if (!TextUtils.isEmpty(userDefinedNumber)) {
                    return userDefinedNumber;
                }
            }
            return mTelephonyManager.getLine1Number();
        }

        @Override
        public int getEffectiveSubId(int subId) {
            Assert.equals(ParticipantData.DEFAULT_SELF_SUB_ID, subId);
            return ParticipantData.DEFAULT_SELF_SUB_ID;
        }

        @Override
        public SmsManager getSmsManager() {
            return SmsManager.getDefault();
        }

        @Override
        public int getDefaultSmsSubscriptionId() {
            Assert.fail("PhoneUtils.getDefaultSmsSubscriptionId(): not supported before L MR1");
            return ParticipantData.DEFAULT_SELF_SUB_ID;
        }

        @Override
        public boolean getHasPreferredSmsSim() {
            // SIM selection is not supported pre-L_MR1.
            return true;
        }

        @Override
        public int getActiveSubscriptionCount() {
            return hasSim() ? 1 : 0;
        }

        @Override
        public int getEffectiveIncomingSubIdFromSystem(Intent intent, String extraName) {
            // Pre-L_MR1 always returns the default id
            return ParticipantData.DEFAULT_SELF_SUB_ID;
        }

        @Override
        public int getSubIdFromTelephony(Cursor cursor, int subIdIndex) {
            // No subscription_id column before L_MR1
            return ParticipantData.DEFAULT_SELF_SUB_ID;
        }

        @Override
        @SuppressWarnings("deprecation")
        public boolean isDataRoamingEnabled() {
            boolean dataRoamingEnabled = false;
            final ContentResolver cr = mContext.getContentResolver();
            if (OsUtil.isAtLeastJB_MR1()) {
                dataRoamingEnabled =
                        (Settings.Global.getInt(cr, Settings.Global.DATA_ROAMING, 0) != 0);
            } else {
                dataRoamingEnabled =
                        (Settings.System.getInt(cr, Settings.System.DATA_ROAMING, 0) != 0);
            }
            return dataRoamingEnabled;
        }

        @Override
        public boolean isMobileDataEnabled() {
            boolean mobileDataEnabled = false;
            try {
                final Class cmClass = mConnectivityManager.getClass();
                final Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
                method.setAccessible(true); // Make the method callable
                // get the setting for "mobile data"
                mobileDataEnabled = (Boolean) method.invoke(mConnectivityManager);
            } catch (final Exception e) {
                LogUtil.e(TAG, "PhoneUtil.isMobileDataEnabled: system api not found", e);
            }
            return mobileDataEnabled;
        }

        @Override
        public HashSet<String> getNormalizedSelfNumbers() {
            final HashSet<String> numbers = new HashSet<>();
            numbers.add(getCanonicalForSelf(true/*allowOverride*/));
            return numbers;
        }
    }

    /**
     * The PhoneUtils class for L_MR1
     */
    public static class PhoneUtilsLMR1 extends PhoneUtils implements LMr1 {
        private final SubscriptionManager mSubscriptionManager;

        public PhoneUtilsLMR1(final int subId) {
            super(subId);
            mSubscriptionManager = SubscriptionManager.from(Factory.get().getApplicationContext());
        }

        @Override
        public String getSimCountry() {
            final SubscriptionInfo subInfo = getActiveSubscriptionInfo();
            if (subInfo != null) {
                final String country = subInfo.getCountryIso();
                if (TextUtils.isEmpty(country)) {
                    return null;
                }
                return country.toUpperCase();
            }
            return null;
        }

        @Override
        public int getSimSlotCount() {
            return mSubscriptionManager.getActiveSubscriptionInfoCountMax();
        }

        @Override
        public String getCarrierName() {
            final SubscriptionInfo subInfo = getActiveSubscriptionInfo();
            if (subInfo != null) {
                final CharSequence displayName = subInfo.getDisplayName();
                if (!TextUtils.isEmpty(displayName)) {
                    return displayName.toString();
                }
                final CharSequence carrierName = subInfo.getCarrierName();
                if (carrierName != null) {
                    return carrierName.toString();
                }
            }
            return null;
        }

        @Override
        public boolean hasSim() {
            return mSubscriptionManager.getActiveSubscriptionInfoCount() > 0;
        }

        @Override
        public boolean isRoaming() {
            return mSubscriptionManager.isNetworkRoaming(mSubId);
        }

        @Override
        public int[] getMccMnc() {
            int mcc = 0;
            int mnc = 0;
            final SubscriptionInfo subInfo = getActiveSubscriptionInfo();
            if (subInfo != null) {
                mcc = subInfo.getMcc();
                mnc = subInfo.getMnc();
            }
            return new int[]{mcc, mnc};
        }

        @Override
        public String getSimOperatorNumeric() {
            // For L_MR1 we return the canonicalized (xxxxxx) string
            return getMccMncString(getMccMnc());
        }

        @Override
        public String getSelfRawNumber(final boolean allowOverride) {
            if (allowOverride) {
                final String userDefinedNumber = getNumberFromPrefs(mContext, mSubId);
                if (!TextUtils.isEmpty(userDefinedNumber)) {
                    return userDefinedNumber;
                }
            }

            final SubscriptionInfo subInfo = getActiveSubscriptionInfo();
            if (subInfo != null) {
                String phoneNumber = subInfo.getNumber();
                if (TextUtils.isEmpty(phoneNumber) && LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                    LogUtil.d(TAG, "SubscriptionInfo phone number for self is empty!");
                }
                return phoneNumber;
            }
            LogUtil.w(TAG, "PhoneUtils.getSelfRawNumber: subInfo is null for " + mSubId);
            throw new IllegalStateException("No active subscription");
        }

        @Override
        public SubscriptionInfo getActiveSubscriptionInfo() {
            try {
                final SubscriptionInfo subInfo =
                        mSubscriptionManager.getActiveSubscriptionInfo(mSubId);
                if (subInfo == null) {
                    if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                        // This is possible if the sub id is no longer available.
                        LogUtil.d(TAG, "PhoneUtils.getActiveSubscriptionInfo(): empty sub info for "
                                + mSubId);
                    }
                }
                return subInfo;
            } catch (Exception e) {
                LogUtil.e(TAG, "PhoneUtils.getActiveSubscriptionInfo: system exception for "
                        + mSubId, e);
            }
            return null;
        }

        @Override
        public List<SubscriptionInfo> getActiveSubscriptionInfoList() {
            final List<SubscriptionInfo> subscriptionInfos =
                    mSubscriptionManager.getActiveSubscriptionInfoList();
            if (subscriptionInfos != null) {
                return subscriptionInfos;
            }
            return EMPTY_SUBSCRIPTION_LIST;
        }

        @Override
        public int getEffectiveSubId(int subId) {
            if (subId == ParticipantData.DEFAULT_SELF_SUB_ID) {
                return getDefaultSmsSubscriptionId();
            }
            return subId;
        }

        @Override
        public void registerOnSubscriptionsChangedListener(
                SubscriptionManager.OnSubscriptionsChangedListener listener) {
            mSubscriptionManager.addOnSubscriptionsChangedListener(listener);
        }

        @Override
        public SmsManager getSmsManager() {
            return SmsManager.getSmsManagerForSubscriptionId(mSubId);
        }

        @Override
        public int getDefaultSmsSubscriptionId() {
            final int systemDefaultSubId = SmsManager.getDefaultSmsSubscriptionId();
            if (systemDefaultSubId < 0) {
                // Always use -1 for any negative subId from system
                return ParticipantData.DEFAULT_SELF_SUB_ID;
            } else if (mSubscriptionManager.getSlotId(systemDefaultSubId) < 0) {
                // Our default isn't inserted. Use the "select one" internal default.
                return ParticipantData.DEFAULT_SELF_SUB_ID;
            }
            return systemDefaultSubId;
        }

        @Override
        public boolean getHasPreferredSmsSim() {
            return getDefaultSmsSubscriptionId() != ParticipantData.DEFAULT_SELF_SUB_ID;
        }

        @Override
        public int getActiveSubscriptionCount() {
            return mSubscriptionManager.getActiveSubscriptionInfoCount();
        }

        @Override
        public int getEffectiveIncomingSubIdFromSystem(Intent intent, String extraName) {
            return getEffectiveIncomingSubIdFromSystem(intent.getIntExtra(extraName,
                    ParticipantData.DEFAULT_SELF_SUB_ID));
        }

        private int getEffectiveIncomingSubIdFromSystem(int subId) {
            if (subId < 0) {
                if (mSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
                    // For multi-SIM device, we can not decide which SIM to use if system
                    // does not know either. So just make it the invalid sub id.
                    return ParticipantData.DEFAULT_SELF_SUB_ID;
                }
                // For single-SIM device, it must come from the only SIM we have
                return getDefaultSmsSubscriptionId();
            }
            return subId;
        }

        @Override
        public int getSubIdFromTelephony(Cursor cursor, int subIdIndex) {
            return getEffectiveIncomingSubIdFromSystem(cursor.getInt(subIdIndex));
        }

        @Override
        public boolean isDataRoamingEnabled() {
            final SubscriptionInfo subInfo = getActiveSubscriptionInfo();
            if (subInfo == null) {
                // There is nothing we can do if system give us empty sub info
                LogUtil.e(TAG, "PhoneUtils.isDataRoamingEnabled: system return empty sub info for "
                        + mSubId);
                return false;
            }
            return subInfo.getDataRoaming() != SubscriptionManager.DATA_ROAMING_DISABLE;
        }

        @Override
        public boolean isMobileDataEnabled() {
            boolean mobileDataEnabled = false;
            try {
                final Class cmClass = mTelephonyManager.getClass();
                final Method method = cmClass.getDeclaredMethod("getDataEnabled", Integer.TYPE);
                method.setAccessible(true); // Make the method callable
                // get the setting for "mobile data"
                mobileDataEnabled = (Boolean) method.invoke(
                        mTelephonyManager, Integer.valueOf(mSubId));
            } catch (final Exception e) {
                LogUtil.e(TAG, "PhoneUtil.isMobileDataEnabled: system api not found", e);
            }
            return mobileDataEnabled;

        }

        @Override
        public HashSet<String> getNormalizedSelfNumbers() {
            final HashSet<String> numbers = new HashSet<>();
            for (SubscriptionInfo info : getActiveSubscriptionInfoList()) {
                numbers.add(PhoneUtils.get(info.getSubscriptionId()).getCanonicalForSelf(
                        true/*allowOverride*/));
            }
            return numbers;
        }
    }

    /**
     * A convenient get() method that uses the default SIM. Use this when SIM is
     * not relevant, e.g. isDefaultSmsApp
     *
     * @return an instance of PhoneUtils for default SIM
     */
    public static PhoneUtils getDefault() {
        return Factory.get().getPhoneUtils(ParticipantData.DEFAULT_SELF_SUB_ID);
    }

    /**
     * Get an instance of PhoneUtils associated with a specific SIM, which is also platform
     * specific.
     *
     * @param subId The SIM's subscription ID
     * @return the instance
     */
    public static PhoneUtils get(int subId) {
        return Factory.get().getPhoneUtils(subId);
    }

    public LMr1 toLMr1() {
        if (OsUtil.isAtLeastL_MR1()) {
            return (LMr1) this;
        } else {
            Assert.fail("PhoneUtils.toLMr1(): invalid OS version");
            return null;
        }
    }

    /**
     * Check if this device supports SMS
     *
     * @return true if SMS is supported, false otherwise
     */
    public boolean isSmsCapable() {
        return mTelephonyManager.isSmsCapable();
    }

    /**
     * Check if this device supports voice calling
     *
     * @return true if voice calling is supported, false otherwise
     */
    public boolean isVoiceCapable() {
        return mTelephonyManager.isVoiceCapable();
    }

    /**
     * Get the ISO country code from system locale setting
     *
     * @return the ISO country code from system locale
     */
    private static String getLocaleCountry() {
        final String country = Locale.getDefault().getCountry();
        if (TextUtils.isEmpty(country)) {
            return null;
        }
        return country.toUpperCase();
    }

    /**
     * Get ISO country code from the SIM, if not available, fall back to locale
     *
     * @return SIM or locale ISO country code
     */
    public String getSimOrDefaultLocaleCountry() {
        String country = getSimCountry();
        if (country == null) {
            country = getLocaleCountry();
        }
        return country;
    }

    // Get or set the cache of canonicalized phone numbers for a specific country
    private static ArrayMap<String, String> getOrAddCountryMapInCacheLocked(String country) {
        if (country == null) {
            country = "";
        }
        ArrayMap<String, String> countryMap = sCanonicalPhoneNumberCache.get(country);
        if (countryMap == null) {
            countryMap = new ArrayMap<>();
            sCanonicalPhoneNumberCache.put(country, countryMap);
        }
        return countryMap;
    }

    // Get canonicalized phone number from cache
    private static String getCanonicalFromCache(final String phoneText, String country) {
        synchronized (sCanonicalPhoneNumberCache) {
            final ArrayMap<String, String> countryMap = getOrAddCountryMapInCacheLocked(country);
            return countryMap.get(phoneText);
        }
    }

    // Put canonicalized phone number into cache
    private static void putCanonicalToCache(final String phoneText, String country,
            final String canonical) {
        synchronized (sCanonicalPhoneNumberCache) {
            final ArrayMap<String, String> countryMap = getOrAddCountryMapInCacheLocked(country);
            countryMap.put(phoneText, canonical);
        }
    }

    /**
     * Utility method to parse user input number into standard E164 number.
     *
     * @param phoneText Phone number text as input by user.
     * @param country ISO country code based on which to parse the number.
     * @return E164 phone number. Returns null in case parsing failed.
     */
    private static String getValidE164Number(final String phoneText, final String country) {
        final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
        try {
            final PhoneNumber phoneNumber = phoneNumberUtil.parse(phoneText, country);
            if (phoneNumber != null && phoneNumberUtil.isValidNumber(phoneNumber)) {
                return phoneNumberUtil.format(phoneNumber, PhoneNumberFormat.E164);
            }
        } catch (final NumberParseException e) {
            LogUtil.e(TAG, "PhoneUtils.getValidE164Number(): Not able to parse phone number "
                        + LogUtil.sanitizePII(phoneText) + " for country " + country);
        }
        return null;
    }

    /**
     * Canonicalize phone number using system locale country
     *
     * @param phoneText The phone number to canonicalize
     * @return the canonicalized number
     */
    public String getCanonicalBySystemLocale(final String phoneText) {
        return getCanonicalByCountry(phoneText, getLocaleCountry());
    }

    /**
     * Canonicalize phone number using SIM's country, may fall back to system locale country
     * if SIM country can not be obtained
     *
     * @param phoneText The phone number to canonicalize
     * @return the canonicalized number
     */
    public String getCanonicalBySimLocale(final String phoneText) {
        return getCanonicalByCountry(phoneText, getSimOrDefaultLocaleCountry());
    }

    /**
     * Canonicalize phone number using a country code.
     * This uses an internal cache per country to speed up.
     *
     * @param phoneText The phone number to canonicalize
     * @param country The ISO country code to use
     * @return the canonicalized number, or the original number if can't be parsed
     */
    private String getCanonicalByCountry(final String phoneText, final String country) {
        Assert.notNull(phoneText);

        String canonicalNumber = getCanonicalFromCache(phoneText, country);
        if (canonicalNumber != null) {
            return canonicalNumber;
        }
        canonicalNumber = getValidE164Number(phoneText, country);
        if (canonicalNumber == null) {
            // If we can't normalize this number, we just use the display string number.
            // This is possible for short codes and other non-localizable numbers.
            canonicalNumber = phoneText;
        }
        putCanonicalToCache(phoneText, country, canonicalNumber);
        return canonicalNumber;
    }

    /**
     * Canonicalize the self (per SIM) phone number
     *
     * @param allowOverride whether to use the override number in app settings
     * @return the canonicalized self phone number
     */
    public String getCanonicalForSelf(final boolean allowOverride) {
        String selfNumber = null;
        try {
            selfNumber = getSelfRawNumber(allowOverride);
        } catch (IllegalStateException e) {
            // continue;
        }
        if (selfNumber == null) {
            return "";
        }
        return getCanonicalBySimLocale(selfNumber);
    }

    /**
     * Get the SIM's phone number in NATIONAL format with only digits, used in sending
     * as LINE1NOCOUNTRYCODE macro in mms_config
     *
     * @return all digits national format number of the SIM
     */
    public String getSimNumberNoCountryCode() {
        String selfNumber = null;
        try {
            selfNumber = getSelfRawNumber(false/*allowOverride*/);
        } catch (IllegalStateException e) {
            // continue
        }
        if (selfNumber == null) {
            selfNumber = "";
        }
        final String country = getSimCountry();
        final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
        try {
            final PhoneNumber phoneNumber = phoneNumberUtil.parse(selfNumber, country);
            if (phoneNumber != null && phoneNumberUtil.isValidNumber(phoneNumber)) {
                return phoneNumberUtil
                        .format(phoneNumber, PhoneNumberFormat.NATIONAL)
                        .replaceAll("\\D", "");
            }
        } catch (final NumberParseException e) {
            LogUtil.e(TAG, "PhoneUtils.getSimNumberNoCountryCode(): Not able to parse phone number "
                    + LogUtil.sanitizePII(selfNumber) + " for country " + country);
        }
        return selfNumber;

    }

    /**
     * Format a phone number for displaying, using system locale country.
     * If the country code matches between the system locale and the input phone number,
     * it will be formatted into NATIONAL format, otherwise, the INTERNATIONAL format
     *
     * @param phoneText The original phone text
     * @return formatted number
     */
    public String formatForDisplay(final String phoneText) {
        // Only format a valid number which length >=6
        if (TextUtils.isEmpty(phoneText) ||
                phoneText.replaceAll("\\D", "").length() < MINIMUM_PHONE_NUMBER_LENGTH_TO_FORMAT) {
            return phoneText;
        }
        final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
        final String systemCountry = getLocaleCountry();
        final int systemCountryCode = phoneNumberUtil.getCountryCodeForRegion(systemCountry);
        try {
            final PhoneNumber parsedNumber = phoneNumberUtil.parse(phoneText, systemCountry);
            final PhoneNumberFormat phoneNumberFormat =
                    (systemCountryCode > 0 && parsedNumber.getCountryCode() == systemCountryCode) ?
                            PhoneNumberFormat.NATIONAL : PhoneNumberFormat.INTERNATIONAL;
            return BidiFormatter.getInstance().unicodeWrap(
                    phoneNumberUtil.format(parsedNumber, phoneNumberFormat),
                    TextDirectionHeuristicsCompat.LTR);
        } catch (NumberParseException e) {
            LogUtil.e(TAG, "PhoneUtils.formatForDisplay: invalid phone number "
                    + LogUtil.sanitizePII(phoneText) + " with country " + systemCountry);
            return phoneText;
        }
    }

    /**
     * Is Messaging the default SMS app?
     * - On KLP+ this checks the system setting.
     * - On JB (and below) this always returns true, since the setting was added in KLP.
     */
    public boolean isDefaultSmsApp() {
        if (OsUtil.isAtLeastKLP()) {
            final String configuredApplication = Telephony.Sms.getDefaultSmsPackage(mContext);
            return  mContext.getPackageName().equals(configuredApplication);
        }
        return true;
    }

    /**
     * Get default SMS app package name
     *
     * @return the package name of default SMS app
     */
    public String getDefaultSmsApp() {
        if (OsUtil.isAtLeastKLP()) {
            return Telephony.Sms.getDefaultSmsPackage(mContext);
        }
        return null;
    }

    /**
     * Determines if SMS is currently enabled on this device.
     * - Device must support SMS
     * - On KLP+ we must be set as the default SMS app
     */
    public boolean isSmsEnabled() {
        return isSmsCapable() && isDefaultSmsApp();
    }

    /**
     * Returns the name of the default SMS app, or the empty string if there is
     * an error or there is no default app (e.g. JB and below).
     */
    public String getDefaultSmsAppLabel() {
        if (OsUtil.isAtLeastKLP()) {
            final String packageName = Telephony.Sms.getDefaultSmsPackage(mContext);
            final PackageManager pm = mContext.getPackageManager();
            try {
                final ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
                return pm.getApplicationLabel(appInfo).toString();
            } catch (NameNotFoundException e) {
                // Fall through and return empty string
            }
        }
        return "";
    }

    /**
     * Gets the state of Airplane Mode.
     *
     * @return true if enabled.
     */
    @SuppressWarnings("deprecation")
    public boolean isAirplaneModeOn() {
        if (OsUtil.isAtLeastJB_MR1()) {
            return Settings.Global.getInt(mContext.getContentResolver(),
                    Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
        } else {
            return Settings.System.getInt(mContext.getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 0) != 0;
        }
    }

    public static String getMccMncString(int[] mccmnc) {
        if (mccmnc == null || mccmnc.length != 2) {
            return "000000";
        }
        return String.format("%03d%03d", mccmnc[0], mccmnc[1]);
    }

    public static String canonicalizeMccMnc(final String mcc, final String mnc) {
        try {
            return String.format("%03d%03d", Integer.parseInt(mcc), Integer.parseInt(mnc));
        } catch (final NumberFormatException e) {
            // Return invalid as is
            LogUtil.w(TAG, "canonicalizeMccMnc: invalid mccmnc:" + mcc + " ," + mnc);
        }
        return mcc + mnc;
    }

    /**
     * Returns whether the given destination is valid for sending SMS/MMS message.
     */
    public static boolean isValidSmsMmsDestination(final String destination) {
        return PhoneNumberUtils.isWellFormedSmsAddress(destination) ||
                MmsSmsUtils.isEmailAddress(destination);
    }

    public interface SubscriptionRunnable {
        void runForSubscription(int subId);
    }

    /**
     * A convenience method for iterating through all active subscriptions
     *
     * @param runnable a {@link SubscriptionRunnable} for performing work on each subscription.
     */
    public static void forEachActiveSubscription(final SubscriptionRunnable runnable) {
        if (OsUtil.isAtLeastL_MR1()) {
            final List<SubscriptionInfo> subscriptionList =
                    getDefault().toLMr1().getActiveSubscriptionInfoList();
            for (final SubscriptionInfo subscriptionInfo : subscriptionList) {
                runnable.runForSubscription(subscriptionInfo.getSubscriptionId());
            }
        } else {
            runnable.runForSubscription(ParticipantData.DEFAULT_SELF_SUB_ID);
        }
    }

    private static String getNumberFromPrefs(final Context context, final int subId) {
        final BuglePrefs prefs = BuglePrefs.getSubscriptionPrefs(subId);
        final String mmsPhoneNumberPrefKey =
                context.getString(R.string.mms_phone_number_pref_key);
        final String userDefinedNumber = prefs.getString(mmsPhoneNumberPrefKey, null);
        if (!TextUtils.isEmpty(userDefinedNumber)) {
            return userDefinedNumber;
        }
        return null;
    }

       /**
     * Decide whether the current product  is DSDS in MMS
     */
    public static boolean isMultiSimEnabledMms() {
        return TelephonyManager.getDefault().isMultiSimEnabled();
    }

    private static boolean isCDMAPhone(int subscription) {
        int activePhone = isMultiSimEnabledMms()
                ? TelephonyManager.getDefault().getCurrentPhoneType(subscription)
                : TelephonyManager.getDefault().getPhoneType();
        return activePhone == TelephonyManager.PHONE_TYPE_CDMA;
    }

    private static boolean isNetworkRoaming(int subscription) {
        return isMultiSimEnabledMms()
                ? TelephonyManager.getDefault().isNetworkRoaming(subscription)
                : TelephonyManager.getDefault().isNetworkRoaming();
    }

    public static boolean isCDMAInternationalRoaming(int subscription) {
        return isCDMAPhone(subscription) && isNetworkRoaming(subscription);
    }

    /**
     * Retrieve the account metadata, but if the account does not exist or the device has only a
     * single registered and enabled account, return null.
     */
    public static PhoneAccount getAccountOrNull(Context context,
            PhoneAccountHandle accountHandle) {
        TelecomManager telecomManager =
                (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
        final PhoneAccount account = telecomManager.getPhoneAccount(accountHandle);
        if (telecomManager.getCallCapablePhoneAccounts().size() <= 1) {
            return null;
        }
        return account;
    }
}