Skip to content

Commit 854b2b1

Browse files
committed
Surface list of apps with given network policy.
Bug: 6007276 Change-Id: I0f0e939ee6481496480c4afaa108c99eb158547c
1 parent a94afeb commit 854b2b1

File tree

5 files changed

+67
-38
lines changed

5 files changed

+67
-38
lines changed

core/java/android/net/INetworkPolicyManager.aidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ interface INetworkPolicyManager {
3232
/** Control UID policies. */
3333
void setAppPolicy(int appId, int policy);
3434
int getAppPolicy(int appId);
35+
int[] getAppsWithPolicy(int policy);
3536

3637
boolean isUidForeground(int uid);
3738

core/java/android/net/NetworkPolicyManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ public int getAppPolicy(int appId) {
9292
}
9393
}
9494

95+
public int[] getAppsWithPolicy(int policy) {
96+
try {
97+
return mService.getAppsWithPolicy(policy);
98+
} catch (RemoteException e) {
99+
return new int[0];
100+
}
101+
}
102+
95103
public void registerListener(INetworkPolicyListener listener) {
96104
try {
97105
mService.registerListener(listener);

core/java/com/android/internal/util/ArrayUtils.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,40 @@ public static <T> T[] removeElement(Class<T> kind, T[] array, T element) {
201201
}
202202
return array;
203203
}
204+
205+
public static int[] appendInt(int[] cur, int val) {
206+
if (cur == null) {
207+
return new int[] { val };
208+
}
209+
final int N = cur.length;
210+
for (int i = 0; i < N; i++) {
211+
if (cur[i] == val) {
212+
return cur;
213+
}
214+
}
215+
int[] ret = new int[N + 1];
216+
System.arraycopy(cur, 0, ret, 0, N);
217+
ret[N] = val;
218+
return ret;
219+
}
220+
221+
public static int[] removeInt(int[] cur, int val) {
222+
if (cur == null) {
223+
return null;
224+
}
225+
final int N = cur.length;
226+
for (int i = 0; i < N; i++) {
227+
if (cur[i] == val) {
228+
int[] ret = new int[N - 1];
229+
if (i > 0) {
230+
System.arraycopy(cur, 0, ret, 0, i);
231+
}
232+
if (i < (N - 1)) {
233+
System.arraycopy(cur, i + 1, ret, i, N - i - 1);
234+
}
235+
return ret;
236+
}
237+
}
238+
return cur;
239+
}
204240
}

services/java/com/android/server/net/NetworkPolicyManagerService.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import static android.net.TrafficStats.MB_IN_BYTES;
5151
import static android.telephony.TelephonyManager.SIM_STATE_READY;
5252
import static android.text.format.DateUtils.DAY_IN_MILLIS;
53+
import static com.android.internal.util.ArrayUtils.appendInt;
5354
import static com.android.internal.util.Preconditions.checkNotNull;
5455
import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT;
5556
import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readBooleanAttribute;
@@ -1215,6 +1216,23 @@ public int getAppPolicy(int appId) {
12151216
}
12161217
}
12171218

1219+
@Override
1220+
public int[] getAppsWithPolicy(int policy) {
1221+
mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
1222+
1223+
int[] appIds = new int[0];
1224+
synchronized (mRulesLock) {
1225+
for (int i = 0; i < mAppPolicy.size(); i++) {
1226+
final int appId = mAppPolicy.keyAt(i);
1227+
final int appPolicy = mAppPolicy.valueAt(i);
1228+
if (appPolicy == policy) {
1229+
appIds = appendInt(appIds, appId);
1230+
}
1231+
}
1232+
}
1233+
return appIds;
1234+
}
1235+
12181236
@Override
12191237
public void registerListener(INetworkPolicyListener listener) {
12201238
// TODO: create permission for observing network policy

services/java/com/android/server/pm/PackageManagerService.java

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package com.android.server.pm;
1818

19+
import static android.Manifest.permission.GRANT_REVOKE_PERMISSIONS;
20+
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
1921
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
2022
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
2123
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER;
2224
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
23-
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
24-
import static android.Manifest.permission.GRANT_REVOKE_PERMISSIONS;
25+
import static com.android.internal.util.ArrayUtils.appendInt;
26+
import static com.android.internal.util.ArrayUtils.removeInt;
2527
import static libcore.io.OsConstants.S_ISLNK;
2628

2729
import com.android.internal.app.IMediaContainerService;
@@ -1451,22 +1453,6 @@ void readPermission(XmlPullParser parser, String name)
14511453
}
14521454
}
14531455

1454-
static int[] appendInt(int[] cur, int val) {
1455-
if (cur == null) {
1456-
return new int[] { val };
1457-
}
1458-
final int N = cur.length;
1459-
for (int i=0; i<N; i++) {
1460-
if (cur[i] == val) {
1461-
return cur;
1462-
}
1463-
}
1464-
int[] ret = new int[N+1];
1465-
System.arraycopy(cur, 0, ret, 0, N);
1466-
ret[N] = val;
1467-
return ret;
1468-
}
1469-
14701456
static int[] appendInts(int[] cur, int[] add) {
14711457
if (add == null) return cur;
14721458
if (cur == null) return add;
@@ -1477,26 +1463,6 @@ static int[] appendInts(int[] cur, int[] add) {
14771463
return cur;
14781464
}
14791465

1480-
static int[] removeInt(int[] cur, int val) {
1481-
if (cur == null) {
1482-
return null;
1483-
}
1484-
final int N = cur.length;
1485-
for (int i=0; i<N; i++) {
1486-
if (cur[i] == val) {
1487-
int[] ret = new int[N-1];
1488-
if (i > 0) {
1489-
System.arraycopy(cur, 0, ret, 0, i);
1490-
}
1491-
if (i < (N-1)) {
1492-
System.arraycopy(cur, i + 1, ret, i, N - i - 1);
1493-
}
1494-
return ret;
1495-
}
1496-
}
1497-
return cur;
1498-
}
1499-
15001466
static int[] removeInts(int[] cur, int[] rem) {
15011467
if (rem == null) return cur;
15021468
if (cur == null) return cur;

0 commit comments

Comments
 (0)