Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | package android.content.res; |
| 18 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 19 | import android.annotation.AnyRes; |
Tor Norbye | 80756e3 | 2015-03-02 09:39:27 -0800 | [diff] [blame] | 20 | import android.annotation.ColorInt; |
Alan Viverette | fde4e3b | 2014-09-22 16:19:51 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 22 | import android.annotation.StyleableRes; |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 23 | import android.content.pm.ActivityInfo; |
| 24 | import android.content.pm.ActivityInfo.Config; |
Clara Bayarri | ed00bfd | 2017-01-20 14:58:21 +0000 | [diff] [blame] | 25 | import android.graphics.Typeface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.graphics.drawable.Drawable; |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 27 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.util.AttributeSet; |
| 29 | import android.util.DisplayMetrics; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.util.TypedValue; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 31 | |
| 32 | import com.android.internal.util.XmlUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 34 | import dalvik.system.VMRuntime; |
| 35 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import java.util.Arrays; |
| 37 | |
| 38 | /** |
| 39 | * Container for an array of values that were retrieved with |
| 40 | * {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)} |
| 41 | * or {@link Resources#obtainAttributes}. Be |
| 42 | * sure to call {@link #recycle} when done with them. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 43 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | * The indices used to retrieve values from this structure correspond to |
| 45 | * the positions of the attributes given to obtainStyledAttributes. |
| 46 | */ |
| 47 | public class TypedArray { |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 48 | |
| 49 | static TypedArray obtain(Resources res, int len) { |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 50 | TypedArray attrs = res.mTypedArrayPool.acquire(); |
| 51 | if (attrs == null) { |
| 52 | attrs = new TypedArray(res); |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 53 | } |
| 54 | |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 55 | attrs.mRecycled = false; |
| 56 | // Reset the assets, which may have changed due to configuration changes |
| 57 | // or further resource loading. |
| 58 | attrs.mAssets = res.getAssets(); |
Adam Lesinski | ec05bc0 | 2017-04-18 14:42:22 -0700 | [diff] [blame] | 59 | attrs.mMetrics = res.getDisplayMetrics(); |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 60 | attrs.resize(len); |
| 61 | return attrs; |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Alan Viverette | 8b5b25b | 2014-09-13 19:30:11 -0700 | [diff] [blame] | 64 | private final Resources mResources; |
Adam Lesinski | ec05bc0 | 2017-04-18 14:42:22 -0700 | [diff] [blame] | 65 | private DisplayMetrics mMetrics; |
Adam Lesinski | e60064a | 2016-07-13 12:09:24 -0700 | [diff] [blame] | 66 | private AssetManager mAssets; |
Alan Viverette | 8b5b25b | 2014-09-13 19:30:11 -0700 | [diff] [blame] | 67 | |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 68 | private boolean mRecycled; |
| 69 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | /*package*/ XmlBlock.Parser mXml; |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 71 | /*package*/ Resources.Theme mTheme; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | /*package*/ int[] mData; |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 73 | /*package*/ long mDataAddress; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | /*package*/ int[] mIndices; |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 75 | /*package*/ long mIndicesAddress; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | /*package*/ int mLength; |
Xavier Ducrohet | 7f9f99ea | 2011-08-11 10:16:17 -0700 | [diff] [blame] | 77 | /*package*/ TypedValue mValue = new TypedValue(); |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 78 | |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 79 | private void resize(int len) { |
| 80 | mLength = len; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 81 | final int dataLen = len * AssetManager.STYLE_NUM_ENTRIES; |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 82 | final int indicesLen = len + 1; |
| 83 | final VMRuntime runtime = VMRuntime.getRuntime(); |
John Reck | abd917d | 2017-01-04 19:04:25 -0800 | [diff] [blame] | 84 | if (mDataAddress == 0 || mData.length < dataLen) { |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 85 | mData = (int[]) runtime.newNonMovableArray(int.class, dataLen); |
| 86 | mDataAddress = runtime.addressOf(mData); |
| 87 | mIndices = (int[]) runtime.newNonMovableArray(int.class, indicesLen); |
| 88 | mIndicesAddress = runtime.addressOf(mIndices); |
| 89 | } |
| 90 | } |
| 91 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | /** |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 93 | * Returns the number of values in this array. |
| 94 | * |
| 95 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | */ |
| 97 | public int length() { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 98 | if (mRecycled) { |
| 99 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 100 | } |
| 101 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | return mLength; |
| 103 | } |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | /** |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 106 | * Returns the number of indices in the array that actually have data. Attributes with a value |
| 107 | * of @empty are included, as this is an explicit indicator. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 108 | * |
| 109 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | */ |
| 111 | public int getIndexCount() { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 112 | if (mRecycled) { |
| 113 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 114 | } |
| 115 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | return mIndices[0]; |
| 117 | } |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | /** |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 120 | * Returns an index in the array that has data. Attributes with a value of @empty are included, |
| 121 | * as this is an explicit indicator. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 122 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | * @param at The index you would like to returned, ranging from 0 to |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 124 | * {@link #getIndexCount()}. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 125 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | * @return The index at the given offset, which can be used with |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 127 | * {@link #getValue} and related APIs. |
| 128 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | */ |
| 130 | public int getIndex(int at) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 131 | if (mRecycled) { |
| 132 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 133 | } |
| 134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | return mIndices[1+at]; |
| 136 | } |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | /** |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 139 | * Returns the Resources object this array was loaded from. |
| 140 | * |
| 141 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | */ |
| 143 | public Resources getResources() { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 144 | if (mRecycled) { |
| 145 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 146 | } |
| 147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | return mResources; |
| 149 | } |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 150 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | /** |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 152 | * Retrieves the styled string value for the attribute at <var>index</var>. |
| 153 | * <p> |
| 154 | * If the attribute is not a string, this method will attempt to coerce |
| 155 | * it to a string. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 156 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | * @param index Index of attribute to retrieve. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 158 | * |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 159 | * @return CharSequence holding string data. May be styled. Returns |
| 160 | * {@code null} if the attribute is not defined or could not be |
| 161 | * coerced to a string. |
| 162 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 164 | public CharSequence getText(@StyleableRes int index) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 165 | if (mRecycled) { |
| 166 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 167 | } |
| 168 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 169 | index *= AssetManager.STYLE_NUM_ENTRIES; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 171 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | if (type == TypedValue.TYPE_NULL) { |
| 173 | return null; |
| 174 | } else if (type == TypedValue.TYPE_STRING) { |
| 175 | return loadStringValueAt(index); |
| 176 | } |
| 177 | |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 178 | final TypedValue v = mValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | if (getValueAt(index, v)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | return v.coerceToString(); |
| 181 | } |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 182 | |
| 183 | // We already checked for TYPE_NULL. This should never happen. |
| 184 | throw new RuntimeException("getText of bad type: 0x" + Integer.toHexString(type)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 188 | * Retrieves the string value for the attribute at <var>index</var>. |
| 189 | * <p> |
| 190 | * If the attribute is not a string, this method will attempt to coerce |
| 191 | * it to a string. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 192 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | * @param index Index of attribute to retrieve. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 194 | * |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 195 | * @return String holding string data. Any styling information is removed. |
| 196 | * Returns {@code null} if the attribute is not defined or could |
| 197 | * not be coerced to a string. |
| 198 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | */ |
Alan Viverette | a6f354b | 2015-05-18 15:26:57 -0700 | [diff] [blame] | 200 | @Nullable |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 201 | public String getString(@StyleableRes int index) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 202 | if (mRecycled) { |
| 203 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 204 | } |
| 205 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 206 | index *= AssetManager.STYLE_NUM_ENTRIES; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 208 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | if (type == TypedValue.TYPE_NULL) { |
| 210 | return null; |
| 211 | } else if (type == TypedValue.TYPE_STRING) { |
| 212 | return loadStringValueAt(index).toString(); |
| 213 | } |
| 214 | |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 215 | final TypedValue v = mValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | if (getValueAt(index, v)) { |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 217 | final CharSequence cs = v.coerceToString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | return cs != null ? cs.toString() : null; |
| 219 | } |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 220 | |
| 221 | // We already checked for TYPE_NULL. This should never happen. |
| 222 | throw new RuntimeException("getString of bad type: 0x" + Integer.toHexString(type)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 226 | * Retrieves the string value for the attribute at <var>index</var>, but |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | * only if that string comes from an immediate value in an XML file. That |
| 228 | * is, this does not allow references to string resources, string |
| 229 | * attributes, or conversions from other types. As such, this method |
| 230 | * will only return strings for TypedArray objects that come from |
| 231 | * attributes in an XML file. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 232 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | * @param index Index of attribute to retrieve. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 234 | * |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 235 | * @return String holding string data. Any styling information is removed. |
| 236 | * Returns {@code null} if the attribute is not defined or is not |
| 237 | * an immediate string value. |
| 238 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 240 | public String getNonResourceString(@StyleableRes int index) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 241 | if (mRecycled) { |
| 242 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 243 | } |
| 244 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 245 | index *= AssetManager.STYLE_NUM_ENTRIES; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 247 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | if (type == TypedValue.TYPE_STRING) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 249 | final int cookie = data[index+AssetManager.STYLE_ASSET_COOKIE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | if (cookie < 0) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 251 | return mXml.getPooledString( |
| 252 | data[index+AssetManager.STYLE_DATA]).toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | return null; |
| 256 | } |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 257 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | /** |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 259 | * Retrieves the string value for the attribute at <var>index</var> that is |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 260 | * not allowed to change with the given configurations. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 261 | * |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 262 | * @param index Index of attribute to retrieve. |
| 263 | * @param allowedChangingConfigs Bit mask of configurations from |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 264 | * {@link Configuration}.NATIVE_CONFIG_* that are allowed to change. |
Dianne Hackborn | 1d0b177 | 2013-09-06 14:02:54 -0700 | [diff] [blame] | 265 | * |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 266 | * @return String holding string data. Any styling information is removed. |
| 267 | * Returns {@code null} if the attribute is not defined. |
| 268 | * @throws RuntimeException if the TypedArray has already been recycled. |
| 269 | * @hide |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 270 | */ |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 271 | public String getNonConfigurationString(@StyleableRes int index, |
| 272 | @Config int allowedChangingConfigs) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 273 | if (mRecycled) { |
| 274 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 275 | } |
| 276 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 277 | index *= AssetManager.STYLE_NUM_ENTRIES; |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 278 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 279 | final int type = data[index+AssetManager.STYLE_TYPE]; |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 280 | final @Config int changingConfigs = ActivityInfo.activityInfoConfigNativeToJava( |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 281 | data[index + AssetManager.STYLE_CHANGING_CONFIGURATIONS]); |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 282 | if ((changingConfigs & ~allowedChangingConfigs) != 0) { |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 283 | return null; |
| 284 | } |
| 285 | if (type == TypedValue.TYPE_NULL) { |
| 286 | return null; |
| 287 | } else if (type == TypedValue.TYPE_STRING) { |
| 288 | return loadStringValueAt(index).toString(); |
| 289 | } |
| 290 | |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 291 | final TypedValue v = mValue; |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 292 | if (getValueAt(index, v)) { |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 293 | final CharSequence cs = v.coerceToString(); |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 294 | return cs != null ? cs.toString() : null; |
| 295 | } |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 296 | |
| 297 | // We already checked for TYPE_NULL. This should never happen. |
| 298 | throw new RuntimeException("getNonConfigurationString of bad type: 0x" |
| 299 | + Integer.toHexString(type)); |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | * Retrieve the boolean value for the attribute at <var>index</var>. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 304 | * <p> |
| 305 | * If the attribute is an integer value, this method will return whether |
| 306 | * it is equal to zero. If the attribute is not a boolean or integer value, |
| 307 | * this method will attempt to coerce it to an integer using |
| 308 | * {@link Integer#decode(String)} and return whether it is equal to zero. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 309 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | * @param index Index of attribute to retrieve. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 311 | * @param defValue Value to return if the attribute is not defined or |
| 312 | * cannot be coerced to an integer. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 313 | * |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 314 | * @return Boolean value of the attribute, or defValue if the attribute was |
| 315 | * not defined or could not be coerced to an integer. |
| 316 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 318 | public boolean getBoolean(@StyleableRes int index, boolean defValue) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 319 | if (mRecycled) { |
| 320 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 321 | } |
| 322 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 323 | index *= AssetManager.STYLE_NUM_ENTRIES; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 325 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | if (type == TypedValue.TYPE_NULL) { |
| 327 | return defValue; |
| 328 | } else if (type >= TypedValue.TYPE_FIRST_INT |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 329 | && type <= TypedValue.TYPE_LAST_INT) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 330 | return data[index+AssetManager.STYLE_DATA] != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 333 | final TypedValue v = mValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | if (getValueAt(index, v)) { |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 335 | StrictMode.noteResourceMismatch(v); |
| 336 | return XmlUtils.convertValueToBoolean(v.coerceToString(), defValue); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | } |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 338 | |
| 339 | // We already checked for TYPE_NULL. This should never happen. |
| 340 | throw new RuntimeException("getBoolean of bad type: 0x" + Integer.toHexString(type)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Retrieve the integer value for the attribute at <var>index</var>. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 345 | * <p> |
| 346 | * If the attribute is not an integer, this method will attempt to coerce |
| 347 | * it to an integer using {@link Integer#decode(String)}. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 348 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | * @param index Index of attribute to retrieve. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 350 | * @param defValue Value to return if the attribute is not defined or |
| 351 | * cannot be coerced to an integer. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 352 | * |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 353 | * @return Integer value of the attribute, or defValue if the attribute was |
| 354 | * not defined or could not be coerced to an integer. |
| 355 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 357 | public int getInt(@StyleableRes int index, int defValue) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 358 | if (mRecycled) { |
| 359 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 360 | } |
| 361 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 362 | index *= AssetManager.STYLE_NUM_ENTRIES; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 364 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | if (type == TypedValue.TYPE_NULL) { |
| 366 | return defValue; |
| 367 | } else if (type >= TypedValue.TYPE_FIRST_INT |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 368 | && type <= TypedValue.TYPE_LAST_INT) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 369 | return data[index+AssetManager.STYLE_DATA]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 372 | final TypedValue v = mValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | if (getValueAt(index, v)) { |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 374 | StrictMode.noteResourceMismatch(v); |
| 375 | return XmlUtils.convertValueToInt(v.coerceToString(), defValue); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | } |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 377 | |
| 378 | // We already checked for TYPE_NULL. This should never happen. |
| 379 | throw new RuntimeException("getInt of bad type: 0x" + Integer.toHexString(type)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Retrieve the float value for the attribute at <var>index</var>. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 384 | * <p> |
| 385 | * If the attribute is not a float or an integer, this method will attempt |
| 386 | * to coerce it to a float using {@link Float#parseFloat(String)}. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 387 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | * @param index Index of attribute to retrieve. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 389 | * |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 390 | * @return Attribute float value, or defValue if the attribute was |
| 391 | * not defined or could not be coerced to a float. |
| 392 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 394 | public float getFloat(@StyleableRes int index, float defValue) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 395 | if (mRecycled) { |
| 396 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 397 | } |
| 398 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 399 | index *= AssetManager.STYLE_NUM_ENTRIES; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 401 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | if (type == TypedValue.TYPE_NULL) { |
| 403 | return defValue; |
| 404 | } else if (type == TypedValue.TYPE_FLOAT) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 405 | return Float.intBitsToFloat(data[index+AssetManager.STYLE_DATA]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | } else if (type >= TypedValue.TYPE_FIRST_INT |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 407 | && type <= TypedValue.TYPE_LAST_INT) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 408 | return data[index+AssetManager.STYLE_DATA]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 411 | final TypedValue v = mValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | if (getValueAt(index, v)) { |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 413 | final CharSequence str = v.coerceToString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | if (str != null) { |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 415 | StrictMode.noteResourceMismatch(v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | return Float.parseFloat(str.toString()); |
| 417 | } |
| 418 | } |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 419 | |
| 420 | // We already checked for TYPE_NULL. This should never happen. |
| 421 | throw new RuntimeException("getFloat of bad type: 0x" + Integer.toHexString(type)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | } |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 423 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | /** |
| 425 | * Retrieve the color value for the attribute at <var>index</var>. If |
| 426 | * the attribute references a color resource holding a complex |
| 427 | * {@link android.content.res.ColorStateList}, then the default color from |
| 428 | * the set is returned. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 429 | * <p> |
| 430 | * This method will throw an exception if the attribute is defined but is |
| 431 | * not an integer color or color state list. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 432 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | * @param index Index of attribute to retrieve. |
| 434 | * @param defValue Value to return if the attribute is not defined or |
| 435 | * not a resource. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 436 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | * @return Attribute color value, or defValue if not defined. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 438 | * @throws RuntimeException if the TypedArray has already been recycled. |
| 439 | * @throws UnsupportedOperationException if the attribute is defined but is |
| 440 | * not an integer color or color state list. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | */ |
Tor Norbye | 80756e3 | 2015-03-02 09:39:27 -0800 | [diff] [blame] | 442 | @ColorInt |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 443 | public int getColor(@StyleableRes int index, @ColorInt int defValue) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 444 | if (mRecycled) { |
| 445 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 446 | } |
| 447 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 448 | final int attrIndex = index; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 449 | index *= AssetManager.STYLE_NUM_ENTRIES; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 450 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 452 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | if (type == TypedValue.TYPE_NULL) { |
| 454 | return defValue; |
| 455 | } else if (type >= TypedValue.TYPE_FIRST_INT |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 456 | && type <= TypedValue.TYPE_LAST_INT) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 457 | return data[index+AssetManager.STYLE_DATA]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | } else if (type == TypedValue.TYPE_STRING) { |
| 459 | final TypedValue value = mValue; |
| 460 | if (getValueAt(index, value)) { |
Alan Viverette | 45c4bbb | 2015-01-05 14:59:19 -0800 | [diff] [blame] | 461 | final ColorStateList csl = mResources.loadColorStateList( |
| 462 | value, value.resourceId, mTheme); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | return csl.getDefaultColor(); |
| 464 | } |
| 465 | return defValue; |
Alan Viverette | 216d3e6 | 2014-07-29 10:35:42 -0700 | [diff] [blame] | 466 | } else if (type == TypedValue.TYPE_ATTRIBUTE) { |
Alan Viverette | 9379505 | 2015-03-09 15:32:50 -0700 | [diff] [blame] | 467 | final TypedValue value = mValue; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 468 | getValueAt(index, value); |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 469 | throw new UnsupportedOperationException( |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 470 | "Failed to resolve attribute at index " + attrIndex + ": " + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 473 | throw new UnsupportedOperationException("Can't convert value at index " + attrIndex |
| 474 | + " to color: type=0x" + Integer.toHexString(type)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /** |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 478 | * Retrieve the ComplexColor for the attribute at <var>index</var>. |
| 479 | * The value may be either a {@link android.content.res.ColorStateList} which can wrap a simple |
| 480 | * color value or a {@link android.content.res.GradientColor} |
| 481 | * <p> |
| 482 | * This method will return {@code null} if the attribute is not defined or |
| 483 | * is not an integer color, color state list or GradientColor. |
| 484 | * |
| 485 | * @param index Index of attribute to retrieve. |
| 486 | * |
| 487 | * @return ComplexColor for the attribute, or {@code null} if not defined. |
| 488 | * @throws RuntimeException if the attribute if the TypedArray has already |
| 489 | * been recycled. |
| 490 | * @throws UnsupportedOperationException if the attribute is defined but is |
| 491 | * not an integer color, color state list or GradientColor. |
Teng-Hui Zhu | e03c469 | 2016-03-17 10:38:43 -0700 | [diff] [blame] | 492 | * @hide |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 493 | */ |
| 494 | @Nullable |
| 495 | public ComplexColor getComplexColor(@StyleableRes int index) { |
| 496 | if (mRecycled) { |
| 497 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 498 | } |
| 499 | |
| 500 | final TypedValue value = mValue; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 501 | if (getValueAt(index*AssetManager.STYLE_NUM_ENTRIES, value)) { |
Teng-Hui Zhu | dbee9bb | 2015-12-15 11:01:27 -0800 | [diff] [blame] | 502 | if (value.type == TypedValue.TYPE_ATTRIBUTE) { |
| 503 | throw new UnsupportedOperationException( |
| 504 | "Failed to resolve attribute at index " + index + ": " + value); |
| 505 | } |
| 506 | return mResources.loadComplexColor(value, value.resourceId, mTheme); |
| 507 | } |
| 508 | return null; |
| 509 | } |
| 510 | |
| 511 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | * Retrieve the ColorStateList for the attribute at <var>index</var>. |
| 513 | * The value may be either a single solid color or a reference to |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 514 | * a color or complex {@link android.content.res.ColorStateList} |
| 515 | * description. |
| 516 | * <p> |
| 517 | * This method will return {@code null} if the attribute is not defined or |
| 518 | * is not an integer color or color state list. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 519 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | * @param index Index of attribute to retrieve. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 521 | * |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 522 | * @return ColorStateList for the attribute, or {@code null} if not |
| 523 | * defined. |
| 524 | * @throws RuntimeException if the attribute if the TypedArray has already |
| 525 | * been recycled. |
| 526 | * @throws UnsupportedOperationException if the attribute is defined but is |
| 527 | * not an integer color or color state list. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | */ |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 529 | @Nullable |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 530 | public ColorStateList getColorStateList(@StyleableRes int index) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 531 | if (mRecycled) { |
| 532 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 533 | } |
| 534 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | final TypedValue value = mValue; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 536 | if (getValueAt(index*AssetManager.STYLE_NUM_ENTRIES, value)) { |
Alan Viverette | 216d3e6 | 2014-07-29 10:35:42 -0700 | [diff] [blame] | 537 | if (value.type == TypedValue.TYPE_ATTRIBUTE) { |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 538 | throw new UnsupportedOperationException( |
Alan Viverette | 9379505 | 2015-03-09 15:32:50 -0700 | [diff] [blame] | 539 | "Failed to resolve attribute at index " + index + ": " + value); |
Alan Viverette | 216d3e6 | 2014-07-29 10:35:42 -0700 | [diff] [blame] | 540 | } |
Alan Viverette | 45c4bbb | 2015-01-05 14:59:19 -0800 | [diff] [blame] | 541 | return mResources.loadColorStateList(value, value.resourceId, mTheme); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | } |
| 543 | return null; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Retrieve the integer value for the attribute at <var>index</var>. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 548 | * <p> |
| 549 | * Unlike {@link #getInt(int, int)}, this method will throw an exception if |
| 550 | * the attribute is defined but is not an integer. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 551 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | * @param index Index of attribute to retrieve. |
| 553 | * @param defValue Value to return if the attribute is not defined or |
| 554 | * not a resource. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 555 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | * @return Attribute integer value, or defValue if not defined. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 557 | * @throws RuntimeException if the TypedArray has already been recycled. |
| 558 | * @throws UnsupportedOperationException if the attribute is defined but is |
| 559 | * not an integer. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 561 | public int getInteger(@StyleableRes int index, int defValue) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 562 | if (mRecycled) { |
| 563 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 564 | } |
| 565 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 566 | final int attrIndex = index; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 567 | index *= AssetManager.STYLE_NUM_ENTRIES; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 568 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 570 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | if (type == TypedValue.TYPE_NULL) { |
| 572 | return defValue; |
| 573 | } else if (type >= TypedValue.TYPE_FIRST_INT |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 574 | && type <= TypedValue.TYPE_LAST_INT) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 575 | return data[index+AssetManager.STYLE_DATA]; |
Alan Viverette | 216d3e6 | 2014-07-29 10:35:42 -0700 | [diff] [blame] | 576 | } else if (type == TypedValue.TYPE_ATTRIBUTE) { |
Alan Viverette | 9379505 | 2015-03-09 15:32:50 -0700 | [diff] [blame] | 577 | final TypedValue value = mValue; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 578 | getValueAt(index, value); |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 579 | throw new UnsupportedOperationException( |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 580 | "Failed to resolve attribute at index " + attrIndex + ": " + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 583 | throw new UnsupportedOperationException("Can't convert value at index " + attrIndex |
| 584 | + " to integer: type=0x" + Integer.toHexString(type)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /** |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 588 | * Retrieve a dimensional unit attribute at <var>index</var>. Unit |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 589 | * conversions are based on the current {@link DisplayMetrics} |
| 590 | * associated with the resources this {@link TypedArray} object |
| 591 | * came from. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 592 | * <p> |
| 593 | * This method will throw an exception if the attribute is defined but is |
| 594 | * not a dimension. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 595 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | * @param index Index of attribute to retrieve. |
| 597 | * @param defValue Value to return if the attribute is not defined or |
| 598 | * not a resource. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 599 | * |
| 600 | * @return Attribute dimension value multiplied by the appropriate |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 601 | * metric, or defValue if not defined. |
| 602 | * @throws RuntimeException if the TypedArray has already been recycled. |
| 603 | * @throws UnsupportedOperationException if the attribute is defined but is |
| 604 | * not an integer. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 605 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | * @see #getDimensionPixelOffset |
| 607 | * @see #getDimensionPixelSize |
| 608 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 609 | public float getDimension(@StyleableRes int index, float defValue) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 610 | if (mRecycled) { |
| 611 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 612 | } |
| 613 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 614 | final int attrIndex = index; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 615 | index *= AssetManager.STYLE_NUM_ENTRIES; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 616 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 618 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | if (type == TypedValue.TYPE_NULL) { |
| 620 | return defValue; |
| 621 | } else if (type == TypedValue.TYPE_DIMENSION) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 622 | return TypedValue.complexToDimension( |
| 623 | data[index + AssetManager.STYLE_DATA], mMetrics); |
Alan Viverette | 216d3e6 | 2014-07-29 10:35:42 -0700 | [diff] [blame] | 624 | } else if (type == TypedValue.TYPE_ATTRIBUTE) { |
Alan Viverette | 9379505 | 2015-03-09 15:32:50 -0700 | [diff] [blame] | 625 | final TypedValue value = mValue; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 626 | getValueAt(index, value); |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 627 | throw new UnsupportedOperationException( |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 628 | "Failed to resolve attribute at index " + attrIndex + ": " + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 631 | throw new UnsupportedOperationException("Can't convert value at index " + attrIndex |
| 632 | + " to dimension: type=0x" + Integer.toHexString(type)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Retrieve a dimensional unit attribute at <var>index</var> for use |
| 637 | * as an offset in raw pixels. This is the same as |
| 638 | * {@link #getDimension}, except the returned value is converted to |
| 639 | * integer pixels for you. An offset conversion involves simply |
| 640 | * truncating the base value to an integer. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 641 | * <p> |
| 642 | * This method will throw an exception if the attribute is defined but is |
| 643 | * not a dimension. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 644 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | * @param index Index of attribute to retrieve. |
| 646 | * @param defValue Value to return if the attribute is not defined or |
| 647 | * not a resource. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 648 | * |
| 649 | * @return Attribute dimension value multiplied by the appropriate |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 650 | * metric and truncated to integer pixels, or defValue if not defined. |
| 651 | * @throws RuntimeException if the TypedArray has already been recycled. |
| 652 | * @throws UnsupportedOperationException if the attribute is defined but is |
| 653 | * not an integer. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 654 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | * @see #getDimension |
| 656 | * @see #getDimensionPixelSize |
| 657 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 658 | public int getDimensionPixelOffset(@StyleableRes int index, int defValue) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 659 | if (mRecycled) { |
| 660 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 661 | } |
| 662 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 663 | final int attrIndex = index; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 664 | index *= AssetManager.STYLE_NUM_ENTRIES; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 665 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 667 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | if (type == TypedValue.TYPE_NULL) { |
| 669 | return defValue; |
| 670 | } else if (type == TypedValue.TYPE_DIMENSION) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 671 | return TypedValue.complexToDimensionPixelOffset( |
| 672 | data[index + AssetManager.STYLE_DATA], mMetrics); |
Alan Viverette | 216d3e6 | 2014-07-29 10:35:42 -0700 | [diff] [blame] | 673 | } else if (type == TypedValue.TYPE_ATTRIBUTE) { |
Alan Viverette | 9379505 | 2015-03-09 15:32:50 -0700 | [diff] [blame] | 674 | final TypedValue value = mValue; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 675 | getValueAt(index, value); |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 676 | throw new UnsupportedOperationException( |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 677 | "Failed to resolve attribute at index " + attrIndex + ": " + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 680 | throw new UnsupportedOperationException("Can't convert value at index " + attrIndex |
| 681 | + " to dimension: type=0x" + Integer.toHexString(type)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Retrieve a dimensional unit attribute at <var>index</var> for use |
| 686 | * as a size in raw pixels. This is the same as |
| 687 | * {@link #getDimension}, except the returned value is converted to |
| 688 | * integer pixels for use as a size. A size conversion involves |
| 689 | * rounding the base value, and ensuring that a non-zero base value |
| 690 | * is at least one pixel in size. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 691 | * <p> |
| 692 | * This method will throw an exception if the attribute is defined but is |
| 693 | * not a dimension. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 694 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | * @param index Index of attribute to retrieve. |
| 696 | * @param defValue Value to return if the attribute is not defined or |
| 697 | * not a resource. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 698 | * |
| 699 | * @return Attribute dimension value multiplied by the appropriate |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 700 | * metric and truncated to integer pixels, or defValue if not defined. |
| 701 | * @throws RuntimeException if the TypedArray has already been recycled. |
| 702 | * @throws UnsupportedOperationException if the attribute is defined but is |
| 703 | * not a dimension. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 704 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | * @see #getDimension |
| 706 | * @see #getDimensionPixelOffset |
| 707 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 708 | public int getDimensionPixelSize(@StyleableRes int index, int defValue) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 709 | if (mRecycled) { |
| 710 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 711 | } |
| 712 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 713 | final int attrIndex = index; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 714 | index *= AssetManager.STYLE_NUM_ENTRIES; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 715 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 717 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | if (type == TypedValue.TYPE_NULL) { |
| 719 | return defValue; |
| 720 | } else if (type == TypedValue.TYPE_DIMENSION) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 721 | return TypedValue.complexToDimensionPixelSize( |
| 722 | data[index+AssetManager.STYLE_DATA], mMetrics); |
Alan Viverette | 216d3e6 | 2014-07-29 10:35:42 -0700 | [diff] [blame] | 723 | } else if (type == TypedValue.TYPE_ATTRIBUTE) { |
Alan Viverette | 9379505 | 2015-03-09 15:32:50 -0700 | [diff] [blame] | 724 | final TypedValue value = mValue; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 725 | getValueAt(index, value); |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 726 | throw new UnsupportedOperationException( |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 727 | "Failed to resolve attribute at index " + attrIndex + ": " + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 730 | throw new UnsupportedOperationException("Can't convert value at index " + attrIndex |
| 731 | + " to dimension: type=0x" + Integer.toHexString(type)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /** |
| 735 | * Special version of {@link #getDimensionPixelSize} for retrieving |
| 736 | * {@link android.view.ViewGroup}'s layout_width and layout_height |
| 737 | * attributes. This is only here for performance reasons; applications |
| 738 | * should use {@link #getDimensionPixelSize}. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 739 | * <p> |
| 740 | * This method will throw an exception if the attribute is defined but is |
| 741 | * not a dimension or integer (enum). |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 742 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | * @param index Index of the attribute to retrieve. |
| 744 | * @param name Textual name of attribute for error reporting. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 745 | * |
| 746 | * @return Attribute dimension value multiplied by the appropriate |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 747 | * metric and truncated to integer pixels. |
| 748 | * @throws RuntimeException if the TypedArray has already been recycled. |
| 749 | * @throws UnsupportedOperationException if the attribute is defined but is |
| 750 | * not a dimension or integer (enum). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 752 | public int getLayoutDimension(@StyleableRes int index, String name) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 753 | if (mRecycled) { |
| 754 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 755 | } |
| 756 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 757 | final int attrIndex = index; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 758 | index *= AssetManager.STYLE_NUM_ENTRIES; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 759 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 761 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | if (type >= TypedValue.TYPE_FIRST_INT |
| 763 | && type <= TypedValue.TYPE_LAST_INT) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 764 | return data[index+AssetManager.STYLE_DATA]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | } else if (type == TypedValue.TYPE_DIMENSION) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 766 | return TypedValue.complexToDimensionPixelSize( |
| 767 | data[index+AssetManager.STYLE_DATA], mMetrics); |
Alan Viverette | 216d3e6 | 2014-07-29 10:35:42 -0700 | [diff] [blame] | 768 | } else if (type == TypedValue.TYPE_ATTRIBUTE) { |
Alan Viverette | 9379505 | 2015-03-09 15:32:50 -0700 | [diff] [blame] | 769 | final TypedValue value = mValue; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 770 | getValueAt(index, value); |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 771 | throw new UnsupportedOperationException( |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 772 | "Failed to resolve attribute at index " + attrIndex + ": " + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 775 | throw new UnsupportedOperationException(getPositionDescription() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | + ": You must supply a " + name + " attribute."); |
| 777 | } |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 778 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | /** |
| 780 | * Special version of {@link #getDimensionPixelSize} for retrieving |
| 781 | * {@link android.view.ViewGroup}'s layout_width and layout_height |
| 782 | * attributes. This is only here for performance reasons; applications |
| 783 | * should use {@link #getDimensionPixelSize}. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 784 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | * @param index Index of the attribute to retrieve. |
| 786 | * @param defValue The default value to return if this attribute is not |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 787 | * default or contains the wrong type of data. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 788 | * |
| 789 | * @return Attribute dimension value multiplied by the appropriate |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 790 | * metric and truncated to integer pixels. |
| 791 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 793 | public int getLayoutDimension(@StyleableRes int index, int defValue) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 794 | if (mRecycled) { |
| 795 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 796 | } |
| 797 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 798 | index *= AssetManager.STYLE_NUM_ENTRIES; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 799 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 800 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | if (type >= TypedValue.TYPE_FIRST_INT |
| 802 | && type <= TypedValue.TYPE_LAST_INT) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 803 | return data[index+AssetManager.STYLE_DATA]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | } else if (type == TypedValue.TYPE_DIMENSION) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 805 | return TypedValue.complexToDimensionPixelSize( |
| 806 | data[index + AssetManager.STYLE_DATA], mMetrics); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | return defValue; |
| 810 | } |
| 811 | |
| 812 | /** |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 813 | * Retrieves a fractional unit attribute at <var>index</var>. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 814 | * |
| 815 | * @param index Index of attribute to retrieve. |
| 816 | * @param base The base value of this fraction. In other words, a |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | * standard fraction is multiplied by this value. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 818 | * @param pbase The parent base value of this fraction. In other |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | * words, a parent fraction (nn%p) is multiplied by this |
| 820 | * value. |
| 821 | * @param defValue Value to return if the attribute is not defined or |
| 822 | * not a resource. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 823 | * |
| 824 | * @return Attribute fractional value multiplied by the appropriate |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 825 | * base value, or defValue if not defined. |
| 826 | * @throws RuntimeException if the TypedArray has already been recycled. |
| 827 | * @throws UnsupportedOperationException if the attribute is defined but is |
| 828 | * not a fraction. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 830 | public float getFraction(@StyleableRes int index, int base, int pbase, float defValue) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 831 | if (mRecycled) { |
| 832 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 833 | } |
| 834 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 835 | final int attrIndex = index; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 836 | index *= AssetManager.STYLE_NUM_ENTRIES; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 837 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 839 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | if (type == TypedValue.TYPE_NULL) { |
| 841 | return defValue; |
| 842 | } else if (type == TypedValue.TYPE_FRACTION) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 843 | return TypedValue.complexToFraction( |
| 844 | data[index+AssetManager.STYLE_DATA], base, pbase); |
Alan Viverette | 216d3e6 | 2014-07-29 10:35:42 -0700 | [diff] [blame] | 845 | } else if (type == TypedValue.TYPE_ATTRIBUTE) { |
Alan Viverette | 9379505 | 2015-03-09 15:32:50 -0700 | [diff] [blame] | 846 | final TypedValue value = mValue; |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 847 | getValueAt(index, value); |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 848 | throw new UnsupportedOperationException( |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 849 | "Failed to resolve attribute at index " + attrIndex + ": " + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Alan Viverette | 5b90e82 | 2016-02-24 09:06:47 -0500 | [diff] [blame] | 852 | throw new UnsupportedOperationException("Can't convert value at index " + attrIndex |
| 853 | + " to fraction: type=0x" + Integer.toHexString(type)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /** |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 857 | * Retrieves the resource identifier for the attribute at |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 858 | * <var>index</var>. Note that attribute resource as resolved when |
| 859 | * the overall {@link TypedArray} object is retrieved. As a |
| 860 | * result, this function will return the resource identifier of the |
| 861 | * final resource value that was found, <em>not</em> necessarily the |
| 862 | * original resource that was specified by the attribute. |
| 863 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | * @param index Index of attribute to retrieve. |
| 865 | * @param defValue Value to return if the attribute is not defined or |
| 866 | * not a resource. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 867 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | * @return Attribute resource identifier, or defValue if not defined. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 869 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 871 | @AnyRes |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 872 | public int getResourceId(@StyleableRes int index, int defValue) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 873 | if (mRecycled) { |
| 874 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 875 | } |
| 876 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 877 | index *= AssetManager.STYLE_NUM_ENTRIES; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 879 | if (data[index+AssetManager.STYLE_TYPE] != TypedValue.TYPE_NULL) { |
| 880 | final int resid = data[index+AssetManager.STYLE_RESOURCE_ID]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | if (resid != 0) { |
| 882 | return resid; |
| 883 | } |
| 884 | } |
| 885 | return defValue; |
| 886 | } |
| 887 | |
| 888 | /** |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 889 | * Retrieves the theme attribute resource identifier for the attribute at |
Alan Viverette | 500c560 | 2014-07-29 11:30:35 -0700 | [diff] [blame] | 890 | * <var>index</var>. |
| 891 | * |
| 892 | * @param index Index of attribute to retrieve. |
| 893 | * @param defValue Value to return if the attribute is not defined or not a |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 894 | * resource. |
| 895 | * |
Alan Viverette | 500c560 | 2014-07-29 11:30:35 -0700 | [diff] [blame] | 896 | * @return Theme attribute resource identifier, or defValue if not defined. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 897 | * @throws RuntimeException if the TypedArray has already been recycled. |
Alan Viverette | 500c560 | 2014-07-29 11:30:35 -0700 | [diff] [blame] | 898 | * @hide |
| 899 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 900 | public int getThemeAttributeId(@StyleableRes int index, int defValue) { |
Alan Viverette | 500c560 | 2014-07-29 11:30:35 -0700 | [diff] [blame] | 901 | if (mRecycled) { |
| 902 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 903 | } |
| 904 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 905 | index *= AssetManager.STYLE_NUM_ENTRIES; |
Alan Viverette | 500c560 | 2014-07-29 11:30:35 -0700 | [diff] [blame] | 906 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 907 | if (data[index + AssetManager.STYLE_TYPE] == TypedValue.TYPE_ATTRIBUTE) { |
| 908 | return data[index + AssetManager.STYLE_DATA]; |
Alan Viverette | 500c560 | 2014-07-29 11:30:35 -0700 | [diff] [blame] | 909 | } |
| 910 | return defValue; |
| 911 | } |
| 912 | |
| 913 | /** |
Alan Viverette | fde4e3b | 2014-09-22 16:19:51 -0700 | [diff] [blame] | 914 | * Retrieve the Drawable for the attribute at <var>index</var>. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 915 | * <p> |
| 916 | * This method will throw an exception if the attribute is defined but is |
| 917 | * not a color or drawable resource. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 918 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | * @param index Index of attribute to retrieve. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 920 | * |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 921 | * @return Drawable for the attribute, or {@code null} if not defined. |
| 922 | * @throws RuntimeException if the TypedArray has already been recycled. |
| 923 | * @throws UnsupportedOperationException if the attribute is defined but is |
| 924 | * not a color or drawable resource. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | */ |
Alan Viverette | fde4e3b | 2014-09-22 16:19:51 -0700 | [diff] [blame] | 926 | @Nullable |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 927 | public Drawable getDrawable(@StyleableRes int index) { |
Adam Lesinski | 50954d2 | 2017-04-14 18:41:52 -0700 | [diff] [blame] | 928 | return getDrawableForDensity(index, 0); |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * Version of {@link #getDrawable(int)} that accepts an override density. |
| 933 | * @hide |
| 934 | */ |
| 935 | @Nullable |
| 936 | public Drawable getDrawableForDensity(@StyleableRes int index, int density) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 937 | if (mRecycled) { |
| 938 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 939 | } |
| 940 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | final TypedValue value = mValue; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 942 | if (getValueAt(index*AssetManager.STYLE_NUM_ENTRIES, value)) { |
Alan Viverette | 216d3e6 | 2014-07-29 10:35:42 -0700 | [diff] [blame] | 943 | if (value.type == TypedValue.TYPE_ATTRIBUTE) { |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 944 | throw new UnsupportedOperationException( |
Alan Viverette | 9379505 | 2015-03-09 15:32:50 -0700 | [diff] [blame] | 945 | "Failed to resolve attribute at index " + index + ": " + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | } |
Adam Lesinski | 50954d2 | 2017-04-14 18:41:52 -0700 | [diff] [blame] | 947 | |
| 948 | if (density > 0) { |
| 949 | // If the density is overridden, the value in the TypedArray will not reflect this. |
| 950 | // Do a separate lookup of the resourceId with the density override. |
| 951 | mResources.getValueForDensity(value.resourceId, density, value, true); |
| 952 | } |
| 953 | return mResources.loadDrawable(value, value.resourceId, density, mTheme); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | } |
| 955 | return null; |
| 956 | } |
| 957 | |
| 958 | /** |
Clara Bayarri | ed00bfd | 2017-01-20 14:58:21 +0000 | [diff] [blame] | 959 | * Retrieve the Typeface for the attribute at <var>index</var>. |
| 960 | * <p> |
| 961 | * This method will throw an exception if the attribute is defined but is |
| 962 | * not a font. |
| 963 | * |
| 964 | * @param index Index of attribute to retrieve. |
| 965 | * |
| 966 | * @return Typeface for the attribute, or {@code null} if not defined. |
| 967 | * @throws RuntimeException if the TypedArray has already been recycled. |
| 968 | * @throws UnsupportedOperationException if the attribute is defined but is |
| 969 | * not a font resource. |
| 970 | */ |
| 971 | @Nullable |
| 972 | public Typeface getFont(@StyleableRes int index) { |
| 973 | if (mRecycled) { |
| 974 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 975 | } |
| 976 | |
| 977 | final TypedValue value = mValue; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 978 | if (getValueAt(index*AssetManager.STYLE_NUM_ENTRIES, value)) { |
Clara Bayarri | ed00bfd | 2017-01-20 14:58:21 +0000 | [diff] [blame] | 979 | if (value.type == TypedValue.TYPE_ATTRIBUTE) { |
| 980 | throw new UnsupportedOperationException( |
| 981 | "Failed to resolve attribute at index " + index + ": " + value); |
| 982 | } |
| 983 | return mResources.getFont(value, value.resourceId); |
| 984 | } |
| 985 | return null; |
| 986 | } |
| 987 | |
| 988 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | * Retrieve the CharSequence[] for the attribute at <var>index</var>. |
| 990 | * This gets the resource ID of the selected attribute, and uses |
| 991 | * {@link Resources#getTextArray Resources.getTextArray} of the owning |
| 992 | * Resources object to retrieve its String[]. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 993 | * <p> |
| 994 | * This method will throw an exception if the attribute is defined but is |
| 995 | * not a text array resource. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 996 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | * @param index Index of attribute to retrieve. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 998 | * |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 999 | * @return CharSequence[] for the attribute, or {@code null} if not |
| 1000 | * defined. |
| 1001 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 1003 | public CharSequence[] getTextArray(@StyleableRes int index) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1004 | if (mRecycled) { |
| 1005 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 1006 | } |
| 1007 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | final TypedValue value = mValue; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1009 | if (getValueAt(index*AssetManager.STYLE_NUM_ENTRIES, value)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | return mResources.getTextArray(value.resourceId); |
| 1011 | } |
| 1012 | return null; |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * Retrieve the raw TypedValue for the attribute at <var>index</var>. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1017 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | * @param index Index of attribute to retrieve. |
| 1019 | * @param outValue TypedValue object in which to place the attribute's |
| 1020 | * data. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1021 | * |
Adam Lesinski | 32e7501 | 2017-05-09 15:25:37 -0700 | [diff] [blame] | 1022 | * @return {@code true} if the value was retrieved and not @empty, {@code false} otherwise. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1023 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 1025 | public boolean getValue(@StyleableRes int index, TypedValue outValue) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1026 | if (mRecycled) { |
| 1027 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1028 | } |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1029 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1030 | return getValueAt(index*AssetManager.STYLE_NUM_ENTRIES, outValue); |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | /** |
| 1034 | * Returns the type of attribute at the specified index. |
| 1035 | * |
| 1036 | * @param index Index of attribute whose type to retrieve. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1037 | * |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1038 | * @return Attribute type. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1039 | * @throws RuntimeException if the TypedArray has already been recycled. |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1040 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 1041 | public int getType(@StyleableRes int index) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1042 | if (mRecycled) { |
| 1043 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 1044 | } |
| 1045 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1046 | index *= AssetManager.STYLE_NUM_ENTRIES; |
| 1047 | return mData[index + AssetManager.STYLE_TYPE]; |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | * Determines whether there is an attribute at <var>index</var>. |
Alan Viverette | f296940 | 2014-10-29 17:09:36 -0700 | [diff] [blame] | 1052 | * <p> |
| 1053 | * <strong>Note:</strong> If the attribute was set to {@code @empty} or |
| 1054 | * {@code @undefined}, this method returns {@code false}. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1055 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | * @param index Index of attribute to retrieve. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1057 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | * @return True if the attribute has a value, false otherwise. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1059 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1060 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 1061 | public boolean hasValue(@StyleableRes int index) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1062 | if (mRecycled) { |
| 1063 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 1064 | } |
| 1065 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1066 | index *= AssetManager.STYLE_NUM_ENTRIES; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1068 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | return type != TypedValue.TYPE_NULL; |
| 1070 | } |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1071 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | /** |
Alan Viverette | f296940 | 2014-10-29 17:09:36 -0700 | [diff] [blame] | 1073 | * Determines whether there is an attribute at <var>index</var>, returning |
| 1074 | * {@code true} if the attribute was explicitly set to {@code @empty} and |
| 1075 | * {@code false} only if the attribute was undefined. |
| 1076 | * |
| 1077 | * @param index Index of attribute to retrieve. |
| 1078 | * |
| 1079 | * @return True if the attribute has a value or is empty, false otherwise. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1080 | * @throws RuntimeException if the TypedArray has already been recycled. |
Alan Viverette | f296940 | 2014-10-29 17:09:36 -0700 | [diff] [blame] | 1081 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 1082 | public boolean hasValueOrEmpty(@StyleableRes int index) { |
Alan Viverette | f296940 | 2014-10-29 17:09:36 -0700 | [diff] [blame] | 1083 | if (mRecycled) { |
| 1084 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 1085 | } |
| 1086 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1087 | index *= AssetManager.STYLE_NUM_ENTRIES; |
Alan Viverette | f296940 | 2014-10-29 17:09:36 -0700 | [diff] [blame] | 1088 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1089 | final int type = data[index+AssetManager.STYLE_TYPE]; |
Alan Viverette | f296940 | 2014-10-29 17:09:36 -0700 | [diff] [blame] | 1090 | return type != TypedValue.TYPE_NULL |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1091 | || data[index+AssetManager.STYLE_DATA] == TypedValue.DATA_NULL_EMPTY; |
Alan Viverette | f296940 | 2014-10-29 17:09:36 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /** |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1095 | * Retrieve the raw TypedValue for the attribute at <var>index</var> |
| 1096 | * and return a temporary object holding its data. This object is only |
| 1097 | * valid until the next call on to {@link TypedArray}. |
| 1098 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | * @param index Index of attribute to retrieve. |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1100 | * |
| 1101 | * @return Returns a TypedValue object if the attribute is defined, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1102 | * containing its data; otherwise returns null. (You will not |
| 1103 | * receive a TypedValue whose type is TYPE_NULL.) |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1104 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | */ |
Deepanshu Gupta | d7bb3f8 | 2015-08-05 16:38:21 -0700 | [diff] [blame] | 1106 | public TypedValue peekValue(@StyleableRes int index) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1107 | if (mRecycled) { |
| 1108 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 1109 | } |
| 1110 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | final TypedValue value = mValue; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1112 | if (getValueAt(index*AssetManager.STYLE_NUM_ENTRIES, value)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1113 | return value; |
| 1114 | } |
| 1115 | return null; |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * Returns a message about the parser state suitable for printing error messages. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1120 | * |
| 1121 | * @return Human-readable description of current parser state. |
| 1122 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1123 | */ |
| 1124 | public String getPositionDescription() { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1125 | if (mRecycled) { |
| 1126 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 1127 | } |
| 1128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | return mXml != null ? mXml.getPositionDescription() : "<internal>"; |
| 1130 | } |
| 1131 | |
| 1132 | /** |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1133 | * Recycles the TypedArray, to be re-used by a later caller. After calling |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1134 | * this function you must not ever touch the typed array again. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1135 | * |
| 1136 | * @throws RuntimeException if the TypedArray has already been recycled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | */ |
| 1138 | public void recycle() { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1139 | if (mRecycled) { |
| 1140 | throw new RuntimeException(toString() + " recycled twice!"); |
| 1141 | } |
| 1142 | |
| 1143 | mRecycled = true; |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 1144 | |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1145 | // These may have been set by the client. |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 1146 | mXml = null; |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 1147 | mTheme = null; |
Adam Lesinski | e60064a | 2016-07-13 12:09:24 -0700 | [diff] [blame] | 1148 | mAssets = null; |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1149 | |
Alan Viverette | 8b5b25b | 2014-09-13 19:30:11 -0700 | [diff] [blame] | 1150 | mResources.mTypedArrayPool.release(this); |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * Extracts theme attributes from a typed array for later resolution using |
Alan Viverette | 9cd14fc | 2014-06-20 14:45:11 -0700 | [diff] [blame] | 1155 | * {@link android.content.res.Resources.Theme#resolveAttributes(int[], int[])}. |
| 1156 | * Removes the entries from the typed array so that subsequent calls to typed |
| 1157 | * getters will return the default value without crashing. |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1158 | * |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1159 | * @return an array of length {@link #getIndexCount()} populated with theme |
| 1160 | * attributes, or null if there are no theme attributes in the typed |
| 1161 | * array |
| 1162 | * @throws RuntimeException if the TypedArray has already been recycled. |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1163 | * @hide |
| 1164 | */ |
Alan Viverette | 45c4bbb | 2015-01-05 14:59:19 -0800 | [diff] [blame] | 1165 | @Nullable |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1166 | public int[] extractThemeAttrs() { |
Alan Viverette | 45c4bbb | 2015-01-05 14:59:19 -0800 | [diff] [blame] | 1167 | return extractThemeAttrs(null); |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * @hide |
| 1172 | */ |
| 1173 | @Nullable |
| 1174 | public int[] extractThemeAttrs(@Nullable int[] scrap) { |
Alan Viverette | fd274a0 | 2014-03-27 15:39:15 -0700 | [diff] [blame] | 1175 | if (mRecycled) { |
| 1176 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 1177 | } |
| 1178 | |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1179 | int[] attrs = null; |
| 1180 | |
Alan Viverette | 0cfb877 | 2014-05-14 17:40:53 -0700 | [diff] [blame] | 1181 | final int[] data = mData; |
Alan Viverette | 1cd4612 | 2014-03-25 17:58:50 -0700 | [diff] [blame] | 1182 | final int N = length(); |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1183 | for (int i = 0; i < N; i++) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1184 | final int index = i * AssetManager.STYLE_NUM_ENTRIES; |
| 1185 | if (data[index + AssetManager.STYLE_TYPE] != TypedValue.TYPE_ATTRIBUTE) { |
Alan Viverette | 45c4bbb | 2015-01-05 14:59:19 -0800 | [diff] [blame] | 1186 | // Not an attribute, ignore. |
Alan Viverette | 0cfb877 | 2014-05-14 17:40:53 -0700 | [diff] [blame] | 1187 | continue; |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1188 | } |
Alan Viverette | 0cfb877 | 2014-05-14 17:40:53 -0700 | [diff] [blame] | 1189 | |
| 1190 | // Null the entry so that we can safely call getZzz(). |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1191 | data[index + AssetManager.STYLE_TYPE] = TypedValue.TYPE_NULL; |
Alan Viverette | 0cfb877 | 2014-05-14 17:40:53 -0700 | [diff] [blame] | 1192 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1193 | final int attr = data[index + AssetManager.STYLE_DATA]; |
Alan Viverette | 0cfb877 | 2014-05-14 17:40:53 -0700 | [diff] [blame] | 1194 | if (attr == 0) { |
Alan Viverette | 45c4bbb | 2015-01-05 14:59:19 -0800 | [diff] [blame] | 1195 | // Useless data, ignore. |
Alan Viverette | 0cfb877 | 2014-05-14 17:40:53 -0700 | [diff] [blame] | 1196 | continue; |
| 1197 | } |
| 1198 | |
Alan Viverette | 45c4bbb | 2015-01-05 14:59:19 -0800 | [diff] [blame] | 1199 | // Ensure we have a usable attribute array. |
Alan Viverette | 0cfb877 | 2014-05-14 17:40:53 -0700 | [diff] [blame] | 1200 | if (attrs == null) { |
Alan Viverette | 45c4bbb | 2015-01-05 14:59:19 -0800 | [diff] [blame] | 1201 | if (scrap != null && scrap.length == N) { |
| 1202 | attrs = scrap; |
| 1203 | Arrays.fill(attrs, 0); |
| 1204 | } else { |
| 1205 | attrs = new int[N]; |
| 1206 | } |
Alan Viverette | 0cfb877 | 2014-05-14 17:40:53 -0700 | [diff] [blame] | 1207 | } |
Alan Viverette | 45c4bbb | 2015-01-05 14:59:19 -0800 | [diff] [blame] | 1208 | |
Alan Viverette | 0cfb877 | 2014-05-14 17:40:53 -0700 | [diff] [blame] | 1209 | attrs[i] = attr; |
Alan Viverette | 52b999f | 2014-03-24 18:00:26 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | return attrs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
Alan Viverette | 8e5e11b | 2014-07-07 14:06:15 -0700 | [diff] [blame] | 1215 | /** |
| 1216 | * Return a mask of the configuration parameters for which the values in |
| 1217 | * this typed array may change. |
| 1218 | * |
| 1219 | * @return Returns a mask of the changing configuration parameters, as |
| 1220 | * defined by {@link android.content.pm.ActivityInfo}. |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1221 | * @throws RuntimeException if the TypedArray has already been recycled. |
Alan Viverette | 8e5e11b | 2014-07-07 14:06:15 -0700 | [diff] [blame] | 1222 | * @see android.content.pm.ActivityInfo |
| 1223 | */ |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 1224 | public @Config int getChangingConfigurations() { |
Alan Viverette | 6bbb47b | 2015-01-05 18:12:44 -0800 | [diff] [blame] | 1225 | if (mRecycled) { |
| 1226 | throw new RuntimeException("Cannot make calls to a recycled instance!"); |
| 1227 | } |
| 1228 | |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 1229 | @Config int changingConfig = 0; |
Alan Viverette | 8e5e11b | 2014-07-07 14:06:15 -0700 | [diff] [blame] | 1230 | |
| 1231 | final int[] data = mData; |
| 1232 | final int N = length(); |
| 1233 | for (int i = 0; i < N; i++) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1234 | final int index = i * AssetManager.STYLE_NUM_ENTRIES; |
| 1235 | final int type = data[index + AssetManager.STYLE_TYPE]; |
Alan Viverette | 8e5e11b | 2014-07-07 14:06:15 -0700 | [diff] [blame] | 1236 | if (type == TypedValue.TYPE_NULL) { |
| 1237 | continue; |
| 1238 | } |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 1239 | changingConfig |= ActivityInfo.activityInfoConfigNativeToJava( |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1240 | data[index + AssetManager.STYLE_CHANGING_CONFIGURATIONS]); |
Alan Viverette | 8e5e11b | 2014-07-07 14:06:15 -0700 | [diff] [blame] | 1241 | } |
| 1242 | return changingConfig; |
| 1243 | } |
| 1244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | private boolean getValueAt(int index, TypedValue outValue) { |
| 1246 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1247 | final int type = data[index+AssetManager.STYLE_TYPE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | if (type == TypedValue.TYPE_NULL) { |
| 1249 | return false; |
| 1250 | } |
| 1251 | outValue.type = type; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1252 | outValue.data = data[index+AssetManager.STYLE_DATA]; |
| 1253 | outValue.assetCookie = data[index+AssetManager.STYLE_ASSET_COOKIE]; |
| 1254 | outValue.resourceId = data[index+AssetManager.STYLE_RESOURCE_ID]; |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 1255 | outValue.changingConfigurations = ActivityInfo.activityInfoConfigNativeToJava( |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1256 | data[index + AssetManager.STYLE_CHANGING_CONFIGURATIONS]); |
| 1257 | outValue.density = data[index+AssetManager.STYLE_DENSITY]; |
Gilles Debunne | 7539968 | 2010-08-19 11:26:14 -0700 | [diff] [blame] | 1258 | outValue.string = (type == TypedValue.TYPE_STRING) ? loadStringValueAt(index) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1259 | return true; |
| 1260 | } |
| 1261 | |
| 1262 | private CharSequence loadStringValueAt(int index) { |
| 1263 | final int[] data = mData; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1264 | final int cookie = data[index+AssetManager.STYLE_ASSET_COOKIE]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | if (cookie < 0) { |
| 1266 | if (mXml != null) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1267 | return mXml.getPooledString( |
| 1268 | data[index+AssetManager.STYLE_DATA]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1269 | } |
| 1270 | return null; |
| 1271 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 1272 | return mAssets.getPooledStringForCookie(cookie, data[index+AssetManager.STYLE_DATA]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | } |
| 1274 | |
John Reck | f32adf4 | 2016-11-23 10:39:40 -0800 | [diff] [blame] | 1275 | /** @hide */ |
| 1276 | protected TypedArray(Resources resources) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | mResources = resources; |
Adam Lesinski | fb302cc | 2016-02-29 16:50:38 -0800 | [diff] [blame] | 1278 | mMetrics = mResources.getDisplayMetrics(); |
| 1279 | mAssets = mResources.getAssets(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
Alan Viverette | 562a6a8 | 2014-01-31 11:07:29 -0800 | [diff] [blame] | 1282 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1283 | public String toString() { |
| 1284 | return Arrays.toString(mData); |
| 1285 | } |
Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 1286 | } |