| 1 |
/* |
| 2 |
* Copyright (C) 2008 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.ide.common.rendering.api.ItemResourceValue.Attribute; |
| 20 |
import com.android.layoutlib.api.IResourceValue; |
| 21 |
import com.android.layoutlib.api.IStyleResourceValue; |
| 22 |
import com.android.resources.ResourceType; |
| 23 |
|
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.Collection; |
| 26 |
import java.util.HashMap; |
| 27 |
import java.util.List; |
| 28 |
import java.util.Map; |
| 29 |
|
| 30 |
/** |
| 31 |
* Represents an android style resource with a name and a list of children {@link ResourceValue}. |
| 32 |
*/ |
| 33 |
@SuppressWarnings("deprecation") |
| 34 |
public final class StyleResourceValue extends ResourceValue implements IStyleResourceValue { |
| 35 |
|
| 36 |
private String mParentStyle = null; |
| 37 |
private final Map<Attribute, ItemResourceValue> mItems |
| 38 |
= new HashMap<Attribute, ItemResourceValue>(); |
| 39 |
|
| 40 |
public StyleResourceValue(ResourceType type, String name, boolean isFramework) { |
| 41 |
this(type, name, isFramework, null); |
| 42 |
} |
| 43 |
|
| 44 |
public StyleResourceValue(ResourceType type, String name, boolean isFramework, String libraryName) { |
| 45 |
super(type, name, isFramework, libraryName); |
| 46 |
} |
| 47 |
|
| 48 |
public StyleResourceValue(ResourceType type, String name, String parentStyle, boolean isFramework) { |
| 49 |
this(type, name, parentStyle, isFramework, null); |
| 50 |
} |
| 51 |
|
| 52 |
public StyleResourceValue(ResourceType type, String name, String parentStyle, |
| 53 |
boolean isFramework, String libraryName) { |
| 54 |
super(type, name, isFramework, libraryName); |
| 55 |
mParentStyle = parentStyle; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the parent style name or <code>null</code> if unknown. |
| 60 |
*/ |
| 61 |
@Override |
| 62 |
public String getParentStyle() { |
| 63 |
return mParentStyle; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Finds a value in the list by name |
| 68 |
* @param name the name of the resource |
| 69 |
* |
| 70 |
* @deprecated use {@link #getItem(String, boolean)} |
| 71 |
*/ |
| 72 |
@Deprecated |
| 73 |
public ResourceValue findValue(String name) { |
| 74 |
return getItem(name, isFramework()); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Finds a value in the list by name |
| 79 |
* @param name the name of the resource |
| 80 |
* |
| 81 |
* @deprecated use {@link #getItem(String, boolean)} |
| 82 |
*/ |
| 83 |
@Deprecated |
| 84 |
public ResourceValue findValue(String name, boolean isFrameworkAttr) { |
| 85 |
return getItem(name, isFrameworkAttr); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Finds a value in the list of items by name. |
| 90 |
* @param name the name of the resource |
| 91 |
* @param isFrameworkAttr is it in the framework namespace |
| 92 |
*/ |
| 93 |
public ItemResourceValue getItem(String name, boolean isFrameworkAttr) { |
| 94 |
return mItems.get(new Attribute(name, isFrameworkAttr)); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @deprecated use {@link #addItem(ItemResourceValue)} |
| 99 |
*/ |
| 100 |
@Deprecated |
| 101 |
public void addValue(ResourceValue value, boolean isFrameworkAttr, String libraryName) { |
| 102 |
addItem(ItemResourceValue.fromResourceValue(value, isFrameworkAttr, libraryName)); |
| 103 |
} |
| 104 |
|
| 105 |
public void addItem(ItemResourceValue value) { |
| 106 |
mItems.put(value.getAttribute(), value); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public void replaceWith(ResourceValue value) { |
| 111 |
assert value instanceof StyleResourceValue : |
| 112 |
value.getClass() + " is not StyleResourceValue"; |
| 113 |
super.replaceWith(value); |
| 114 |
|
| 115 |
//noinspection ConstantConditions |
| 116 |
if (value instanceof StyleResourceValue) { |
| 117 |
mItems.clear(); |
| 118 |
mItems.putAll(((StyleResourceValue) value).mItems); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Legacy method. |
| 124 |
* @deprecated use {@link #getValue()} |
| 125 |
*/ |
| 126 |
@Override |
| 127 |
@Deprecated |
| 128 |
public IResourceValue findItem(String name) { |
| 129 |
return mItems.get(new Attribute(name, true)); |
| 130 |
} |
| 131 |
|
| 132 |
/** Returns the names available in this style, intended for diagnostic purposes */ |
| 133 |
public List<String> getNames() { |
| 134 |
List<String> names = new ArrayList<String>(); |
| 135 |
for (Attribute item : mItems.keySet()) { |
| 136 |
String name = item.mName; |
| 137 |
if (item.mIsFrameworkAttr) { |
| 138 |
name = "android:" + name; |
| 139 |
} |
| 140 |
names.add(name); |
| 141 |
} |
| 142 |
return names; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Returns a list of all values defined in this Style. This doesn't return the values |
| 147 |
* inherited from the parent. |
| 148 |
*/ |
| 149 |
public Collection<ItemResourceValue> getValues() { |
| 150 |
return mItems.values(); |
| 151 |
} |
| 152 |
} |