blob: 0f5e950582dd7e04b22c1435aadccb0cb26979e2 [file] [log] [blame]
Qasid Ahmad Sadiq834787a2019-01-18 00:01:54 -08001/*
2 * Copyright (C) 2019 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.util.SparseArray;
20import android.view.View;
21
22/** @hide */
23public final class AccessibilityNodeIdManager {
24 private SparseArray<View> mIdsToViews = new SparseArray<>();
25 private static AccessibilityNodeIdManager sIdManager;
26
27 /**
28 * Gets singleton.
29 * @return The instance.
30 */
31 public static synchronized AccessibilityNodeIdManager getInstance() {
32 if (sIdManager == null) {
33 sIdManager = new AccessibilityNodeIdManager();
34 }
35 return sIdManager;
36 }
37
38 private AccessibilityNodeIdManager() {
39 }
40
41 /**
42 * Register view to be kept track of by the accessibility system.
43 * Must be paired with unregisterView, otherwise this will leak.
44 * @param view The view to be registered.
45 * @param id The accessibilityViewId of the view.
46 */
47 public void registerViewWithId(View view, int id) {
Qasid Ahmad Sadiq1aa9da42019-02-27 18:26:34 -080048 synchronized (mIdsToViews) {
49 mIdsToViews.append(id, view);
50 }
Qasid Ahmad Sadiq834787a2019-01-18 00:01:54 -080051 }
52
53 /**
54 * Unregister view, accessibility won't keep track of this view after this call.
55 * @param id The id returned from registerView when the view as first associated.
56 */
57 public void unregisterViewWithId(int id) {
Qasid Ahmad Sadiq1aa9da42019-02-27 18:26:34 -080058 synchronized (mIdsToViews) {
59 mIdsToViews.remove(id);
60 }
Qasid Ahmad Sadiq834787a2019-01-18 00:01:54 -080061 }
62
63 /**
64 * Accessibility uses this to find the view in the hierarchy.
65 * @param id The accessibility view id.
66 * @return The view.
67 */
68 public View findView(int id) {
Qasid Ahmad Sadiq1aa9da42019-02-27 18:26:34 -080069 View view = null;
70 synchronized (mIdsToViews) {
71 view = mIdsToViews.get(id);
72 }
Qasid Ahmad Sadiq834787a2019-01-18 00:01:54 -080073 return view != null && view.includeForAccessibility() ? view : null;
74 }
75}