diff --git a/app/src/googlevr/java/org/mozilla/vrbrowser/PlatformActivity.java b/app/src/googlevr/java/org/mozilla/vrbrowser/PlatformActivity.java index 3a6662a68..e104902bf 100644 --- a/app/src/googlevr/java/org/mozilla/vrbrowser/PlatformActivity.java +++ b/app/src/googlevr/java/org/mozilla/vrbrowser/PlatformActivity.java @@ -17,6 +17,8 @@ import com.google.vr.ndk.base.AndroidCompat; import com.google.vr.ndk.base.GvrLayout; +import java.util.ArrayList; + import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; @@ -30,6 +32,8 @@ public static boolean filterPermission(final String aPermission) { private GvrLayout mLayout; private GLSurfaceView mView; private static final String FLAT_ACTIVITY_CLASSNAME = "org.mozilla.vrbrowser.BrowserActivity"; + private ArrayList mPendingEvents; + private boolean mSurfaceCreated = false; private final Runnable activityDestroyedRunnable = new Runnable() { @Override @@ -63,6 +67,7 @@ protected void onCreate(Bundle savedInstanceState) { Log.e(LOGTAG, "PlatformActivity onCreate"); super.onCreate(savedInstanceState); + mPendingEvents = new ArrayList<>(); AndroidCompat.setVrModeEnabled(this, true); // Keep the screen on @@ -85,6 +90,8 @@ protected void onCreate(Bundle savedInstanceState) { public void onSurfaceCreated(GL10 gl, EGLConfig config) { Log.e(LOGTAG, "In onSurfaceCreated"); activityCreated(getAssets(), mLayout.getGvrApi().getNativeGvrContext()); + mSurfaceCreated = true; + notifyPendingEvents(); } @Override @@ -122,7 +129,7 @@ public void run() { protected void onPause() { Log.e(LOGTAG, "PlatformActivity onPause"); synchronized (activityPausedRunnable) { - mView.queueEvent(activityPausedRunnable); + queueRunnable(activityPausedRunnable); try { activityPausedRunnable.wait(); } catch(InterruptedException e) { @@ -140,7 +147,7 @@ protected void onResume() { super.onResume(); mLayout.onResume(); mView.onResume(); - mView.queueEvent(activityResumedRunnable); + queueRunnable(activityResumedRunnable); setImmersiveSticky(); } @@ -149,7 +156,7 @@ protected void onDestroy() { Log.e(LOGTAG, "PlatformActivity onDestroy"); super.onDestroy(); synchronized (activityDestroyedRunnable) { - mView.queueEvent(activityDestroyedRunnable); + queueRunnable(activityDestroyedRunnable); try { activityDestroyedRunnable.wait(); } catch(InterruptedException e) { @@ -170,8 +177,27 @@ void setImmersiveSticky() { | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); } + void queueRunnable(Runnable aRunnable) { - mView.queueEvent(aRunnable); + if (mSurfaceCreated) { + mView.queueEvent(aRunnable); + } else { + synchronized (mPendingEvents) { + mPendingEvents.add(aRunnable); + } + if (mSurfaceCreated) { + notifyPendingEvents(); + } + } + } + + private void notifyPendingEvents() { + synchronized (mPendingEvents) { + for (Runnable runnable: mPendingEvents) { + mView.queueEvent(runnable); + } + mPendingEvents.clear(); + } } private native void activityCreated(Object aAssetManager, final long aContext); diff --git a/app/src/main/cpp/BrowserWorld.cpp b/app/src/main/cpp/BrowserWorld.cpp index 41e9d5598..ad454a13e 100644 --- a/app/src/main/cpp/BrowserWorld.cpp +++ b/app/src/main/cpp/BrowserWorld.cpp @@ -712,7 +712,7 @@ BrowserWorld::AddWidget(int32_t aHandle, const WidgetPlacementPtr& aPlacement) { const float worldHeight = worldWidth / aspect; WidgetPtr widget; - if (aPlacement->cylinder && m.cylinderDensity > 0 && m.device) { + if (aPlacement->cylinder && m.cylinderDensity > 0) { VRLayerCylinderPtr layer = m.device->CreateLayerCylinder(textureWidth, textureHeight, VRLayerQuad::SurfaceType::AndroidSurface); CylinderPtr cylinder = Cylinder::Create(m.create, layer); widget = Widget::Create(m.context, aHandle, textureWidth, textureHeight, worldWidth, worldHeight, cylinder); @@ -720,7 +720,7 @@ BrowserWorld::AddWidget(int32_t aHandle, const WidgetPlacementPtr& aPlacement) { if (!widget) { VRLayerQuadPtr layer; - if (aPlacement->layer && m.device) { + if (aPlacement->layer) { layer = m.device->CreateLayerQuad(textureWidth, textureHeight, VRLayerQuad::SurfaceType::AndroidSurface); } diff --git a/app/src/noapi/java/org/mozilla/vrbrowser/PlatformActivity.java b/app/src/noapi/java/org/mozilla/vrbrowser/PlatformActivity.java index 5004e6313..7b7f31ab2 100644 --- a/app/src/noapi/java/org/mozilla/vrbrowser/PlatformActivity.java +++ b/app/src/noapi/java/org/mozilla/vrbrowser/PlatformActivity.java @@ -14,6 +14,8 @@ import android.view.View; import android.widget.ImageButton; +import java.util.ArrayList; + import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; @@ -26,6 +28,8 @@ public static boolean filterPermission(final String aPermission) { } private GLSurfaceView mView; + private ArrayList mPendingEvents; + private boolean mSurfaceCreated = false; private final Runnable activityDestroyedRunnable = new Runnable() { @Override @@ -60,6 +64,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.noapi_layout); + mPendingEvents = new ArrayList<>(); mView = findViewById(R.id.gl_view); mView.setEGLContextClientVersion(3); mView.setEGLConfigChooser(8, 8, 8, 0, 16, 0); @@ -71,6 +76,8 @@ protected void onCreate(Bundle savedInstanceState) { public void onSurfaceCreated(GL10 gl, EGLConfig config) { Log.e(LOGTAG, "In onSurfaceCreated"); activityCreated(getAssets()); + mSurfaceCreated = true; + notifyPendingEvents(); } @Override @@ -112,12 +119,7 @@ public boolean onTouchEvent(MotionEvent aEvent) { final float xx = aEvent.getX(0); final float yy = aEvent.getY(0); - mView.queueEvent(new Runnable() { - @Override - public void run() { - touchEvent(isDown, xx, yy); - } - }); + queueRunnable(() -> touchEvent(isDown, xx, yy)); return true; } @@ -136,12 +138,7 @@ public boolean onGenericMotionEvent(MotionEvent aEvent) { final float xx = aEvent.getX(0); final float yy = aEvent.getY(0); - mView.queueEvent(new Runnable() { - @Override - public void run() { - touchEvent(false, xx, yy); - } - }); + queueRunnable(() -> touchEvent(false, xx, yy)); return true; } @@ -149,7 +146,7 @@ public void run() { protected void onPause() { Log.e(LOGTAG, "PlatformActivity onPause"); synchronized (activityPausedRunnable) { - mView.queueEvent(activityPausedRunnable); + queueRunnable(activityPausedRunnable); try { activityPausedRunnable.wait(); } catch(InterruptedException e) { @@ -165,7 +162,7 @@ protected void onResume() { Log.e(LOGTAG, "PlatformActivity onResume"); super.onResume(); mView.onResume(); - mView.queueEvent(activityResumedRunnable); + queueRunnable(activityResumedRunnable); // setImmersiveSticky(); } @@ -174,7 +171,7 @@ protected void onDestroy() { Log.e(LOGTAG, "PlatformActivity onDestroy"); super.onDestroy(); synchronized (activityDestroyedRunnable) { - mView.queueEvent(activityDestroyedRunnable); + queueRunnable(activityDestroyedRunnable); try { activityDestroyedRunnable.wait(); } catch(InterruptedException e) { @@ -196,7 +193,25 @@ protected void onDestroy() { // } void queueRunnable(Runnable aRunnable) { - mView.queueEvent(aRunnable); + if (mSurfaceCreated) { + mView.queueEvent(aRunnable); + } else { + synchronized (mPendingEvents) { + mPendingEvents.add(aRunnable); + } + if (mSurfaceCreated) { + notifyPendingEvents(); + } + } + } + + private void notifyPendingEvents() { + synchronized (mPendingEvents) { + for (Runnable runnable: mPendingEvents) { + mView.queueEvent(runnable); + } + mPendingEvents.clear(); + } } private void setupUI() { @@ -261,21 +276,11 @@ public void onClick(View view) { } private void dispatchMoveAxis(final float aX, final float aY, final float aZ) { - mView.queueEvent(new Runnable() { - @Override - public void run() { - moveAxis(aX, aY, aZ); - } - }); + queueRunnable(() -> moveAxis(aX, aY, aZ)); } private void dispatchRotateHeading(final float aHeading) { - mView.queueEvent(new Runnable() { - @Override - public void run() { - rotateHeading(aHeading); - } - }); + queueRunnable(() -> rotateHeading(aHeading)); } private native void activityCreated(Object aAssetManager);