Skip to content

Commit 4eb7e68

Browse files
Delete old cache files on startup
1 parent b964a84 commit 4eb7e68

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

app/src/main/java/protect/card_locker/MainActivity.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.android.material.floatingactionbutton.FloatingActionButton;
3636
import com.google.android.material.tabs.TabLayout;
3737

38+
import java.io.File;
3839
import java.io.UnsupportedEncodingException;
3940
import java.util.ArrayList;
4041
import java.util.Arrays;
@@ -201,6 +202,28 @@ public void onDestroyActionMode(ActionMode inputMode) {
201202
protected void onCreate(Bundle inputSavedInstanceState) {
202203
SplashScreen.installSplashScreen(this);
203204
super.onCreate(inputSavedInstanceState);
205+
206+
// Delete old cache files
207+
// These could be temporary images for the cropper, temporary images in LoyaltyCard toBundle/writeParcel/ etc.
208+
new Thread(() -> {
209+
long twentyFourHoursAgo = System.currentTimeMillis() - (1000 * 60 * 60 * 24);
210+
211+
File[] tempFiles = getCacheDir().listFiles();
212+
213+
if (tempFiles == null) {
214+
Log.e(TAG, "getCacheDir().listFiles() somehow returned null, this should never happen... Skipping cache cleanup...");
215+
return;
216+
}
217+
218+
for (File file : tempFiles) {
219+
if (file.lastModified() < twentyFourHoursAgo) {
220+
if (!file.delete()) {
221+
Log.w(TAG, "Failed to delete cache file " + file.getPath());
222+
}
223+
};
224+
}
225+
}).start();
226+
204227
// We should extract the share intent after we called the super.onCreate as it may need to spawn a dialog window and the app needs to be initialized to not crash
205228
extractIntentFields(getIntent());
206229

0 commit comments

Comments
 (0)