* improved pluginPreferenceActivity (yes, I tested it in tablet mode)

* made IITC_FileManager.readStream(...) static and always use it when reading src files
This commit is contained in:
Philipp Schaefer 2014-01-22 10:45:20 +01:00
parent a161c4e181
commit d8a09bbae2
4 changed files with 98 additions and 99 deletions

View File

@ -71,10 +71,28 @@ public class IITC_FileManager {
} }
} }
public static String readStream(final InputStream stream) {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final byte[] buffer = new byte[4096];
try {
while (true) {
final int read = stream.read(buffer);
if (read == -1)
break;
os.write(buffer, 0, read);
}
} catch (final IOException e) {
Log.w(e);
return "";
}
return os.toString();
}
public static HashMap<String, String> getScriptInfo(final String js) { public static HashMap<String, String> getScriptInfo(final String js) {
final HashMap<String, String> map = new HashMap<String, String>(); final HashMap<String, String> map = new HashMap<String, String>();
String header = ""; String header = "";
if (js != null) { if (js != null && js.contains("==UserScript==") && js.contains("==/UserScript==")) {
header = js.substring(js.indexOf("==UserScript=="), header = js.substring(js.indexOf("==UserScript=="),
js.indexOf("==/UserScript==")); js.indexOf("==/UserScript=="));
} }
@ -215,24 +233,6 @@ public class IITC_FileManager {
return new ByteArrayInputStream((gmInfo + content).getBytes()); return new ByteArrayInputStream((gmInfo + content).getBytes());
} }
private String readStream(final InputStream stream) {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final byte[] buffer = new byte[4096];
try {
while (true) {
final int read = stream.read(buffer);
if (read == -1)
break;
os.write(buffer, 0, read);
}
} catch (final IOException e) {
Log.w(e);
return "";
}
return os.toString();
}
public String getFileRequestPrefix() { public String getFileRequestPrefix() {
return "//file-request" + DOMAIN + "/"; return "//file-request" + DOMAIN + "/";
} }

View File

@ -17,21 +17,26 @@ import android.widget.TextView;
import com.cradle.iitc_mobile.fragments.PluginsFragment; import com.cradle.iitc_mobile.fragments.PluginsFragment;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap; import java.util.TreeMap;
public class IITC_PluginPreferenceActivity extends PreferenceActivity { public class IITC_PluginPreferenceActivity extends PreferenceActivity {
private List<Header> mHeaders; private List<Header> mHeaders;
// we use a tree map to have a map with alphabetical order // we use a tree map to have a map with alphabetical order
private static TreeMap<String, ArrayList<IITC_PluginPreference>> sPlugins = null; // don't initialize the asset plugin map, because it tells us if the settings are started the first time
public static final String USER_PLUGIN = "00000"; // and we have to parse plugins to build the preference screen
private static TreeMap<String, ArrayList<IITC_PluginPreference>> sAssetPlugins = null;
// user plugins can be initialized.
private static final TreeMap<String, ArrayList<IITC_PluginPreference>> sUserPlugins =
new TreeMap<String, ArrayList<IITC_PluginPreference>>();
private static int mDeletedPlugins = 0; private static int mDeletedPlugins = 0;
@Override @Override
@ -54,9 +59,9 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
mHeaders = target; mHeaders = target;
// since the plugins container is static, // since the plugins container is static,
// it is enough to parse the plugin only on first start. // it is enough to parse the plugin only on first start.
if (sPlugins == null) { if (sAssetPlugins == null) {
Log.d("opened plugin prefs the first time since app start -> parse plugins"); Log.d("opened plugin prefs the first time since app start -> parse plugins");
sPlugins = new TreeMap<String, ArrayList<IITC_PluginPreference>>(); sAssetPlugins = new TreeMap<String, ArrayList<IITC_PluginPreference>>();
setUpPluginPreferenceScreen(); setUpPluginPreferenceScreen();
} else { } else {
checkForNewPlugins(); checkForNewPlugins();
@ -78,7 +83,6 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
@Override @Override
protected void onResume() { protected void onResume() {
// Call super : // Call super :
super.onResume(); super.onResume();
@ -116,8 +120,9 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
} }
// called by Plugins Fragment // called by Plugins Fragment
public static ArrayList<IITC_PluginPreference> getPluginPreference(String key) { public static ArrayList<IITC_PluginPreference> getPluginPreference(String key, boolean userPlugin) {
return sPlugins.get(key); if (userPlugin) return sUserPlugins.get(key);
else return sAssetPlugins.get(key);
} }
private String[] getAssetPlugins() { private String[] getAssetPlugins() {
@ -147,62 +152,53 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
} }
void checkForNewPlugins() { void checkForNewPlugins() {
File[] user = getUserPlugins(); File[] userPlugins = getUserPlugins();
String[] official = getAssetPlugins(); String[] officialPlugins = getAssetPlugins();
int numPlugins = 0; int numPlugins = 0;
for (Map.Entry<String, ArrayList<IITC_PluginPreference>> entry : sPlugins.entrySet()) { for (Map.Entry<String, ArrayList<IITC_PluginPreference>> entry : sUserPlugins.entrySet()) {
numPlugins += entry.getValue().size(); numPlugins += entry.getValue().size();
} }
if ((user.length + official.length) != (numPlugins + mDeletedPlugins)) { for (Map.Entry<String, ArrayList<IITC_PluginPreference>> entry : sAssetPlugins.entrySet()) {
numPlugins += entry.getValue().size();
}
if ((userPlugins.length + officialPlugins.length) != (numPlugins + mDeletedPlugins)) {
Log.d("new or less plugins found since last start, rebuild preferences"); Log.d("new or less plugins found since last start, rebuild preferences");
sPlugins.clear(); sAssetPlugins.clear();
setUpPluginPreferenceScreen(); setUpPluginPreferenceScreen();
} }
} }
void setUpPluginPreferenceScreen() { void setUpPluginPreferenceScreen() {
// get all plugins from asset manager // get all plugins from asset manager
String[] assets = getAssetPlugins(); String[] assets = getAssetPlugins();
for (String asset : assets) { for (String asset : assets) {
// find user plugin name for user readable entries // find user plugin name for user readable entries
Scanner s = null;
String src = "";
try { try {
s = new Scanner(getAssets().open("plugins/" + asset)).useDelimiter("\\A"); InputStream is = getAssets().open("plugins/" + asset);
addPluginPreference(IITC_FileManager.readStream(is), asset, false);
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("iitcm", asset + " not found");
} catch (IOException e) { } catch (IOException e) {
Log.w(e); e.printStackTrace();
Log.e("iitcm", "couldn't read plugin " + asset);
} }
if (s != null) {
src = s.hasNext() ? s.next() : "";
}
// now we have all stuff together and can build the pref screen
addPluginPreference(src, asset, false);
} }
// load user plugins from <storage-path>/IITC_Mobile/plugins/ // load user plugins from <storage-path>/IITC_Mobile/plugins/
File[] files = getUserPlugins(); File[] files = getUserPlugins();
for (File file : files) { for (File file : files) {
Scanner s = null;
String src = "";
try { try {
s = new Scanner(file).useDelimiter("\\A"); InputStream is = new FileInputStream(file);
addPluginPreference(IITC_FileManager.readStream(is), file.toString(), true);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Log.w(e); e.printStackTrace();
Log.d("failed to parse file " + file); Log.e("iitcm", "couldn't read plugin " + file.toString());
} }
if (s != null) {
src = s.hasNext() ? s.next() : "";
}
// now we have all stuff together and can build the pref screen
addPluginPreference(src, file.toString(), true);
} }
} }
void addPluginPreference(String src, String plugin_key, boolean user) { void addPluginPreference(String src, String plugin_key, boolean userPlugin) {
// parse plugin name, description and category // parse plugin name, description and category
// we need default versions here otherwise iitcm may crash // we need default versions here otherwise iitcm may crash
HashMap<String, String> info = IITC_FileManager.getScriptInfo(src); HashMap<String, String> info = IITC_FileManager.getScriptInfo(src);
@ -214,11 +210,6 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
plugin_name = plugin_name.replace("IITC Plugin: ", ""); plugin_name = plugin_name.replace("IITC Plugin: ", "");
plugin_name = plugin_name.replace("IITC plugin: ", ""); plugin_name = plugin_name.replace("IITC plugin: ", "");
// add [User] tag to user plugins
if (user) {
plugin_cat = USER_PLUGIN + plugin_cat;
}
// do not add deleted or stock map plugins // do not add deleted or stock map plugins
if (plugin_cat.equals("Deleted") || plugin_cat.equals("Stock")) { if (plugin_cat.equals("Deleted") || plugin_cat.equals("Stock")) {
mDeletedPlugins++; mDeletedPlugins++;
@ -227,9 +218,16 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
// now we have all stuff together and can build the preference // now we have all stuff together and can build the preference
// first check if we need a new category // first check if we need a new category
if (!sPlugins.containsKey(plugin_cat)) { if (userPlugin) {
sPlugins.put(plugin_cat, new ArrayList<IITC_PluginPreference>()); if (!sUserPlugins.containsKey(plugin_cat)) {
Log.d("create " + plugin_cat + " and add " + plugin_name); sUserPlugins.put(plugin_cat, new ArrayList<IITC_PluginPreference>());
Log.d("create " + plugin_cat + " and add " + plugin_name);
}
} else {
if (!sAssetPlugins.containsKey(plugin_cat)) {
sAssetPlugins.put(plugin_cat, new ArrayList<IITC_PluginPreference>());
Log.d("create " + plugin_cat + " and add " + plugin_name);
}
} }
// now build a new checkable preference for the plugin // now build a new checkable preference for the plugin
@ -239,39 +237,38 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
plugin_pref.setSummary(plugin_desc); plugin_pref.setSummary(plugin_desc);
plugin_pref.setDefaultValue(false); plugin_pref.setDefaultValue(false);
plugin_pref.setPersistent(true); plugin_pref.setPersistent(true);
ArrayList<IITC_PluginPreference> list = sPlugins.get(plugin_cat); ArrayList<IITC_PluginPreference> list = (userPlugin) ? sUserPlugins.get(plugin_cat) : sAssetPlugins.get(plugin_cat);
list.add(plugin_pref); list.add(plugin_pref);
} }
void addHeaders() { void addHeaders() {
boolean first_user = true; if (sUserPlugins.size() > 0) {
boolean first_official = true; Header category = new Header();
// every fragment handles 1 plugin category category.title = "User Plugins";
// push the category to the fragment and add the header to the list mHeaders.add(category);
for (Map.Entry<String, ArrayList<IITC_PluginPreference>> entry : sPlugins.entrySet()) { for(Map.Entry<String, ArrayList<IITC_PluginPreference>> entry : sUserPlugins.entrySet()) {
Bundle bundle = new Bundle(); addHeader(entry.getKey(), true);
String plugin_cat = entry.getKey();
bundle.putString("category", plugin_cat);
if (plugin_cat.startsWith(USER_PLUGIN)) {
if (first_user) {
Header category = new Header();
category.title = "User Plugins";
first_user = false;
mHeaders.add(category);
}
plugin_cat = plugin_cat.replace(USER_PLUGIN, "");
} else if (first_official) {
Header category = new Header();
category.title = "Official Plugins";
first_official = false;
mHeaders.add(category);
} }
Header newHeader = new Header();
newHeader.title = plugin_cat;
newHeader.fragmentArguments = bundle;
newHeader.fragment = "com.cradle.iitc_mobile.fragments.PluginsFragment";
mHeaders.add(newHeader);
} }
if (sAssetPlugins.size() > 0) {
Header category = new Header();
category.title = "Official Plugins";
mHeaders.add(category);
for(Map.Entry<String, ArrayList<IITC_PluginPreference>> entry : sAssetPlugins.entrySet()) {
addHeader(entry.getKey(), false);
}
}
}
private void addHeader(String title, boolean userPlugin) {
Bundle bundle = new Bundle();
bundle.putString("category", title);
bundle.putBoolean("userPlugin", userPlugin);
Header newHeader = new Header();
newHeader.title = title;
newHeader.fragmentArguments = bundle;
newHeader.fragment = "com.cradle.iitc_mobile.fragments.PluginsFragment";
mHeaders.add(newHeader);
} }
/* /*

View File

@ -2,11 +2,12 @@ package com.cradle.iitc_mobile.async;
import android.os.AsyncTask; import android.os.AsyncTask;
import com.cradle.iitc_mobile.Log; import com.cradle.iitc_mobile.IITC_FileManager;
import java.io.IOException; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL; import java.net.URL;
import java.util.Scanner;
/* /*
* this class parses the content of a web page. * this class parses the content of a web page.
@ -20,10 +21,10 @@ public class UrlContentToString extends AsyncTask<URL, Integer, String> {
String js = ""; String js = "";
URL url = urls[0]; URL url = urls[0];
try { try {
js = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A") FileInputStream is = new FileInputStream(new File(url.getPath()));
.next(); js = IITC_FileManager.readStream(is);
} catch (IOException e) { } catch (FileNotFoundException e) {
Log.w(e); e.printStackTrace();
} }
return js; return js;
} }

View File

@ -25,8 +25,9 @@ public class PluginsFragment extends PreferenceFragment {
if (getArguments() != null) { if (getArguments() != null) {
// get plugins category for this fragments and plugins list // get plugins category for this fragments and plugins list
String category = getArguments().getString("category"); String category = getArguments().getString("category");
boolean userPlugin = getArguments().getBoolean("userPlugin");
ArrayList<IITC_PluginPreference> prefs = ArrayList<IITC_PluginPreference> prefs =
IITC_PluginPreferenceActivity.getPluginPreference(category); IITC_PluginPreferenceActivity.getPluginPreference(category, userPlugin);
// add plugin checkbox preferences // add plugin checkbox preferences
for (IITC_PluginPreference pref : prefs) { for (IITC_PluginPreference pref : prefs) {