Home | History | Annotate | Download | only in launcher3
      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 
     17 package com.android.launcher3;
     18 
     19 import android.view.View;
     20 import android.view.ViewGroup;
     21 import android.view.ViewGroup.OnHierarchyChangeListener;
     22 
     23 import java.util.HashMap;
     24 
     25 public 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         Integer important = mPreviousValues.get(v);
     68         v.setImportantForAccessibility(important);
     69         mPreviousValues.remove(v);
     70 
     71         // Call method on children recursively
     72         if (v instanceof ViewGroup) {
     73             ViewGroup vg = (ViewGroup) v;
     74 
     75             // We assume if a class implements OnHierarchyChangeListener, it listens
     76             // to changes to any of its children (happens to be the case in Launcher)
     77             if (vg instanceof OnHierarchyChangeListener) {
     78                 vg.setOnHierarchyChangeListener((OnHierarchyChangeListener) vg);
     79             } else {
     80                 vg.setOnHierarchyChangeListener(null);
     81             }
     82             for (int i = 0; i < vg.getChildCount(); i++) {
     83                 View child = vg.getChildAt(i);
     84                 if (includeView(child)) {
     85                     restoreImportantForAccessibilityHelper(child);
     86                 }
     87             }
     88         }
     89     }
     90 
     91     public void onChildViewAdded(View parent, View child) {
     92         if (mHide && includeView(child)) {
     93             setImportantForAccessibilityToNoHelper(child);
     94         }
     95     }
     96 
     97     public void onChildViewRemoved(View parent, View child) {
     98         if (mHide && includeView(child)) {
     99             restoreImportantForAccessibilityHelper(child);
    100         }
    101     }
    102 
    103     private boolean includeView(View v) {
    104         return !hasAncestorOfType(v, Cling.class) &&
    105                 (!mOnlyAllApps || hasAncestorOfType(v, AppsCustomizeTabHost.class));
    106     }
    107 
    108     private boolean hasAncestorOfType(View v, Class c) {
    109         return v != null &&
    110                 (v.getClass().equals(c) ||
    111                  (v.getParent() instanceof ViewGroup &&
    112                   hasAncestorOfType((ViewGroup) v.getParent(), c)));
    113     }
    114 }