• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.accessibility;
18 
19 import android.view.View;
20 import android.view.ViewGroup;
21 import android.view.ViewGroup.OnHierarchyChangeListener;
22 
23 import com.android.launcher3.CellLayout;
24 import com.android.launcher3.DropTarget.DragObject;
25 import com.android.launcher3.Launcher;
26 import com.android.launcher3.dragndrop.DragController.DragListener;
27 import com.android.launcher3.dragndrop.DragOptions;
28 
29 import java.util.function.Function;
30 
31 /**
32  * Utility listener to enable/disable accessibility drag flags for a ViewGroup
33  * containing CellLayouts
34  */
35 public class AccessibleDragListenerAdapter implements DragListener, OnHierarchyChangeListener {
36 
37     private final ViewGroup mViewGroup;
38     private final Function<CellLayout, DragAndDropAccessibilityDelegate> mDelegateFactory;
39 
40     /**
41      * @param parent the viewgroup containing all the children
42      * @param delegateFactory function to create no delegates
43      */
AccessibleDragListenerAdapter(ViewGroup parent, Function<CellLayout, DragAndDropAccessibilityDelegate> delegateFactory)44     public AccessibleDragListenerAdapter(ViewGroup parent,
45             Function<CellLayout, DragAndDropAccessibilityDelegate> delegateFactory) {
46         mViewGroup = parent;
47         mDelegateFactory = delegateFactory;
48     }
49 
50     @Override
onDragStart(DragObject dragObject, DragOptions options)51     public void onDragStart(DragObject dragObject, DragOptions options) {
52         mViewGroup.setOnHierarchyChangeListener(this);
53         enableAccessibleDrag(true);
54     }
55 
56     @Override
onDragEnd()57     public void onDragEnd() {
58         mViewGroup.setOnHierarchyChangeListener(null);
59         enableAccessibleDrag(false);
60         Launcher.getLauncher(mViewGroup.getContext()).getDragController().removeDragListener(this);
61     }
62 
63 
64     @Override
onChildViewAdded(View parent, View child)65     public void onChildViewAdded(View parent, View child) {
66         if (parent == mViewGroup) {
67             setEnableForLayout((CellLayout) child, true);
68         }
69     }
70 
71     @Override
onChildViewRemoved(View parent, View child)72     public void onChildViewRemoved(View parent, View child) {
73         if (parent == mViewGroup) {
74             setEnableForLayout((CellLayout) child, false);
75         }
76     }
77 
enableAccessibleDrag(boolean enable)78     protected void enableAccessibleDrag(boolean enable) {
79         for (int i = 0; i < mViewGroup.getChildCount(); i++) {
80             setEnableForLayout((CellLayout) mViewGroup.getChildAt(i), enable);
81         }
82     }
83 
setEnableForLayout(CellLayout layout, boolean enable)84     protected final void setEnableForLayout(CellLayout layout, boolean enable) {
85         layout.setDragAndDropAccessibilityDelegate(enable ? mDelegateFactory.apply(layout) : null);
86     }
87 }
88