Skip to content

Commit 42b3866

Browse files
committed
uses same file name for pixels cache, delete it on destroy
1 parent d2c7312 commit 42b3866

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

core/src/processing/a2d/PGraphicsAndroid2D.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@
5454
import android.graphics.Bitmap.Config;
5555
import android.graphics.Paint.Style;
5656
import android.os.Build;
57+
import android.os.Environment;
5758
import android.view.SurfaceHolder;
5859

60+
import static android.os.Environment.isExternalStorageRemovable;
61+
5962

6063
/**
6164
* Subclass for PGraphics that implements the graphics API using
@@ -2055,6 +2058,16 @@ public void resize(int wide, int high) {
20552058
}
20562059

20572060

2061+
@Override
2062+
protected void clearState() {
2063+
super.clearState();
2064+
if (restoreFilename != null) {
2065+
File cacheFile = new File(restoreFilename);
2066+
cacheFile.delete();
2067+
}
2068+
}
2069+
2070+
20582071
@Override
20592072
protected void saveState() {
20602073
super.saveState();
@@ -2070,9 +2083,13 @@ protected void saveState() {
20702083
ByteBuffer restoreBitmap = ByteBuffer.allocate(size);
20712084
bitmap.copyPixelsToBuffer(restoreBitmap);
20722085

2073-
File cacheDir = context.getCacheDir();
2074-
File cacheFile = File.createTempFile("processing", "pixels", cacheDir);
2086+
// Tries to use external but if not mounted, falls back on internal storage, as shown in
2087+
// https://developer.android.com/topic/performance/graphics/cache-bitmap#java
2088+
File cacheDir = Environment.MEDIA_MOUNTED == Environment.getExternalStorageState() || !isExternalStorageRemovable() ?
2089+
context.getExternalCacheDir() : context.getCacheDir();
2090+
File cacheFile = new File(cacheDir + File.separator + "restore_pixels");
20752091
restoreFilename = cacheFile.getAbsolutePath();
2092+
20762093
FileOutputStream stream = new FileOutputStream(cacheFile);
20772094
ObjectOutputStream dout = new ObjectOutputStream(stream);
20782095
byte[] array = new byte[size];

core/src/processing/core/PApplet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1896,7 +1896,7 @@ public void handleDraw() {
18961896

18971897

18981898
// This method handles some special situations on Android where beginDraw/endDraw are needed,
1899-
// but not to render the actualy contents of draw(). In general, this situations arise from
1899+
// but not to render the actual contents of draw(). In general, these situations arise from
19001900
// having to refresh/restore the screen after requesting no loop, or resuming the sketch in
19011901
// no-loop state.
19021902
protected boolean handleSpecialDraw() {
@@ -2908,6 +2908,7 @@ final public void dispose() {
29082908
surface.dispose();
29092909
}
29102910
if (g != null) {
2911+
g.clearState(); // This should probably go in dispose, but for the time being...
29112912
g.dispose();
29122913
}
29132914

core/src/processing/core/PGraphics.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,10 +982,16 @@ protected void reapplySettings() { // ignore
982982
// RENDERER STATE
983983

984984

985+
protected void clearState() { // ignore
986+
// Nothing to do here, it depends on the renderer's implementation.
987+
}
988+
989+
985990
protected void saveState() { // ignore
986991
// Nothing to do here, it depends on the renderer's implementation.
987992
}
988993

994+
989995
protected void restoreState() { // ignore
990996
// This method probably does not need to be re-implemented in the subclasses. All we need to
991997
// do is to check for the resume in no-loop state situation:

core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@
3939
import java.util.*;
4040

4141
import android.content.Context;
42+
import android.os.Environment;
4243
import android.view.SurfaceHolder;
4344

45+
import static android.os.Environment.isExternalStorageRemovable;
46+
4447
/**
4548
* OpenGL renderer.
4649
*/
@@ -5694,6 +5697,16 @@ protected void drawPixels(int[] pixBuffer, int x, int y, int w, int h) {
56945697
}
56955698

56965699

5700+
@Override
5701+
protected void clearState() {
5702+
super.clearState();
5703+
if (restoreFilename != null) {
5704+
File cacheFile = new File(restoreFilename);
5705+
cacheFile.delete();
5706+
}
5707+
}
5708+
5709+
56975710
@Override
56985711
protected void saveState() {
56995712
super.saveState();
@@ -5715,9 +5728,13 @@ public void run() {
57155728
pgl.readPixelsImpl(0, 0, pixelWidth, pixelHeight, PGL.RGBA, PGL.UNSIGNED_BYTE, buf);
57165729
endPixelsOp();
57175730

5718-
File cacheDir = context.getCacheDir();
5719-
File cacheFile = File.createTempFile("processing", "pixels", cacheDir);
5731+
// Tries to use external but if not mounted, falls back on internal storage, as shown in
5732+
// https://developer.android.com/topic/performance/graphics/cache-bitmap#java
5733+
File cacheDir = Environment.MEDIA_MOUNTED == Environment.getExternalStorageState() || !isExternalStorageRemovable() ?
5734+
context.getExternalCacheDir() : context.getCacheDir();
5735+
File cacheFile = new File(cacheDir + File.separator + "restore_pixels");
57205736
restoreFilename = cacheFile.getAbsolutePath();
5737+
57215738
FileOutputStream stream = new FileOutputStream(cacheFile);
57225739
ObjectOutputStream dout = new ObjectOutputStream(stream);
57235740
dout.writeObject(restorePixels);

0 commit comments

Comments
 (0)