| 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.annotations.Nullable; |
| 20 |
import com.android.layoutlib.api.IResourceValue; |
| 21 |
import com.android.resources.ResourceType; |
| 22 |
|
| 23 |
/** |
| 24 |
* Represents an android resource with a name and a string value. |
| 25 |
*/ |
| 26 |
@SuppressWarnings("deprecation") |
| 27 |
public class ResourceValue extends ResourceReference implements IResourceValue { |
| 28 |
private final ResourceType mType; |
| 29 |
private final String mLibraryName; |
| 30 |
protected String mValue; |
| 31 |
|
| 32 |
public ResourceValue(ResourceType type, String name, boolean isFramework) { |
| 33 |
this(type, name, isFramework, null); |
| 34 |
} |
| 35 |
|
| 36 |
public ResourceValue(ResourceType type, String name, boolean isFramework, String libraryName) { |
| 37 |
super(name, isFramework); |
| 38 |
mType = type; |
| 39 |
mLibraryName = libraryName; |
| 40 |
} |
| 41 |
|
| 42 |
public ResourceValue(ResourceType type, String name, String value, boolean isFramework) { |
| 43 |
this(type, name, value, isFramework, null); |
| 44 |
} |
| 45 |
|
| 46 |
public ResourceValue(ResourceType type, String name, String value, boolean isFramework, String libraryName) { |
| 47 |
super(name, isFramework); |
| 48 |
mType = type; |
| 49 |
mValue = value; |
| 50 |
mLibraryName = libraryName; |
| 51 |
} |
| 52 |
|
| 53 |
public ResourceType getResourceType() { |
| 54 |
return mType; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the type of the resource. For instance "drawable", "color", etc... |
| 59 |
* @deprecated use {@link #getResourceType()} instead. |
| 60 |
*/ |
| 61 |
@Override |
| 62 |
@Deprecated |
| 63 |
public String getType() { |
| 64 |
return mType.getName(); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns the name of the library where this resource was found or null if it is not from a library. |
| 69 |
*/ |
| 70 |
public String getLibraryName() { |
| 71 |
return mLibraryName; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns true if the resource is user defined. |
| 76 |
*/ |
| 77 |
public boolean isUserDefined() { |
| 78 |
return !isFramework() && mLibraryName == null; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns the value of the resource, as defined in the XML. This can be <code>null</code>, |
| 83 |
* for example for instances of {@link StyleResourceValue}. |
| 84 |
*/ |
| 85 |
@Override |
| 86 |
@Nullable |
| 87 |
public String getValue() { |
| 88 |
return mValue; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Similar to {@link #getValue()}, but returns the raw XML value. This is <b>usually</b> |
| 93 |
* the same as getValue, but with a few exceptions. For example, for markup strings, |
| 94 |
* you can have * {@code <string name="markup">This is <b>bold</b></string>}. |
| 95 |
* Here, {@link #getValue()} will return "{@code This is bold}" -- e.g. just |
| 96 |
* the plain text flattened. However, this method will return "{@code This is <b>bold</b>}", |
| 97 |
* which preserves the XML markup elements. |
| 98 |
*/ |
| 99 |
public String getRawXmlValue() { |
| 100 |
return getValue(); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Sets the value of the resource. |
| 105 |
* @param value the new value |
| 106 |
*/ |
| 107 |
public void setValue(String value) { |
| 108 |
mValue = value; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Sets the value from another resource. |
| 113 |
* @param value the resource value |
| 114 |
*/ |
| 115 |
public void replaceWith(ResourceValue value) { |
| 116 |
mValue = value.mValue; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public String toString() { |
| 121 |
return getClass().getSimpleName() + " [" + mType + "/" + getName() + " = " + mValue //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 122 |
+ " (framework:" + isFramework() + ")]"; //$NON-NLS-1$ //$NON-NLS-2$ |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public int hashCode() { |
| 127 |
final int prime = 31; |
| 128 |
int result = super.hashCode(); |
| 129 |
result = prime * result + ((mType == null) ? 0 : mType.hashCode()); |
| 130 |
result = prime * result + ((mValue == null) ? 0 : mValue.hashCode()); |
| 131 |
return result; |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public boolean equals(Object obj) { |
| 136 |
if (this == obj) |
| 137 |
return true; |
| 138 |
if (!super.equals(obj)) |
| 139 |
return false; |
| 140 |
if (getClass() != obj.getClass()) |
| 141 |
return false; |
| 142 |
ResourceValue other = (ResourceValue) obj; |
| 143 |
if (mType == null) { |
| 144 |
//noinspection VariableNotUsedInsideIf |
| 145 |
if (other.mType != null) |
| 146 |
return false; |
| 147 |
} else if (!mType.equals(other.mType)) |
| 148 |
return false; |
| 149 |
if (mValue == null) { |
| 150 |
//noinspection VariableNotUsedInsideIf |
| 151 |
if (other.mValue != null) |
| 152 |
return false; |
| 153 |
} else if (!mValue.equals(other.mValue)) |
| 154 |
return false; |
| 155 |
return true; |
| 156 |
} |
| 157 |
} |