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

Commit c319a161 authored by Kris Alder's avatar Kris Alder
Browse files

Always make feature available, even when no SIMS are currently installed

If we only enable the feature when SIMs are installed at startup, there
is a bug where inserting the first SIM after the service starts will
still allow 2G networking.

Bug: 401244615
Test: atest DisallowCellular2GTest
Flag: android.security.aapm_feature_disable_cellular_2g
Change-Id: Ic98b8fa04efc639ba9504014eb79406c7a8273d8
parent 3b22042f
Loading
Loading
Loading
Loading
+4 −42
Original line number Diff line number Diff line
@@ -22,16 +22,11 @@ import static android.security.advancedprotection.AdvancedProtectionManager.FEAT
import android.annotation.NonNull;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.UserManager;
import android.security.advancedprotection.AdvancedProtectionFeature;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Slog;

import java.util.ArrayList;
import java.util.List;

/** @hide */
public final class DisallowCellular2GAdvancedProtectionHook extends AdvancedProtectionHook {
    private static final String TAG = "AdvancedProtectionDisallowCellular2G";
@@ -39,14 +34,12 @@ public final class DisallowCellular2GAdvancedProtectionHook extends AdvancedProt
    private final AdvancedProtectionFeature mFeature =
            new AdvancedProtectionFeature(FEATURE_ID_DISALLOW_CELLULAR_2G);
    private final DevicePolicyManager mDevicePolicyManager;
    private final TelephonyManager mTelephonyManager;
    private final SubscriptionManager mSubscriptionManager;
    private final PackageManager mPackageManager;

    public DisallowCellular2GAdvancedProtectionHook(@NonNull Context context, boolean enabled) {
        super(context, enabled);
        mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class);
        mTelephonyManager = context.getSystemService(TelephonyManager.class);
        mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
        mPackageManager = context.getPackageManager();

        onAdvancedProtectionChanged(enabled);
    }
@@ -57,40 +50,9 @@ public final class DisallowCellular2GAdvancedProtectionHook extends AdvancedProt
        return mFeature;
    }

    private static boolean isEmbeddedSubscriptionVisible(SubscriptionInfo subInfo) {
        if (subInfo.isEmbedded()
                && (subInfo.getProfileClass() == SubscriptionManager.PROFILE_CLASS_PROVISIONING
                        || subInfo.isOnlyNonTerrestrialNetwork())) {
            return false;
        }

        return true;
    }

    private List<TelephonyManager> getActiveTelephonyManagers() {
        List<TelephonyManager> telephonyManagers = new ArrayList<>();

        for (SubscriptionInfo subInfo : mSubscriptionManager.getActiveSubscriptionInfoList()) {
            if (isEmbeddedSubscriptionVisible(subInfo)) {
                telephonyManagers.add(
                        mTelephonyManager.createForSubscriptionId(subInfo.getSubscriptionId()));
            }
        }

        return telephonyManagers;
    }

    @Override
    public boolean isAvailable() {
        for (TelephonyManager telephonyManager : getActiveTelephonyManagers()) {
            if (telephonyManager.isDataCapable()
                    && telephonyManager.isRadioInterfaceCapabilitySupported(
                            mTelephonyManager.CAPABILITY_USES_ALLOWED_NETWORK_TYPES_BITMASK)) {
                return true;
            }
        }

        return false;
        return mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
    }

    @Override