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 17 package com.android.systemui.statusbar; 18 19 import android.graphics.RectF; 20 import android.view.MotionEvent; 21 import android.view.View; 22 23 import com.android.systemui.R; 24 25 public class DelegateViewHelper { 26 private View mDelegateView; 27 private View mSourceView; 28 private BaseStatusBar mBar; 29 private int[] mTempPoint = new int[2]; 30 private float[] mDownPoint = new float[2]; 31 private float mTriggerThreshhold; 32 private boolean mPanelShowing; 33 34 RectF mInitialTouch = new RectF(); 35 private boolean mStarted; 36 private boolean mSwapXY = false; 37 DelegateViewHelper(View sourceView)38 public DelegateViewHelper(View sourceView) { 39 setSourceView(sourceView); 40 } 41 setDelegateView(View view)42 public void setDelegateView(View view) { 43 mDelegateView = view; 44 } 45 setBar(BaseStatusBar phoneStatusBar)46 public void setBar(BaseStatusBar phoneStatusBar) { 47 mBar = phoneStatusBar; 48 } 49 onInterceptTouchEvent(MotionEvent event)50 public boolean onInterceptTouchEvent(MotionEvent event) { 51 if (mSourceView == null || mDelegateView == null 52 || mBar.shouldDisableNavbarGestures()) { 53 return false; 54 } 55 56 mSourceView.getLocationOnScreen(mTempPoint); 57 final float sourceX = mTempPoint[0]; 58 final float sourceY = mTempPoint[1]; 59 60 61 switch (event.getAction()) { 62 case MotionEvent.ACTION_DOWN: 63 mPanelShowing = mDelegateView.getVisibility() == View.VISIBLE; 64 mDownPoint[0] = event.getX(); 65 mDownPoint[1] = event.getY(); 66 mStarted = mInitialTouch.contains(mDownPoint[0] + sourceX, mDownPoint[1] + sourceY); 67 break; 68 } 69 70 if (!mStarted) { 71 return false; 72 } 73 74 if (!mPanelShowing && event.getAction() == MotionEvent.ACTION_MOVE) { 75 final int historySize = event.getHistorySize(); 76 for (int k = 0; k < historySize + 1; k++) { 77 float x = k < historySize ? event.getHistoricalX(k) : event.getX(); 78 float y = k < historySize ? event.getHistoricalY(k) : event.getY(); 79 final float distance = mSwapXY ? (mDownPoint[0] - x) : (mDownPoint[1] - y); 80 if (distance > mTriggerThreshhold) { 81 mBar.showSearchPanel(); 82 mPanelShowing = true; 83 break; 84 } 85 } 86 } 87 88 mDelegateView.getLocationOnScreen(mTempPoint); 89 final float delegateX = mTempPoint[0]; 90 final float delegateY = mTempPoint[1]; 91 92 float deltaX = sourceX - delegateX; 93 float deltaY = sourceY - delegateY; 94 event.offsetLocation(deltaX, deltaY); 95 mDelegateView.dispatchTouchEvent(event); 96 event.offsetLocation(-deltaX, -deltaY); 97 return mPanelShowing; 98 } 99 100 public void setSourceView(View view) { 101 mSourceView = view; 102 if (mSourceView != null) { 103 mTriggerThreshhold = mSourceView.getContext().getResources() 104 .getDimension(R.dimen.navbar_search_up_threshhold); 105 } 106 } 107 108 /** 109 * Selects the initial touch region based on a list of views. This is meant to be called by 110 * a container widget on children over which the initial touch should be detected. Note this 111 * will compute a minimum bound that contains all specified views. 112 * 113 * @param views 114 */ 115 public void setInitialTouchRegion(View ... views) { 116 RectF bounds = new RectF(); 117 int p[] = new int[2]; 118 for (int i = 0; i < views.length; i++) { 119 View view = views[i]; 120 if (view == null) continue; 121 view.getLocationOnScreen(p); 122 if (i == 0) { 123 bounds.set(p[0], p[1], p[0] + view.getWidth(), p[1] + view.getHeight()); 124 } else { 125 bounds.union(p[0], p[1], p[0] + view.getWidth(), p[1] + view.getHeight()); 126 } 127 } 128 mInitialTouch.set(bounds); 129 } 130 131 /** 132 * When rotation is set to NO_SENSOR, then this allows swapping x/y for gesture detection 133 * @param swap 134 */ 135 public void setSwapXY(boolean swap) { 136 mSwapXY = swap; 137 } 138 }