Always save long screenshot temp file to the same place

If we fail to clean up the file, they won't accumulate over time.

Bug: 191499234
Test: adb shell ls /data/user_de/0/com.android.systemui/cache/long_screenshot_cache* to view files
Test: Verify state restoration still works
Test: Verify temp file disappears when activity is done.
Test: Verify that temp file is overwritten when a new session begins.
Change-Id: Ie15065ef9ff3db91852df1a0dc96afd494e85b8c
This commit is contained in:
Matt Casey
2021-06-23 11:34:09 -04:00
parent 2a7139a34f
commit d687b131b3
2 changed files with 12 additions and 21 deletions

View File

@@ -111,30 +111,21 @@ class ImageExporter {
}
/**
* Stores the given Bitmap to a temp file.
* Writes the given Bitmap to outputFile.
*/
ListenableFuture<File> exportAsTempFile(Executor executor, Bitmap bitmap) {
ListenableFuture<File> exportToRawFile(Executor executor, Bitmap bitmap,
final File outputFile) {
return CallbackToFutureAdapter.getFuture(
(completer) -> {
executor.execute(() -> {
File cachePath;
try {
cachePath = File.createTempFile("long_screenshot_cache_", ".tmp");
try (FileOutputStream stream = new FileOutputStream(cachePath)) {
bitmap.compress(mCompressFormat, mQuality, stream);
} catch (IOException e) {
if (cachePath.exists()) {
//noinspection ResultOfMethodCallIgnored
cachePath.delete();
cachePath = null;
}
completer.setException(e);
}
if (cachePath != null) {
completer.set(cachePath);
}
try (FileOutputStream stream = new FileOutputStream(outputFile)) {
bitmap.compress(mCompressFormat, mQuality, stream);
completer.set(outputFile);
} catch (IOException e) {
// Failed to create a new file
if (outputFile.exists()) {
//noinspection ResultOfMethodCallIgnored
outputFile.delete();
}
completer.setException(e);
}
});

View File

@@ -228,8 +228,8 @@ public class LongScreenshotActivity extends Activity {
});
// Immediately export to temp image file for saved state
mCacheSaveFuture = mImageExporter.exportAsTempFile(mBackgroundExecutor,
mLongScreenshot.toBitmap());
mCacheSaveFuture = mImageExporter.exportToRawFile(mBackgroundExecutor,
mLongScreenshot.toBitmap(), new File(getCacheDir(), "long_screenshot_cache.png"));
mCacheSaveFuture.addListener(() -> {
try {
// Get the temp file path to persist, used in onSavedInstanceState