* 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:
@@ -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,9 +218,16 @@ 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>());
|
||||
Log.d("create " + plugin_cat + " and add " + plugin_name);
|
||||
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
|
||||
@@ -239,39 +237,38 @@ 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) {
|
||||
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);
|
||||
if (sUserPlugins.size() > 0) {
|
||||
Header category = new Header();
|
||||
category.title = "User Plugins";
|
||||
mHeaders.add(category);
|
||||
for(Map.Entry<String, ArrayList<IITC_PluginPreference>> entry : sUserPlugins.entrySet()) {
|
||||
addHeader(entry.getKey(), true);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user