Merge pull request #3453 from nextcloud/DocumentProviderThumbnailsNew

Document provider: show thumbnail, if available, else fetch from server
This commit is contained in:
Andy Scherzinger 2019-01-18 13:35:12 +01:00 committed by GitHub
commit 440eb53d28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 80 additions and 18 deletions

View file

@ -193,6 +193,10 @@ public final class ThumbnailsCacheManager {
}
}
public static boolean containsBitmap(String key) {
return mThumbnailCache.containsKey(key);
}
public static Bitmap getBitmapFromDiskCache(String key) {
synchronized (mThumbnailsDiskCacheLock) {
// Wait while disk cache is started from background thread
@ -1166,4 +1170,53 @@ public final class ThumbnailsCacheManager {
addThumbnailToCache(imageKey, bitmap, file.getStoragePath(), pxW, pxH);
}
}
public static void generateThumbnailFromOCFile(OCFile file) {
int pxW;
int pxH;
pxW = pxH = getThumbnailDimension();
String imageKey = PREFIX_THUMBNAIL + String.valueOf(file.getRemoteId());
GetMethod getMethod = null;
try {
Bitmap thumbnail = null;
String uri = mClient.getBaseUri() + "/index.php/apps/files/api/v1/thumbnail/" +
pxW + "/" + pxH + Uri.encode(file.getRemotePath(), "/");
Log_OC.d(TAG, "generate thumbnail: " + file.getFileName() + " URI: " + uri);
getMethod = new GetMethod(uri);
getMethod.setRequestHeader("Cookie", "nc_sameSiteCookielax=true;nc_sameSiteCookiestrict=true");
getMethod.setRequestHeader(RemoteOperation.OCS_API_HEADER,
RemoteOperation.OCS_API_HEADER_VALUE);
int status = mClient.executeMethod(getMethod);
if (status == HttpStatus.SC_OK) {
InputStream inputStream = getMethod.getResponseBodyAsStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
thumbnail = ThumbnailUtils.extractThumbnail(bitmap, pxW, pxH);
} else {
mClient.exhaustResponse(getMethod.getResponseBodyAsStream());
}
// Add thumbnail to cache
if (thumbnail != null) {
// Handle PNG
if (file.getMimeType().equalsIgnoreCase(PNG_MIMETYPE)) {
thumbnail = handlePNG(thumbnail, pxW, pxH);
}
Log_OC.d(TAG, "add thumbnail to cache: " + file.getFileName());
addBitmapToCache(imageKey, thumbnail);
}
} catch (Exception e) {
Log_OC.d(TAG, e.getMessage(), e);
} finally {
if (getMethod != null) {
getMethod.releaseConnection();
}
}
}
}

View file

@ -74,10 +74,7 @@ public class AccountRemovalJob extends Job implements AccountManagerCallback<Boo
// disable contact backup job
ContactsPreferenceActivity.cancelContactBackupJobForAccount(context, account);
if (am != null) {
am.removeAccount(account, this, null);
}
am.removeAccount(account, this, null);
FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());

View file

@ -75,13 +75,13 @@ public class ContactsImportJob extends Job {
cursor = getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
TreeMap<VCard, Long> ownContactList = new TreeMap<>(new ContactListFragment.VCardComparator());
TreeMap<VCard, Long> ownContactMap = new TreeMap<>(new ContactListFragment.VCardComparator());
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
VCard vCard = getContactFromCursor(cursor);
if (vCard != null) {
ownContactList.put(vCard, cursor.getLong(cursor.getColumnIndex("NAME_RAW_CONTACT_ID")));
ownContactMap.put(vCard, cursor.getLong(cursor.getColumnIndex("NAME_RAW_CONTACT_ID")));
}
cursor.moveToNext();
}
@ -91,10 +91,10 @@ public class ContactsImportJob extends Job {
for (int checkedItem : intArray) {
VCard vCard = vCards.get(checkedItem);
if (ContactListFragment.getDisplayName(vCard).length() != 0) {
if (!ownContactList.containsKey(vCard)) {
if (!ownContactMap.containsKey(vCard)) {
operations.insertContact(vCard);
} else {
operations.updateContact(vCard, ownContactList.get(vCard));
operations.updateContact(vCard, ownContactMap.get(vCard));
}
} else {
operations.insertContact(vCard); //Insert All the contacts without name

View file

@ -86,7 +86,7 @@ public class DiskLruImageCacheFileProvider extends ContentProvider {
//Convert bitmap to byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bos);
thumbnail.compress(Bitmap.CompressFormat.PNG, 90, bos);
byte[] bitmapData = bos.toByteArray();
//write the bytes in file

View file

@ -32,6 +32,7 @@ import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.graphics.Point;
import android.net.Uri;
import android.os.Build;
import android.os.CancellationSignal;
import android.os.Handler;
@ -49,6 +50,7 @@ import com.owncloud.android.R;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.datamodel.ThumbnailsCacheManager;
import com.owncloud.android.files.services.FileDownloader;
import com.owncloud.android.files.services.FileUploader;
import com.owncloud.android.lib.common.OwnCloudAccount;
@ -67,6 +69,7 @@ import com.owncloud.android.operations.UploadFileOperation;
import com.owncloud.android.ui.activity.ConflictsResolveActivity;
import com.owncloud.android.ui.activity.Preferences;
import com.owncloud.android.utils.FileStorageUtils;
import com.owncloud.android.utils.UriUtils;
import org.nextcloud.providers.cursors.FileCursor;
import org.nextcloud.providers.cursors.RootCursor;
@ -304,12 +307,22 @@ public class DocumentsStorageProvider extends DocumentsProvider {
throw new FileNotFoundException("File with id " + documentId + " not found!");
}
File realFile = new File(file.getStoragePath());
Context context = getContext();
return new AssetFileDescriptor(
ParcelFileDescriptor.open(realFile, ParcelFileDescriptor.MODE_READ_ONLY),
0,
AssetFileDescriptor.UNKNOWN_LENGTH);
if (context == null) {
throw new FileNotFoundException("Context may not be null!");
}
boolean exists = ThumbnailsCacheManager.containsBitmap(ThumbnailsCacheManager.PREFIX_THUMBNAIL
+ file.getRemoteId());
if (!exists) {
ThumbnailsCacheManager.generateThumbnailFromOCFile(file);
}
Uri uri = Uri.parse(UriUtils.URI_CONTENT_SCHEME + context.getResources().getString(
R.string.image_cache_provider_authority) + file.getRemotePath());
return context.getContentResolver().openAssetFileDescriptor(uri, "r");
}
@Override

View file

@ -108,7 +108,7 @@ public class DiskLruImageCache {
Bitmap bitmap = null;
DiskLruCache.Snapshot snapshot = null;
String validKey = convertToValidKey(key);
try {
snapshot = mDiskCache.get(validKey);
if (snapshot == null) {
@ -189,4 +189,4 @@ public class DiskLruImageCache {
Log_OC.d(TAG, e.getMessage(), e);
}
}
}
}

View file

@ -48,10 +48,9 @@ public class FileCursor extends MatrixCursor {
final int iconRes = MimeTypeUtil.getFileTypeIconId(file.getMimeType(), file.getFileName());
final String mimeType = file.isFolder() ? Document.MIME_TYPE_DIR : file.getMimeType();
final String imagePath = MimeTypeUtil.isImage(file) && file.isDown() ? file.getStoragePath() : null;
int flags = Document.FLAG_SUPPORTS_DELETE |
Document.FLAG_SUPPORTS_WRITE |
(imagePath != null ? Document.FLAG_SUPPORTS_THUMBNAIL : 0);
(MimeTypeUtil.isImage(file) ? Document.FLAG_SUPPORTS_THUMBNAIL : 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
flags = Document.FLAG_SUPPORTS_COPY | Document.FLAG_SUPPORTS_MOVE | Document.FLAG_SUPPORTS_REMOVE | flags;