Skip to content

Commit 3cc03eb

Browse files
committed
use com.squareup.wire instead of com.google.protobuf
1 parent 5de7cc5 commit 3cc03eb

15 files changed

+3093
-13266
lines changed

library/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies {
3232
compile fileTree(include: ['*.jar'], dir: 'libs')
3333
compile 'com.android.support:appcompat-v7:25.3.1'
3434
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
35-
compile 'com.google.protobuf:protobuf-java:3.4.0'
35+
compile 'com.squareup.wire:wire-runtime:2.3.0-RC1'
3636
}
3737
repositories {
3838
mavenCentral()

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.opensource.svgaplayer
33
import android.app.Activity
44
import android.content.Context
55
import android.os.Handler
6+
import com.opensource.svgaplayer.proto.MovieEntity
67

78
import org.json.JSONObject
89
import java.io.*
@@ -113,15 +114,17 @@ class SVGAParser(private val context: Context) {
113114
if (bytes.size > 4 && bytes[0].toInt() == 80 && bytes[1].toInt() == 75 && bytes[2].toInt() == 3 && bytes[3].toInt() == 4) {
114115
synchronized(sharedLock, {
115116
if (!cacheDir(cacheKey).exists()) {
116-
unzip(ByteArrayInputStream(bytes), cacheKey)
117+
try {
118+
unzip(ByteArrayInputStream(bytes), cacheKey)
119+
} catch (e: Exception) { e.printStackTrace() }
117120
}
118121
})
119122
try {
120123
val cacheDir = File(context.cacheDir.absolutePath + "/" + cacheKey + "/")
121124
File(cacheDir, "movie.binary")?.takeIf { it.isFile }?.let { binaryFile ->
122125
try {
123126
FileInputStream(binaryFile)?.let {
124-
val videoItem = SVGAVideoEntity(Svga.MovieEntity.parseFrom(it), cacheDir)
127+
val videoItem = SVGAVideoEntity(MovieEntity.ADAPTER.decode(it), cacheDir)
125128
it.close()
126129
return videoItem
127130
}
@@ -163,7 +166,7 @@ class SVGAParser(private val context: Context) {
163166
else {
164167
try {
165168
inflate(bytes)?.let {
166-
return SVGAVideoEntity(Svga.MovieEntity.parseFrom(it), File(cacheKey))
169+
return SVGAVideoEntity(MovieEntity.ADAPTER.decode(it), File(cacheKey))
167170
}
168171
} catch (e: Exception) {
169172
e.printStackTrace()
@@ -178,7 +181,7 @@ class SVGAParser(private val context: Context) {
178181
File(cacheDir, "movie.binary")?.takeIf { it.isFile }?.let { binaryFile ->
179182
try {
180183
FileInputStream(binaryFile)?.let {
181-
val videoItem = SVGAVideoEntity(Svga.MovieEntity.parseFrom(it), cacheDir)
184+
val videoItem = SVGAVideoEntity(MovieEntity.ADAPTER.decode(it), cacheDir)
182185
it.close()
183186
return videoItem
184187
}
@@ -279,6 +282,9 @@ class SVGAParser(private val context: Context) {
279282
val zipInputStream = ZipInputStream(BufferedInputStream(inputStream))
280283
while (true) {
281284
val zipItem = zipInputStream.nextEntry ?: break
285+
if (zipItem.name.contains("/")) {
286+
continue
287+
}
282288
val file = File(cacheDir, zipItem.name)
283289
val fileOutputStream = FileOutputStream(file)
284290
val buff = ByteArray(2048)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class SVGAPath(val strValue: String) {
1919
var argLast: String? = null
2020
val items = strValue.split("[,\\s+]".toRegex()).dropLastWhile(String::isEmpty).toTypedArray()
2121
for (item in items) {
22-
if (item.length < 1) {
22+
if (item.isEmpty()) {
2323
continue
2424
}
2525
val firstLetter = item.substring(0, 1)

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

+33-45
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
package com.opensource.svgaplayer
22

3-
import android.content.res.Resources
43
import android.graphics.Bitmap
54
import android.graphics.BitmapFactory
6-
import android.graphics.drawable.BitmapDrawable
7-
import android.util.Log
8-
9-
import org.json.JSONArray
10-
import org.json.JSONException
5+
import com.opensource.svgaplayer.proto.MovieEntity
116
import org.json.JSONObject
12-
137
import java.io.File
14-
import java.io.Serializable
15-
import java.util.ArrayList
16-
import java.util.HashMap
8+
import java.util.*
179

1810
/**
1911
* Created by PonyCui_Home on 16/6/18.
@@ -52,61 +44,57 @@ class SVGAVideoEntity {
5244
resetSprites(obj)
5345
}
5446

55-
constructor(obj: Svga.MovieEntity, cacheDir: File) {
47+
constructor(obj: MovieEntity, cacheDir: File) {
5648
this.cacheDir = cacheDir
57-
if (obj.hasParams()) {
58-
obj.params?.let { movieParams ->
59-
videoSize = SVGARect(0.0, 0.0, movieParams.viewBoxWidth.toDouble(), movieParams.viewBoxHeight.toDouble())
60-
FPS = movieParams.fps
61-
frames = movieParams.frames
62-
}
49+
obj.params?.let { movieParams ->
50+
videoSize = SVGARect(0.0, 0.0, (movieParams.viewBoxWidth ?: 0.0f).toDouble(), (movieParams.viewBoxHeight ?: 0.0f).toDouble())
51+
FPS = movieParams.fps ?: 20
52+
frames = movieParams.frames ?: 0
6353
}
6454
resetImages(obj)
6555
resetSprites(obj)
6656
}
6757

6858
private fun resetImages(obj: JSONObject) {
69-
obj.optJSONObject("images")?.let {
70-
val imgObjects = it
71-
it.keys().forEach {
72-
val imageKey = it
59+
obj.optJSONObject("images")?.let { imgObjects ->
60+
imgObjects.keys().forEach { imageKey ->
7361
var filePath = cacheDir.absolutePath + "/" + imgObjects[imageKey]
7462
var bitmap = if (File(filePath).exists()) BitmapFactory.decodeFile(filePath) else null
7563
if (bitmap != null) {
7664
images.put(imageKey, bitmap)
7765
}
7866
else {
79-
filePath = cacheDir.absolutePath + "/" + imageKey + ".png"
80-
bitmap = if (File(filePath).exists()) BitmapFactory.decodeFile(filePath) else null
81-
if (bitmap != null) {
82-
images.put(imageKey, bitmap)
67+
(cacheDir.absolutePath + "/" + imageKey + ".png")?.takeIf { File(it).exists() }?.let { it
68+
BitmapFactory.decodeFile(it)?.let {
69+
images.put(imageKey, it)
70+
}
8371
}
8472
}
8573
}
8674
}
8775
}
8876

89-
private fun resetImages(obj: Svga.MovieEntity) {
90-
obj.imagesMap.entries.forEach {
77+
private fun resetImages(obj: MovieEntity) {
78+
obj.images?.entries?.forEach {
9179
val imageKey = it.key
92-
if (it.value.isValidUtf8) {
93-
var filePath = cacheDir.absolutePath + "/" + it.value.toString()
94-
var bitmap = if (File(filePath).exists()) BitmapFactory.decodeFile(filePath) else null
95-
if (bitmap != null) {
96-
images.put(imageKey, bitmap)
97-
}
98-
else {
99-
filePath = cacheDir.absolutePath + "/" + imageKey + ".png"
100-
bitmap = if (File(filePath).exists()) BitmapFactory.decodeFile(filePath) else null
80+
val bitmap = BitmapFactory.decodeByteArray(it.value.toByteArray(), 0, it.value.size())
81+
if (bitmap != null) {
82+
images.put(imageKey, bitmap)
83+
}
84+
else {
85+
it.value.utf8()?.let {
86+
var filePath = cacheDir.absolutePath + "/" + it
87+
var bitmap = if (File(filePath).exists()) BitmapFactory.decodeFile(filePath) else null
10188
if (bitmap != null) {
10289
images.put(imageKey, bitmap)
10390
}
104-
}
105-
}
106-
else {
107-
val bitmap = BitmapFactory.decodeByteArray(it.value.toByteArray(), 0, it.value.size())
108-
if (bitmap != null) {
109-
images.put(imageKey, bitmap)
91+
else {
92+
(cacheDir.absolutePath + "/" + imageKey + ".png")?.takeIf { File(it).exists() }?.let { it
93+
BitmapFactory.decodeFile(it)?.let {
94+
images.put(imageKey, it)
95+
}
96+
}
97+
}
11098
}
11199
}
112100
}
@@ -124,10 +112,10 @@ class SVGAVideoEntity {
124112
sprites = mutableList.toList()
125113
}
126114

127-
private fun resetSprites(obj: Svga.MovieEntity) {
128-
sprites = obj.spritesList.map {
115+
private fun resetSprites(obj: MovieEntity) {
116+
sprites = obj.sprites?.map {
129117
return@map SVGAVideoSpriteEntity(it)
130-
}
118+
} ?: listOf()
131119
}
132120

133121
}

0 commit comments

Comments
 (0)