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