Skip to content

Commit f65d814

Browse files
authored
Merge pull request #346 from processing/vrapi
Merge VR API into master
2 parents e5faea6 + 3e70df9 commit f65d814

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+846
-567
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ allprojects {
2020
jcenter()
2121
flatDir dirs: "$rootDir/core/library"
2222
flatDir dirs: "$rootDir/core/build/libs"
23-
flatDir dirs: "$rootDir/libraries/vr/lib"
23+
flatDir dirs: "$rootDir/libraries/vr/library"
2424
flatDir dirs: android_platform
2525
}
2626

core/src/processing/a2d/PGraphicsAndroid2D.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public class PGraphicsAndroid2D extends PGraphics {
104104
Paint tintPaint;
105105

106106

107+
/**
108+
* Marks when changes to the size have occurred, so that the backing bitmap
109+
* can be recreated.
110+
*/
111+
protected boolean sized;
112+
107113
//////////////////////////////////////////////////////////////
108114

109115
// INTERNAL
@@ -135,6 +141,12 @@ public PGraphicsAndroid2D() {
135141
//public void setPath(String path)
136142

137143

144+
@Override
145+
public void setSize(int iwidth, int iheight) {
146+
super.setSize(iwidth, iheight);
147+
sized = true;
148+
}
149+
138150

139151
@Override
140152
public void dispose() {
@@ -165,15 +177,20 @@ public void requestDraw() {
165177

166178
// @Override
167179
// public void requestDraw() {
168-
// parent.handleDraw();
180+
//parent.handleDraw();
169181
// }
170182

171183

172184
protected Canvas checkCanvas() {
173-
if (canvas == null && (useBitmap || !primaryGraphics)) {
174-
if (bitmap != null) bitmap.recycle();
175-
bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
185+
if ((canvas == null || sized) && (useBitmap || !primaryGraphics)) {
186+
if (bitmap == null || bitmap.getWidth() * bitmap.getHeight() < width * height) {
187+
if (bitmap != null) bitmap.recycle();
188+
bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
189+
} else {
190+
bitmap.reconfigure(width, height, bitmap.getConfig());
191+
}
176192
canvas = new Canvas(bitmap);
193+
sized = false;
177194
}
178195
return canvas;
179196
}
@@ -365,6 +382,14 @@ public void vertex(float x, float y) {
365382
}
366383
break;
367384

385+
case LINE_STRIP:
386+
case LINE_LOOP:
387+
if (vertexCount >= 2) {
388+
line(vertices[vertexCount-2][X],
389+
vertices[vertexCount-2][Y], x, y);
390+
}
391+
break;
392+
368393
case TRIANGLES:
369394
if ((vertexCount % 3) == 0) {
370395
triangle(vertices[vertexCount - 3][X],
@@ -490,6 +515,11 @@ public void endShape(int mode) {
490515
}
491516
drawPath();
492517
}
518+
} else if (shape == LINE_LOOP && vertexCount >= 2) {
519+
line(vertices[vertexCount-1][X],
520+
vertices[vertexCount-1][Y],
521+
vertices[0][X],
522+
vertices[0][Y]);
493523
}
494524
shape = 0;
495525
}

core/src/processing/android/PFragment.java

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
import android.support.annotation.IdRes;
2626
import android.support.annotation.LayoutRes;
2727
import android.support.v4.app.Fragment;
28+
import android.support.v4.app.FragmentActivity;
2829
import android.support.v4.app.FragmentManager;
2930
import android.support.v4.app.FragmentTransaction;
3031
import android.util.DisplayMetrics;
31-
32+
import android.content.Intent;
3233
import android.content.pm.ActivityInfo;
3334
import android.content.res.Configuration;
3435
import android.graphics.Point;
@@ -46,9 +47,17 @@ public class PFragment extends Fragment implements AppComponent {
4647
private DisplayMetrics metrics;
4748
private Point size;
4849
private PApplet sketch;
50+
private @LayoutRes int layout = -1;
4951

5052

5153
public PFragment() {
54+
super();
55+
}
56+
57+
58+
public PFragment(PApplet sketch) {
59+
super();
60+
setSketch(sketch);
5261
}
5362

5463

@@ -100,32 +109,37 @@ public int getKind() {
100109

101110
public void setSketch(PApplet sketch) {
102111
this.sketch = sketch;
112+
if (layout != -1) {
113+
sketch.parentLayout = layout;
114+
}
103115
}
104116

105117

106-
public void setSketch(PApplet sketch, @IdRes int id, @LayoutRes int layout,
107-
FragmentManager manager) {
108-
this.sketch = sketch;
109-
sketch.parentLayout = layout;
118+
public PApplet getSketch() {
119+
return sketch;
120+
}
121+
122+
123+
public void setLayout(@LayoutRes int layout, @IdRes int id, FragmentActivity activity) {
124+
this.layout = layout;
125+
if (sketch != null) {
126+
sketch.parentLayout = layout;
127+
}
128+
FragmentManager manager = activity.getSupportFragmentManager();
110129
FragmentTransaction transaction = manager.beginTransaction();
111130
transaction.add(id, this);
112131
transaction.commit();
113132
}
114133

115134

116-
public void setSketch(PApplet sketch, View view, FragmentManager manager) {
117-
this.sketch = sketch;
135+
public void setView(View view, FragmentActivity activity) {
136+
FragmentManager manager = activity.getSupportFragmentManager();
118137
FragmentTransaction transaction = manager.beginTransaction();
119138
transaction.add(view.getId(), this);
120139
transaction.commit();
121140
}
122141

123142

124-
public PApplet getSketch() {
125-
return sketch;
126-
}
127-
128-
129143
public boolean isService() {
130144
return false;
131145
}
@@ -145,45 +159,70 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
145159
Bundle savedInstanceState) {
146160
if (sketch != null) {
147161
sketch.initSurface(inflater, container, savedInstanceState, this, null);
162+
163+
// For compatibility with older sketches that run some hardware initialization
164+
// inside onCreate(), don't call from Fragment.onCreate() because the surface
165+
// will not be yet ready, and so the reference to the activity and other
166+
// system variables will be null. In any case, onCreateView() is called
167+
// immediately after onCreate():
168+
// https://developer.android.com/reference/android/app/Fragment.html#Lifecycle
169+
sketch.onCreate(savedInstanceState);
170+
148171
return sketch.getSurface().getRootView();
149172
} else {
150173
return null;
151174
}
152175
}
153176

154177

178+
@Override
179+
public void onStart() {
180+
super.onStart();
181+
if (sketch != null) {
182+
sketch.onStart();
183+
}
184+
}
185+
186+
155187
@Override
156188
public void onResume() {
157189
super.onResume();
158-
if (sketch != null) sketch.onResume();
190+
if (sketch != null) {
191+
sketch.onResume();
192+
}
159193
}
160194

161195

162196
@Override
163197
public void onPause() {
164198
super.onPause();
165-
if (sketch != null) sketch.onPause();
199+
if (sketch != null) {
200+
sketch.onPause();
201+
}
166202
}
167203

168204

169205
@Override
170-
public void onDestroy() {
171-
super.onDestroy();
172-
if (sketch != null) sketch.onDestroy();
206+
public void onStop() {
207+
super.onStop();
208+
if (sketch != null) {
209+
sketch.onStop();
210+
}
173211
}
174212

175213

176214
@Override
177-
public void onStart() {
178-
super.onStart();
179-
if (sketch != null) sketch.onStart();
215+
public void onDestroy() {
216+
super.onDestroy();
217+
if (sketch != null) {
218+
sketch.onDestroy();
219+
}
180220
}
181221

182222

183223
@Override
184-
public void onStop() {
185-
super.onStop();
186-
if (sketch != null) sketch.onStop();
224+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
225+
if (sketch != null) sketch.onActivityResult(requestCode, resultCode, data);
187226
}
188227

189228

core/src/processing/android/PWallpaper.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ public void onCreate(SurfaceHolder surfaceHolder) {
153153
super.onCreate(surfaceHolder);
154154
sketch = createSketch();
155155
sketch.initSurface(PWallpaper.this, getSurfaceHolder());
156-
sketch.preview = isPreview();
157156
if (isPreview()) requestPermissions();
158157
setTouchEventsEnabled(true);
159158
}
@@ -213,13 +212,6 @@ public void onOffsetsChanged(float xOffset, float yOffset,
213212
this.yOffsetStep = yOffsetStep;
214213
this.xPixelOffset = xPixelOffset;
215214
this.yPixelOffset = yPixelOffset;
216-
217-
sketch.homeScreenOffset = xOffset;
218-
if (0 < xOffsetStep) {
219-
sketch.homeScreenCount = (int)(1 + 1 / xOffsetStep);
220-
} else {
221-
sketch.homeScreenCount = 1;
222-
}
223215
}
224216
}
225217

core/src/processing/android/PWatchFaceCanvas.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ private void invalidateIfNecessary() {
202202
public void onAmbientModeChanged(boolean inAmbientMode) {
203203
super.onAmbientModeChanged(inAmbientMode);
204204
invalidateIfNecessary();
205-
if (sketch != null) sketch.ambientMode = inAmbientMode;
206205
// call new event handlers in sketch (?)
207206
}
208207

@@ -212,10 +211,6 @@ public void onPropertiesChanged(Bundle properties) {
212211
super.onPropertiesChanged(properties);
213212
lowBitAmbient = properties.getBoolean(PROPERTY_LOW_BIT_AMBIENT, false);
214213
burnInProtection = properties.getBoolean(PROPERTY_BURN_IN_PROTECTION, false);
215-
if (sketch != null) {
216-
sketch.lowBitAmbient = properties.getBoolean(PROPERTY_LOW_BIT_AMBIENT, false);
217-
sketch.burnInProtection = properties.getBoolean(PROPERTY_BURN_IN_PROTECTION, false);
218-
}
219214
}
220215

221216

@@ -227,13 +222,6 @@ public void onApplyWindowInsets(WindowInsets insets) {
227222
insets.getSystemWindowInsetTop(),
228223
insets.getSystemWindowInsetRight(),
229224
insets.getSystemWindowInsetBottom());
230-
if (sketch != null) {
231-
sketch.isRound = insets.isRound();
232-
sketch.insetLeft = insets.getSystemWindowInsetLeft();
233-
sketch.insetRight = insets.getSystemWindowInsetRight();
234-
sketch.insetTop = insets.getSystemWindowInsetTop();
235-
sketch.insetBottom = insets.getSystemWindowInsetBottom();
236-
}
237225
}
238226

239227

@@ -364,7 +352,9 @@ public void onComplicationDataUpdate(
364352
@Override
365353
public void onDestroy() {
366354
super.onDestroy();
367-
if (sketch != null) sketch.onDestroy();
355+
if (sketch != null) {
356+
sketch.onDestroy();
357+
}
368358
}
369359

370360

core/src/processing/android/PWatchFaceGLES.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ private void invalidateIfNecessary() {
219219
public void onAmbientModeChanged(boolean inAmbientMode) {
220220
super.onAmbientModeChanged(inAmbientMode);
221221
invalidateIfNecessary();
222-
if (sketch != null) sketch.ambientMode = inAmbientMode;
223222
// call new event handlers in sketch (?)
224223
}
225224

@@ -229,10 +228,6 @@ public void onPropertiesChanged(Bundle properties) {
229228
super.onPropertiesChanged(properties);
230229
lowBitAmbient = properties.getBoolean(PROPERTY_LOW_BIT_AMBIENT, false);
231230
burnInProtection = properties.getBoolean(PROPERTY_BURN_IN_PROTECTION, false);
232-
if (sketch != null) {
233-
sketch.lowBitAmbient = properties.getBoolean(PROPERTY_LOW_BIT_AMBIENT, false);
234-
sketch.burnInProtection = properties.getBoolean(PROPERTY_BURN_IN_PROTECTION, false);
235-
}
236231
}
237232

238233

@@ -243,13 +238,6 @@ public void onApplyWindowInsets(WindowInsets insets) {
243238
insets.getSystemWindowInsetTop(),
244239
insets.getSystemWindowInsetRight(),
245240
insets.getSystemWindowInsetBottom());
246-
if (sketch != null) {
247-
sketch.isRound = insets.isRound();
248-
sketch.insetLeft = insets.getSystemWindowInsetLeft();
249-
sketch.insetRight = insets.getSystemWindowInsetRight();
250-
sketch.insetTop = insets.getSystemWindowInsetTop();
251-
sketch.insetBottom = insets.getSystemWindowInsetBottom();
252-
}
253241
}
254242

255243

@@ -365,7 +353,9 @@ public void onComplicationDataUpdate(
365353
@Override
366354
public void onDestroy() {
367355
super.onDestroy();
368-
if (sketch != null) sketch.onDestroy();
356+
if (sketch != null) {
357+
sketch.onDestroy();
358+
}
369359
}
370360

371361

core/src/processing/android/PermissionRequestor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.support.v4.app.ActivityCompat;
66
import android.support.v4.os.ResultReceiver;
77

8+
// A simple utility activity to request permissions in a service.
89
public class PermissionRequestor extends Activity {
910
public static final String KEY_RESULT_RECEIVER = "resultReceiver";
1011
public static final String KEY_PERMISSIONS = "permissions";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package processing.android;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
import android.annotation.SuppressLint;
5+
import android.os.Build;
6+
import android.view.View;
7+
8+
// An utility class to generate unique View ID's. Needed until minimum API level
9+
// in raised to 17, where we can just use View.generateViewId().
10+
// Largely based on fantouch code at http://stackoverflow.com/a/21000252
11+
public class ViewIdGenerator {
12+
// Start at 15,000,000, taking into account the comment from Singed
13+
// http://stackoverflow.com/a/39307421
14+
private static final AtomicInteger nextId = new AtomicInteger(15000000);
15+
16+
@SuppressLint("NewApi")
17+
public static int getUniqueId() {
18+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
19+
for (;;) {
20+
final int result = nextId.get();
21+
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
22+
int newValue = result + 1;
23+
if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
24+
if (nextId.compareAndSet(result, newValue)) {
25+
return result;
26+
}
27+
}
28+
} else {
29+
return View.generateViewId();
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)