diff --git a/res/values/strings.xml b/res/values/strings.xml index f3abd16c..e5b114e9 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -18,7 +18,10 @@ Updater Updater + /data/ota_package/ + https://download.lineageos.org/api Verification failed diff --git a/src/org/lineageos/updater/UpdatesCheckReceiver.java b/src/org/lineageos/updater/UpdatesCheckReceiver.java index 3ccd5fbd..54066066 100644 --- a/src/org/lineageos/updater/UpdatesCheckReceiver.java +++ b/src/org/lineageos/updater/UpdatesCheckReceiver.java @@ -46,6 +46,8 @@ public class UpdatesCheckReceiver extends BroadcastReceiver { if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { // Set a repeating alarm on boot to check for new updates once per day scheduleRepeatingUpdatesCheck(context); + + Utils.cleanupDownloadsDir(context); } final SharedPreferences preferences = diff --git a/src/org/lineageos/updater/controller/UpdaterController.java b/src/org/lineageos/updater/controller/UpdaterController.java index fa145931..987483e5 100644 --- a/src/org/lineageos/updater/controller/UpdaterController.java +++ b/src/org/lineageos/updater/controller/UpdaterController.java @@ -79,6 +79,8 @@ public class UpdaterController implements UpdaterControllerInt { mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Updater"); mWakeLock.setReferenceCounted(false); + Utils.cleanupDownloadsDir(context); + for (UpdateDownload update : mUpdatesDbHelper.getUpdates()) { addUpdate(update, false); } diff --git a/src/org/lineageos/updater/misc/Utils.java b/src/org/lineageos/updater/misc/Utils.java index 0eb76e58..644bab53 100644 --- a/src/org/lineageos/updater/misc/Utils.java +++ b/src/org/lineageos/updater/misc/Utils.java @@ -16,9 +16,11 @@ package org.lineageos.updater.misc; import android.content.Context; +import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.SystemProperties; +import android.preference.PreferenceManager; import android.util.Log; import org.json.JSONArray; @@ -28,6 +30,7 @@ import org.lineageos.updater.R; import org.lineageos.updater.Update; import org.lineageos.updater.UpdateDownload; import org.lineageos.updater.UpdateStatus; +import org.lineageos.updater.UpdatesDbHelper; import java.io.BufferedReader; import java.io.File; @@ -201,4 +204,45 @@ public class Utils { Log.e(TAG, "Entry " + entryPath + " not found"); throw new IllegalArgumentException("The given entry was not found"); } + + /** + * Cleanup the download directory, which is assumed to be a privileged location + * the user can't access and that might have stale files. This can happen if + * the data of the application are wiped. + * + * @param context + */ + public static void cleanupDownloadsDir(Context context) { + final String DOWNLOADS_CLEANUP_DONE = "cleanup_done"; + + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + if (preferences.getBoolean(DOWNLOADS_CLEANUP_DONE, false)) { + return; + } + + File downloadPath = getDownloadPath(context); + Log.d(TAG, "Cleaning " + downloadPath); + if (!downloadPath.isDirectory()) { + return; + } + File[] files = downloadPath.listFiles(); + if (files == null) { + return; + } + + // Ideally the database is empty when we get here + UpdatesDbHelper dbHelper = new UpdatesDbHelper(context); + List knownPaths = new ArrayList<>(); + for (UpdateDownload update : dbHelper.getUpdates()) { + knownPaths.add(update.getFile().getAbsolutePath()); + } + for (File file : files) { + if (!knownPaths.contains(file.getAbsolutePath())) { + Log.d(TAG, "Deleting " + file.getAbsolutePath()); + file.delete(); + } + } + + preferences.edit().putBoolean(DOWNLOADS_CLEANUP_DONE, true).apply(); + } }