diff --git a/mobile/res/values/strings.xml b/mobile/res/values/strings.xml index 5eb600b9..4a1500a7 100644 --- a/mobile/res/values/strings.xml +++ b/mobile/res/values/strings.xml @@ -130,6 +130,9 @@ Note: If just want to use the desktop mode use the \'force desktop mode\' setting Configure IITCm menu Toggle visibility of IITCm menu entries + Manage plugin updates + Force plugin update + Update all enabled user plugins System Bar diff --git a/mobile/res/xml/preferences.xml b/mobile/res/xml/preferences.xml index dbea9820..9758ec01 100644 --- a/mobile/res/xml/preferences.xml +++ b/mobile/res/xml/preferences.xml @@ -92,6 +92,14 @@ android:key="pref_disable_splash" android:title="@string/pref_disable_splash"/> + + + diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java b/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java index 64ade391..de7cae41 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_FileManager.java @@ -40,6 +40,8 @@ import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; public class IITC_FileManager { private static final WebResourceResponse EMPTY = @@ -50,6 +52,8 @@ public class IITC_FileManager { + "(document.body || document.head || document.documentElement).appendChild(script);"; public static final String DOMAIN = ".iitcm.localhost"; + // update interval is 2 days by default + public static long updateInterval = 1000*60*60*24*2; /** * copies the contents of a stream into another stream and (optionally) closes the output stream afterwards @@ -200,7 +204,6 @@ public class IITC_FileManager { final InputStream data = prepareUserScript(stream); - new UpdateScript(mActivity).execute(uri.getPath()); return new WebResourceResponse("application/x-javascript", "UTF-8", data); } @@ -315,6 +318,31 @@ public class IITC_FileManager { } } + public void updatePlugins(boolean force) { + // check last script update + final long lastUpdated = mPrefs.getLong("pref_last_plugin_update", 0); + final long now = System.currentTimeMillis(); + + // return if no update wanted + if (!force && (now - lastUpdated < updateInterval)) return; + // get the plugin preferences + final TreeMap all_prefs = new TreeMap(mPrefs.getAll()); + + // iterate through all plugins + for (final Map.Entry entry : all_prefs.entrySet()) { + final String plugin = entry.getKey(); + if (plugin.endsWith(".user.js") && entry.getValue().toString().equals("true")) { + if (plugin.startsWith(PLUGINS_PATH)) { + new UpdateScript(mActivity).execute(plugin); + } + } + } + mPrefs + .edit() + .putLong("pref_last_plugin_update", now) + .commit(); + } + private class FileRequest extends WebResourceResponse implements ResponseHandler, Runnable { private Intent mData; private final String mFunctionName; diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_ForceUpdatePreference.java b/mobile/src/com/cradle/iitc_mobile/IITC_ForceUpdatePreference.java new file mode 100644 index 00000000..1a6c4fbe --- /dev/null +++ b/mobile/src/com/cradle/iitc_mobile/IITC_ForceUpdatePreference.java @@ -0,0 +1,54 @@ +package com.cradle.iitc_mobile; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.SharedPreferences; +import android.preference.Preference; +import android.preference.PreferenceManager; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; + +import com.cradle.iitc_mobile.IITC_FileManager; +import com.cradle.iitc_mobile.IITC_Mobile; + +/** + * The OptionDialogPreference will display a dialog, and will persist the + * true when pressing the positive button and false + * otherwise. It will persist to the android:key specified in xml-preference. + */ +public class IITC_ForceUpdatePreference extends Preference { + + public IITC_ForceUpdatePreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + protected void onClick() { + super.onClick(); + new AlertDialog.Builder(getContext()) + .setTitle(R.string.pref_force_plugin_update) + .setMessage(R.string.pref_force_plugin_update_sum) + .setCancelable(true) + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + PreferenceManager.getDefaultSharedPreferences(getContext()) + .edit() + .putLong("pref_last_plugin_update", 0) + .commit(); + dialog.cancel(); + } + }) + .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }) + .create() + .show(); + } +} \ No newline at end of file diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index 1ee36f8b..853995d3 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -191,6 +191,10 @@ public class IITC_Mobile extends Activity return; } else if (key.equals("pref_fake_user_agent")) { mIitcWebView.setUserAgent(); + } else if (key.equals("pref_last_plugin_update")) { + Long forceUpdate = sharedPreferences.getLong("pref_last_plugin_update", 0); + if (forceUpdate == 0) mFileManager.updatePlugins(true); + return; } else if (key.equals("pref_press_twice_to_exit") || key.equals("pref_share_selected_tab") || key.equals("pref_messages") @@ -704,6 +708,7 @@ public class IITC_Mobile extends Activity mNavigationHelper.onLoadingStateChanged(); invalidateOptionsMenu(); updateViews(); + if (!isLoading) mFileManager.updatePlugins(false); if (mSearchTerm != null && !isLoading) { new Handler().postDelayed(new Runnable() { diff --git a/mobile/src/com/cradle/iitc_mobile/async/UpdateScript.java b/mobile/src/com/cradle/iitc_mobile/async/UpdateScript.java index d684a489..aca03e42 100644 --- a/mobile/src/com/cradle/iitc_mobile/async/UpdateScript.java +++ b/mobile/src/com/cradle/iitc_mobile/async/UpdateScript.java @@ -8,6 +8,7 @@ import android.os.AsyncTask; import android.preference.PreferenceManager; import com.cradle.iitc_mobile.IITC_FileManager; +import com.cradle.iitc_mobile.IITC_Mobile; import com.cradle.iitc_mobile.Log; import java.io.File; @@ -20,8 +21,6 @@ import java.net.URL; public class UpdateScript extends AsyncTask { private final Activity mActivity; - // update interval is 2 days - private final long updateInterval = 1000*60*60*24*2; private String mFilePath; private String mScript; @@ -33,18 +32,6 @@ public class UpdateScript extends AsyncTask { protected Boolean doInBackground(final String... urls) { try { mFilePath = urls[0]; - // check last script update - final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity); - final long lastUpdated = prefs.getLong(mFilePath + "-update", 0); - final long now = System.currentTimeMillis(); - - // return if no update wanted - if (now - lastUpdated < updateInterval) return false; - prefs - .edit() - .putLong(mFilePath + "-update", now) - .commit(); - // get local script meta information mScript = IITC_FileManager.readStream(new FileInputStream(new File(mFilePath))); final String updateURL = IITC_FileManager.getScriptInfo(mScript).get("updateURL"); @@ -77,12 +64,19 @@ public class UpdateScript extends AsyncTask { .setTitle("Plugin updated") .setMessage(name) .setCancelable(true) - .setNeutralButton("OK", new DialogInterface.OnClickListener() { + .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) + .setNegativeButton("Reload", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + ((IITC_Mobile) mActivity).reloadIITC(); + } + }) .create() .show(); }