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:
fkloft 2014-08-06 14:38:38 +02:00
parent 40d2b3ebb0
commit 9a82110a1f
2 changed files with 59 additions and 26 deletions

View File

@ -52,10 +52,10 @@ public class IITC_FileManager {
"script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');'));\n"
+ "(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";
// update interval is 2 days by default
/**
* 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;
}
public static String readFile(final File file) throws IOException {
return readStream(new FileInputStream(file));
}
public static String readStream(final InputStream stream) {
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
if (mUpdateInterval == 0 && !force) return;
// check last script update
@ -347,8 +351,8 @@ public class IITC_FileManager {
.commit();
}
public void setUpdateInterval(int interval) {
mUpdateInterval = 1000*60*60*24 * interval;
public void setUpdateInterval(final int interval) {
mUpdateInterval = 1000 * 60 * 60 * 24 * interval;
}
private class FileRequest extends WebResourceResponse implements ResponseHandler, Runnable {

View File

@ -3,7 +3,6 @@ package com.cradle.iitc_mobile.async;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
@ -16,6 +15,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
@ -25,9 +25,12 @@ public class UpdateScript extends AsyncTask<String, Void, Boolean> {
private String mFilePath;
private String mScript;
private HashMap<String, String> mScriptInfo;
private final boolean mForceSecureUpdates;
public UpdateScript(final Activity activity) {
mActivity = activity;
mForceSecureUpdates = PreferenceManager.getDefaultSharedPreferences(mActivity)
.getBoolean("pref_secure_updates", true);
}
@Override
@ -37,37 +40,63 @@ public class UpdateScript extends AsyncTask<String, Void, Boolean> {
// get local script meta information
mScript = IITC_FileManager.readStream(new FileInputStream(new File(mFilePath)));
mScriptInfo = IITC_FileManager.getScriptInfo(mScript);
String updateURL = mScriptInfo.get("updateURL");
final String downloadURL = mScriptInfo.get("downloadURL");
String downloadURL = mScriptInfo.get("downloadURL");
if (updateURL == null) updateURL = downloadURL;
// check for https protocol
final URL url = new URL(updateURL);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
Boolean secureUpdates = prefs.getBoolean("pref_secure_updates", true);
if (!url.getProtocol().equals("https") && secureUpdates) return false;
if (!isUpdateAllowed(updateURL)) return false;
final File updateFile = File.createTempFile("iitc.update", ".meta.js", mActivity.getCacheDir());
IITC_FileManager.copyStream(new URL(updateURL).openStream(), new FileOutputStream(updateFile), true);
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 String local_version = mScriptInfo.get("version");
// update script if neccessary
if (local_version.compareTo(remote_version) < 0) {
if (local_version.compareTo(remote_version) >= 0) return false;
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);
Log.d("...done");
return true;
InputStream sourceStream;
if (updateURL.equals(downloadURL)) {
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) {
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) {
final String name = IITC_FileManager.getScriptInfo(mScript).get("name");
new AlertDialog.Builder(mActivity)
@ -76,13 +105,13 @@ public class UpdateScript extends AsyncTask<String, Void, Boolean> {
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
public void onClick(final DialogInterface dialog, final int which) {
dialog.cancel();
}
})
.setNegativeButton("Reload", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
public void onClick(final DialogInterface dialog, final int which) {
dialog.cancel();
((IITC_Mobile) mActivity).reloadIITC();
}