Skip to content

Commit

Permalink
Merge pull request #3017 from owncloud/feature/use_public_preview_api
Browse files Browse the repository at this point in the history
Use public preview api to retrieve thumbnails
  • Loading branch information
abelgardep authored Nov 25, 2020
2 parents 3ae86e2 + 79c3d73 commit 43fcae1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@
import androidx.core.content.ContextCompat;
import com.owncloud.android.MainApp;
import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.lib.common.OwnCloudAccount;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.SingleSessionManager;
import com.owncloud.android.lib.common.http.HttpConstants;
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod;
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
import com.owncloud.android.ui.adapter.DiskLruImageCache;
import com.owncloud.android.utils.BitmapUtils;
import timber.log.Timber;
Expand All @@ -52,6 +50,7 @@
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.Locale;

/**
* Manager for concurrent access to thumbnails cache.
Expand All @@ -69,6 +68,8 @@ public class ThumbnailsCacheManager {
private static final int mCompressQuality = 70;
private static OwnCloudClient mClient = null;

private static final String PREVIEW_URI = "%s/remote.php/dav/files/%s%s?x=%d&y=%d&c=%s&preview=1";

public static Bitmap mDefaultImg =
BitmapFactory.decodeResource(
MainApp.Companion.getAppContext().getResources(),
Expand Down Expand Up @@ -249,6 +250,17 @@ private int getThumbnailDimension() {
return Math.round(r.getDimension(R.dimen.file_icon_size_grid));
}

private String getPreviewUrl(OCFile ocFile, Account account) {
return String.format(Locale.ROOT,
PREVIEW_URI,
mClient.getBaseUri(),
account.name.split("@")[0],
Uri.encode(ocFile.getRemotePath(), "/"),
getThumbnailDimension(),
getThumbnailDimension(),
ocFile.getEtag());
}

private Bitmap doOCFileInBackground() {
OCFile file = (OCFile) mFile;

Expand Down Expand Up @@ -281,13 +293,10 @@ private Bitmap doOCFileInBackground() {

} else {
// Download thumbnail from server
OwnCloudVersion serverOCVersion = AccountUtils.getServerVersion(mAccount);
if (mClient != null && serverOCVersion != null) {
if (mClient != null) {
GetMethod get;
try {
String uri = mClient.getBaseUri() + "" +
"/index.php/apps/files/api/v1/thumbnail/" +
px + "/" + px + Uri.encode(file.getRemotePath(), "/");
String uri = getPreviewUrl(file, mAccount);
Timber.d("URI: %s", uri);
get = new GetMethod(new URL(uri));
int status = mClient.executeHttpMethod(get);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ private void mergeRemoteFolder(ArrayList<RemoteFile> remoteFolderAndFiles)
// remote eTag will not be set unless file CONTENTS are synchronized
updatedLocalFile.setEtag(localFile.getEtag());
if (!updatedLocalFile.isFolder() &&
remoteFile.isImage() &&
remoteFile.getModificationTimestamp() != localFile.getModificationTimestamp()) {
updatedLocalFile.setNeedsUpdateThumbnail(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
private Account mAccount;
private ComponentsGetter mTransferServiceGetter;

private enum ViewType {LIST_ITEM, GRID_IMAGE, GRID_ITEM}

public FileListListAdapter(
boolean justFolders,
boolean onlyAvailableOffline,
Expand Down Expand Up @@ -283,7 +281,9 @@ public View getView(int position, View convertView, ViewGroup parent) {
file.isSharedWithMe() || file.isSharedWithSharee(),
file.isSharedViaLink()));
} else {
if (file.isImage() && file.getRemoteId() != null) {
// Set file icon depending on its mimetype. Ask for thumbnail later.
fileIcon.setImageResource(MimetypeIconUtil.getFileTypeIconId(file.getMimetype(), file.getFileName()));
if (file.getRemoteId() != null) {
// Thumbnail in Cache?
Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(file.getRemoteId());
if (thumbnail != null && !file.needsUpdateThumbnail()) {
Expand All @@ -295,16 +295,16 @@ public View getView(int position, View convertView, ViewGroup parent) {
new ThumbnailsCacheManager.ThumbnailGenerationTask(
fileIcon, mStorageManager, mAccount
);
if (thumbnail == null) {
thumbnail = ThumbnailsCacheManager.mDefaultImg;
}
final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable =
new ThumbnailsCacheManager.AsyncThumbnailDrawable(
mContext.getResources(),
thumbnail,
task
);
fileIcon.setImageDrawable(asyncDrawable);
// If drawable is not visible, do not update it.
if (asyncDrawable.getMinimumHeight() > 0 && asyncDrawable.getMinimumWidth() > 0) {
fileIcon.setImageDrawable(asyncDrawable);
}
task.execute(file);
}
}
Expand All @@ -314,9 +314,6 @@ public View getView(int position, View convertView, ViewGroup parent) {
.getColor(R.color.background_color));
}

} else {
fileIcon.setImageResource(MimetypeIconUtil.getFileTypeIconId(file.getMimetype(),
file.getFileName()));
}

}
Expand Down Expand Up @@ -501,4 +498,6 @@ public void clearFilterBySearch() {
mFiles = (Vector<OCFile>) mImmutableFilesList.clone();
notifyDataSetChanged();
}

private enum ViewType {LIST_ITEM, GRID_IMAGE, GRID_ITEM}
}

0 comments on commit 43fcae1

Please sign in to comment.