1/*
2 * Copyright (C) 2012 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 com.android.launcher2;
18
19import android.view.View;
20import android.view.ViewGroup;
21import android.view.ViewGroup.OnHierarchyChangeListener;
22
23import java.util.HashMap;
24
25public class HideFromAccessibilityHelper implements OnHierarchyChangeListener {
26    private HashMap<View, Integer> mPreviousValues;
27    boolean mHide;
28    boolean mOnlyAllApps;
29
30    public HideFromAccessibilityHelper() {
31        mPreviousValues = new HashMap<View, Integer>();
32        mHide = false;
33    }
34
35    public void setImportantForAccessibilityToNo(View v, boolean onlyAllApps) {
36        mOnlyAllApps = onlyAllApps;
37        setImportantForAccessibilityToNoHelper(v);
38        mHide = true;
39    }
40
41    private void setImportantForAccessibilityToNoHelper(View v) {
42        mPreviousValues.put(v, v.getImportantForAccessibility());
43        v.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
44
45        // Call method on children recursively
46        if (v instanceof ViewGroup) {
47            ViewGroup vg = (ViewGroup) v;
48            vg.setOnHierarchyChangeListener(this);
49            for (int i = 0; i < vg.getChildCount(); i++) {
50                View child = vg.getChildAt(i);
51
52                if (includeView(child)) {
53                    setImportantForAccessibilityToNoHelper(child);
54                }
55            }
56        }
57    }
58
59    public void restoreImportantForAccessibility(View v) {
60        if (mHide) {
61            restoreImportantForAccessibilityHelper(v);
62        }
63        mHide = false;
64    }
65
66    private void restoreImportantForAccessibilityHelper(View v) {
67        v.setImportantForAccessibility(mPreviousValues.get(v));
68        mPreviousValues.remove(v);
69
70        // Call method on children recursively
71        if (v instanceof ViewGroup) {
72            ViewGroup vg = (ViewGroup) v;
73
74            // We assume if a class implements OnHierarchyChangeListener, it listens
75            // to changes to any of its children (happens to be the case in Launcher)
76            if (vg instanceof OnHierarchyChangeListener) {
77                vg.setOnHierarchyChangeListener((OnHierarchyChangeListener) vg);
78            } else {
79                vg.setOnHierarchyChangeListener(null);
80            }
81            for (int i = 0; i < vg.getChildCount(); i++) {
82                View child = vg.getChildAt(i);
83                if (includeView(child)) {
84                    restoreImportantForAccessibilityHelper(child);
85                }
86            }
87        }
88    }
89
90    public void onChildViewAdded(View parent, View child) {
91        if (mHide && includeView(child)) {
92            setImportantForAccessibilityToNoHelper(child);
93        }
94    }
95
96    public void onChildViewRemoved(View parent, View child) {
97        if (mHide && includeView(child)) {
98            restoreImportantForAccessibilityHelper(child);
99        }
100    }
101
102    private boolean includeView(View v) {
103        return !hasAncestorOfType(v, Cling.class) &&
104                (!mOnlyAllApps || hasAncestorOfType(v, AppsCustomizeTabHost.class));
105    }
106
107    private boolean hasAncestorOfType(View v, Class c) {
108        return v != null &&
109                (v.getClass().equals(c) ||
110                 (v.getParent() instanceof ViewGroup &&
111                  hasAncestorOfType((ViewGroup) v.getParent(), c)));
112    }
113}