From 0ab80e4c6ac67508b71a8152aeb93da356c8376e Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Thu, 19 May 2016 20:42:01 +0200 Subject: [PATCH] delete the APK copy that Installer instances make Installer instances always copy the APK to a safe place to run the install from. That copy needs to be deleted. Until we have the whole lifecycle in InstallManagerService, we need this hack. It should be handled on the broadcast from InstallerService to say that its complete. #611 !300 --- .../org/fdroid/fdroid/CleanCacheService.java | 26 ++++++++++++++++--- .../fdroid/fdroid/installer/Installer.java | 18 ++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java b/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java index 2feb9d73d..1f817f03e 100644 --- a/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java +++ b/app/src/main/java/org/fdroid/fdroid/CleanCacheService.java @@ -7,7 +7,6 @@ import android.content.Context; import android.content.Intent; import android.os.Process; import android.os.SystemClock; -import android.util.Log; import org.apache.commons.io.FileUtils; @@ -20,7 +19,6 @@ import java.io.File; * {@link FDroidApp#onCreate()} */ public class CleanCacheService extends IntentService { - private static final String TAG = "CleanCacheService"; /** * Schedule or cancel this service to update the app index, according to the @@ -33,7 +31,6 @@ public class CleanCacheService extends IntentService { if (keepTime < interval) { interval = keepTime * 1000; } - Log.i(TAG, "schedule " + keepTime + " " + interval); Intent intent = new Intent(context, CleanCacheService.class); PendingIntent pending = PendingIntent.getService(context, 0, intent, 0); @@ -53,6 +50,29 @@ public class CleanCacheService extends IntentService { Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST); Utils.clearOldFiles(Utils.getApkCacheDir(this), Preferences.get().getKeepCacheTime()); deleteStrayIndexFiles(); + deleteOldInstallerFiles(); + } + + /** + * {@link org.fdroid.fdroid.installer.Installer} instances copy the APK into + * a safe place before installing. It doesn't clean up them reliably yet. + */ + private void deleteOldInstallerFiles() { + File filesDir = getFilesDir(); + if (filesDir == null) { + return; + } + + final File[] files = filesDir.listFiles(); + if (files == null) { + return; + } + + for (File f : files) { + if (f.getName().startsWith("install-")) { + FileUtils.deleteQuietly(f); + } + } } /** diff --git a/app/src/main/java/org/fdroid/fdroid/installer/Installer.java b/app/src/main/java/org/fdroid/fdroid/installer/Installer.java index daf27221d..8f4011190 100644 --- a/app/src/main/java/org/fdroid/fdroid/installer/Installer.java +++ b/app/src/main/java/org/fdroid/fdroid/installer/Installer.java @@ -170,7 +170,7 @@ public abstract class Installer { */ public void installPackage(File apkFile, String packageName, String urlString) throws InstallFailedException { - SanitizedFile apkToInstall; + SanitizedFile apkToInstall = null; try { Map attributes = AndroidXMLDecompress.getManifestHeaderAttributes(apkFile.getAbsolutePath()); @@ -232,6 +232,22 @@ public abstract class Installer { throw new InstallFailedException(e); } catch (ClassCastException e) { throw new InstallFailedException("F-Droid Privileged can only be updated using an activity!"); + } finally { + // 20 minutes the start of the install process, delete the file + final File apkToDelete = apkToInstall; + new Thread() { + @Override + public void run() { + android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LOWEST); + try { + Thread.sleep(1200000); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + FileUtils.deleteQuietly(apkToDelete); + } + } + }.start(); } }