AccessibilityRecordCompat.java revision ac5fe7c617c66850fff75a9fce9979c6e5674b0f
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 androidx.core.view.accessibility;
18
19import android.os.Build;
20import android.os.Parcelable;
21import androidx.annotation.NonNull;
22import android.view.View;
23import android.view.accessibility.AccessibilityEvent;
24import android.view.accessibility.AccessibilityRecord;
25
26import java.util.List;
27
28/**
29 * Helper for accessing {@link AccessibilityRecord}.
30 */
31public class AccessibilityRecordCompat {
32    private final AccessibilityRecord mRecord;
33
34    /**
35     * @deprecated This is not type safe. If you want to modify an
36     * {@link AccessibilityEvent}'s properties defined in
37     * {@link android.view.accessibility.AccessibilityRecord} use
38     * {@link AccessibilityEventCompat#asRecord(AccessibilityEvent)}. This method will be removed
39     * in a subsequent release of the support library.
40     */
41    @Deprecated
42    public AccessibilityRecordCompat(Object record) {
43        mRecord = (AccessibilityRecord) record;
44    }
45
46    /**
47     * @return The wrapped implementation.
48     *
49     * @deprecated This method will be removed in a subsequent release of
50     * the support library.
51     */
52    @Deprecated
53    public Object getImpl() {
54        return mRecord;
55    }
56
57    /**
58     * Returns a cached instance if such is available or a new one is
59     * instantiated. The instance is initialized with data from the
60     * given record.
61     *
62     * @return An instance.
63     *
64     * @deprecated Use {@link AccessibilityRecord#obtain(AccessibilityRecord)} directly.
65     */
66    @Deprecated
67    public static AccessibilityRecordCompat obtain(AccessibilityRecordCompat record) {
68        return new AccessibilityRecordCompat(AccessibilityRecord.obtain(record.mRecord));
69    }
70
71    /**
72     * Returns a cached instance if such is available or a new one is
73     * instantiated.
74     *
75     * @return An instance.
76     *
77     * @deprecated Use {@link AccessibilityRecord#obtain()} directly.
78     */
79    @Deprecated
80    public static AccessibilityRecordCompat obtain() {
81        return new AccessibilityRecordCompat(AccessibilityRecord.obtain());
82    }
83
84    /**
85     * Sets the event source.
86     *
87     * @param source The source.
88     *
89     * @throws IllegalStateException If called from an AccessibilityService.
90     *
91     * @deprecated Use {@link AccessibilityRecord#setSource(View)} directly.
92     */
93    @Deprecated
94    public void setSource(View source) {
95        mRecord.setSource(source);
96    }
97
98    /**
99     * Sets the source to be a virtual descendant of the given <code>root</code>.
100     * If <code>virtualDescendantId</code> equals to {@link View#NO_ID} the root
101     * is set as the source.
102     * <p>
103     * A virtual descendant is an imaginary View that is reported as a part of the view
104     * hierarchy for accessibility purposes. This enables custom views that draw complex
105     * content to report them selves as a tree of virtual views, thus conveying their
106     * logical structure.
107     * </p>
108     *
109     * @param root The root of the virtual subtree.
110     * @param virtualDescendantId The id of the virtual descendant.
111     *
112     * @deprecated Use {@link #setSource(AccessibilityRecord, View, int)} instead.
113     */
114    @Deprecated
115    public void setSource(View root, int virtualDescendantId) {
116        AccessibilityRecordCompat.setSource(mRecord, root, virtualDescendantId);
117    }
118
119    /**
120     * Sets the source to be a virtual descendant of the given <code>root</code>.
121     * If <code>virtualDescendantId</code> equals to {@link View#NO_ID} the root
122     * is set as the source.
123     * <p>
124     * A virtual descendant is an imaginary View that is reported as a part of the view
125     * hierarchy for accessibility purposes. This enables custom views that draw complex
126     * content to report them selves as a tree of virtual views, thus conveying their
127     * logical structure.
128     * </p>
129     *
130     * @param record The {@link AccessibilityRecord} instance to use.
131     * @param root The root of the virtual subtree.
132     * @param virtualDescendantId The id of the virtual descendant.
133     */
134    public static void setSource(@NonNull AccessibilityRecord record, View root,
135            int virtualDescendantId) {
136        if (Build.VERSION.SDK_INT >= 16) {
137            record.setSource(root, virtualDescendantId);
138        }
139    }
140
141    /**
142     * Gets the {@link android.view.accessibility.AccessibilityNodeInfo} of
143     * the event source.
144     * <p>
145     * <strong>Note:</strong> It is a client responsibility to recycle the
146     * received info by calling
147     * {@link android.view.accessibility.AccessibilityNodeInfo#recycle()
148     * AccessibilityNodeInfo#recycle()} to avoid creating of multiple instances.
149     *</p>
150     *
151     * @return The info of the source.
152     *
153     * @deprecated Use {@link AccessibilityRecord#getSource()} directly.
154     */
155    @Deprecated
156    public AccessibilityNodeInfoCompat getSource() {
157        return AccessibilityNodeInfoCompat.wrapNonNullInstance(mRecord.getSource());
158    }
159
160    /**
161     * Gets the id of the window from which the event comes from.
162     *
163     * @return The window id.
164     *
165     * @deprecated Use {@link AccessibilityRecord#getWindowId()} directly.
166     */
167    @Deprecated
168    public int getWindowId() {
169        return mRecord.getWindowId();
170    }
171
172    /**
173     * Gets if the source is checked.
174     *
175     * @return True if the view is checked, false otherwise.
176     *
177     * @deprecated Use {@link AccessibilityRecord#isChecked()} directly.
178     */
179    @Deprecated
180    public boolean isChecked() {
181        return mRecord.isChecked();
182    }
183
184    /**
185     * Sets if the source is checked.
186     *
187     * @param isChecked True if the view is checked, false otherwise.
188     *
189     * @throws IllegalStateException If called from an AccessibilityService.
190     *
191     * @deprecated Use {@link AccessibilityRecord#setChecked(boolean)} directly.
192     */
193    @Deprecated
194    public void setChecked(boolean isChecked) {
195        mRecord.setChecked(isChecked);
196    }
197
198    /**
199     * Gets if the source is enabled.
200     *
201     * @return True if the view is enabled, false otherwise.
202     *
203     * @deprecated Use {@link AccessibilityRecord#isEnabled()} directly.
204     */
205    @Deprecated
206    public boolean isEnabled() {
207        return mRecord.isEnabled();
208    }
209
210    /**
211     * Sets if the source is enabled.
212     *
213     * @param isEnabled True if the view is enabled, false otherwise.
214     *
215     * @throws IllegalStateException If called from an AccessibilityService.
216     *
217     * @deprecated Use {@link AccessibilityRecord#isEnabled()} directly.
218     */
219    @Deprecated
220    public void setEnabled(boolean isEnabled) {
221        mRecord.setEnabled(isEnabled);
222    }
223
224    /**
225     * Gets if the source is a password field.
226     *
227     * @return True if the view is a password field, false otherwise.
228     *
229     * @deprecated Use {@link AccessibilityRecord#isPassword()} directly.
230     */
231    @Deprecated
232    public boolean isPassword() {
233        return mRecord.isPassword();
234    }
235
236    /**
237     * Sets if the source is a password field.
238     *
239     * @param isPassword True if the view is a password field, false otherwise.
240     *
241     * @throws IllegalStateException If called from an AccessibilityService.
242     *
243     * @deprecated Use {@link AccessibilityRecord#setPassword(boolean)} directly.
244     */
245    @Deprecated
246    public void setPassword(boolean isPassword) {
247        mRecord.setPassword(isPassword);
248    }
249
250    /**
251     * Gets if the source is taking the entire screen.
252     *
253     * @return True if the source is full screen, false otherwise.
254     *
255     * @deprecated Use {@link AccessibilityRecord#isFullScreen()} directly.
256     */
257    @Deprecated
258    public boolean isFullScreen() {
259        return mRecord.isFullScreen();
260    }
261
262    /**
263     * Sets if the source is taking the entire screen.
264     *
265     * @param isFullScreen True if the source is full screen, false otherwise.
266     *
267     * @throws IllegalStateException If called from an AccessibilityService.
268     *
269     * @deprecated Use {@link AccessibilityRecord#setFullScreen(boolean)} directly.
270     */
271    @Deprecated
272    public void setFullScreen(boolean isFullScreen) {
273        mRecord.setFullScreen(isFullScreen);
274    }
275
276    /**
277     * Gets if the source is scrollable.
278     *
279     * @return True if the source is scrollable, false otherwise.
280     *
281     * @deprecated Use {@link AccessibilityRecord#isScrollable()} directly.
282     */
283    @Deprecated
284    public boolean isScrollable() {
285        return mRecord.isScrollable();
286    }
287
288    /**
289     * Sets if the source is scrollable.
290     *
291     * @param scrollable True if the source is scrollable, false otherwise.
292     *
293     * @throws IllegalStateException If called from an AccessibilityService.
294     *
295     * @deprecated Use {@link AccessibilityRecord#setScrollable(boolean)} directly.
296     */
297    @Deprecated
298    public void setScrollable(boolean scrollable) {
299        mRecord.setScrollable(scrollable);
300    }
301
302    /**
303     * Gets the number of items that can be visited.
304     *
305     * @return The number of items.
306     *
307     * @deprecated Use {@link AccessibilityRecord#getItemCount()} directly.
308     */
309    @Deprecated
310    public int getItemCount() {
311        return mRecord.getItemCount();
312    }
313
314    /**
315     * Sets the number of items that can be visited.
316     *
317     * @param itemCount The number of items.
318     *
319     * @throws IllegalStateException If called from an AccessibilityService.
320     *
321     * @deprecated Use {@link AccessibilityRecord#setItemCount(int)} directly.
322     */
323    @Deprecated
324    public void setItemCount(int itemCount) {
325        mRecord.setItemCount(itemCount);
326    }
327
328    /**
329     * Gets the index of the source in the list of items the can be visited.
330     *
331     * @return The current item index.
332     *
333     * @deprecated Use {@link AccessibilityRecord#getCurrentItemIndex()} directly.
334     */
335    @Deprecated
336    public int getCurrentItemIndex() {
337        return mRecord.getCurrentItemIndex();
338    }
339
340    /**
341     * Sets the index of the source in the list of items that can be visited.
342     *
343     * @param currentItemIndex The current item index.
344     *
345     * @throws IllegalStateException If called from an AccessibilityService.
346     *
347     * @deprecated Use {@link AccessibilityRecord#setCurrentItemIndex(int)} directly.
348     */
349    @Deprecated
350    public void setCurrentItemIndex(int currentItemIndex) {
351        mRecord.setCurrentItemIndex(currentItemIndex);
352    }
353
354    /**
355     * Gets the index of the first character of the changed sequence,
356     * or the beginning of a text selection or the index of the first
357     * visible item when scrolling.
358     *
359     * @return The index of the first character or selection
360     *        start or the first visible item.
361     *
362     * @deprecated Use {@link AccessibilityRecord#getFromIndex()} directly.
363     */
364    @Deprecated
365    public int getFromIndex() {
366        return mRecord.getFromIndex();
367    }
368
369    /**
370     * Sets the index of the first character of the changed sequence
371     * or the beginning of a text selection or the index of the first
372     * visible item when scrolling.
373     *
374     * @param fromIndex The index of the first character or selection
375     *        start or the first visible item.
376     *
377     * @throws IllegalStateException If called from an AccessibilityService.
378     *
379     * @deprecated Use {@link AccessibilityRecord#setFromIndex(int)} directly.
380     */
381    @Deprecated
382    public void setFromIndex(int fromIndex) {
383        mRecord.setFromIndex(fromIndex);
384    }
385
386    /**
387     * Gets the index of text selection end or the index of the last
388     * visible item when scrolling.
389     *
390     * @return The index of selection end or last item index.
391     *
392     * @deprecated Use {@link AccessibilityRecord#getToIndex()} directly.
393     */
394    @Deprecated
395    public int getToIndex() {
396        return mRecord.getToIndex();
397    }
398
399    /**
400     * Sets the index of text selection end or the index of the last
401     * visible item when scrolling.
402     *
403     * @param toIndex The index of selection end or last item index.
404     *
405     * @deprecated Use {@link AccessibilityRecord#setToIndex(int)} directly.
406     */
407    @Deprecated
408    public void setToIndex(int toIndex) {
409        mRecord.setToIndex(toIndex);
410    }
411
412    /**
413     * Gets the scroll offset of the source left edge in pixels.
414     *
415     * @return The scroll.
416     *
417     * @deprecated Use {@link AccessibilityRecord#getScrollX()} directly.
418     */
419    @Deprecated
420    public int getScrollX() {
421        return mRecord.getScrollX();
422    }
423
424    /**
425     * Sets the scroll offset of the source left edge in pixels.
426     *
427     * @param scrollX The scroll.
428     *
429     * @deprecated Use {@link AccessibilityRecord#setScrollX(int)} directly.
430     */
431    @Deprecated
432    public void setScrollX(int scrollX) {
433        mRecord.setScrollX(scrollX);
434    }
435
436    /**
437     * Gets the scroll offset of the source top edge in pixels.
438     *
439     * @return The scroll.
440     *
441     * @deprecated Use {@link AccessibilityRecord#getScrollY()} directly.
442     */
443    @Deprecated
444    public int getScrollY() {
445        return mRecord.getScrollY();
446    }
447
448    /**
449     * Sets the scroll offset of the source top edge in pixels.
450     *
451     * @param scrollY The scroll.
452     *
453     * @deprecated Use {@link AccessibilityRecord#setScrollY(int)} directly.
454     */
455    @Deprecated
456    public void setScrollY(int scrollY) {
457        mRecord.setScrollY(scrollY);
458    }
459
460    /**
461     * Gets the max scroll offset of the source left edge in pixels.
462     *
463     * @return The max scroll.
464     *
465     * @deprecated Use {@link #getMaxScrollX(AccessibilityRecord)} instead.
466     */
467    @Deprecated
468    public int getMaxScrollX() {
469        return AccessibilityRecordCompat.getMaxScrollX(mRecord);
470    }
471
472    /**
473     * Gets the max scroll offset of the source left edge in pixels.
474     *
475     * @param record The {@link AccessibilityRecord} instance to use.
476     * @return The max scroll.
477     */
478    public static int getMaxScrollX(AccessibilityRecord record) {
479        if (Build.VERSION.SDK_INT >= 15) {
480            return record.getMaxScrollX();
481        } else {
482            return 0;
483        }
484    }
485
486    /**
487     * Sets the max scroll offset of the source left edge in pixels.
488     *
489     * @param maxScrollX The max scroll.
490     *
491     * @deprecated Use {@link #setMaxScrollX(AccessibilityRecord, int)} instead.
492     */
493    @Deprecated
494    public void setMaxScrollX(int maxScrollX) {
495        AccessibilityRecordCompat.setMaxScrollX(mRecord, maxScrollX);
496    }
497
498    /**
499     * Sets the max scroll offset of the source left edge in pixels.
500     *
501     * @param record The {@link AccessibilityRecord} instance to use.
502     * @param maxScrollX The max scroll.
503     */
504    public static void setMaxScrollX(AccessibilityRecord record, int maxScrollX) {
505        if (Build.VERSION.SDK_INT >= 15) {
506            record.setMaxScrollX(maxScrollX);
507        }
508    }
509
510    /**
511     * Gets the max scroll offset of the source top edge in pixels.
512     *
513     * @return The max scroll.
514     *
515     * @deprecated Use {@link #getMaxScrollY(AccessibilityRecord)} instead.
516     */
517    @Deprecated
518    public int getMaxScrollY() {
519        return AccessibilityRecordCompat.getMaxScrollY(mRecord);
520    }
521
522    /**
523     * Gets the max scroll offset of the source top edge in pixels.
524     *
525     * @param record The {@link AccessibilityRecord} instance to use.
526     * @return The max scroll.
527     */
528    public static int getMaxScrollY(AccessibilityRecord record) {
529        if (Build.VERSION.SDK_INT >= 15) {
530            return record.getMaxScrollY();
531        } else {
532            return 0;
533        }
534    }
535
536    /**
537     * Sets the max scroll offset of the source top edge in pixels.
538     *
539     * @param maxScrollY The max scroll.
540     *
541     * @deprecated Use {@link #setMaxScrollY(AccessibilityRecord, int)} instead.
542     */
543    @Deprecated
544    public void setMaxScrollY(int maxScrollY) {
545        AccessibilityRecordCompat.setMaxScrollY(mRecord, maxScrollY);
546    }
547
548    /**
549     * Sets the max scroll offset of the source top edge in pixels.
550     *
551     * @param record The {@link AccessibilityRecord} instance to use.
552     * @param maxScrollY The max scroll.
553     */
554    public static void setMaxScrollY(AccessibilityRecord record, int maxScrollY) {
555        if (Build.VERSION.SDK_INT >= 15) {
556            record.setMaxScrollY(maxScrollY);
557        }
558    }
559
560    /**
561     * Gets the number of added characters.
562     *
563     * @return The number of added characters.
564     *
565     * @deprecated Use {@link AccessibilityRecord#getAddedCount()} directly.
566     */
567    @Deprecated
568    public int getAddedCount() {
569        return mRecord.getAddedCount();
570    }
571
572    /**
573     * Sets the number of added characters.
574     *
575     * @param addedCount The number of added characters.
576     *
577     * @throws IllegalStateException If called from an AccessibilityService.
578     *
579     * @deprecated Use {@link AccessibilityRecord#setAddedCount(int)} directly.
580     */
581    @Deprecated
582    public void setAddedCount(int addedCount) {
583        mRecord.setAddedCount(addedCount);
584    }
585
586    /**
587     * Gets the number of removed characters.
588     *
589     * @return The number of removed characters.
590     *
591     * @deprecated Use {@link AccessibilityRecord#getRemovedCount()} directly.
592     */
593    @Deprecated
594    public int getRemovedCount() {
595        return mRecord.getRemovedCount();
596    }
597
598    /**
599     * Sets the number of removed characters.
600     *
601     * @param removedCount The number of removed characters.
602     *
603     * @throws IllegalStateException If called from an AccessibilityService.
604     *
605     * @deprecated Use {@link AccessibilityRecord#setRemovedCount(int)} directly.
606     */
607    @Deprecated
608    public void setRemovedCount(int removedCount) {
609        mRecord.setRemovedCount(removedCount);
610    }
611
612    /**
613     * Gets the class name of the source.
614     *
615     * @return The class name.
616     *
617     * @deprecated Use {@link AccessibilityRecord#getClassName()} directly.
618     */
619    @Deprecated
620    public CharSequence getClassName() {
621        return mRecord.getClassName();
622    }
623
624    /**
625     * Sets the class name of the source.
626     *
627     * @param className The lass name.
628     *
629     * @throws IllegalStateException If called from an AccessibilityService.
630     *
631     * @deprecated Use {@link AccessibilityRecord#setClassName(CharSequence)} directly.
632     */
633    @Deprecated
634    public void setClassName(CharSequence className) {
635        mRecord.setClassName(className);
636    }
637
638    /**
639     * Gets the text of the event. The index in the list represents the priority
640     * of the text. Specifically, the lower the index the higher the priority.
641     *
642     * @return The text.
643     *
644     * @deprecated Use {@link AccessibilityRecord#getText()} directly.
645     */
646    @Deprecated
647    public List<CharSequence> getText() {
648        return mRecord.getText();
649    }
650
651    /**
652     * Sets the text before a change.
653     *
654     * @return The text before the change.
655     *
656     * @deprecated Use {@link AccessibilityRecord#getBeforeText()} directly.
657     */
658    @Deprecated
659    public CharSequence getBeforeText() {
660        return mRecord.getBeforeText();
661    }
662
663    /**
664     * Sets the text before a change.
665     *
666     * @param beforeText The text before the change.
667     *
668     * @throws IllegalStateException If called from an AccessibilityService.
669     *
670     * @deprecated Use {@link AccessibilityRecord#setBeforeText(CharSequence)} directly.
671     */
672    @Deprecated
673    public void setBeforeText(CharSequence beforeText) {
674        mRecord.setBeforeText(beforeText);
675    }
676
677    /**
678     * Gets the description of the source.
679     *
680     * @return The description.
681     *
682     * @deprecated Use {@link AccessibilityRecord#getContentDescription()} directly.
683     */
684    @Deprecated
685    public CharSequence getContentDescription() {
686        return mRecord.getContentDescription();
687    }
688
689    /**
690     * Sets the description of the source.
691     *
692     * @param contentDescription The description.
693     *
694     * @throws IllegalStateException If called from an AccessibilityService.
695     *
696     * @deprecated Use {@link AccessibilityRecord#setContentDescription(CharSequence)} directly.
697     */
698    @Deprecated
699    public void setContentDescription(CharSequence contentDescription) {
700        mRecord.setContentDescription(contentDescription);
701    }
702
703    /**
704     * Gets the {@link Parcelable} data.
705     *
706     * @return The parcelable data.
707     *
708     * @deprecated Use {@link AccessibilityRecord#getParcelableData()} directly.
709     */
710    @Deprecated
711    public Parcelable getParcelableData() {
712        return mRecord.getParcelableData();
713    }
714
715    /**
716     * Sets the {@link Parcelable} data of the event.
717     *
718     * @param parcelableData The parcelable data.
719     *
720     * @throws IllegalStateException If called from an AccessibilityService.
721     *
722     * @deprecated Use {@link AccessibilityRecord#setParcelableData(Parcelable)} directly.
723     */
724    @Deprecated
725    public void setParcelableData(Parcelable parcelableData) {
726        mRecord.setParcelableData(parcelableData);
727    }
728
729    /**
730     * Return an instance back to be reused.
731     * <p>
732     * <strong>Note:</strong> You must not touch the object after calling this
733     * function.
734     * </p>
735     *
736     * @throws IllegalStateException If the record is already recycled.
737     *
738     * @deprecated Use {@link AccessibilityRecord#recycle()} directly.
739     */
740    @Deprecated
741    public void recycle() {
742        mRecord.recycle();
743    }
744
745    /**
746     * @deprecated Use {@link AccessibilityRecord#hashCode()} directly.
747     */
748    @Deprecated
749    @Override
750    public int hashCode() {
751        return (mRecord == null) ? 0 : mRecord.hashCode();
752    }
753
754    /**
755     * @deprecated Use {@link AccessibilityRecord} directly.
756     */
757    @Deprecated
758    @Override
759    public boolean equals(Object obj) {
760        if (this == obj) {
761            return true;
762        }
763        if (obj == null) {
764            return false;
765        }
766        if (getClass() != obj.getClass()) {
767            return false;
768        }
769        AccessibilityRecordCompat other = (AccessibilityRecordCompat) obj;
770        if (mRecord == null) {
771            if (other.mRecord != null) {
772                return false;
773            }
774        } else if (!mRecord.equals(other.mRecord)) {
775            return false;
776        }
777        return true;
778    }
779}
780