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
|
/*
* 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.content.Intent;
import android.os.Bundle;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import com.android.settings.R;
import java.util.List;
/**
* This activity provides singleton semantics per dialog type for showing various kinds of
* dialogs asking the user to make choices about which SIM to use for various services
* (calls, SMS, and data).
*/
public class SimDialogActivity extends FragmentActivity {
private static String TAG = "SimDialogActivity";
public static String PREFERRED_SIM = "preferred_sim";
public static String DIALOG_TYPE_KEY = "dialog_type";
// sub ID returned from startActivityForResult
public static String RESULT_SUB_ID = "result_sub_id";
public static final int INVALID_PICK = -1;
public static final int DATA_PICK = 0;
public static final int CALLS_PICK = 1;
public static final int SMS_PICK = 2;
public static final int PREFERRED_PICK = 3;
// Show the "select SMS subscription" dialog, but don't save as default, just return a result
public static final int SMS_PICK_FOR_MESSAGE = 4;
// Dismiss the current dialog and finish the activity.
public static final int PICK_DISMISS = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addSystemFlags(
WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
showOrUpdateDialog();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
showOrUpdateDialog();
}
private void showOrUpdateDialog() {
final int dialogType = getIntent().getIntExtra(DIALOG_TYPE_KEY, INVALID_PICK);
if (dialogType == PICK_DISMISS) {
finishAndRemoveTask();
return;
}
final String tag = Integer.toString(dialogType);
final FragmentManager fragmentManager = getSupportFragmentManager();
SimDialogFragment fragment = (SimDialogFragment) fragmentManager.findFragmentByTag(tag);
if (fragment == null) {
fragment = createFragment(dialogType);
fragment.show(fragmentManager, tag);
} else {
fragment.updateDialog();
}
}
private SimDialogFragment createFragment(int dialogType) {
switch (dialogType) {
case DATA_PICK:
return SimListDialogFragment.newInstance(dialogType, R.string.select_sim_for_data,
false /* includeAskEveryTime */);
case CALLS_PICK:
return CallsSimListDialogFragment.newInstance(dialogType,
R.string.select_sim_for_calls,
true /* includeAskEveryTime */);
case SMS_PICK:
return SimListDialogFragment.newInstance(dialogType, R.string.select_sim_for_sms,
true /* includeAskEveryTime */);
case PREFERRED_PICK:
if (!getIntent().hasExtra(PREFERRED_SIM)) {
throw new IllegalArgumentException("Missing required extra " + PREFERRED_SIM);
}
return PreferredSimDialogFragment.newInstance();
case SMS_PICK_FOR_MESSAGE:
return SimListDialogFragment.newInstance(dialogType, R.string.select_sim_for_sms,
false /* includeAskEveryTime */);
default:
throw new IllegalArgumentException("Invalid dialog type " + dialogType + " sent.");
}
}
public void onSubscriptionSelected(int dialogType, int subId) {
if (getSupportFragmentManager().findFragmentByTag(Integer.toString(dialogType)) == null) {
Log.w(TAG, "onSubscriptionSelected ignored because stored fragment was null");
return;
}
switch (dialogType) {
case DATA_PICK:
setDefaultDataSubId(subId);
break;
case CALLS_PICK:
setDefaultCallsSubId(subId);
break;
case SMS_PICK:
setDefaultSmsSubId(subId);
break;
case PREFERRED_PICK:
setPreferredSim(subId);
break;
case SMS_PICK_FOR_MESSAGE:
// Don't set a default here.
// The caller has created this dialog waiting for a result.
Intent intent = new Intent();
intent.putExtra(RESULT_SUB_ID, subId);
setResult(Activity.RESULT_OK, intent);
break;
default:
throw new IllegalArgumentException(
"Invalid dialog type " + dialogType + " sent.");
}
}
public void onFragmentDismissed(SimDialogFragment simDialogFragment) {
final List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (fragments.size() == 1 && fragments.get(0) == simDialogFragment) {
finishAndRemoveTask();
}
}
private void setDefaultDataSubId(final int subId) {
final SubscriptionManager subscriptionManager = getSystemService(SubscriptionManager.class);
final TelephonyManager telephonyManager = getSystemService(
TelephonyManager.class).createForSubscriptionId(subId);
subscriptionManager.setDefaultDataSubId(subId);
telephonyManager.setDataEnabled(true);
Toast.makeText(this, R.string.data_switch_started, Toast.LENGTH_LONG).show();
}
private void setDefaultCallsSubId(final int subId) {
final PhoneAccountHandle phoneAccount = subscriptionIdToPhoneAccountHandle(subId);
final TelecomManager telecomManager = getSystemService(TelecomManager.class);
telecomManager.setUserSelectedOutgoingPhoneAccount(phoneAccount);
}
private void setDefaultSmsSubId(final int subId) {
final SubscriptionManager subscriptionManager = getSystemService(SubscriptionManager.class);
subscriptionManager.setDefaultSmsSubId(subId);
}
private void setPreferredSim(final int subId) {
setDefaultDataSubId(subId);
setDefaultSmsSubId(subId);
setDefaultCallsSubId(subId);
}
private PhoneAccountHandle subscriptionIdToPhoneAccountHandle(final int subId) {
final TelecomManager telecomManager = getSystemService(TelecomManager.class);
final TelephonyManager telephonyManager = getSystemService(TelephonyManager.class);
for (PhoneAccountHandle handle : telecomManager.getCallCapablePhoneAccounts()) {
if (subId == telephonyManager.getSubscriptionId(handle)) {
return handle;
}
}
return null;
}
}
|