Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed CWebViewPlugin.java to check whether activity is destroyed just before calling runOnUiThread. #754

Merged
merged 1 commit into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public void call(final String message) {

public void call(final String method, final String message) {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mPlugin.IsInitialized()) {
UnityPlayer.UnitySendMessage(mGameObject, method, message);
Expand Down Expand Up @@ -111,6 +114,19 @@ public class CWebViewPlugin {
private String mBasicAuthUserName;
private String mBasicAuthPassword;

// cf. https://github.com/gree/unity-webview/issues/753
// cf. https://github.com/mixpanel/mixpanel-android/issues/400
// cf. https://github.com/mixpanel/mixpanel-android/commit/98bb530f9263f3bac0737971acc00dfef7ea4c35
public static boolean isDestroyed(final Activity a) {
if (a == null) {
return true;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return a.isDestroyed();
} else {
return false;
}
}

public CWebViewPlugin() {
}

Expand All @@ -130,6 +146,9 @@ public Boolean call() throws Exception {
return isAvailable;
}
});
if (CWebViewPlugin.isDestroyed(a)) {
return false;
}
a.runOnUiThread(t);
try {
return t.get();
Expand All @@ -145,6 +164,9 @@ public boolean IsInitialized() {
public void Init(final String gameObject, final boolean transparent, final boolean zoom, final int androidForceDarkMode, final String ua) {
final CWebViewPlugin self = this;
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView != null) {
return;
Expand Down Expand Up @@ -345,11 +367,14 @@ public WebResourceResponse shouldInterceptRequest(WebView view, final String url
if (setCookieHeaders != null) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT || Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT_WATCH) {
// In addition to getCookie, setCookie cause deadlock on Android 4.4.4 cf. https://issuetracker.google.com/issues/36989494
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
SetCookies(url, setCookieHeaders);
}
});
final Activity a = UnityPlayer.currentActivity;
if (!CWebViewPlugin.isDestroyed(a)) {
a.runOnUiThread(new Runnable() {
public void run() {
SetCookies(url, setCookieHeaders);
}
});
}
} else {
SetCookies(url, setCookieHeaders);
}
Expand Down Expand Up @@ -526,6 +551,9 @@ public void onGlobalLayout() {

public void Destroy() {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand Down Expand Up @@ -554,6 +582,9 @@ public boolean SetURLPattern(final String allowPattern, final String denyPattern
final Pattern deny = (denyPattern == null || denyPattern.length() == 0) ? null : Pattern.compile(denyPattern);
final Pattern hook = (hookPattern == null || hookPattern.length() == 0) ? null : Pattern.compile(hookPattern);
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return false;
}
a.runOnUiThread(new Runnable() {public void run() {
mAllowRegex = allow;
mDenyRegex = deny;
Expand All @@ -567,6 +598,9 @@ public boolean SetURLPattern(final String allowPattern, final String denyPattern

public void LoadURL(final String url) {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand All @@ -582,6 +616,9 @@ public void LoadURL(final String url) {
public void LoadHTML(final String html, final String baseURL)
{
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand All @@ -592,6 +629,9 @@ public void LoadHTML(final String html, final String baseURL)

public void EvaluateJS(final String js) {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand All @@ -606,6 +646,9 @@ public void EvaluateJS(final String js) {

public void GoBack() {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand All @@ -616,6 +659,9 @@ public void GoBack() {

public void GoForward() {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand All @@ -626,6 +672,9 @@ public void GoForward() {

public void Reload() {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand All @@ -642,6 +691,9 @@ public void SetMargins(int left, int top, int right, int bottom) {
Gravity.NO_GRAVITY);
params.setMargins(left, top, right, bottom);
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand All @@ -652,6 +704,9 @@ public void SetMargins(int left, int top, int right, int bottom) {

public void SetVisibility(final boolean visibility) {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand All @@ -668,6 +723,9 @@ public void SetVisibility(final boolean visibility) {

public void SetScrollbarsVisibility(final boolean visibility) {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand All @@ -679,20 +737,29 @@ public void SetScrollbarsVisibility(final boolean visibility) {

public void SetAlertDialogEnabled(final boolean enabled) {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
mAlertDialogEnabled = enabled;
}});
}

public void SetCameraAccess(final boolean allowed) {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
mAllowVideoCapture = allowed;
}});
}

public void SetMicrophoneAccess(final boolean allowed) {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
mAllowAudioCapture = allowed;
}});
Expand All @@ -701,6 +768,9 @@ public void SetMicrophoneAccess(final boolean allowed) {
// cf. https://stackoverflow.com/questions/31788748/webview-youtube-videos-playing-in-background-on-rotation-and-minimise/31789193#31789193
public void OnApplicationPause(final boolean paused) {
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand Down Expand Up @@ -829,6 +899,9 @@ public void SetBasicAuthInfo(final String userName, final String password)
public void ClearCache(final boolean includeDiskFiles)
{
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand All @@ -840,6 +913,9 @@ public void ClearCache(final boolean includeDiskFiles)
public void SetTextZoom(final int textZoom)
{
final Activity a = UnityPlayer.currentActivity;
if (CWebViewPlugin.isDestroyed(a)) {
return;
}
a.runOnUiThread(new Runnable() {public void run() {
if (mWebView == null) {
return;
Expand Down
Loading