Debian Codegraph @

layoutlib-api/src/main/java/com/android/ide/common/rendering/api/DeclareStyleableResourceValue.java

raw
1 /*
2 * Copyright (C) 2011 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 com.android.ide.common.rendering.api;
18
19 import com.android.annotations.NonNull;
20 import com.android.resources.ResourceType;
21
22 import java.util.ArrayList;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 /**
28 * A Resource value representing a declare-styleable resource.
29 *
30 * {@link #getValue()} will return null, instead use {@link #getAllAttributes()} to
31 * get the list of attributes defined in the declare-styleable.
32 */
33 public class DeclareStyleableResourceValue extends ResourceValue {
34
35 /** Used only for {@link #getAttributeValues(String)}, which is used only by old LayoutLibs. */
36 @Deprecated
37 private Map<String, AttrResourceValue> mAttrMap;
38
39 @NonNull
40 private List<AttrResourceValue> mAttrs = new ArrayList<AttrResourceValue>();
41
42 public DeclareStyleableResourceValue(@NonNull ResourceType type, @NonNull String name,
43 boolean isFramework) {
44 this(type, name, isFramework, null);
45 }
46
47 public DeclareStyleableResourceValue(@NonNull ResourceType type, @NonNull String name,
48 boolean isFramework, String libraryName) {
49 super(type, name, isFramework, libraryName);
50 assert type == ResourceType.DECLARE_STYLEABLE;
51 }
52
53 /**
54 * Return the enum/flag integer value for a given attribute.
55 *
56 * @param name the name of the attribute
57 * @return the map of (name, integer) values.
58 * @deprecated the method doesn't respect namespaces and is only present for older versions
59 * of LayoutLibs.
60 */
61 @Deprecated
62 public Map<String, Integer> getAttributeValues(String name) {
63 if (mAttrMap == null && !mAttrs.isEmpty()) {
64 // Preserve insertion order. This order affects the int[] indices for styleables.
65 mAttrMap = new LinkedHashMap<String, AttrResourceValue>(mAttrs.size());
66 for (AttrResourceValue attr : mAttrs) {
67 mAttrMap.put(attr.getName(), attr);
68 }
69 }
70 if (mAttrMap != null) {
71 AttrResourceValue attr = mAttrMap.get(name);
72 if (attr != null) {
73 return attr.getAttributeValues();
74 }
75 }
76 return null;
77 }
78
79 @NonNull
80 public List<AttrResourceValue> getAllAttributes() {
81 return mAttrs;
82 }
83
84 public void addValue(@NonNull AttrResourceValue attr) {
85 assert attr.isFramework() || !isFramework()
86 : "Can't add non-framework attributes to framework resource.";
87 mAttrs.add(attr);
88 }
89 }