DragLayer.java revision d4844c3e731b00547a31f23a00f8bd4a271e2b62
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        // Here we need to detect if any touch event has occured which doesn't result
62        // in resizing a widget. In this case, we dismiss any visible resize frames.
63        post(new Runnable() {
64            public void run() {
65                Workspace w = (Workspace) findViewById(R.id.workspace);
66                CellLayout currentPage = (CellLayout) w.getChildAt(w.getCurrentPage());
67                if (!currentPage.getChildrenLayout().isWidgetBeingResized()) {
68                    currentPage.getChildrenLayout().clearAllResizeFrames();
69                }
70            }
71        });
72        return mDragController.onInterceptTouchEvent(ev);
73    }
74
75    @Override
76    public boolean onTouchEvent(MotionEvent ev) {
77        return mDragController.onTouchEvent(ev);
78    }
79
80    @Override
81    public boolean dispatchUnhandledMove(View focused, int direction) {
82        return mDragController.dispatchUnhandledMove(focused, direction);
83    }
84
85    public View createDragView(Bitmap b, int xPos, int yPos) {
86        ImageView imageView = new ImageView(mContext);
87        imageView.setImageBitmap(b);
88        imageView.setX(xPos);
89        imageView.setY(yPos);
90        addView(imageView, b.getWidth(), b.getHeight());
91
92        return imageView;
93    }
94
95    public View createDragView(View v) {
96        v.getLocationOnScreen(mTmpXY);
97        return createDragView(mDragController.getViewBitmap(v), mTmpXY[0], mTmpXY[1]);
98    }
99}
100