Merge "Deleting old cache images before saving new ones." into ub-launcher3-rvc-dev am: 5ec76de182
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/11934850
Change-Id: I422c02a268a274b5f5035492d93aa4319c2b4d0a
diff --git a/quickstep/src/com/android/quickstep/util/ImageActionUtils.java b/quickstep/src/com/android/quickstep/util/ImageActionUtils.java
index f302fdd..e998e9a 100644
--- a/quickstep/src/com/android/quickstep/util/ImageActionUtils.java
+++ b/quickstep/src/com/android/quickstep/util/ImageActionUtils.java
@@ -19,6 +19,7 @@
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
+import static com.android.launcher3.util.Executors.THREAD_POOL_EXECUTOR;
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
import android.content.ClipData;
@@ -55,6 +56,9 @@
public class ImageActionUtils {
private static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".overview.fileprovider";
+ private static final long FILE_LIFE = 1000L /*ms*/ * 60L /*s*/ * 60L /*m*/ * 24L /*h*/;
+ private static final String SUB_FOLDER = "Overview";
+ private static final String BASE_NAME = "overview_image_";
/**
* Saves screenshot to location determine by SystemUiProxy
@@ -110,10 +114,13 @@
*/
@WorkerThread
public static Uri getImageUri(Bitmap bitmap, Rect crop, Context context, String tag) {
+ clearOldCacheFiles(context);
Bitmap croppedBitmap = cropBitmap(bitmap, crop);
int cropHash = crop == null ? 0 : crop.hashCode();
- String baseName = "image_" + bitmap.hashCode() + "_" + cropHash + ".png";
- File file = new File(context.getCacheDir(), baseName);
+ String baseName = BASE_NAME + bitmap.hashCode() + "_" + cropHash + ".png";
+ File parent = new File(context.getCacheDir(), SUB_FOLDER);
+ parent.mkdir();
+ File file = new File(parent, baseName);
try (FileOutputStream fos = new FileOutputStream(file)) {
croppedBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
@@ -175,4 +182,19 @@
.setClipData(clipdata);
return new Intent[]{Intent.createChooser(intent, null).addFlags(FLAG_ACTIVITY_NEW_TASK)};
}
+
+ private static void clearOldCacheFiles(Context context) {
+ THREAD_POOL_EXECUTOR.execute(() -> {
+ File parent = new File(context.getCacheDir(), SUB_FOLDER);
+ File[] files = parent.listFiles((File f, String s) -> s.startsWith(BASE_NAME));
+ if (files != null) {
+ for (File file: files) {
+ if (file.lastModified() + FILE_LIFE < System.currentTimeMillis()) {
+ file.delete();
+ }
+ }
+ }
+ });
+
+ }
}