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