Skip to content

Commit

Permalink
Merge pull request #14 from sp0x/webviewFix
Browse files Browse the repository at this point in the history
Webview fix
  • Loading branch information
sarooghi authored Jul 20, 2020
2 parents b2befc8 + 629a368 commit 1b9c5f2
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 206 deletions.
1 change: 1 addition & 0 deletions demoapp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:testOnly="false"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
11 changes: 6 additions & 5 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apply plugin: 'com.android.library'

def org = "queueit"
android {
compileSdkVersion 23
buildToolsVersion "28.0.3"
Expand All @@ -8,7 +8,7 @@ android {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "2.0.20"
versionName "2.0.22"
}
buildTypes {
release {
Expand All @@ -18,6 +18,7 @@ android {
}
}


dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
Expand All @@ -35,8 +36,8 @@ ext {

libraryDescription = 'Android SDK to integrate with Queue-it'

siteUrl = 'https://github.com/queueit/android-webui-sdk'
gitUrl = 'https://github.com/queueit/android-webui-sdk.git'
siteUrl = "https://github.com/${org}/android-webui-sdk"
gitUrl = "https://github.com/${org}/android-webui-sdk.git"

libraryVersion = android.defaultConfig.versionName

Expand Down Expand Up @@ -113,7 +114,7 @@ bintray {
pkg {
repo = bintrayRepo
name = bintrayName
userOrg = 'queueit'
userOrg = "${org}"
desc = libraryDescription
websiteUrl = siteUrl
vcsUrl = gitUrl
Expand Down
224 changes: 122 additions & 102 deletions library/src/main/java/com/queue_it/androidsdk/QueueActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.CookieSyncManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import java.net.MalformedURLException;
Expand All @@ -26,6 +28,125 @@ public class QueueActivity extends AppCompatActivity {
private String queueUrl;
private String targetUrl;
private String userId;
private WebView webview;
private URL target;
private URL queue;
private static WebView previousWebView;

WebViewClient webviewClient = new WebViewClient() {

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
CookieSyncManager.getInstance().sync();
}

@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
String errorMessage;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
errorMessage = String.format("%s %s: %s %s", request.getMethod(), request.getUrl(), errorResponse.getStatusCode(), errorResponse.getReasonPhrase());
} else {
errorMessage = errorResponse.toString();
}
Log.v("QueueActivity", String.format("%s: %s", "onReceivedHttpError", errorMessage));
super.onReceivedHttpError(view, request, errorResponse);
}

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
String errorMessage;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
errorMessage = String.format("%s %s: %s %s", request.getMethod(), request.getUrl(), error.getErrorCode(), error.getDescription());
} else {
errorMessage = error.toString();
}
Log.v("QueueActivity", String.format("%s: %s", "onReceivedError", errorMessage));
super.onReceivedError(view, request, error);
}

@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
handler.cancel();
broadcastQueueError("SslError, code: " + error.getPrimaryError());
disposeWebview(webview);
}

public boolean shouldOverrideUrlLoading(WebView view, String urlString) {
Log.v("QueueITEngine", "URL loading: " + urlString);
URL url;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}

boolean isQueueItUrl = queue.getHost().contains(url.getHost());
if (isQueueItUrl) {
boolean needsRewrite = QueueUrlHelper.urlUpdateNeeded(urlString, userId);
if (needsRewrite) {
urlString = QueueUrlHelper.updateUrl(urlString, userId);
Log.v("QueueITEngine", "URL intercepting: " + urlString);
}
broadcastChangedQueueUrl(urlString);
if(needsRewrite){
webview.loadUrl(urlString);
return true;
}
}
boolean isTarget = target.getHost().contains(url.getHost());
if (isTarget) {
Uri uri = Uri.parse(urlString);
String queueItToken = uri.getQueryParameter("queueittoken");
broadcastQueuePassed(queueItToken);
disposeWebview(webview);
return true;
}
if (!isQueueItUrl) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlString));
startActivity(browserIntent);
return true;
}
return false;
}
};

private static void cleanupWebView(){
if(previousWebView==null) return;
previousWebView.destroy();
previousWebView = null;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_queue);
readActivityUrls(savedInstanceState);
cleanupWebView();
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);

FrameLayout layout = (FrameLayout) findViewById(R.id.relativeLayout);
webview = new WebView(this);
layout.addView(webview);
previousWebView = webview;
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
Log.v("Progress", Integer.toString(newProgress));
if (newProgress < 100) {
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.GONE);
}
progressBar.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});
webview.setWebViewClient(webviewClient);
Log.v("QueueITEngine", "Loading initial URL: " + queueUrl);
webview.loadUrl(queueUrl);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
Expand All @@ -40,19 +161,10 @@ protected void onDestroy() {
if (isFinishing()) {
broadcastQueueActivityClosed();
}

super.onDestroy();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_queue);

final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);

final WebView webView = (WebView) findViewById(R.id.webView);

private void readActivityUrls(Bundle savedInstanceState) {
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
Expand All @@ -69,104 +181,12 @@ protected void onCreate(Bundle savedInstanceState) {
userId = (String) savedInstanceState.getSerializable("userId");
}

final URL target;
final URL queue;
try {
target = new URL(targetUrl);
queue = new URL(queueUrl);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}

Log.v("QueueITEngine", "Loading initial URL: " + queueUrl);

webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
Log.v("Progress", Integer.toString(newProgress));
if (newProgress < 100) {
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.GONE);
}
progressBar.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});

webView.setWebViewClient(new WebViewClient() {

@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
String errorMessage;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
errorMessage = String.format("%s %s: %s %s", request.getMethod(), request.getUrl(), errorResponse.getStatusCode(), errorResponse.getReasonPhrase());
} else {
errorMessage = errorResponse.toString();
}
Log.v("QueueActivity", String.format("%s: %s", "onReceivedHttpError", errorMessage));
super.onReceivedHttpError(view, request, errorResponse);
}

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
String errorMessage;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
errorMessage = String.format("%s %s: %s %s", request.getMethod(), request.getUrl(), error.getErrorCode(), error.getDescription());
} else {
errorMessage = error.toString();
}
Log.v("QueueActivity", String.format("%s: %s", "onReceivedError", errorMessage));
super.onReceivedError(view, request, error);
}

@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
handler.cancel();
broadcastQueueError("SslError, code: " + error.getPrimaryError());
disposeWebview(webView);
}

public boolean shouldOverrideUrlLoading(WebView view, String urlString) {
Log.v("QueueITEngine", "URL loading: " + urlString);

URL url;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}

boolean isQueueUrl = queue.getHost().contains(url.getHost());
if (isQueueUrl) {
if (QueueUrlHelper.urlUpdateNeeded(urlString, userId)) {
urlString = QueueUrlHelper.updateUrl(urlString, userId);
Log.v("QueueITEngine", "URL intercepting: " + urlString);
webView.loadUrl(urlString);
return true;
}
broadcastChangedQueueUrl(urlString);
}
boolean isTarget = target.getHost().contains(url.getHost());
if (isTarget) {
Uri uri = Uri.parse(urlString);
String queueItToken = uri.getQueryParameter("queueittoken");

broadcastQueuePassed(queueItToken);
disposeWebview(webView);
return true;
}
if (!isQueueUrl) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlString));
startActivity(browserIntent);
return true;
}

return false;
}
});
webView.loadUrl(queueUrl);
}

private void broadcastChangedQueueUrl(String urlString) {
Expand Down
Loading

0 comments on commit 1b9c5f2

Please sign in to comment.