33
33
import android .graphics .Point ;
34
34
35
35
public class PWallpaper extends WallpaperService implements AppComponent {
36
- String TAG = "PWallpaper" ;
37
-
38
36
protected Point size ;
39
37
private DisplayMetrics metrics ;
40
- protected PEngine engine ;
41
-
42
- public PWallpaper () {
43
- }
38
+ protected WallpaperEngine engine ;
44
39
45
- public PWallpaper (PApplet sketch ) {
46
- }
47
40
48
41
public void initDimensions () {
49
42
WindowManager wm = (WindowManager ) getSystemService (WINDOW_SERVICE );
@@ -68,64 +61,89 @@ public void initDimensions() {
68
61
}
69
62
}
70
63
71
- public int getKind () {
72
- return WALLPAPER ;
73
- }
74
64
75
65
public int getDisplayWidth () {
76
66
return size .x ;
77
67
// return metrics.widthPixels;
78
68
}
79
69
70
+
80
71
public int getDisplayHeight () {
81
72
return size .y ;
82
73
// return metrics.heightPixels;
83
74
}
84
75
76
+
85
77
public float getDisplayDensity () {
86
78
return metrics .density ;
87
79
}
88
80
81
+
82
+ public int getKind () {
83
+ return WALLPAPER ;
84
+ }
85
+
86
+
87
+ public PApplet createSketch () {
88
+ return new PApplet ();
89
+ }
90
+
91
+
89
92
public void setSketch (PApplet sketch ) {
90
93
engine .sketch = sketch ;
91
94
}
92
95
96
+
93
97
public PApplet getSketch () {
94
98
return engine .sketch ;
95
99
}
96
100
97
- public PApplet createSketch () {
98
- return new PApplet ();
101
+
102
+ public Engine getEngine () {
103
+ return engine ;
99
104
}
100
105
101
- public void requestDraw () {
102
106
107
+ public void requestDraw () {
103
108
}
104
109
110
+
105
111
public boolean canDraw () {
106
112
return true ;
107
113
}
108
114
115
+
109
116
public void dispose () {
110
117
}
111
118
112
- public void requestPermissions () {
113
119
120
+ public void requestPermissions () {
114
121
}
115
122
123
+
116
124
public void onPermissionsGranted () {
117
125
if (engine != null ) engine .onPermissionsGranted ();
118
126
}
119
127
128
+
120
129
@ Override
121
130
public Engine onCreateEngine () {
122
- engine = new PEngine ();
131
+ engine = new WallpaperEngine ();
123
132
return engine ;
124
133
}
125
134
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 {
127
144
protected PApplet sketch ;
128
145
146
+
129
147
@ Override
130
148
public void onCreate (SurfaceHolder surfaceHolder ) {
131
149
super .onCreate (surfaceHolder );
@@ -137,11 +155,20 @@ public void onCreate(SurfaceHolder surfaceHolder) {
137
155
setTouchEventsEnabled (true );
138
156
}
139
157
158
+
159
+ private void onPermissionsGranted () {
160
+ if (sketch != null ) {
161
+ sketch .onPermissionsGranted ();
162
+ }
163
+ }
164
+
165
+
140
166
@ Override
141
167
public void onSurfaceCreated (SurfaceHolder surfaceHolder ) {
142
168
super .onSurfaceCreated (surfaceHolder );
143
169
}
144
170
171
+
145
172
@ Override
146
173
public void onSurfaceChanged (final SurfaceHolder holder , final int format ,
147
174
final int width , final int height ) {
@@ -151,6 +178,7 @@ public void onSurfaceChanged(final SurfaceHolder holder, final int format,
151
178
super .onSurfaceChanged (holder , format , width , height );
152
179
}
153
180
181
+
154
182
@ Override
155
183
public void onVisibilityChanged (boolean visible ) {
156
184
if (sketch != null ) {
@@ -163,6 +191,7 @@ public void onVisibilityChanged(boolean visible) {
163
191
super .onVisibilityChanged (visible );
164
192
}
165
193
194
+
166
195
/*
167
196
* Store the position of the touch event so we can use it for drawing
168
197
* later
@@ -175,6 +204,7 @@ public void onTouchEvent(MotionEvent event) {
175
204
}
176
205
}
177
206
207
+
178
208
@ Override
179
209
public void onOffsetsChanged (float xOffset , float yOffset ,
180
210
float xOffsetStep , float yOffsetStep ,
@@ -190,6 +220,7 @@ public void onOffsetsChanged(float xOffset, float yOffset,
190
220
}
191
221
}
192
222
223
+
193
224
@ Override
194
225
public void onSurfaceDestroyed (SurfaceHolder holder ) {
195
226
// This is called immediately before a surface is being destroyed.
@@ -200,18 +231,13 @@ public void onSurfaceDestroyed(SurfaceHolder holder) {
200
231
super .onSurfaceDestroyed (holder );
201
232
}
202
233
234
+
203
235
@ Override
204
236
public void onDestroy () {
205
237
super .onDestroy ();
206
238
if (sketch != null ) {
207
239
sketch .onDestroy ();
208
240
}
209
241
}
210
-
211
- public void onPermissionsGranted () {
212
- if (sketch != null ) {
213
- sketch .onPermissionsGranted ();
214
- }
215
- }
216
242
}
217
243
}
0 commit comments