Remove app metadata if data is null or empty

The getAppMetadata API will now remove any metadata that may have
already been set in the same install session if the data param is set to
null or a empty bundle.

Bug: 265896861
Test: atest android.packageinstaller.install.cts.InstallAppMetadataTest
Change-Id: I95c611c71f978629ca1e7781e3515e2a67cceba6
This commit is contained in:
William Loh
2023-02-01 22:21:41 +00:00
parent 2d3a9823bf
commit f7ee7642bc
3 changed files with 23 additions and 5 deletions

View File

@@ -69,4 +69,5 @@ interface IPackageInstallerSession {
ParcelFileDescriptor getAppMetadataFd();
ParcelFileDescriptor openWriteAppMetadata();
void removeAppMetadata();
}

View File

@@ -1956,16 +1956,22 @@ public class PackageInstaller {
/**
* Optionally set the app metadata. The size of this data cannot exceed the maximum allowed.
* Any existing data from the previous install will not be retained even if no data is set
* for the current install session.
* for the current install session. Setting data to null or an empty PersistableBundle will
* remove any metadata that has previously been set in the same session.
*
* @param data a PersistableBundle containing the app metadata. If this is set to null then
* any existing app metadata will be removed.
* @param data a PersistableBundle containing the app metadata.
* @throws IOException if writing the data fails.
*/
public void setAppMetadata(@Nullable PersistableBundle data) throws IOException {
if (data == null) {
if (data == null || data.isEmpty()) {
try {
mSession.removeAppMetadata();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
return;
}
Objects.requireNonNull(data);
try (OutputStream outputStream = openWriteAppMetadata()) {
data.writeToStream(outputStream);
}

View File

@@ -1520,7 +1520,10 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
public ParcelFileDescriptor getAppMetadataFd() {
assertCallerIsOwnerOrRoot();
synchronized (mLock) {
assertPreparedAndNotCommittedOrDestroyedLocked("openRead");
assertPreparedAndNotCommittedOrDestroyedLocked("getAppMetadataFd");
if (getStagedAppMetadataFile() == null) {
return null;
}
try {
return openReadInternalLocked(APP_METADATA_FILE_NAME);
} catch (IOException e) {
@@ -1529,6 +1532,14 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
}
}
@Override
public void removeAppMetadata() {
File file = getStagedAppMetadataFile();
if (file != null) {
file.delete();
}
}
private static long getAppMetadataSizeLimit() {
final long token = Binder.clearCallingIdentity();
try {