create an async task for online js parsing

This commit is contained in:
Philipp Schaefer 2013-07-09 00:14:12 +02:00
parent c2634ce266
commit 7fcb1e0874
4 changed files with 60 additions and 15 deletions

View File

@ -68,15 +68,15 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle> {
*/
private final DialogInterface.OnClickListener onClickListener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int index) {
if (index >= 0 && index < mAccounts.length) {
mAccount = mAccounts[index];
startAuthentication();
}
dialog.cancel();
}
};
@Override
public void onClick(DialogInterface dialog, int index) {
if (index >= 0 && index < mAccounts.length) {
mAccount = mAccounts[index];
startAuthentication();
}
dialog.cancel();
}
};
public IITC_DeviceAccountLogin(IITC_Mobile activity, WebView webView,
WebViewClient webViewClient) {

View File

@ -66,10 +66,6 @@ public class IITC_Mobile extends Activity {
// enable progress bar above action bar
requestWindowFeature(Window.FEATURE_PROGRESS);
// TODO build an async task for url.openStream() in IITC_WebViewClient
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
iitc_view = (IITC_WebView) findViewById(R.id.iitc_webview);

View File

@ -17,6 +17,8 @@ import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.cradle.iitc_mobile.async.UrlContentToString;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
@ -24,6 +26,9 @@ import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class IITC_WebViewClient extends WebViewClient {
private static final ByteArrayInputStream style = new ByteArrayInputStream(
@ -90,8 +95,21 @@ public class IITC_WebViewClient extends WebViewClient {
// load iitc script from web or asset folder
if (iitc_source.contains("http")) {
URL url = new URL(iitc_source);
js = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A")
.next();
// if url.openStream() is parsed from a scanner
// the NetworkOnMainThread exception is thrown
// use a async task for this
try {
js = new UrlContentToString().execute(url).get(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
js = this.fileToString("total-conversion-build.user.js", true);
} catch (ExecutionException e) {
e.printStackTrace();
js = this.fileToString("total-conversion-build.user.js", true);
} catch (TimeoutException e) {
e.printStackTrace();
js = this.fileToString("total-conversion-build.user.js", true);
}
} else {
js = this.fileToString("total-conversion-build.user.js", true);
}

View File

@ -0,0 +1,31 @@
package com.cradle.iitc_mobile.async;
import android.os.AsyncTask;
import com.cradle.iitc_mobile.IITC_Mobile;
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
/*
* this class parses the content of an web page.
* since network operations shouldn't be done on main UI thread
* we use an async task for this.
*/
public class UrlContentToString extends AsyncTask<URL, Integer, String> {
@Override
protected String doInBackground(URL... urls) {
String js = "";
URL url = urls[0];
try {
js = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A")
.next();
} catch (IOException e) {
e.printStackTrace();
}
return js;
}
}