Avoid calling installd where not needed

The method StorageStatsManager#getCacheBytes is called several times
during package installation. It calls
InstalldNativeService::getUserSize via a binder call.

The runtime of getUserSize is variable; I have traces where it takes
50ms and traces where it takes 500ms.

The return value of getUserSize used to take into account freeable cache
space when determining available space on a given volume. In most cases,
it is possible to first do a pessimistic check of the space available on
the volume - forgoing the installd call - and only consider cache space
if the pessimistic check fails.

This avoids a binder call + the time spend in getUserSize for most
installs.

Bug: None
Test: atest frameworks/base/core/tests/coretests/src/android/content/pm/PackageHelperTests.java
cts/tests/tests/os/src/android/os/storage/cts/StorageManagerTest.java
Change-Id: Icaecee732ef330fee1b409d2dd76723822c25959
3 files changed