diff --git a/build.py b/build.py index 42b1e4bf..05017116 100755 --- a/build.py +++ b/build.py @@ -156,6 +156,14 @@ for fn in glob.glob("plugins/*.user.js"): metafn = fn.replace('.user.js', '.meta.js') saveScriptAndMeta(script, os.path.join(outDir,fn), os.path.join(outDir,metafn)) +def copytree(src, dst, symlinks=False, ignore=None): + for item in os.listdir(src): + s = os.path.join(src, item) + d = os.path.join(dst, item) + if os.path.isdir(s): + shutil.copytree(s, d, symlinks, ignore) + else: + shutil.copy2(s, d) # if we're building mobile too if buildMobile: @@ -169,7 +177,12 @@ if buildMobile: pass shutil.copy(os.path.join(outDir,"total-conversion-build.user.js"), "mobile/assets/iitc.js") - # TODO? also copy plugins - once the mobile app supports plugins, that is + # also copy plugins + try: + os.makedirs("mobile/assets/plugins") + except: + pass + copytree(os.path.join(outDir,"plugins"), "mobile/assets/plugins") # now launch 'ant' to build the mobile project diff --git a/mobile/AndroidManifest.xml b/mobile/AndroidManifest.xml index d032ca5d..844d7462 100644 --- a/mobile/AndroidManifest.xml +++ b/mobile/AndroidManifest.xml @@ -1,8 +1,8 @@ + android:versionCode="11" + android:versionName="0.3" > UI + Plugins + Available plugins Force desktop mode Nice for tablets, looks awful on smartphones Developer options diff --git a/mobile/res/xml/preferences.xml b/mobile/res/xml/preferences.xml index fd50c037..2d72b3e1 100644 --- a/mobile/res/xml/preferences.xml +++ b/mobile/res/xml/preferences.xml @@ -17,6 +17,10 @@ android:title="@string/pref_force_desktop" android:summary="@string/pref_force_desktop_sum" android:defaultValue="false" /> + asset_list = new ArrayList(Arrays.asList(asset_array)); + ArrayList asset_values = new ArrayList(); + + for (int i = 0; i < asset_list.size();) { + try { + if (asset_list.get(i).endsWith("user.js")) { + asset_values.add(am.open("plugins/" + asset_list.get(i)).toString()); + i++; + } + else { + asset_list.remove(i); + asset_values.add(am.open("plugins/" + asset_list.get(i)).toString()); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + 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. getFragmentManager().beginTransaction() diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_SettingsFragment.java b/mobile/src/com/cradle/iitc_mobile/IITC_SettingsFragment.java index 34e3c0c0..702f59db 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_SettingsFragment.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_SettingsFragment.java @@ -6,6 +6,7 @@ import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; 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; @@ -21,7 +22,12 @@ public class IITC_SettingsFragment extends PreferenceFragment { iitc_version = getArguments().getString("iitc_version"); addPreferencesFromResource(R.xml.preferences); - + + //plugins + MultiSelectListPreference pref_plugins = (MultiSelectListPreference) findPreference("pref_plugins"); + pref_plugins.setEntries(getArguments().getStringArray("ASSETS")); + pref_plugins.setEntryValues(getArguments().getStringArray("ASSETS")); + // set build version ListPreference pref_build_version = (ListPreference) findPreference("pref_build_version"); PackageManager pm = getActivity().getPackageManager(); diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java b/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java index 74e75c1b..15873a28 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java @@ -3,6 +3,7 @@ package com.cradle.iitc_mobile; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.res.AssetManager; import android.net.Uri; import android.net.http.SslError; import android.preference.PreferenceManager; @@ -17,6 +18,7 @@ import java.io.InputStream; import java.io.IOException; import java.net.URL; import java.util.Scanner; +import java.util.Set; public class IITC_WebViewClient extends WebViewClient { private static final ByteArrayInputStream style = new ByteArrayInputStream( @@ -91,6 +93,39 @@ public class IITC_WebViewClient extends WebViewClient { handler.proceed() ; }; + // plugins should be loaded after the main script is injected + @Override + public void onPageFinished(WebView view, String url) { + super.onPageFinished(view, url); + + // get the plugin preferences + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); + Set plugin_list = sharedPref.getStringSet("pref_plugins", null); + + // iterate through all enabled plugins and load them + if (plugin_list != null) { + AssetManager am = context.getAssets(); + String[] plugin_array = plugin_list.toArray(new String[0]); + + for(int i = 0; i < plugin_list.size(); i++) { + if (plugin_array[i].endsWith("user.js")); + { + Log.d("iitcm", "adding plugin " + plugin_array[i]); + Scanner s = null; + String src = ""; + try { + s = new Scanner(am.open("plugins/" + plugin_array[i])).useDelimiter("\\A"); + } catch (IOException e2) { + // TODO Auto-generated catch block + e2.printStackTrace(); + } + if (s != null) src = s.hasNext() ? s.next() : ""; + view.loadUrl("javascript:" + src); + } + } + } + } + // Check every external resource if it’s okay to load it and maybe replace it // with our own content. This is used to block loading Niantic resources // which aren’t required and to inject IITC early into the site.