DragView.java revision 3e8f811f31a2275e778244427271f77f4116c08b
100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato/*
200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato * Copyright (C) 2008 The Android Open Source Project
300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato *
400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato * you may not use this file except in compliance with the License.
600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato * You may obtain a copy of the License at
700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato *
800acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato *
1000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato * Unless required by applicable law or agreed to in writing, software
1100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
1200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato * See the License for the specific language governing permissions and
1400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato * limitations under the License.
1500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato */
1600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
1700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
1800acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratopackage com.android.launcher2;
1900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
20a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroyimport android.animation.ValueAnimator;
21a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroyimport android.animation.ValueAnimator.AnimatorUpdateListener;
2200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratoimport android.content.Context;
23de7658b5e02ae10010e44fcf8d9c5814f54d9eb0Patrick Dubroyimport android.content.res.Resources;
2400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratoimport android.graphics.Bitmap;
2500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratoimport android.graphics.Canvas;
2600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratoimport android.graphics.Matrix;
2700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratoimport android.graphics.Paint;
2800acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratoimport android.graphics.PixelFormat;
29e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohenimport android.graphics.Rect;
3000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratoimport android.os.IBinder;
3100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratoimport android.view.Gravity;
3200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratoimport android.view.View;
3300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratoimport android.view.ViewGroup;
3400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratoimport android.view.WindowManager;
3500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onoratoimport android.view.WindowManagerImpl;
36a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroyimport android.view.animation.DecelerateInterpolator;
3700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
38120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohenimport com.android.launcher.R;
39120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen
40a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroypublic class DragView extends View {
4100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    private Bitmap mBitmap;
4200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    private Paint mPaint;
4300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    private int mRegistrationX;
4400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    private int mRegistrationY;
4500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
46e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    private Rect mDragRegion = null;
47a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
48a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy    ValueAnimator mAnim;
49a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy    private float mOffsetX = 0.0f;
50a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy    private float mOffsetY = 0.0f;
5100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
5200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    private WindowManager.LayoutParams mLayoutParams;
5300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    private WindowManager mWindowManager;
5400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
5500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    /**
56a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy     * A callback to be called the first time this view is drawn.
57a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy     * This allows the originator of the drag to dim or hide the original view as soon
58a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy     * as the DragView is drawn.
59a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy     */
60a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy    private Runnable mOnDrawRunnable = null;
61a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy
62a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy    /**
6300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * Construct the drag view.
6400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * <p>
6500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * The registration point is the point inside our view that the touch events should
6600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * be centered upon.
6700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     *
6800acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * @param context A context
6900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * @param bitmap The view that we're dragging around.  We scale it up when we draw it.
7000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * @param registrationX The x coordinate of the registration point.
7100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * @param registrationY The y coordinate of the registration point.
7200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     */
735162ea9b1f41dbebe00fd9ec4d1e15a697971439Joe Onorato    public DragView(Context context, Bitmap bitmap, int registrationX, int registrationY,
745162ea9b1f41dbebe00fd9ec4d1e15a697971439Joe Onorato            int left, int top, int width, int height) {
7500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        super(context);
7600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
77de7658b5e02ae10010e44fcf8d9c5814f54d9eb0Patrick Dubroy        final Resources res = getResources();
78de7658b5e02ae10010e44fcf8d9c5814f54d9eb0Patrick Dubroy        final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels);
79de7658b5e02ae10010e44fcf8d9c5814f54d9eb0Patrick Dubroy
8000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        mWindowManager = WindowManagerImpl.getDefault();
8100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
8200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        Matrix scale = new Matrix();
83a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        final float scaleFactor = (width + dragScale) / width;
84a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        if (scaleFactor != 1.0f) {
85a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy            scale.setScale(scaleFactor, scaleFactor);
86a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        }
87a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy
888878a32f26b7e6c287d970950526cd0e6eabb21dAdam Cohen        final int offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
898878a32f26b7e6c287d970950526cd0e6eabb21dAdam Cohen        final int offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
90a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy
91a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        // Animate the view into the correct position
92a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        mAnim = ValueAnimator.ofFloat(0.0f, 1.0f);
93a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        mAnim.setDuration(110);
94a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        mAnim.setInterpolator(new DecelerateInterpolator(2.5f));
95a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        mAnim.addUpdateListener(new AnimatorUpdateListener() {
96a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy            @Override
97a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy            public void onAnimationUpdate(ValueAnimator animation) {
98a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                final float value = (Float) animation.getAnimatedValue();
99a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy
100a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                final int deltaX = (int) ((value * offsetX) - mOffsetX);
101a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                final int deltaY = (int) ((value * offsetY) - mOffsetY);
102a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy
103a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                mOffsetX += deltaX;
104a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                mOffsetY += deltaY;
105a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy
106a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                if (getParent() == null) {
107a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                    animation.cancel();
108a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                } else {
109a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                    WindowManager.LayoutParams lp = mLayoutParams;
110a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                    lp.x += deltaX;
111a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                    lp.y += deltaY;
112a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                    mWindowManager.updateViewLayout(DragView.this, lp);
113a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                }
114a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy            }
115a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        });
11600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
1175162ea9b1f41dbebe00fd9ec4d1e15a697971439Joe Onorato        mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
118e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        setDragRegion(new Rect(0, 0, width, height));
11900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
12000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        // The point in our scaled bitmap that the touch events are located
121a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        mRegistrationX = registrationX;
122a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        mRegistrationY = registrationY;
12362bbb3c573fe902940431e4b957c0c4918358d36Patrick Dubroy
12462bbb3c573fe902940431e4b957c0c4918358d36Patrick Dubroy        // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
12562bbb3c573fe902940431e4b957c0c4918358d36Patrick Dubroy        int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
12662bbb3c573fe902940431e4b957c0c4918358d36Patrick Dubroy        measure(ms, ms);
12762bbb3c573fe902940431e4b957c0c4918358d36Patrick Dubroy    }
12862bbb3c573fe902940431e4b957c0c4918358d36Patrick Dubroy
12962bbb3c573fe902940431e4b957c0c4918358d36Patrick Dubroy    public float getOffsetY() {
13062bbb3c573fe902940431e4b957c0c4918358d36Patrick Dubroy        return mOffsetY;
13100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
13200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
133a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy    public void setOnDrawRunnable(Runnable r) {
134a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        mOnDrawRunnable = r;
135a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy    }
136a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy
137a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    public int getDragRegionLeft() {
138e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        return mDragRegion.left;
139a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    }
140a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
141a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    public int getDragRegionTop() {
142e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        return mDragRegion.top;
143a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    }
144a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
145a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    public int getDragRegionWidth() {
146e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        return mDragRegion.width();
147a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    }
148a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
149a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    public int getDragRegionHeight() {
150e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        return mDragRegion.height();
151e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    }
152e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
153e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    public void setDragRegion(Rect r) {
154e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        mDragRegion = r;
155e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    }
156e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
157e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    public Rect getDragRegion() {
158e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        return mDragRegion;
159a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    }
160a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
16100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    @Override
16200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
163eebd92496e2fbfbd81ea871862a681929101ba06Joe Onorato        setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
16400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
16500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
16600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    @Override
16700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    protected void onDraw(Canvas canvas) {
16800acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        if (false) {
16900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato            // for debugging
17000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato            Paint p = new Paint();
17100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato            p.setStyle(Paint.Style.FILL);
17200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato            p.setColor(0xaaffffff);
17300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato            canvas.drawRect(0, 0, getWidth(), getHeight(), p);
17400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        }
175a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy
176a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        // Call the callback if we haven't already been detached
177a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        if (getParent() != null) {
178a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy            if (mOnDrawRunnable != null) {
179a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                mOnDrawRunnable.run();
180a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                mOnDrawRunnable = null;
181a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy            }
18200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        }
183a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy
18400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
18500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
18600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
18700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    @Override
18800acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    protected void onDetachedFromWindow() {
18900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        super.onDetachedFromWindow();
19000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        mBitmap.recycle();
19100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
19200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
19300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    public void setPaint(Paint paint) {
19400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        mPaint = paint;
19500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        invalidate();
19600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
19700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
1983e8f811f31a2275e778244427271f77f4116c08bAdam Cohen    @Override
1993e8f811f31a2275e778244427271f77f4116c08bAdam Cohen    public void setAlpha(float alpha) {
2003e8f811f31a2275e778244427271f77f4116c08bAdam Cohen        super.setAlpha(alpha);
2013e8f811f31a2275e778244427271f77f4116c08bAdam Cohen        if (mPaint == null) {
2023e8f811f31a2275e778244427271f77f4116c08bAdam Cohen            mPaint = new Paint();
2033e8f811f31a2275e778244427271f77f4116c08bAdam Cohen        }
2043e8f811f31a2275e778244427271f77f4116c08bAdam Cohen        mPaint.setAlpha((int) (255 * alpha));
2053e8f811f31a2275e778244427271f77f4116c08bAdam Cohen        invalidate();
2063e8f811f31a2275e778244427271f77f4116c08bAdam Cohen    }
2073e8f811f31a2275e778244427271f77f4116c08bAdam Cohen
20800acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    /**
20900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * Create a window containing this view and show it.
21000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     *
21100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * @param windowToken obtained from v.getWindowToken() from one of your views
21200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * @param touchX the x coordinate the user touched in screen coordinates
21300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * @param touchY the y coordinate the user touched in screen coordinates
21400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     */
21500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    public void show(IBinder windowToken, int touchX, int touchY) {
21600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        WindowManager.LayoutParams lp;
21700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        int pixelFormat;
21800acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
21900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        pixelFormat = PixelFormat.TRANSLUCENT;
22000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
22100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        lp = new WindowManager.LayoutParams(
22200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato                ViewGroup.LayoutParams.WRAP_CONTENT,
22300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato                ViewGroup.LayoutParams.WRAP_CONTENT,
224a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy                touchX - mRegistrationX, touchY - mRegistrationY,
22500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato                WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
22600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
22700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
22800acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato                    /*| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM*/,
22900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato                pixelFormat);
23000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato//        lp.token = mStatusBarView.getWindowToken();
23100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        lp.gravity = Gravity.LEFT | Gravity.TOP;
23200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        lp.token = windowToken;
23300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        lp.setTitle("DragView");
23400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        mLayoutParams = lp;
23500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
23600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        mWindowManager.addView(this, lp);
23700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
238a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        mAnim.start();
23900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
240716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen
24100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    /**
24200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * Move the window containing this view.
24300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     *
24400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * @param touchX the x coordinate the user touched in screen coordinates
24500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     * @param touchY the y coordinate the user touched in screen coordinates
24600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato     */
24700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    void move(int touchX, int touchY) {
24800acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        WindowManager.LayoutParams lp = mLayoutParams;
249a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        lp.x = touchX - mRegistrationX + (int) mOffsetX;
250a669d79d0dcf034651ce305a8cd334272bec3374Patrick Dubroy        lp.y = touchY - mRegistrationY + (int) mOffsetY;
25100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        mWindowManager.updateViewLayout(this, lp);
25200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
25300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
25400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    void remove() {
2553e8f811f31a2275e778244427271f77f4116c08bAdam Cohen        post(new Runnable() {
2563e8f811f31a2275e778244427271f77f4116c08bAdam Cohen            public void run() {
2573e8f811f31a2275e778244427271f77f4116c08bAdam Cohen                mWindowManager.removeView(DragView.this);
2583e8f811f31a2275e778244427271f77f4116c08bAdam Cohen            }
2593e8f811f31a2275e778244427271f77f4116c08bAdam Cohen        });
26000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
2616f13342ffd3f968de9ff86b988621cc91d94adffPatrick Dubroy
2626f13342ffd3f968de9ff86b988621cc91d94adffPatrick Dubroy    int[] getPosition(int[] result) {
2636f13342ffd3f968de9ff86b988621cc91d94adffPatrick Dubroy        WindowManager.LayoutParams lp = mLayoutParams;
2646f13342ffd3f968de9ff86b988621cc91d94adffPatrick Dubroy        if (result == null) result = new int[2];
2656f13342ffd3f968de9ff86b988621cc91d94adffPatrick Dubroy        result[0] = lp.x;
2666f13342ffd3f968de9ff86b988621cc91d94adffPatrick Dubroy        result[1] = lp.y;
2676f13342ffd3f968de9ff86b988621cc91d94adffPatrick Dubroy        return result;
2686f13342ffd3f968de9ff86b988621cc91d94adffPatrick Dubroy    }
26900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato}
27000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
271