Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view.accessibility; |
| 18 | |
| 19 | import android.os.Parcelable; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
| 23 | |
| 24 | /** |
| 25 | * Represents a record in an accessibility event. This class encapsulates |
| 26 | * the information for a {@link android.view.View}. Note that not all properties |
| 27 | * are applicable to all view types. For detailed information please refer to |
| 28 | * {@link AccessibilityEvent}. |
| 29 | * |
| 30 | * @see AccessibilityEvent |
| 31 | */ |
| 32 | public class AccessibilityRecord { |
| 33 | |
| 34 | private static final int INVALID_POSITION = -1; |
| 35 | |
| 36 | private static final int PROPERTY_CHECKED = 0x00000001; |
| 37 | private static final int PROPERTY_ENABLED = 0x00000002; |
| 38 | private static final int PROPERTY_PASSWORD = 0x00000004; |
| 39 | private static final int PROPERTY_FULL_SCREEN = 0x00000080; |
| 40 | |
| 41 | private static final int MAX_POOL_SIZE = 10; |
Svetoslav Ganov | 887e1a1 | 2011-04-29 15:09:28 -0700 | [diff] [blame] | 42 | private static final Object sPoolLock = new Object(); |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 43 | private static AccessibilityRecord sPool; |
| 44 | private static int sPoolSize; |
| 45 | |
| 46 | private AccessibilityRecord mNext; |
| 47 | private boolean mIsInPool; |
| 48 | |
| 49 | protected int mBooleanProperties; |
| 50 | protected int mCurrentItemIndex; |
| 51 | protected int mItemCount; |
| 52 | protected int mFromIndex; |
| 53 | protected int mAddedCount; |
| 54 | protected int mRemovedCount; |
| 55 | |
| 56 | protected CharSequence mClassName; |
| 57 | protected CharSequence mContentDescription; |
| 58 | protected CharSequence mBeforeText; |
| 59 | protected Parcelable mParcelableData; |
| 60 | |
| 61 | protected final List<CharSequence> mText = new ArrayList<CharSequence>(); |
| 62 | |
| 63 | /* |
| 64 | * Hide constructor. |
| 65 | */ |
| 66 | protected AccessibilityRecord() { |
| 67 | |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Gets if the source is checked. |
| 72 | * |
| 73 | * @return True if the view is checked, false otherwise. |
| 74 | */ |
| 75 | public boolean isChecked() { |
| 76 | return getBooleanProperty(PROPERTY_CHECKED); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sets if the source is checked. |
| 81 | * |
| 82 | * @param isChecked True if the view is checked, false otherwise. |
| 83 | */ |
| 84 | public void setChecked(boolean isChecked) { |
| 85 | setBooleanProperty(PROPERTY_CHECKED, isChecked); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Gets if the source is enabled. |
| 90 | * |
| 91 | * @return True if the view is enabled, false otherwise. |
| 92 | */ |
| 93 | public boolean isEnabled() { |
| 94 | return getBooleanProperty(PROPERTY_ENABLED); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Sets if the source is enabled. |
| 99 | * |
| 100 | * @param isEnabled True if the view is enabled, false otherwise. |
| 101 | */ |
| 102 | public void setEnabled(boolean isEnabled) { |
| 103 | setBooleanProperty(PROPERTY_ENABLED, isEnabled); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Gets if the source is a password field. |
| 108 | * |
| 109 | * @return True if the view is a password field, false otherwise. |
| 110 | */ |
| 111 | public boolean isPassword() { |
| 112 | return getBooleanProperty(PROPERTY_PASSWORD); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Sets if the source is a password field. |
| 117 | * |
| 118 | * @param isPassword True if the view is a password field, false otherwise. |
| 119 | */ |
| 120 | public void setPassword(boolean isPassword) { |
| 121 | setBooleanProperty(PROPERTY_PASSWORD, isPassword); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Sets if the source is taking the entire screen. |
| 126 | * |
| 127 | * @param isFullScreen True if the source is full screen, false otherwise. |
| 128 | */ |
| 129 | public void setFullScreen(boolean isFullScreen) { |
| 130 | setBooleanProperty(PROPERTY_FULL_SCREEN, isFullScreen); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Gets if the source is taking the entire screen. |
| 135 | * |
| 136 | * @return True if the source is full screen, false otherwise. |
| 137 | */ |
| 138 | public boolean isFullScreen() { |
| 139 | return getBooleanProperty(PROPERTY_FULL_SCREEN); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Gets the number of items that can be visited. |
| 144 | * |
| 145 | * @return The number of items. |
| 146 | */ |
| 147 | public int getItemCount() { |
| 148 | return mItemCount; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Sets the number of items that can be visited. |
| 153 | * |
| 154 | * @param itemCount The number of items. |
| 155 | */ |
| 156 | public void setItemCount(int itemCount) { |
| 157 | mItemCount = itemCount; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Gets the index of the source in the list of items the can be visited. |
| 162 | * |
| 163 | * @return The current item index. |
| 164 | */ |
| 165 | public int getCurrentItemIndex() { |
| 166 | return mCurrentItemIndex; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Sets the index of the source in the list of items that can be visited. |
| 171 | * |
| 172 | * @param currentItemIndex The current item index. |
| 173 | */ |
| 174 | public void setCurrentItemIndex(int currentItemIndex) { |
| 175 | mCurrentItemIndex = currentItemIndex; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Gets the index of the first character of the changed sequence. |
| 180 | * |
| 181 | * @return The index of the first character. |
| 182 | */ |
| 183 | public int getFromIndex() { |
| 184 | return mFromIndex; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Sets the index of the first character of the changed sequence. |
| 189 | * |
| 190 | * @param fromIndex The index of the first character. |
| 191 | */ |
| 192 | public void setFromIndex(int fromIndex) { |
| 193 | mFromIndex = fromIndex; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Gets the number of added characters. |
| 198 | * |
| 199 | * @return The number of added characters. |
| 200 | */ |
| 201 | public int getAddedCount() { |
| 202 | return mAddedCount; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Sets the number of added characters. |
| 207 | * |
| 208 | * @param addedCount The number of added characters. |
| 209 | */ |
| 210 | public void setAddedCount(int addedCount) { |
| 211 | mAddedCount = addedCount; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Gets the number of removed characters. |
| 216 | * |
| 217 | * @return The number of removed characters. |
| 218 | */ |
| 219 | public int getRemovedCount() { |
| 220 | return mRemovedCount; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Sets the number of removed characters. |
| 225 | * |
| 226 | * @param removedCount The number of removed characters. |
| 227 | */ |
| 228 | public void setRemovedCount(int removedCount) { |
| 229 | mRemovedCount = removedCount; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Gets the class name of the source. |
| 234 | * |
| 235 | * @return The class name. |
| 236 | */ |
| 237 | public CharSequence getClassName() { |
| 238 | return mClassName; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Sets the class name of the source. |
| 243 | * |
| 244 | * @param className The lass name. |
| 245 | */ |
| 246 | public void setClassName(CharSequence className) { |
| 247 | mClassName = className; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Gets the text of the event. The index in the list represents the priority |
| 252 | * of the text. Specifically, the lower the index the higher the priority. |
| 253 | * |
| 254 | * @return The text. |
| 255 | */ |
| 256 | public List<CharSequence> getText() { |
| 257 | return mText; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Sets the text before a change. |
| 262 | * |
| 263 | * @return The text before the change. |
| 264 | */ |
| 265 | public CharSequence getBeforeText() { |
| 266 | return mBeforeText; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Sets the text before a change. |
| 271 | * |
| 272 | * @param beforeText The text before the change. |
| 273 | */ |
| 274 | public void setBeforeText(CharSequence beforeText) { |
| 275 | mBeforeText = beforeText; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Gets the description of the source. |
| 280 | * |
| 281 | * @return The description. |
| 282 | */ |
| 283 | public CharSequence getContentDescription() { |
| 284 | return mContentDescription; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Sets the description of the source. |
| 289 | * |
| 290 | * @param contentDescription The description. |
| 291 | */ |
| 292 | public void setContentDescription(CharSequence contentDescription) { |
| 293 | mContentDescription = contentDescription; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Gets the {@link Parcelable} data. |
| 298 | * |
| 299 | * @return The parcelable data. |
| 300 | */ |
| 301 | public Parcelable getParcelableData() { |
| 302 | return mParcelableData; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Sets the {@link Parcelable} data of the event. |
| 307 | * |
| 308 | * @param parcelableData The parcelable data. |
| 309 | */ |
| 310 | public void setParcelableData(Parcelable parcelableData) { |
| 311 | mParcelableData = parcelableData; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Gets the value of a boolean property. |
| 316 | * |
| 317 | * @param property The property. |
| 318 | * @return The value. |
| 319 | */ |
| 320 | public boolean getBooleanProperty(int property) { |
| 321 | return (mBooleanProperties & property) == property; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Sets a boolean property. |
| 326 | * |
| 327 | * @param property The property. |
| 328 | * @param value The value. |
| 329 | */ |
| 330 | private void setBooleanProperty(int property, boolean value) { |
| 331 | if (value) { |
| 332 | mBooleanProperties |= property; |
| 333 | } else { |
| 334 | mBooleanProperties &= ~property; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Returns a cached instance if such is available or a new one is |
| 340 | * instantiated. |
| 341 | * |
| 342 | * @return An instance. |
| 343 | */ |
| 344 | protected static AccessibilityRecord obtain() { |
Svetoslav Ganov | 887e1a1 | 2011-04-29 15:09:28 -0700 | [diff] [blame] | 345 | synchronized (sPoolLock) { |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 346 | if (sPool != null) { |
| 347 | AccessibilityRecord record = sPool; |
| 348 | sPool = sPool.mNext; |
| 349 | sPoolSize--; |
| 350 | record.mNext = null; |
| 351 | record.mIsInPool = false; |
| 352 | return record; |
| 353 | } |
| 354 | return new AccessibilityRecord(); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Return an instance back to be reused. |
| 360 | * <p> |
| 361 | * <b>Note: You must not touch the object after calling this function.</b> |
Svetoslav Ganov | 887e1a1 | 2011-04-29 15:09:28 -0700 | [diff] [blame] | 362 | * |
| 363 | * @throws IllegalStateException If the record is already recycled. |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 364 | */ |
| 365 | public void recycle() { |
| 366 | if (mIsInPool) { |
Svetoslav Ganov | 887e1a1 | 2011-04-29 15:09:28 -0700 | [diff] [blame] | 367 | throw new IllegalStateException("Record already recycled!"); |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 368 | } |
| 369 | clear(); |
Svetoslav Ganov | 887e1a1 | 2011-04-29 15:09:28 -0700 | [diff] [blame] | 370 | synchronized (sPoolLock) { |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 371 | if (sPoolSize <= MAX_POOL_SIZE) { |
| 372 | mNext = sPool; |
| 373 | sPool = this; |
| 374 | mIsInPool = true; |
| 375 | sPoolSize++; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Clears the state of this instance. |
| 382 | */ |
| 383 | protected void clear() { |
| 384 | mBooleanProperties = 0; |
| 385 | mCurrentItemIndex = INVALID_POSITION; |
| 386 | mItemCount = 0; |
| 387 | mFromIndex = 0; |
| 388 | mAddedCount = 0; |
| 389 | mRemovedCount = 0; |
| 390 | mClassName = null; |
| 391 | mContentDescription = null; |
| 392 | mBeforeText = null; |
| 393 | mParcelableData = null; |
| 394 | mText.clear(); |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public String toString() { |
| 399 | StringBuilder builder = new StringBuilder(); |
| 400 | builder.append(" [ ClassName: " + mClassName); |
| 401 | builder.append("; Text: " + mText); |
| 402 | builder.append("; ContentDescription: " + mContentDescription); |
| 403 | builder.append("; ItemCount: " + mItemCount); |
| 404 | builder.append("; CurrentItemIndex: " + mCurrentItemIndex); |
| 405 | builder.append("; IsEnabled: " + getBooleanProperty(PROPERTY_ENABLED)); |
| 406 | builder.append("; IsPassword: " + getBooleanProperty(PROPERTY_PASSWORD)); |
| 407 | builder.append("; IsChecked: " + getBooleanProperty(PROPERTY_CHECKED)); |
| 408 | builder.append("; IsFullScreen: " + getBooleanProperty(PROPERTY_FULL_SCREEN)); |
| 409 | builder.append("; BeforeText: " + mBeforeText); |
| 410 | builder.append("; FromIndex: " + mFromIndex); |
| 411 | builder.append("; AddedCount: " + mAddedCount); |
| 412 | builder.append("; RemovedCount: " + mRemovedCount); |
| 413 | builder.append("; ParcelableData: " + mParcelableData); |
| 414 | builder.append(" ]"); |
| 415 | return builder.toString(); |
| 416 | } |
| 417 | } |