From 12688ba761ad7e9669c7a1c1b1141ac3ef38cba5 Mon Sep 17 00:00:00 2001 From: Gabriele M Date: Sun, 16 Jul 2017 22:28:11 +0200 Subject: [PATCH] Allow to check if an update is of type AB from anywhere --- .../updater/controller/ABUpdateInstaller.java | 13 +++---------- .../updater/controller/UpdaterService.java | 6 ++---- src/org/lineageos/updater/misc/Constants.java | 3 +++ src/org/lineageos/updater/misc/Utils.java | 12 ++++++++++++ 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/org/lineageos/updater/controller/ABUpdateInstaller.java b/src/org/lineageos/updater/controller/ABUpdateInstaller.java index 0141c14..fe62bb4 100644 --- a/src/org/lineageos/updater/controller/ABUpdateInstaller.java +++ b/src/org/lineageos/updater/controller/ABUpdateInstaller.java @@ -21,6 +21,7 @@ import android.util.Log; import org.lineageos.updater.UpdateDownload; import org.lineageos.updater.UpdateStatus; +import org.lineageos.updater.misc.Constants; import org.lineageos.updater.misc.Utils; import java.io.BufferedReader; @@ -39,9 +40,6 @@ class ABUpdateInstaller { private static String sDownloadId; - private static final String PAYLOAD_BIN_PATH = "payload.bin"; - private static final String PAYLOAD_PROPERTIES_PATH = "payload_properties.txt"; - private final UpdaterController mUpdaterController; private final String mDownloadId; @@ -131,8 +129,8 @@ class ABUpdateInstaller { String[] headerKeyValuePairs; try { ZipFile zipFile = new ZipFile(file); - offset = Utils.getZipEntryOffset(zipFile, PAYLOAD_BIN_PATH); - ZipEntry payloadPropEntry = zipFile.getEntry(PAYLOAD_PROPERTIES_PATH); + offset = Utils.getZipEntryOffset(zipFile, Constants.AB_PAYLOAD_BIN_PATH); + ZipEntry payloadPropEntry = zipFile.getEntry(Constants.AB_PAYLOAD_PROPERTIES_PATH); try (InputStream is = zipFile.getInputStream(payloadPropEntry); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr)) { @@ -159,9 +157,4 @@ class ABUpdateInstaller { return true; } - - static boolean isABUpdate(ZipFile zipFile) { - return zipFile.getEntry(PAYLOAD_BIN_PATH) != null && - zipFile.getEntry(PAYLOAD_PROPERTIES_PATH) != null; - } } diff --git a/src/org/lineageos/updater/controller/UpdaterService.java b/src/org/lineageos/updater/controller/UpdaterService.java index 10c2c9b..312e523 100644 --- a/src/org/lineageos/updater/controller/UpdaterService.java +++ b/src/org/lineageos/updater/controller/UpdaterService.java @@ -36,6 +36,7 @@ import org.lineageos.updater.UpdateDownload; import org.lineageos.updater.UpdateStatus; import org.lineageos.updater.UpdaterReceiver; import org.lineageos.updater.UpdatesActivity; +import org.lineageos.updater.misc.Utils; import java.io.IOException; import java.text.NumberFormat; @@ -165,10 +166,7 @@ public class UpdaterService extends Service { throw new IllegalArgumentException(update.getDownloadId() + " is not verified"); } try { - ZipFile zipFile = new ZipFile(update.getFile()); - boolean isABUpdate = ABUpdateInstaller.isABUpdate(zipFile); - zipFile.close(); - if (isABUpdate) { + if (Utils.isABUpdate(update.getFile())) { ABUpdateInstaller.start(mUpdaterController, downloadId); } else { android.os.RecoverySystem.installPackage(this, update.getFile()); diff --git a/src/org/lineageos/updater/misc/Constants.java b/src/org/lineageos/updater/misc/Constants.java index 3f31d98..570fd4b 100644 --- a/src/org/lineageos/updater/misc/Constants.java +++ b/src/org/lineageos/updater/misc/Constants.java @@ -20,6 +20,9 @@ public final class Constants { private Constants() { } + public static final String AB_PAYLOAD_BIN_PATH = "payload.bin"; + public static final String AB_PAYLOAD_PROPERTIES_PATH = "payload_properties.txt"; + public static final String PREF_LAST_UPDATE_CHECK = "last_update_check"; public static final String PREF_AUTO_UPDATES_CHECK = "auto_updates_check"; diff --git a/src/org/lineageos/updater/misc/Utils.java b/src/org/lineageos/updater/misc/Utils.java index a9bfc35..be7fc21 100644 --- a/src/org/lineageos/updater/misc/Utils.java +++ b/src/org/lineageos/updater/misc/Utils.java @@ -268,4 +268,16 @@ public class Utils { } throw new IllegalStateException(); } + + public static boolean isABUpdate(ZipFile zipFile) { + return zipFile.getEntry(Constants.AB_PAYLOAD_BIN_PATH) != null && + zipFile.getEntry(Constants.AB_PAYLOAD_PROPERTIES_PATH) != null; + } + + public static boolean isABUpdate(File file) throws IOException { + ZipFile zipFile = new ZipFile(file); + boolean isAB = isABUpdate(zipFile); + zipFile.close(); + return isAB; + } }