own method for js-file to string...improve code readability

This commit is contained in:
Philipp Schaefer 2013-05-04 11:36:32 +02:00
parent bbeecfaaaf
commit 041c7dd0bd

View File

@ -18,7 +18,6 @@ import android.widget.Toast;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.Scanner; import java.util.Scanner;
@ -69,31 +68,23 @@ public class IITC_WebViewClient extends WebViewClient {
// if developer mode are enabled, load all iitc script from external storage // if developer mode are enabled, load all iitc script from external storage
if (sharedPref.getBoolean("pref_dev_checkbox", true)) { if (sharedPref.getBoolean("pref_dev_checkbox", true)) {
File js_file = new File(iitc_path + "/dev/total-conversion-build.user.js"); js = this.fileToString(iitc_path + "/dev/total-conversion-build.user.js", false);
if (!js_file.exists()) { if (js.equals("false")) {
Toast.makeText(context, "File " + iitc_path + Toast.makeText(context, "File " + iitc_path +
"/dev/total-conversion-build.user.js not found. " + "/dev/total-conversion-build.user.js not found. " +
"Disable developer mode or add iitc files " + "Disable developer mode or add iitc files " +
"to the dev folder.", Toast.LENGTH_LONG).show(); "to the dev folder.", Toast.LENGTH_LONG).show();
return;
} else { } else {
Toast.makeText(context, "Developer mode enabled", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Developer mode enabled", Toast.LENGTH_SHORT).show();
} }
Scanner s = null;
s = new Scanner(js_file).useDelimiter("\\A");
if (s != null) js = s.hasNext() ? s.next() : "";
} else { } else {
// 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").next(); js = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A").next();
} else { } else {
InputStream input; js = this.fileToString("total-conversion-build.user.js", true);
input = c.getAssets().open("total-conversion-build.user.js");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
js = new String(buffer);
} }
} }
@ -131,27 +122,14 @@ public class IITC_WebViewClient extends WebViewClient {
// iterate through all enabled plugins and load them // iterate through all enabled plugins and load them
if (plugin_list != null) { if (plugin_list != null) {
AssetManager am = context.getAssets();
String[] plugin_array = plugin_list.toArray(new String[0]); String[] plugin_array = plugin_list.toArray(new String[0]);
for(int i = 0; i < plugin_list.size(); i++) { for(int i = 0; i < plugin_list.size(); i++) {
Log.d("iitcm", "adding plugin " + plugin_array[i]); Log.d("iitcm", "adding plugin " + plugin_array[i]);
Scanner s = null; if (dev_enabled)
String src = ""; this.loadJS(iitc_path + "/dev/plugins/" + plugin_array[i], false, view);
try {
// load plugins from external storage if dev mode are enabled
if (dev_enabled) {
File js_file = new File(iitc_path + "/dev/plugins/" + plugin_array[i]);
s = new Scanner(js_file).useDelimiter("\\A");
}
else else
// load plugins from asset folder this.loadJS("plugins/" + plugin_array[i], true, view);
s = new Scanner(am.open("plugins/" + plugin_array[i])).useDelimiter("\\A");
} catch (IOException e2) {
e2.printStackTrace();
}
if (s != null) src = s.hasNext() ? s.next() : "";
view.loadUrl("javascript:" + src);
} }
} }
@ -164,41 +142,63 @@ public class IITC_WebViewClient extends WebViewClient {
File[] files = directory.listFiles(); File[] files = directory.listFiles();
if (files != null) { if (files != null) {
for (int i = 0; i < files.length; ++i) { for (int i = 0; i < files.length; ++i) {
try { if(this.loadJS(files[i].toString(), false, view))
// the file should be a user.js-file Log.d("iitcm", "loading additional plugin " + files[i].toString());
if (!files[i].toString().endsWith("user.js"))
continue;
String src = "";
Scanner s = new Scanner(files[i]).useDelimiter("\\A");
if (s != null) src = s.hasNext() ? s.next() : "";
Log.d("iitcm", "Loading additional plugin " + iitc_path + files[i]);
view.loadUrl("javascript:" + src);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} }
} }
} }
public void enableTracking(WebView view, boolean dev_enabled) { public void enableTracking(WebView view, boolean dev_enabled) {
Log.d("iitcm", "enable tracking..."); Log.d("iitcm", "enable tracking...");
AssetManager am = context.getAssets();
Scanner s = null;
String src = "";
try {
// load plugin from external storage if dev mode are enabled // load plugin from external storage if dev mode are enabled
if (dev_enabled) { if (dev_enabled)
File js_file = new File(iitc_path + "/dev/user-location.user.js"); this.loadJS(iitc_path + "/dev/user-location.user.js", false, view);
s = new Scanner(js_file).useDelimiter("\\A");
}
else else
// load plugin from asset folder // load plugin from asset folder
s = new Scanner(am.open("user-location.user.js")).useDelimiter("\\A"); this.loadJS("user-location.user.js", true, view);
} catch (IOException e2) {
e2.printStackTrace();
} }
// read a file into a string
// use the full path for File
// if asset == true use the asset manager to open file
public String fileToString(String file, boolean asset) {
Scanner s = null;
String src= "";
if (!asset) {
File js_file = new File(file);
try {
s = new Scanner(js_file).useDelimiter("\\A");
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d("iitcm", "failed to parse file " + file);
return "false";
}
} else {
// load plugins from asset folder
AssetManager am = context.getAssets();
try {
s = new Scanner(am.open(file)).useDelimiter("\\A");
} catch (IOException e) {
e.printStackTrace();
Log.d("iitcm", "failed to parse file assets/" + file);
return "false";
}
}
if (s != null) src = s.hasNext() ? s.next() : ""; if (s != null) src = s.hasNext() ? s.next() : "";
view.loadUrl("javascript:" + src); return src;
}
// read a file into a string
// load it as javascript
public boolean loadJS(String file, boolean asset, WebView view) {
if (!file.endsWith("user.js"))
return false;
String js = fileToString(file, asset);
if (js.equals("false"))
return false;
else view.loadUrl("javascript:" + js);
return true;
} }
// Check every external resource if its okay to load it and maybe replace it // Check every external resource if its okay to load it and maybe replace it