opt
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package com.mogo.commons.voice;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.mogo.utils.logger.Logger;
|
||||
import com.zhidao.auto.platform.voice.VoiceClient;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -11,6 +13,7 @@ import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
@@ -20,8 +23,11 @@ import java.util.Map;
|
||||
*/
|
||||
public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
|
||||
private static final String TAG = "AIAssist";
|
||||
private static volatile AIAssist sInstance;
|
||||
private String mLastQAndAspeakText;
|
||||
private String mLastQAndASpeakText;
|
||||
|
||||
private boolean mHasFlush = false;
|
||||
|
||||
public static AIAssist getInstance( Context context ) {
|
||||
if ( sInstance == null ) {
|
||||
@@ -43,6 +49,7 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
mQAndAMap.clear();
|
||||
mVoiceClient.release();
|
||||
mSpeakVoiceMap.clear();
|
||||
mCacheUnWakeupCommands.clear();
|
||||
sInstance = null;
|
||||
}
|
||||
|
||||
@@ -54,10 +61,15 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
// 单独的语音播放
|
||||
private Map< String, IMogoVoiceCmdCallBack > mSpeakVoiceMap = new HashMap<>();
|
||||
|
||||
private Map< String, String[] > mCacheUnWakeupCommands = new ConcurrentHashMap<>();
|
||||
|
||||
private AIAssist( Context context ) {
|
||||
// private constructor
|
||||
mVoiceClient = new VoiceClient( context.getApplicationContext() );
|
||||
mVoiceClient.setCallBack( this );
|
||||
mHasFlush = isVoiceServiceReady( context );
|
||||
|
||||
Logger.w( TAG, "voice is ready = %s", mHasFlush );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -65,6 +77,7 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
if ( !mCmdMap.containsKey( cmd ) ) {
|
||||
return;
|
||||
}
|
||||
Logger.d( TAG, "received command: %s", cmd );
|
||||
final List< IMogoVoiceCmdCallBack > cmdCallBacks = mCmdMap.get( cmd );
|
||||
Iterator< IMogoVoiceCmdCallBack > iterator = cmdCallBacks.iterator();
|
||||
while ( iterator.hasNext() ) {
|
||||
@@ -77,8 +90,8 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
|
||||
@Override
|
||||
public void onCmdAction( String speakText ) {
|
||||
if ( !TextUtils.isEmpty( mLastQAndAspeakText ) ) {
|
||||
IMogoVoiceCmdCallBack cmdCallBack = mQAndAMap.remove( mLastQAndAspeakText );
|
||||
if ( !TextUtils.isEmpty( mLastQAndASpeakText ) ) {
|
||||
IMogoVoiceCmdCallBack cmdCallBack = mQAndAMap.remove( mLastQAndASpeakText );
|
||||
if ( cmdCallBack != null ) {
|
||||
cmdCallBack.onCmdAction( speakText );
|
||||
}
|
||||
@@ -87,8 +100,8 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
|
||||
@Override
|
||||
public void onCmdCancel( String speakText ) {
|
||||
if ( !TextUtils.isEmpty( mLastQAndAspeakText ) ) {
|
||||
IMogoVoiceCmdCallBack cmdCallBack = mQAndAMap.remove( mLastQAndAspeakText );
|
||||
if ( !TextUtils.isEmpty( mLastQAndASpeakText ) ) {
|
||||
IMogoVoiceCmdCallBack cmdCallBack = mQAndAMap.remove( mLastQAndASpeakText );
|
||||
if ( cmdCallBack != null ) {
|
||||
cmdCallBack.onCmdCancel( speakText );
|
||||
}
|
||||
@@ -98,7 +111,7 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
@Override
|
||||
public void onSpeakEnd( String speakText ) {
|
||||
if ( mQAndAMap.containsKey( speakText ) ) {
|
||||
mLastQAndAspeakText = speakText;
|
||||
mLastQAndASpeakText = speakText;
|
||||
IMogoVoiceCmdCallBack cmdCallBack = mQAndAMap.get( speakText );
|
||||
if ( cmdCallBack != null ) {
|
||||
cmdCallBack.onSpeakEnd( speakText );
|
||||
@@ -114,8 +127,8 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
@Override
|
||||
public void onSpeakSelectTimeOut( String speakText ) {
|
||||
if ( mQAndAMap.containsKey( speakText ) ) {
|
||||
if ( TextUtils.equals( speakText, mLastQAndAspeakText ) ) {
|
||||
mLastQAndAspeakText = null;
|
||||
if ( TextUtils.equals( speakText, mLastQAndASpeakText ) ) {
|
||||
mLastQAndASpeakText = null;
|
||||
}
|
||||
IMogoVoiceCmdCallBack cmdCallBack = mQAndAMap.remove( speakText );
|
||||
if ( cmdCallBack != null ) {
|
||||
@@ -136,8 +149,10 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
*/
|
||||
public void speakTTSVoice( String text, IMogoVoiceCmdCallBack callBack ) {
|
||||
try {
|
||||
mSpeakVoiceMap.put( text, callBack );
|
||||
mVoiceClient.speakDefault( text );
|
||||
if ( mHasFlush ) {
|
||||
mSpeakVoiceMap.put( text, callBack );
|
||||
mVoiceClient.speakDefault( text );
|
||||
}
|
||||
} catch ( Exception e ) {
|
||||
}
|
||||
}
|
||||
@@ -149,7 +164,9 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
*/
|
||||
public void speakTTSVoice( String text ) {
|
||||
try {
|
||||
mVoiceClient.speakDefault( text );
|
||||
if ( mHasFlush ) {
|
||||
mVoiceClient.speakDefault( text );
|
||||
}
|
||||
} catch ( Exception e ) {
|
||||
}
|
||||
}
|
||||
@@ -163,8 +180,10 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
*/
|
||||
public void speakTTSVoice( String text, VoicePreemptType type, IMogoVoiceCmdCallBack callBack ) {
|
||||
try {
|
||||
mSpeakVoiceMap.put( text, callBack );
|
||||
mVoiceClient.speakTypeText( text, type.getPreemptType() );
|
||||
if ( mHasFlush ) {
|
||||
mSpeakVoiceMap.put( text, callBack );
|
||||
mVoiceClient.speakTypeText( text, type.getPreemptType() );
|
||||
}
|
||||
} catch ( Exception e ) {
|
||||
}
|
||||
}
|
||||
@@ -175,8 +194,10 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
* @param tts 播报内容
|
||||
*/
|
||||
public void speakQAndACmd( String tts, IMogoVoiceCmdCallBack callBack ) {
|
||||
mQAndAMap.put( tts, callBack );
|
||||
mVoiceClient.speakTtsAndRegistCmd( tts );
|
||||
if ( mHasFlush ) {
|
||||
mQAndAMap.put( tts, callBack );
|
||||
mVoiceClient.speakTtsAndRegistCmd( tts );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,8 +208,10 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
* @param cancelCmds 取消命令唤醒词
|
||||
*/
|
||||
public void speakQAndACmd( String tts, String[] okCmds, String[] cancelCmds, IMogoVoiceCmdCallBack callBack ) {
|
||||
mQAndAMap.put( tts, callBack );
|
||||
mVoiceClient.speakTtsAndRegistCmd( tts, okCmds, cancelCmds );
|
||||
if ( mHasFlush ) {
|
||||
mQAndAMap.put( tts, callBack );
|
||||
mVoiceClient.speakTtsAndRegistCmd( tts, okCmds, cancelCmds );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -200,10 +223,17 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
*/
|
||||
public void registerUnWakeupCommand( String cmd, String[] cmdWords, IMogoVoiceCmdCallBack callBack ) {
|
||||
if ( !mCmdMap.containsKey( cmd ) ) {
|
||||
mCmdMap.put( cmd, new ArrayList< IMogoVoiceCmdCallBack >() );
|
||||
mCmdMap.put( cmd, new ArrayList<>() );
|
||||
}
|
||||
mCmdMap.get( cmd ).add( callBack );
|
||||
mVoiceClient.registerCustomWakeupCmd( cmd, cmdWords );
|
||||
|
||||
if ( mHasFlush ) {
|
||||
mVoiceClient.registerCustomWakeupCmd( cmd, cmdWords );
|
||||
mCacheUnWakeupCommands.remove( cmd );
|
||||
} else {
|
||||
Logger.i( TAG, "cache un wakeup command2. %s", cmd );
|
||||
mCacheUnWakeupCommands.put( cmd, cmdWords );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,7 +243,13 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
* @param cmdWords
|
||||
*/
|
||||
public void registerUnWakeupCommand( String cmd, String[] cmdWords ) {
|
||||
mVoiceClient.registerCustomWakeupCmd( cmd, cmdWords );
|
||||
if ( mHasFlush ) {
|
||||
mVoiceClient.registerCustomWakeupCmd( cmd, cmdWords );
|
||||
mCacheUnWakeupCommands.remove( cmd );
|
||||
} else {
|
||||
Logger.i( TAG, "cache un wakeup command. %s", cmd );
|
||||
mCacheUnWakeupCommands.put( cmd, cmdWords );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -224,7 +260,7 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
*/
|
||||
public synchronized void registerUnWakeupCommandCallback( String cmd, IMogoVoiceCmdCallBack callBack ) {
|
||||
if ( !mCmdMap.containsKey( cmd ) ) {
|
||||
mCmdMap.put( cmd, new ArrayList< IMogoVoiceCmdCallBack >() );
|
||||
mCmdMap.put( cmd, new ArrayList<>() );
|
||||
}
|
||||
mCmdMap.get( cmd ).add( callBack );
|
||||
}
|
||||
@@ -237,6 +273,7 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
public synchronized void unregisterUnWakeupCommand( String cmd ) {
|
||||
mCmdMap.remove( cmd );
|
||||
mVoiceClient.unRegisterCustomWakeupCmd( cmd );
|
||||
mCacheUnWakeupCommands.remove( cmd );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,11 +282,13 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
* @param cmd
|
||||
*/
|
||||
public synchronized void unregisterUnWakeupCommand( String cmd, IMogoVoiceCmdCallBack callBack ) {
|
||||
if ( !mCmdMap.containsKey( cmd ) ) {
|
||||
return;
|
||||
mCacheUnWakeupCommands.remove( cmd );
|
||||
if ( mCmdMap.containsKey( cmd ) ) {
|
||||
List< IMogoVoiceCmdCallBack > callBacks = mCmdMap.get( cmd );
|
||||
if ( callBacks != null ) {
|
||||
callBacks.remove( callBack );
|
||||
}
|
||||
}
|
||||
List< IMogoVoiceCmdCallBack > callBacks = mCmdMap.get( cmd );
|
||||
callBacks.remove( callBack );
|
||||
}
|
||||
|
||||
public static void startAssistant( Context context ) {
|
||||
@@ -260,4 +299,35 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
intent.putExtra( "extra_switch_type", "window_start_cancel" );
|
||||
context.sendBroadcast( intent );
|
||||
}
|
||||
|
||||
public synchronized void flush() {
|
||||
if ( mCacheUnWakeupCommands.isEmpty() ) {
|
||||
return;
|
||||
}
|
||||
mHasFlush = true;
|
||||
Logger.d( TAG, "flush cache voice command when voice service ready." );
|
||||
for ( String cmd : mCacheUnWakeupCommands.keySet() ) {
|
||||
registerUnWakeupCommand( cmd, mCacheUnWakeupCommands.get( cmd ) );
|
||||
}
|
||||
mCacheUnWakeupCommands.clear();
|
||||
}
|
||||
|
||||
private boolean isVoiceServiceReady( Context context ) {
|
||||
if ( isRunningTaskExist( context, "com.zhidao.speech" )
|
||||
&& isRunningTaskExist( context, "com.zhidao.speech.adapter" ) ) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isRunningTaskExist( Context context, String processName ) {
|
||||
ActivityManager am = ( ActivityManager ) context.getSystemService( Context.ACTIVITY_SERVICE );
|
||||
List< ActivityManager.RunningAppProcessInfo > processList = am.getRunningAppProcesses();
|
||||
for ( ActivityManager.RunningAppProcessInfo info : processList ) {
|
||||
if ( info.processName.equals( processName ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user