2323
2424package processing .a2d ;
2525
26+ import java .io .File ;
27+ import java .io .FileInputStream ;
28+ import java .io .FileOutputStream ;
29+ import java .io .ObjectInputStream ;
30+ import java .io .ObjectOutputStream ;
2631import java .nio .ByteBuffer ;
2732import java .io .InputStream ;
2833import java .util .zip .GZIPInputStream ;
4449import android .app .Activity ;
4550import android .app .ActivityManager ;
4651import android .app .ActivityManager .MemoryInfo ;
52+ import android .content .Context ;
4753import android .graphics .*;
4854import android .graphics .Bitmap .Config ;
4955import android .graphics .Paint .Style ;
@@ -122,8 +128,9 @@ public class PGraphicsAndroid2D extends PGraphics {
122128 //////////////////////////////////////////////////////////////
123129
124130 /** To save the surface contents before the activity is taken to the background. */
131+ private String restoreFilename ;
132+ private int restoreWidth , restoreHeight ;
125133 private int restoreCount ;
126- private ByteBuffer restoreBitmap ;
127134
128135 //////////////////////////////////////////////////////////////
129136
@@ -184,22 +191,6 @@ public PSurface createSurface(AppComponent component, SurfaceHolder holder, bool
184191
185192 // FRAME
186193
187- /*
188- public void requestDraw() {
189- parent.surfaceView.requestRender();
190- }
191- */
192-
193- // public boolean canDraw() {
194- // return true;
195- // }
196-
197-
198- // @Override
199- // public void requestDraw() {
200- //parent.handleDraw();
201- // }
202-
203194 @ SuppressLint ("NewApi" )
204195 protected Canvas checkCanvas () {
205196 if ((canvas == null || sized ) && (useBitmap || !primaryGraphics )) {
@@ -221,7 +212,6 @@ protected Canvas checkCanvas() {
221212
222213 @ Override
223214 public void beginDraw () {
224-
225215 canvas = checkCanvas ();
226216
227217 checkSettings ();
@@ -253,6 +243,7 @@ public void endDraw() {
253243 try {
254244 holder .unlockCanvasAndPost (screen );
255245 } catch (IllegalStateException ex ) {
246+ } catch (IllegalArgumentException ex ) {
256247 }
257248 }
258249 }
@@ -2073,10 +2064,32 @@ public void resize(int wide, int high) {
20732064
20742065 @ Override
20752066 protected void saveState () {
2076- if (bitmap != null ) {
2067+ Context context = parent .getContext ();
2068+ if (context == null || bitmap == null || parent .getSurface ().getComponent ().isService ()) return ;
2069+ try {
2070+ // Saving current width and height to avoid restoring the screen after a screen rotation
2071+ restoreWidth = pixelWidth ;
2072+ restoreHeight = pixelHeight ;
2073+
20772074 int size = bitmap .getHeight () * bitmap .getRowBytes ();
2078- restoreBitmap = ByteBuffer .allocate (size );
2075+ ByteBuffer restoreBitmap = ByteBuffer .allocate (size );
20792076 bitmap .copyPixelsToBuffer (restoreBitmap );
2077+
2078+ File cacheDir = context .getCacheDir ();
2079+ File cacheFile = File .createTempFile ("processing" , "pixels" , cacheDir );
2080+ restoreFilename = cacheFile .getAbsolutePath ();
2081+ FileOutputStream stream = new FileOutputStream (cacheFile );
2082+ ObjectOutputStream dout = new ObjectOutputStream (stream );
2083+ byte [] array = new byte [size ];
2084+ restoreBitmap .rewind ();
2085+ restoreBitmap .get (array );
2086+ dout .writeObject (array );
2087+ dout .flush ();
2088+ stream .getFD ().sync ();
2089+ stream .close ();
2090+ } catch (Exception ex ) {
2091+ PGraphics .showWarning ("Could not save screen contents to cache" );
2092+ ex .printStackTrace ();
20802093 }
20812094 }
20822095
@@ -2090,17 +2103,35 @@ protected void restoreState() {
20902103 protected void restoreSurface () {
20912104 if (changed ) {
20922105 changed = false ;
2093- if (restoreBitmap != null ) {
2106+ if (restoreFilename != null && restoreWidth == pixelWidth && restoreHeight == pixelHeight ) {
20942107 // Set the counter to 1 so the restore bitmap is drawn in the next frame.
20952108 restoreCount = 1 ;
20962109 }
20972110 } else if (restoreCount > 0 ) {
20982111 restoreCount --;
20992112 if (restoreCount == 0 ) {
2100- // Draw and dispose bitmap
2101- restoreBitmap .rewind ();
2102- bitmap .copyPixelsFromBuffer (restoreBitmap );
2103- restoreBitmap = null ;
2113+ Context context = parent .getContext ();
2114+ if (context == null ) return ;
2115+ try {
2116+ // Load cached bitmap and draw
2117+ File cacheFile = new File (restoreFilename );
2118+ FileInputStream inStream = new FileInputStream (cacheFile );
2119+ ObjectInputStream din = new ObjectInputStream (inStream );
2120+ byte [] array = (byte []) din .readObject ();
2121+ ByteBuffer restoreBitmap = ByteBuffer .wrap (array );
2122+ if (restoreBitmap .capacity () == bitmap .getHeight () * bitmap .getRowBytes ()) {
2123+ restoreBitmap .rewind ();
2124+ bitmap .copyPixelsFromBuffer (restoreBitmap );
2125+ }
2126+ inStream .close ();
2127+ cacheFile .delete ();
2128+ restoreFilename = null ;
2129+ restoreWidth = -1 ;
2130+ restoreHeight = -1 ;
2131+ } catch (Exception ex ) {
2132+ PGraphics .showWarning ("Could not restore screen contents from cache" );
2133+ ex .printStackTrace ();
2134+ }
21042135 }
21052136 }
21062137 }
0 commit comments