From 03166de709cb507b71b1abe07f0715fed53a4b4a Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Tue, 25 Oct 2016 17:37:22 +0200 Subject: [PATCH] Moved WebView related method calls to UiThread --- .../activity/MainActivity.java | 15 +++++++-- .../fragment/BrowserFragment.java | 33 ++++++++++++++++--- .../fragment/DiasporaStreamFragment.java | 21 +++++++----- 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java index 4b3c36f6..feeb24d2 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java @@ -440,11 +440,22 @@ public class MainActivity extends ThemedActivity } else if (ACTION_CHANGE_ACCOUNT.equals(action)) { AppLog.v(this, "Reset pod data and show PodSelectionFragment"); appSettings.setPod(null); - app.resetPodData(((DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG)).getWebView()); + runOnUiThread(new Runnable() { + @Override + public void run() { + app.resetPodData(((DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG)).getWebView()); + } + }); showFragment(getFragment(PodSelectionFragment.TAG)); } else if (ACTION_CLEAR_CACHE.equals(action)) { AppLog.v(this, "Clear WebView cache"); - ((DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG)).getWebView().clearCache(true); + runOnUiThread(new Runnable() { + @Override + public void run() { + ((DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG)).getWebView().clearCache(true); + } + }); + } else if (Intent.ACTION_SEND.equals(action) && type != null) { switch (type) { case "text/plain": diff --git a/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java b/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java index 38cabc79..7905bc6b 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java @@ -95,7 +95,13 @@ public class BrowserFragment extends ThemedFragment { if (this.webView == null) { this.webView = (ContextMenuWebView) view.findViewById(R.id.webView); - this.applyWebViewSettings(); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + BrowserFragment.this.applyWebViewSettings(); + } + }); + ProxyHandler.getInstance().addWebView(webView); } @@ -249,16 +255,27 @@ public class BrowserFragment extends ThemedFragment { public boolean onBackPressed() { if (webView.canGoBack()) { - webView.goBack(); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + webView.goBack(); + } + }); return true; } return false; } - public void loadUrl(String url) { + public void loadUrl(final String url) { if (getWebView() != null) { AppLog.v(this, "loadUrl(): load " + url); - getWebView().loadUrlNew(url); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + getWebView().loadUrlNew(url); + } + }); + } else { AppLog.v(this, "loadUrl(): WebView null: Set pending url to " + url); pendingUrl = url; @@ -276,7 +293,13 @@ public class BrowserFragment extends ThemedFragment { public void reloadUrl() { AppLog.v(this, "reloadUrl()"); if (getWebView() != null) { - getWebView().reload(); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + getWebView().reload(); + } + }); + } } diff --git a/app/src/main/java/com/github/dfa/diaspora_android/fragment/DiasporaStreamFragment.java b/app/src/main/java/com/github/dfa/diaspora_android/fragment/DiasporaStreamFragment.java index 7d289d70..67523858 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/fragment/DiasporaStreamFragment.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/fragment/DiasporaStreamFragment.java @@ -75,15 +75,20 @@ public class DiasporaStreamFragment extends BrowserFragment { public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); this.urls = new DiasporaUrlHelper(appSettings); - webView.setWebChromeClient(new DiasporaStreamWebChromeClient(webView, progressBar, fileUploadCallback, sharedTextCallback)); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + webView.setWebChromeClient(new DiasporaStreamWebChromeClient(webView, progressBar, fileUploadCallback, sharedTextCallback)); + webView.getSettings().setJavaScriptEnabled(true); + webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidBridge"); + if (((MainActivity) getActivity()).getTextToBeShared() != null) { + loadUrl(urls.getNewPostUrl()); + } else if (webView.getUrl() == null) { + loadUrl(urls.getStreamUrl()); + } + } + }); - webView.getSettings().setJavaScriptEnabled(true); - webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidBridge"); - if (((MainActivity) getActivity()).getTextToBeShared() != null) { - loadUrl(urls.getNewPostUrl()); - } else if (webView.getUrl() == null) { - loadUrl(urls.getStreamUrl()); - } } public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {