Skip to content

Commit

Permalink
* Revert usage of 'ImageDecoder' in gif loading.
Browse files Browse the repository at this point in the history
* Update some libraries.
  • Loading branch information
mopsalarm committed Feb 2, 2022
1 parent 8598d72 commit 82ff01b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 79 deletions.
10 changes: 7 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ android.applicationVariants.all { variant ->

def output = new ByteArrayOutputStream()
exec {
commandLine("unzip", "-t", "$buildDir/outputs/apk/${variant.name}/app-${variant.name}.apk")
if (variant.name.toLowerCase() == "release") {
commandLine("unzip", "-t", "$buildDir/outputs/apk/${variant.name}/app-${variant.name}.apk")
} else {
commandLine("unzip", "-t", "$buildDir/intermediates/apk/${variant.name}/app-${variant.name}.apk")
}
standardOutput output
}

Expand Down Expand Up @@ -160,7 +164,7 @@ dependencies {
implementation 'androidx.annotation:annotation:1.3.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.exifinterface:exifinterface:1.3.3'
implementation 'androidx.preference:preference-ktx:1.1.1'
implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'androidx.drawerlayout:drawerlayout:1.1.1'
Expand All @@ -176,7 +180,7 @@ dependencies {

implementation 'androidx.core:core-ktx:1.7.0'

implementation 'androidx.fragment:fragment-ktx:1.4.0'
implementation 'androidx.fragment:fragment-ktx:1.4.1'

implementation "com.google.android.gms:play-services-ads-lite:20.5.0"
implementation 'com.google.firebase:firebase-analytics:20.0.2'
Expand Down
39 changes: 16 additions & 23 deletions app/src/main/java/com/pr0gramm/app/services/GifDrawableLoader.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.pr0gramm.app.services

import android.graphics.ImageDecoder
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.os.ParcelFileDescriptor
import androidx.core.net.toFile
import com.pr0gramm.app.Duration.Companion.millis
Expand All @@ -15,8 +12,12 @@ import com.pr0gramm.app.util.readStream
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import pl.droidsonroids.gif.GifAnimationMetaData
import pl.droidsonroids.gif.GifDrawable
import pl.droidsonroids.gif.GifDrawableBuilder
import pl.droidsonroids.gif.GifOptions
import java.io.File
Expand Down Expand Up @@ -52,21 +53,19 @@ class GifDrawableLoader(private val cache: Cache) {
*/
private fun loadGifUsingCache(uri: Uri): Flow<State> {
return flow {
logger.info { "storing data into temporary file" }
logger.info { "Getting file reference from cached file" }

cache.get(uri).let { entry ->
cache.get(uri).use { entry ->
if (entry.fractionCached < 1.0) {
// read the gif once so it is in the cache.
emitAll(readToCache(entry))
}

// the file should now be fully cached, we should be able to read it now.
entry.file?.let { file ->
// we have a cached file, use that one.
// we should now have a file there.
return@flow emit(State(createGifDrawable(file)))
}

// read the gif once so it is in the cache.
emitAll(readToCache(entry))
}

cache.get(uri).file?.let { file ->
// we should now have a file there.
return@flow emit(State(createGifDrawable(file)))
}

// crap, no file in cache, we cant do much now.
Expand Down Expand Up @@ -100,13 +99,7 @@ class GifDrawableLoader(private val cache: Cache) {
}
}

private fun createGifDrawable(file: File): Drawable {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
return ImageDecoder.decodeDrawable(ImageDecoder.createSource(file))
}

// use the android-gif-drawable library to decode the image

private fun createGifDrawable(file: File): GifDrawable {
RandomAccessFile(file, "r").use { storage ->
val meta = ParcelFileDescriptor.dup(storage.fd).use {
GifAnimationMetaData(it.fileDescriptor)
Expand All @@ -127,7 +120,7 @@ class GifDrawableLoader(private val cache: Cache) {
}

class State(
val drawable: Drawable? = null,
val drawable: GifDrawable? = null,
val progress: Float = 1.0f
)
}
4 changes: 1 addition & 3 deletions app/src/main/java/com/pr0gramm/app/ui/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ class SettingsActivity : BaseAppCompatActivity("SettingsActivity"), PreferenceFr
}
}

override fun onPreferenceStartScreen(caller: PreferenceFragmentCompat?, pref: PreferenceScreen?): Boolean {
pref ?: return false

override fun onPreferenceStartScreen(caller: PreferenceFragmentCompat, pref: PreferenceScreen): Boolean {
startActivity(Intent(this, SettingsActivity::class.java).apply {
putExtra("rootKey", pref.key)
})
Expand Down
25 changes: 14 additions & 11 deletions app/src/main/java/com/pr0gramm/app/ui/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ import com.pr0gramm.app.util.AndroidUtility
import com.pr0gramm.app.util.di.instance
import com.pr0gramm.app.util.doInBackground
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.runInterruptible

class SettingsFragment : BasePreferenceFragment("SettingsFragment"),
SharedPreferences.OnSharedPreferenceChangeListener {
SharedPreferences.OnSharedPreferenceChangeListener {

private val userService: UserService by instance()
private val bookmarkService: BookmarkService by instance()
Expand Down Expand Up @@ -92,7 +91,7 @@ class SettingsFragment : BasePreferenceFragment("SettingsFragment"),

private fun updatePreloadInfo() {
val preference: Preference = preferenceManager.findPreference("pref_pseudo_clean_preloaded")
?: return
?: return

launchUntilPause {
preloadManager.items.collect { items ->
Expand All @@ -107,8 +106,9 @@ class SettingsFragment : BasePreferenceFragment("SettingsFragment"),
}

preference.summary = getString(
R.string.pseudo_clean_preloaded_summary_with_size,
totalSize / (1024f * 1024f))
R.string.pseudo_clean_preloaded_summary_with_size,
totalSize / (1024f * 1024f)
)
}
}
}
Expand All @@ -117,20 +117,20 @@ class SettingsFragment : BasePreferenceFragment("SettingsFragment"),
super.onResume()

preferenceScreen.sharedPreferences
.registerOnSharedPreferenceChangeListener(this)
?.registerOnSharedPreferenceChangeListener(this)

updatePreloadInfo()
}

override fun onPause() {
preferenceScreen.sharedPreferences
.unregisterOnSharedPreferenceChangeListener(this)
?.unregisterOnSharedPreferenceChangeListener(this)

super.onPause()
}

override fun onPreferenceTreeClick(preference: Preference?): Boolean {
when (preference?.key) {
override fun onPreferenceTreeClick(preference: Preference): Boolean {
when (preference.key) {
"pref_pseudo_update" -> {
val activity = activity as BaseAppCompatActivity
UpdateDialogFragment.checkForUpdatesInteractive(activity)
Expand Down Expand Up @@ -197,8 +197,11 @@ class SettingsFragment : BasePreferenceFragment("SettingsFragment"),

override fun onActivityResult(requestCode: Int, resultCode: Int, resultIntent: Intent?) {
if (requestCode == RequestCodes.SELECT_DOWNLOAD_PATH && resultCode == Activity.RESULT_OK) {
if (!Storage.persistTreeUri(requireContext(), resultIntent
?: return)) {
if (!Storage.persistTreeUri(
requireContext(), resultIntent
?: return
)
) {
showInvalidDownloadDirectorySelected()
}
} else {
Expand Down
49 changes: 10 additions & 39 deletions app/src/main/java/com/pr0gramm/app/ui/views/viewer/GifMediaView.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.pr0gramm.app.ui.views.viewer

import android.annotation.SuppressLint
import android.graphics.drawable.Animatable
import android.graphics.drawable.AnimatedImageDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import androidx.core.view.isVisible
import com.pr0gramm.app.Duration
import com.pr0gramm.app.R
Expand All @@ -26,7 +22,7 @@ class GifMediaView(config: Config) : AbstractProgressMediaView(config, R.layout.
private val views = PlayerGifBinding.bind(this)

// the gif that is shown
private var gif: Drawable? = null
private var gif: GifDrawable? = null

init {
views.image.alpha = 0f
Expand All @@ -42,8 +38,7 @@ class GifMediaView(config: Config) : AbstractProgressMediaView(config, R.layout.
views.image.setImageDrawable(null)

// recycle if possible
(gif as? GifDrawable)?.recycle()

gif?.recycle()
gif = null
}
}
Expand Down Expand Up @@ -73,17 +68,15 @@ class GifMediaView(config: Config) : AbstractProgressMediaView(config, R.layout.
viewAspect = state.drawable.intrinsicWidth.toFloat() / state.drawable.intrinsicHeight

if (isPlaying) {
gifStart()
gif?.start()

views.image.animate().alpha(1f)
.withEndAction { onMediaShown() }
.setDuration(MediaView.ANIMATION_DURATION)
.start()
} else {
views.image.alpha = 1f

val animatable = state.drawable as? Animatable
animatable?.stop()
state.drawable.stop()
}
}
}
Expand All @@ -98,7 +91,8 @@ class GifMediaView(config: Config) : AbstractProgressMediaView(config, R.layout.
}

override fun currentVideoProgress(): ProgressInfo? {
val gif = gif as? GifDrawable
val gif = gif

if (isPlaying && gif != null) {
val position = gif.currentFrameIndex
val duration = gif.numberOfFrames
Expand All @@ -117,42 +111,19 @@ class GifMediaView(config: Config) : AbstractProgressMediaView(config, R.layout.
override fun playMedia() {
super.playMedia()

gif.takeIf { isPlaying }?.let { gif ->
gifStart()
gif.takeIf { isPlaying }?.let {
gif?.start()
onMediaShown()
}
}

override fun stopMedia() {
super.stopMedia()
gifStop()
gif?.stop()
}

override fun rewind() {
val gif = gif as? GifDrawable
gif?.seekTo(0)
}

private fun gifStart() {
val gif = this.gif

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (gif is AnimatedImageDrawable) {
gif.repeatCount = AnimatedImageDrawable.REPEAT_INFINITE
}
}

if (gif is Animatable) {
gif.start()
}
}

private fun gifStop() {
val gif = this.gif

if (gif is Animatable) {
gif.stop()
}
this.gif?.seekTo(0)
}
}

Expand Down

0 comments on commit 82ff01b

Please sign in to comment.