From ce0f3c66a080e43867bf11ec50275e2bec171b6c Mon Sep 17 00:00:00 2001 From: Bonian Chen Date: Mon, 25 May 2020 22:53:25 +0800 Subject: [PATCH] [Settings] Avoid crash for VoIP account displayed as default voice When displaying VoIP account in Default voice account UI within mobile network configuration, some null pointer checking need to applied due to this is no longer a real account bind to SIM card. Bug: 157334667 Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=DefaultSubscriptionControllerTest Change-Id: I3927362676c867ac245f16e1d00ea953b21ec1d4 --- .../DefaultSubscriptionController.java | 32 +++++++++++++++---- .../DefaultSubscriptionControllerTest.java | 21 ++++++++++-- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/com/android/settings/network/telephony/DefaultSubscriptionController.java b/src/com/android/settings/network/telephony/DefaultSubscriptionController.java index 779802a22ac..1d82f659b87 100644 --- a/src/com/android/settings/network/telephony/DefaultSubscriptionController.java +++ b/src/com/android/settings/network/telephony/DefaultSubscriptionController.java @@ -34,6 +34,7 @@ import androidx.preference.ListPreference; import androidx.preference.Preference; import androidx.preference.PreferenceScreen; +import com.android.internal.annotations.VisibleForTesting; import com.android.settings.R; import com.android.settings.network.SubscriptionUtil; import com.android.settings.network.SubscriptionsChangeListener; @@ -64,7 +65,6 @@ public abstract class DefaultSubscriptionController extends TelephonyBasePrefere public DefaultSubscriptionController(Context context, String preferenceKey) { super(context, preferenceKey); mManager = context.getSystemService(SubscriptionManager.class); - mTelecomManager = mContext.getSystemService(TelecomManager.class); mChangeListener = new SubscriptionsChangeListener(context, this); } @@ -184,12 +184,12 @@ public abstract class DefaultSubscriptionController extends TelephonyBasePrefere */ public PhoneAccountHandle getDefaultCallingAccountHandle() { final PhoneAccountHandle currentSelectPhoneAccount = - mTelecomManager.getUserSelectedOutgoingPhoneAccount(); + getTelecomManager().getUserSelectedOutgoingPhoneAccount(); if (currentSelectPhoneAccount == null) { return null; } final List accountHandles = - mTelecomManager.getCallCapablePhoneAccounts(false); + getTelecomManager().getCallCapablePhoneAccounts(false); final PhoneAccountHandle emergencyAccountHandle = new PhoneAccountHandle( PSTN_CONNECTION_SERVICE_COMPONENT, EMERGENCY_ACCOUNT_HANDLE_ID); if (currentSelectPhoneAccount.equals(emergencyAccountHandle)) { @@ -203,14 +203,30 @@ public abstract class DefaultSubscriptionController extends TelephonyBasePrefere return null; } + @VisibleForTesting + TelecomManager getTelecomManager() { + if (mTelecomManager == null) { + mTelecomManager = mContext.getSystemService(TelecomManager.class); + } + return mTelecomManager; + } + + @VisibleForTesting + PhoneAccount getPhoneAccount(PhoneAccountHandle handle) { + return getTelecomManager().getPhoneAccount(handle); + } + /** * Check if calling account bind to subscription * * @param handle {@link PhoneAccountHandle} for specific calling account */ public boolean isCallingAccountBindToSubscription(PhoneAccountHandle handle) { - return mTelecomManager.getPhoneAccount(handle) - .hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION); + final PhoneAccount account = getPhoneAccount(handle); + if (account == null) { + return false; + } + return account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION); } /** @@ -220,7 +236,11 @@ public abstract class DefaultSubscriptionController extends TelephonyBasePrefere * @return label of calling account */ public CharSequence getLabelFromCallingAccount(PhoneAccountHandle handle) { - CharSequence label = mTelecomManager.getPhoneAccount(handle).getLabel(); + CharSequence label = null; + final PhoneAccount account = getPhoneAccount(handle); + if (account != null) { + label = account.getLabel(); + } if (label != null) { label = mContext.getPackageManager().getUserBadgedLabel(label, handle.getUserHandle()); } diff --git a/tests/robotests/src/com/android/settings/network/telephony/DefaultSubscriptionControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/DefaultSubscriptionControllerTest.java index dbdad505d34..e20241b8b41 100644 --- a/tests/robotests/src/com/android/settings/network/telephony/DefaultSubscriptionControllerTest.java +++ b/tests/robotests/src/com/android/settings/network/telephony/DefaultSubscriptionControllerTest.java @@ -21,6 +21,7 @@ import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_U import static com.google.common.truth.Truth.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -32,6 +33,9 @@ import android.content.Context; import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionManager; +import androidx.preference.ListPreference; +import androidx.preference.PreferenceScreen; + import com.android.settings.R; import com.android.settings.network.SubscriptionUtil; @@ -46,9 +50,6 @@ import org.robolectric.RuntimeEnvironment; import java.util.Arrays; -import androidx.preference.ListPreference; -import androidx.preference.PreferenceScreen; - @RunWith(RobolectricTestRunner.class) public class DefaultSubscriptionControllerTest { @Mock @@ -91,6 +92,20 @@ public class DefaultSubscriptionControllerTest { assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); } + @Test + public void isCallingAccountBindToSubscription_invalidAccount_withoutCrash() { + doReturn(null).when(mController).getPhoneAccount(any()); + + mController.isCallingAccountBindToSubscription(null); + } + + @Test + public void getLabelFromCallingAccount_invalidAccount_emptyString() { + doReturn(null).when(mController).getPhoneAccount(any()); + + assertThat(mController.getLabelFromCallingAccount(null)).isEqualTo(""); + } + @Test public void displayPreference_twoSubscriptionsSub1Default_correctListPreferenceValues() { final SubscriptionInfo sub1 = createMockSub(111, "sub1");