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

Commit ffcbbd37 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Clean up MobileNetworkSummaryStatus

Fix: 338904413
Test: m Settings
Change-Id: Ic26bf7833c8fbdbeffc4f47dcd6dcd6191394e1b
parent 9245bf0b
Loading
Loading
Loading
Loading
+0 −176
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.network;

import android.content.Context;
import android.telephony.SubscriptionManager;
import android.util.Log;

import com.android.settings.network.SubscriptionUtil;
import com.android.settings.network.helper.SelectableSubscriptions;
import com.android.settings.network.helper.SubscriptionAnnotation;
import com.android.settings.network.helper.SubscriptionGrouping;
import com.android.settings.network.telephony.MobileNetworkUtils;
import com.android.settingslib.utils.ThreadUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * This one keeps the information required by MobileNetworkSummaryController.
 */
public class MobileNetworkSummaryStatus {
    private static final String LOG_TAG = "MobileNetworkSummaryStatus";

    private Future<Map<Integer, CharSequence>> mUniqueNameMapping;
    private Map<Integer, CharSequence> mUniqueNameMappingCache;

    private Future<Boolean> mIsEuiccConfiguable;
    private Boolean mIsEuiccConfiguableCache;

    private Future<Boolean> mIsPsimDisableSupported;
    private Boolean mIsPsimDisableSupportedCache;

    private List<SubscriptionAnnotation> mSubscriptionList;

    private boolean mDisableReEntranceUpdate;

    // Constructor
    public MobileNetworkSummaryStatus() {}

    /**
     * Update the status
     * @param context
     * @param andThen Consumer which always performed by the end of #update()
     *                and avoid from repeated queries.
     */
    public void update(Context context, Consumer<MobileNetworkSummaryStatus> andThen) {
        if (mDisableReEntranceUpdate) {
            Log.d(LOG_TAG, "network summary query ignored");
            if (andThen != null) {
                andThen.accept(this);
            }
            return;
        }
        mDisableReEntranceUpdate = true;
        Log.d(LOG_TAG, "network summary query");

        // Query Euicc in background
        mIsEuiccConfiguable = (Future<Boolean>)
                ThreadUtils.postOnBackgroundThread(() -> isEuiccConfiguable(context));

        // Query display name in background
        mUniqueNameMapping = (Future<Map<Integer, CharSequence>>)
                ThreadUtils.postOnBackgroundThread(() -> getUniqueNameForDisplay(context));

        // Query support status of pSIM disable feature
        mIsPsimDisableSupported = (Future<Boolean>) ThreadUtils.postOnBackgroundThread(()
                -> isPhysicalSimDisableSupported(context));

        // Query subscription
        mSubscriptionList = getSubscriptions(context);

        if (andThen != null) {
            andThen.accept(this);
        }
        mDisableReEntranceUpdate = false;
    }

    /**
     * Get the subscription information
     * @return a list of SubscriptionAnnotation
     */
    public List<SubscriptionAnnotation> getSubscriptionList() {
        return mSubscriptionList;
    }

    /**
     * Get unique display name for a specific subscription
     * @param subscriptionId subscription ID
     * @return display name for that subscription
     */
    public CharSequence getDisplayName(int subscriptionId) {
        if (mUniqueNameMapping != null) {
            try {
                mUniqueNameMappingCache = mUniqueNameMapping.get();
            } catch (Exception exception) {
                Log.w(LOG_TAG, "Fail to get display names", exception);
            }
            mUniqueNameMapping = null;
        }
        if (mUniqueNameMappingCache == null) {
            return null;
        }
        return mUniqueNameMappingCache.get(subscriptionId);
    }

    // Check if Euicc is currently available
    public boolean isEuiccConfigSupport() {
        if (mIsEuiccConfiguable != null) {
            try {
                mIsEuiccConfiguableCache = mIsEuiccConfiguable.get();
            } catch (Exception exception) {
                Log.w(LOG_TAG, "Fail to get euicc config status", exception);
            }
            mIsEuiccConfiguable = null;
        }
        return (mIsEuiccConfiguableCache == null) ?
                false : mIsEuiccConfiguableCache.booleanValue();
    }

    // Check if disable physical SIM is supported
    public boolean isPhysicalSimDisableSupport() {
        if (mIsPsimDisableSupported != null) {
            try {
                mIsPsimDisableSupportedCache = mIsPsimDisableSupported.get();
            } catch (Exception exception) {
                Log.w(LOG_TAG, "Fail to get pSIM disable support", exception);
            }
            mIsPsimDisableSupported = null;
        }
        return (mIsPsimDisableSupportedCache == null) ?
                false : mIsPsimDisableSupportedCache.booleanValue();
    }

    private List<SubscriptionAnnotation> getSubscriptions(Context context) {
        return (new SelectableSubscriptions(context, true))

                // To maintain the consistency with SubscriptionUtil#getAvailableSubscriptions().
                .addFinisher(new SubscriptionGrouping())

                .call()
                .stream()
                .filter(SubscriptionAnnotation::isDisplayAllowed)
                .collect(Collectors.toList());
    }

    private Map<Integer, CharSequence> getUniqueNameForDisplay(Context context) {
        return SubscriptionUtil.getUniqueSubscriptionDisplayNames(context);
    }

    private boolean isPhysicalSimDisableSupported(Context context) {
        SubscriptionManager subMgr = context.getSystemService(SubscriptionManager.class);
        return SubscriptionUtil.showToggleForPhysicalSim(subMgr);
    }

    private boolean isEuiccConfiguable(Context context) {
        return MobileNetworkUtils.showEuiccSettingsDetecting(context);
    }
}
+0 −100
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.network.helper;

import android.os.ParcelUuid;
import android.util.Log;

import androidx.annotation.Keep;
import androidx.annotation.VisibleForTesting;

import com.android.settings.network.helper.SubscriptionAnnotation;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

/**
 * A UnaryOperator for converting a list of SubscriptionAnnotation into
 * another list of SubscriptionAnnotation based on group UUID.
 * Only one SubscriptionAnnotation with entries with same (valid) group UUID would be kept.
 *
 * Here's an example when applying this operation as a finisher of SelectableSubscriptions:
 *
 * Callable<SubscriptionAnnotation> callable = (new SelectableSubscriptions(context, true))
 *         .addFinisher(new SubscriptionGrouping());
 *
 * List<SubscriptionAnnotation> result = ExecutorService.submit(callable).get()
 */
public class SubscriptionGrouping
        implements UnaryOperator<List<SubscriptionAnnotation>> {
    private static final String LOG_TAG = "SubscriptionGrouping";

    // implementation of UnaryOperator
    public List<SubscriptionAnnotation> apply(List<SubscriptionAnnotation> listOfSubscriptions) {
        Log.d(LOG_TAG, "Grouping " + listOfSubscriptions);

        // group by GUID
        Map<ParcelUuid, List<SubscriptionAnnotation>> groupedSubInfoList =
                listOfSubscriptions.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.groupingBy(subAnno -> getGroupUuid(subAnno)));

        // select best one from subscription(s) within the same group
        groupedSubInfoList.replaceAll((uuid, annoList) -> {
            if ((uuid == SubscriptionAnnotation.EMPTY_UUID) || (annoList.size() <= 1)) {
                return annoList;
            }
            return Collections.singletonList(selectBestFromList(annoList));
        });

        // build a stream of subscriptions
        return groupedSubInfoList.values()
                .stream().flatMap(List::stream).collect(Collectors.toList());
    }

    @Keep
    @VisibleForTesting
    protected ParcelUuid getGroupUuid(SubscriptionAnnotation subAnno) {
        ParcelUuid groupUuid = subAnno.getGroupUuid();
        return (groupUuid == null) ? SubscriptionAnnotation.EMPTY_UUID : groupUuid;
    }

    protected SubscriptionAnnotation selectBestFromList(List<SubscriptionAnnotation> annoList) {
        Comparator<SubscriptionAnnotation> annoSelector = (anno1, anno2) -> {
            if (anno1.isDisplayAllowed() != anno2.isDisplayAllowed()) {
                return anno1.isDisplayAllowed() ? -1 : 1;
            }
            if (anno1.isActive() != anno2.isActive()) {
                return anno1.isActive() ? -1 : 1;
            }
            if (anno1.isExisted() != anno2.isExisted()) {
                return anno1.isExisted() ? -1 : 1;
            }
            return 0;
        };
        annoSelector = annoSelector
                // eSIM in front of pSIM
                .thenComparingInt(anno -> -anno.getType())
                // maintain the ordering given within constructor
                .thenComparingInt(anno -> annoList.indexOf(anno));
        return annoList.stream().sorted(annoSelector).findFirst().orElse(null);
    }
}
+0 −2
Original line number Diff line number Diff line
@@ -44,8 +44,6 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;

import com.android.settings.Settings.MobileNetworkActivity;
import com.android.settings.network.helper.SubscriptionAnnotation;
import com.android.settings.network.helper.SubscriptionGrouping;
import com.android.settings.widget.AddPreference;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.core.lifecycle.Lifecycle;
+0 −137
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.network.helper;

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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.os.ParcelUuid;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.android.settings.network.helper.SubscriptionAnnotation;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;

import java.util.Arrays;
import java.util.List;

@RunWith(AndroidJUnit4.class)
public class SubscriptionGroupingTest {

    private ParcelUuid mMockUuid;

    private Context mContext;
    private SubscriptionGrouping mTarget;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        mContext = spy(ApplicationProvider.getApplicationContext());
        mMockUuid = ParcelUuid.fromString("1-1-1-1-1");
        mTarget = spy(new SubscriptionGrouping());
    }

    @Test
    public void apply_multipleEntriesWithSameGroupUuid_onlyOneLeft() {
        SubscriptionAnnotation subAnno1 = new TestSubAnnotation(1,
                SubscriptionAnnotation.TYPE_ESIM, true, true, mMockUuid);
        SubscriptionAnnotation subAnno2 = new TestSubAnnotation(2,
                SubscriptionAnnotation.TYPE_PSIM, true, true, mMockUuid);
        SubscriptionAnnotation subAnno3 = new TestSubAnnotation(3,
                SubscriptionAnnotation.TYPE_ESIM, true, true, mMockUuid);
        doReturn(mMockUuid).when(mTarget).getGroupUuid(any());

        List<SubscriptionAnnotation> result = mTarget
                .apply(Arrays.asList(subAnno2, subAnno1, subAnno3));
        assertThat(result.size()).isEqualTo(1);
        assertThat(result.get(0)).isEqualTo(subAnno1);
    }

    @Test
    public void apply_multipleEntriesWithSameGroupUuid_disabledOneIsAvoided() {
        SubscriptionAnnotation subAnno1 = new TestSubAnnotation(1,
                SubscriptionAnnotation.TYPE_ESIM, true, true, mMockUuid);
        SubscriptionAnnotation subAnno2 = new TestSubAnnotation(2,
                SubscriptionAnnotation.TYPE_PSIM, true, true, mMockUuid);
        SubscriptionAnnotation subAnno3 = new TestSubAnnotation(3,
                SubscriptionAnnotation.TYPE_ESIM, false, false, mMockUuid);
        doReturn(mMockUuid).when(mTarget).getGroupUuid(any());

        List<SubscriptionAnnotation> result = mTarget
                .apply(Arrays.asList(subAnno2, subAnno1, subAnno3));
        assertThat(result.size()).isEqualTo(1);
        assertThat(result.get(0)).isEqualTo(subAnno1);
    }

    private class TestSubAnnotation extends SubscriptionAnnotation {
        private int mSubId;
        private int mSimType;
        private boolean mIsActive;
        private boolean mIsDisplayAllowed;
        private ParcelUuid mUuid;

        private TestSubAnnotation(int subId, int simType,
                boolean isActive, boolean isDisplayAllowed, ParcelUuid guuid) {
            super(null, -1, null, null, null, null);
            mSubId = subId;
            mSimType = simType;
            mIsActive = isActive;
            mIsDisplayAllowed = isDisplayAllowed;
            mUuid = guuid;
        }

        @Override
        public int getSubscriptionId() {
            return mSubId;
        }

        @Override
        public int getType() {
            return mSimType;
        }

        @Override
        public boolean isExisted() {
            return true;
        }

        @Override
        public boolean isActive() {
            return mIsActive;
        }

        @Override
        public boolean isDisplayAllowed() {
            return mIsDisplayAllowed;
        }

        @Override
        public ParcelUuid getGroupUuid() {
            return mUuid;
        }
    }
}