| 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.resources.ResourceType; |
| 20 |
|
| 21 |
import java.util.List; |
| 22 |
|
| 23 |
/** |
| 24 |
* A class containing all the resources needed to do a rendering. |
| 25 |
* <p> |
| 26 |
* This contains both the project specific resources and the framework resources, and provide |
| 27 |
* convenience methods to resolve resource and theme references. |
| 28 |
*/ |
| 29 |
public class RenderResources { |
| 30 |
|
| 31 |
public static final String REFERENCE_NULL = "@null"; |
| 32 |
public static final String REFERENCE_EMPTY = "@empty"; |
| 33 |
public static final String REFERENCE_UNDEFINED = "@undefined"; |
| 34 |
|
| 35 |
public static class FrameworkResourceIdProvider { |
| 36 |
public Integer getId(ResourceType resType, String resName) { |
| 37 |
return null; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public void setFrameworkResourceIdProvider(FrameworkResourceIdProvider provider) { |
| 42 |
} |
| 43 |
|
| 44 |
public void setLogger(LayoutLog logger) { |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Returns the {@link StyleResourceValue} representing the current theme. |
| 49 |
* @return the theme or null if there is no current theme. |
| 50 |
* @deprecated Use {@link #getDefaultTheme()} or {@link #getAllThemes()} |
| 51 |
*/ |
| 52 |
@Deprecated |
| 53 |
public StyleResourceValue getCurrentTheme() { |
| 54 |
// Default theme is same as the current theme was on older versions of the API. |
| 55 |
// With the introduction of applyStyle() "current" theme makes little sense. |
| 56 |
// Hence, simply return defaultTheme. |
| 57 |
return getDefaultTheme(); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the {@link StyleResourceValue} representing the default theme. |
| 62 |
*/ |
| 63 |
public StyleResourceValue getDefaultTheme() { |
| 64 |
return null; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Use this theme to resolve resources. |
| 69 |
* <p> |
| 70 |
* Remember to call {@link #clearStyles()} to clear the applied styles, so the default theme |
| 71 |
* may be restored. |
| 72 |
* |
| 73 |
* @param theme The style to use for resource resolution in addition to the the default theme |
| 74 |
* and the styles applied earlier. If null, the operation is a no-op. |
| 75 |
* @param useAsPrimary If true, the {@code theme} is used first to resolve attributes. If |
| 76 |
* false, the theme is used if the resource cannot be resolved using the default theme and |
| 77 |
* all the themes that have been applied prior to this call. |
| 78 |
*/ |
| 79 |
public void applyStyle(StyleResourceValue theme, boolean useAsPrimary) { |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Clear all the themes applied with {@link #applyStyle(StyleResourceValue, boolean)} |
| 84 |
*/ |
| 85 |
public void clearStyles() { |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns a list of {@link StyleResourceValue} containing a list of themes to be used for |
| 90 |
* resolving resources. The order of the themes in the list specifies the order in which they |
| 91 |
* should be used to resolve resources. |
| 92 |
*/ |
| 93 |
public List<StyleResourceValue> getAllThemes() { |
| 94 |
return null; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns a theme by its name. |
| 99 |
* |
| 100 |
* @param name the name of the theme |
| 101 |
* @param frameworkTheme whether the theme is a framework theme. |
| 102 |
* @return the theme or null if there's no match |
| 103 |
*/ |
| 104 |
public StyleResourceValue getTheme(String name, boolean frameworkTheme) { |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Returns whether a theme is a parent of a given theme. |
| 110 |
* @param parentTheme the parent theme |
| 111 |
* @param childTheme the child theme. |
| 112 |
* @return true if the parent theme is indeed a parent theme of the child theme. |
| 113 |
*/ |
| 114 |
public boolean themeIsParentOf(StyleResourceValue parentTheme, StyleResourceValue childTheme) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns a framework resource by type and name. The returned resource is resolved. |
| 120 |
* @param resourceType the type of the resource |
| 121 |
* @param resourceName the name of the resource |
| 122 |
*/ |
| 123 |
public ResourceValue getFrameworkResource(ResourceType resourceType, String resourceName) { |
| 124 |
return null; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns a project resource by type and name. The returned resource is resolved. |
| 129 |
* @param resourceType the type of the resource |
| 130 |
* @param resourceName the name of the resource |
| 131 |
*/ |
| 132 |
public ResourceValue getProjectResource(ResourceType resourceType, String resourceName) { |
| 133 |
return null; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Returns the {@link ResourceValue} matching a given name in the all themes returned by |
| 138 |
* {@link #getAllThemes()}. If the item is not directly available in the a theme, its parent |
| 139 |
* theme is used before checking the next theme from the list. |
| 140 |
* |
| 141 |
* @param itemName the name of the item to search for. |
| 142 |
* @return the {@link ResourceValue} object or <code>null</code> |
| 143 |
* |
| 144 |
* @deprecated Use {@link #findItemInTheme(String, boolean)} |
| 145 |
*/ |
| 146 |
@Deprecated |
| 147 |
public ResourceValue findItemInTheme(String itemName) { |
| 148 |
List<StyleResourceValue> allThemes = getAllThemes(); |
| 149 |
if (allThemes == null) { |
| 150 |
return null; |
| 151 |
} |
| 152 |
for (StyleResourceValue theme : allThemes) { |
| 153 |
//noinspection deprecation |
| 154 |
ResourceValue value = findItemInStyle(theme, itemName); |
| 155 |
if (value != null) { |
| 156 |
return value; |
| 157 |
} |
| 158 |
} |
| 159 |
return null; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Returns the {@link ResourceValue} matching a given name in the all themes returned by |
| 164 |
* {@link #getAllThemes()}. If the item is not directly available in the a theme, its parent |
| 165 |
* theme is used before checking the next theme from the list. |
| 166 |
* |
| 167 |
* @param attrName the name of the attribute to search for. |
| 168 |
* @param isFrameworkAttr whether the attribute is a framework attribute |
| 169 |
* @return the {@link ResourceValue} object or <code>null</code> |
| 170 |
*/ |
| 171 |
public ResourceValue findItemInTheme(String attrName, boolean isFrameworkAttr) { |
| 172 |
List<StyleResourceValue> allThemes = getAllThemes(); |
| 173 |
if (allThemes == null) { |
| 174 |
return null; |
| 175 |
} |
| 176 |
for (StyleResourceValue theme : allThemes) { |
| 177 |
ResourceValue value = findItemInStyle(theme, attrName, isFrameworkAttr); |
| 178 |
if (value != null) { |
| 179 |
return value; |
| 180 |
} |
| 181 |
} |
| 182 |
return null; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Returns the {@link ResourceValue} matching a given name in a given style. If the |
| 187 |
* item is not directly available in the style, the method looks in its parent style. |
| 188 |
* |
| 189 |
* This version of doesn't support providing the namespace of the attribute so it'll search |
| 190 |
* in both the project's namespace and then in the android namespace. |
| 191 |
* |
| 192 |
* @param style the style to search in |
| 193 |
* @param attrName the name of the attribute to search for. |
| 194 |
* @return the {@link ResourceValue} object or <code>null</code> |
| 195 |
* |
| 196 |
* @deprecated Use {@link #findItemInStyle(StyleResourceValue, String, boolean)} since this |
| 197 |
* method doesn't know the item namespace. |
| 198 |
*/ |
| 199 |
@Deprecated |
| 200 |
public ResourceValue findItemInStyle(StyleResourceValue style, String attrName) { |
| 201 |
return null; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Returns the {@link ResourceValue} matching a given attribute in a given style. If the |
| 206 |
* item is not directly available in the style, the method looks in its parent style. |
| 207 |
* |
| 208 |
* @param style the style to search in |
| 209 |
* @param attrName the name of the attribute to search for. |
| 210 |
* @param isFrameworkAttr whether the attribute is a framework attribute |
| 211 |
* @return the {@link ResourceValue} object or <code>null</code> |
| 212 |
*/ |
| 213 |
public ResourceValue findItemInStyle(StyleResourceValue style, String attrName, |
| 214 |
boolean isFrameworkAttr) { |
| 215 |
return null; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Searches for, and returns a {@link ResourceValue} by its reference. |
| 220 |
* <p> |
| 221 |
* The reference format can be: |
| 222 |
* <pre>@resType/resName</pre> |
| 223 |
* <pre>@android:resType/resName</pre> |
| 224 |
* <pre>@resType/android:resName</pre> |
| 225 |
* <pre>?resType/resName</pre> |
| 226 |
* <pre>?android:resType/resName</pre> |
| 227 |
* <pre>?resType/android:resName</pre> |
| 228 |
* Any other string format will return <code>null</code>. |
| 229 |
* <p> |
| 230 |
* The actual format of a reference is <pre>@[namespace:]resType/resName</pre> but this method |
| 231 |
* only support the android namespace. |
| 232 |
* |
| 233 |
* @param reference the resource reference to search for. |
| 234 |
* @param forceFrameworkOnly if true all references are considered to be toward framework |
| 235 |
* resource even if the reference does not include the android: prefix. |
| 236 |
* @return a {@link ResourceValue} or <code>null</code>. |
| 237 |
*/ |
| 238 |
public ResourceValue findResValue(String reference, boolean forceFrameworkOnly) { |
| 239 |
return null; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Resolves the value of a resource, if the value references a theme or resource value. |
| 244 |
* <p> |
| 245 |
* This method ensures that it returns a {@link ResourceValue} object that does not |
| 246 |
* reference another resource. |
| 247 |
* If the resource cannot be resolved, it returns <code>null</code>. |
| 248 |
* <p> |
| 249 |
* If a value that does not need to be resolved is given, the method will return a new |
| 250 |
* instance of {@link ResourceValue} that contains the input value. |
| 251 |
* |
| 252 |
* @param type the type of the resource |
| 253 |
* @param name the name of the attribute containing this value. |
| 254 |
* @param value the resource value, or reference to resolve |
| 255 |
* @param isFrameworkValue whether the value is a framework value. |
| 256 |
* |
| 257 |
* @return the resolved resource value or <code>null</code> if it failed to resolve it. |
| 258 |
*/ |
| 259 |
public ResourceValue resolveValue(ResourceType type, String name, String value, |
| 260 |
boolean isFrameworkValue) { |
| 261 |
return null; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Returns the {@link ResourceValue} referenced by the value of <var>value</var>. |
| 266 |
* <p> |
| 267 |
* This method ensures that it returns a {@link ResourceValue} object that does not |
| 268 |
* reference another resource. |
| 269 |
* If the resource cannot be resolved, it returns <code>null</code>. |
| 270 |
* <p> |
| 271 |
* If a value that does not need to be resolved is given, the method will return the input |
| 272 |
* value. |
| 273 |
* |
| 274 |
* @param value the value containing the reference to resolve. |
| 275 |
* @return a {@link ResourceValue} object or <code>null</code> |
| 276 |
*/ |
| 277 |
public ResourceValue resolveResValue(ResourceValue value) { |
| 278 |
return null; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Returns the parent style of the given style, if any |
| 283 |
* @param style the style to look up |
| 284 |
* @return the parent style, or null |
| 285 |
*/ |
| 286 |
public StyleResourceValue getParent(StyleResourceValue style) { |
| 287 |
return null; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Returns the style matching the given name. The name should not contain any namespace prefix. |
| 292 |
* @param styleName Name of the style. For example, "Widget.ListView.DropDown". |
| 293 |
* @return the {link StyleResourceValue} for the style, or null if not found. |
| 294 |
*/ |
| 295 |
public StyleResourceValue getStyle(String styleName, boolean isFramework) { |
| 296 |
return null; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Returns the name of the resources as written in the XML. This undos the flattening of some |
| 301 |
* characters to '_' as done by aapt. |
| 302 |
* <p> |
| 303 |
* The method is not guaranteed to be implemented on the IDE side. In such a case, the method |
| 304 |
* will return null. |
| 305 |
* @param type the type of the resource |
| 306 |
* @param name the name of the resource that may have been obtained from the R class. |
| 307 |
* @param isFramework whether the resource is a framework resource. |
| 308 |
* |
| 309 |
* @return the name as written in the XML or null if not implemented for the resource type. |
| 310 |
*/ |
| 311 |
public String getXmlName(ResourceType type, String name, boolean isFramework) { |
| 312 |
return null; |
| 313 |
} |
| 314 |
} |