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 package com.android.launcher3.accessibility 17 18 import android.view.View 19 import android.view.ViewGroup 20 import com.android.launcher3.CellLayout 21 import com.android.launcher3.DropTarget.DragObject 22 import com.android.launcher3.dragndrop.DragController 23 import com.android.launcher3.dragndrop.DragOptions 24 import com.android.launcher3.views.ActivityContext 25 import java.util.function.Function 26 27 /** 28 * Utility listener to enable/disable accessibility drag flags for a ViewGroup containing 29 * CellLayouts 30 */ 31 open class AccessibleDragListenerAdapter 32 /** 33 * @param parent the viewgroup containing all the children 34 * @param delegateFactory function to create no delegates 35 */ 36 ( 37 private val mViewGroup: ViewGroup, 38 private val mDelegateFactory: Function<CellLayout, DragAndDropAccessibilityDelegate> 39 ) : DragController.DragListener, ViewGroup.OnHierarchyChangeListener { onDragStartnull40 override fun onDragStart(dragObject: DragObject, options: DragOptions) { 41 mViewGroup.setOnHierarchyChangeListener(this) 42 enableAccessibleDrag(true, dragObject) 43 } 44 onDragEndnull45 override fun onDragEnd() { 46 mViewGroup.setOnHierarchyChangeListener(null) 47 enableAccessibleDrag(false, null) 48 val activityContext = ActivityContext.lookupContext(mViewGroup.context) as ActivityContext 49 activityContext.getDragController<DragController<*>>()?.removeDragListener(this) 50 } 51 onChildViewAddednull52 override fun onChildViewAdded(parent: View, child: View) { 53 if (parent === mViewGroup) { 54 setEnableForLayout(child as CellLayout, true) 55 } 56 } 57 onChildViewRemovednull58 override fun onChildViewRemoved(parent: View, child: View) { 59 if (parent === mViewGroup) { 60 setEnableForLayout(child as CellLayout, false) 61 } 62 } 63 enableAccessibleDragnull64 protected open fun enableAccessibleDrag(enable: Boolean, dragObject: DragObject?) { 65 for (i in 0 until mViewGroup.childCount) { 66 setEnableForLayout(mViewGroup.getChildAt(i) as CellLayout, enable) 67 } 68 } 69 setEnableForLayoutnull70 protected fun setEnableForLayout(layout: CellLayout, enable: Boolean) { 71 layout.setDragAndDropAccessibilityDelegate( 72 if (enable) mDelegateFactory.apply(layout) else null 73 ) 74 } 75 } 76