本修改方案是基于android 4.4 修改SystemUI中源码的完成,仅供参考 有其他思路的,欢迎大家来交流、指正。
大家都知道Home键长按、或者在Home键附近快速向上滑动 都会出现SearchPanelView布局,当滑动到搜索图标后,会启动Goole Search(或者能够接收到相应Intent的apk 大于2个以上 会弹出选择对话框)。
快速向上滑动显示SearchPanelView的关键代码在: frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\DelegateViewHelper.java 中的onInterceptTouchEvent方法中,具体代码如下:
if (!mPanelShowing && event.getAction() == MotionEvent.ACTION_MOVE) {
final int historySize = event.getHistorySize();
for (int k = 0; k < historySize + 1; k++) {
float x = k < historySize ? event.getHistoricalX(k) : event.getX();
float y = k < historySize ? event.getHistoricalY(k) : event.getY();
final float distance = mSwapXY ? (mDownPoint[0] - x) : (mDownPoint[1] - y);
if (distance > mTriggerThreshhold) {
mBar.showSearchPanel(); //显示的关键性方法
mPanelShowing = true;
break;
}
}
}
所以屏蔽滑动显示SearchPanelView,只需要注释关键性方法的代码即可。
长按Home键,显示SearchPanelView的关键代码在:frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\PhoneStatusBar.java中的
View.OnTouchListener mHomeSearchActionListener = new View.OnTouchListener() { ...... }方法中, 具体代码如下:
case MotionEvent.ACTION_DOWN:
if (!shouldDisableNavbarGestures()) {
mHandler.removeCallbacks(mShowSearchPanel);
mHandler.postDelayed(mShowSearchPanel, mShowSearchHoldoff);//给Home键设定一个OnTouchListener监听事件,通过Handler机制最终还是调用的showSearchPanel()方法显示
}
break;
所以屏蔽长按Home键 显示SearchPanelView,只需要注释关键性方法的代码即可。
以上代码中关键方式注释后,仅仅只是不让SearchPanelView 显示,在Home键附近快速向上滑动时,依旧会发送Intent 调起响应的Search应用(大于2个响应apk会以Dialog方式显示,以供选择) 如果想将这个效果现象一并去掉的话,那么只有将SearchPanelView 在WindowManager中移除,不加载到StatusBar中,关键代码如下:
protected void updateSearchPanel() {
// Search Panel
boolean visible = false;
if (mSearchPanelView != null) {
visible = mSearchPanelView.isShowing();
mWindowManager.removeView(mSearchPanelView);//如果注释掉加载mWindowManager.addView(mSearchPanelView, lp);该行,请一并注释掉removeView(mSearchPanelView),否则在部分场景下,会出现null指正异常
}
// Provide SearchPanel with a temporary parent to allow layout params to work.
LinearLayout tmpRoot = new LinearLayout(mContext);
mSearchPanelView = (SearchPanelView) LayoutInflater.from(mContext).inflate(
R.layout.status_bar_search_panel, tmpRoot, false);
mSearchPanelView.setOnTouchListener(
new TouchOutsideListener(MSG_CLOSE_SEARCH_PANEL, mSearchPanelView));
mSearchPanelView.setVisibility(View.GONE);
WindowManager.LayoutParams lp = getSearchLayoutParams(mSearchPanelView.getLayoutParams());
mWindowManager.addView(mSearchPanelView, lp);//加载mSearchPanelView代码
mSearchPanelView.setBar(this);
if (visible) {
mSearchPanelView.show(true, false);
}
}
注释以上两行后,即可去掉 在Home键附近快速向上滑动时,依旧会发送Intent 调起响应的Search应用效果。
下面在说说Home的长按事件,为什么说要类似长按事件效果呢? 原因是framework底层执行onTouch事件 return true,事件不再会下发,在SystemUI下 给Home键设置OnLongClickListener事件流程不会走进来,具体原因为分析,可参考http://blog.csdn.net/harhy/article/details/8724779。那么如何模拟呢? 解决方案就是:在onTouch事件中的计算ActionDown、ActionUp之间的时间差,利用handler构造类似onLongClick的响应事件. 具体代码如下:
frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\PhoneStatusBar.java
SystemUI原始代码部分:
View.OnTouchListener mHomeSearchActionListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!shouldDisableNavbarGestures()) {
mHandler.removeCallbacks(mShowSearchPanel);
mHandler.postDelayed(mShowSearchPanel, mShowSearchHoldoff);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mHandler.removeCallbacks(mShowSearchPanel);
awakenDreams();
break;
default:
break;
}
return false;
}
};
修改后代码:
/* < added begin */
private boolean isActionDown = false;
private long mCurrentActionDownTime = 0;
private long DELAY_TIME = 500;
private Handler mCommonHandler;
private static final int MSG_SHOW_SEARCH_DIALOG = 110;
/* added end > */
View.OnTouchListener mHomeSearchActionListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!shouldDisableNavbarGestures()) {
/*< modified begin */
//mHandler.removeCallbacks(mShowSearchPanel);
//mHandler.postDelayed(mShowSearchPanel, mShowSearchHoldoff);
isActionDown = true;
mCurrentActionDownTime = System.currentTimeMillis();
Handler handler = getCommonHandler();
if (!handler.hasMessages(MSG_SHOW_SEARCH_DIALOG)) {
handler.sendEmptyMessageDelayed(MSG_SHOW_SEARCH_DIALOG, DELAY_TIME);
}
/*modified end > */
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
/* < modified begin */
if(isActionDown && System.currentTimeMillis() - mCurrentActionDownTime < DELAY_TIME) {
Handler handler = getCommonHandler();
if (handler.hasMessages(MSG_SHOW_SEARCH_DIALOG)) {
handler.removeMessages(MSG_SHOW_SEARCH_DIALOG);
}
isActionDown = false;
}
mHandler.removeCallbacks(mShowSearchPanel);
awakenDreams();
if (isActionDown) {
mNavigationBarView.getHomeButton().setPressed(false);
mNavigationBarView.getHomeButton().invalidate();
return isActionDown;
}
/* modified end > */
break;
default:
break;
}
return false;
}
};
/* < added begin */
private synchronized Handler getCommonHandler() {
if (mCommonHandler == null) {
mCommonHandler = new Handler(mContext.getMainLooper(), new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case MSG_SHOW_SEARCH_DIALOG:
TO DO SOMETHING 处理长按事件操作
break;
default:
break;
}
return true;
}
});
}
return mCommonHandler;
}
/* modified end > */
以上就是处理Home的长按事件修改,本贴没有把SystemUI细化流程分析,仅仅直奔解决方案,不好意思的啦 To be continued.....