Skip to content

Commit d0c6ccb

Browse files
committed
Move NetworkPolicy from apps to UID.
For multi-user devices, switch to storing policy per-user instead of per-app. Also watch for user added/removed broadcasts to clean up policies and apply global restrictions. Bug: 7121279 Change-Id: Ia7326bd0ebe0586fa4ec6d3a62f6313dc8814007
1 parent ee10044 commit d0c6ccb

File tree

6 files changed

+174
-152
lines changed

6 files changed

+174
-152
lines changed

core/java/android/content/Intent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ public static Intent createChooser(Intent target, CharSequence title) {
14731473
* Broadcast Action: A new application package has been installed on the
14741474
* device. The data contains the name of the package. Note that the
14751475
* newly installed package does <em>not</em> receive this broadcast.
1476-
* <p>My include the following extras:
1476+
* <p>May include the following extras:
14771477
* <ul>
14781478
* <li> {@link #EXTRA_UID} containing the integer uid assigned to the new package.
14791479
* <li> {@link #EXTRA_REPLACING} is set to true if this is following
@@ -1489,7 +1489,7 @@ public static Intent createChooser(Intent target, CharSequence title) {
14891489
* Broadcast Action: A new version of an application package has been
14901490
* installed, replacing an existing version that was previously installed.
14911491
* The data contains the name of the package.
1492-
* <p>My include the following extras:
1492+
* <p>May include the following extras:
14931493
* <ul>
14941494
* <li> {@link #EXTRA_UID} containing the integer uid assigned to the new package.
14951495
* </ul>

core/java/android/net/INetworkPolicyManager.aidl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ import android.net.NetworkTemplate;
3030
interface INetworkPolicyManager {
3131

3232
/** Control UID policies. */
33-
void setAppPolicy(int appId, int policy);
34-
int getAppPolicy(int appId);
35-
int[] getAppsWithPolicy(int policy);
33+
void setUidPolicy(int uid, int policy);
34+
int getUidPolicy(int uid);
35+
int[] getUidsWithPolicy(int policy);
3636

3737
boolean isUidForeground(int uid);
3838

core/java/android/net/NetworkPolicyManager.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import android.content.pm.PackageManager.NameNotFoundException;
2727
import android.content.pm.Signature;
2828
import android.os.RemoteException;
29+
import android.os.UserHandle;
2930
import android.text.format.Time;
3031

3132
import com.google.android.collect.Sets;
@@ -72,29 +73,29 @@ public static NetworkPolicyManager from(Context context) {
7273
}
7374

7475
/**
75-
* Set policy flags for specific application.
76+
* Set policy flags for specific UID.
7677
*
7778
* @param policy {@link #POLICY_NONE} or combination of flags like
7879
* {@link #POLICY_REJECT_METERED_BACKGROUND}.
7980
*/
80-
public void setAppPolicy(int appId, int policy) {
81+
public void setUidPolicy(int uid, int policy) {
8182
try {
82-
mService.setAppPolicy(appId, policy);
83+
mService.setUidPolicy(uid, policy);
8384
} catch (RemoteException e) {
8485
}
8586
}
8687

87-
public int getAppPolicy(int appId) {
88+
public int getUidPolicy(int uid) {
8889
try {
89-
return mService.getAppPolicy(appId);
90+
return mService.getUidPolicy(uid);
9091
} catch (RemoteException e) {
9192
return POLICY_NONE;
9293
}
9394
}
9495

95-
public int[] getAppsWithPolicy(int policy) {
96+
public int[] getUidsWithPolicy(int policy) {
9697
try {
97-
return mService.getAppsWithPolicy(policy);
98+
return mService.getUidsWithPolicy(policy);
9899
} catch (RemoteException e) {
99100
return new int[0];
100101
}
@@ -236,8 +237,7 @@ public static void snapToCycleDay(Time time, int cycleDay) {
236237
@Deprecated
237238
public static boolean isUidValidForPolicy(Context context, int uid) {
238239
// first, quick-reject non-applications
239-
if (uid < android.os.Process.FIRST_APPLICATION_UID
240-
|| uid > android.os.Process.LAST_APPLICATION_UID) {
240+
if (!UserHandle.isApp(uid)) {
241241
return false;
242242
}
243243

core/java/android/os/UserHandle.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,19 @@ public static final boolean isSameApp(int uid1, int uid2) {
8787

8888
/** @hide */
8989
public static final boolean isIsolated(int uid) {
90-
uid = getAppId(uid);
91-
return uid >= Process.FIRST_ISOLATED_UID && uid <= Process.LAST_ISOLATED_UID;
90+
if (uid > 0) {
91+
final int appId = getAppId(uid);
92+
return appId >= Process.FIRST_ISOLATED_UID && appId <= Process.LAST_ISOLATED_UID;
93+
} else {
94+
return false;
95+
}
9296
}
9397

9498
/** @hide */
9599
public static boolean isApp(int uid) {
96100
if (uid > 0) {
97-
uid = UserHandle.getAppId(uid);
98-
return uid >= Process.FIRST_APPLICATION_UID && uid <= Process.LAST_APPLICATION_UID;
101+
final int appId = getAppId(uid);
102+
return appId >= Process.FIRST_APPLICATION_UID && appId <= Process.LAST_APPLICATION_UID;
99103
} else {
100104
return false;
101105
}

0 commit comments

Comments
 (0)