From 11f0d4aa8bcfecd987882ea9eff1aec3cd42fa54 Mon Sep 17 00:00:00 2001 From: Devashishbasu Date: Mon, 26 Feb 2024 00:02:53 +0530 Subject: [PATCH] Added code to stop gl thread terminate the thread using callOnDetachFromWindow and finalize method present in GLSurafceView --- .../shadows/ShadowGLSurfaceView.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowGLSurfaceView.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowGLSurfaceView.java index 157896e6441..30462c2ec8b 100644 --- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowGLSurfaceView.java +++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowGLSurfaceView.java @@ -1,15 +1,50 @@ package org.robolectric.shadows; import android.opengl.GLSurfaceView; +import android.util.Log; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import org.robolectric.annotation.Implementation; import org.robolectric.annotation.Implements; /** Fake implementation of GLSurfaceView */ @Implements(GLSurfaceView.class) public class ShadowGLSurfaceView extends ShadowSurfaceView { + + private static final String TAG = "ShadowGLSurfaceView"; + @Implementation protected void onPause() {} @Implementation protected void onResume() {} + + @Implementation + protected void onDetachedFromWindow() { + stopGLThread(); + } + + @Implementation + protected void finalize() throws Throwable { + try { + stopGLThread(); + } finally { + super.finalize(); + } + } + + private void stopGLThread() { + try { + Field glThreadField = GLSurfaceView.class.getDeclaredField("mGLThread"); + glThreadField.setAccessible(true); + Thread glThread = (Thread) glThreadField.get(realView); + if (glThread != null) { + Method requestExitAndWait = glThread.getClass().getMethod("requestExitAndWait"); + requestExitAndWait.invoke(glThread); + } + } catch (Exception e) { + Log.e(TAG, "Exception occurred while stopping GLThread: " + e.getMessage()); + e.printStackTrace(); + } + } }