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
|
/*
* Copyright (C) 2021 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.Context;
import android.content.Intent;
import android.icu.text.MessageFormat;
import android.os.Bundle;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settings.SidecarFragment;
import com.android.settings.network.SubscriptionUtil;
import com.android.settings.network.SwitchToEuiccSubscriptionSidecar;
import com.android.settings.network.SwitchToRemovableSlotSidecar;
import com.android.settings.network.UiccSlotUtil;
import com.google.android.setupdesign.GlifLayout;
import com.google.android.setupdesign.GlifRecyclerLayout;
import com.google.android.setupdesign.items.Dividable;
import com.google.android.setupdesign.items.IItem;
import com.google.android.setupdesign.items.Item;
import com.google.android.setupdesign.items.ItemGroup;
import com.google.android.setupdesign.items.RecyclerItemAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/** Activity to show a list of profiles for user to choose. */
public class ChooseSimActivity extends Activity
implements RecyclerItemAdapter.OnItemSelectedListener, SidecarFragment.Listener {
// Whether there is a pSIM profile in the selection list.
public static final String KEY_HAS_PSIM = "has_psim";
// After the user selects eSIM profile, whether continue to show Mobile Network Settings screen
// to select other preferences.
// Note: KEY_NO_PSIM_CONTINUE_TO_SETTINGS and mNoPsimContinueToSettings are not used for now
// for UI changes. We may use them in the future.
public static final String KEY_NO_PSIM_CONTINUE_TO_SETTINGS = "no_psim_continue_to_settings";
private static final String TAG = "ChooseSimActivity";
private static final int INDEX_PSIM = -1;
private static final String STATE_SELECTED_INDEX = "selected_index";
private static final String STATE_IS_SWITCHING = "is_switching";
private boolean mHasPsim;
private boolean mNoPsimContinueToSettings;
private ArrayList<SubscriptionInfo> mEmbeddedSubscriptions = new ArrayList<>();
private SubscriptionInfo mRemovableSubscription = null;
private ItemGroup mItemGroup;
private SwitchToEuiccSubscriptionSidecar mSwitchToEuiccSubscriptionSidecar;
private SwitchToRemovableSlotSidecar mSwitchToRemovableSlotSidecar;
// Variables have states.
private int mSelectedItemIndex;
private boolean mIsSwitching;
/** Returns an intent of {@code ChooseSimActivity} */
public static Intent getIntent(Context context) {
return new Intent(context, ChooseSimActivity.class);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choose_sim_activity);
Intent intent = getIntent();
mHasPsim = intent.getBooleanExtra(KEY_HAS_PSIM, false);
mNoPsimContinueToSettings = intent.getBooleanExtra(KEY_NO_PSIM_CONTINUE_TO_SETTINGS, false);
updateSubscriptions();
if (mEmbeddedSubscriptions.size() == 0) {
Log.e(TAG, "Unable to find available eSIM subscriptions.");
finish();
return;
}
if (savedInstanceState != null) {
mSelectedItemIndex = savedInstanceState.getInt(STATE_SELECTED_INDEX);
mIsSwitching = savedInstanceState.getBoolean(STATE_IS_SWITCHING);
}
GlifLayout layout = findViewById(R.id.glif_layout);
int subscriptionCount = mEmbeddedSubscriptions.size();
if (mHasPsim) { // Choose a number to use
subscriptionCount++;
}
layout.setHeaderText(getString(R.string.choose_sim_title));
MessageFormat msgFormat = new MessageFormat(
getString(R.string.choose_sim_text),
Locale.getDefault());
Map<String, Object> arguments = new HashMap<>();
arguments.put("count", subscriptionCount);
layout.setDescriptionText(msgFormat.format(arguments));
displaySubscriptions();
mSwitchToRemovableSlotSidecar = SwitchToRemovableSlotSidecar.get(getFragmentManager());
mSwitchToEuiccSubscriptionSidecar =
SwitchToEuiccSubscriptionSidecar.get(getFragmentManager());
}
@Override
public void onResume() {
super.onResume();
mSwitchToRemovableSlotSidecar.addListener(this);
mSwitchToEuiccSubscriptionSidecar.addListener(this);
}
@Override
public void onPause() {
mSwitchToEuiccSubscriptionSidecar.removeListener(this);
mSwitchToRemovableSlotSidecar.removeListener(this);
super.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_SELECTED_INDEX, mSelectedItemIndex);
outState.putBoolean(STATE_IS_SWITCHING, mIsSwitching);
super.onSaveInstanceState(outState);
}
@Override
public void onItemSelected(IItem item) {
if (mIsSwitching) {
// If we already selected an item, do not try to switch to another one.
return;
}
mIsSwitching = true;
Item subItem = (Item) item;
subItem.setSummary(getString(R.string.choose_sim_activating));
mSelectedItemIndex = subItem.getId();
if (mSelectedItemIndex == INDEX_PSIM) {
Log.i(TAG, "Ready to switch to pSIM slot.");
mSwitchToRemovableSlotSidecar.run(UiccSlotUtil.INVALID_PHYSICAL_SLOT_ID);
} else {
Log.i(TAG, "Ready to switch to eSIM subscription with index: " + mSelectedItemIndex);
mSwitchToEuiccSubscriptionSidecar.run(
mEmbeddedSubscriptions.get(mSelectedItemIndex).getSubscriptionId());
}
}
@Override
public void onStateChange(SidecarFragment fragment) {
if (fragment == mSwitchToRemovableSlotSidecar) {
switch (mSwitchToRemovableSlotSidecar.getState()) {
case SidecarFragment.State.SUCCESS:
mSwitchToRemovableSlotSidecar.reset();
Log.i(TAG, "Switch slot successfully.");
SubscriptionManager subMgr = getSystemService(SubscriptionManager.class);
if (subMgr.canDisablePhysicalSubscription()) {
SubscriptionInfo removableSub =
SubscriptionUtil.getFirstRemovableSubscription(this);
if (removableSub != null) {
subMgr.setUiccApplicationsEnabled(
removableSub.getSubscriptionId(), true);
}
}
finish();
break;
case SidecarFragment.State.ERROR:
mSwitchToRemovableSlotSidecar.reset();
Log.e(TAG, "Failed to switch slot in ChooseSubscriptionsActivity.");
handleEnableRemovableSimError();
// We don't call finish() and just stay on this page.
break;
}
} else if (fragment == mSwitchToEuiccSubscriptionSidecar) {
switch (mSwitchToEuiccSubscriptionSidecar.getState()) {
case SidecarFragment.State.SUCCESS:
mSwitchToEuiccSubscriptionSidecar.reset();
if (mNoPsimContinueToSettings) {
// Currently, there shouldn't be a case that mNoPsimContinueToSettings is
// true. If this can be true in the future, we should finish() this page
// and direct to Settings page here.
Log.e(
TAG,
"mNoPsimContinueToSettings is true which is not supported for"
+ " now.");
} else {
Log.i(TAG, "User finished selecting eSIM profile.");
finish();
}
break;
case SidecarFragment.State.ERROR:
mSwitchToEuiccSubscriptionSidecar.reset();
Log.e(TAG, "Failed to switch subscription in ChooseSubscriptionsActivity.");
Item item = (Item) mItemGroup.getItemAt(mSelectedItemIndex);
item.setEnabled(false);
item.setSummary(getString(R.string.choose_sim_could_not_activate));
mIsSwitching = false;
// We don't call finish() and just stay on this page.
break;
}
}
}
private void displaySubscriptions() {
View rootView = findViewById(android.R.id.content);
GlifRecyclerLayout layout = rootView.findViewById(R.id.glif_layout);
RecyclerItemAdapter adapter = (RecyclerItemAdapter) layout.getAdapter();
adapter.setOnItemSelectedListener(this);
mItemGroup = (ItemGroup) adapter.getRootItemHierarchy();
// Display pSIM profile.
if (mHasPsim) {
Item item = new DisableableItem();
// Title
CharSequence title = null;
if (mRemovableSubscription != null) {
title =
SubscriptionUtil.getUniqueSubscriptionDisplayName(
mRemovableSubscription.getSubscriptionId(), this);
}
item.setTitle(TextUtils.isEmpty(title) ? getString(R.string.sim_card_label) : title);
if (mIsSwitching && mSelectedItemIndex == INDEX_PSIM) {
item.setSummary(getString(R.string.choose_sim_activating));
} else {
// Phone number
String phoneNumber =
SubscriptionUtil.getFormattedPhoneNumber(this, mRemovableSubscription);
item.setSummary(TextUtils.isEmpty(phoneNumber) ? "" : phoneNumber);
}
// pSIM profile has index -1.
item.setId(INDEX_PSIM);
mItemGroup.addChild(item);
}
// Display all eSIM profiles.
int index = 0;
for (SubscriptionInfo sub : mEmbeddedSubscriptions) {
Item item = new DisableableItem();
CharSequence title =
SubscriptionUtil.getUniqueSubscriptionDisplayName(
sub.getSubscriptionId(), this);
item.setTitle(TextUtils.isEmpty(title) ? sub.getDisplayName() : title);
if (mIsSwitching && mSelectedItemIndex == index) {
item.setSummary(getString(R.string.choose_sim_activating));
} else {
String phoneNumber = SubscriptionUtil.getFormattedPhoneNumber(this, sub);
item.setSummary(TextUtils.isEmpty(phoneNumber) ? "" : phoneNumber);
}
item.setId(index++);
mItemGroup.addChild(item);
}
}
private void updateSubscriptions() {
List<SubscriptionInfo> subscriptions =
SubscriptionUtil.getSelectableSubscriptionInfoList(this);
if (subscriptions != null) {
for (SubscriptionInfo sub : subscriptions) {
if (sub == null) {
continue;
}
if (sub.isEmbedded()) {
mEmbeddedSubscriptions.add(sub);
} else {
mRemovableSubscription = sub;
}
}
}
}
private void handleEnableRemovableSimError() {
// mSelectedItemIndex will be -1 if pSIM is selected. Since pSIM is always be
// listed at index 0, we change the itemIndex to 0 if pSIM is selected.
int itemIndex = mSelectedItemIndex == INDEX_PSIM ? 0 : mSelectedItemIndex;
Item item = (Item) mItemGroup.getItemAt(itemIndex);
item.setEnabled(false);
item.setSummary(getString(R.string.choose_sim_could_not_activate));
mIsSwitching = false;
}
class DisableableItem extends Item implements Dividable {
@Override
public boolean isDividerAllowedAbove() {
return true;
}
@Override
public boolean isDividerAllowedBelow() {
return true;
}
@Override
public void onBindView(View view) {
super.onBindView(view);
TextView title = view.findViewById(R.id.sud_items_title);
TextView summary = view.findViewById(R.id.sud_items_summary);
title.setEnabled(isEnabled());
summary.setEnabled(isEnabled());
}
}
}
|