From 0daa4ad9ef335711c6c04d2773f391fce0b053e2 Mon Sep 17 00:00:00 2001 From: Ido Ben-Hur Date: Thu, 23 Feb 2023 02:26:13 +0200 Subject: [PATCH] OpenDelta: Describe non update engine errors as well And improve some code / logic --- res/values/strings.xml | 5 +++ src/eu/chainfire/opendelta/ABUpdate.java | 40 ++++++++++++------- src/eu/chainfire/opendelta/UpdateService.java | 38 ++++++------------ 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 04e6b9d..bba8a5c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -162,6 +162,11 @@ Not enough free space Device corruption\nA clean install through recovery is advised + + Update file not found\nPress check / select it again + Update engine isn\'t ready\nReboot and try again + Update file corrupted\nDownload the file again + Not an AB update file\nDownload the file again Long press to hide Info hidden. Undo in settings diff --git a/src/eu/chainfire/opendelta/ABUpdate.java b/src/eu/chainfire/opendelta/ABUpdate.java index 845a9c9..11e84ca 100644 --- a/src/eu/chainfire/opendelta/ABUpdate.java +++ b/src/eu/chainfire/opendelta/ABUpdate.java @@ -42,6 +42,12 @@ class ABUpdate { private static final String PREFS_IS_INSTALLING_UPDATE = "prefs_is_installing_update"; private static final long WAKELOCK_TIMEOUT = 60 * 60 * 1000; /* 1 hour */ + // non UpdateEngine errors + public static final int ERROR_NOT_FOUND = 99; + private static final int ERROR_NOT_READY = 98; + private static final int ERROR_CORRUPTED = 97; + private static final int ERROR_INVALID = 96; + private static ABUpdate mInstance; private final UpdateService mUpdateService; @@ -103,21 +109,27 @@ class ABUpdate { } }; - public boolean start(String zipPath, ProgressListener listener) { + public int start(String zipPath, ProgressListener listener) { + try (ZipFile zipFile = new ZipFile(zipPath)) { + if (!isABUpdate(zipFile)) return ERROR_INVALID; + } catch (Exception ex) { + Logger.ex(ex); + return ERROR_INVALID; + } mZipPath = zipPath; mProgressListener = listener; if (isInstallingUpdate(mUpdateService)) { - return true; + return -1; } - final boolean installing = startUpdate(); - setInstallingUpdate(installing, mUpdateService); + final int installing = startUpdate(); + setInstallingUpdate(installing < 0, mUpdateService); return installing; } - public boolean resume() { + public int resume() { final boolean installing = bindCallbacks(); setInstallingUpdate(installing, mUpdateService); - return installing; + return installing ? -1 : ERROR_NOT_READY; } public void pokeStatus() { @@ -165,17 +177,16 @@ class ABUpdate { return true; } - private boolean startUpdate() { + private int startUpdate() { File file = new File(mZipPath); if (!file.exists()) { Log.e(TAG, "The given update doesn't exist"); - return false; + return ERROR_NOT_FOUND; } long offset; String[] headerKeyValuePairs; - try { - ZipFile zipFile = new ZipFile(file); + try (ZipFile zipFile = new ZipFile(file)) { offset = getZipEntryOffset(zipFile, PAYLOAD_BIN_PATH); ZipEntry payloadPropEntry = zipFile.getEntry(PAYLOAD_PROPERTIES_PATH); try (InputStream is = zipFile.getInputStream(payloadPropEntry); @@ -188,21 +199,20 @@ class ABUpdate { headerKeyValuePairs = new String[lines.size()]; headerKeyValuePairs = lines.toArray(headerKeyValuePairs); } - zipFile.close(); } catch (IOException | IllegalArgumentException e) { Log.e(TAG, "Could not prepare " + file, e); - return false; + return ERROR_CORRUPTED; } mUpdateEngine.setPerformanceMode(mEnableABPerfMode); - if (!bindCallbacks()) return false; + if (!bindCallbacks()) return ERROR_NOT_READY; String zipFileUri = "file://" + file.getAbsolutePath(); mUpdateEngine.applyPayload(zipFileUri, offset, 0, headerKeyValuePairs); - return true; + return -1; } - static boolean isABUpdate(ZipFile zipFile) { + private static boolean isABUpdate(ZipFile zipFile) { return zipFile.getEntry(PAYLOAD_BIN_PATH) != null && zipFile.getEntry(PAYLOAD_PROPERTIES_PATH) != null; } diff --git a/src/eu/chainfire/opendelta/UpdateService.java b/src/eu/chainfire/opendelta/UpdateService.java index 882e2f2..4c58c15 100644 --- a/src/eu/chainfire/opendelta/UpdateService.java +++ b/src/eu/chainfire/opendelta/UpdateService.java @@ -449,10 +449,11 @@ public class UpdateService extends Service implements OnSharedPreferenceChangeLi mLastProgressTime = new long[] { 0, SystemClock.elapsedRealtime() }; mProgressListener.setStatus(_filename); mState.update(State.ACTION_AB_FLASH, 0f, 0L, 100L, _filename, null); - mIsUpdateRunning = ABUpdate.getInstance(this).resume(); + final int code = ABUpdate.getInstance(this).resume(); + mIsUpdateRunning = code < 0; if (!mIsUpdateRunning) { mNotificationManager.cancel(NOTIFICATION_UPDATE); - mState.update(State.ERROR_AB_FLASH); + mState.update(State.ERROR_AB_FLASH, code); } else { newFlashNotification(_filename); } @@ -1045,7 +1046,7 @@ public class UpdateService extends Service implements OnSharedPreferenceChangeLi flashFilename = handleUpdateCleanup(); } catch (Exception ex) { mIsUpdateRunning = false; - mState.update(State.ERROR_AB_FLASH); + mState.update(State.ERROR_AB_FLASH, ABUpdate.ERROR_NOT_FOUND); Logger.ex(ex); return; } @@ -1061,29 +1062,16 @@ public class UpdateService extends Service implements OnSharedPreferenceChangeLi newFlashNotification(_filename); - try { - ZipFile zipFile = new ZipFile(flashFilename); - boolean isABUpdate = ABUpdate.isABUpdate(zipFile); - zipFile.close(); - if (isABUpdate) { - mLastProgressTime = new long[] { 0, SystemClock.elapsedRealtime() }; - mProgressListener.setStatus(_filename); - mIsUpdateRunning = true; - if (!ABUpdate.getInstance(this).start(flashFilename, mProgressListener)) { - mNotificationManager.cancel(NOTIFICATION_UPDATE); - mIsUpdateRunning = false; - mState.update(State.ERROR_AB_FLASH); - } - } else { - mNotificationManager.cancel(NOTIFICATION_UPDATE); - mIsUpdateRunning = false; - mState.update(State.ERROR_AB_FLASH); - } - } catch (Exception ex) { - Logger.ex(ex); - mIsUpdateRunning = false; - mState.update(State.ERROR_AB_FLASH); + final int code = ABUpdate.getInstance(this).start(flashFilename, mProgressListener); + if (code < 0) { + mLastProgressTime = new long[] { 0, SystemClock.elapsedRealtime() }; + mProgressListener.setStatus(_filename); + mIsUpdateRunning = true; + return; } + mNotificationManager.cancel(NOTIFICATION_UPDATE); + mIsUpdateRunning = false; + mState.update(State.ERROR_AB_FLASH, code); } @SuppressLint({"SdCardPath", "SetWorldReadable"})