refactored read and fetch of configuration
diff --git a/src/com/fairphone/updater/FairphoneUpdater.java b/src/com/fairphone/updater/FairphoneUpdater.java
index ca2b7f5..c86898d 100644
--- a/src/com/fairphone/updater/FairphoneUpdater.java
+++ b/src/com/fairphone/updater/FairphoneUpdater.java
@@ -1,13 +1,11 @@
package com.fairphone.updater;
import android.app.AlertDialog;
-import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
-import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
@@ -179,6 +177,8 @@
initHeaderViews();
setupFragments(savedInstanceState);
+
+ startService();
}
@@ -836,8 +836,6 @@
updateStatePreference(TextUtils.isEmpty(mZipPath) ? UpdaterState.NORMAL : UpdaterState.ZIP_INSTALL);
}
- startService();
-
boolean isConfigLoaded = UpdaterService.readUpdaterData(this);
mDeviceVersion = VersionParserHelper.getDeviceVersion(this);
diff --git a/src/com/fairphone/updater/UpdaterService.java b/src/com/fairphone/updater/UpdaterService.java
index a211cd8..42ab2a2 100644
--- a/src/com/fairphone/updater/UpdaterService.java
+++ b/src/com/fairphone/updater/UpdaterService.java
@@ -16,12 +16,6 @@
package com.fairphone.updater;
-import java.io.File;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-import java.nio.charset.Charset;
-
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.app.Notification;
@@ -49,8 +43,6 @@
import android.util.Log;
import android.widget.Toast;
-import java.util.concurrent.TimeoutException;
-
import com.fairphone.updater.data.UpdaterData;
import com.fairphone.updater.data.Version;
import com.fairphone.updater.data.VersionParserHelper;
@@ -61,6 +53,16 @@
import com.stericson.RootTools.execution.CommandCapture;
import com.stericson.RootTools.execution.Shell;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.nio.channels.FileChannel;
+import java.nio.charset.Charset;
+import java.util.concurrent.TimeoutException;
+
public class UpdaterService extends Service
{
@@ -462,11 +464,22 @@
}
}
+ private static void copyConfigToData(Context context) throws IOException {
+ Resources resources = context.getApplicationContext().getResources();
+ String targetPath = Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder);
+ FileInputStream inStream = new FileInputStream(targetPath + resources.getString(R.string.configFilename) + resources.getString(R.string.config_xml));
+ FileOutputStream outStream = context.openFileOutput(resources.getString(R.string.configFilename) + resources.getString(R.string.config_xml), MODE_PRIVATE);
+ FileChannel inChannel = inStream.getChannel();
+ FileChannel outChannel = outStream.getChannel();
+ inChannel.transferTo(0, inChannel.size(), outChannel);
+ inStream.close();
+ outStream.close();
+ }
+
private static void checkVersionValidation(Context context)
{
Version latestVersion = VersionParserHelper.getLatestVersion(context.getApplicationContext());
Version currentVersion = VersionParserHelper.getDeviceVersion(context.getApplicationContext());
-
if (latestVersion != null)
{
if (latestVersion.isNewerVersionThan(currentVersion))
@@ -481,7 +494,12 @@
context.sendBroadcast(updateIntent);
}
- public static boolean readUpdaterData(Context context)
+ public static boolean readUpdaterData(Context context){
+ Version latestVersion = VersionParserHelper.getLatestVersion(context.getApplicationContext());
+ return latestVersion != null;
+ }
+
+ public static boolean updateUpdaterData(Context context)
{
boolean retVal = false;
@@ -495,21 +513,31 @@
if (file.exists())
{
String md5sum = Utils.calculateMD5(file);
- SharedPreferences sp = context.getSharedPreferences(FairphoneUpdater.FAIRPHONE_UPDATER_PREFERENCES, MODE_PRIVATE);
+ SharedPreferences sp = context.getApplicationContext().getSharedPreferences(FairphoneUpdater.FAIRPHONE_UPDATER_PREFERENCES, MODE_PRIVATE);
if(sp.getString(PREFERENCE_CONFIG_MD_5, "").equals(md5sum)){
retVal = true;
- } else if (RSAUtils.checkFileSignature(context, filePath, targetPath)) {
- checkVersionValidation(context);
- retVal = true;
- sp.edit().putString(PREFERENCE_CONFIG_MD_5, md5sum).apply();
} else {
- //Toast.makeText(context, resources.getString(R.string.invalid_signature_download_message), Toast.LENGTH_LONG).show();
- final boolean notDeleted = !file.delete();
- if(notDeleted) {
- Log.d(TAG, "Unable to delete "+file.getAbsolutePath());
- }
+ if (RSAUtils.checkFileSignature(context, filePath, targetPath)) {
+ try {
+ copyConfigToData(context);
+ checkVersionValidation(context);
+ retVal = true;
+ sp.edit().putString(PREFERENCE_CONFIG_MD_5, md5sum).apply();
+ } catch (IOException e) {
+ Log.e(TAG, "Failed to store configuration " + e.getLocalizedMessage());
+ retVal = false;
+ }
+ } else {
+ //Toast.makeText(context, resources.getString(R.string.invalid_signature_download_message), Toast.LENGTH_LONG).show();
+ final boolean notDeleted = !file.delete();
+ if(notDeleted) {
+ Log.d(TAG, "Unable to delete "+file.getAbsolutePath());
+ }
+ }
}
+ } else {
+ Log.wtf(TAG, "No file");
}
return retVal;
@@ -603,13 +631,10 @@
case DownloadManager.STATUS_SUCCESSFUL:
{
Log.d(TAG, "Download successful.");
- String filePath = mDownloadManager.getUriForDownloadedFile(mLatestFileDownloadId).getPath();
- String targetPath = Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder);
-
- if (RSAUtils.checkFileSignature(context, filePath, targetPath))
+ if (updateUpdaterData(context))
{
- checkVersionValidation(context);
+ removeLatestFileDownload(context);
}
else
{
diff --git a/src/com/fairphone/updater/data/VersionParserHelper.java b/src/com/fairphone/updater/data/VersionParserHelper.java
index 320a935..c2b7fcf 100644
--- a/src/com/fairphone/updater/data/VersionParserHelper.java
+++ b/src/com/fairphone/updater/data/VersionParserHelper.java
@@ -32,6 +32,7 @@
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
@@ -124,15 +125,17 @@
Version latest = null;
Resources resources = context.getResources();
- String filePath =
- Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder) + resources.getString(R.string.configFilename);
- File file = new File(filePath + resources.getString(R.string.config_xml));
+ FileInputStream fis = null;
+ try {
+ fis = context.openFileInput(resources.getString(R.string.configFilename) + resources.getString(R.string.config_xml));
+ } catch (FileNotFoundException e){
+ }
- if (file.exists())
+ if (fis != null)
{
try
{
- latest = parseLatestXML(context, file);
+ latest = parseLatestXML(context, fis);
} catch (XmlPullParserException e)
{
Log.e(TAG, "Could not start the XML parser", e);
@@ -162,7 +165,12 @@
// @formatter:on
- private static Version parseLatestXML(Context context, File latestFile) throws XmlPullParserException, IOException
+ private static Version parseLatestXML(Context context, File latestFile) throws XmlPullParserException, IOException {
+ FileInputStream fis = new FileInputStream(latestFile);
+ return parseLatestXML(context, fis);
+ }
+
+ private static Version parseLatestXML(Context context, FileInputStream fis) throws XmlPullParserException, IOException
{
Version version = null;
@@ -175,7 +183,6 @@
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
- FileInputStream fis = new FileInputStream(latestFile);
XmlPullParser xpp = factory.newPullParser();
diff --git a/src/com/fairphone/updater/tools/RSAUtils.java b/src/com/fairphone/updater/tools/RSAUtils.java
index 90cef76..ed98d7b 100644
--- a/src/com/fairphone/updater/tools/RSAUtils.java
+++ b/src/com/fairphone/updater/tools/RSAUtils.java
@@ -16,6 +16,13 @@
package com.fairphone.updater.tools;
+import android.content.Context;
+import android.content.res.Resources;
+import android.util.Base64;
+import android.util.Log;
+
+import com.fairphone.updater.R;
+
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
@@ -32,13 +39,6 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
-import android.content.Context;
-import android.content.res.Resources;
-import android.util.Base64;
-import android.util.Log;
-
-import com.fairphone.updater.R;
-
public class RSAUtils
{
@@ -163,7 +163,7 @@
return valid;
}
- private static void unzip(String filePath, String targetPath)
+ public static void unzip(String filePath, String targetPath)
{
File path = new File(targetPath);
boolean notMkDirs = !path.mkdirs();