Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit d2ca3e6

Browse files
authored
Merge pull request #382 from svga/perf_sound_pool_feature
SVGASoundManager 优化以及其他 issue 修复
2 parents ffb1656 + 7616a13 commit d2ca3e6

File tree

15 files changed

+448
-218
lines changed

15 files changed

+448
-218
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# SVGAPlayer-Android CHANGELOG (2021-03-02)
22

3+
## [2.6.0](2021-08-18)
4+
5+
## Features
6+
7+
* feat(SVGASoundManager): Added SVGASoundManager to control SVGA audio, you need to manually call the init method to initialize, otherwise follow the default audio loading logic.
8+
* feat(SVGAParser): SVGAParser#decodeFromAssets and SVGAParser#decodeFromURL add a parameter, which can be null. It is used to send the audio file back to the developer. The developer can control the audio playback by himself. If this parameter is set, the audio part will not be processed internally.
9+
* feat(SVGAParser): Add aliases to the log section to facilitate developers to troubleshoot problems
10+
* feat(SVGACache): Open cache cleaning method: SVGACache#clearCache().
11+
12+
### Bug Fixes
13+
14+
* refactor(ILogger): Remove redundant api.
15+
* fix(SoundPool): Added SVGASoundManager to solve the problem that the internal SoundPool does not load audio occasionally.
16+
* fix(SVGAParser): Zip Path Traversal Vulnerability.
17+
* fix(SVGAParser): link reuse problem.
318

419
## [2.5.15](https://github.com/svga/SVGAPlayer-Android/compare/2.5.14...2.5.15) (2021-03-02)
520

app/src/main/java/com/example/ponycui_home/svgaplayer/AnimationFromAssetsActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void onClick(View view) {
3636
}
3737
});
3838
SVGALogger.INSTANCE.setLogEnabled(true);
39-
SVGASoundManager.Companion.get().init();
39+
SVGASoundManager.INSTANCE.init();
4040
loadAnimation();
4141
setContentView(animationView);
4242
}

library/src/main/java/com/opensource/svgaplayer/SVGACache.kt

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
package com.opensource.svgaplayer
22

33
import android.content.Context
4+
import com.opensource.svgaplayer.utils.log.LogUtils
45
import java.io.File
56
import java.net.URL
67
import java.security.MessageDigest
78

8-
9+
/**
10+
* SVGA 缓存管理
11+
*/
912
object SVGACache {
1013
enum class Type {
1114
DEFAULT,
1215
FILE
1316
}
1417

18+
private const val TAG = "SVGACache"
1519
private var type: Type = Type.DEFAULT
1620
private var cacheDir: String = "/"
21+
get() {
22+
if (field != "/") {
23+
val dir = File(field)
24+
if (!dir.exists()) {
25+
dir.mkdirs()
26+
}
27+
}
28+
return field
29+
}
30+
1731

1832
fun onCreate(context: Context?) {
1933
onCreate(context, Type.DEFAULT)
@@ -27,22 +41,54 @@ object SVGACache {
2741
this.type = type
2842
}
2943

30-
// fun clearCache(context: Context?){
31-
// context ?: return
32-
// cacheDir = "${context.cacheDir.absolutePath}/svga/"
33-
// File(cacheDir).takeIf { it.exists() }?.delete()
34-
// }
44+
/**
45+
* 清理缓存
46+
*/
47+
fun clearCache() {
48+
if (!isInitialized()) {
49+
LogUtils.error(TAG, "SVGACache is not init!")
50+
return
51+
}
52+
SVGAParser.threadPoolExecutor.execute {
53+
clearDir(cacheDir)
54+
LogUtils.info(TAG, "Clear svga cache done!")
55+
}
56+
}
57+
58+
// 清除目录下的所有文件
59+
internal fun clearDir(path: String) {
60+
try {
61+
val dir = File(path)
62+
dir.takeIf { it.exists() }?.let { parentDir ->
63+
parentDir.listFiles()?.forEach { file ->
64+
if (!file.exists()) {
65+
return@forEach
66+
}
67+
if (file.isDirectory) {
68+
clearDir(file.absolutePath)
69+
}
70+
file.delete()
71+
}
72+
}
73+
} catch (e: Exception) {
74+
LogUtils.error(TAG, "Clear svga cache path: $path fail", e)
75+
}
76+
}
3577

3678
fun isInitialized(): Boolean {
37-
return "/" != cacheDir&&File(cacheDir).exists()
79+
return "/" != cacheDir && File(cacheDir).exists()
3880
}
3981

4082
fun isDefaultCache(): Boolean = type == Type.DEFAULT
4183

4284
fun isCached(cacheKey: String): Boolean {
43-
return (if (isDefaultCache()) buildCacheDir(cacheKey) else buildSvgaFile(
44-
cacheKey
45-
)).exists()
85+
return if (isDefaultCache()) {
86+
buildCacheDir(cacheKey)
87+
} else {
88+
buildSvgaFile(
89+
cacheKey
90+
)
91+
}.exists()
4692
}
4793

4894
fun buildCacheKey(str: String): String {

library/src/main/java/com/opensource/svgaplayer/SVGADrawable.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ class SVGADrawable(val videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
5757
fun resume() {
5858
videoItem.audioList.forEach { audio ->
5959
audio.playID?.let {
60-
if (SVGASoundManager.get().isInit()){
61-
SVGASoundManager.get().resume(it)
60+
if (SVGASoundManager.isInit()){
61+
SVGASoundManager.resume(it)
6262
}else{
6363
videoItem.soundPool?.resume(it)
6464
}
@@ -69,8 +69,8 @@ class SVGADrawable(val videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
6969
fun pause() {
7070
videoItem.audioList.forEach { audio ->
7171
audio.playID?.let {
72-
if (SVGASoundManager.get().isInit()){
73-
SVGASoundManager.get().pause(it)
72+
if (SVGASoundManager.isInit()){
73+
SVGASoundManager.pause(it)
7474
}else{
7575
videoItem.soundPool?.pause(it)
7676
}
@@ -81,8 +81,8 @@ class SVGADrawable(val videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
8181
fun stop() {
8282
videoItem.audioList.forEach { audio ->
8383
audio.playID?.let {
84-
if (SVGASoundManager.get().isInit()){
85-
SVGASoundManager.get().stop(it)
84+
if (SVGASoundManager.isInit()){
85+
SVGASoundManager.stop(it)
8686
}else{
8787
videoItem.soundPool?.stop(it)
8888
}
@@ -93,8 +93,8 @@ class SVGADrawable(val videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
9393
fun clear() {
9494
videoItem.audioList.forEach { audio ->
9595
audio.playID?.let {
96-
if (SVGASoundManager.get().isInit()){
97-
SVGASoundManager.get().stop(it)
96+
if (SVGASoundManager.isInit()){
97+
SVGASoundManager.stop(it)
9898
}else{
9999
videoItem.soundPool?.stop(it)
100100
}

library/src/main/java/com/opensource/svgaplayer/SVGAImageView.kt

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import android.annotation.SuppressLint
66
import android.content.Context
77
import android.os.Build
88
import android.util.AttributeSet
9-
import android.util.Log
109
import android.view.MotionEvent
1110
import android.view.View
1211
import android.view.animation.LinearInterpolator
@@ -19,22 +18,32 @@ import java.net.URL
1918
/**
2019
* Created by PonyCui on 2017/3/29.
2120
*/
22-
open class SVGAImageView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
23-
: ImageView(context, attrs, defStyleAttr) {
21+
open class SVGAImageView @JvmOverloads constructor(
22+
context: Context,
23+
attrs: AttributeSet? = null,
24+
defStyleAttr: Int = 0
25+
) : ImageView(context, attrs, defStyleAttr) {
2426

2527
private val TAG = "SVGAImageView"
2628

2729
enum class FillMode {
2830
Backward,
2931
Forward,
32+
Clear,
3033
}
3134

3235
var isAnimating = false
3336
private set
3437

3538
var loops = 0
36-
var clearsAfterStop = true
37-
var clearsAfterDetached = true
39+
40+
@Deprecated(
41+
"It is recommended to use clearAfterDetached, or manually call to SVGAVideoEntity#clear." +
42+
"If you just consider cleaning up the canvas after playing, you can use FillMode#Clear.",
43+
level = DeprecationLevel.WARNING
44+
)
45+
var clearsAfterStop = false
46+
var clearsAfterDetached = false
3847
var fillMode: FillMode = FillMode.Forward
3948
var callback: SVGACallback? = null
4049

@@ -57,15 +66,21 @@ open class SVGAImageView @JvmOverloads constructor(context: Context, attrs: Attr
5766
private fun loadAttrs(attrs: AttributeSet) {
5867
val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.SVGAImageView, 0, 0)
5968
loops = typedArray.getInt(R.styleable.SVGAImageView_loopCount, 0)
60-
clearsAfterStop = typedArray.getBoolean(R.styleable.SVGAImageView_clearsAfterStop, true)
61-
clearsAfterDetached = typedArray.getBoolean(R.styleable.SVGAImageView_clearsAfterDetached, true)
69+
clearsAfterStop = typedArray.getBoolean(R.styleable.SVGAImageView_clearsAfterStop, false)
70+
clearsAfterDetached = typedArray.getBoolean(R.styleable.SVGAImageView_clearsAfterDetached, false)
6271
mAntiAlias = typedArray.getBoolean(R.styleable.SVGAImageView_antiAlias, true)
6372
mAutoPlay = typedArray.getBoolean(R.styleable.SVGAImageView_autoPlay, true)
6473
typedArray.getString(R.styleable.SVGAImageView_fillMode)?.let {
65-
if (it == "0") {
66-
fillMode = FillMode.Backward
67-
} else if (it == "1") {
68-
fillMode = FillMode.Forward
74+
when (it) {
75+
"0" -> {
76+
fillMode = FillMode.Backward
77+
}
78+
"1" -> {
79+
fillMode = FillMode.Forward
80+
}
81+
"2" -> {
82+
fillMode = FillMode.Clear
83+
}
6984
}
7085
}
7186
typedArray.getString(R.styleable.SVGAImageView_source)?.let {
@@ -178,16 +193,19 @@ open class SVGAImageView @JvmOverloads constructor(context: Context, attrs: Attr
178193
isAnimating = false
179194
stopAnimation()
180195
val drawable = getSVGADrawable()
181-
if (!clearsAfterStop && drawable != null) {
182-
if (fillMode == FillMode.Backward) {
183-
drawable.currentFrame = mStartFrame
184-
} else if (fillMode == FillMode.Forward) {
185-
drawable.currentFrame = mEndFrame
196+
if (drawable != null) {
197+
when (fillMode) {
198+
FillMode.Backward -> {
199+
drawable.currentFrame = mStartFrame
200+
}
201+
FillMode.Forward -> {
202+
drawable.currentFrame = mEndFrame
203+
}
204+
FillMode.Clear -> {
205+
drawable.cleared = true
206+
}
186207
}
187208
}
188-
if (clearsAfterStop && (animation as ValueAnimator).repeatCount <= 0) {
189-
clear()
190-
}
191209
callback?.onFinished()
192210
}
193211

@@ -274,7 +292,7 @@ open class SVGAImageView @JvmOverloads constructor(context: Context, attrs: Attr
274292

275293
override fun onDetachedFromWindow() {
276294
super.onDetachedFromWindow()
277-
stopAnimation(true)
295+
stopAnimation(clearsAfterDetached)
278296
if (clearsAfterDetached) {
279297
clear()
280298
}

0 commit comments

Comments
 (0)