added plugin categories (see #358)

This commit is contained in:
Philipp Schaefer
2013-06-08 18:50:26 +02:00
parent ffbf82a44e
commit 51be5bee04
43 changed files with 184 additions and 113 deletions

2
mobile/.idea/misc.xml generated
View File

@ -3,7 +3,7 @@
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="Android 4.2.2 Platform" project-jdk-type="Android SDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="Android 4.2.2" project-jdk-type="Android SDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cradle.iitc_mobile"
android:versionCode="26"
android:versionName="0.4.5">
android:versionCode="27"
android:versionName="0.4.6">
<uses-sdk
android:minSdkVersion="14"

View File

@ -32,7 +32,7 @@
<string name="pref_ui_cat">UI</string>
<string name="pref_misc_cat">Misc</string>
<string name="pref_plugins">Plugins</string>
<string name="pref_plugins">IITC Plugins</string>
<string name="pref_plugins_title">Available plugins</string>
<string name="pref_user_loc">Display user location</string>
<string name="pref_user_loc_sum">Show users position on map</string>

View File

@ -42,10 +42,9 @@
android:title="@string/pref_select_iitc"
android:summary="Load IITC main script from url or use local script. Currently used source: "
android:defaultValue="local"/>
<MultiSelectListPreference
android:key="pref_plugins"
android:title="@string/pref_plugins"
android:dialogTitle="@string/pref_plugins_title"/>
<PreferenceScreen
android:key="pref_plugins"
android:title="@string/pref_plugins" />
<CheckBoxPreference
android:key="pref_force_https"
android:title="@string/pref_force_https"

View File

@ -1,16 +1,7 @@
package com.cradle.iitc_mobile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MenuItem;
public class IITC_Settings extends Activity {
@ -21,70 +12,12 @@ public class IITC_Settings extends Activity {
IITC_SettingsFragment settings = new IITC_SettingsFragment();
AssetManager am = this.getAssets();
String[] asset_array = null;
try {
asset_array = am.list("plugins");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// set action bar title
this.getActionBar().setTitle("IITC Mobile Settings");
this.getActionBar().setHomeButtonEnabled(true);
ArrayList<String> asset_list = new ArrayList<String>();
ArrayList<String> asset_values = new ArrayList<String>();
for (int i = 0; i < asset_array.length; i++) {
// find user plugin name for user readable entries
Scanner s = null;
String src = "";
try {
s = new Scanner(am.open("plugins/" + asset_array[i]))
.useDelimiter("\\A");
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if (s != null)
src = s.hasNext() ? s.next() : "";
String plugin_name = getPluginName(src);
asset_list.add(plugin_name);
// real value
asset_values.add(asset_array[i]);
}
// load additional plugins from <storage-path>/IITC_Mobile/plugins/
String iitc_path = Environment.getExternalStorageDirectory().getPath()
+ "/IITC_Mobile/";
File directory = new File(iitc_path + "plugins/");
File[] files = directory.listFiles();
if (files != null) {
Scanner s = null;
String src = "";
for (int i = 0; i < files.length; ++i) {
try {
s = new Scanner(files[i]).useDelimiter("\\A");
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d("iitcm", "failed to parse file " + files[i]);
}
if (s != null)
src = s.hasNext() ? s.next() : "";
String plugin_name = getPluginName(src);
asset_list.add("[User] " + plugin_name);
// real value
asset_values.add(files[i].toString());
}
}
getActionBar().setTitle("IITC Mobile Settings");
getActionBar().setHomeButtonEnabled(true);
// iitc version
Bundle bundle = getIntent().getExtras();
bundle.putStringArray("ASSETS",
(String[]) asset_list.toArray(new String[0]));
bundle.putStringArray("ASSETS_VAL",
(String[]) asset_values.toArray(new String[0]));
settings.setArguments(bundle);
// Display the fragment as the main content.
@ -92,30 +25,12 @@ public class IITC_Settings extends Activity {
.replace(android.R.id.content, settings).commit();
}
// parse header for @name of plugin
public String getPluginName(String src) {
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";
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];
}
return plugin_name;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// exit settings when home button (iitc icon) is pressed
case android.R.id.home :
this.finish();
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);

View File

@ -3,13 +3,22 @@ package com.cradle.iitc_mobile;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.MultiSelectListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class IITC_SettingsFragment extends PreferenceFragment {
@ -24,10 +33,7 @@ public class IITC_SettingsFragment extends PreferenceFragment {
addPreferencesFromResource(R.xml.preferences);
// plugins
MultiSelectListPreference pref_plugins = (MultiSelectListPreference) findPreference("pref_plugins");
pref_plugins.setEntries(getArguments().getStringArray("ASSETS"));
pref_plugins
.setEntryValues(getArguments().getStringArray("ASSETS_VAL"));
setUpPluginPreferenceScreen();
// set build version
ListPreference pref_build_version = (ListPreference) findPreference("pref_build_version");
@ -64,4 +70,117 @@ public class IITC_SettingsFragment extends PreferenceFragment {
+ pref_iitc_source.getText();
pref_iitc_source.setSummary(pref_iitc_source_sum);
}
void setUpPluginPreferenceScreen() {
PreferenceScreen root = (PreferenceScreen) findPreference("pref_plugins");
// alphabetical order
root.setOrderingAsAdded(false);
root.setPersistent(true);
// get all plugins from asset manager
AssetManager am = this.getActivity().getAssets();
String[] asset_array = null;
try {
asset_array = am.list("plugins");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < asset_array.length; i++) {
// find user plugin name for user readable entries
Scanner s = null;
String src = "";
try {
s = new Scanner(am.open("plugins/" + asset_array[i]))
.useDelimiter("\\A");
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if (s != null)
src = s.hasNext() ? s.next() : "";
// now we have all stuff together and can build the pref screen
addPluginPreference(root, src, asset_array[i], false);
}
// load additional plugins from <storage-path>/IITC_Mobile/plugins/
String iitc_path = Environment.getExternalStorageDirectory().getPath()
+ "/IITC_Mobile/";
File directory = new File(iitc_path + "plugins/");
File[] files = directory.listFiles();
if (files != null) {
Scanner s = null;
String src = "";
for (int i = 0; i < files.length; ++i) {
try {
s = new Scanner(files[i]).useDelimiter("\\A");
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d("iitcm", "failed to parse file " + files[i]);
}
if (s != null)
src = s.hasNext() ? s.next() : "";
// now we have all stuff together and can build the pref screen
addPluginPreference(root, src, files[i].toString(), true);
}
}
}
void addPluginPreference(PreferenceScreen root, String src, String plugin_key, boolean additional) {
// 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];
}
// remove IITC plugin prefix from plugin_name
plugin_name = plugin_name.replace("IITC Plugin: ", "");
plugin_name = plugin_name.replace("IITC plugin: ", "");
// add [User] tag to additional plugins
if (additional)
plugin_cat = "[User] " + plugin_cat;
// now we have all stuff together and can build the pref screen
PreferenceScreen pref_screen;
if (root.findPreference(plugin_cat) == null) {
Log.d("iitcm", "create " + plugin_cat + " and add " + plugin_name);
pref_screen = getPreferenceManager().createPreferenceScreen(root.getContext());
pref_screen.setTitle(plugin_cat);
pref_screen.setKey(plugin_cat);
// alphabetical order
pref_screen.setOrderingAsAdded(false);
pref_screen.setPersistent(true);
root.addPreference(pref_screen);
} else {
Log.d("iitcm", "add " + plugin_name + " to " + plugin_cat);
pref_screen = (PreferenceScreen) findPreference(plugin_cat);
}
// now build a new checkable preference for the plugin
CheckBoxPreference plugin_pref = new CheckBoxPreference(pref_screen.getContext());
plugin_pref.setKey(plugin_key);
plugin_pref.setTitle(plugin_name);
plugin_pref.setSummary(plugin_desc);
plugin_pref.setDefaultValue(false);
plugin_pref.setPersistent(true);
pref_screen.addPreference(plugin_pref);
}
}

View File

@ -21,6 +21,7 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
@ -152,23 +153,24 @@ public class IITC_WebViewClient extends WebViewClient {
Set<String> plugin_list = sharedPref.getStringSet("pref_plugins", null);
boolean dev_enabled = sharedPref.getBoolean("pref_dev_checkbox", false);
// iterate through all enabled plugins and load them
if (plugin_list != null) {
String[] plugin_array = plugin_list.toArray(new String[0]);
Map<String, ?> all_prefs = sharedPref.getAll();
for (int i = 0; i < plugin_list.size(); i++) {
// iterate through all plugins
for(Map.Entry<String, ?> entry : all_prefs.entrySet()){
String plugin = entry.getKey();
if (plugin.endsWith("user.js") && entry.getValue().toString() == "true") {
// load default iitc plugins
if (!plugin_array[i].startsWith(iitc_path)) {
Log.d("iitcm", "adding plugin " + plugin_array[i]);
if (!plugin.startsWith(iitc_path)) {
Log.d("iitcm", "adding plugin " + plugin);
if (dev_enabled)
js += this.removePluginWrapper(iitc_path + "dev/plugins/"
+ plugin_array[i], false);
+ plugin, false);
else
js += this.removePluginWrapper("plugins/" + plugin_array[i], true);
// load additional iitc plugins
js += this.removePluginWrapper("plugins/" + plugin, true);
// load additional iitc plugins
} else {
Log.d("iitcm", "adding additional plugin " + plugin_array[i]);
js += this.removePluginWrapper(plugin_array[i], false);
Log.d("iitcm", "adding additional plugin " + plugin);
js += this.removePluginWrapper(plugin, false);
}
}
}