added preferences for user plugin updates
This commit is contained in:
parent
69d6dde544
commit
4bec474b21
@ -130,6 +130,9 @@
|
|||||||
Note: If just want to use the desktop mode use the \'force desktop mode\' setting</string>
|
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">Configure IITCm menu</string>
|
||||||
<string name="pref_android_menu_sum">Toggle visibility of IITCm menu entries</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">
|
<string-array name="pref_hide_fullscreen">
|
||||||
<item>System Bar</item>
|
<item>System Bar</item>
|
||||||
|
@ -92,6 +92,14 @@
|
|||||||
android:key="pref_disable_splash"
|
android:key="pref_disable_splash"
|
||||||
android:title="@string/pref_disable_splash"/>
|
android:title="@string/pref_disable_splash"/>
|
||||||
</PreferenceCategory>
|
</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
|
<PreferenceCategory
|
||||||
android:key="pref_advanced_tweaks_cat"
|
android:key="pref_advanced_tweaks_cat"
|
||||||
android:title="@string/pref_tweaks_cat">
|
android:title="@string/pref_tweaks_cat">
|
||||||
|
@ -40,6 +40,8 @@ import java.net.URL;
|
|||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
public class IITC_FileManager {
|
public class IITC_FileManager {
|
||||||
private static final WebResourceResponse EMPTY =
|
private static final WebResourceResponse EMPTY =
|
||||||
@ -50,6 +52,8 @@ public class IITC_FileManager {
|
|||||||
+ "(document.body || document.head || document.documentElement).appendChild(script);";
|
+ "(document.body || document.head || document.documentElement).appendChild(script);";
|
||||||
|
|
||||||
public static final String DOMAIN = ".iitcm.localhost";
|
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
|
* 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);
|
final InputStream data = prepareUserScript(stream);
|
||||||
|
|
||||||
new UpdateScript(mActivity).execute(uri.getPath());
|
|
||||||
return new WebResourceResponse("application/x-javascript", "UTF-8", data);
|
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 class FileRequest extends WebResourceResponse implements ResponseHandler, Runnable {
|
||||||
private Intent mData;
|
private Intent mData;
|
||||||
private final String mFunctionName;
|
private final String mFunctionName;
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -191,6 +191,10 @@ public class IITC_Mobile extends Activity
|
|||||||
return;
|
return;
|
||||||
} else if (key.equals("pref_fake_user_agent")) {
|
} else if (key.equals("pref_fake_user_agent")) {
|
||||||
mIitcWebView.setUserAgent();
|
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")
|
} else if (key.equals("pref_press_twice_to_exit")
|
||||||
|| key.equals("pref_share_selected_tab")
|
|| key.equals("pref_share_selected_tab")
|
||||||
|| key.equals("pref_messages")
|
|| key.equals("pref_messages")
|
||||||
@ -704,6 +708,7 @@ public class IITC_Mobile extends Activity
|
|||||||
mNavigationHelper.onLoadingStateChanged();
|
mNavigationHelper.onLoadingStateChanged();
|
||||||
invalidateOptionsMenu();
|
invalidateOptionsMenu();
|
||||||
updateViews();
|
updateViews();
|
||||||
|
if (!isLoading) mFileManager.updatePlugins(false);
|
||||||
|
|
||||||
if (mSearchTerm != null && !isLoading) {
|
if (mSearchTerm != null && !isLoading) {
|
||||||
new Handler().postDelayed(new Runnable() {
|
new Handler().postDelayed(new Runnable() {
|
||||||
|
@ -8,6 +8,7 @@ import android.os.AsyncTask;
|
|||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
import com.cradle.iitc_mobile.IITC_FileManager;
|
import com.cradle.iitc_mobile.IITC_FileManager;
|
||||||
|
import com.cradle.iitc_mobile.IITC_Mobile;
|
||||||
import com.cradle.iitc_mobile.Log;
|
import com.cradle.iitc_mobile.Log;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -20,8 +21,6 @@ import java.net.URL;
|
|||||||
public class UpdateScript extends AsyncTask<String, Void, Boolean> {
|
public class UpdateScript extends AsyncTask<String, Void, Boolean> {
|
||||||
|
|
||||||
private final Activity mActivity;
|
private final Activity mActivity;
|
||||||
// update interval is 2 days
|
|
||||||
private final long updateInterval = 1000*60*60*24*2;
|
|
||||||
private String mFilePath;
|
private String mFilePath;
|
||||||
private String mScript;
|
private String mScript;
|
||||||
|
|
||||||
@ -33,18 +32,6 @@ public class UpdateScript extends AsyncTask<String, Void, Boolean> {
|
|||||||
protected Boolean doInBackground(final String... urls) {
|
protected Boolean doInBackground(final String... urls) {
|
||||||
try {
|
try {
|
||||||
mFilePath = urls[0];
|
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
|
// get local script meta information
|
||||||
mScript = IITC_FileManager.readStream(new FileInputStream(new File(mFilePath)));
|
mScript = IITC_FileManager.readStream(new FileInputStream(new File(mFilePath)));
|
||||||
final String updateURL = IITC_FileManager.getScriptInfo(mScript).get("updateURL");
|
final String updateURL = IITC_FileManager.getScriptInfo(mScript).get("updateURL");
|
||||||
@ -77,12 +64,19 @@ public class UpdateScript extends AsyncTask<String, Void, Boolean> {
|
|||||||
.setTitle("Plugin updated")
|
.setTitle("Plugin updated")
|
||||||
.setMessage(name)
|
.setMessage(name)
|
||||||
.setCancelable(true)
|
.setCancelable(true)
|
||||||
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
dialog.cancel();
|
dialog.cancel();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
.setNegativeButton("Reload", new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
dialog.cancel();
|
||||||
|
((IITC_Mobile) mActivity).reloadIITC();
|
||||||
|
}
|
||||||
|
})
|
||||||
.create()
|
.create()
|
||||||
.show();
|
.show();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user