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
|
/*
* Copyright (C) 2016 The CyanogenMod Project
* Copyright (C) 2017-2018,2020 The LineageOS 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.aicp.setupwizard;
import static com.aicp.setupwizard.SetupWizardApp.LOGV;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.ServiceState;
import android.telephony.SignalStrength;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.aicp.setupwizard.util.PhoneMonitor;
import com.aicp.setupwizard.util.SetupWizardUtils;
import java.util.List;
public class ChooseDataSimActivity extends BaseSetupWizardActivity {
public static final String TAG = ChooseDataSimActivity.class.getSimpleName();
private ViewGroup mPageView;
private ProgressBar mProgressBar;
private SparseArray<TextView> mNameViews;
private SparseArray<ImageView> mSignalViews;
private SparseArray<CheckBox> mCheckBoxes;
private SparseArray<View> mRows;
private SparseArray<SubscriptionInfo> mSubInfoRecords;
private SparseArray<SignalStrength> mSignalStrengths;
private SparseArray<ServiceState> mServiceStates;
private boolean mIsAttached = false;
private boolean mRadioReady = false;
private PhoneMonitor mPhoneMonitor;
private boolean mDisabledForSwitch = false;
private final Handler mHandler = new Handler();
private final Runnable mRadioReadyRunnable = () -> {
// If we timeout out waiting for the radio, Oh well.
if (!mRadioReady) {
mRadioReady = true;
checkForRadioReady();
}
};
private final View.OnClickListener mSetDataSimClickListener = view -> {
SubscriptionInfo subInfoRecord = (SubscriptionInfo) view.getTag();
if (subInfoRecord != null) {
changeDataSub(subInfoRecord);
}
};
private final PhoneMonitor.SubscriptionStateListener mSubscriptionStateListener =
new PhoneMonitor.SubscriptionStateListener() {
@Override
public void onServiceStateChanged(int subId, ServiceState serviceState) {
if (LOGV) {
Log.v(TAG, "onServiceStateChanged{" +
"subId='" + subId + '\'' +
", serviceState=" + serviceState.toString() +
'}');
}
SubscriptionInfo subInfoRecord =
mPhoneMonitor.getActiveSubscriptionInfo(subId);
mRadioReady = SetupWizardUtils.isRadioReady(
ChooseDataSimActivity.this, serviceState);
checkForRadioReady();
mServiceStates.put(subInfoRecord.getSimSlotIndex(), serviceState);
updateSignalStrength(subInfoRecord);
}
@Override
public void onDataConnectionStateChanged(int subId, int state, int networkType) {
if (LOGV) {
Log.v(TAG, "onDataConnectionStateChanged{" +
"subId='" + subId + '\'' +
", state=" + state +
'}');
}
onDefaultDataSubscriptionChanged(subId);
}
@Override
public void onDefaultDataSubscriptionChanged(int subId) {
if (LOGV) {
Log.v(TAG, "onDefaultDataSubscriptionChanged{" +
"subId='" + subId + '\'' +
'}');
}
final int currentDataSubId = SubscriptionManager.getDefaultDataSubscriptionId();
if (currentDataSubId != subId) {
updateCurrentDataSub();
hideProgress();
enableViews(true);
}
checkSimChangingState(currentDataSubId, subId);
}
@Override
public void onDefaultDataSubscriptionChangeRequested(int currentSubId,
int newSubId) {
if (LOGV) {
Log.v(TAG, "onDefaultDataSubscriptionChangeRequested{" +
"currentSubId='" + currentSubId + '\'' +
", newSubId=" + newSubId +
'}');
}
checkSimChangingState(currentSubId, newSubId);
}
@Override
public void onSignalStrengthsChanged(int subId, SignalStrength signalStrength) {
if (LOGV) {
Log.v(TAG, "onSignalStrengthsChanged{" +
"subId='" + subId + '\'' +
", signalStrength=" + signalStrength.toString() +
'}');
}
SubscriptionInfo subInfoRecord =
mPhoneMonitor.getActiveSubscriptionInfo(subId);
mSignalStrengths.put(subInfoRecord.getSimSlotIndex(), signalStrength);
updateSignalStrength(subInfoRecord);
}
@Override
public void onSimStateChanged(int subId, int simState) {
if (LOGV) {
Log.v(TAG, "onSimStateChanged{" +
"subId='" + subId + '\'' +
", simState=" + simState +
'}');
}
updateSignalStrengths();
updateCurrentDataSub();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setNextText(R.string.next);
mPhoneMonitor = PhoneMonitor.getInstance();
mPageView = (ViewGroup) findViewById(R.id.page_view);
mProgressBar = (ProgressBar) findViewById(R.id.progress);
List<SubscriptionInfo> subInfoRecords = mPhoneMonitor.getActiveSubscriptionInfoList();
int simCount =
subInfoRecords != null ? subInfoRecords.size() : 0;
mSubInfoRecords = new SparseArray<>(simCount);
for (SubscriptionInfo subInfoRecord : subInfoRecords) {
mSubInfoRecords.put(subInfoRecord.getSimSlotIndex(), subInfoRecord);
updateSignalStrength(subInfoRecord);
}
mNameViews = new SparseArray<>(simCount);
mSignalViews = new SparseArray<>(simCount);
mCheckBoxes = new SparseArray<>(simCount);
mRows = new SparseArray<>(simCount);
mServiceStates = new SparseArray<>(simCount);
mSignalStrengths = new SparseArray<>(simCount);
LayoutInflater inflater = LayoutInflater.from(this);
for (int i = 0; i < simCount; i++) {
View simRow = inflater.inflate(R.layout.data_sim_row, null);
mPageView.addView(simRow);
SubscriptionInfo subInfoRecord = mSubInfoRecords.valueAt(i);
simRow.setTag(subInfoRecord);
simRow.setOnClickListener(mSetDataSimClickListener);
int slot = subInfoRecord.getSimSlotIndex();
mNameViews.put(slot, (TextView) simRow.findViewById(R.id.sim_title));
mSignalViews.put(slot, (ImageView) simRow.findViewById(R.id.signal));
mCheckBoxes.put(slot, (CheckBox) simRow.findViewById(R.id.enable_check));
mRows.put(slot, simRow);
mPageView.addView(inflater.inflate(R.layout.divider, null));
}
updateSignalStrengths();
updateCurrentDataSub();
}
@Override
public void onPause() {
super.onPause();
mIsAttached = false;
mPhoneMonitor.removeListener(mSubscriptionStateListener);
}
@Override
public void onResume() {
super.onResume();
mIsAttached = true;
mPhoneMonitor.addListener(mSubscriptionStateListener);
mRadioReady = SetupWizardUtils.isRadioReady(this, null);
updateSignalStrengths();
updateCurrentDataSub();
checkForRadioReady();
if (mRadioReady) {
final int currentDataSub = SubscriptionManager.getDefaultDataSubscriptionId();
checkSimChangingState(currentDataSub, currentDataSub);
}
}
private void checkForRadioReady() {
if (mRadioReady) {
mHandler.removeCallbacks(mRadioReadyRunnable);
showPage();
final int currentDataSub = SubscriptionManager.getDefaultDataSubscriptionId();
checkSimChangingState(currentDataSub, currentDataSub);
return;
} else {
enableViews(false);
showProgress();
if (!mHandler.hasCallbacks(mRadioReadyRunnable)) {
mHandler.postDelayed(mRadioReadyRunnable, SetupWizardApp.RADIO_READY_TIMEOUT);
}
}
}
private void showPage() {
mPageView.setVisibility(View.VISIBLE);
if (!mPageView.isShown()) {
mPageView.startAnimation(
AnimationUtils.loadAnimation(this, R.anim.translucent_enter));
}
}
private void showProgress() {
if (!mProgressBar.isShown()) {
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.startAnimation(
AnimationUtils.loadAnimation(this, R.anim.translucent_enter));
}
}
private void hideProgress() {
if (mProgressBar.isShown()) {
mProgressBar.startAnimation(
AnimationUtils.loadAnimation(this, R.anim.translucent_exit));
mProgressBar.setVisibility(View.INVISIBLE);
}
}
private void updateSignalStrengths() {
if (mIsAttached) {
for (int i = 0; i < mSubInfoRecords.size(); i++) {
updateSignalStrength(mSubInfoRecords.valueAt(i));
}
}
}
private void changeDataSub(SubscriptionInfo subInfoRecord) {
final int currentDataSub = SubscriptionManager.getDefaultDataSubscriptionId();
final int requestedDataSub = subInfoRecord.getSubscriptionId();
if (LOGV) {
Log.v(TAG, "changeDataSub{" +
"currentDataSub='" + currentDataSub + '\'' +
", requestedDataSub=" + requestedDataSub +
'}');
}
if (currentDataSub != requestedDataSub) {
mPhoneMonitor.changeDataSub(requestedDataSub);
setDataSubChecked(subInfoRecord);
checkSimChangingState(currentDataSub, requestedDataSub);
}
}
private void checkSimChangingState(int currentDataSubId, int changingToDataSubId) {
if (LOGV) {
Log.v(TAG, "checkSimChangingState{" +
"currentDataSubId='" + currentDataSubId + '\'' +
"changingToDataSubId='" + changingToDataSubId + '\'' +
"mIsAttached='" + mIsAttached + '\'' +
", mRadioReady=" + mRadioReady +
'}');
}
if (mIsAttached && mRadioReady) {
if (currentDataSubId != changingToDataSubId) {
showProgress();
enableViews(false);
} else {
hideProgress();
enableViews(true);
}
}
}
private void setDataSubChecked(SubscriptionInfo subInfoRecord) {
if (mIsAttached) {
for (int i = 0; i < mCheckBoxes.size(); i++) {
int key = mCheckBoxes.keyAt(i);
mCheckBoxes.get(key).setChecked(subInfoRecord.getSimSlotIndex() == key);
}
}
}
private void updateCurrentDataSub() {
if (mIsAttached) {
for (int i = 0; i < mSubInfoRecords.size(); i++) {
SubscriptionInfo subInfoRecord = mSubInfoRecords.valueAt(i);
int slot = subInfoRecord.getSimSlotIndex();
mCheckBoxes.get(slot).setChecked(SubscriptionManager.getDefaultDataSubscriptionId()
== subInfoRecord.getSubscriptionId());
if (LOGV) {
Log.v(TAG, "updateCurrentDataSub{" +
"currentDataSubId='" + SubscriptionManager
.getDefaultDataSubscriptionId() + '\'' +
"subInfoRecord.getSubscriptionId()='" +
subInfoRecord.getSubscriptionId() +
'}');
}
}
}
}
private void enableViews(boolean enabled) {
mDisabledForSwitch = !enabled;
enableRows(enabled);
setNextAllowed(enabled);
}
private void enableRows(boolean enabled) {
for (int i = 0; i < mRows.size(); i++) {
final View v = mRows.get(mRows.keyAt(i));
v.setEnabled(enabled);
final SubscriptionInfo subInfoRecord = (SubscriptionInfo) v.getTag();
if (subInfoRecord != null) {
updateCarrierText(subInfoRecord);
}
}
}
private void updateCarrierText(SubscriptionInfo subInfoRecord) {
if (mIsAttached) {
String name = mPhoneMonitor.getSimOperatorName(subInfoRecord.getSubscriptionId());
if (TextUtils.isEmpty(name)) {
name = mPhoneMonitor.getNetworkOperatorName(subInfoRecord.getSubscriptionId());
}
ServiceState serviceState = mServiceStates.get(subInfoRecord.getSimSlotIndex());
final int slot = subInfoRecord.getSimSlotIndex();
final View v = mRows.get(slot);
if (TextUtils.isEmpty(name)) {
if (serviceState != null && serviceState.isEmergencyOnly()) {
name = getString(R.string.setup_mobile_data_emergency_only);
} else {
name = getString(R.string.setup_mobile_data_no_service);
}
if (v != null) {
v.setEnabled(false);
}
} else {
if (v != null && !mDisabledForSwitch) {
v.setEnabled(true);
}
}
String formattedName =
getString(R.string.data_sim_name,
slot + 1, name);
mNameViews.get(slot).setText(formattedName);
}
}
private void updateSignalStrength(SubscriptionInfo subInfoRecord) {
if (mIsAttached) {
ImageView signalView = mSignalViews.get(subInfoRecord.getSimSlotIndex());
SignalStrength signalStrength = mSignalStrengths.get(subInfoRecord.getSimSlotIndex());
if (LOGV) {
Log.v(TAG, "updateSignalStrength{" +
"signalStrength='" + signalStrength + '\'' +
"signalStrengthLevel='" + ((signalStrength != null) ?
signalStrength.getLevel() : "null") + '\'' +
", subInfoRecord.getSimSlotIndex() =" + subInfoRecord.getSimSlotIndex() +
'}');
}
if (!hasService(subInfoRecord)) {
signalView.setImageResource(R.drawable.ic_signal_no_signal);
} else {
if (signalStrength != null) {
int resId;
switch (signalStrength.getLevel()) {
case 4:
resId = R.drawable.ic_signal_4;
break;
case 3:
resId = R.drawable.ic_signal_3;
break;
case 2:
resId = R.drawable.ic_signal_2;
break;
case 1:
resId = R.drawable.ic_signal_1;
break;
default:
resId = R.drawable.ic_signal_0;
break;
}
signalView.setImageResource(resId);
}
}
updateCarrierText(subInfoRecord);
}
}
private boolean hasService(SubscriptionInfo subInfoRecord) {
boolean retVal;
ServiceState serviceState = mServiceStates.get(subInfoRecord.getSimSlotIndex());
if (serviceState == null) {
serviceState = mPhoneMonitor
.getServiceStateForSubscriber(subInfoRecord.getSubscriptionId());
mServiceStates.put(subInfoRecord.getSimSlotIndex(), serviceState);
}
if (serviceState != null) {
if (LOGV) {
Log.v(TAG, "hasService{" +
"serviceState.getVoiceRegState()='" + serviceState.getVoiceRegState()
+ '\''
+ "serviceState.getVoiceRegState()='" + serviceState.getVoiceRegState()
+ '\''
+ ", subInfoRecord.getSimSlotIndex() =" + subInfoRecord.getSimSlotIndex()
+ '}');
}
// Consider the device to be in service if either voice or data service is available.
// Some SIM cards are marketed as data-only and do not support voice service, and on
// these SIM cards, we want to show signal bars for data service as well as the "no
// service" or "emergency calls only" text that indicates that voice is not available.
switch (serviceState.getVoiceRegState()) {
case ServiceState.STATE_POWER_OFF:
retVal = false;
break;
case ServiceState.STATE_OUT_OF_SERVICE:
case ServiceState.STATE_EMERGENCY_ONLY:
retVal = serviceState.getDataRegState() == ServiceState.STATE_IN_SERVICE;
break;
default:
retVal = true;
}
} else {
retVal = false;
}
return retVal;
}
@Override
protected int getLayoutResId() {
return R.layout.choose_data_sim_page;
}
@Override
protected int getTitleResId() {
return R.string.setup_choose_data_sim;
}
@Override
protected int getIconResId() {
return R.drawable.ic_sim;
}
}
|