导航菜单

页面标题

页面副标题

Petal Maps v4.7.0.310001 - IntentionExecutor.java 源代码

正在查看: Petal Maps v4.7.0.310001 应用的 IntentionExecutor.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.huawei.hiassistant.platform.framework.intentionexecutor;

import android.text.TextUtils;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.huawei.hiassistant.platform.base.bean.InteractionIdInfo;
import com.huawei.hiassistant.platform.base.bean.llmstatus.StreamingStatusManager;
import com.huawei.hiassistant.platform.base.bean.recognize.DirectiveInfo;
import com.huawei.hiassistant.platform.base.bean.recognize.HeaderPayload;
import com.huawei.hiassistant.platform.base.bean.recognize.MessageConstants;
import com.huawei.hiassistant.platform.base.bean.recognize.Session;
import com.huawei.hiassistant.platform.base.bean.recognize.VoiceKitMessage;
import com.huawei.hiassistant.platform.base.messagebus.SwitchCommand;
import com.huawei.hiassistant.platform.base.messagebus.SwitchConsumer;
import com.huawei.hiassistant.platform.base.module.ExecutorState;
import com.huawei.hiassistant.platform.base.module.IntentionExecutorInterface;
import com.huawei.hiassistant.platform.base.module.PlatformModule;
import com.huawei.hiassistant.platform.base.msg.AssistantMessage;
import com.huawei.hiassistant.platform.base.msg.MessageSparse;
import com.huawei.hiassistant.platform.base.msg.PlatformMsg;
import com.huawei.hiassistant.platform.base.northinterface.recognize.RecognizerIntent;
import com.huawei.hiassistant.platform.base.report.OperationReportUtils;
import com.huawei.hiassistant.platform.base.util.KitLog;
import com.huawei.hiassistant.platform.framework.bus.FrameworkBus;
import com.huawei.hiassistant.platform.framework.bus.msg.MessageSenderInterface;
import com.huawei.hiassistant.platform.framework.intentionexecutor.IntentionExecutor;
import com.huawei.hiassistant.platform.framework.msg.HandlerThreadModule;
import defpackage.agb;
import defpackage.fnb;
import defpackage.joa;
import defpackage.uq1;
import defpackage.xrb;
import defpackage.yna;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public class IntentionExecutor extends HandlerThreadModule {
    private static final int ERROR_NO_ASR_TEXT = 10006;
    private static final String TAG = "IntentionExecutor";
    private agb actionExecutor;
    private fnb allActionsManager;
    private SwitchCommand commands;
    private SwitchConsumer<AssistantMessage<?>> consumers;
    private IntentionExecutorInterface.CallBack intentionExecutorCallBack;

    private IntentionExecutor() {
        super(TAG);
    }

    public void checkCurrentDirectiveSet() {
        xrb i0 = this.actionExecutor.i0();
        if (i0 == null || i0.h().isEmpty()) {
            KitLog.debug(TAG, "checkCurrentDirectiveSet empty", new Object[0]);
            return;
        }
        Session f = i0.f();
        if (f == null) {
            KitLog.debug(TAG, "checkCurrentDirectiveSet session is null", new Object[0]);
        } else {
            KitLog.debug(TAG, "checkCurrentDirectiveSet addActiveInteraction {}", Short.valueOf(f.getInteractionId()));
            FrameworkBus.msg().sendMsg(PlatformModule.INTENTION_EXECUTOR, PlatformModule.ASSISTANT_CONTROLLER, PlatformMsg.CtlExt.INTENTION_EXECUTOR_ADD_ACTIVE_INTERACTION, f);
        }
    }

    private void executeDirectiveByPolicy(String str, VoiceKitMessage voiceKitMessage, boolean z) {
        if (voiceKitMessage.getErrorCode() == 10006) {
            KitLog.info(TAG, "handleNoInputError");
            this.actionExecutor.G("NoInput");
        }
        if (verifyExecStatusByRsp(voiceKitMessage, z)) {
            str.hashCode();
            switch (str) {
                case "ExecuteIndependently":
                    this.actionExecutor.M(voiceKitMessage);
                    break;
                case "QueueAndExecuteCheckDialogChange":
                    updateExecStatusByRsp(voiceKitMessage, z);
                    handleSequentialDirectiveSet(voiceKitMessage);
                    break;
                case "ClearSetAndExecute":
                    sendStopRespondingIfNeeded(voiceKitMessage);
                    updateExecStatusByRsp(voiceKitMessage, z);
                    handleNewDirectiveSet(voiceKitMessage);
                    break;
                case "QueueAndExecute":
                    handleSequentialDirectiveSet(voiceKitMessage);
                    break;
                default:
                    KitLog.debug(TAG, "else policy", new Object[0]);
                    break;
            }
        }
    }

    private void handleDmMessage(AssistantMessage<?> assistantMessage) {
        Object body = assistantMessage.getBody();
        VoiceKitMessage voiceKitMessage = body instanceof VoiceKitMessage ? (VoiceKitMessage) body : null;
        if (isDmRspInValid(voiceKitMessage)) {
            return;
        }
        String directivePolicy = voiceKitMessage.getDirectivePolicy();
        if (TextUtils.isEmpty(directivePolicy)) {
            directivePolicy = messageName2PolicyMapping(voiceKitMessage.getSession().getMessageName());
        }
        executeDirectiveByPolicy(directivePolicy, voiceKitMessage, voiceKitMessage.getSession().isIgnoreVerify());
    }

    private void handleEventMessage(VoiceKitMessage voiceKitMessage) {
        if (voiceKitMessage == null) {
            KitLog.error(TAG, "handleEventMessage: msg.obj type error");
            return;
        }
        List<HeaderPayload> directives = voiceKitMessage.getDirectives();
        if (directives == null || directives.isEmpty()) {
            KitLog.error(TAG, "handleEventMessage: invalid voice response");
            return;
        }
        Session session = voiceKitMessage.getSession();
        if (session == null || !session.isOmt() || !TextUtils.equals(MessageConstants.MessageName.MSG_NAME_POST_RESULT, session.getMessageName())) {
            this.actionExecutor.W(voiceKitMessage);
            return;
        }
        KitLog.info(TAG, "omt post result");
        Optional.of(voiceKitMessage).filter(new Predicate() {
            @Override
            public final boolean test(Object obj) {
                boolean lambda$handleEventMessage$0;
                lambda$handleEventMessage$0 = IntentionExecutor.lambda$handleEventMessage$0((VoiceKitMessage) obj);
                return lambda$handleEventMessage$0;
            }
        }).map(new Function() {
            @Override
            public final Object apply(Object obj) {
                HeaderPayload lambda$handleEventMessage$1;
                lambda$handleEventMessage$1 = IntentionExecutor.lambda$handleEventMessage$1((VoiceKitMessage) obj);
                return lambda$handleEventMessage$1;
            }
        }).map(new yna()).map(new joa()).filter(new Predicate() {
            @Override
            public final boolean test(Object obj) {
                return Objects.nonNull((JsonObject) obj);
            }
        }).map(new Function() {
            @Override
            public final Object apply(Object obj) {
                JsonElement lambda$handleEventMessage$2;
                lambda$handleEventMessage$2 = IntentionExecutor.lambda$handleEventMessage$2((JsonObject) obj);
                return lambda$handleEventMessage$2;
            }
        }).map(new uq1()).filter(new Predicate() {
            @Override
            public final boolean test(Object obj) {
                boolean lambda$handleEventMessage$3;
                lambda$handleEventMessage$3 = IntentionExecutor.lambda$handleEventMessage$3((String) obj);
                return lambda$handleEventMessage$3;
            }
        }).ifPresent(new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.lambda$handleEventMessage$4((String) obj);
            }
        });
        executeDirectiveByPolicy(DirectiveInfo.QUEUE_AND_EXECUTE, voiceKitMessage, voiceKitMessage.getSession().isIgnoreVerify());
    }

    private void handleNewDirectiveSet(VoiceKitMessage voiceKitMessage) {
        KitLog.debug(TAG, "handleNewDirectiveSet", new Object[0]);
        if (this.actionExecutor.U().size() > 0) {
            this.actionExecutor.L();
        }
        this.actionExecutor.g0();
        this.actionExecutor.B(voiceKitMessage);
        OperationReportUtils.getInstance().getDelayTimeRecord().setIntentionExecuteStartTime(System.currentTimeMillis());
        this.actionExecutor.t(1);
    }

    private void handleSequentialDirectiveSet(VoiceKitMessage voiceKitMessage) {
        this.actionExecutor.B(voiceKitMessage);
        int size = this.actionExecutor.U().size();
        KitLog.info(TAG, "handleSequentialDirectiveSet newSize:" + size);
        if (size != 1 || this.actionExecutor.q() == ExecutorState.PAUSING) {
            return;
        }
        this.actionExecutor.g0();
        this.actionExecutor.t(1);
    }

    private void initProcessMsg() {
        SwitchCommand switchCommand = new SwitchCommand();
        this.commands = switchCommand;
        switchCommand.addCommand(PlatformMsg.Ctl.CONTROLLER_BUSINESS_ABORT, new Runnable() {
            @Override
            public final void run() {
                IntentionExecutor.this.processCommandBusinessAbort();
            }
        });
        this.commands.addCommand(PlatformMsg.Ctl.CONTROLLER_CHECK_CURRENT_DIRECTIVE_SET, new Runnable() {
            @Override
            public final void run() {
                IntentionExecutor.this.checkCurrentDirectiveSet();
            }
        });
        SwitchConsumer<AssistantMessage<?>> switchConsumer = new SwitchConsumer<>();
        this.consumers = switchConsumer;
        switchConsumer.addCommand(PlatformMsg.Data.INTENTION_HANDLER_RESULT, new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.processCommandResult((AssistantMessage) obj);
            }
        });
        this.consumers.addCommand(PlatformMsg.Data.INTENTION_HANDLER_DM_PARTIAL_RESULT, new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.processCommandResult((AssistantMessage) obj);
            }
        });
        this.consumers.addCommand(PlatformMsg.Data.INTENTION_HANDLER_UPDATE_VOICE_CONTEXT_RESULT, new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.processCommandResult((AssistantMessage) obj);
            }
        });
        this.consumers.addCommand(PlatformMsg.Data.INTENTION_HANDLER_CONTROL_RESULT, new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.processCommandResult((AssistantMessage) obj);
            }
        });
        this.consumers.addCommand(PlatformMsg.Data.INTENTION_HANDLER_EVENT_RESULT, new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.processCommandEventResult((AssistantMessage) obj);
            }
        });
        this.consumers.addCommand(PlatformMsg.Ctl.INTENTION_EXECUTOR_DISMISS_GUARD_COMPLETED, new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.processCommandDismissGuardCompleted((AssistantMessage) obj);
            }
        });
        this.consumers.addCommand(PlatformMsg.Ctl.ABILITY_CONNECTOR_TTS_COMPLETE, new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.processCommandTtsCompleted((AssistantMessage) obj);
            }
        });
        this.consumers.addCommand(PlatformMsg.Data.INTENTION_EXECUTOR_SEND_ACTION_TO_EXECUTOR, new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.processCommandSendActionToExecutor((AssistantMessage) obj);
            }
        });
        this.consumers.addCommand(PlatformMsg.Data.NORTH_INTERFACE_SUBMIT_INTENTION_ACTION, new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.processCommandSendActionToExecutor((AssistantMessage) obj);
            }
        });
        this.consumers.addCommand(PlatformMsg.CtlExt.INTENTION_EXECUTOR_CALL_RETURN, new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.processCommandCallReturn((AssistantMessage) obj);
            }
        });
        this.consumers.addCommand(PlatformMsg.CtlExt.INTENTION_EXECUTOR_INTERRUPT_TIMER_EXPIRED, new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.processCommandInterruptTimerExpired((AssistantMessage) obj);
            }
        });
        this.consumers.addCommand(PlatformMsg.Data.INTENTION_HANDLER_DIS_RESULT, new Consumer() {
            @Override
            public final void accept(Object obj) {
                IntentionExecutor.this.processCommandDisResult((AssistantMessage) obj);
            }
        });
    }

    private boolean isDmRspInValid(VoiceKitMessage voiceKitMessage) {
        if (voiceKitMessage == null) {
            KitLog.error(TAG, "handleDmMessage msg.obj type error");
            return true;
        }
        if (voiceKitMessage.getSession() == null) {
            KitLog.error(TAG, "handleDmMessage voiceResponse session is null");
            return true;
        }
        if (this.actionExecutor == null) {
            KitLog.error(TAG, "executeForNewDialog actionExecutor is null");
            return true;
        }
        List<HeaderPayload> directives = voiceKitMessage.getDirectives();
        if (directives == null || directives.isEmpty()) {
            KitLog.error(TAG, "handleDmMessage invalid voice response");
            return true;
        }
        String messageName = voiceKitMessage.getSession().getMessageName();
        if (messageName == null) {
            KitLog.error(TAG, "handleDmMessage messageName empty");
            return true;
        }
        KitLog.debug(TAG, "handleDmMessage messageName = {}", messageName);
        return false;
    }

    private boolean isMessageActive(AssistantMessage assistantMessage) {
        short interactionId = assistantMessage.getInteractionId();
        KitLog.debug(TAG, "isMessageActive interactionId={}", Short.valueOf(interactionId));
        if (interactionId == 0 || FrameworkBus.flowState().isInteractionActive(InteractionIdInfo.build(assistantMessage))) {
            return true;
        }
        KitLog.info(TAG, "msg inactive, skip " + MessageSparse.getName(assistantMessage.getType()));
        return false;
    }

    public static boolean lambda$handleEventMessage$0(VoiceKitMessage voiceKitMessage) {
        return voiceKitMessage.getSession().isOmt();
    }

    public static HeaderPayload lambda$handleEventMessage$1(VoiceKitMessage voiceKitMessage) {
        return voiceKitMessage.getVoicePayload("Command", "Omt");
    }

    public static JsonElement lambda$handleEventMessage$2(JsonObject jsonObject) {
        return jsonObject.get("type");
    }

    public static boolean lambda$handleEventMessage$3(String str) {
        return TextUtils.equals(str, "visibleOmt");
    }

    public void lambda$handleEventMessage$4(String str) {
        KitLog.info(TAG, "visible omt");
        if (this.actionExecutor.q() == ExecutorState.RUNNING || this.actionExecutor.q() == ExecutorState.PAUSING) {
            this.actionExecutor.Y().put(RecognizerIntent.DIALOG_FINISHED_BREAK, Boolean.TRUE);
        }
        MessageSenderInterface msg = FrameworkBus.msg();
        PlatformModule platformModule = PlatformModule.INTENTION_EXECUTOR;
        PlatformModule platformModule2 = PlatformModule.INTENTION_HANDLER;
        msg.removeMsg(platformModule, platformModule2, PlatformMsg.Ctl.INTENTION_EXECUTOR_RESULT);
        FrameworkBus.msg().removeMsg(platformModule, platformModule2, PlatformMsg.Ctl.INTENTION_EXECUTOR_SEND_APP_DIALOG_FINISHED);
    }

    private String messageName2PolicyMapping(String str) {
        str.hashCode();
        switch (str) {
            case "lowPriorityDialogResult":
            case "dialogPartialResult":
                break;
            case "updateVoiceContextRsp":
            case "dialogResult":
                break;
            case "postResult":
                break;
            case "dialogControl":
                break;
            default:
                KitLog.warn(TAG, "unknown message name:" + str);
                break;
        }
        return DirectiveInfo.CLEAR_SET_AND_EXECUTE;
    }

    public void processCommandBusinessAbort() {
        this.actionExecutor.d0();
    }

    public void processCommandCallReturn(AssistantMessage<?> assistantMessage) {
        this.actionExecutor.X(assistantMessage);
    }

    public void processCommandDisResult(AssistantMessage<?> assistantMessage) {
        handleDmMessage(assistantMessage);
    }

    public void processCommandDismissGuardCompleted(AssistantMessage<?> assistantMessage) {
        this.actionExecutor.N(assistantMessage);
    }

    public void processCommandEventResult(AssistantMessage<?> assistantMessage) {
        handleEventMessage((VoiceKitMessage) assistantMessage.getBody(VoiceKitMessage.class).orElse(null));
    }

    public void processCommandInterruptTimerExpired(AssistantMessage<?> assistantMessage) {
        this.actionExecutor.f0(assistantMessage);
    }

    private void processCommandOmtTimerExpired(AssistantMessage<?> assistantMessage) {
        this.actionExecutor.f0(assistantMessage);
    }

    public void processCommandResult(AssistantMessage<?> assistantMessage) {
        Object body = assistantMessage.getBody();
        VoiceKitMessage voiceKitMessage = body instanceof VoiceKitMessage ? (VoiceKitMessage) body : null;
        boolean z = voiceKitMessage != null && voiceKitMessage.getSession().isIgnoreVerify();
        KitLog.info(TAG, "ignoreVerify: " + z);
        if (z || isMessageActive(assistantMessage)) {
            handleDmMessage(assistantMessage);
        }
    }

    public void processCommandSendActionToExecutor(AssistantMessage<?> assistantMessage) {
        this.actionExecutor.a0(assistantMessage);
    }

    public void processCommandTtsCompleted(AssistantMessage<?> assistantMessage) {
        if (isMessageActive(assistantMessage)) {
            this.actionExecutor.D(assistantMessage);
        }
    }

    private void reportDelayTimeRecord(VoiceKitMessage voiceKitMessage) {
        if (voiceKitMessage == null) {
            KitLog.warn(TAG, "reportDelayTimeRecord voiceKitMessage is null");
        } else {
            if (voiceKitMessage.getSession() == null) {
                KitLog.warn(TAG, "reportDelayTimeRecord voiceKitMessage session is null");
                return;
            }
            OperationReportUtils.getInstance().getDelayTimeRecord().setIntentionExecuteEndTime(System.currentTimeMillis());
            OperationReportUtils.getInstance().reportDelayTimeRecord();
            OperationReportUtils.getInstance().getDelayTimeRecord().setInteraction(String.valueOf((int) voiceKitMessage.getSession().getInteractionId())).copyCacheValue();
        }
    }

    private void sendStopRespondingIfNeeded(VoiceKitMessage voiceKitMessage) {
        Session f;
        xrb i0 = this.actionExecutor.i0();
        if (i0 == null || i0.h().isEmpty() || (f = i0.f()) == null) {
            return;
        }
        boolean isReject = voiceKitMessage.isReject();
        StreamingStatusManager.StreamingState currentStreamingState = StreamingStatusManager.getInstance().getCurrentStreamingState(InteractionIdInfo.build(f.getSessionId(), f.getInteractionId()));
        if (voiceKitMessage.getSession().isFullDuplexMode()) {
            if ((currentStreamingState == StreamingStatusManager.StreamingState.STREAMING_ON || currentStreamingState == StreamingStatusManager.StreamingState.STREAMING_START) && !isReject) {
                KitLog.debug(TAG, "need to send StopResponding.", new Object[0]);
                FrameworkBus.msg().sendMsg(PlatformModule.INTENTION_EXECUTOR, PlatformModule.ASSISTANT_CONTROLLER, PlatformMsg.Ctl.INTENTION_EXECUTOR_SEND_STOP_RESPONDING, f);
            }
        }
    }

    private void updateExecStatusByRsp(VoiceKitMessage voiceKitMessage, boolean z) {
        boolean z2;
        boolean z3 = true;
        if (voiceKitMessage.getErrorCode() == 0 || this.actionExecutor.q() != ExecutorState.HOLDING) {
            z2 = false;
        } else {
            this.actionExecutor.I(false, z);
            z2 = true;
        }
        if (z) {
            if (voiceKitMessage.getSession().isNewFlow()) {
                KitLog.info(TAG, "new flow, destroy current business.");
                this.actionExecutor.I(false, true);
            }
            z3 = z2;
        } else {
            if (!this.actionExecutor.b0(voiceKitMessage) && (this.actionExecutor.q() == ExecutorState.RUNNING || this.actionExecutor.q() == ExecutorState.PAUSING || this.actionExecutor.q() == ExecutorState.HOLDING)) {
                this.actionExecutor.I(false, false);
            }
            z3 = z2;
        }
        if (!z3 || FrameworkBus.flowState().getInterruptInfo(InteractionIdInfo.build(voiceKitMessage)) == null) {
            return;
        }
        reportDelayTimeRecord(voiceKitMessage);
    }

    private boolean verifyExecStatusByRsp(VoiceKitMessage voiceKitMessage, boolean z) {
        if (!z && voiceKitMessage.getErrorCode() == 0) {
            long dialogId = voiceKitMessage.getSession().getDialogId();
            if (this.actionExecutor.b0(voiceKitMessage)) {
                ExecutorState q = this.actionExecutor.q();
                if ((!TextUtils.equals(voiceKitMessage.getSession().getMessageName(), MessageConstants.MessageName.MSG_NAME_POST_RESULT) && q == ExecutorState.HOLDING) || q == ExecutorState.DESTROYED) {
                    KitLog.error(TAG, "executor error, dialogId= " + dialogId + " state= " + q);
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void destroy() {
        super.destroy();
    }

    @Override
    public void initInHandler() {
        KitLog.debug(TAG, "[platform init] init start", new Object[0]);
        initProcessMsg();
        this.intentionExecutorCallBack = new IntentionExecutorCallBackImpl();
        KitLog.debug(TAG, "AllActionsManager onInit start", new Object[0]);
        fnb fnbVar = new fnb();
        this.allActionsManager = fnbVar;
        fnbVar.c();
        KitLog.debug(TAG, "ActionExecutor onInit continue", new Object[0]);
        this.actionExecutor = new agb(this.intentionExecutorCallBack, this.allActionsManager);
    }

    @Override
    public void processMessage(AssistantMessage<?> assistantMessage) {
        if (assistantMessage == null) {
            return;
        }
        int type = assistantMessage.getType();
        KitLog.info(TAG, "handMsg what = " + type + " name->" + MessageSparse.getName(type));
        if (this.commands.process(String.valueOf(type))) {
            KitLog.debug(TAG, "handMsg end: what = {} name->{}", Integer.valueOf(type), MessageSparse.getName(type));
            return;
        }
        if (this.consumers.process(String.valueOf(type), assistantMessage)) {
            KitLog.debug(TAG, "handMsg end: what = {} name->{}", Integer.valueOf(type), MessageSparse.getName(type));
            return;
        }
        KitLog.info(TAG, "unknown messageType:" + type);
    }
}