DelegateViewHelper.java revision 161538985ee51e109f64af9a332d396bbffd7e2c
1/*
2 * Copyright (C) 2010 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.systemui.statusbar;
18
19import android.util.Slog;
20import android.view.MotionEvent;
21import android.view.Surface;
22import android.view.View;
23
24import com.android.systemui.R;
25
26public class DelegateViewHelper {
27    private View mDelegateView;
28    private View mSourceView;
29    private BaseStatusBar mBar;
30    private int[] mTempPoint = new int[2];
31    private float[] mDownPoint = new float[2];
32    private int mOrientation;
33    private float mTriggerThreshhold;
34
35    public DelegateViewHelper(View sourceView) {
36        setSourceView(sourceView);
37    }
38
39    public void setDelegateView(View view) {
40        mDelegateView = view;
41    }
42
43    public void setBar(BaseStatusBar phoneStatusBar) {
44        mBar = phoneStatusBar;
45    }
46
47    public void setOrientation(int orientation) {
48        mOrientation = orientation;
49    }
50
51    public boolean onInterceptTouchEvent(MotionEvent event) {
52        if (mBar.shouldDisableNavbarGestures()) {
53            return false;
54        }
55        switch (event.getAction()) {
56            case MotionEvent.ACTION_DOWN:
57                mDownPoint[0] = event.getX();
58                mDownPoint[1] = event.getY();
59                break;
60        }
61        if (mDelegateView != null) {
62            if (mDelegateView.getVisibility() != View.VISIBLE
63                    && event.getAction() != MotionEvent.ACTION_CANCEL) {
64                final boolean isVertical = (mOrientation == Surface.ROTATION_90
65                        || mOrientation == Surface.ROTATION_270);
66                final int historySize = event.getHistorySize();
67                for (int k = 0; k < historySize + 1; k++) {
68                    float x = k < historySize ? event.getHistoricalX(k) : event.getX();
69                    float y = k < historySize ? event.getHistoricalY(k) : event.getY();
70                    final float distance = isVertical ? (mDownPoint[0] - x) : (mDownPoint[1] - y);
71                    if (distance > mTriggerThreshhold) {
72                        mBar.showSearchPanel();
73                        break;
74                    }
75                }
76            }
77            mSourceView.getLocationOnScreen(mTempPoint);
78            float deltaX = mTempPoint[0];
79            float deltaY = mTempPoint[1];
80
81            mDelegateView.getLocationOnScreen(mTempPoint);
82            deltaX -= mTempPoint[0];
83            deltaY -= mTempPoint[1];
84
85            event.offsetLocation(deltaX, deltaY);
86            mDelegateView.dispatchTouchEvent(event);
87            event.offsetLocation(-deltaX, -deltaY);
88        }
89        return false;
90    }
91
92    public void setSourceView(View view) {
93        mSourceView = view;
94        if (mSourceView != null) {
95            mTriggerThreshhold = mSourceView.getContext().getResources()
96                    .getDimension(R.dimen.navbar_search_up_threshhold);
97        }
98    }
99}