Skip to content

Commit 6d339b6

Browse files
committed
refactor services for consistency, addressing #237
1 parent d588c61 commit 6d339b6

File tree

6 files changed

+255
-81
lines changed

6 files changed

+255
-81
lines changed

core/src/processing/android/PWallpaper.java

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,10 @@
3333
import android.graphics.Point;
3434

3535
public class PWallpaper extends WallpaperService implements AppComponent {
36-
String TAG = "PWallpaper";
37-
3836
protected Point size;
3937
private DisplayMetrics metrics;
40-
protected PEngine engine;
41-
42-
public PWallpaper() {
43-
}
38+
protected WallpaperEngine engine;
4439

45-
public PWallpaper(PApplet sketch) {
46-
}
4740

4841
public void initDimensions() {
4942
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
@@ -68,64 +61,89 @@ public void initDimensions() {
6861
}
6962
}
7063

71-
public int getKind() {
72-
return WALLPAPER;
73-
}
7464

7565
public int getDisplayWidth() {
7666
return size.x;
7767
// return metrics.widthPixels;
7868
}
7969

70+
8071
public int getDisplayHeight() {
8172
return size.y;
8273
// return metrics.heightPixels;
8374
}
8475

76+
8577
public float getDisplayDensity() {
8678
return metrics.density;
8779
}
8880

81+
82+
public int getKind() {
83+
return WALLPAPER;
84+
}
85+
86+
87+
public PApplet createSketch() {
88+
return new PApplet();
89+
}
90+
91+
8992
public void setSketch(PApplet sketch) {
9093
engine.sketch = sketch;
9194
}
9295

96+
9397
public PApplet getSketch() {
9498
return engine.sketch;
9599
}
96100

97-
public PApplet createSketch() {
98-
return new PApplet();
101+
102+
public Engine getEngine() {
103+
return engine;
99104
}
100105

101-
public void requestDraw() {
102106

107+
public void requestDraw() {
103108
}
104109

110+
105111
public boolean canDraw() {
106112
return true;
107113
}
108114

115+
109116
public void dispose() {
110117
}
111118

112-
public void requestPermissions() {
113119

120+
public void requestPermissions() {
114121
}
115122

123+
116124
public void onPermissionsGranted() {
117125
if (engine != null) engine.onPermissionsGranted();
118126
}
119127

128+
120129
@Override
121130
public Engine onCreateEngine() {
122-
engine = new PEngine();
131+
engine = new WallpaperEngine();
123132
return engine;
124133
}
125134

126-
public class PEngine extends Engine {
135+
136+
@Override
137+
public void onDestroy() {
138+
super.onDestroy();
139+
if (engine != null) engine.onDestroy();
140+
}
141+
142+
143+
public class WallpaperEngine extends Engine {
127144
protected PApplet sketch;
128145

146+
129147
@Override
130148
public void onCreate(SurfaceHolder surfaceHolder) {
131149
super.onCreate(surfaceHolder);
@@ -137,11 +155,20 @@ public void onCreate(SurfaceHolder surfaceHolder) {
137155
setTouchEventsEnabled(true);
138156
}
139157

158+
159+
private void onPermissionsGranted() {
160+
if (sketch != null) {
161+
sketch.onPermissionsGranted();
162+
}
163+
}
164+
165+
140166
@Override
141167
public void onSurfaceCreated(SurfaceHolder surfaceHolder) {
142168
super.onSurfaceCreated(surfaceHolder);
143169
}
144170

171+
145172
@Override
146173
public void onSurfaceChanged(final SurfaceHolder holder, final int format,
147174
final int width, final int height) {
@@ -151,6 +178,7 @@ public void onSurfaceChanged(final SurfaceHolder holder, final int format,
151178
super.onSurfaceChanged(holder, format, width, height);
152179
}
153180

181+
154182
@Override
155183
public void onVisibilityChanged(boolean visible) {
156184
if (sketch != null) {
@@ -163,6 +191,7 @@ public void onVisibilityChanged(boolean visible) {
163191
super.onVisibilityChanged(visible);
164192
}
165193

194+
166195
/*
167196
* Store the position of the touch event so we can use it for drawing
168197
* later
@@ -175,6 +204,7 @@ public void onTouchEvent(MotionEvent event) {
175204
}
176205
}
177206

207+
178208
@Override
179209
public void onOffsetsChanged(float xOffset, float yOffset,
180210
float xOffsetStep, float yOffsetStep,
@@ -190,6 +220,7 @@ public void onOffsetsChanged(float xOffset, float yOffset,
190220
}
191221
}
192222

223+
193224
@Override
194225
public void onSurfaceDestroyed(SurfaceHolder holder) {
195226
// This is called immediately before a surface is being destroyed.
@@ -200,18 +231,13 @@ public void onSurfaceDestroyed(SurfaceHolder holder) {
200231
super.onSurfaceDestroyed(holder);
201232
}
202233

234+
203235
@Override
204236
public void onDestroy() {
205237
super.onDestroy();
206238
if (sketch != null) {
207239
sketch.onDestroy();
208240
}
209241
}
210-
211-
public void onPermissionsGranted() {
212-
if (sketch != null) {
213-
sketch.onPermissionsGranted();
214-
}
215-
}
216242
}
217243
}

0 commit comments

Comments
 (0)