mt6735 耳机按键功能定制: 单击:播放/暂停音乐,双击:下一首,三击:...
时间:10-02
整理:3721RD
点击:
[Audio Common]耳机按键功能定制:单击:播放/暂停音乐,双击:下一首,三击:上一首
[DESCRIPTION]
有些耳机只有一个按键, 需要通过一个按键来实现播放,暂停,下一首,上一首的功能
功能定义:
单击: 播放/暂停
双击: 下一首
三击: 上一首
[SOLUTION]
完整方案如下,只需要修改 phoneWindowManager.java
1: 加入定义:
long preLastHeADSethookDownTime=0,preLastHeadsethookUpTime=0, lastHeadsethookDownTime = 0,
lastHeadsethookUpTime = 0;
long thisHeadsethookDownTime = 0, thisHeadsethookUpTime = 0;
static int CLIckCount = 0;
2:加入方法:
private void sendHeadsetBroADCast(int clickCount){
Log.d(TAG,"clickCount="+clickCount);
KeyEvent event;
switch(clickCount){
case 3:
event = new KeyEvent(thisHeadsethookUpTime,thisHeadsethookDownTime, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
break;
case 2:
event = new KeyEvent(thisHeadsethookUpTime,thisHeadsethookDownTime,KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_MEDIA_NEXT,0);
break;
case 1:
default:
event = new KeyEvent(thisHeadsethookUpTime,thisHeadsethookDownTime,KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_HEADSETHOOK,0);
break;
}
Log.d(TAG,"keycode = "+event.getKeyCode());
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT,event);
mContext.sendBroadcast(intent);
intent = new Intent();
event = event.changeAction(event,KeyEvent.ACTION_UP);
intent.setAction(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT,event);
mContext.sendBroadcast(intent);
}
3:interceptKeyBeforeQueueing 方法内在参考行
int result;
之后加入如下语句:
if(keyCode == KeyEvent.KEYCODE_HEADSETHOOK /*KeyEvent.KEYCODE_VOLUME_DOWN*/){
if(down){
//this is headsethook down
preLastHeadsethookDownTime = lastHeadsethookDownTime;
lastHeadsethookDownTime = thisHeadsethookDownTime;
thisHeadsethookDownTime = event.getEventTime();
}else{
preLastHeadsethookUpTime = lastHeadsethookUpTime;
lastHeadsethookUpTime = thisHeadsethookUpTime;
thisHeadsethookUpTime = event.getEventTime();
Log.d(TAG,"thisHeadsethookUpTime="+thisHeadsethookUpTime+",thisHeadsethookDownTime="+thisHeadsethookDown
Time);
Log.d(TAG,"lastHeadsethookUpTime="+lastHeadsethookUpTime+",lastHeadsethookDownTime="+lastHeadsethookDown
Time);
Log.d(TAG,"preLastHeadsethookUpTime="+preLastHeadsethookUpTime+",preLastHeadsethookDownTime="+preLastHea
dsethookDownTime);
if((thisHeadsethookUpTime - thisHeadsethookDownTime) < 500){
if((thisHeadsethookDownTime-lastHeadsethookDownTime)>500){
Log.d(TAG,"new click serial detected");
clickCount = 1;
mHandler.postDelayed(new Runnable(){
public void run() {
Log.d(TAG,"call sendHeadsetBroadcast clickCount="+clickCount);
sendHeadsetBroadcast(clickCount);
}
}, 1000);
}else{
Log.d(TAG,"click serial runing");
if((thisHeadsethookUpTime - preLastHeadsethookUpTime)>1000){
Log.d(TAG, "double click detect");
clickCount = 2;
}else{
Log.d(TAG, "triple click detect");
clickCount = 3;
}
}
}
}
result = 0;
result &= ~ACTION_PASS_TO_USER;
return result;
}
[DESCRIPTION]
有些耳机只有一个按键, 需要通过一个按键来实现播放,暂停,下一首,上一首的功能
功能定义:
单击: 播放/暂停
双击: 下一首
三击: 上一首
[SOLUTION]
完整方案如下,只需要修改 phoneWindowManager.java
1: 加入定义:
long preLastHeADSethookDownTime=0,preLastHeadsethookUpTime=0, lastHeadsethookDownTime = 0,
lastHeadsethookUpTime = 0;
long thisHeadsethookDownTime = 0, thisHeadsethookUpTime = 0;
static int CLIckCount = 0;
2:加入方法:
private void sendHeadsetBroADCast(int clickCount){
Log.d(TAG,"clickCount="+clickCount);
KeyEvent event;
switch(clickCount){
case 3:
event = new KeyEvent(thisHeadsethookUpTime,thisHeadsethookDownTime, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
break;
case 2:
event = new KeyEvent(thisHeadsethookUpTime,thisHeadsethookDownTime,KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_MEDIA_NEXT,0);
break;
case 1:
default:
event = new KeyEvent(thisHeadsethookUpTime,thisHeadsethookDownTime,KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_HEADSETHOOK,0);
break;
}
Log.d(TAG,"keycode = "+event.getKeyCode());
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT,event);
mContext.sendBroadcast(intent);
intent = new Intent();
event = event.changeAction(event,KeyEvent.ACTION_UP);
intent.setAction(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT,event);
mContext.sendBroadcast(intent);
}
3:interceptKeyBeforeQueueing 方法内在参考行
int result;
之后加入如下语句:
if(keyCode == KeyEvent.KEYCODE_HEADSETHOOK /*KeyEvent.KEYCODE_VOLUME_DOWN*/){
if(down){
//this is headsethook down
preLastHeadsethookDownTime = lastHeadsethookDownTime;
lastHeadsethookDownTime = thisHeadsethookDownTime;
thisHeadsethookDownTime = event.getEventTime();
}else{
preLastHeadsethookUpTime = lastHeadsethookUpTime;
lastHeadsethookUpTime = thisHeadsethookUpTime;
thisHeadsethookUpTime = event.getEventTime();
Log.d(TAG,"thisHeadsethookUpTime="+thisHeadsethookUpTime+",thisHeadsethookDownTime="+thisHeadsethookDown
Time);
Log.d(TAG,"lastHeadsethookUpTime="+lastHeadsethookUpTime+",lastHeadsethookDownTime="+lastHeadsethookDown
Time);
Log.d(TAG,"preLastHeadsethookUpTime="+preLastHeadsethookUpTime+",preLastHeadsethookDownTime="+preLastHea
dsethookDownTime);
if((thisHeadsethookUpTime - thisHeadsethookDownTime) < 500){
if((thisHeadsethookDownTime-lastHeadsethookDownTime)>500){
Log.d(TAG,"new click serial detected");
clickCount = 1;
mHandler.postDelayed(new Runnable(){
public void run() {
Log.d(TAG,"call sendHeadsetBroadcast clickCount="+clickCount);
sendHeadsetBroadcast(clickCount);
}
}, 1000);
}else{
Log.d(TAG,"click serial runing");
if((thisHeadsethookUpTime - preLastHeadsethookUpTime)>1000){
Log.d(TAG, "double click detect");
clickCount = 2;
}else{
Log.d(TAG, "triple click detect");
clickCount = 3;
}
}
}
}
result = 0;
result &= ~ACTION_PASS_TO_USER;
return result;
}