| 1 |
/* |
| 2 |
* Copyright (C) 2014 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 |
package com.android.ide.common.rendering.api; |
| 17 |
|
| 18 |
/** |
| 19 |
* Represents each item in the android style resource. |
| 20 |
*/ |
| 21 |
public class ItemResourceValue extends ResourceValue { |
| 22 |
|
| 23 |
private final boolean mIsFrameworkAttr; |
| 24 |
|
| 25 |
/** |
| 26 |
* @see #ItemResourceValue(String, boolean, String, boolean, String) |
| 27 |
*/ |
| 28 |
public ItemResourceValue(String name, boolean isFrameworkAttr, boolean isFrameworkStyle) { |
| 29 |
this(name, isFrameworkAttr, null, isFrameworkStyle, null); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @see #ItemResourceValue(String, boolean, String, boolean, String) |
| 34 |
*/ |
| 35 |
public ItemResourceValue(String name, boolean isFrameworkAttr, boolean isFrameworkStyle, String libraryName) { |
| 36 |
this(name, isFrameworkAttr, null, isFrameworkStyle, libraryName); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @see #ItemResourceValue(String, boolean, String, boolean, String) |
| 41 |
*/ |
| 42 |
public ItemResourceValue(String attributeName, boolean isFrameworkAttr, String value, |
| 43 |
boolean isFrameworkStyle) { |
| 44 |
this(attributeName, isFrameworkStyle, value, isFrameworkStyle, null); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* If the value is a reference to a framework resource or not is NOT represented with a boolean! |
| 49 |
* but can be deduced with: |
| 50 |
* <pre> {@code |
| 51 |
* boolean isFrameworkValue = item.isFramework() || |
| 52 |
* item.getValue().startsWith(SdkConstants.ANDROID_PREFIX) || |
| 53 |
* item.getValue().startsWith(SdkConstants.ANDROID_THEME_PREFIX); |
| 54 |
* } </pre> |
| 55 |
* For {@code <item name="foo">bar</item>}, item in a style resource, the values of the |
| 56 |
* parameters will be as follows: |
| 57 |
* |
| 58 |
* @param attributeName foo |
| 59 |
* @param isFrameworkAttr is foo in framework namespace. |
| 60 |
* @param value bar (in case of a reference, the value may include the namespace. |
| 61 |
* if the namespace is absent, default namespace is assumed based on |
| 62 |
* isFrameworkStyle (android namespace when isFrameworkStyle=true and app |
| 63 |
* namespace when isFrameworkStyle=false)) |
| 64 |
* @param isFrameworkStyle if the style is a framework file or project file. |
| 65 |
*/ |
| 66 |
public ItemResourceValue(String attributeName, boolean isFrameworkAttr, String value, |
| 67 |
boolean isFrameworkStyle, String libraryName) { |
| 68 |
super(null, attributeName, value, isFrameworkStyle, libraryName); |
| 69 |
mIsFrameworkAttr = isFrameworkAttr; |
| 70 |
} |
| 71 |
|
| 72 |
public boolean isFrameworkAttr() { |
| 73 |
return mIsFrameworkAttr; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public String toString() { |
| 78 |
return super.toString() + " (mIsFrameworkAttr=" + mIsFrameworkAttr + ")"; |
| 79 |
} |
| 80 |
|
| 81 |
Attribute getAttribute() { |
| 82 |
return new Attribute(getName(), mIsFrameworkAttr); |
| 83 |
} |
| 84 |
|
| 85 |
static ItemResourceValue fromResourceValue(ResourceValue res, boolean isFrameworkAttr, String libraryName) { |
| 86 |
assert res.getResourceType() == null : res.getResourceType() + " is not null"; |
| 87 |
return new ItemResourceValue(res.getName(), isFrameworkAttr, res.getValue(), |
| 88 |
res.isFramework(), libraryName); |
| 89 |
} |
| 90 |
|
| 91 |
static final class Attribute { |
| 92 |
String mName; |
| 93 |
boolean mIsFrameworkAttr; |
| 94 |
|
| 95 |
Attribute(String name, boolean isFrameworkAttr) { |
| 96 |
mName = name; |
| 97 |
mIsFrameworkAttr = isFrameworkAttr; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public int hashCode() { |
| 102 |
int booleanHash = mIsFrameworkAttr ? 1231 : 1237; // see java.lang.Boolean#hashCode() |
| 103 |
return 31 * booleanHash + mName.hashCode(); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public boolean equals(Object obj) { |
| 108 |
if (obj instanceof Attribute) { |
| 109 |
Attribute attr = (Attribute) obj; |
| 110 |
return mIsFrameworkAttr == attr.mIsFrameworkAttr && mName.equals(attr.mName); |
| 111 |
} |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public String toString() { |
| 117 |
return mName + " (framework:" + mIsFrameworkAttr + ")"; |
| 118 |
} |
| 119 |
} |
| 120 |
} |