DragLayer.java revision 1b607ed454ed22c2fd855cb3e428376520fb2388
1/*
2 * Copyright (C) 2008 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.launcher2;
18
19import com.android.launcher.R;
20
21import android.content.Context;
22import android.graphics.Bitmap;
23import android.util.AttributeSet;
24import android.view.KeyEvent;
25import android.view.MotionEvent;
26import android.view.View;
27import android.widget.FrameLayout;
28import android.widget.ImageView;
29
30/**
31 * A ViewGroup that coordinates dragging across its descendants
32 */
33public class DragLayer extends FrameLayout {
34    private DragController mDragController;
35    private int[] mTmpXY = new int[2];
36
37    /**
38     * Used to create a new DragLayer from XML.
39     *
40     * @param context The application's context.
41     * @param attrs The attributes set containing the Workspace's customization values.
42     */
43    public DragLayer(Context context, AttributeSet attrs) {
44        super(context, attrs);
45
46        // Disable multitouch across the workspace/all apps/customize tray
47        setMotionEventSplittingEnabled(false);
48    }
49
50    public void setDragController(DragController controller) {
51        mDragController = controller;
52    }
53
54    @Override
55    public boolean dispatchKeyEvent(KeyEvent event) {
56        return mDragController.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);
57    }
58
59    @Override
60    public boolean onInterceptTouchEvent(MotionEvent ev) {
61        // If the current CellLayoutChildren has a resize frame, we need to detect if any touch
62        // event has occurred which doesn't result in resizing a widget. In this case, we
63        // dismiss any visible resize frames.
64        final Workspace w = (Workspace) findViewById(R.id.workspace);
65        final CellLayout currentPage = (CellLayout) w.getChildAt(w.getCurrentPage());
66
67        if (currentPage.getChildrenLayout().hasResizeFrames()) {
68            post(new Runnable() {
69                public void run() {
70                    if (!currentPage.getChildrenLayout().isWidgetBeingResized()) {
71                        currentPage.getChildrenLayout().clearAllResizeFrames();
72                    }
73                }
74            });
75        }
76        return mDragController.onInterceptTouchEvent(ev);
77    }
78
79    @Override
80    public boolean onTouchEvent(MotionEvent ev) {
81        return mDragController.onTouchEvent(ev);
82    }
83
84    @Override
85    public boolean dispatchUnhandledMove(View focused, int direction) {
86        return mDragController.dispatchUnhandledMove(focused, direction);
87    }
88
89    public View createDragView(Bitmap b, int xPos, int yPos) {
90        ImageView imageView = new ImageView(mContext);
91        imageView.setImageBitmap(b);
92        imageView.setX(xPos);
93        imageView.setY(yPos);
94        addView(imageView, b.getWidth(), b.getHeight());
95
96        return imageView;
97    }
98
99    public View createDragView(View v) {
100        v.getLocationOnScreen(mTmpXY);
101        return createDragView(mDragController.getViewBitmap(v), mTmpXY[0], mTmpXY[1]);
102    }
103}
104