Skip to content

Commit a5802e6

Browse files
committed
properly fix live wallpaper issues
the problem was simply due to the save/restore style functionality
1 parent de39c83 commit a5802e6

File tree

6 files changed

+42
-107
lines changed

6 files changed

+42
-107
lines changed

core/src/processing/a2d/PGraphicsAndroid2D.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,10 @@ public void endDraw() {
238238
}
239239
} finally {
240240
if (screen != null) {
241-
parent.getSurfaceHolder().unlockCanvasAndPost(screen);
241+
try {
242+
parent.getSurfaceHolder().unlockCanvasAndPost(screen);
243+
} catch (IllegalStateException ex) {
244+
}
242245
}
243246
}
244247
}

core/src/processing/android/PWallpaper.java

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,18 @@
2323
package processing.android;
2424

2525
import android.service.wallpaper.WallpaperService;
26-
//import android.util.DisplayMetrics;
2726
import android.view.MotionEvent;
2827
import android.view.SurfaceHolder;
2928
import android.view.WindowManager;
3029
import processing.core.PApplet;
3130
import android.util.DisplayMetrics;
32-
import android.util.Log;
3331
import android.os.Build;
34-
//import android.view.WindowManager;
3532
import android.view.Display;
3633
import android.graphics.Point;
3734

3835
public class PWallpaper extends WallpaperService implements AppComponent {
3936
String TAG = "PWallpaper";
4037

41-
private PApplet sketch;
42-
4338
protected Point size;
4439
private DisplayMetrics metrics;
4540
protected PEngine engine;
@@ -92,11 +87,11 @@ public float getDisplayDensity() {
9287
}
9388

9489
public void setSketch(PApplet sketch) {
95-
// engine.sketch = sketch;
90+
engine.sketch = sketch;
9691
}
9792

9893
public PApplet getSketch() {
99-
return sketch;
94+
return engine.sketch;
10095
}
10196

10297
public PApplet createSketch() {
@@ -129,33 +124,17 @@ public Engine onCreateEngine() {
129124
}
130125

131126
public class PEngine extends Engine {
127+
protected PApplet sketch;
132128

133129
@Override
134130
public void onCreate(SurfaceHolder surfaceHolder) {
135131
super.onCreate(surfaceHolder);
136-
if (sketch == null || isPreview()) {
137-
// Creating the sketch for the first time, this means that we are
138-
// in preview mode
139-
sketch = createSketch();
140-
sketch.initSurface(PWallpaper.this, getSurfaceHolder());
141-
sketch.startSurface();
142-
// By default we don't get touch events, so enable them.
143-
setTouchEventsEnabled(true);
144-
} else {
145-
// Sketch already exists, so we are launching the "real" wallpaper.
146-
// Since the containing process is still the same, we don't want to
147-
// recreate the sketch and all associated resources (renderer, etc) not
148-
// only to keep resource usage/starting times low, but also because
149-
// otherwise strange things might happen (e.g.: static variables in places
150-
// like PConstants are zeroed when the preview instance is disposed, but
151-
// still accessed by the new instance).
152-
sketch.resetSurface(PWallpaper.this, getSurfaceHolder());
153-
sketch.startSurface();
154-
}
155-
if (sketch != null) {
156-
sketch.preview = isPreview();
157-
if (!sketch.preview) requestPermissions();
158-
}
132+
sketch = createSketch();
133+
sketch.initSurface(PWallpaper.this, getSurfaceHolder());
134+
sketch.startSurface();
135+
sketch.preview = isPreview();
136+
if (isPreview()) requestPermissions();
137+
setTouchEventsEnabled(true);
159138
}
160139

161140
@Override
@@ -174,7 +153,7 @@ public void onSurfaceChanged(final SurfaceHolder holder, final int format,
174153

175154
@Override
176155
public void onVisibilityChanged(boolean visible) {
177-
if (!isPreview() && sketch != null) {
156+
if (sketch != null) {
178157
if (visible) {
179158
sketch.onResume();
180159
} else {
@@ -191,7 +170,9 @@ public void onVisibilityChanged(boolean visible) {
191170
@Override
192171
public void onTouchEvent(MotionEvent event) {
193172
super.onTouchEvent(event);
194-
if (sketch != null) sketch.surfaceTouchEvent(event);
173+
if (sketch != null) {
174+
sketch.surfaceTouchEvent(event);
175+
}
195176
}
196177

197178
@Override
@@ -204,23 +185,26 @@ public void onOffsetsChanged(float xOffset, float yOffset,
204185

205186
@Override
206187
public void onSurfaceDestroyed(SurfaceHolder holder) {
207-
// This is called immediately before a surface is being destroyed. After returning from this
208-
// call, you should no longer try to access this surface. If you have a rendering thread that
209-
// directly accesses the surface, you must ensure that thread is no longer touching the
210-
// Surface before returning from this function.
188+
// This is called immediately before a surface is being destroyed.
189+
// After returning from this call, you should no longer try to access this
190+
// surface. If you have a rendering thread that directly accesses the
191+
// surface, you must ensure that thread is no longer touching the Surface
192+
// before returning from this function.
211193
super.onSurfaceDestroyed(holder);
212194
}
213195

214196
@Override
215197
public void onDestroy() {
216198
super.onDestroy();
217-
if (!isPreview() && sketch != null) {
199+
if (sketch != null) {
218200
sketch.onDestroy();
219201
}
220202
}
221203

222204
public void onPermissionsGranted() {
223-
if (sketch != null) sketch.onPermissionsGranted();
205+
if (sketch != null) {
206+
sketch.onPermissionsGranted();
207+
}
224208
}
225209
}
226210
}

core/src/processing/core/PApplet.java

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -520,40 +520,6 @@ public void initSurface(LayoutInflater inflater, ViewGroup container,
520520
if (DEBUG) println("Done with init surface");
521521
}
522522

523-
public void resetSurface(AppComponent component, SurfaceHolder holder) {
524-
parentLayout = -1;
525-
resetSurface(null, null, null, component, holder);
526-
}
527-
528-
public void resetSurface(LayoutInflater inflater, ViewGroup container,
529-
Bundle savedInstanceState,
530-
AppComponent component, SurfaceHolder holder) {
531-
if (surface != null) {
532-
// Don't kill the process (activity or service) after stopping the
533-
// animation thread, otherwise the app will just quit. This in particular
534-
// is needed by live wallpapers (since the preview and real wallpaper are
535-
// the same service process).
536-
surface.stopThread(false);
537-
surface.dispose();
538-
}
539-
surface = g.createSurface(component, holder, true);
540-
541-
if (parentLayout == -1) {
542-
setFullScreenVisibility();
543-
surface.initView(width, height);
544-
} else {
545-
surface.initView(inflater, container, savedInstanceState,
546-
fullScreen, width, height);
547-
}
548-
549-
// Reset frame count to start from setup() again
550-
frameCount = 0;
551-
552-
finished = false;
553-
looping = true;
554-
redraw = true;
555-
}
556-
557523

558524
public void startSurface() {
559525
surface.startThread();
@@ -614,8 +580,9 @@ public void onResume() {
614580

615581
public void onPause() {
616582
// TODO need to save all application state here!
617-
// At least we save the current style.
618-
if (g != null) {
583+
// At least we save the current style (once we had at least drawn one
584+
// frame, otherwise we might be saving a "null" style with all zeroes).
585+
if (g != null && 0 < frameCount) {
619586
savedStyle = new PStyle();
620587
g.getStyle(savedStyle);
621588
}
@@ -2770,17 +2737,13 @@ final public void dispose() {
27702737
// moved here from stop()
27712738
finished = true; // let the sketch know it is shut down time
27722739

2773-
// don't run stop and disposers twice
2774-
// if (thread == null) return;
2775-
// thread = null;
2776-
27772740
// call to shut down renderer, in case it needs it (pdf does)
2778-
// if (surface != null) surface.dispose();
2779-
if (surface != null && surface.stopThread()) { // TODO stopping the thread for good?
2780-
if (g != null) {
2781-
g.dispose(); // TODO this would call PSurface.finish(), and so quit activity... but shouldn't
2782-
surface.dispose();
2783-
}
2741+
if (surface != null) {
2742+
surface.stopThread();
2743+
surface.dispose();
2744+
}
2745+
if (g != null) {
2746+
g.dispose();
27842747
}
27852748

27862749
handleMethods("dispose");

core/src/processing/core/PSurface.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ public void initView(LayoutInflater inflater, ViewGroup container,
8787

8888
public boolean stopThread();
8989

90-
public boolean stopThread(boolean killProc);
91-
9290
public boolean isStopped();
9391

9492
public void finish();

core/src/processing/core/PSurfaceNone.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ public class PSurfaceNone implements PSurface, PConstants {
6666
protected float frameRateTarget = 60;
6767
protected long frameRatePeriod = 1000000000L / 60L;
6868

69-
protected boolean killProcAfterStop = true;
70-
7169
@Override
7270
public Context getContext() {
7371
if (component.getKind() == AppComponent.FRAGMENT) {
@@ -351,19 +349,14 @@ public void resumeThread() {
351349

352350
@Override
353351
public boolean stopThread() {
354-
return stopThread(true);
355-
}
356-
357-
@Override
358-
public boolean stopThread(boolean killProc) {
359-
killProcAfterStop = killProc;
360352
if (thread == null) {
361353
return false;
362354
}
363355
thread = null;
364356
return true;
365357
}
366358

359+
367360
@Override
368361
public boolean isStopped() {
369362
return thread == null;
@@ -453,7 +446,7 @@ public void run() { // not good to make this synchronized, locks things up
453446
beforeTime = System.nanoTime();
454447
}
455448

456-
if (killProcAfterStop) finish();
449+
finish();
457450
}
458451
}
459452
}

core/src/processing/opengl/PSurfaceGLES.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ public class PSurfaceGLES extends PSurfaceNone {
5050
public PGLES pgl;
5151
private SketchSurfaceViewGL glsurf;
5252

53-
/** The renderer object driving the rendering loop, analogous to the
54-
* GLEventListener in JOGL */
55-
protected AndroidRenderer renderer;
56-
protected AndroidConfigChooser configChooser;
57-
5853
public PSurfaceGLES() { }
5954

6055
public PSurfaceGLES(PGraphics graphics, AppComponent component, SurfaceHolder holder) {
@@ -242,8 +237,9 @@ public boolean onKeyUp(int code, android.view.KeyEvent event) {
242237

243238

244239
public AndroidRenderer getRenderer() {
245-
renderer = new AndroidRenderer();
246-
return renderer;
240+
// renderer = new AndroidRenderer();
241+
// return renderer;
242+
return new AndroidRenderer();
247243
}
248244

249245

@@ -253,15 +249,13 @@ public AndroidContextFactory getContextFactory() {
253249

254250

255251
public AndroidConfigChooser getConfigChooser(int samples) {
256-
configChooser = new AndroidConfigChooser(5, 6, 5, 4, 16, 1, samples);
257-
return configChooser;
252+
return new AndroidConfigChooser(5, 6, 5, 4, 16, 1, samples);
258253
}
259254

260255

261256
public AndroidConfigChooser getConfigChooser(int r, int g, int b, int a,
262257
int d, int s, int samples) {
263-
configChooser = new AndroidConfigChooser(r, g, b, a, d, s, samples);
264-
return configChooser;
258+
return new AndroidConfigChooser(r, g, b, a, d, s, samples);
265259
}
266260

267261

0 commit comments

Comments
 (0)