1 /*
<lambda>null2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.animation.graphics.vector.compat
18 
19 import android.content.res.Resources
20 import android.util.AttributeSet
21 import androidx.compose.animation.graphics.res.loadAnimatorResource
22 import androidx.compose.animation.graphics.vector.AnimatedImageVector
23 import androidx.compose.animation.graphics.vector.AnimatedVectorTarget
24 import androidx.compose.ui.graphics.vector.ImageVector
25 import androidx.compose.ui.res.vectorResource
26 import org.xmlpull.v1.XmlPullParser
27 
28 private const val TagAnimatedVector = "animated-vector"
29 private const val TagAnimatedVectorTarget = "target"
30 
31 private fun parseAnimatedVectorTarget(
32     res: Resources,
33     theme: Resources.Theme?,
34     attrs: AttributeSet
35 ): AnimatedVectorTarget {
36     return attrs.attrs(
37         res,
38         theme,
39         AndroidVectorResources.STYLEABLE_ANIMATED_VECTOR_DRAWABLE_TARGET
40     ) { a ->
41         AnimatedVectorTarget(
42             a.getString(AndroidVectorResources.STYLEABLE_ANIMATED_VECTOR_DRAWABLE_TARGET_NAME)
43                 ?: "",
44             loadAnimatorResource(
45                 theme,
46                 res,
47                 a.getResourceId(
48                     AndroidVectorResources.STYLEABLE_ANIMATED_VECTOR_DRAWABLE_TARGET_ANIMATION,
49                     0
50                 )
51             )
52         )
53     }
54 }
55 
parseAnimatedImageVectornull56 internal fun XmlPullParser.parseAnimatedImageVector(
57     res: Resources,
58     theme: Resources.Theme?,
59     attrs: AttributeSet
60 ): AnimatedImageVector {
61     return attrs.attrs(res, theme, AndroidVectorResources.STYLEABLE_ANIMATED_VECTOR_DRAWABLE) { a ->
62         val drawableId =
63             a.getResourceId(AndroidVectorResources.STYLEABLE_ANIMATED_VECTOR_DRAWABLE_DRAWABLE, 0)
64         val targets = mutableListOf<AnimatedVectorTarget>()
65         forEachChildOf(TagAnimatedVector) {
66             if (eventType == XmlPullParser.START_TAG && name == TagAnimatedVectorTarget) {
67                 targets.add(parseAnimatedVectorTarget(res, theme, attrs))
68             }
69         }
70         AnimatedImageVector(ImageVector.vectorResource(theme, res, drawableId), targets)
71     }
72 }
73