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

Commit 5984d67a authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊
Browse files

base: Grant internet permission for new/updated system apps

parent 75cd0ea3
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
@@ -392,6 +392,9 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
    private static final int LINEAGE_VERSION_REINSTATED_POLICY_REJECT_ALL = 2;
    private static final int LINEAGE_VERSION_LATEST = LINEAGE_VERSION_REINSTATED_POLICY_REJECT_ALL;

    private static final int E_VERSION_INIT = 1;
    private static final int E_VERSION_LATEST = 2;

    @VisibleForTesting
    public static final int TYPE_WARNING = SystemMessage.NOTE_NET_WARNING;
    @VisibleForTesting
@@ -412,6 +415,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {

    private static final String ATTR_VERSION = "version";
    private static final String ATTR_LINEAGE_VERSION = "lineageVersion";
    private static final String ATTR_E_VERSION = "eVersion";
    private static final String ATTR_RESTRICT_BACKGROUND = "restrictBackground";
    private static final String ATTR_NETWORK_TEMPLATE = "networkTemplate";
    private static final String ATTR_SUBSCRIBER_ID = "subscriberId";
@@ -2900,6 +2904,26 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
        }
    }

    private void allowNetworkAccessForPackages(final List<String> packageNames) {
        final Set<Integer> uidsToAllow = new ArraySet<>();
        for (UserInfo userInfo : UserManager.get(mContext).getUsers()) {
            for (final String allowedPackageName : packageNames) {
                final int uid = getUidForPackage(allowedPackageName, userInfo.id);
                if (uid == -1) {
                    Log.w(TAG, "allowNetworkAccessForPackages: Did not find expected package "
                            + allowedPackageName + " for user " + userInfo.id);
                    continue;
                }
                Log.i(TAG, "allowNetworkAccessForPackages: Will ensure network access for "
                        + allowedPackageName + " (uid " + uid + ")");
                uidsToAllow.add(uid);
            }
        }

        // Remove POLICY_REJECT_ALL from UIDs that are now allowed to use networks.
        setPolicyForUids(uidsToAllow, POLICY_REJECT_ALL, false /* enabled */);
    }

    @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"})
    private void readPolicyAL() {
        if (LOGV) Slog.v(TAG, "readPolicyAL()");
@@ -2935,6 +2959,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
        int type;
        int version = VERSION_INIT;
        int lineageVersion = LINEAGE_VERSION_INIT;
        int eVersion = E_VERSION_INIT;
        boolean insideAllowlist = false;
        while ((type = in.next()) != END_DOCUMENT) {
            final String tag = in.getName();
@@ -2943,6 +2968,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                    final boolean oldValue = mRestrictBackground;
                    version = readIntAttribute(in, ATTR_VERSION);
                    lineageVersion = readIntAttribute(in, ATTR_LINEAGE_VERSION, lineageVersion);
                    eVersion = readIntAttribute(in, ATTR_E_VERSION, eVersion);
                    mLoadedRestrictBackground = (version >= VERSION_ADDED_RESTRICT_BACKGROUND)
                            && readBooleanAttribute(in, ATTR_RESTRICT_BACKGROUND);
                } else if (TAG_NETWORK_POLICY.equals(tag)) {
@@ -3122,6 +3148,29 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                && isMigratingFromAtLeastAndroid12) {
            migrateToPolicyRejectAll();
        }

        Log.i(TAG, "Current eVersion: " + eVersion);
        if (eVersion < E_VERSION_LATEST) {
            List<String> packageNames = new ArrayList<>();

            switch (eVersion) {
                case 1:
                    packageNames.add("com.android.vending");
                    packageNames.add("foundation.e.blissweather");
                    // Fall-through intended
                case 2:
                case 3:
                    // packageNames.add("com.android.example");
                    // Add new packages here for future use cases.
                    break; // Break to end the block after adding eVersion 2 or 3
                default:
                    break;
            }

            if (!packageNames.isEmpty()) {
                allowNetworkAccessForPackages(packageNames);
            }
        }
    }

    /**
@@ -3272,6 +3321,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
        out.startTag(null, TAG_POLICY_LIST);
        writeIntAttribute(out, ATTR_VERSION, VERSION_LATEST);
        writeIntAttribute(out, ATTR_LINEAGE_VERSION, LINEAGE_VERSION_LATEST);
        writeIntAttribute(out, ATTR_E_VERSION, E_VERSION_LATEST);
        writeBooleanAttribute(out, ATTR_RESTRICT_BACKGROUND, mRestrictBackground);

        // write all known network policies