AccessibilityRecord.java revision 736c2756bf3c14ae9fef7255c119057f7a2be1ed
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 17package android.view.accessibility; 18 19import android.os.Parcelable; 20 21import java.util.ArrayList; 22import 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 */ 32public 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; 42 private static final Object mPoolLock = new Object(); 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() { 345 synchronized (mPoolLock) { 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> 362 */ 363 public void recycle() { 364 if (mIsInPool) { 365 return; 366 } 367 clear(); 368 synchronized (mPoolLock) { 369 if (sPoolSize <= MAX_POOL_SIZE) { 370 mNext = sPool; 371 sPool = this; 372 mIsInPool = true; 373 sPoolSize++; 374 } 375 } 376 } 377 378 /** 379 * Clears the state of this instance. 380 */ 381 protected void clear() { 382 mBooleanProperties = 0; 383 mCurrentItemIndex = INVALID_POSITION; 384 mItemCount = 0; 385 mFromIndex = 0; 386 mAddedCount = 0; 387 mRemovedCount = 0; 388 mClassName = null; 389 mContentDescription = null; 390 mBeforeText = null; 391 mParcelableData = null; 392 mText.clear(); 393 } 394 395 @Override 396 public String toString() { 397 StringBuilder builder = new StringBuilder(); 398 builder.append(" [ ClassName: " + mClassName); 399 builder.append("; Text: " + mText); 400 builder.append("; ContentDescription: " + mContentDescription); 401 builder.append("; ItemCount: " + mItemCount); 402 builder.append("; CurrentItemIndex: " + mCurrentItemIndex); 403 builder.append("; IsEnabled: " + getBooleanProperty(PROPERTY_ENABLED)); 404 builder.append("; IsPassword: " + getBooleanProperty(PROPERTY_PASSWORD)); 405 builder.append("; IsChecked: " + getBooleanProperty(PROPERTY_CHECKED)); 406 builder.append("; IsFullScreen: " + getBooleanProperty(PROPERTY_FULL_SCREEN)); 407 builder.append("; BeforeText: " + mBeforeText); 408 builder.append("; FromIndex: " + mFromIndex); 409 builder.append("; AddedCount: " + mAddedCount); 410 builder.append("; RemovedCount: " + mRemovedCount); 411 builder.append("; ParcelableData: " + mParcelableData); 412 builder.append(" ]"); 413 return builder.toString(); 414 } 415} 416