made getScriptInfo static and use it in pluginpreferenceactivity

This commit is contained in:
Philipp Schaefer 2013-10-30 23:45:59 +01:00
parent bb4d57acad
commit 3f901a588a
2 changed files with 18 additions and 25 deletions

View File

@ -21,6 +21,7 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
@ -193,29 +194,12 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
void addPluginPreference(String src, String plugin_key, boolean user) {
// now parse plugin name, description and category
String header = src.substring(src.indexOf("==UserScript=="),
src.indexOf("==/UserScript=="));
// remove new line comments and replace with space
// this way we get double spaces instead of newline + double slash
header = header.replace("\n//", " ");
// get a list of key-value...split on multiple spaces
String[] attributes = header.split(" +");
String plugin_name = "not found";
String plugin_desc = "not found";
String plugin_cat = "Misc";
for (int j = 0; j < attributes.length; j++) {
// search for name and use the value
if (attributes[j].equals("@name")) {
plugin_name = attributes[j + 1];
}
if (attributes[j].equals("@description")) {
plugin_desc = attributes[j + 1];
}
if (attributes[j].equals("@category")) {
plugin_cat = attributes[j + 1];
}
}
// parse plugin name, description and category
// we need default versions here otherwise iitcm may crash
HashMap<String,String> info = IITC_WebViewClient.getScriptInfo(src);
String plugin_name = info.get("name");
String plugin_cat = info.get("category");
String plugin_desc = info.get("description");
// remove IITC plugin prefix from plugin_name
plugin_name = plugin_name.replace("IITC Plugin: ", "");

View File

@ -56,7 +56,8 @@ public class IITC_WebViewClient extends WebViewClient {
return map.get("version");
}
public HashMap<String, String> getScriptInfo(String js) {
// static method because we use it in IITC_PluginPreferenceActivity too
public static HashMap<String, String> getScriptInfo(String js) {
HashMap<String, String> map = new HashMap<String, String>();
String header = "";
if (js != null) {
@ -67,7 +68,12 @@ public class IITC_WebViewClient extends WebViewClient {
header = header.replace("\n//", " ");
// get a list of key-value
String[] attributes = header.split(" +");
String iitc_version = "not found";
// add default values
map.put("version", "not found");
map.put("name", "unknown");
map.put("description", "");
map.put("category", "Misc");
// add parsed values
for (int i = 0; i < attributes.length; i++) {
// search for attributes and use the value
if (attributes[i].equals("@version")) {
@ -79,6 +85,9 @@ public class IITC_WebViewClient extends WebViewClient {
if (attributes[i].equals("@description")) {
map.put("description", attributes[i + 1]);
}
if (attributes[i].equals("@category")) {
map.put("category", attributes[i + 1]);
}
}
return map;
}