Fix NullPointerException, TelephonyManager.getServiceState is null

Add the null protect for TelephonyManager.getServiceState.

Bug: 166676020
Test: 1. make RunSettingsRoboTests ROBOTEST_FILTER=\
RenameMobileNetworkDialogFragmentTest (PASS)
2. make RunSettingsRoboTests ROBOTEST_FILTER=\
AutoSelectPreferenceControllerTest (PASS)
3. make RunSettingsRoboTests ROBOTEST_FILTER=\
PreferredNetworkModePreferenceControllerTest (PASS)
4. make RunSettingsRoboTests ROBOTEST_FILTER=\
EnabledNetworkModePreferenceControllerTest (PASS)

Change-Id: I61e59f7ba9d6c64019d620d6cb80099ded41473f
This commit is contained in:
SongFerngWang
2020-08-28 14:33:39 +08:00
parent 59d8ab1094
commit 09dc253a47
4 changed files with 16 additions and 4 deletions

View File

@@ -48,6 +48,7 @@ import android.provider.Settings;
import android.telecom.PhoneAccountHandle; import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager; import android.telecom.TelecomManager;
import android.telephony.CarrierConfigManager; import android.telephony.CarrierConfigManager;
import android.telephony.ServiceState;
import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
@@ -475,8 +476,9 @@ public class MobileNetworkUtils {
if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL)) { if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL)) {
return true; return true;
} }
final ServiceState serviceState = telephonyManager.getServiceState();
final String operatorNumeric = telephonyManager.getServiceState().getOperatorNumeric(); final String operatorNumeric =
(serviceState != null) ? serviceState.getOperatorNumeric() : null;
final String[] numericArray = carrierConfig.getStringArray( final String[] numericArray = carrierConfig.getStringArray(
CarrierConfigManager.KEY_SUPPORT_TDSCDMA_ROAMING_NETWORKS_STRING_ARRAY); CarrierConfigManager.KEY_SUPPORT_TDSCDMA_ROAMING_NETWORKS_STRING_ARRAY);
if (numericArray == null || operatorNumeric == null) { if (numericArray == null || operatorNumeric == null) {

View File

@@ -405,6 +405,9 @@ public class NetworkSelectSettings extends DashboardFragment {
if (mTelephonyManager.getDataState() == mTelephonyManager.DATA_CONNECTED) { if (mTelephonyManager.getDataState() == mTelephonyManager.DATA_CONNECTED) {
// Try to get the network registration states // Try to get the network registration states
final ServiceState ss = mTelephonyManager.getServiceState(); final ServiceState ss = mTelephonyManager.getServiceState();
if (ss == null) {
return;
}
final List<NetworkRegistrationInfo> networkList = final List<NetworkRegistrationInfo> networkList =
ss.getNetworkRegistrationInfoListForTransportType( ss.getNetworkRegistrationInfoListForTransportType(
AccessNetworkConstants.TRANSPORT_TYPE_WWAN); AccessNetworkConstants.TRANSPORT_TYPE_WWAN);

View File

@@ -183,7 +183,7 @@ public class RenameMobileNetworkDialogFragment extends InstrumentedDialogFragmen
final TextView operatorName = view.findViewById(R.id.operator_name_value); final TextView operatorName = view.findViewById(R.id.operator_name_value);
mTelephonyManager = mTelephonyManager.createForSubscriptionId(mSubId); mTelephonyManager = mTelephonyManager.createForSubscriptionId(mSubId);
final ServiceState serviceState = mTelephonyManager.getServiceState(); final ServiceState serviceState = mTelephonyManager.getServiceState();
operatorName.setText(serviceState.getOperatorAlphaLong()); operatorName.setText(serviceState == null ? "" : serviceState.getOperatorAlphaLong());
final TextView phoneTitle = view.findViewById(R.id.number_label); final TextView phoneTitle = view.findViewById(R.id.number_label);
phoneTitle.setVisibility(info.isOpportunistic() ? View.GONE : View.VISIBLE); phoneTitle.setVisibility(info.isOpportunistic() ? View.GONE : View.VISIBLE);

View File

@@ -29,6 +29,7 @@ import android.os.PersistableBundle;
import android.os.SystemClock; import android.os.SystemClock;
import android.provider.Settings; import android.provider.Settings;
import android.telephony.CarrierConfigManager; import android.telephony.CarrierConfigManager;
import android.telephony.ServiceState;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
@@ -125,7 +126,13 @@ public class AutoSelectPreferenceController extends TelephonyTogglePreferenceCon
super.updateState(preference); super.updateState(preference);
preference.setSummary(null); preference.setSummary(null);
if (mTelephonyManager.getServiceState().getRoaming()) { final ServiceState serviceState = mTelephonyManager.getServiceState();
if (serviceState == null) {
preference.setEnabled(false);
return;
}
if (serviceState.getRoaming()) {
preference.setEnabled(true); preference.setEnabled(true);
} else { } else {
preference.setEnabled(!mOnlyAutoSelectInHome); preference.setEnabled(!mOnlyAutoSelectInHome);