1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.net;
18 
19 import static com.android.server.net.NetworkPolicyManagerService.isUidNetworkingBlockedInternal;
20 
21 import android.annotation.NonNull;
22 import android.net.Network;
23 import android.net.NetworkTemplate;
24 import android.net.netstats.provider.NetworkStatsProvider;
25 import android.telephony.SubscriptionPlan;
26 
27 import java.util.Set;
28 
29 /**
30  * Network Policy Manager local system service interface.
31  *
32  * @hide Only for use within the system server.
33  */
34 public abstract class NetworkPolicyManagerInternal {
35 
36     /**
37      * Resets all policies associated with a given user.
38      */
resetUserState(int userId)39     public abstract void resetUserState(int userId);
40 
41     /**
42      * @return true if the given uid is restricted from doing networking on metered networks.
43      */
isUidRestrictedOnMeteredNetworks(int uid)44     public abstract boolean isUidRestrictedOnMeteredNetworks(int uid);
45 
46     /**
47      * @return true if networking is blocked on the given interface for the given uid according
48      * to current networking policies.
49      */
isUidNetworkingBlocked(int uid, String ifname)50     public abstract boolean isUidNetworkingBlocked(int uid, String ifname);
51 
52     /**
53      * Figure out if networking is blocked for a given set of conditions.
54      *
55      * This is used by ConnectivityService via passing stale copies of conditions, so it must not
56      * take any locks.
57      *
58      * @param uid The target uid.
59      * @param uidRules The uid rules which are obtained from NetworkPolicyManagerService.
60      * @param isNetworkMetered True if the network is metered.
61      * @param isBackgroundRestricted True if data saver is enabled.
62      *
63      * @return true if networking is blocked for the UID under the specified conditions.
64      */
isUidNetworkingBlocked(int uid, int uidRules, boolean isNetworkMetered, boolean isBackgroundRestricted)65     public static boolean isUidNetworkingBlocked(int uid, int uidRules, boolean isNetworkMetered,
66             boolean isBackgroundRestricted) {
67         // Log of invoking internal function is disabled because it will be called very
68         // frequently. And metrics are unlikely needed on this method because the callers are
69         // external and this method doesn't take any locks or perform expensive operations.
70         return isUidNetworkingBlockedInternal(uid, uidRules, isNetworkMetered,
71                 isBackgroundRestricted, null);
72     }
73 
74     /**
75      * Informs that an appId has been added or removed from the temp-powersave-whitelist so that
76      * that network rules for that appId can be updated.
77      *
78      * @param appId The appId which has been updated in the whitelist.
79      * @param added Denotes whether the {@param appId} has been added or removed from the whitelist.
80      */
onTempPowerSaveWhitelistChange(int appId, boolean added)81     public abstract void onTempPowerSaveWhitelistChange(int appId, boolean added);
82 
83     /**
84      * Return the active {@link SubscriptionPlan} for the given network.
85      */
getSubscriptionPlan(Network network)86     public abstract SubscriptionPlan getSubscriptionPlan(Network network);
87 
88     /**
89      * Return the active {@link SubscriptionPlan} for the given template.
90      */
getSubscriptionPlan(NetworkTemplate template)91     public abstract SubscriptionPlan getSubscriptionPlan(NetworkTemplate template);
92 
93     public static final int QUOTA_TYPE_JOBS = 1;
94     public static final int QUOTA_TYPE_MULTIPATH = 2;
95 
96     /**
97      * Return the daily quota (in bytes) that can be opportunistically used on
98      * the given network to improve the end user experience. It's called
99      * "opportunistic" because it's traffic that would typically not use the
100      * given network.
101      */
getSubscriptionOpportunisticQuota(Network network, int quotaType)102     public abstract long getSubscriptionOpportunisticQuota(Network network, int quotaType);
103 
104     /**
105      * Informs that admin data is loaded and available.
106      */
onAdminDataAvailable()107     public abstract void onAdminDataAvailable();
108 
109     /**
110      * Control if a UID should be whitelisted even if it's in app idle mode. Other restrictions may
111      * still be in effect.
112      */
setAppIdleWhitelist(int uid, boolean shouldWhitelist)113     public abstract void setAppIdleWhitelist(int uid, boolean shouldWhitelist);
114 
115     /**
116      * Sets a list of packages which are restricted by admin from accessing metered data.
117      *
118      * @param packageNames the list of restricted packages.
119      * @param userId the userId in which {@param packagesNames} are restricted.
120      */
setMeteredRestrictedPackages( Set<String> packageNames, int userId)121     public abstract void setMeteredRestrictedPackages(
122             Set<String> packageNames, int userId);
123 
124 
125     /**
126      * Similar to {@link #setMeteredRestrictedPackages(Set, int)} but updates the restricted
127      * packages list asynchronously.
128      */
setMeteredRestrictedPackagesAsync( Set<String> packageNames, int userId)129     public abstract void setMeteredRestrictedPackagesAsync(
130             Set<String> packageNames, int userId);
131 
132     /**
133      *  Notifies that the specified {@link NetworkStatsProvider} has reached its quota
134      *  which was set through {@link NetworkStatsProvider#onSetLimit(String, long)}.
135      *
136      * @param tag the human readable identifier of the custom network stats provider.
137      */
onStatsProviderLimitReached(@onNull String tag)138     public abstract void onStatsProviderLimitReached(@NonNull String tag);
139 }
140