Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
terminate the thread using callOnDetachFromWindow and finalize method present in GLSurafceView
- Loading branch information
1 parent
c53f25c
commit 11f0d4a
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
shadows/framework/src/main/java/org/robolectric/shadows/ShadowGLSurfaceView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |