Skip to content

Commit 85062a5

Browse files
committed
updates/fixes to activty lifecyle
1 parent e32d812 commit 85062a5

File tree

11 files changed

+278
-117
lines changed

11 files changed

+278
-117
lines changed

core/src/processing/a2d/PGraphicsAndroid2D.java

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
package processing.a2d;
2525

26+
import java.nio.ByteBuffer;
2627
import java.io.InputStream;
2728
import java.util.zip.GZIPInputStream;
2829

@@ -38,12 +39,15 @@
3839
import processing.core.PShapeSVG;
3940
import processing.core.PSurface;
4041
import processing.data.XML;
42+
43+
import android.annotation.SuppressLint;
4144
import android.app.Activity;
4245
import android.app.ActivityManager;
4346
import android.app.ActivityManager.MemoryInfo;
4447
import android.graphics.*;
4548
import android.graphics.Bitmap.Config;
4649
import android.graphics.Paint.Style;
50+
import android.os.Build;
4751
import android.view.SurfaceHolder;
4852

4953

@@ -110,6 +114,17 @@ public class PGraphicsAndroid2D extends PGraphics {
110114
*/
111115
protected boolean sized;
112116

117+
/**
118+
* Marks when some changes have occurred, to the surface view.
119+
*/
120+
protected boolean changed;
121+
122+
//////////////////////////////////////////////////////////////
123+
124+
/** To save the surface contents before the activity is taken to the background. */
125+
private int restoreCount;
126+
private ByteBuffer restoreBitmap;
127+
113128
//////////////////////////////////////////////////////////////
114129

115130
// INTERNAL
@@ -140,11 +155,16 @@ public PGraphicsAndroid2D() {
140155

141156
//public void setPath(String path)
142157

158+
@Override
159+
public void surfaceChanged() {
160+
changed = true;
161+
}
162+
143163

144164
@Override
145165
public void setSize(int iwidth, int iheight) {
166+
sized = iwidth != width || iheight != height;
146167
super.setSize(iwidth, iheight);
147-
sized = true;
148168
}
149169

150170

@@ -180,34 +200,29 @@ public void requestDraw() {
180200
//parent.handleDraw();
181201
// }
182202

183-
203+
@SuppressLint("NewApi")
184204
protected Canvas checkCanvas() {
185205
if ((canvas == null || sized) && (useBitmap || !primaryGraphics)) {
186-
if (bitmap == null || bitmap.getWidth() * bitmap.getHeight() < width * height) {
206+
if (bitmap == null || bitmap.getWidth() * bitmap.getHeight() < width * height ||
207+
Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
187208
if (bitmap != null) bitmap.recycle();
188209
bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
189210
} else {
211+
// reconfigure is only available in API level 19 or higher.
190212
bitmap.reconfigure(width, height, bitmap.getConfig());
191213
}
192214
canvas = new Canvas(bitmap);
193215
sized = false;
194216
}
217+
restoreSurface();
195218
return canvas;
196219
}
197220

198221

199222
@Override
200223
public void beginDraw() {
201-
canvas = checkCanvas();
202224

203-
// if (primaryGraphics) {
204-
// canvas = parent.getSurfaceHolder().lockCanvas(null);
205-
// if (canvas == null) {
206-
// throw new RuntimeException("canvas is still null");
207-
// }
208-
// } else {
209-
// throw new RuntimeException("not primary surface");
210-
// }
225+
canvas = checkCanvas();
211226

212227
checkSettings();
213228

@@ -222,16 +237,6 @@ public void beginDraw() {
222237
public void endDraw() {
223238
if (bitmap == null) return;
224239

225-
// hm, mark pixels as changed, because this will instantly do a full
226-
// copy of all the pixels to the surface.. so that's kind of a mess.
227-
//updatePixels();
228-
229-
// if (primaryGraphics) {
230-
// if (canvas != null) {
231-
// parent.getSurfaceHolder().unlockCanvasAndPost(canvas);
232-
// }
233-
// }
234-
235240
if (primaryGraphics) {
236241
SurfaceHolder holder = parent.getSurface().getSurfaceHolder();
237242
if (holder != null) {
@@ -2064,6 +2069,40 @@ public void resize(int wide, int high) {
20642069
}
20652070

20662071

2072+
@Override
2073+
protected void saveState() {
2074+
if (bitmap != null) {
2075+
int size = bitmap.getHeight() * bitmap.getRowBytes();
2076+
restoreBitmap = ByteBuffer.allocate(size);
2077+
bitmap.copyPixelsToBuffer(restoreBitmap);
2078+
}
2079+
}
2080+
2081+
2082+
@Override
2083+
protected void restoreState() {
2084+
}
2085+
2086+
2087+
@Override
2088+
protected void restoreSurface() {
2089+
if (changed) {
2090+
changed = false;
2091+
if (restoreBitmap != null) {
2092+
// Set the counter to 1 so the restore bitmap is drawn in the next frame.
2093+
restoreCount = 1;
2094+
}
2095+
} else if (restoreCount > 0) {
2096+
restoreCount--;
2097+
if (restoreCount == 0) {
2098+
// Draw and dispose bitmap
2099+
restoreBitmap.rewind();
2100+
bitmap.copyPixelsFromBuffer(restoreBitmap);
2101+
restoreBitmap = null;
2102+
}
2103+
}
2104+
}
2105+
20672106

20682107
//////////////////////////////////////////////////////////////
20692108

core/src/processing/a2d/PSurfaceAndroid2D.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ public void surfaceChanged(SurfaceHolder holder, int format, int iwidth, int ihe
123123
System.out.println("SketchSurfaceView.surfaceChanged() " + iwidth + " " + iheight);
124124
}
125125

126+
sketch.surfaceChanged();
127+
graphics.surfaceChanged();
128+
126129
sketch.setSize(iwidth, iheight);
127130
graphics.setSize(sketch.sketchWidth(), sketch.sketchHeight());
128-
sketch.surfaceChanged();
129131
}
130132

131-
132133
@Override
133134
public void onWindowFocusChanged(boolean hasFocus) {
134135
super.onWindowFocusChanged(hasFocus);

core/src/processing/core/PApplet.java

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
public class PApplet extends Object implements ActivityAPI, PConstants {
6666

6767
// static final public boolean DEBUG = true;
68-
static final public boolean DEBUG = false;
68+
static final public boolean DEBUG = true;
6969

7070
// Convenience public constant holding the SDK version, akin to platform in Java mode
7171
static final public int SDK = Build.VERSION.SDK_INT;
@@ -532,13 +532,13 @@ public void onResume() {
532532
// TODO need to bring back app state here!
533533
// At least we restore the current style.
534534
if (savedStyle != null && g != null) {
535+
g.restoreState();
535536
g.style(savedStyle);
536537
savedStyle = null;
537538
}
538539

539540
handleMethods("resume");
540541

541-
surface.resumeThread();
542542
if (0 < frameCount) {
543543
// Don't call resume() when the app is starting and setup() has not been
544544
// called yet
@@ -549,21 +549,25 @@ public void onResume() {
549549
// https://developer.android.com/guide/components/activities/activity-lifecycle.html
550550
resume();
551551
}
552+
553+
surface.resumeThread();
552554
}
553555

554556

555557
public void onPause() {
558+
surface.pauseThread();
559+
556560
// TODO need to save all application state here!
557561
// At least we save the current style (once we had at least drawn one
558562
// frame, otherwise we might be saving a "null" style with all zeroes).
559563
if (g != null && 0 < frameCount) {
560564
savedStyle = new PStyle();
561565
g.getStyle(savedStyle);
566+
g.saveState();
562567
}
563568

564569
handleMethods("pause");
565570

566-
surface.pauseThread();
567571
pause(); // handler for others to write
568572
}
569573

@@ -844,7 +848,6 @@ public void surfaceKeyUp(int code, android.view.KeyEvent event) {
844848
* PAppletGL needs to have a usable screen before getting things rolling.
845849
*/
846850
public void start() {
847-
surface.resumeThread();
848851
}
849852

850853

@@ -857,11 +860,6 @@ public void start() {
857860
* or when moving between web pages), and it's not always called.
858861
*/
859862
public void stop() {
860-
// this used to shut down the sketch, but that code has
861-
// been moved to dispose()
862-
surface.pauseThread();
863-
864-
//TODO listeners
865863
}
866864

867865

@@ -1322,30 +1320,27 @@ public void setExternal(boolean external) {
13221320
//. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13231321

13241322

1325-
public void smooth() {
1323+
public void smooth() {
13261324
smooth(1);
13271325
}
13281326

13291327

1330-
public void smooth(int level) {
1331-
if (insideSettings) {
1332-
this.smooth = level;
1333-
1334-
} else if (this.smooth != level) {
1335-
smoothWarning("smooth");
1336-
}
1337-
}
1338-
1339-
1340-
public void noSmooth() {
1341-
if (insideSettings) {
1342-
this.smooth = 0;
1328+
public void smooth(int level) {
1329+
if (insideSettings) {
1330+
this.smooth = level;
1331+
} else if (this.smooth != level) {
1332+
smoothWarning("smooth");
1333+
}
1334+
}
13431335

1344-
} else if (this.smooth != 0) {
1345-
smoothWarning("noSmooth");
1346-
}
1347-
}
13481336

1337+
public void noSmooth() {
1338+
if (insideSettings) {
1339+
this.smooth = 0;
1340+
} else if (this.smooth != 0) {
1341+
smoothWarning("noSmooth");
1342+
}
1343+
}
13491344

13501345
private void smoothWarning(String method) {
13511346
// When running from the PDE, say setup(), otherwise say settings()
@@ -1357,7 +1352,11 @@ private void smoothWarning(String method) {
13571352
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13581353

13591354

1360-
public void orientation(int which) {
1355+
public PGraphics getGraphics() {
1356+
return g;
1357+
}
1358+
1359+
public void orientation(int which) {
13611360
surface.setOrientation(which);
13621361
}
13631362

@@ -1756,11 +1755,6 @@ public void handleDraw() {
17561755
return;
17571756
}
17581757

1759-
// if (surfaceChanged) {
1760-
// surfaceChanged = false;
1761-
// surfaceReady = true;
1762-
// }
1763-
17641758
if (!looping && !redraw) return;
17651759

17661760
if (insideDraw) {
@@ -1839,12 +1833,6 @@ public void handleDraw() {
18391833
}
18401834

18411835

1842-
/** Not official API, not guaranteed to work in the future. */
1843-
// public boolean canDraw() {
1844-
// return g != null && surfaceReady && !paused && (looping || redraw);
1845-
// }
1846-
1847-
18481836
//////////////////////////////////////////////////////////////
18491837

18501838

core/src/processing/core/PGraphics.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,9 @@ public void setFrameRate(float framerate) { // ignore
687687
}
688688

689689

690+
public void surfaceChanged() { // ignore
691+
}
692+
690693
/**
691694
* The final step in setting up a renderer, set its size of this renderer.
692695
* This was formerly handled by the constructor, but instead it's been broken
@@ -903,7 +906,7 @@ protected void defaultSettings() { // ignore
903906
* size(), which is safely called from inside beginDraw(). And it cannot be
904907
* called before defaultSettings(), so we should be safe.
905908
*/
906-
protected void reapplySettings() {
909+
protected void reapplySettings() { // ignore
907910
// System.out.println("attempting reapplySettings()");
908911
if (!settingsInited) return; // if this is the initial setup, no need to reapply
909912

@@ -962,6 +965,18 @@ protected void reapplySettings() {
962965
}
963966

964967

968+
protected void saveState() { // ignore
969+
970+
}
971+
972+
protected void restoreState() { // ignore
973+
974+
}
975+
976+
protected void restoreSurface() { // ignore
977+
978+
}
979+
965980
//////////////////////////////////////////////////////////////
966981

967982
// HINTS

core/src/processing/core/PSurfaceNone.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
/**
5959
* Base surface for Android2D and OpenGL renderers.
60-
* It includes the standard rendering loop.
60+
* It includes the implementation of the rendering thread.
6161
*/
6262
public class PSurfaceNone implements PSurface, PConstants {
6363
protected PApplet sketch;
@@ -375,7 +375,6 @@ public void finish() {
375375
// }
376376
}
377377

378-
379378
///////////////////////////////////////////////////////////
380379

381380
// Thread handling
@@ -453,8 +452,9 @@ public void setFrameRate(float fps) {
453452

454453

455454
protected void checkPause() {
456-
if (paused) {
455+
// if (paused) {
457456
synchronized (pauseObject) {
457+
while (paused) {
458458
try {
459459
pauseObject.wait();
460460
} catch (InterruptedException e) {

0 commit comments

Comments
 (0)