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