opt
This commit is contained in:
@@ -21,18 +21,18 @@ import com.zhidao.auto.platform.voice.VoiceClient;
|
||||
public class MogoVoiceManager implements IMogoVoiceManager {
|
||||
|
||||
@Override
|
||||
public void registerIntentListener( String action, IMogoVoiceListener listener ) {
|
||||
VoiceManager.getInstance().registerIntentListener( action, listener );
|
||||
public void registerIntentListener( String command, IMogoVoiceListener listener ) {
|
||||
VoiceManager.getInstance().registerIntentListener( command, listener );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterIntentListener( String action ) {
|
||||
VoiceManager.getInstance().unregisterIntentListener( action );
|
||||
public void unregisterIntentListener( String command ) {
|
||||
VoiceManager.getInstance().unregisterIntentListener( command );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke( String action, Intent intent ) {
|
||||
VoiceManager.getInstance().invoke( action, intent );
|
||||
public void invoke( String command, Intent intent ) {
|
||||
VoiceManager.getInstance().invoke( command, intent );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.mogo.service.impl.voice;
|
||||
|
||||
import android.content.Intent;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020-01-13
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class MsgObject {
|
||||
|
||||
private final String mCommand;
|
||||
private final Intent mIntent;
|
||||
|
||||
public MsgObject( String command, Intent intent ) {
|
||||
this.mCommand = command;
|
||||
this.mIntent = intent;
|
||||
}
|
||||
|
||||
public String getCommand() {
|
||||
return mCommand;
|
||||
}
|
||||
|
||||
public Intent getIntent() {
|
||||
return mIntent;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,11 @@ package com.mogo.service.impl.voice;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.mogo.service.voice.IMogoVoiceListener;
|
||||
import com.mogo.service.voice.IMogoVoiceManager;
|
||||
@@ -10,6 +15,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
@@ -21,6 +27,25 @@ public class VoiceManager implements IMogoVoiceManager {
|
||||
|
||||
private static volatile VoiceManager sInstance;
|
||||
|
||||
public static final int MSG_COMMAND_RECEIVED = 2000;
|
||||
|
||||
|
||||
private Handler mHandler = new Handler( Looper.getMainLooper() ) {
|
||||
@Override
|
||||
public void handleMessage( @NonNull Message msg ) {
|
||||
super.handleMessage( msg );
|
||||
if ( msg.what == MSG_COMMAND_RECEIVED ) {
|
||||
MsgObject object = ( ( MsgObject ) msg.obj );
|
||||
List< IMogoVoiceListener > listeners = mListeners.get( object.getCommand() );
|
||||
if ( listeners != null && !listeners.isEmpty() ) {
|
||||
for ( IMogoVoiceListener listener : listeners ) {
|
||||
listener.onIntentReceived( object.getCommand(), object.getIntent() );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private VoiceManager() {
|
||||
}
|
||||
|
||||
@@ -40,23 +65,23 @@ public class VoiceManager implements IMogoVoiceManager {
|
||||
}
|
||||
|
||||
|
||||
private Map< String, List< IMogoVoiceListener > > mListeners = new HashMap<>();
|
||||
private Map< String, List< IMogoVoiceListener > > mListeners = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void registerIntentListener( String action, IMogoVoiceListener listener ) {
|
||||
if ( listener == null || action == null ) {
|
||||
public void registerIntentListener( String command, IMogoVoiceListener listener ) {
|
||||
if ( listener == null || command == null ) {
|
||||
return;
|
||||
}
|
||||
if ( !mListeners.containsKey( action ) ) {
|
||||
mListeners.put( action, new ArrayList<>() );
|
||||
if ( !mListeners.containsKey( command ) ) {
|
||||
mListeners.put( command, new ArrayList<>() );
|
||||
}
|
||||
mListeners.get( action ).add( listener );
|
||||
mListeners.get( command ).add( listener );
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterIntentListener( String action ) {
|
||||
mListeners.remove( action );
|
||||
public void unregisterIntentListener( String command ) {
|
||||
mListeners.remove( command );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -65,12 +90,10 @@ public class VoiceManager implements IMogoVoiceManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke( String action, Intent intent ) {
|
||||
List< IMogoVoiceListener > listeners = mListeners.get( action );
|
||||
if ( listeners != null && !listeners.isEmpty() ) {
|
||||
for ( IMogoVoiceListener listener : listeners ) {
|
||||
listener.onIntentReceived( action, intent );
|
||||
}
|
||||
}
|
||||
public void invoke( String command, Intent intent ) {
|
||||
Message msg = Message.obtain();
|
||||
msg.what = MSG_COMMAND_RECEIVED;
|
||||
msg.obj = new MsgObject( command, intent );
|
||||
mHandler.sendMessage( msg );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user