[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
This commit is contained in:
Bonian Chen
2020-05-25 22:53:25 +08:00
parent 1821ab5e5b
commit ce0f3c66a0
2 changed files with 44 additions and 9 deletions

View File

@@ -34,6 +34,7 @@ import androidx.preference.ListPreference;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.network.SubscriptionUtil; import com.android.settings.network.SubscriptionUtil;
import com.android.settings.network.SubscriptionsChangeListener; import com.android.settings.network.SubscriptionsChangeListener;
@@ -64,7 +65,6 @@ public abstract class DefaultSubscriptionController extends TelephonyBasePrefere
public DefaultSubscriptionController(Context context, String preferenceKey) { public DefaultSubscriptionController(Context context, String preferenceKey) {
super(context, preferenceKey); super(context, preferenceKey);
mManager = context.getSystemService(SubscriptionManager.class); mManager = context.getSystemService(SubscriptionManager.class);
mTelecomManager = mContext.getSystemService(TelecomManager.class);
mChangeListener = new SubscriptionsChangeListener(context, this); mChangeListener = new SubscriptionsChangeListener(context, this);
} }
@@ -184,12 +184,12 @@ public abstract class DefaultSubscriptionController extends TelephonyBasePrefere
*/ */
public PhoneAccountHandle getDefaultCallingAccountHandle() { public PhoneAccountHandle getDefaultCallingAccountHandle() {
final PhoneAccountHandle currentSelectPhoneAccount = final PhoneAccountHandle currentSelectPhoneAccount =
mTelecomManager.getUserSelectedOutgoingPhoneAccount(); getTelecomManager().getUserSelectedOutgoingPhoneAccount();
if (currentSelectPhoneAccount == null) { if (currentSelectPhoneAccount == null) {
return null; return null;
} }
final List<PhoneAccountHandle> accountHandles = final List<PhoneAccountHandle> accountHandles =
mTelecomManager.getCallCapablePhoneAccounts(false); getTelecomManager().getCallCapablePhoneAccounts(false);
final PhoneAccountHandle emergencyAccountHandle = new PhoneAccountHandle( final PhoneAccountHandle emergencyAccountHandle = new PhoneAccountHandle(
PSTN_CONNECTION_SERVICE_COMPONENT, EMERGENCY_ACCOUNT_HANDLE_ID); PSTN_CONNECTION_SERVICE_COMPONENT, EMERGENCY_ACCOUNT_HANDLE_ID);
if (currentSelectPhoneAccount.equals(emergencyAccountHandle)) { if (currentSelectPhoneAccount.equals(emergencyAccountHandle)) {
@@ -203,14 +203,30 @@ public abstract class DefaultSubscriptionController extends TelephonyBasePrefere
return null; 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 * Check if calling account bind to subscription
* *
* @param handle {@link PhoneAccountHandle} for specific calling account * @param handle {@link PhoneAccountHandle} for specific calling account
*/ */
public boolean isCallingAccountBindToSubscription(PhoneAccountHandle handle) { public boolean isCallingAccountBindToSubscription(PhoneAccountHandle handle) {
return mTelecomManager.getPhoneAccount(handle) final PhoneAccount account = getPhoneAccount(handle);
.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION); 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 * @return label of calling account
*/ */
public CharSequence getLabelFromCallingAccount(PhoneAccountHandle handle) { 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) { if (label != null) {
label = mContext.getPackageManager().getUserBadgedLabel(label, handle.getUserHandle()); label = mContext.getPackageManager().getUserBadgedLabel(label, handle.getUserHandle());
} }

View File

@@ -21,6 +21,7 @@ import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_U
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@@ -32,6 +33,9 @@ import android.content.Context;
import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.network.SubscriptionUtil; import com.android.settings.network.SubscriptionUtil;
@@ -46,9 +50,6 @@ import org.robolectric.RuntimeEnvironment;
import java.util.Arrays; import java.util.Arrays;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class DefaultSubscriptionControllerTest { public class DefaultSubscriptionControllerTest {
@Mock @Mock
@@ -91,6 +92,20 @@ public class DefaultSubscriptionControllerTest {
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 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 @Test
public void displayPreference_twoSubscriptionsSub1Default_correctListPreferenceValues() { public void displayPreference_twoSubscriptionsSub1Default_correctListPreferenceValues() {
final SubscriptionInfo sub1 = createMockSub(111, "sub1"); final SubscriptionInfo sub1 = createMockSub(111, "sub1");