From 3ac74964526543bff37a4c0200a236dc0c2f0c1f Mon Sep 17 00:00:00 2001 From: tobiaskaminsky Date: Wed, 28 Jun 2017 13:00:09 +0200 Subject: [PATCH 1/2] - set priority of image processing to higher value - cancel all pending async tasks when leaving FDA: needs to be done by get.abort() as this is a blocking command and needs to be accessed directly --- .../datamodel/ThumbnailsCacheManager.java | 35 ++++++++++++++----- .../ui/adapter/FileListListAdapter.java | 20 +++++++++-- .../ui/fragment/OCFileListFragment.java | 8 +++++ .../ui/preview/PreviewImageFragment.java | 4 +++ 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java b/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java index 1f731e740b..6d7aa079a7 100644 --- a/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java +++ b/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java @@ -56,6 +56,7 @@ import org.apache.commons.httpclient.methods.GetMethod; import java.io.File; import java.io.InputStream; import java.lang.ref.WeakReference; +import java.util.ArrayList; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -183,13 +184,20 @@ public class ThumbnailsCacheManager { public static class ThumbnailGenerationTask extends AsyncTask { private final WeakReference mImageViewReference; private static Account mAccount; + private ArrayList mAsyncTasks = null; private Object mFile; private String mImageKey = null; private FileDataStorageManager mStorageManager; + private GetMethod getMethod; + public ThumbnailGenerationTask(ImageView imageView, FileDataStorageManager storageManager, Account account) + throws IllegalArgumentException { + this(imageView, storageManager, account, null); + } public ThumbnailGenerationTask(ImageView imageView, FileDataStorageManager storageManager, - Account account) throws IllegalArgumentException { + Account account, ArrayList asyncTasks) + throws IllegalArgumentException { // Use a WeakReference to ensure the ImageView can be garbage collected mImageViewReference = new WeakReference(imageView); if (storageManager == null) { @@ -197,6 +205,11 @@ public class ThumbnailsCacheManager { } mStorageManager = storageManager; mAccount = account; + mAsyncTasks = asyncTasks; + } + + public GetMethod getGetMethod() { + return getMethod; } public ThumbnailGenerationTask(FileDataStorageManager storageManager, Account account){ @@ -277,6 +290,10 @@ public class ThumbnailsCacheManager { } } } + + if (mAsyncTasks != null) { + mAsyncTasks.remove(this); + } } /** @@ -325,18 +342,18 @@ public class ThumbnailsCacheManager { OwnCloudVersion serverOCVersion = AccountUtils.getServerVersion(mAccount); if (mClient != null && serverOCVersion != null) { if (serverOCVersion.supportsRemoteThumbnails()) { - GetMethod get = null; + getMethod = null; try { String uri = mClient.getBaseUri() + "" + "/index.php/apps/files/api/v1/thumbnail/" + px + "/" + px + Uri.encode(file.getRemotePath(), "/"); Log_OC.d("Thumbnail", "URI: " + uri); - get = new GetMethod(uri); - get.setRequestHeader("Cookie", + getMethod = new GetMethod(uri); + getMethod.setRequestHeader("Cookie", "nc_sameSiteCookielax=true;nc_sameSiteCookiestrict=true"); - int status = mClient.executeMethod(get); + int status = mClient.executeMethod(getMethod); if (status == HttpStatus.SC_OK) { - InputStream inputStream = get.getResponseBodyAsStream(); + InputStream inputStream = getMethod.getResponseBodyAsStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px); @@ -350,13 +367,13 @@ public class ThumbnailsCacheManager { addBitmapToCache(imageKey, thumbnail); } } else { - mClient.exhaustResponse(get.getResponseBodyAsStream()); + mClient.exhaustResponse(getMethod.getResponseBodyAsStream()); } } catch (Exception e) { Log_OC.d(TAG, e.getMessage(), e); } finally { - if (get != null) { - get.releaseConnection(); + if (getMethod != null) { + getMethod.releaseConnection(); } } } else { diff --git a/src/main/java/com/owncloud/android/ui/adapter/FileListListAdapter.java b/src/main/java/com/owncloud/android/ui/adapter/FileListListAdapter.java index ef62878c90..ab0085a509 100644 --- a/src/main/java/com/owncloud/android/ui/adapter/FileListListAdapter.java +++ b/src/main/java/com/owncloud/android/ui/adapter/FileListListAdapter.java @@ -92,6 +92,8 @@ public class FileListListAdapter extends BaseAdapter { private OCFile currentDirectory; private static final String TAG = FileListListAdapter.class.getSimpleName(); + private ArrayList asyncTasks = new ArrayList<>(); + public FileListListAdapter( boolean justFolders, Context context, @@ -375,8 +377,7 @@ public class FileListListAdapter extends BaseAdapter { try { final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask( - fileIcon, mStorageManager, mAccount - ); + fileIcon, mStorageManager, mAccount, asyncTasks); if (thumbnail == null) { if (MimeTypeUtil.isVideo(file)) { @@ -392,6 +393,7 @@ public class FileListListAdapter extends BaseAdapter { task ); fileIcon.setImageDrawable(asyncDrawable); + asyncTasks.add(task); task.execute(file); } catch (IllegalArgumentException e) { Log_OC.d(TAG, "ThumbnailGenerationTask : " + e.getMessage()); @@ -711,4 +713,18 @@ public class FileListListAdapter extends BaseAdapter { return ret; } + public void cancelAllPendingTasks() { + for (ThumbnailsCacheManager.ThumbnailGenerationTask task : asyncTasks) { + if (task != null) { + task.cancel(true); + if (task.getGetMethod() != null) { + Log_OC.d(TAG, "cancel: abort get method directly"); + task.getGetMethod().abort(); + } + } + } + + asyncTasks.clear(); + } + } diff --git a/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java b/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java index 79b0dcd9be..2cea9901ef 100644 --- a/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -278,6 +278,14 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi super.onDetach(); } + @Override + public void onPause() { + super.onPause(); + mAdapter.cancelAllPendingTasks(); + } + + + /** * {@inheritDoc} */ diff --git a/src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java b/src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java index 7f29676335..bc1f53aa64 100644 --- a/src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java +++ b/src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java @@ -32,6 +32,7 @@ import android.graphics.drawable.PictureDrawable; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; +import android.os.Process; import android.support.annotation.DrawableRes; import android.support.annotation.StringRes; import android.support.v4.app.FragmentStatePagerAdapter; @@ -442,6 +443,9 @@ public class PreviewImageFragment extends FileFragment { @Override protected LoadImage doInBackground(OCFile... params) { + Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND + + android.os.Process.THREAD_PRIORITY_MORE_FAVORABLE); + Bitmap bitmapResult = null; Drawable drawableResult = null; From 2cc1487f2754efa4028bcf72f2bc473574faf29f Mon Sep 17 00:00:00 2001 From: tobiaskaminsky Date: Wed, 28 Jun 2017 14:57:57 +0200 Subject: [PATCH 2/2] adapt import for codacy --- .../android/ui/preview/PreviewImageFragment.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java b/src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java index bc1f53aa64..b7f6b59f28 100644 --- a/src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java +++ b/src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java @@ -1,18 +1,18 @@ -/** +/* * ownCloud Android client application * * @author David A. Velasco * Copyright (C) 2015 ownCloud Inc. - *

+ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, * as published by the Free Software Foundation. - *

+ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - *

+ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -443,8 +443,7 @@ public class PreviewImageFragment extends FileFragment { @Override protected LoadImage doInBackground(OCFile... params) { - Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND + - android.os.Process.THREAD_PRIORITY_MORE_FAVORABLE); + Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE); Bitmap bitmapResult = null; Drawable drawableResult = null;