added preferences for user plugin updates

This commit is contained in:
Philipp Schaefer 2014-05-16 14:14:09 +02:00
parent 69d6dde544
commit 4bec474b21
6 changed files with 108 additions and 16 deletions

View File

@ -130,6 +130,9 @@
Note: If just want to use the desktop mode use the \'force desktop mode\' setting</string>
<string name="pref_android_menu">Configure IITCm menu</string>
<string name="pref_android_menu_sum">Toggle visibility of IITCm menu entries</string>
<string name="pref_update_plugins_cat">Manage plugin updates</string>
<string name="pref_force_plugin_update">Force plugin update</string>
<string name="pref_force_plugin_update_sum">Update all enabled user plugins</string>
<string-array name="pref_hide_fullscreen">
<item>System Bar</item>

View File

@ -92,6 +92,14 @@
android:key="pref_disable_splash"
android:title="@string/pref_disable_splash"/>
</PreferenceCategory>
<PreferenceCategory
android:key="pref_update_plugins"
android:title="@string/pref_update_plugins_cat">
<com.cradle.iitc_mobile.IITC_ForceUpdatePreference
android:key="pref_force_plugin_update"
android:title="@string/pref_force_plugin_update"
android:summary="@string/pref_force_plugin_update_sum"/>
</PreferenceCategory>
<PreferenceCategory
android:key="pref_advanced_tweaks_cat"
android:title="@string/pref_tweaks_cat">

View File

@ -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<String, ?> all_prefs = new TreeMap<String, Object>(mPrefs.getAll());
// iterate through all plugins
for (final Map.Entry<String, ?> 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;

View File

@ -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
* <code>true</code> when pressing the positive button and <code>false</code>
* 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();
}
}

View File

@ -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() {

View File

@ -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<String, Void, Boolean> {
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<String, Void, Boolean> {
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<String, Void, Boolean> {
.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();
}