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

Commit 04d61ae6 authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

Add DPM API to block apps from using metered data.

Bug: 63700027
Test: cts-tradefed run singleCommand cts-dev -m CtsDevicePolicyManagerTestCases -t \
      com.android.cts.devicepolicy.MixedDeviceOwnerTest#testSetMeteredDataDisabled
Test: cts-tradefed run singleCommand cts-dev -m CtsDevicePolicyManagerTestCases -t \
      com.android.cts.devicepolicy.MixedProfileOwnerTest#testSetMeteredDataDisabled
Test: atest com.android.server.devicepolicy.DevicePolicyManagerTest#testSetGetMeteredDataDisabled
Change-Id: I147399d316f375c68b415dc6ede837c53cd1aad0
parent f93526c1
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -6422,6 +6422,7 @@ package android.app.admin {
    method public android.content.ComponentName getMandatoryBackupTransport();
    method public int getMaximumFailedPasswordsForWipe(android.content.ComponentName);
    method public long getMaximumTimeToLock(android.content.ComponentName);
    method public java.util.List<java.lang.String> getMeteredDataDisabled(android.content.ComponentName);
    method public int getOrganizationColor(android.content.ComponentName);
    method public java.lang.CharSequence getOrganizationName(android.content.ComponentName);
    method public android.app.admin.DevicePolicyManager getParentProfileInstance(android.content.ComponentName);
@@ -6525,6 +6526,7 @@ package android.app.admin {
    method public void setMasterVolumeMuted(android.content.ComponentName, boolean);
    method public void setMaximumFailedPasswordsForWipe(android.content.ComponentName, int);
    method public void setMaximumTimeToLock(android.content.ComponentName, long);
    method public java.util.List<java.lang.String> setMeteredDataDisabled(android.content.ComponentName, java.util.List<java.lang.String>);
    method public void setNetworkLoggingEnabled(android.content.ComponentName, boolean);
    method public void setOrganizationColor(android.content.ComponentName, int);
    method public void setOrganizationName(android.content.ComponentName, java.lang.CharSequence);
+41 −0
Original line number Diff line number Diff line
@@ -8218,6 +8218,47 @@ public class DevicePolicyManager {
        return new DevicePolicyManager(mContext, mService, true);
    }

    /**
     * Called by a device or profile owner to restrict packages from accessing metered data.
     *
     * @param admin which {@link DeviceAdminReceiver} this request is associated with.
     * @param packageNames the list of package names to be restricted.
     * @return a list of package names which could not be restricted.
     * @throws SecurityException if {@code admin} is not a device or profile owner.
     */
    public @NonNull List<String> setMeteredDataDisabled(@NonNull ComponentName admin,
            @NonNull List<String> packageNames) {
        throwIfParentInstance("setMeteredDataDisabled");
        if (mService != null) {
            try {
                return mService.setMeteredDataDisabled(admin, packageNames);
            } catch (RemoteException re) {
                throw re.rethrowFromSystemServer();
            }
        }
        return packageNames;
    }

    /**
     * Called by a device or profile owner to retrieve the list of packages which are restricted
     * by the admin from accessing metered data.
     *
     * @param admin which {@link DeviceAdminReceiver} this request is associated with.
     * @return the list of restricted package names.
     * @throws SecurityException if {@code admin} is not a device or profile owner.
     */
    public @NonNull List<String> getMeteredDataDisabled(@NonNull ComponentName admin) {
        throwIfParentInstance("getMeteredDataDisabled");
        if (mService != null) {
            try {
                return mService.getMeteredDataDisabled(admin);
            } catch (RemoteException re) {
                throw re.rethrowFromSystemServer();
            }
        }
        return new ArrayList<>();
    }

    /**
     * Called by device owners to retrieve device logs from before the device's last reboot.
     * <p>
+3 −0
Original line number Diff line number Diff line
@@ -400,4 +400,7 @@ interface IDevicePolicyManager {
    void setPrintingEnabled(in ComponentName admin, boolean enabled);
    boolean isPrintingEnabled();
    CharSequence getPrintingDisabledReason();

    List<String> setMeteredDataDisabled(in ComponentName admin, in List<String> packageNames);
    List<String> getMeteredDataDisabled(in ComponentName admin);
}
+10 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import com.android.server.am.ProcessList;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Set;

public class NetworkPolicyLogger {
    static final String TAG = "NetworkPolicy";
@@ -62,6 +63,7 @@ public class NetworkPolicyLogger {
    private static final int EVENT_TEMP_POWER_SAVE_WL_CHANGED = 10;
    private static final int EVENT_UID_FIREWALL_RULE_CHANGED = 11;
    private static final int EVENT_FIREWALL_CHAIN_ENABLED = 12;
    private static final int EVENT_UPDATE_METERED_RESTRICTED_PKGS = 13;

    static final int NTWK_BLOCKED_POWER = 0;
    static final int NTWK_ALLOWED_NON_METERED = 1;
@@ -179,6 +181,14 @@ public class NetworkPolicyLogger {
        }
    }

    void meteredRestrictedPkgsChanged(Set<Integer> restrictedUids) {
        synchronized (mLock) {
            final String log = "Metered restricted uids: " + restrictedUids;
            if (LOGD) Slog.d(TAG, log);
            mEventsBuffer.event(log);
        }
    }

    void dumpLogs(IndentingPrintWriter pw) {
        synchronized (mLock) {
            pw.println();
+19 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ package com.android.server.net;
import android.net.Network;
import android.telephony.SubscriptionPlan;

import java.util.Set;

/**
 * Network Policy Manager local system service interface.
 *
@@ -71,4 +73,21 @@ public abstract class NetworkPolicyManagerInternal {
     * Informs that admin data is loaded and available.
     */
    public abstract void onAdminDataAvailable();

    /**
     * Sets a list of packages which are restricted by admin from accessing metered data.
     *
     * @param packageNames the list of restricted packages.
     * @param userId the userId in which {@param packagesNames} are restricted.
     */
    public abstract void setMeteredRestrictedPackages(
            Set<String> packageNames, int userId);


    /**
     * Similar to {@link #setMeteredRestrictedPackages(Set, int)} but updates the restricted
     * packages list asynchronously.
     */
    public abstract void setMeteredRestrictedPackagesAsync(
            Set<String> packageNames, int userId);
}
Loading