Merge "Add SDK sandbox network usage to corresponding apps." into tm-dev am: 63db54c290

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/17944004

Change-Id: I6e942c241c734314246623b7521af6ebf9d2896c
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Martijn Coenen
2022-05-10 20:33:13 +00:00
committed by Automerger Merge Worker
4 changed files with 38 additions and 14 deletions

View File

@@ -20,6 +20,7 @@ import android.content.Context;
import android.net.NetworkStats;
import android.net.NetworkTemplate;
import android.os.Bundle;
import android.os.Process;
import android.text.format.DateUtils;
import android.text.format.Formatter;
@@ -91,11 +92,19 @@ public class AppDataUsagePreferenceController extends AppInfoPreferenceControlle
@Override
public Loader<List<NetworkCycleDataForUid>> onCreateLoader(int id, Bundle args) {
final NetworkTemplate template = getTemplate(mContext);
return NetworkCycleDataForUidLoader.builder(mContext)
.addUid(mParent.getAppEntry().info.uid)
.setRetrieveDetail(false)
.setNetworkTemplate(template)
.build();
final int uid = mParent.getAppEntry().info.uid;
final NetworkCycleDataForUidLoader.Builder builder =
NetworkCycleDataForUidLoader.builder(mContext);
builder.setRetrieveDetail(false)
.setNetworkTemplate(template);
builder.addUid(uid);
if (Process.isApplicationUid(uid)) {
// Also add in network usage for the app's SDK sandbox
builder.addUid(Process.toSdkSandboxUid(uid));
}
return builder.build();
}
@Override

View File

@@ -25,6 +25,7 @@ import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.NetworkTemplate;
import android.os.Bundle;
import android.os.Process;
import android.os.UserHandle;
import android.telephony.SubscriptionManager;
import android.util.ArraySet;
@@ -140,6 +141,14 @@ public class AppDataUsage extends DataUsageBaseFragment implements OnPreferenceC
}
}
if (mAppItem.key > 0 && UserHandle.isApp(mAppItem.key)) {
// In case we've been asked data usage for an app, automatically
// include data usage of the corresponding SDK sandbox
final int appSandboxUid = Process.toSdkSandboxUid(mAppItem.key);
if (!mAppItem.uids.get(appSandboxUid)) {
mAppItem.addUid(appSandboxUid);
}
}
mTotalUsage = findPreference(KEY_TOTAL_USAGE);
mForegroundUsage = findPreference(KEY_FOREGROUND_USAGE);
mBackgroundUsage = findPreference(KEY_BACKGROUND_USAGE);
@@ -307,6 +316,10 @@ public class AppDataUsage extends DataUsageBaseFragment implements OnPreferenceC
}
private void addUid(int uid) {
if (Process.isSdkSandboxUid(uid)) {
// For a sandbox process, get the associated app UID
uid = Process.getAppUidForSdkSandboxUid(uid);
}
String[] packages = mPackageManager.getPackagesForUid(uid);
if (packages != null) {
for (int i = 0; i < packages.length; i++) {
@@ -404,12 +417,8 @@ public class AppDataUsage extends DataUsageBaseFragment implements OnPreferenceC
= NetworkCycleDataForUidLoader.builder(mContext);
builder.setRetrieveDetail(true)
.setNetworkTemplate(mTemplate);
if (mAppItem.category == AppItem.CATEGORY_USER) {
for (int i = 0; i < mAppItem.uids.size(); i++) {
builder.addUid(mAppItem.uids.keyAt(i));
}
} else {
builder.addUid(mAppItem.key);
for (int i = 0; i < mAppItem.uids.size(); i++) {
builder.addUid(mAppItem.uids.keyAt(i));
}
if (mCycles != null) {
builder.setCycles(mCycles);

View File

@@ -369,7 +369,7 @@ public class DataUsageList extends DataUsageBaseFragment
final int collapseKey;
final int category;
final int userId = UserHandle.getUserId(uid);
if (UserHandle.isApp(uid)) {
if (UserHandle.isApp(uid) || Process.isSdkSandboxUid(uid)) {
if (profiles.contains(new UserHandle(userId))) {
if (userId != currentUserId) {
// Add to a managed user item.
@@ -377,8 +377,12 @@ public class DataUsageList extends DataUsageBaseFragment
largest = accumulate(managedKey, knownItems, bucket,
AppItem.CATEGORY_USER, items, largest);
}
// Add to app item.
collapseKey = uid;
// Map SDK sandbox back to its corresponding app
if (Process.isSdkSandboxUid(uid)) {
collapseKey = Process.getAppUidForSdkSandboxUid(uid);
} else {
collapseKey = uid;
}
category = AppItem.CATEGORY_APP;
} else {
// If it is a removed user add it to the removed users' key
@@ -416,6 +420,7 @@ public class DataUsageList extends DataUsageBaseFragment
if (item == null) {
item = new AppItem(uid);
item.total = -1;
item.addUid(uid);
items.add(item);
knownItems.put(item.key, item);
}

View File

@@ -302,6 +302,7 @@ public class AppDataUsageTest {
final Context context = RuntimeEnvironment.application;
final int testUid = 123123;
final AppItem appItem = new AppItem(testUid);
appItem.addUid(testUid);
appItem.category = AppItem.CATEGORY_APP;
ReflectionHelpers.setField(mFragment, "mContext", context);
ReflectionHelpers.setField(mFragment, "mAppItem", appItem);