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 = private final DialogInterface.OnClickListener onClickListener =
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int index) { public void onClick(DialogInterface dialog, int index) {
if (index >= 0 && index < mAccounts.length) { if (index >= 0 && index < mAccounts.length) {
mAccount = mAccounts[index]; mAccount = mAccounts[index];
startAuthentication(); startAuthentication();
} }
dialog.cancel(); dialog.cancel();
} }
}; };
public IITC_DeviceAccountLogin(IITC_Mobile activity, WebView webView, public IITC_DeviceAccountLogin(IITC_Mobile activity, WebView webView,
WebViewClient webViewClient) { WebViewClient webViewClient) {

View File

@ -66,10 +66,6 @@ public class IITC_Mobile extends Activity {
// enable progress bar above action bar // enable progress bar above action bar
requestWindowFeature(Window.FEATURE_PROGRESS); 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); setContentView(R.layout.activity_main);
iitc_view = (IITC_WebView) findViewById(R.id.iitc_webview); 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.webkit.WebViewClient;
import android.widget.Toast; import android.widget.Toast;
import com.cradle.iitc_mobile.async.UrlContentToString;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -24,6 +26,9 @@ import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.Map; import java.util.Map;
import java.util.Scanner; 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 { public class IITC_WebViewClient extends WebViewClient {
private static final ByteArrayInputStream style = new ByteArrayInputStream( 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 // load iitc script from web or asset folder
if (iitc_source.contains("http")) { if (iitc_source.contains("http")) {
URL url = new URL(iitc_source); URL url = new URL(iitc_source);
js = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A") // if url.openStream() is parsed from a scanner
.next(); // 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 { } else {
js = this.fileToString("total-conversion-build.user.js", true); 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;
}
}