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