Skip to content

Commit

Permalink
Fix context memory leak
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Jan 5, 2024
1 parent ac1cd17 commit c318b63
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = ","
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

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;
Expand All @@ -62,7 +63,7 @@ public class DownloadFileOperation extends RemoteOperation {
private String packageName;
private DownloadType downloadType;

private Context context;
private final WeakReference<Context> context;
private Set<OnDatatransferProgressListener> dataTransferListeners = new HashSet<>();
private long modificationTimestamp;
private DownloadFileRemoteOperation downloadOperation;
Expand Down Expand Up @@ -90,7 +91,7 @@ public DownloadFileOperation(User user,
this.behaviour = behaviour;
this.activityName = activityName;
this.packageName = packageName;
this.context = context;
this.context = new WeakReference<>(context);
this.downloadType = downloadType;
}

Expand Down Expand Up @@ -203,13 +204,13 @@ protected RemoteOperationResult run(OwnCloudClient client) {

// 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) {
Expand All @@ -227,7 +228,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
key,
iv,
authenticationTag,
new ArbitraryDataProviderImpl(context),
new ArbitraryDataProviderImpl(context.get()),
user);

try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)) {
Expand All @@ -247,7 +248,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
} else if (downloadType == DownloadType.EXPORT) {
new FileExportUtils().exportFile(file.getFileName(),
file.getMimeType(),
context.getContentResolver(),
context.get().getContentResolver(),
null,
tmpFile);
if (!tmpFile.delete()) {
Expand Down

0 comments on commit c318b63

Please sign in to comment.