Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import android.net.Uri
import android.text.method.LinkMovementMethod
import android.util.TypedValue
import android.view.inputmethod.InputMethodManager
import android.webkit.MimeTypeMap
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
Expand Down Expand Up @@ -163,8 +162,8 @@ private fun getIntentForSavedMimeType(data: Uri, type: String): Intent {
private fun getIntentForGuessedMimeType(storagePath: String, type: String, data: Uri): Intent? {
var intentForGuessedMimeType: Intent? = null
if (storagePath.lastIndexOf('.') >= 0) {
val guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1))
if (guessedMimeType != null && guessedMimeType != type) {
val guessedMimeType = MimetypeIconUtil.getBestMimeTypeByFilename(storagePath)
if (guessedMimeType != null && guessedMimeType != type && guessedMimeType != "application/octet-stream") {
intentForGuessedMimeType = Intent(Intent.ACTION_VIEW)
intentForGuessedMimeType.setDataAndType(data, guessedMimeType)
intentForGuessedMimeType.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
Expand Down Expand Up @@ -456,8 +455,13 @@ fun FragmentActivity.sendDownloadedFilesByShareSheet(ocFiles: List<OCFile>) {
}

fun Activity.openOCFile(ocFile: OCFile) {
var finalMimeType = MimetypeIconUtil.getBestMimeTypeByFilename(ocFile.fileName)
if (finalMimeType.isNullOrEmpty() || finalMimeType == "application/octet-stream") {
finalMimeType = ocFile.mimeType
}

val intentForSavedMimeType = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(getExposedFileUriForOCFile(this@openOCFile, ocFile), ocFile.mimeType)
setDataAndType(getExposedFileUriForOCFile(this@openOCFile, ocFile), finalMimeType)
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
if (ocFile.hasWritePermission) {
flags = flags or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ object ThumbnailsRequester : KoinComponent {

private val thumbnailImageLoaders = ConcurrentHashMap<String, ImageLoader>()
private val avatarImageLoaders = ConcurrentHashMap<String, ImageLoader>()
private val accountBaseUrls = ConcurrentHashMap<String, String>()

private val sharedDiskCache: DiskCache by lazy {
DiskCache.Builder()
Expand All @@ -86,11 +87,12 @@ object ThumbnailsRequester : KoinComponent {
}

fun getAvatarUri(account: Account): String {
val accountManager = AccountManager.get(appContext)
val baseUrl =
val baseUrl = accountBaseUrls.getOrPut(account.name) {
val accountManager = AccountManager.get(appContext)
accountManager.getUserData(account, eu.opencloud.android.lib.common.accounts.AccountUtils.Constants.KEY_OC_BASE_URL)
?.trimEnd('/')
.orEmpty()
}
// ?u= disambiguates the Coil cache key per account; without it two accounts
// on the same server share the same URL and collide in the shared disk/memory cache.
return "$baseUrl/graph/v1.0/me/photo/\$value?u=${account.name.hashCode().toString(16)}"
Expand All @@ -106,10 +108,12 @@ object ThumbnailsRequester : KoinComponent {
String.format(Locale.US, SPACE_SPECIAL_PREVIEW_URI, spaceSpecial.webDavUrl, 1024, 1024, spaceSpecial.eTag)

private fun getPreviewUri(remotePath: String?, etag: String?, account: Account, width: Int, height: Int): String {
val accountManager = AccountManager.get(appContext)
val baseUrl = accountManager.getUserData(account, eu.opencloud.android.lib.common.accounts.AccountUtils.Constants.KEY_OC_BASE_URL)
?.trimEnd('/')
.orEmpty()
val baseUrl = accountBaseUrls.getOrPut(account.name) {
val accountManager = AccountManager.get(appContext)
accountManager.getUserData(account, eu.opencloud.android.lib.common.accounts.AccountUtils.Constants.KEY_OC_BASE_URL)
?.trimEnd('/')
.orEmpty()
}
val path = if (remotePath?.startsWith("/") == true) remotePath else "/$remotePath"
val encodedPath = Uri.encode(path, "/")

Expand Down Expand Up @@ -156,7 +160,9 @@ object ThumbnailsRequester : KoinComponent {
// must not run on the main thread.
clientManager.getClientForCoilThumbnails(account.name)
.okHttpClient.newBuilder()
.addInterceptor(interceptor).build()
.addInterceptor(interceptor)
.addNetworkInterceptor(CoilCacheResponseInterceptor())
.build()
}
.apply { if (preferencesProvider.getBoolean("enable_logging", false)) logger(DebugLogger()) }
.memoryCache { sharedMemoryCache }
Expand All @@ -172,6 +178,7 @@ object ThumbnailsRequester : KoinComponent {
clientManager.getClientForCoilThumbnails(account.name)
.okHttpClient.newBuilder()
.addInterceptor(interceptor)
.addNetworkInterceptor(CoilCacheResponseInterceptor())
.cache(avatarHttpCache)
.build()
}
Expand Down Expand Up @@ -221,7 +228,13 @@ object ThumbnailsRequester : KoinComponent {
requestHeaders.toHeaders().forEach { requestBuilder.addHeader(it.first, it.second) }
val requestWithHeaders = requestBuilder.build()

var response = chain.proceed(requestWithHeaders)
return chain.proceed(requestWithHeaders)
}
}

private class CoilCacheResponseInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val response = chain.proceed(chain.request())

var builder = response.newBuilder()
var changed = false
Expand Down Expand Up @@ -252,6 +265,5 @@ object ThumbnailsRequester : KoinComponent {
}
return response
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private Intent getIntentForGuessedMimeType(String storagePath, String type, Uri
Intent intentForGuessedMimeType = null;

if (storagePath != null && storagePath.lastIndexOf('.') >= 0) {
String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
String guessedMimeType = eu.opencloud.android.utils.MimetypeIconUtil.getBestMimeTypeByFilename(storagePath);

if (guessedMimeType != null && !guessedMimeType.equals(type)) {
intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ class OCRemoteFileDataSource(
OCFile(
owner = owner,
remoteId = remoteId,
remotePath = remotePath,
remotePath = if (isFolder && !remotePath.endsWith(OCFile.PATH_SEPARATOR)) {
"$remotePath${OCFile.PATH_SEPARATOR}"
} else {
remotePath
},
length = if (isFolder) {
size
} else {
Expand Down
Loading