From 1ada24583d3c03ed3c7dd923364c71dbc8798d68 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 5 Jan 2024 15:08:22 +0100 Subject: [PATCH] Fix context memory leak Signed-off-by: alperozturk --- .../client/files/downloader/FileDownloadWorker.kt | 2 -- .../android/operations/DownloadFileOperation.java | 13 +++++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt index 508a9979e8..178761ef84 100644 --- a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt +++ b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt @@ -24,7 +24,6 @@ package com.nextcloud.client.files.downloader import android.accounts.Account import android.accounts.AccountManager import android.accounts.OnAccountsUpdateListener -import android.annotation.SuppressLint import android.app.PendingIntent import android.content.Context import androidx.core.util.component1 @@ -66,7 +65,6 @@ class FileDownloadWorker( companion object { private val TAG = FileDownloadWorker::class.java.simpleName - @SuppressLint("StaticFieldLeak") private var currentDownload: DownloadFileOperation? = null const val FILES_SEPARATOR = "," diff --git a/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java b/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java index d00ab8b6a1..1aaca76493 100644 --- a/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java @@ -43,6 +43,7 @@ import com.owncloud.android.utils.FileStorageUtils; import java.io.File; import java.io.FileOutputStream; +import java.lang.ref.WeakReference; import java.util.HashSet; import java.util.Iterator; import java.util.Set; @@ -62,7 +63,7 @@ public class DownloadFileOperation extends RemoteOperation { private String packageName; private DownloadType downloadType; - private Context context; + private final WeakReference context; private Set dataTransferListeners = new HashSet<>(); private long modificationTimestamp; private DownloadFileRemoteOperation downloadOperation; @@ -90,7 +91,7 @@ public class DownloadFileOperation extends RemoteOperation { this.behaviour = behaviour; this.activityName = activityName; this.packageName = packageName; - this.context = context; + this.context = new WeakReference<>(context); this.downloadType = downloadType; } @@ -203,13 +204,13 @@ public class DownloadFileOperation extends RemoteOperation { // decrypt file if (file.isEncrypted()) { - FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, context.getContentResolver()); + FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, context.get().getContentResolver()); OCFile parent = fileDataStorageManager.getFileByPath(file.getParentRemotePath()); DecryptedFolderMetadata metadata = EncryptionUtils.downloadFolderMetadata(parent, client, - context, + context.get(), user); if (metadata == null) { @@ -227,7 +228,7 @@ public class DownloadFileOperation extends RemoteOperation { key, iv, authenticationTag, - new ArbitraryDataProviderImpl(context), + new ArbitraryDataProviderImpl(context.get()), user); try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)) { @@ -247,7 +248,7 @@ public class DownloadFileOperation extends RemoteOperation { } else if (downloadType == DownloadType.EXPORT) { new FileExportUtils().exportFile(file.getFileName(), file.getMimeType(), - context.getContentResolver(), + context.get().getContentResolver(), null, tmpFile); if (!tmpFile.delete()) {