Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce0f3c66 authored by Bonian Chen's avatar Bonian Chen
Browse files

[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
parent 1821ab5e
Loading
Loading
Loading
Loading
+26 −6
Original line number Diff line number Diff line
@@ -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<PhoneAccountHandle> 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());
        }
+18 −3
Original line number Diff line number Diff line
@@ -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");