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

Commit 75a070f3 authored by Weng Su's avatar Weng Su
Browse files

Restrict MobileDataSlice

- Hide MobileDataSlice if the user is not allowed to configure mobile networks.

Bug: 310630794
Flag: EXEMPT bugfix
Test: manual test
atest -c MobileDataSliceTest

Change-Id: I35814733a915f011e284b082ce7a94898ce8a6fb
parent 09fb13fd
Loading
Loading
Loading
Loading
+31 −8
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.UserManager;
import android.provider.Settings;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
@@ -79,19 +80,24 @@ public class MobileDataSlice implements CustomSliceable {

    @Override
    public Slice getSlice() {
        ListBuilder listBuilder = createListBuilder();
        if (!isConfigMobileNetworksAllowed()) {
            return listBuilder.build();
        }

        final IconCompat icon = IconCompat.createWithResource(mContext,
                R.drawable.ic_network_cell);
        final String title = mContext.getText(R.string.mobile_data_settings_title).toString();
        @ColorInt final int color = Utils.getColorAccentDefaultColor(mContext);

        // Return null until we can show a disabled-action Slice, blaming Airplane mode.
        // Return empty slice until we can show a disabled-action Slice, blaming Airplane mode.
        if (isAirplaneModeEnabled()) {
            return null;
            return listBuilder.build();
        }

        // Return null until we can show a disabled-action Slice.
        // Return empty slice until we can show a disabled-action Slice.
        if (!isMobileDataAvailable()) {
            return null;
            return listBuilder.build();
        }

        final CharSequence summary = getSummary();
@@ -109,11 +115,15 @@ public class MobileDataSlice implements CustomSliceable {
            rowBuilder.setSubtitle(summary);
        }

        final ListBuilder listBuilder = new ListBuilder(mContext, getUri(),
                ListBuilder.INFINITY)
        return listBuilder
                .setAccentColor(color)
                .addRow(rowBuilder);
        return listBuilder.build();
                .addRow(rowBuilder)
                .build();
    }

    @VisibleForTesting
    ListBuilder createListBuilder() {
        return new ListBuilder(mContext, getUri(), ListBuilder.INFINITY);
    }

    @Override
@@ -211,6 +221,19 @@ public class MobileDataSlice implements CustomSliceable {
        return mTelephonyManager.isDataEnabled();
    }

    @VisibleForTesting
    boolean isConfigMobileNetworksAllowed() {
        if (mContext == null) return true;
        UserManager userManager = mContext.getSystemService(UserManager.class);
        if (userManager == null) return true;
        boolean isAllowed = userManager.isAdminUser() && !userManager.hasUserRestriction(
                UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
        if (!isAllowed) {
            Log.w(TAG, "The user is not allowed to configure Mobile Networks.");
        }
        return isAllowed;
    }

    /**
     * Listener for mobile data state changes.
     *
+28 −6
Original line number Diff line number Diff line
@@ -19,8 +19,10 @@ package com.android.settings.network.telephony;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -37,6 +39,7 @@ import androidx.core.graphics.drawable.IconCompat;
import androidx.slice.Slice;
import androidx.slice.SliceMetadata;
import androidx.slice.SliceProvider;
import androidx.slice.builders.ListBuilder;
import androidx.slice.core.SliceAction;
import androidx.slice.widget.SliceLiveData;

@@ -68,6 +71,7 @@ public class MobileDataSliceTest {

    private Context mContext;
    private MobileDataSlice mMobileDataSlice;
    private ListBuilder mListBuilder;

    @Before
    public void setUp() {
@@ -86,6 +90,8 @@ public class MobileDataSliceTest {
        SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);

        mMobileDataSlice = spy(new MobileDataSlice(mContext));
        mListBuilder = spy(mMobileDataSlice.createListBuilder());
        doReturn(mListBuilder).when(mMobileDataSlice).createListBuilder();
    }

    @Test
@@ -175,25 +181,41 @@ public class MobileDataSliceTest {
    @Test
    public void isMobileDataAvailable_noSubscriptions_slicePrimaryActionIsEmpty() {
        when(mSubscriptionManager.getAvailableSubscriptionInfoList()).thenReturn(new ArrayList<>());
        final Slice mobileData = mMobileDataSlice.getSlice();

        assertThat(mobileData).isNull();
        Slice mobileData = mMobileDataSlice.getSlice();

        assertThat(mobileData).isNotNull();
        verify(mListBuilder, never()).addRow(any());
    }

    @Test
    public void isMobileDataAvailable_nullSubscriptions_slicePrimaryActionIsEmpty() {
        when(mSubscriptionManager.getAvailableSubscriptionInfoList()).thenReturn(null);
        final Slice mobileData = mMobileDataSlice.getSlice();

        assertThat(mobileData).isNull();
        Slice mobileData = mMobileDataSlice.getSlice();

        assertThat(mobileData).isNotNull();
        verify(mListBuilder, never()).addRow(any());
    }

    @Test
    public void airplaneModeEnabled_slicePrimaryActionIsEmpty() {
        doReturn(true).when(mMobileDataSlice).isAirplaneModeEnabled();
        doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(SUB_ID);
        final Slice mobileData = mMobileDataSlice.getSlice();

        assertThat(mobileData).isNull();
        Slice mobileData = mMobileDataSlice.getSlice();

        assertThat(mobileData).isNotNull();
        verify(mListBuilder, never()).addRow(any());
    }

    @Test
    public void getSlice_disallowConfigMobileNetworks_slicePrimaryActionIsEmpty() {
        doReturn(false).when(mMobileDataSlice).isConfigMobileNetworksAllowed();

        Slice mobileData = mMobileDataSlice.getSlice();

        assertThat(mobileData).isNotNull();
        verify(mListBuilder, never()).addRow(any());
    }
}