fixed media viewer

This commit is contained in:
Mariotaku Lee 2017-03-02 21:59:57 +08:00
parent 95f8b7a8e0
commit 8ca63c2073
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
5 changed files with 96 additions and 74 deletions

View File

@ -173,8 +173,8 @@ dependencies {
compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
compile 'jp.wasabeef:glide-transformations:2.0.1'
compile 'com.github.mariotaku.MediaViewerLibrary:base:0.9.20'
compile 'com.github.mariotaku.MediaViewerLibrary:subsample-image-view:0.9.20'
compile 'com.github.mariotaku.MediaViewerLibrary:base:0.9.22'
compile 'com.github.mariotaku.MediaViewerLibrary:subsample-image-view:0.9.22'
compile 'com.github.mariotaku:SQLiteQB:0.9.10'
compile "com.github.mariotaku.ObjectCursor:core:$mariotaku_object_cursor_version"
compile 'com.github.mariotaku:MultiValueSwitch:0.9.7'

View File

@ -11,22 +11,23 @@ import android.webkit.MimeTypeMap
import okio.ByteString
import org.mariotaku.commons.logansquare.LoganSquareMapperFinder
import org.mariotaku.mediaviewer.library.FileCache
import org.mariotaku.restfu.RestFuUtils
import org.mariotaku.twidere.TwidereConstants
import org.mariotaku.twidere.annotation.CacheFileType
import org.mariotaku.twidere.model.CacheMetadata
import org.mariotaku.twidere.task.SaveFileTask
import org.mariotaku.twidere.util.BitmapUtils
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper
import java.io.FileInputStream
import java.io.ByteArrayInputStream
import java.io.FileNotFoundException
import java.io.IOException
import java.util.*
import javax.inject.Inject
/**
* Created by mariotaku on 16/1/1.
*/
class CacheProvider : ContentProvider() {
@Inject
internal lateinit var fileCache: FileCache
override fun onCreate(): Boolean {
@ -61,16 +62,10 @@ class CacheProvider : ContentProvider() {
}
fun getMetadata(uri: Uri): CacheMetadata? {
val file = fileCache.get(getMetadataKey(uri)) ?: return null
var `is`: FileInputStream? = null
try {
val bytes = fileCache.getExtra(getCacheKey(uri)) ?: return null
return ByteArrayInputStream(bytes).use {
val mapper = LoganSquareMapperFinder.mapperFor(CacheMetadata::class.java)
`is` = FileInputStream(file)
return mapper.parse(`is`)
} catch (e: IOException) {
return null
} finally {
RestFuUtils.closeSilently(`is`)
return mapper.parse(it)
}
}
@ -156,18 +151,6 @@ class CacheProvider : ContentProvider() {
}
fun getMetadataKey(uri: Uri): String {
if (ContentResolver.SCHEME_CONTENT != uri.scheme)
throw IllegalArgumentException(uri.toString())
if (TwidereConstants.AUTHORITY_TWIDERE_CACHE != uri.authority)
throw IllegalArgumentException(uri.toString())
return getExtraKey(ByteString.decodeBase64(uri.lastPathSegment).utf8())
}
fun getExtraKey(key: String): String {
return key + ".extra"
}
/**
* Copied from ContentResolver.java
*/

View File

@ -0,0 +1,86 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util.cache
import android.net.Uri
import com.bumptech.glide.disklrucache.DiskLruCache
import okio.ByteString
import org.mariotaku.mediaviewer.library.FileCache
import org.mariotaku.twidere.BuildConfig
import org.mariotaku.twidere.provider.CacheProvider
import java.io.File
import java.io.IOException
import java.io.InputStream
class DiskLRUFileCache(val cacheDir: File) : FileCache {
private val cache = try {
DiskLruCache.open(cacheDir, BuildConfig.VERSION_CODE, 2, 100 * 1048576)
} catch (e: IOException) {
null
}
override fun fromUri(uri: Uri): String {
return CacheProvider.getCacheKey(uri)
}
override fun toUri(key: String): Uri {
return CacheProvider.getCacheUri(key, null)
}
override fun get(key: String): File? {
return cache?.get(hash(key))?.getFile(0)
}
override fun getExtra(key: String): ByteArray? {
return cache?.get(hash(key))?.getFile(1)?.readBytes()
}
override fun remove(key: String) {
cache?.remove(hash(key))
}
override fun save(key: String, stream: InputStream, extra: ByteArray?, listener: FileCache.CopyListener?) {
val editor = cache?.edit(hash(key)) ?: return
editor.getFile(0).outputStream().use {
var bytesCopied: Int = 0
val buffer = ByteArray(8192)
var bytes = stream.read(buffer)
while (bytes >= 0) {
it.write(buffer, 0, bytes)
bytesCopied += bytes
if (listener != null && !listener.onCopied(bytesCopied)) {
editor.abort()
return
}
bytes = stream.read(buffer)
}
}
if (extra != null) {
editor.getFile(1).writeBytes(extra)
}
editor.commit()
}
private fun hash(key: String): String {
return ByteString.encodeString(key, Charsets.UTF_8).sha256().hex()
}
}

View File

@ -1,47 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util.cache
import android.net.Uri
import org.mariotaku.mediaviewer.library.FileCache
import org.mariotaku.twidere.provider.CacheProvider
import java.io.File
import java.io.InputStream
class NoOpFileCache : FileCache {
override fun fromUri(uri: Uri): String {
return CacheProvider.getCacheKey(uri)
}
override fun toUri(key: String): Uri {
return CacheProvider.getCacheUri(key, null)
}
override fun get(key: String): File? {
return null
}
override fun remove(key: String) {
}
override fun save(key: String, stream: InputStream, metadata: ByteArray?, listener: FileCache.CopyListener?) {
}
}

View File

@ -52,8 +52,8 @@ import org.mariotaku.twidere.constant.SharedPreferenceConstants.KEY_CACHE_SIZE_L
import org.mariotaku.twidere.constant.autoRefreshCompatibilityModeKey
import org.mariotaku.twidere.model.DefaultFeatures
import org.mariotaku.twidere.util.*
import org.mariotaku.twidere.util.cache.DiskLRUFileCache
import org.mariotaku.twidere.util.cache.JsonCache
import org.mariotaku.twidere.util.cache.NoOpFileCache
import org.mariotaku.twidere.util.media.TwidereMediaDownloader
import org.mariotaku.twidere.util.net.TwidereDns
import org.mariotaku.twidere.util.premium.ExtraFeaturesService
@ -320,7 +320,7 @@ class ApplicationModule(private val application: Application) {
@Provides
@Singleton
fun fileCache(): FileCache {
return NoOpFileCache()
return DiskLRUFileCache(getCacheDir("media"))
}
private fun getCacheDir(dirName: String): File {