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