DragLayer.java revision d1837cc69eb222371afdd30890f7215d117510f2
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        if (w != null) {
66            final CellLayout currentPage = (CellLayout) w.getChildAt(w.getCurrentPage());
67            final CellLayoutChildren childrenLayout = currentPage.getChildrenLayout();
68
69            if (childrenLayout.hasResizeFrames() && !childrenLayout.isWidgetBeingResized()) {
70                post(new Runnable() {
71                    public void run() {
72                        if (!childrenLayout.isWidgetBeingResized()) {
73                            childrenLayout.clearAllResizeFrames();
74                        }
75                    }
76                });
77            }
78        }
79        return mDragController.onInterceptTouchEvent(ev);
80    }
81
82    @Override
83    public boolean onTouchEvent(MotionEvent ev) {
84        return mDragController.onTouchEvent(ev);
85    }
86
87    @Override
88    public boolean dispatchUnhandledMove(View focused, int direction) {
89        return mDragController.dispatchUnhandledMove(focused, direction);
90    }
91
92    public View createDragView(Bitmap b, int xPos, int yPos) {
93        ImageView imageView = new ImageView(mContext);
94        imageView.setImageBitmap(b);
95        imageView.setX(xPos);
96        imageView.setY(yPos);
97        addView(imageView, b.getWidth(), b.getHeight());
98
99        return imageView;
100    }
101
102    public View createDragView(View v) {
103        v.getLocationOnScreen(mTmpXY);
104        return createDragView(mDragController.getViewBitmap(v), mTmpXY[0], mTmpXY[1]);
105    }
106}
107