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
|
/*
* Copyright (C) 2014 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.settings.sim;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemProperties;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.preference.PreferenceCategory;
import android.provider.SearchIndexableResource;
import android.provider.Settings;
import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.telephony.PhoneConstants;
import com.android.settings.RestrictedSettingsFragment;
import com.android.settings.Utils;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settings.R;
import com.android.internal.telephony.IExtTelephony;
import com.android.internal.telephony.TelephonyProperties;
import java.util.ArrayList;
import java.util.List;
public class SimSettings extends RestrictedSettingsFragment implements Indexable {
private static final String TAG = "SimSettings";
private static final boolean DBG = false;
// These are the list of possible values that
// IExtTelephony.getCurrentUiccCardProvisioningStatus() can return
private static final int PROVISIONED = 1;
private static final int NOT_PROVISIONED = 0;
private static final int INVALID_STATE = -1;
private static final int CARD_NOT_PRESENT = -2;
private static final String DISALLOW_CONFIG_SIM = "no_config_sim";
private static final String SIM_CARD_CATEGORY = "sim_cards";
private static final String KEY_CELLULAR_DATA = "sim_cellular_data";
private static final String KEY_CALLS = "sim_calls";
private static final String KEY_SMS = "sim_sms";
public static final String EXTRA_SLOT_ID = "slot_id";
private static final String SIM_ACTIVITIES_CATEGORY = "sim_activities";
private static final String MOBILE_NETWORK_CATEGORY = "mobile_network";
private static final String KEY_PRIMARY_SUB_SELECT = "select_primary_sub";
private IExtTelephony mExtTelephony = IExtTelephony.Stub.
asInterface(ServiceManager.getService("extphone"));
/**
* By UX design we use only one Subscription Information(SubInfo) record per SIM slot.
* mAvalableSubInfos is the list of SubInfos we present to the user.
* mSubInfoList is the list of all SubInfos.
* mSelectableSubInfos is the list of SubInfos that a user can select for data, calls, and SMS.
*/
private List<SubscriptionInfo> mAvailableSubInfos = null;
private List<SubscriptionInfo> mSubInfoList = null;
private List<SubscriptionInfo> mSelectableSubInfos = null;
private PreferenceCategory mSimCards = null;
private PreferenceCategory mMobileNetwork;
private SubscriptionManager mSubscriptionManager;
private int mNumSlots;
private Context mContext;
private static AlertDialog sAlertDialog = null;
private static ProgressDialog sProgressDialog = null;
private boolean needUpdate = false;
private int mPhoneCount = TelephonyManager.getDefault().getPhoneCount();
private int[] mUiccProvisionStatus = new int[mPhoneCount];
private Preference mPrimarySubSelect = null;
private int[] mCallState = new int[mPhoneCount];
private PhoneStateListener[] mPhoneStateListener = new PhoneStateListener[mPhoneCount];
private static final String ACTION_UICC_MANUAL_PROVISION_STATUS_CHANGED =
"org.codeaurora.intent.action.ACTION_UICC_MANUAL_PROVISION_STATUS_CHANGED";
private static final String EXTRA_NEW_PROVISION_STATE = "newProvisionState";
private static final String CONFIG_LTE_SUB_SELECT_MODE = "config_lte_sub_select_mode";
private static final String CONFIG_PRIMARY_SUB_SETABLE = "config_primary_sub_setable";
private static final String CONFIG_CURRENT_PRIMARY_SUB = "config_current_primary_sub";
// If this config set to '1' DDS option would be greyed out on UI.
// For more info pls refere framework code.
private static final String CONFIG_DISABLE_DDS_PREFERENCE = "config_disable_dds_preference";
public SimSettings() {
super(DISALLOW_CONFIG_SIM);
}
@Override
protected int getMetricsCategory() {
return MetricsLogger.SIM;
}
@Override
public void onCreate(final Bundle bundle) {
super.onCreate(bundle);
mContext = getActivity();
mSubscriptionManager = SubscriptionManager.from(getActivity());
final TelephonyManager tm =
(TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
addPreferencesFromResource(R.xml.sim_settings);
mPrimarySubSelect = (Preference) findPreference(KEY_PRIMARY_SUB_SELECT);
mNumSlots = tm.getSimCount();
mSimCards = (PreferenceCategory)findPreference(SIM_CARD_CATEGORY);
mMobileNetwork = (PreferenceCategory) findPreference(MOBILE_NETWORK_CATEGORY);
mAvailableSubInfos = new ArrayList<SubscriptionInfo>(mNumSlots);
mSelectableSubInfos = new ArrayList<SubscriptionInfo>();
SimSelectNotification.cancelNotification(getActivity());
IntentFilter intentFilter = new IntentFilter(ACTION_UICC_MANUAL_PROVISION_STATUS_CHANGED);
mContext.registerReceiver(mReceiver, intentFilter);
}
@Override
public void onDestroy() {
mContext.unregisterReceiver(mReceiver);
Log.d(TAG,"on onDestroy");
super.onDestroy();
}
private final SubscriptionManager.OnSubscriptionsChangedListener mOnSubscriptionsChangeListener
= new SubscriptionManager.OnSubscriptionsChangedListener() {
@Override
public void onSubscriptionsChanged() {
if (DBG) log("onSubscriptionsChanged:");
Activity activity = getActivity();
if (activity != null && !activity.isFinishing()) {
updateSubscriptions();
}
}
};
private void updateSubscriptions() {
mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
for (int i = 0; i < mNumSlots; ++i) {
Preference pref = mSimCards.findPreference("sim" + i);
if (pref instanceof SimPreference) {
mSimCards.removePreference(pref);
}
}
mMobileNetwork.removeAll();
mAvailableSubInfos.clear();
mSelectableSubInfos.clear();
for (int i = 0; i < mNumSlots; ++i) {
final SubscriptionInfo sir = mSubscriptionManager
.getActiveSubscriptionInfoForSimSlotIndex(i);
int subscriptionId = sir != null ?
sir.getSubscriptionId() :
SubscriptionManager.INVALID_SUBSCRIPTION_ID;
SimPreference simPreference = new SimEnablerPreference(mContext, sir, i);
simPreference.setOrder(i-mNumSlots);
mSimCards.addPreference(simPreference);
mAvailableSubInfos.add(sir);
if (sir != null && (isSubProvisioned(i))) {
mSelectableSubInfos.add(sir);
}
Intent mobileNetworkIntent = new Intent();
mobileNetworkIntent.setComponent(new ComponentName(
"com.android.phone", "com.android.phone.MobileNetworkSettings"));
SubscriptionManager.putPhoneIdAndSubIdExtra(mobileNetworkIntent, i, subscriptionId);
Preference mobileNetworkPref = new Preference(getActivity());
mobileNetworkPref.setTitle(
getString(R.string.sim_mobile_network_settings_title, (i + 1)));
mobileNetworkPref.setIntent(mobileNetworkIntent);
mobileNetworkPref.setEnabled(
subscriptionId != SubscriptionManager.INVALID_SUBSCRIPTION_ID);
mMobileNetwork.addPreference(mobileNetworkPref);
}
updateAllOptions();
}
private void updateAllOptions() {
updateSimSlotValues();
updateActivitesCategory();
}
private void updateSimSlotValues() {
final int prefSize = mSimCards.getPreferenceCount();
for (int i = 0; i < prefSize; ++i) {
Preference pref = mSimCards.getPreference(i);
if (pref instanceof SimPreference) {
((SimPreference)pref).update();
}
}
}
private void updateActivitesCategory() {
updateCellularDataValues();
updateCallValues();
updateSmsValues();
}
private void updateSmsValues() {
final Preference simPref = findPreference(KEY_SMS);
simPref.setTitle(R.string.sms_messages_title);
boolean isSMSPrompt = false;
SubscriptionInfo sir = mSubscriptionManager.getActiveSubscriptionInfo(
mSubscriptionManager.getDefaultSmsSubId());
try {
isSMSPrompt = mExtTelephony.isSMSPromptEnabled();
} catch (RemoteException ex) {
loge("RemoteException @isSMSPromptEnabled" + ex);
} catch (NullPointerException ex) {
loge("NullPointerException @isSMSPromptEnabled" + ex);
}
// External telephony interfaces may not exist, fall back to our impl
if (mExtTelephony == null) {
isSMSPrompt = SmsManager.getDefault().isSMSPromptEnabled();
}
log("[updateSmsValues] isSMSPrompt: " + isSMSPrompt);
if (isSMSPrompt || sir == null) {
simPref.setSummary(mContext.getResources().getString(
R.string.sim_calls_ask_first_prefs_title));
} else {
simPref.setSummary(sir.getDisplayName());
}
simPref.setEnabled(mSelectableSubInfos.size() > 1);
}
private void updateCellularDataValues() {
final Preference simPref = findPreference(KEY_CELLULAR_DATA);
final SubscriptionInfo sir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
simPref.setTitle(R.string.cellular_data_title);
if (DBG) log("[updateCellularDataValues] mSubInfoList=" + mSubInfoList);
if (sir != null) {
simPref.setSummary(sir.getDisplayName());
} else if (sir == null) {
simPref.setSummary(R.string.sim_selection_required_pref);
}
boolean callStateIdle = isCallStateIdle();
final boolean ecbMode = SystemProperties.getBoolean(
TelephonyProperties.PROPERTY_INECM_MODE, false);
// Enable data preference in msim mode and call state idle
simPref.setEnabled((mSelectableSubInfos.size() > 1) && !disableDds()
&& callStateIdle && !ecbMode);
}
private void updateCallValues() {
final Preference simPref = findPreference(KEY_CALLS);
final TelecomManager telecomManager = TelecomManager.from(mContext);
final PhoneAccountHandle phoneAccountHandle =
telecomManager.getUserSelectedOutgoingPhoneAccount();
final List<PhoneAccountHandle> allPhoneAccounts =
telecomManager.getCallCapablePhoneAccounts();
simPref.setTitle(R.string.calls_title);
PhoneAccount phoneAccount = null;
if (phoneAccountHandle != null) {
phoneAccount = telecomManager.getPhoneAccount(phoneAccountHandle);
}
simPref.setSummary(phoneAccount == null
? mContext.getResources().getString(R.string.sim_calls_ask_first_prefs_title)
: (String)phoneAccount.getLabel());
simPref.setEnabled(allPhoneAccounts.size() > 1);
}
@Override
public void onResume() {
super.onResume();
mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
initLTEPreference();
updateSubscriptions();
listen();
}
@Override
public void onPause() {
super.onPause();
mSubscriptionManager.removeOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
unRegisterPhoneStateListener();
for (int i = 0; i < mSimCards.getPreferenceCount(); ++i) {
Preference pref = mSimCards.getPreference(i);
if (pref instanceof SimEnablerPreference) {
// Calling cleanUp() here to dismiss/cleanup any pending dialog exists.
((SimEnablerPreference)pref).cleanUpPendingDialogs();
}
}
}
@Override
public boolean onPreferenceTreeClick(final PreferenceScreen preferenceScreen,
final Preference preference) {
final Context context = mContext;
Intent intent = new Intent(context, SimDialogActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (preference instanceof SimPreference) {
Intent newIntent = new Intent(context, SimPreferenceDialog.class);
newIntent.putExtra(EXTRA_SLOT_ID, ((SimPreference)preference).getSlotId());
startActivity(newIntent);
return true;
} else if (findPreference(KEY_CELLULAR_DATA) == preference) {
intent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.DATA_PICK);
context.startActivity(intent);
return true;
} else if (findPreference(KEY_CALLS) == preference) {
intent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.CALLS_PICK);
context.startActivity(intent);
return true;
} else if (findPreference(KEY_SMS) == preference) {
intent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.SMS_PICK);
context.startActivity(intent);
return true;
}
return false;
}
private void loge(String msg) {
if (DBG) Log.e(TAG + "message", msg);
}
private void simEnablerUpdate() {
if (isAdded()) {
updateAllOptions();
} else {
needUpdate = true;
}
}
private class SimPreference extends Preference {
SubscriptionInfo mSubInfoRecord;
int mSlotId;
Context mContext;
public SimPreference(Context context, SubscriptionInfo subInfoRecord, int slotId) {
super(context);
mContext = context;
mSubInfoRecord = subInfoRecord;
mSlotId = slotId;
setKey("sim" + mSlotId);
update();
}
public SimPreference (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void update() {
final Resources res = mContext.getResources();
setTitle(String.format(mContext.getResources()
.getString(R.string.sim_editor_title), (mSlotId + 1)));
if (mSubInfoRecord != null) {
if (TextUtils.isEmpty(getPhoneNumber(mSubInfoRecord))) {
setSummary(mSubInfoRecord.getDisplayName());
} else {
setSummary(mSubInfoRecord.getDisplayName() + " - " +
getPhoneNumber(mSubInfoRecord));
setEnabled(true);
}
setIcon(new BitmapDrawable(res, (mSubInfoRecord.createIconBitmap(mContext))));
} else {
setSummary(R.string.sim_slot_empty);
setFragment(null);
setEnabled(false);
}
}
private int getSlotId() {
return mSlotId;
}
@Override
protected void onAttachedToActivity() {
super.onAttachedToActivity();
if (needUpdate) {
needUpdate = false;
updateAllOptions();
}
}
}
// This is to show SIM Enable options on/off on UI for user selection.
// User can activate/de-activate through SIM on/off options.
private class SimEnablerPreference extends SimPreference implements OnCheckedChangeListener {
private String TAG = "SimEnablerPreference";
private static final boolean DBG = true;
private static final int EVT_UPDATE = 1;
private static final int EVT_SHOW_RESULT_DLG = 2;
private static final int EVT_SHOW_PROGRESS_DLG = 3;
private static final int EVT_PROGRESS_DLG_TIME_OUT = 4;
private static final int CONFIRM_ALERT_DLG_ID = 1;
private static final int ERROR_ALERT_DLG_ID = 2;
private static final int RESULT_ALERT_DLG_ID = 3;
private static final int REQUEST_SUCCESS = 0;
private static final int GENERIC_FAILURE = -1;
private static final int INVALID_INPUT = -2;
private static final int REQUEST_IN_PROGRESS = -3;
private static final String DISPLAY_NUMBERS_TYPE = "display_numbers_type";
private SubscriptionInfo mSir;
private boolean mCurrentUiccProvisionState;
private boolean mIsChecked;
private boolean mCmdInProgress = false;
private int mSwitchVisibility = View.VISIBLE;
private CompoundButton mSwitch;
//Delay for progress dialog to dismiss
private static final int PROGRESS_DLG_TIME_OUT = 30000;
private static final int MSG_DELAY_TIME = 2000;
private IExtTelephony mExtTelephony;
public SimEnablerPreference(Context context, SubscriptionInfo sir, int slotId) {
super(context, (AttributeSet)null,
com.android.internal.R.attr.checkBoxPreferenceStyle);
logd("Contructor..Enter");
mContext = context;
mSlotId = slotId;
mSir = sir;
mSubInfoRecord = sir;
if (mContext.getResources().getBoolean(R.bool.config_custom_multi_sim_checkbox)) {
setWidgetLayoutResource(R.layout.custom_sim_checkbox);
} else {
setWidgetLayoutResource(R.layout.custom_sim_switch);
}
mExtTelephony = IExtTelephony.Stub.asInterface(ServiceManager.getService("extphone"));
setSwitchVisibility(View.VISIBLE);
setKey("sim" + mSlotId);
update();
}
private void sendMessage(int event, Handler handler, int delay) {
Message message = handler.obtainMessage(event);
handler.sendMessageDelayed(message, delay);
}
private void sendMessage(int event, Handler handler, int delay, int arg1, int arg2) {
Message message = handler.obtainMessage(event, arg1, arg2);
handler.sendMessageDelayed(message, delay);
}
private boolean hasCard() {
return TelephonyManager.getDefault().hasIccCard(mSlotId);
}
private boolean isAirplaneModeOn() {
return (Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0);
}
private int getProvisionStatus(int slotId) {
return mUiccProvisionStatus[slotId];
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
logd("onBindView....");
mSwitch = (CompoundButton) view.findViewById(R.id.sub_switch_widget);
mSwitch.setOnCheckedChangeListener(this);
update();
// now use other config screen to active/deactive sim card\
mSwitch.setVisibility(mSwitchVisibility);
// Disable manual provisioning option to user when
// device is in Airplane mode. Hide it if the extphone framework
// is not present, as the operation relies on said framework.
if (mExtTelephony == null ||
!mContext.getResources().getBoolean(R.bool.config_enableManualSubProvisioning)) {
mSwitch.setVisibility(View.GONE);
} else {
mSwitch.setVisibility(View.VISIBLE);
mSwitch.setEnabled(!isAirplaneModeOn() && isCurrentSubValid());
}
}
@Override
public void update() {
final Resources res = mContext.getResources();
logd("update()" + mSir);
try {
//get current provision state of the SIM.
mUiccProvisionStatus[mSlotId] =
mExtTelephony.getCurrentUiccCardProvisioningStatus(mSlotId);
} catch (RemoteException ex) {
mUiccProvisionStatus[mSlotId] = INVALID_STATE;
loge("Failed to get pref, slotId: "+ mSlotId +" Exception: " + ex);
} catch (NullPointerException ex) {
mUiccProvisionStatus[mSlotId] = INVALID_STATE;
loge("Failed to get pref, slotId: "+ mSlotId +" Exception: " + ex);
}
if (mUiccProvisionStatus[mSlotId] == INVALID_STATE) {
mUiccProvisionStatus[mSlotId] = PROVISIONED;
}
boolean isSubValid = isCurrentSubValid();
setEnabled(isSubValid);
logd("update: isSubValid " + isSubValid + " provision status["
+ mSlotId + "] = " + mUiccProvisionStatus[mSlotId]);
setTitle(res.getString(R.string.sim_card_number_title, mSlotId + 1));
if (isSubValid) {
updateSummary();
setIcon(new BitmapDrawable(res, (mSir.createIconBitmap(mContext))));
} else {
setSummary(res.getString(R.string.sim_slot_empty));
}
}
// This method returns true if SubScription record corresponds to this
// Preference screen has a valid SIM and slot index/SubId.
private boolean isCurrentSubValid() {
boolean isSubValid = false;
if (hasCard()) {
List<SubscriptionInfo> sirList =
mSubscriptionManager.getActiveSubscriptionInfoList();
if (sirList != null ) {
for (SubscriptionInfo sir : sirList) {
if (sir != null && mSlotId == sir.getSimSlotIndex()) {
mSir = sir;
break;
}
}
if (mSir != null &&
SubscriptionManager.isValidSubscriptionId(mSir.getSubscriptionId()) &&
mSir.getSimSlotIndex() >= 0 &&
getProvisionStatus(mSir.getSimSlotIndex()) >= 0) {
isSubValid = true;
}
}
}
return isSubValid;
}
public void setSwitchVisibility (int visibility) {
mSwitchVisibility = visibility;
}
// Based on the received SIM provision state this method
// sets the check box on Sim Preference UI and updates new
// state to mCurrentUiccProvisionState.
private void setChecked(boolean uiccProvisionState) {
logd("setChecked: uiccProvisionState " + uiccProvisionState + "sir:" + mSir);
if (mSwitch != null) {
mSwitch.setOnCheckedChangeListener(null);
// Do not update update checkstatus again in progress
if (!mCmdInProgress) {
mSwitch.setChecked(uiccProvisionState);
}
mSwitch.setOnCheckedChangeListener(this);
mCurrentUiccProvisionState = uiccProvisionState;
}
}
private void updateSummary() {
Resources res = mContext.getResources();
String summary;
boolean isActivated = (getProvisionStatus(mSir.getSimSlotIndex()) == PROVISIONED);
logd("updateSummary: subId " + mSir.getSubscriptionId() + " isActivated = "
+ isActivated + " slot id = " + mSlotId);
String displayName = mSir == null ? "SIM" : (String)mSir.getDisplayName();
if (isActivated) {
summary = displayName;
if (!TextUtils.isEmpty(mSir.getNumber())) {
summary = displayName + " - " + mSir.getNumber();
}
} else {
summary = mContext.getString(R.string.sim_enabler_summary, displayName,
res.getString(hasCard() ? R.string.sim_disabled : R.string.sim_missing));
}
setSummary(summary);
setChecked(isActivated);
}
/**
* get number of Subs provisioned on the device
* @param context
* @return
*/
public int getNumOfSubsProvisioned() {
int activeSubInfoCount = 0;
List<SubscriptionInfo> subInfoLists =
mSubscriptionManager.getActiveSubscriptionInfoList();
if (subInfoLists != null) {
for (SubscriptionInfo subInfo : subInfoLists) {
if (getProvisionStatus(subInfo.getSimSlotIndex())
== PROVISIONED) activeSubInfoCount++;
}
}
return activeSubInfoCount;
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mIsChecked = isChecked;
logd("onClick: " + isChecked);
handleUserRequest();
}
// This internal method called when user changes preference from UI
// 1. For activation/deactivation request from User, if device in APM mode
// OR if voice call active on any SIM it dispay error dialog and returns.
// 2. For deactivation request it returns error dialog if only one SUB in
// active state.
// 3. In other cases it sends user request to framework.
synchronized private void handleUserRequest() {
if (isAirplaneModeOn()) {
// do nothing but warning
logd("APM is on, EXIT!");
showAlertDialog(ERROR_ALERT_DLG_ID, R.string.sim_enabler_airplane_on);
return;
}
for (int i = 0; i < mPhoneCount; i++) {
int[] subId = SubscriptionManager.getSubId(i);
//when voice call in progress, subscription can't be activate/deactivate.
if (TelephonyManager.getDefault().getCallState(subId[0])
!= TelephonyManager.CALL_STATE_IDLE) {
logd("Call state for phoneId: " + i + " is not idle, EXIT!");
showAlertDialog(ERROR_ALERT_DLG_ID, R.string.sim_enabler_in_call);
return;
}
}
if (!mIsChecked) {
if (getNumOfSubsProvisioned() > 1) {
logd("More than one sub is active, Deactivation possible.");
showAlertDialog(CONFIRM_ALERT_DLG_ID, 0);
} else {
logd("Only one sub is active. Deactivation not possible.");
showAlertDialog(ERROR_ALERT_DLG_ID, R.string.sim_enabler_both_inactive);
return;
}
} else {
logd("Activate the sub");
sendUiccProvisioningRequest();
}
}
private void sendUiccProvisioningRequest() {
if (!mSwitch.isEnabled()) {
return;
}
new SimEnablerDisabler().execute();
}
private class SimEnablerDisabler extends AsyncTask<Void, Void, Integer> {
int newProvisionedState = NOT_PROVISIONED;
@Override
protected void onPreExecute() {
super.onPreExecute();
mCmdInProgress = true;
showProgressDialog();
setEnabled(false);
}
@Override
protected Integer doInBackground(Void... params) {
int result = -1;
newProvisionedState = NOT_PROVISIONED;
try {
if (mIsChecked) {
result = mExtTelephony.activateUiccCard(mSir.getSimSlotIndex());
newProvisionedState = PROVISIONED;
} else {
result = mExtTelephony.deactivateUiccCard(mSir.getSimSlotIndex());
}
} catch (RemoteException ex) {
loge("Activate sub failed " + result + " phoneId " + mSir.getSimSlotIndex());
} catch (NullPointerException ex) {
loge("Failed to activate sub Exception: " + ex);
}
return result;
}
@Override
protected void onPostExecute(Integer result) {
processSetUiccDone(result.intValue(), newProvisionedState);
}
}
private void processSetUiccDone(int result, int newProvisionedState) {
sendMessage(EVT_UPDATE, mHandler, MSG_DELAY_TIME);
sendMessage(EVT_SHOW_RESULT_DLG, mHandler, MSG_DELAY_TIME, result, newProvisionedState);
mCmdInProgress = false;
}
private void showAlertDialog(int dialogId, int msgId) {
String title = mSir == null ? "SUB" : mSir.getDisplayName().toString();
// Confirm only one AlertDialog instance to show.
dismissDialog(sAlertDialog);
dismissDialog(sProgressDialog);
AlertDialog.Builder builder = new AlertDialog.Builder(mContext)
.setTitle(title);
switch(dialogId) {
case CONFIRM_ALERT_DLG_ID:
String message;
if (mContext.getResources().getBoolean(
R.bool.confirm_to_switch_data_service)) {
if (SubscriptionManager.getDefaultDataSubId() ==
mSir.getSubscriptionId()) {
message = mContext.getString(
R.string.sim_enabler_need_switch_data_service,
getProvisionedSlotId(mContext));
} else {
message = mContext.getString(R.string.sim_enabler_need_disable_sim);
}
builder.setTitle(R.string.sim_enabler_will_disable_sim_title);
} else {
message = mContext.getString(R.string.sim_enabler_need_disable_sim);
}
builder.setMessage(message);
builder.setPositiveButton(android.R.string.ok, mDialogClickListener);
builder.setNegativeButton(android.R.string.no, mDialogClickListener);
builder.setOnCancelListener(mDialogCanceListener);
break;
case ERROR_ALERT_DLG_ID:
builder.setMessage(mContext.getString(msgId));
builder.setNeutralButton(android.R.string.ok, mDialogClickListener);
builder.setCancelable(false);
break;
case RESULT_ALERT_DLG_ID:
String msg = mCurrentUiccProvisionState ?
mContext.getString(R.string.sub_activate_success) :
mContext.getString(R.string.sub_deactivate_success);
builder.setMessage(msg);
builder.setNeutralButton(android.R.string.ok, null);
break;
default:
break;
}
sAlertDialog = builder.create();
sAlertDialog.setCanceledOnTouchOutside(false);
sAlertDialog.show();
}
private int getProvisionedSlotId(Context context) {
int activeSlotId = -1;
List<SubscriptionInfo> subInfoLists =
mSubscriptionManager.getActiveSubscriptionInfoList();
if (subInfoLists != null) {
for (SubscriptionInfo subInfo : subInfoLists) {
if (getProvisionStatus(subInfo.getSimSlotIndex()) == PROVISIONED
&& subInfo.getSubscriptionId() != mSir.getSubscriptionId())
activeSlotId = subInfo.getSimSlotIndex() + 1;
}
}
return activeSlotId;
}
private void showProgressDialog() {
String title = mSir == null ? "SUB" : mSir.getDisplayName().toString();
String msg = mContext.getString(mIsChecked ? R.string.sim_enabler_enabling
: R.string.sim_enabler_disabling);
dismissDialog(sProgressDialog);
sProgressDialog = new ProgressDialog(mContext);
sProgressDialog.setIndeterminate(true);
sProgressDialog.setTitle(title);
sProgressDialog.setMessage(msg);
sProgressDialog.setCancelable(false);
sProgressDialog.setCanceledOnTouchOutside(false);
sProgressDialog.show();
sendMessage(EVT_PROGRESS_DLG_TIME_OUT, mHandler, PROGRESS_DLG_TIME_OUT);
}
private void dismissDialog(Dialog dialog) {
if((dialog != null) && (dialog.isShowing())) {
dialog.dismiss();
dialog = null;
}
}
public void cleanUpPendingDialogs() {
dismissDialog(sProgressDialog);
dismissDialog(sAlertDialog);
}
private DialogInterface.OnClickListener mDialogClickListener = new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
dismissDialog(sAlertDialog);
sendUiccProvisioningRequest();
} else if (which == DialogInterface.BUTTON_NEGATIVE) {
update();
} else if (which == DialogInterface.BUTTON_NEUTRAL) {
update();
}
}
};
private DialogInterface.OnCancelListener mDialogCanceListener = new DialogInterface
.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
update();
}
};
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case EVT_UPDATE:
simEnablerUpdate();
case EVT_SHOW_RESULT_DLG:
int result = msg.arg1;
int newProvisionedState = msg.arg2;
logd("EVT_SHOW_RESULT_DLG result: " + result +
" new provisioned state " + newProvisionedState);
update();
if (result != REQUEST_SUCCESS) {
int msgId = (newProvisionedState == PROVISIONED) ?
R.string.sub_activate_failed :
R.string.sub_deactivate_failed;
showAlertDialog(ERROR_ALERT_DLG_ID, msgId);
} else {
showAlertDialog(RESULT_ALERT_DLG_ID, 0);
}
mHandler.removeMessages(EVT_PROGRESS_DLG_TIME_OUT);
break;
case EVT_SHOW_PROGRESS_DLG:
logd("EVT_SHOW_PROGRESS_DLG");
showProgressDialog();
break;
case EVT_PROGRESS_DLG_TIME_OUT:
logd("EVT_PROGRESS_DLG_TIME_OUT");
dismissDialog(sProgressDialog);
// Must update UI when time out
update();
break;
default:
break;
}
}
};
private void logd(String msg) {
if (DBG) Log.d(TAG + "(" + mSlotId + ")", msg);
}
private void loge(String msg) {
Log.e(TAG + "(" + mSlotId + ")", msg);
}
}
// Returns the line1Number. Line1number should always be read from TelephonyManager since it can
// be overridden for display purposes.
private String getPhoneNumber(SubscriptionInfo info) {
final TelephonyManager tm =
(TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getLine1NumberForSubscriber(info.getSubscriptionId());
}
private void log(String s) {
Log.d(TAG, s);
}
/**
* For search
*/
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
boolean enabled) {
ArrayList<SearchIndexableResource> result =
new ArrayList<SearchIndexableResource>();
if (Utils.showSimCardTile(context)) {
SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = R.xml.sim_settings;
result.add(sir);
}
return result;
}
};
// Internal utility, returns true if Uicc card
// corresponds to given slotId is provisioned.
private boolean isSubProvisioned(int slotId) {
boolean retVal = false;
if (mUiccProvisionStatus[slotId] == PROVISIONED) retVal = true;
return retVal;
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "Intent received: " + action);
if (ACTION_UICC_MANUAL_PROVISION_STATUS_CHANGED.equals(action)) {
int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY,
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
int newProvisionedState = intent.getIntExtra(EXTRA_NEW_PROVISION_STATE,
NOT_PROVISIONED);
updateSubscriptions();
Log.d(TAG, "Received ACTION_UICC_MANUAL_PROVISION_STATUS_CHANGED on phoneId: "
+ phoneId + " new sub state " + newProvisionedState);
}
}
};
// When primarycard feature enabled this provides menu option for user
// to view/select current primary slot.
private void initLTEPreference() {
boolean isPrimarySubFeatureEnable =
SystemProperties.getBoolean("persist.radio.primarycard", false);
boolean primarySetable = Settings.Global.getInt(mContext.getContentResolver(),
CONFIG_PRIMARY_SUB_SETABLE, 0) == 1;
log("isPrimarySubFeatureEnable :" + isPrimarySubFeatureEnable +
" primarySetable :" + primarySetable);
if (!isPrimarySubFeatureEnable || !primarySetable) {
final PreferenceCategory simActivities =
(PreferenceCategory) findPreference(SIM_ACTIVITIES_CATEGORY);
simActivities.removePreference(mPrimarySubSelect);
return;
}
int currentPrimarySlot = Settings.Global.getInt(mContext.getContentResolver(),
CONFIG_CURRENT_PRIMARY_SUB, SubscriptionManager.INVALID_SIM_SLOT_INDEX);
boolean isManualMode = Settings.Global.getInt(mContext.getContentResolver(),
CONFIG_LTE_SUB_SELECT_MODE, 1) == 0;
log("init LTE primary slot : " + currentPrimarySlot + " isManualMode :" + isManualMode);
if (SubscriptionManager.isValidSlotId(currentPrimarySlot)) {
final SubscriptionInfo subInfo = mSubscriptionManager
.getActiveSubscriptionInfoForSimSlotIndex(currentPrimarySlot);
CharSequence lteSummary = (subInfo == null ) ? null : subInfo.getDisplayName();
mPrimarySubSelect.setSummary(lteSummary);
} else {
mPrimarySubSelect.setSummary("");
}
mPrimarySubSelect.setEnabled(isManualMode);
}
private boolean disableDds() {
boolean disableDds = Settings.Global.getInt(mContext.getContentResolver(),
CONFIG_DISABLE_DDS_PREFERENCE, 0) == 1;
log(" config disable dds = " + disableDds);
return disableDds;
}
private void listen() {
TelephonyManager tm =
(TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
if (mSelectableSubInfos.size() > 1) {
Log.d(TAG, "Register for call state change");
for (int i = 0; i < mPhoneCount; i++) {
int subId = mSelectableSubInfos.get(i).getSubscriptionId();
tm.listen(getPhoneStateListener(i, subId),
PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
private void unRegisterPhoneStateListener() {
TelephonyManager tm =
(TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
for (int i = 0; i < mPhoneCount; i++) {
if (mPhoneStateListener[i] != null) {
tm.listen(mPhoneStateListener[i], PhoneStateListener.LISTEN_NONE);
mPhoneStateListener[i] = null;
}
}
}
private PhoneStateListener getPhoneStateListener(int phoneId, int subId) {
// Disable Sim selection for Data when voice call is going on as changing the default data
// sim causes a modem reset currently and call gets disconnected
// ToDo : Add subtext on disabled preference to let user know that default data sim cannot
// be changed while call is going on
final int i = phoneId;
mPhoneStateListener[phoneId] = new PhoneStateListener(subId) {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (DBG) log("PhoneStateListener.onCallStateChanged: state=" + state);
mCallState[i] = state;
updateCellularDataValues();
}
};
return mPhoneStateListener[phoneId];
}
private boolean isCallStateIdle() {
boolean callStateIdle = true;
for (int i = 0; i < mCallState.length; i++) {
if (TelephonyManager.CALL_STATE_IDLE != mCallState[i]) {
callStateIdle = false;
}
}
Log.d(TAG, "isCallStateIdle " + callStateIdle);
return callStateIdle;
}
}
|