SetupWizard: Only update network state when resumed

Change-Id: Id018bf0ed4d9c54a1d6d57d53749c265b1d0e269
This commit is contained in:
cretin45 2015-03-11 17:13:05 -07:00
parent 77fce341b1
commit f66c56b62f
2 changed files with 133 additions and 119 deletions

View File

@ -86,6 +86,8 @@ public class ChooseDataSimPage extends SetupPage {
private SparseArray<ServiceState> mServiceStates; private SparseArray<ServiceState> mServiceStates;
private SparseArray<PhoneStateListener> mPhoneStateListeners; private SparseArray<PhoneStateListener> mPhoneStateListeners;
private boolean mIsAttached = false;
private View.OnClickListener mSetDataSimClickListener = new View.OnClickListener() { private View.OnClickListener mSetDataSimClickListener = new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
@ -121,12 +123,6 @@ public class ChooseDataSimPage extends SetupPage {
mPhoneStateListeners.put(i, createPhoneStateListener(subInfoRecord)); mPhoneStateListeners.put(i, createPhoneStateListener(subInfoRecord));
mPageView.addView(inflater.inflate(R.layout.divider, null)); mPageView.addView(inflater.inflate(R.layout.divider, null));
} }
mPhone = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
for (int i = 0; i < mPhoneStateListeners.size(); i++) {
mPhone.listen(mPhoneStateListeners.get(i),
PhoneStateListener.LISTEN_SERVICE_STATE
| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
updateSignalStrengths(); updateSignalStrengths();
updateCurrentDataSub(); updateCurrentDataSub();
} }
@ -139,16 +135,24 @@ public class ChooseDataSimPage extends SetupPage {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
mIsAttached = true;
mPhone = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
for (int i = 0; i < mPhoneStateListeners.size(); i++) {
mPhone.listen(mPhoneStateListeners.get(i),
PhoneStateListener.LISTEN_SERVICE_STATE
| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
updateSignalStrengths(); updateSignalStrengths();
updateCurrentDataSub(); updateCurrentDataSub();
} }
@Override @Override
public void onDetach() { public void onPause() {
super.onPause();
mIsAttached = false;
for (int i = 0; i < mPhoneStateListeners.size(); i++) { for (int i = 0; i < mPhoneStateListeners.size(); i++) {
mPhone.listen(mPhoneStateListeners.get(i), PhoneStateListener.LISTEN_NONE); mPhone.listen(mPhoneStateListeners.get(i), PhoneStateListener.LISTEN_NONE);
} }
super.onDetach();
} }
private PhoneStateListener createPhoneStateListener(final SubInfoRecord subInfoRecord) { private PhoneStateListener createPhoneStateListener(final SubInfoRecord subInfoRecord) {
@ -156,98 +160,105 @@ public class ChooseDataSimPage extends SetupPage {
@Override @Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) { public void onSignalStrengthsChanged(SignalStrength signalStrength) {
if (isDetached()) return; if (mIsAttached) {
mSignalStrengths.put(subInfoRecord.slotId, signalStrength); mSignalStrengths.put(subInfoRecord.slotId, signalStrength);
updateSignalStrength(subInfoRecord); updateSignalStrength(subInfoRecord);
}
} }
@Override @Override
public void onServiceStateChanged(ServiceState state) { public void onServiceStateChanged(ServiceState state) {
if (isDetached()) return; if (mIsAttached) {
mServiceStates.put(subInfoRecord.slotId, state); mServiceStates.put(subInfoRecord.slotId, state);
updateSignalStrength(subInfoRecord); updateSignalStrength(subInfoRecord);
}
} }
}; };
} }
private void updateSignalStrengths() { private void updateSignalStrengths() {
if (isDetached()) return; if (mIsAttached) {
for (int i = 0; i < mSubInfoRecords.size(); i++) { for (int i = 0; i < mSubInfoRecords.size(); i++) {
updateSignalStrength(mSubInfoRecords.get(i)); updateSignalStrength(mSubInfoRecords.get(i));
}
} }
} }
private void setDataSubChecked(SubInfoRecord subInfoRecord) { private void setDataSubChecked(SubInfoRecord subInfoRecord) {
if (isDetached()) return; if (mIsAttached) {
for (int i = 0; i < mCheckBoxes.size(); i++) { for (int i = 0; i < mCheckBoxes.size(); i++) {
if (subInfoRecord.slotId == i) { if (subInfoRecord.slotId == i) {
mCheckBoxes.get(i).setChecked(true); mCheckBoxes.get(i).setChecked(true);
SetupStats.addEvent(SetupStats.Categories.SETTING_CHANGED, SetupStats.addEvent(SetupStats.Categories.SETTING_CHANGED,
SetupStats.Action.PREFERRED_DATA_SIM, SetupStats.Action.PREFERRED_DATA_SIM,
SetupStats.Label.SLOT, String.valueOf(i + 1)); SetupStats.Label.SLOT, String.valueOf(i + 1));
} else { } else {
mCheckBoxes.get(i).setChecked(false); mCheckBoxes.get(i).setChecked(false);
} }
}
} }
} }
private void updateCurrentDataSub() { private void updateCurrentDataSub() {
if (isDetached()) return; if (mIsAttached) {
for (int i = 0; i < mSubInfoRecords.size(); i++) { for (int i = 0; i < mSubInfoRecords.size(); i++) {
SubInfoRecord subInfoRecord = mSubInfoRecords.get(i); SubInfoRecord subInfoRecord = mSubInfoRecords.get(i);
mCheckBoxes.get(i).setChecked(SubscriptionManager.getDefaultDataSubId() mCheckBoxes.get(i).setChecked(SubscriptionManager.getDefaultDataSubId()
== subInfoRecord.subId); == subInfoRecord.subId);
}
} }
} }
private void updateCarrierText(SubInfoRecord subInfoRecord) { private void updateCarrierText(SubInfoRecord subInfoRecord) {
if (isDetached()) return; if (mIsAttached) {
String name = mPhone.getNetworkOperatorName(subInfoRecord.subId); String name = mPhone.getNetworkOperatorName(subInfoRecord.subId);
ServiceState serviceState = mServiceStates.get(subInfoRecord.slotId); ServiceState serviceState = mServiceStates.get(subInfoRecord.slotId);
if (TextUtils.isEmpty(name)) { if (TextUtils.isEmpty(name)) {
if (serviceState != null && serviceState.isEmergencyOnly()) { if (serviceState != null && serviceState.isEmergencyOnly()) {
name = getString(R.string.setup_mobile_data_emergency_only); name = getString(R.string.setup_mobile_data_emergency_only);
} else { } else {
name = getString(R.string.setup_mobile_data_no_service); name = getString(R.string.setup_mobile_data_no_service);
}
} }
String formattedName =
getString(R.string.data_sim_name, subInfoRecord.slotId + 1, name);
mNameViews.get(subInfoRecord.slotId).setText(formattedName);
} }
String formattedName =
getString(R.string.data_sim_name, subInfoRecord.slotId + 1, name);
mNameViews.get(subInfoRecord.slotId).setText(formattedName);
} }
private void updateSignalStrength(SubInfoRecord subInfoRecord) { private void updateSignalStrength(SubInfoRecord subInfoRecord) {
if (isDetached()) return; if (mIsAttached) {
ImageView signalView = mSignalViews.get(subInfoRecord.slotId); ImageView signalView = mSignalViews.get(subInfoRecord.slotId);
SignalStrength signalStrength = mSignalStrengths.get(subInfoRecord.slotId); SignalStrength signalStrength = mSignalStrengths.get(subInfoRecord.slotId);
if (!hasService(subInfoRecord)) { if (!hasService(subInfoRecord)) {
signalView.setImageResource(R.drawable.ic_signal_no_signal); signalView.setImageResource(R.drawable.ic_signal_no_signal);
} else { } else {
if (signalStrength != null) { if (signalStrength != null) {
int resId; int resId;
switch (signalStrength.getLevel()) { switch (signalStrength.getLevel()) {
case 4: case 4:
resId = R.drawable.ic_signal_4; resId = R.drawable.ic_signal_4;
break; break;
case 3: case 3:
resId = R.drawable.ic_signal_3; resId = R.drawable.ic_signal_3;
break; break;
case 2: case 2:
resId = R.drawable.ic_signal_2; resId = R.drawable.ic_signal_2;
break; break;
case 1: case 1:
resId = R.drawable.ic_signal_1; resId = R.drawable.ic_signal_1;
break; break;
default: default:
resId = R.drawable.ic_signal_0; resId = R.drawable.ic_signal_0;
break; break;
}
signalView.setImageResource(resId);
} }
signalView.setImageResource(resId);
} }
updateCarrierText(subInfoRecord);
} }
updateCarrierText(subInfoRecord);
} }
private boolean hasService(SubInfoRecord subInfoRecord) { private boolean hasService(SubInfoRecord subInfoRecord) {

View File

@ -16,7 +16,6 @@
package com.cyanogenmod.setupwizard.setup; package com.cyanogenmod.setupwizard.setup;
import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentManager; import android.app.FragmentManager;
import android.content.Context; import android.content.Context;
@ -79,21 +78,25 @@ public class MobileDataPage extends SetupPage {
private SignalStrength mSignalStrength; private SignalStrength mSignalStrength;
private ServiceState mServiceState; private ServiceState mServiceState;
private boolean mIsAttached = false;
private PhoneStateListener mPhoneStateListener = private PhoneStateListener mPhoneStateListener =
new PhoneStateListener(SubscriptionManager.getDefaultDataSubId()) { new PhoneStateListener(SubscriptionManager.getDefaultDataSubId()) {
@Override @Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) { public void onSignalStrengthsChanged(SignalStrength signalStrength) {
if (isDetached()) return; if (mIsAttached) {
mSignalStrength = signalStrength; mSignalStrength = signalStrength;
updateSignalStrength(); updateSignalStrength();
}
} }
@Override @Override
public void onServiceStateChanged(ServiceState state) { public void onServiceStateChanged(ServiceState state) {
if (isDetached()) return; if (mIsAttached) {
mServiceState = state; mServiceState = state;
updateSignalStrength(); updateSignalStrength();
}
} }
}; };
@ -129,66 +132,66 @@ public class MobileDataPage extends SetupPage {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
mIsAttached = true;
mPhone = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
mPhone.listen(mPhoneStateListener,
PhoneStateListener.LISTEN_SERVICE_STATE
| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
updateDataConnectionStatus(); updateDataConnectionStatus();
updateSignalStrength(); updateSignalStrength();
} }
@Override @Override
public void onAttach(Activity activity) { public void onPause() {
super.onAttach(activity); super.onPause();
mPhone = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE); mIsAttached = false;
mPhone.listen(mPhoneStateListener,
PhoneStateListener.LISTEN_SERVICE_STATE
| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
@Override
public void onDetach() {
mPhone.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE); mPhone.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
super.onDetach();
} }
private void updateCarrierText() { private void updateCarrierText() {
if (isDetached()) return; if (mIsAttached) {
String name = mPhone.getNetworkOperatorName(SubscriptionManager.getDefaultDataSubId()); String name =
if (TextUtils.isEmpty(name)) { mPhone.getNetworkOperatorName(SubscriptionManager.getDefaultDataSubId());
if (mServiceState != null && mServiceState.isEmergencyOnly()) { if (TextUtils.isEmpty(name)) {
name = getString(R.string.setup_mobile_data_emergency_only); if (mServiceState != null && mServiceState.isEmergencyOnly()) {
} else { name = getString(R.string.setup_mobile_data_emergency_only);
name = getString(R.string.setup_mobile_data_no_service); } else {
name = getString(R.string.setup_mobile_data_no_service);
}
} }
mNameView.setText(name);
} }
mNameView.setText(name);
} }
private void updateSignalStrength() { private void updateSignalStrength() {
if (isDetached()) return; if (mIsAttached) {
if (!hasService()) { if (!hasService()) {
mSignalView.setImageResource(R.drawable.ic_signal_no_signal); mSignalView.setImageResource(R.drawable.ic_signal_no_signal);
} else { } else {
if (mSignalStrength != null) { if (mSignalStrength != null) {
int resId; int resId;
switch (mSignalStrength.getLevel()) { switch (mSignalStrength.getLevel()) {
case 4: case 4:
resId = R.drawable.ic_signal_4; resId = R.drawable.ic_signal_4;
break; break;
case 3: case 3:
resId = R.drawable.ic_signal_3; resId = R.drawable.ic_signal_3;
break; break;
case 2: case 2:
resId = R.drawable.ic_signal_2; resId = R.drawable.ic_signal_2;
break; break;
case 1: case 1:
resId = R.drawable.ic_signal_1; resId = R.drawable.ic_signal_1;
break; break;
default: default:
resId = R.drawable.ic_signal_0; resId = R.drawable.ic_signal_0;
break; break;
}
mSignalView.setImageResource(resId);
} }
mSignalView.setImageResource(resId);
} }
updateCarrierText();
} }
updateCarrierText();
} }
private void updateDataConnectionStatus() { private void updateDataConnectionStatus() {