improvments to plugin updater
- don't download a file twice if there is no updateURL (or if updateURL==downloadURL) - use the downloadURL from the new meta file
This commit is contained in:
parent
40d2b3ebb0
commit
9a82110a1f
@ -52,10 +52,10 @@ public class IITC_FileManager {
|
|||||||
"script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');'));\n"
|
"script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');'));\n"
|
||||||
+ "(document.body || document.head || document.documentElement).appendChild(script);";
|
+ "(document.body || document.head || document.documentElement).appendChild(script);";
|
||||||
|
|
||||||
private long mUpdateInterval = 1000*60*60*24*7;
|
// update interval is 2 days by default
|
||||||
|
private long mUpdateInterval = 1000 * 60 * 60 * 24 * 7;
|
||||||
|
|
||||||
public static final String DOMAIN = ".iitcm.localhost";
|
public static final String DOMAIN = ".iitcm.localhost";
|
||||||
// update interval is 2 days by default
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
@ -119,6 +119,10 @@ public class IITC_FileManager {
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String readFile(final File file) throws IOException {
|
||||||
|
return readStream(new FileInputStream(file));
|
||||||
|
}
|
||||||
|
|
||||||
public static String readStream(final InputStream stream) {
|
public static String readStream(final InputStream stream) {
|
||||||
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
|
||||||
@ -320,7 +324,7 @@ public class IITC_FileManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updatePlugins(boolean force) {
|
public void updatePlugins(final boolean force) {
|
||||||
// do nothing if updates are disabled
|
// do nothing if updates are disabled
|
||||||
if (mUpdateInterval == 0 && !force) return;
|
if (mUpdateInterval == 0 && !force) return;
|
||||||
// check last script update
|
// check last script update
|
||||||
@ -347,8 +351,8 @@ public class IITC_FileManager {
|
|||||||
.commit();
|
.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUpdateInterval(int interval) {
|
public void setUpdateInterval(final int interval) {
|
||||||
mUpdateInterval = 1000*60*60*24 * interval;
|
mUpdateInterval = 1000 * 60 * 60 * 24 * interval;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class FileRequest extends WebResourceResponse implements ResponseHandler, Runnable {
|
private class FileRequest extends WebResourceResponse implements ResponseHandler, Runnable {
|
||||||
|
@ -3,7 +3,6 @@ package com.cradle.iitc_mobile.async;
|
|||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
@ -16,6 +15,7 @@ import java.io.FileInputStream;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
@ -25,9 +25,12 @@ public class UpdateScript extends AsyncTask<String, Void, Boolean> {
|
|||||||
private String mFilePath;
|
private String mFilePath;
|
||||||
private String mScript;
|
private String mScript;
|
||||||
private HashMap<String, String> mScriptInfo;
|
private HashMap<String, String> mScriptInfo;
|
||||||
|
private final boolean mForceSecureUpdates;
|
||||||
|
|
||||||
public UpdateScript(final Activity activity) {
|
public UpdateScript(final Activity activity) {
|
||||||
mActivity = activity;
|
mActivity = activity;
|
||||||
|
mForceSecureUpdates = PreferenceManager.getDefaultSharedPreferences(mActivity)
|
||||||
|
.getBoolean("pref_secure_updates", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -37,37 +40,63 @@ public class UpdateScript extends AsyncTask<String, Void, Boolean> {
|
|||||||
// 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)));
|
||||||
mScriptInfo = IITC_FileManager.getScriptInfo(mScript);
|
mScriptInfo = IITC_FileManager.getScriptInfo(mScript);
|
||||||
|
|
||||||
String updateURL = mScriptInfo.get("updateURL");
|
String updateURL = mScriptInfo.get("updateURL");
|
||||||
final String downloadURL = mScriptInfo.get("downloadURL");
|
String downloadURL = mScriptInfo.get("downloadURL");
|
||||||
if (updateURL == null) updateURL = downloadURL;
|
if (updateURL == null) updateURL = downloadURL;
|
||||||
|
|
||||||
// check for https protocol
|
if (!isUpdateAllowed(updateURL)) return false;
|
||||||
final URL url = new URL(updateURL);
|
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
|
final File updateFile = File.createTempFile("iitc.update", ".meta.js", mActivity.getCacheDir());
|
||||||
Boolean secureUpdates = prefs.getBoolean("pref_secure_updates", true);
|
IITC_FileManager.copyStream(new URL(updateURL).openStream(), new FileOutputStream(updateFile), true);
|
||||||
if (!url.getProtocol().equals("https") && secureUpdates) return false;
|
|
||||||
|
final HashMap<String, String> updateInfo =
|
||||||
|
IITC_FileManager.getScriptInfo(IITC_FileManager.readFile(updateFile));
|
||||||
|
|
||||||
|
final String remote_version = updateInfo.get("version");
|
||||||
|
|
||||||
// get remote script meta information
|
|
||||||
final InputStream is = url.openStream();
|
|
||||||
final String remote_version = IITC_FileManager.getScriptInfo(IITC_FileManager.readStream(is)).get("version");
|
|
||||||
final File local_file = new File(mFilePath);
|
final File local_file = new File(mFilePath);
|
||||||
final String local_version = mScriptInfo.get("version");
|
final String local_version = mScriptInfo.get("version");
|
||||||
|
|
||||||
// update script if neccessary
|
if (local_version.compareTo(remote_version) >= 0) return false;
|
||||||
if (local_version.compareTo(remote_version) < 0) {
|
|
||||||
Log.d("plugin " + mFilePath + " outdated\n" + local_version + " vs " + remote_version);
|
Log.d("plugin " + mFilePath + " outdated\n" + local_version + " vs " + remote_version);
|
||||||
Log.d("updating file....");
|
|
||||||
IITC_FileManager.copyStream(new URL(downloadURL).openStream(), new FileOutputStream(local_file), true);
|
InputStream sourceStream;
|
||||||
Log.d("...done");
|
if (updateURL.equals(downloadURL)) {
|
||||||
return true;
|
sourceStream = new FileInputStream(updateFile);
|
||||||
|
} else {
|
||||||
|
if (updateInfo.get("downloadURL") != null) {
|
||||||
|
downloadURL = updateInfo.get("downloadURL");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isUpdateAllowed(downloadURL)) return false;
|
||||||
|
|
||||||
|
sourceStream = new URL(downloadURL).openStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.d("updating file....");
|
||||||
|
IITC_FileManager.copyStream(sourceStream, new FileOutputStream(local_file), true);
|
||||||
|
Log.d("...done");
|
||||||
|
|
||||||
|
updateFile.delete();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void onPostExecute(Boolean updated) {
|
private boolean isUpdateAllowed(final String url) throws MalformedURLException {
|
||||||
|
if (new URL(url).getProtocol().equals("https"))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return !mForceSecureUpdates;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(final Boolean updated) {
|
||||||
if (updated) {
|
if (updated) {
|
||||||
final String name = IITC_FileManager.getScriptInfo(mScript).get("name");
|
final String name = IITC_FileManager.getScriptInfo(mScript).get("name");
|
||||||
new AlertDialog.Builder(mActivity)
|
new AlertDialog.Builder(mActivity)
|
||||||
@ -76,13 +105,13 @@ public class UpdateScript extends AsyncTask<String, Void, Boolean> {
|
|||||||
.setCancelable(true)
|
.setCancelable(true)
|
||||||
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(final DialogInterface dialog, final int which) {
|
||||||
dialog.cancel();
|
dialog.cancel();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.setNegativeButton("Reload", new DialogInterface.OnClickListener() {
|
.setNegativeButton("Reload", new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(final DialogInterface dialog, final int which) {
|
||||||
dialog.cancel();
|
dialog.cancel();
|
||||||
((IITC_Mobile) mActivity).reloadIITC();
|
((IITC_Mobile) mActivity).reloadIITC();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user