summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Strømme <[email protected]>2015-01-28 12:02:55 +0100
committerChristian Stromme <[email protected]>2015-02-10 11:37:41 +0000
commite59d6a79fd5cd208fece67f79e544807bd2d72f9 (patch)
treece14dd0fa2ab49d6565c22dba92d6db9b309a1f4
parent92ad76a7245900a08d913ada21e65229ca5bf5d0 (diff)
Android: Fix isLoading() and progress tracking.
There are no good ways to know if a WebView is loading or not. onPageStarted() is called for each frame, so it might be called multiple times while onPageFinished() is only called for the main frame, that is, resources and other frames might still be loading. To make it worse load status is handled in a different client then the progress, so to make sure we report progress and finished in the right order we need to sync them up and let the last call be responsible for reporting the loading succeeded status. Change-Id: Ibcf858bfa026c5b280c70b338801be9a1475b376 Reviewed-by: Eskil Abrahamsen Blomfeldt <[email protected]>
-rw-r--r--src/jar/src/org/qtproject/qt5/android/view/QtAndroidWebViewController.java46
-rw-r--r--src/webview/qwebview_android.cpp2
2 files changed, 32 insertions, 16 deletions
diff --git a/src/jar/src/org/qtproject/qt5/android/view/QtAndroidWebViewController.java b/src/jar/src/org/qtproject/qt5/android/view/QtAndroidWebViewController.java
index 404fa3a..dde2fff 100644
--- a/src/jar/src/org/qtproject/qt5/android/view/QtAndroidWebViewController.java
+++ b/src/jar/src/org/qtproject/qt5/android/view/QtAndroidWebViewController.java
@@ -57,6 +57,9 @@ public class QtAndroidWebViewController
private final long m_id;
private WebView m_webView = null;
private static final String TAG = "QtAndroidWebViewController";
+ private volatile boolean m_onPageFinishedCalled = false;
+ private volatile int m_progress = 0;
+ private volatile int m_frameCount = 0;
// API 11 methods
private Method m_webViewOnResume = null;
@@ -74,6 +77,12 @@ public class QtAndroidWebViewController
private native void c_onReceivedTitle(long id, String title);
private native void c_onRunJavaScriptResult(long id, long callbackId, String result);
+ private void resetLoadingState()
+ {
+ m_progress = 0;
+ m_onPageFinishedCalled = false;
+ }
+
private class QtAndroidWebViewClient extends WebViewClient
{
QtAndroidWebViewClient() { super(); }
@@ -88,14 +97,21 @@ public class QtAndroidWebViewController
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
- c_onPageFinished(m_id, url);
+ m_onPageFinishedCalled = true;
+ if (m_progress == 100) { // onProgressChanged() will notify Qt if we didn't finish here.
+ m_frameCount = 0;
+ c_onPageFinished(m_id, url);
+ }
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
- c_onPageStarted(m_id, url, favicon);
+ if (++m_frameCount == 1) { // Only call onPageStarted for the first frame.
+ m_onPageFinishedCalled = false;
+ c_onPageStarted(m_id, url, favicon);
+ }
}
}
@@ -106,7 +122,12 @@ public class QtAndroidWebViewController
public void onProgressChanged(WebView view, int newProgress)
{
super.onProgressChanged(view, newProgress);
+ m_progress = newProgress;
c_onProgressChanged(m_id, newProgress);
+ if (m_onPageFinishedCalled && m_progress == 100) { // Did we finish?
+ m_frameCount = 0;
+ c_onPageFinished(m_id, view.getUrl());
+ }
}
@Override
@@ -173,6 +194,7 @@ public class QtAndroidWebViewController
return;
}
+ resetLoadingState();
m_activity.runOnUiThread(new Runnable() {
@Override
public void run() { m_webView.loadUrl(url); }
@@ -184,6 +206,7 @@ public class QtAndroidWebViewController
if (data == null)
return;
+ resetLoadingState();
m_activity.runOnUiThread(new Runnable() {
@Override
public void run() { m_webView.loadData(data, mimeType, encoding); }
@@ -199,6 +222,7 @@ public class QtAndroidWebViewController
if (data == null)
return;
+ resetLoadingState();
m_activity.runOnUiThread(new Runnable() {
@Override
public void run() { m_webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl); }
@@ -293,20 +317,12 @@ public class QtAndroidWebViewController
public int getProgress()
{
- final int[] progress = {0};
- final Semaphore sem = new Semaphore(0);
- m_activity.runOnUiThread(new Runnable() {
- @Override
- public void run() { progress[0] = m_webView.getProgress(); sem.release(); }
- });
-
- try {
- sem.acquire();
- } catch (Exception e) {
- e.printStackTrace();
- }
+ return m_progress;
+ }
- return progress[0];
+ public boolean isLoading()
+ {
+ return (m_progress != 100 && !m_onPageFinishedCalled);
}
public void runJavaScript(final String script, final long callbackId)
diff --git a/src/webview/qwebview_android.cpp b/src/webview/qwebview_android.cpp
index b7e19b6..22c7f75 100644
--- a/src/webview/qwebview_android.cpp
+++ b/src/webview/qwebview_android.cpp
@@ -203,7 +203,7 @@ int QAndroidWebViewPrivate::loadProgress() const
bool QAndroidWebViewPrivate::isLoading() const
{
- return true;
+ return m_viewController.callMethod<jboolean>("isLoading");
}
void QAndroidWebViewPrivate::setParentView(QObject *view)