* 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:
parent
a161c4e181
commit
d8a09bbae2
@ -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) {
|
||||
final HashMap<String, String> map = new HashMap<String, String>();
|
||||
String header = "";
|
||||
if (js != null) {
|
||||
if (js != null && js.contains("==UserScript==") && js.contains("==/UserScript==")) {
|
||||
header = js.substring(js.indexOf("==UserScript=="),
|
||||
js.indexOf("==/UserScript=="));
|
||||
}
|
||||
@ -215,24 +233,6 @@ public class IITC_FileManager {
|
||||
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() {
|
||||
return "//file-request" + DOMAIN + "/";
|
||||
}
|
||||
|
@ -17,21 +17,26 @@ import android.widget.TextView;
|
||||
import com.cradle.iitc_mobile.fragments.PluginsFragment;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class IITC_PluginPreferenceActivity extends PreferenceActivity {
|
||||
|
||||
private List<Header> mHeaders;
|
||||
// we use a tree map to have a map with alphabetical order
|
||||
private static TreeMap<String, ArrayList<IITC_PluginPreference>> sPlugins = null;
|
||||
public static final String USER_PLUGIN = "00000";
|
||||
// don't initialize the asset plugin map, because it tells us if the settings are started the first time
|
||||
// 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;
|
||||
|
||||
@Override
|
||||
@ -54,9 +59,9 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
|
||||
mHeaders = target;
|
||||
// since the plugins container is static,
|
||||
// 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");
|
||||
sPlugins = new TreeMap<String, ArrayList<IITC_PluginPreference>>();
|
||||
sAssetPlugins = new TreeMap<String, ArrayList<IITC_PluginPreference>>();
|
||||
setUpPluginPreferenceScreen();
|
||||
} else {
|
||||
checkForNewPlugins();
|
||||
@ -78,7 +83,6 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
|
||||
// Call super :
|
||||
super.onResume();
|
||||
|
||||
@ -116,8 +120,9 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
|
||||
}
|
||||
|
||||
// called by Plugins Fragment
|
||||
public static ArrayList<IITC_PluginPreference> getPluginPreference(String key) {
|
||||
return sPlugins.get(key);
|
||||
public static ArrayList<IITC_PluginPreference> getPluginPreference(String key, boolean userPlugin) {
|
||||
if (userPlugin) return sUserPlugins.get(key);
|
||||
else return sAssetPlugins.get(key);
|
||||
}
|
||||
|
||||
private String[] getAssetPlugins() {
|
||||
@ -147,62 +152,53 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
|
||||
}
|
||||
|
||||
void checkForNewPlugins() {
|
||||
File[] user = getUserPlugins();
|
||||
String[] official = getAssetPlugins();
|
||||
File[] userPlugins = getUserPlugins();
|
||||
String[] officialPlugins = getAssetPlugins();
|
||||
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();
|
||||
}
|
||||
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");
|
||||
sPlugins.clear();
|
||||
sAssetPlugins.clear();
|
||||
setUpPluginPreferenceScreen();
|
||||
}
|
||||
}
|
||||
|
||||
void setUpPluginPreferenceScreen() {
|
||||
|
||||
// get all plugins from asset manager
|
||||
String[] assets = getAssetPlugins();
|
||||
|
||||
for (String asset : assets) {
|
||||
// find user plugin name for user readable entries
|
||||
Scanner s = null;
|
||||
String src = "";
|
||||
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) {
|
||||
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/
|
||||
File[] files = getUserPlugins();
|
||||
for (File file : files) {
|
||||
Scanner s = null;
|
||||
String src = "";
|
||||
try {
|
||||
s = new Scanner(file).useDelimiter("\\A");
|
||||
InputStream is = new FileInputStream(file);
|
||||
addPluginPreference(IITC_FileManager.readStream(is), file.toString(), true);
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.w(e);
|
||||
Log.d("failed to parse file " + file);
|
||||
e.printStackTrace();
|
||||
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
|
||||
// we need default versions here otherwise iitcm may crash
|
||||
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: ", "");
|
||||
|
||||
// add [User] tag to user plugins
|
||||
if (user) {
|
||||
plugin_cat = USER_PLUGIN + plugin_cat;
|
||||
}
|
||||
|
||||
// do not add deleted or stock map plugins
|
||||
if (plugin_cat.equals("Deleted") || plugin_cat.equals("Stock")) {
|
||||
mDeletedPlugins++;
|
||||
@ -227,10 +218,17 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
|
||||
|
||||
// now we have all stuff together and can build the preference
|
||||
// first check if we need a new category
|
||||
if (!sPlugins.containsKey(plugin_cat)) {
|
||||
sPlugins.put(plugin_cat, new ArrayList<IITC_PluginPreference>());
|
||||
if (userPlugin) {
|
||||
if (!sUserPlugins.containsKey(plugin_cat)) {
|
||||
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
|
||||
IITC_PluginPreference plugin_pref = new IITC_PluginPreference(this);
|
||||
@ -239,40 +237,39 @@ public class IITC_PluginPreferenceActivity extends PreferenceActivity {
|
||||
plugin_pref.setSummary(plugin_desc);
|
||||
plugin_pref.setDefaultValue(false);
|
||||
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);
|
||||
}
|
||||
|
||||
void addHeaders() {
|
||||
boolean first_user = true;
|
||||
boolean first_official = true;
|
||||
// every fragment handles 1 plugin category
|
||||
// push the category to the fragment and add the header to the list
|
||||
for (Map.Entry<String, ArrayList<IITC_PluginPreference>> entry : sPlugins.entrySet()) {
|
||||
Bundle bundle = new Bundle();
|
||||
String plugin_cat = entry.getKey();
|
||||
bundle.putString("category", plugin_cat);
|
||||
if (plugin_cat.startsWith(USER_PLUGIN)) {
|
||||
if (first_user) {
|
||||
if (sUserPlugins.size() > 0) {
|
||||
Header category = new Header();
|
||||
category.title = "User Plugins";
|
||||
first_user = false;
|
||||
mHeaders.add(category);
|
||||
for(Map.Entry<String, ArrayList<IITC_PluginPreference>> entry : sUserPlugins.entrySet()) {
|
||||
addHeader(entry.getKey(), true);
|
||||
}
|
||||
plugin_cat = plugin_cat.replace(USER_PLUGIN, "");
|
||||
} else if (first_official) {
|
||||
}
|
||||
if (sAssetPlugins.size() > 0) {
|
||||
Header category = new Header();
|
||||
category.title = "Official Plugins";
|
||||
first_official = false;
|
||||
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 = plugin_cat;
|
||||
newHeader.title = title;
|
||||
newHeader.fragmentArguments = bundle;
|
||||
newHeader.fragment = "com.cradle.iitc_mobile.fragments.PluginsFragment";
|
||||
mHeaders.add(newHeader);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This code is only for header categories. Thx to Android that we haven't this by default and
|
||||
|
@ -2,11 +2,12 @@ package com.cradle.iitc_mobile.async;
|
||||
|
||||
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.util.Scanner;
|
||||
|
||||
/*
|
||||
* this class parses the content of a web page.
|
||||
@ -20,10 +21,10 @@ public class UrlContentToString extends AsyncTask<URL, Integer, String> {
|
||||
String js = "";
|
||||
URL url = urls[0];
|
||||
try {
|
||||
js = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A")
|
||||
.next();
|
||||
} catch (IOException e) {
|
||||
Log.w(e);
|
||||
FileInputStream is = new FileInputStream(new File(url.getPath()));
|
||||
js = IITC_FileManager.readStream(is);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return js;
|
||||
}
|
||||
|
@ -25,8 +25,9 @@ public class PluginsFragment extends PreferenceFragment {
|
||||
if (getArguments() != null) {
|
||||
// get plugins category for this fragments and plugins list
|
||||
String category = getArguments().getString("category");
|
||||
boolean userPlugin = getArguments().getBoolean("userPlugin");
|
||||
ArrayList<IITC_PluginPreference> prefs =
|
||||
IITC_PluginPreferenceActivity.getPluginPreference(category);
|
||||
IITC_PluginPreferenceActivity.getPluginPreference(category, userPlugin);
|
||||
|
||||
// add plugin checkbox preferences
|
||||
for (IITC_PluginPreference pref : prefs) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user