119a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller/*
219a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller * Copyright (C) 2012 The Android Open Source Project
319a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller *
419a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller * Licensed under the Apache License, Version 2.0 (the "License");
519a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller * you may not use this file except in compliance with the License.
619a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller * You may obtain a copy of the License at
719a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller *
819a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller *      http://www.apache.org/licenses/LICENSE-2.0
919a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller *
1019a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller * Unless required by applicable law or agreed to in writing, software
1119a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller * distributed under the License is distributed on an "AS IS" BASIS,
1219a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1319a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller * See the License for the specific language governing permissions and
1419a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller * limitations under the License.
1519a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller */
1619a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller
175ecd81154fa039961f65bb4e36d18ac555b0d1d6Jim Millerpackage com.android.keyguard;
1819a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller
1919a5267003e7dc70100a4bd4f1f449523b2ff38bJim Millerimport android.view.MotionEvent;
2019a5267003e7dc70100a4bd4f1f449523b2ff38bJim Millerimport android.view.View;
2119a5267003e7dc70100a4bd4f1f449523b2ff38bJim Millerimport android.view.ViewConfiguration;
2219a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller
2319a5267003e7dc70100a4bd4f1f449523b2ff38bJim Millerpublic class CheckLongPressHelper {
2419a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    private View mView;
2519a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    private boolean mHasPerformedLongPress;
2619a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    private CheckForLongPress mPendingCheckForLongPress;
2719a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    private float mDownX, mDownY;
2819a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    private int mLongPressTimeout;
2919a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    private int mScaledTouchSlop;
3019a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller
3119a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    class CheckForLongPress implements Runnable {
3219a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        public void run() {
3319a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller            if ((mView.getParent() != null) && mView.hasWindowFocus()
3419a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller                    && !mHasPerformedLongPress) {
3519a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller                if (mView.performLongClick()) {
3619a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller                    mView.setPressed(false);
3719a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller                    mHasPerformedLongPress = true;
3819a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller                }
3919a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller            }
4019a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        }
4119a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    }
4219a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller
4319a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    public CheckLongPressHelper(View v) {
4419a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        mScaledTouchSlop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop();
4519a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        mLongPressTimeout = ViewConfiguration.getLongPressTimeout();
4619a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        mView = v;
4719a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    }
4819a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller
4919a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    public void postCheckForLongPress(MotionEvent ev) {
5019a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        mDownX = ev.getX();
5119a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        mDownY = ev.getY();
5219a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        mHasPerformedLongPress = false;
5319a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller
5419a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        if (mPendingCheckForLongPress == null) {
5519a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller            mPendingCheckForLongPress = new CheckForLongPress();
5619a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        }
5719a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        mView.postDelayed(mPendingCheckForLongPress, mLongPressTimeout);
5819a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    }
5919a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller
6019a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    public void onMove(MotionEvent ev) {
6119a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        float x = ev.getX();
6219a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        float y = ev.getY();
63e3643138c89d49a01ba6a622ffaf71c9a95d5cdcAdam Cohen        boolean xMoved = Math.abs(mDownX - x) > mScaledTouchSlop;
64e3643138c89d49a01ba6a622ffaf71c9a95d5cdcAdam Cohen        boolean yMoved = Math.abs(mDownY - y) > mScaledTouchSlop;
6519a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller
66e3643138c89d49a01ba6a622ffaf71c9a95d5cdcAdam Cohen        if (xMoved || yMoved) {
6719a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller            cancelLongPress();
6819a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        }
6919a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    }
7019a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller
7119a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    public void cancelLongPress() {
7219a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        mHasPerformedLongPress = false;
7319a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        if (mPendingCheckForLongPress != null) {
7419a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller            mView.removeCallbacks(mPendingCheckForLongPress);
7519a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller            mPendingCheckForLongPress = null;
7619a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        }
7719a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    }
7819a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller
7919a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    public boolean hasPerformedLongPress() {
8019a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller        return mHasPerformedLongPress;
8119a5267003e7dc70100a4bd4f1f449523b2ff38bJim Miller    }
82e3643138c89d49a01ba6a622ffaf71c9a95d5cdcAdam Cohen}
83