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

Commit 28309fec authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Added option to reset network policies for a given user." into nyc-dev

parents 65fad184 d17fda40
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 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.server.net;

/**
 * Network Policy Manager local system service interface.
 *
 * @hide Only for use within the system server.
 */
public abstract class NetworkPolicyManagerInternal {

    /**
     * Resets all policies associated with a given user.
     */
    public abstract void resetUserState(int userId);
}
+29 −9
Original line number Diff line number Diff line
@@ -406,6 +406,10 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
        mAppOps = context.getSystemService(AppOpsManager.class);

        mPackageMonitor = new MyPackageMonitor();

        // Expose private service for system components to use.
        LocalServices.addService(NetworkPolicyManagerInternal.class,
                new NetworkPolicyManagerInternalImpl());
    }

    public void bindConnectivityManager(IConnectivityManager connManager) {
@@ -742,7 +746,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                    synchronized (mRulesLock) {
                        // Remove any persistable state for the given user; both cleaning up after a
                        // USER_REMOVED, and one last sanity check during USER_ADDED
                        removeUserStateLocked(userId);
                        removeUserStateLocked(userId, true);
                        if (action == ACTION_USER_ADDED) {
                            // Add apps that are whitelisted by default.
                            addDefaultRestrictBackgroundWhitelistUidsLocked(userId);
@@ -1742,12 +1746,13 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
    }

    /**
     * Remove any persistable state associated with given {@link UserHandle}, persisting
     * if any changes are made.
     * Removes any persistable state associated with given {@link UserHandle}, persisting
     * if any changes that are made.
     */
    void removeUserStateLocked(int userId) {
    boolean removeUserStateLocked(int userId, boolean writePolicy) {

        if (LOGV) Slog.v(TAG, "removeUserStateLocked()");
        boolean writePolicy = false;
        boolean changed = false;

        // Remove entries from restricted background UID whitelist
        int[] wlUids = new int[0];
@@ -1762,7 +1767,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
            for (int uid : wlUids) {
                removeRestrictBackgroundWhitelistedUidLocked(uid, false, false);
            }
            writePolicy = true;
            changed = true;
        }

        // Remove entries from revoked default restricted background UID whitelist
@@ -1770,7 +1775,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
            final int uid = mRestrictBackgroundWhitelistRevokedUids.keyAt(i);
            if (UserHandle.getUserId(uid) == userId) {
                mRestrictBackgroundWhitelistRevokedUids.removeAt(i);
                writePolicy = true;
                changed = true;
            }
        }

@@ -1787,14 +1792,15 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
            for (int uid : uids) {
                mUidPolicy.delete(uid);
            }
            writePolicy = true;
            changed = true;
        }

        updateRulesForGlobalChangeLocked(true);

        if (writePolicy) {
        if (writePolicy && changed) {
            writePolicyLocked();
        }
        return changed;
    }

    @Override
@@ -3295,4 +3301,18 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
            }
        }
    }

    private class NetworkPolicyManagerInternalImpl extends NetworkPolicyManagerInternal {

        @Override
        public void resetUserState(int userId) {
            synchronized (mRulesLock) {
                boolean changed = removeUserStateLocked(userId, false);
                changed = addDefaultRestrictBackgroundWhitelistUidsLocked(userId) || changed;
                if (changed) {
                    writePolicyLocked();
                }
            }
        }
    }
}
+12 −5
Original line number Diff line number Diff line
@@ -164,6 +164,7 @@ import android.content.pm.VerifierInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.hardware.display.DisplayManager;
import android.net.INetworkPolicyManager;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
@@ -243,6 +244,7 @@ import com.android.server.LocalServices;
import com.android.server.ServiceThread;
import com.android.server.SystemConfig;
import com.android.server.Watchdog;
import com.android.server.net.NetworkPolicyManagerInternal;
import com.android.server.pm.PermissionsState.PermissionState;
import com.android.server.pm.Settings.DatabaseVersion;
import com.android.server.pm.Settings.VersionInfo;
@@ -16159,6 +16161,10 @@ public class PackageManagerService extends IPackageManager.Stub {
        }
    }
    private void resetNetworkPolicies(int userId) {
        LocalServices.getService(NetworkPolicyManagerInternal.class).resetUserState(userId);
    }
    /**
     * Reverts user permission state changes (permissions and flags).
     *
@@ -16649,10 +16655,10 @@ public class PackageManagerService extends IPackageManager.Stub {
    public void resetApplicationPreferences(int userId) {
        mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null);
        // writer
        synchronized (mPackages) {
        final long identity = Binder.clearCallingIdentity();
        // writer
        try {
            synchronized (mPackages) {
                clearPackagePreferredActivitiesLPw(null, userId);
                mSettings.applyDefaultPreferredAppsLPw(this, userId);
                // TODO: We have to reset the default SMS and Phone. This requires
@@ -16664,11 +16670,12 @@ public class PackageManagerService extends IPackageManager.Stub {
                primeDomainVerificationsLPw(userId);
                resetUserChangesToRuntimePermissionsAndFlagsLPw(userId);
                scheduleWritePackageRestrictionsLocked(userId);
            }
            resetNetworkPolicies(userId);
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
    }
    }
    @Override
    public int getPreferredActivities(List<IntentFilter> outFilters,