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
17package com.android.launcher3.accessibility;
18
19import android.view.ViewGroup;
20
21import com.android.launcher3.CellLayout;
22import com.android.launcher3.DropTarget.DragObject;
23import com.android.launcher3.Launcher;
24import com.android.launcher3.dragndrop.DragController.DragListener;
25import com.android.launcher3.dragndrop.DragOptions;
26
27/**
28 * Utility listener to enable/disable accessibility drag flags for a ViewGroup
29 * containing CellLayouts
30 */
31public class AccessibleDragListenerAdapter implements DragListener {
32
33    private final ViewGroup mViewGroup;
34    private final int mDragType;
35
36    /**
37     * @param parent
38     * @param dragType either {@link CellLayout#WORKSPACE_ACCESSIBILITY_DRAG} or
39     *                 {@link CellLayout#FOLDER_ACCESSIBILITY_DRAG}
40     */
41    public AccessibleDragListenerAdapter(ViewGroup parent, int dragType) {
42        mViewGroup = parent;
43        mDragType = dragType;
44    }
45
46    @Override
47    public void onDragStart(DragObject dragObject, DragOptions options) {
48        enableAccessibleDrag(true);
49    }
50
51    @Override
52    public void onDragEnd() {
53        enableAccessibleDrag(false);
54        Launcher.getLauncher(mViewGroup.getContext()).getDragController().removeDragListener(this);
55    }
56
57    protected void enableAccessibleDrag(boolean enable) {
58        for (int i = 0; i < mViewGroup.getChildCount(); i++) {
59            setEnableForLayout((CellLayout) mViewGroup.getChildAt(i), enable);
60        }
61    }
62
63    protected final void setEnableForLayout(CellLayout layout, boolean enable) {
64        layout.enableAccessibleDrag(enable, mDragType);
65    }
66}
67