Unhide the storage attribution API with system visibility.
Bug: 179160886
Test: StorageStatsManagerLocalTest
Change-Id: I3a8994e215baff123df65d89d2d71b9e12de2af5
diff --git a/services/api/current.txt b/services/api/current.txt
index 475dcf5..50f0052 100644
--- a/services/api/current.txt
+++ b/services/api/current.txt
@@ -60,6 +60,20 @@
}
+package com.android.server.usage {
+
+ public interface StorageStatsManagerLocal {
+ method public void registerStorageStatsAugmenter(@NonNull com.android.server.usage.StorageStatsManagerLocal.StorageStatsAugmenter, @NonNull String);
+ }
+
+ public static interface StorageStatsManagerLocal.StorageStatsAugmenter {
+ method public void augmentStatsForPackageForUser(@NonNull android.content.pm.PackageStats, @NonNull String, @NonNull android.os.UserHandle, boolean);
+ method public void augmentStatsForUid(@NonNull android.content.pm.PackageStats, int, boolean);
+ method public void augmentStatsForUser(@NonNull android.content.pm.PackageStats, @NonNull android.os.UserHandle);
+ }
+
+}
+
package com.android.server.wifi {
public class SupplicantManager {
diff --git a/services/usage/java/com/android/server/usage/StorageStatsManagerInternal.java b/services/usage/java/com/android/server/usage/StorageStatsManagerInternal.java
deleted file mode 100644
index 47760ef..0000000
--- a/services/usage/java/com/android/server/usage/StorageStatsManagerInternal.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (C) 2020 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.server.usage;
-
-import android.annotation.NonNull;
-import android.annotation.UserIdInt;
-import android.content.pm.PackageStats;
-
-/**
- * StorageStatsManager local system service interface.
- *
- * Only for use within the system server.
- */
-public abstract class StorageStatsManagerInternal {
- /**
- * Class used to augment {@link PackageStats} with the data stored by the system on
- * behalf of apps in system specific directories
- * ({@link android.os.Environment#getDataSystemDirectory},
- * {@link android.os.Environment#getDataSystemCeDirectory}, etc).
- */
- public interface StorageStatsAugmenter {
- void augmentStatsForPackage(@NonNull PackageStats stats,
- @NonNull String packageName, @UserIdInt int userId,
- boolean callerHasStatsPermission);
- void augmentStatsForUid(@NonNull PackageStats stats, int uid,
- boolean callerHasStatsPermission);
- }
-
- /**
- * Register a {@link StorageStatsAugmenter}.
- *
- * @param augmenter the {@link StorageStatsAugmenter} object to be registered.
- * @param tag the identifier to be used for debugging in logs/trace.
- */
- public abstract void registerStorageStatsAugmenter(@NonNull StorageStatsAugmenter augmenter,
- @NonNull String tag);
-}
diff --git a/services/usage/java/com/android/server/usage/StorageStatsManagerLocal.java b/services/usage/java/com/android/server/usage/StorageStatsManagerLocal.java
new file mode 100644
index 0000000..7cac820
--- /dev/null
+++ b/services/usage/java/com/android/server/usage/StorageStatsManagerLocal.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.usage;
+
+import android.annotation.NonNull;
+import android.annotation.SystemApi;
+import android.content.pm.PackageStats;
+import android.os.UserHandle;
+
+/**
+ * StorageStatsManager local system service interface.
+ *
+ * @hide Only for use within the system server.
+ */
+@SystemApi(client = SystemApi.Client.SYSTEM_SERVER)
+public interface StorageStatsManagerLocal {
+ /**
+ * Class used to augment {@link PackageStats} with the data stored by the system on
+ * behalf of apps in system specific directories
+ * ({@link android.os.Environment#getDataSystemDirectory},
+ * {@link android.os.Environment#getDataSystemCeDirectory}, etc).
+ */
+ interface StorageStatsAugmenter {
+ /**
+ * Augments {@link PackageStats} with data stored by the system for the given package.
+ *
+ * @param stats Structure to modify with usage data
+ * @param packageName Package name of the app whose data is stored by the
+ * system and needs to be added to {@code stats}.
+ * @param userHandle Device user for which usage stats are being requested.
+ * @param canCallerAccessAllStats Whether the caller who is requesting the storage stats
+ * can query stats for packages other than itself. For
+ * example, holding the PACKAGE_USAGE_STATS permission is one
+ * way to accomplish this.
+ */
+ void augmentStatsForPackageForUser(
+ @NonNull PackageStats stats,
+ @NonNull String packageName,
+ @NonNull UserHandle userHandle,
+ boolean canCallerAccessAllStats);
+
+ /**
+ * Augments {@link PackageStats} with data stored by the system for the given uid.
+ *
+ * @param stats Structure to modify with usage data
+ * @param uid Unique app ID for the app instance whose stats are being
+ * requested.
+ * @param canCallerAccessAllStats Whether the caller who is requesting the storage stats
+ * can query stats for packages other than itself. For
+ * example, holding the PACKAGE_USAGE_STATS permission is one
+ * way to accomplish this.
+ */
+ void augmentStatsForUid(
+ @NonNull PackageStats stats, int uid, boolean canCallerAccessAllStats);
+
+ /**
+ * Augments {@link PackageStats} with data stored by the system for the given device user.
+ *
+ * @param stats Structure to modify with usage data
+ * @param userHandle Device user whose data is stored by the system and needs to be added to
+ * {@code stats}.
+ */
+ void augmentStatsForUser(@NonNull PackageStats stats, @NonNull UserHandle userHandle);
+ }
+
+ /**
+ * Register a {@link StorageStatsAugmenter}.
+ *
+ * @param augmenter the {@link StorageStatsAugmenter} object to be registered.
+ * @param tag the identifier to be used for debugging in logs/trace.
+ */
+ void registerStorageStatsAugmenter(
+ @NonNull StorageStatsAugmenter augmenter, @NonNull String tag);
+}
diff --git a/services/usage/java/com/android/server/usage/StorageStatsService.java b/services/usage/java/com/android/server/usage/StorageStatsService.java
index a0a3909..b056de0 100644
--- a/services/usage/java/com/android/server/usage/StorageStatsService.java
+++ b/services/usage/java/com/android/server/usage/StorageStatsService.java
@@ -20,7 +20,7 @@
import static com.android.internal.util.ArrayUtils.defeatNullable;
import static com.android.server.pm.PackageManagerService.PLATFORM_PACKAGE_NAME;
-import static com.android.server.usage.StorageStatsManagerInternal.StorageStatsAugmenter;
+import static com.android.server.usage.StorageStatsManagerLocal.StorageStatsAugmenter;
import android.Manifest;
import android.annotation.NonNull;
@@ -71,6 +71,7 @@
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.Preconditions;
import com.android.server.IoThread;
+import com.android.server.LocalManagerRegistry;
import com.android.server.LocalServices;
import com.android.server.SystemService;
import com.android.server.pm.Installer;
@@ -152,7 +153,7 @@
}
});
- LocalServices.addService(StorageStatsManagerInternal.class, new LocalService());
+ LocalManagerRegistry.addManager(StorageStatsManagerLocal.class, new LocalService());
}
private void invalidateMounts() {
@@ -335,9 +336,10 @@
throw new ParcelableException(new IOException(e.getMessage()));
}
if (volumeUuid == StorageManager.UUID_PRIVATE_INTERNAL) {
+ UserHandle userHandle = UserHandle.of(userId);
forEachStorageStatsAugmenter((storageStatsAugmenter) -> {
- storageStatsAugmenter.augmentStatsForPackage(stats,
- packageName, userId, callerHasStatsPermission);
+ storageStatsAugmenter.augmentStatsForPackageForUser(stats,
+ packageName, userHandle, callerHasStatsPermission);
}, "queryStatsForPackage");
}
return translate(stats);
@@ -430,6 +432,12 @@
} catch (InstallerException e) {
throw new ParcelableException(new IOException(e.getMessage()));
}
+ if (volumeUuid == StorageManager.UUID_PRIVATE_INTERNAL) {
+ UserHandle userHandle = UserHandle.of(userId);
+ forEachStorageStatsAugmenter((storageStatsAugmenter) -> {
+ storageStatsAugmenter.augmentStatsForUser(stats, userHandle);
+ }, "queryStatsForUser");
+ }
return translate(stats);
}
@@ -784,7 +792,7 @@
}
}
- private class LocalService extends StorageStatsManagerInternal {
+ private class LocalService implements StorageStatsManagerLocal {
@Override
public void registerStorageStatsAugmenter(
@NonNull StorageStatsAugmenter storageStatsAugmenter,
diff --git a/services/usage/java/com/android/server/usage/package-info.java b/services/usage/java/com/android/server/usage/package-info.java
new file mode 100644
index 0000000..0719df4
--- /dev/null
+++ b/services/usage/java/com/android/server/usage/package-info.java
@@ -0,0 +1,18 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/** @hide */
+package com.android.server.usage;