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

Commit

Permalink
feat: Add shareParser.
Browse files Browse the repository at this point in the history
errnull committed Dec 23, 2019
1 parent 3bc8915 commit 3a4d5b9
Showing 6 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@ public class AnimationFromAssetsActivity extends Activity {

int currentIndex = 0;
SVGAImageView animationView = null;
SVGAParser parser = new SVGAParser(this);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -36,13 +35,13 @@ public void onClick(View view) {
}

private void loadAnimation() {
parser.decodeFromAssets(this.randomSample(), new SVGAParser.ParseCompletion() {
@Override
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
animationView.setVideoItem(videoItem);
animationView.stepToFrame(0, true);
}
@Override
SVGAParser.Companion.shareParser().decodeFromAssets(this.randomSample(), new SVGAParser.ParseCompletion() {
@Override
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
animationView.setVideoItem(videoItem);
animationView.stepToFrame(0, true);
}
@Override
public void onError() {

}
Original file line number Diff line number Diff line change
@@ -23,7 +23,6 @@
public class AnimationFromClickActivity extends Activity {

SVGAImageView animationView = null;
SVGAParser parser = new SVGAParser(this);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -41,7 +40,7 @@ public void onClick(@NotNull String clickKey) {
}

private void loadAnimation() {
parser.decodeFromAssets("test2.svga",new SVGAParser.ParseCompletion() {
SVGAParser.Companion.shareParser().decodeFromAssets("test2.svga",new SVGAParser.ParseCompletion() {
@Override
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
SVGADynamicEntity dynamicEntity = new SVGADynamicEntity();
Original file line number Diff line number Diff line change
@@ -17,7 +17,6 @@
public class AnimationFromNetworkActivity extends Activity {

SVGAImageView animationView = null;
SVGAParser parser = new SVGAParser(this);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -30,7 +29,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

private void loadAnimation() {
try { // new URL needs try catch.
parser.decodeFromURL(new URL("https://github.com/yyued/SVGA-Samples/blob/master/posche.svga?raw=true"), new SVGAParser.ParseCompletion() {
SVGAParser.Companion.shareParser().decodeFromURL(new URL("https://github.com/yyued/SVGA-Samples/blob/master/posche.svga?raw=true"), new SVGAParser.ParseCompletion() {
@Override
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
animationView.setVideoItem(videoItem);
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setupData();
this.setupListView();
this.setupSVGAParser();
setContentView(listView);
}

@@ -155,6 +156,9 @@ public boolean isEmpty() {
});
this.listView.setBackgroundColor(Color.WHITE);
}
void setupSVGAParser() {
SVGAParser.Companion.shareParser().init(this);
}


}
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ import android.animation.Animator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.AttributeSet
import android.util.Log
@@ -13,7 +12,6 @@ import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import com.opensource.svgaplayer.utils.SVGARange
import java.lang.ref.WeakReference
import java.net.URL

/**
35 changes: 26 additions & 9 deletions library/src/main/java/com/opensource/svgaplayer/SVGAParser.kt
Original file line number Diff line number Diff line change
@@ -11,9 +11,7 @@ import java.net.HttpURLConnection
import java.net.URL
import java.security.MessageDigest
import java.util.concurrent.Executors
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import java.util.zip.Inflater
import java.util.zip.ZipInputStream

@@ -23,13 +21,12 @@ import java.util.zip.ZipInputStream

private var fileLock: Int = 0

class SVGAParser(private val context: Context) {
class SVGAParser(private var mContext: Context?) {

interface ParseCompletion {

fun onComplete(videoItem: SVGAVideoEntity)
fun onError()

}

open class FileDownloader {
@@ -91,11 +88,22 @@ class SVGAParser(private val context: Context) {
fun setThreadPoolExecutor(executor: ThreadPoolExecutor) {
threadPoolExecutor = executor
}
private var mShareParser = SVGAParser(null)
fun shareParser(): SVGAParser {
return mShareParser
}
}

fun init(context: Context) {
mContext = context
}

fun decodeFromAssets(name: String, callback: ParseCompletion?) {
if (mContext == null) {
Log.e("SVGAParser", "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
}
try {
context.assets.open(name)?.let {
mContext?.assets?.open(name)?.let {
this.decodeFromInputStream(it, buildCacheKey("file:///assets/$name"), callback, true)
}
}
@@ -176,14 +184,20 @@ class SVGAParser(private val context: Context) {
}

private fun invokeCompleteCallback(videoItem: SVGAVideoEntity, callback: ParseCompletion?) {
Handler(context.mainLooper).post {
if (mContext == null) {
Log.e("SVGAParser", "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
}
Handler(mContext?.mainLooper).post {
callback?.onComplete(videoItem)
}
}

private fun invokeErrorCallback(e: java.lang.Exception, callback: ParseCompletion?) {
e.printStackTrace()
Handler(context.mainLooper).post {
if (mContext == null) {
Log.e("SVGAParser", "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
}
Handler(mContext?.mainLooper).post {
callback?.onError()
}
}
@@ -193,8 +207,11 @@ class SVGAParser(private val context: Context) {
}

private fun decodeFromCacheKey(cacheKey: String, callback: ParseCompletion?) {
if (mContext == null) {
Log.e("SVGAParser", "在配置 SVGAParser context 前, 无法解析 SVGA 文件。")
}
try {
val cacheDir = File(context.cacheDir.absolutePath + "/" + cacheKey + "/")
val cacheDir = File(mContext?.cacheDir?.absolutePath + "/" + cacheKey + "/")
File(cacheDir, "movie.binary").takeIf { it.isFile }?.let { binaryFile ->
try {
FileInputStream(binaryFile).use {
@@ -249,7 +266,7 @@ class SVGAParser(private val context: Context) {

private fun buildCacheKey(url: URL): String = buildCacheKey(url.toString())

private fun buildCacheDir(cacheKey: String): File = File(context.cacheDir.absolutePath + "/" + cacheKey + "/")
private fun buildCacheDir(cacheKey: String): File = File(mContext?.cacheDir?.absolutePath + "/" + cacheKey + "/")

private fun readAsBytes(inputStream: InputStream): ByteArray? {
ByteArrayOutputStream().use { byteArrayOutputStream ->

0 comments on commit 3a4d5b9

Please sign in to comment.