导航菜单

页面标题

页面副标题

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

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

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


package com.huawei.hiassistant.voice.intentionhandler;

import android.content.Intent;
import android.text.TextUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.huawei.hiassistant.platform.base.VoiceKitSdkContext;
import com.huawei.hiassistant.platform.base.bean.CommonHeader;
import com.huawei.hiassistant.platform.base.bean.DecisionServiceParams;
import com.huawei.hiassistant.platform.base.bean.ErrorInfo;
import com.huawei.hiassistant.platform.base.bean.recognize.CmdData;
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.RecognizeContext;
import com.huawei.hiassistant.platform.base.bean.recognize.Session;
import com.huawei.hiassistant.platform.base.bean.recognize.VoiceEvent;
import com.huawei.hiassistant.platform.base.bean.recognize.VoiceKitMessage;
import com.huawei.hiassistant.platform.base.bean.recognize.payload.Header;
import com.huawei.hiassistant.platform.base.bean.recognize.payload.Payload;
import com.huawei.hiassistant.platform.base.bean.util.GsonUtils;
import com.huawei.hiassistant.platform.base.fullduplex.FullDuplex;
import com.huawei.hiassistant.platform.base.module.DirectivesManagerInterface;
import com.huawei.hiassistant.platform.base.module.IntentionHandlerInterface;
import com.huawei.hiassistant.platform.base.module.ModuleInstanceFactory;
import com.huawei.hiassistant.platform.base.msg.AssistantMessage;
import com.huawei.hiassistant.platform.base.northinterface.recognize.RecognizerIntent;
import com.huawei.hiassistant.platform.base.timedelay.TimeDelayConstants;
import com.huawei.hiassistant.platform.base.util.BaseUtils;
import com.huawei.hiassistant.platform.base.util.BusinessFlowId;
import com.huawei.hiassistant.platform.base.util.DirectiveUtil;
import com.huawei.hiassistant.platform.base.util.IAssistantConfig;
import com.huawei.hiassistant.platform.base.util.KitLog;
import com.huawei.hiassistant.platform.base.util.SecureIntentUtil;
import com.huawei.hiassistant.voice.R$string;
import com.huawei.hiassistant.voice.abilityconnector.recognizer.local.dm.DecisionLearnParam;
import com.huawei.hiassistant.voice.abilityconnector.recognizer.local.nlu.NluResult;
import com.huawei.hiassistant.voice.common.bean.DialogRequestParam;
import com.huawei.hiassistant.voice.intentionhandler.VoiceIntentionHandlerImpl;
import com.huawei.hms.ml.language.common.utils.Constant;
import defpackage.bcb;
import defpackage.bhb;
import defpackage.di7;
import defpackage.dna;
import defpackage.fgb;
import defpackage.fna;
import defpackage.gna;
import defpackage.ynb;
import defpackage.zg7;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
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 VoiceIntentionHandlerImpl implements IntentionHandlerInterface {
    private static final int DEFAULT_LIST_CAPACITY = 10;
    private static final int ERROR_CODE_SUCCESS = 0;
    private static final int EVENT_ID_FAILED = 1007;
    private static final int EVENT_ID_SUCCESS = 1006;
    private static final String KEY_ASR_NAME = "UserInteraction";
    private static final String KEY_ASR_NAMESPACE = "DisplayASR";
    private static final String KEY_FINAL_NLU_RESULT = "finalNluResult";
    private static final String KEY_INTENTIONS = "intentions";
    private static final String KEY_NAME = "NLPRecognizer";
    private static final String KEY_NAMESPACE = "DialogInfo";
    private static final String KEY_TEXT = "text";
    private static final String STRING_TEXT = "text";
    private static final String STRING_USER_INTERACTION = "UserInteraction";
    private static final String TAG = "VoiceIntentionHandlerImpl";
    private static final String UNEXPECTED_INTENTION_LOG_PREFIX = "unexpected intention handle result type ";
    private DirectivesManagerInterface directivesManager;
    private long intentHandleTime = 0;
    private IntentionHandlerInterface.CallBack intentionHandlerCallBack = new IntentionHandlerInterface.CallBack() {
        @Override
        public void onDisResult(CmdData cmdData) {
            KitLog.error(VoiceIntentionHandlerImpl.TAG, "unexpected method onDisResult call");
        }

        @Override
        public void onDmPartialResult(VoiceKitMessage voiceKitMessage) {
            KitLog.error(VoiceIntentionHandlerImpl.TAG, "unexpected method onDmPartialResult call");
        }

        @Override
        public void onEventResult(VoiceKitMessage voiceKitMessage) {
            KitLog.error(VoiceIntentionHandlerImpl.TAG, "unexpected method onEventResult call");
        }

        @Override
        public void onResult(VoiceKitMessage voiceKitMessage) {
            KitLog.error(VoiceIntentionHandlerImpl.TAG, "unexpected method onResult call");
        }

        @Override
        public void onUpdateVoiceContextResult(VoiceKitMessage voiceKitMessage) {
            KitLog.error(VoiceIntentionHandlerImpl.TAG, "unexpected method onUpdateVoiceContextResult call");
        }
    };
    private boolean isViaGreen;

    private String constructDmServiceParam(Session session, final String str, String str2) {
        Optional map = Optional.ofNullable(str).filter(new Predicate() {
            @Override
            public final boolean test(Object obj) {
                boolean lambda$constructDmServiceParam$3;
                lambda$constructDmServiceParam$3 = VoiceIntentionHandlerImpl.lambda$constructDmServiceParam$3(str, (String) obj);
                return lambda$constructDmServiceParam$3;
            }
        }).map(new Function() {
            @Override
            public final Object apply(Object obj) {
                NluResult lambda$constructDmServiceParam$4;
                lambda$constructDmServiceParam$4 = VoiceIntentionHandlerImpl.lambda$constructDmServiceParam$4(str, (String) obj);
                return lambda$constructDmServiceParam$4;
            }
        });
        Session session2 = (Session) map.map(new di7()).orElse(session);
        String str3 = (String) Optional.ofNullable(session2).map(new zg7()).orElse("");
        KitLog.debug(TAG, "constructDmServiceParam session={}", session2);
        DialogRequestParam dialogRequestParam = (DialogRequestParam) GsonUtils.toBean(FullDuplex.stateManager().getContextByAudioStreamId(str3), DialogRequestParam.class);
        if (dialogRequestParam == null) {
            dialogRequestParam = getCurrentContext(new DialogRequestParam(session2), str2);
        }
        Optional filter = map.map(new bcb()).map(new dna()).filter(new Predicate() {
            @Override
            public final boolean test(Object obj) {
                boolean lambda$constructDmServiceParam$5;
                lambda$constructDmServiceParam$5 = VoiceIntentionHandlerImpl.lambda$constructDmServiceParam$5((String) obj);
                return lambda$constructDmServiceParam$5;
            }
        });
        Objects.requireNonNull(dialogRequestParam);
        filter.map(new fna(dialogRequestParam));
        map.ifPresent(new gna(dialogRequestParam));
        if (session2 == null || !session2.isFullDuplexMode()) {
            dialogRequestParam.getSession().setMessageName(MessageConstants.MessageName.MSG_NAME_START_RECOGNIZE).setReceiver(MessageConstants.Receiver.MSG_RECEIVER_CLOUD_DM);
        } else {
            dialogRequestParam.getSession().setMessageName(MessageConstants.MessageName.MSG_NAME_START_RECOGNIZE_BY_FULLDUPLEX).setReceiver(MessageConstants.Receiver.MSG_RECEIVER_CLOUD_DM);
        }
        ynb.g(dialogRequestParam, ModuleInstanceFactory.Ability.visible().getCachedVisibleInfo());
        String json = GsonUtils.toJson(dialogRequestParam);
        KitLog.debug(TAG, "constructDmServiceParam dmServiceParam ={}", json);
        return json;
    }

    private String extractAsrTextFromDm(VoiceKitMessage voiceKitMessage) {
        HeaderPayload voicePayload;
        return (voiceKitMessage == null || (voicePayload = voiceKitMessage.getVoicePayload("UserInteraction", KEY_ASR_NAMESPACE)) == null || voicePayload.getPayload() == null || voicePayload.getPayload().getJsonObject() == null || voicePayload.getPayload().getJsonObject().get("text") == null) ? "" : voicePayload.getPayload().getJsonObject().get("text").getAsString();
    }

    private JsonArray extractIntentionsFromDm(VoiceKitMessage voiceKitMessage) {
        JsonObject asJsonObject;
        JsonArray jsonArray = new JsonArray();
        if (voiceKitMessage == null) {
            return jsonArray;
        }
        HeaderPayload voicePayload = voiceKitMessage.getVoicePayload(KEY_NAMESPACE, KEY_NAME);
        return (voicePayload == null || voicePayload.getPayload() == null || voicePayload.getPayload().getJsonObject() == null || (asJsonObject = voicePayload.getPayload().getJsonObject().getAsJsonObject(KEY_FINAL_NLU_RESULT)) == null) ? jsonArray : asJsonObject.getAsJsonArray(KEY_INTENTIONS);
    }

    private DialogRequestParam getCurrentContext(DialogRequestParam dialogRequestParam, String str) {
        RecognizeContext recognizeContext;
        if (dialogRequestParam == null) {
            KitLog.error(TAG, "getCurrentContext dialogRequestParam is null");
            return dialogRequestParam;
        }
        if (TextUtils.isEmpty(str)) {
            str = (String) VoiceKitSdkContext.getInstance().get(RecognizerIntent.EXT_VOICE_CONTEXT, String.class).orElse("");
        }
        KitLog.debug(TAG, "constructDmServiceParam clientContext: {}", str);
        if (!TextUtils.isEmpty(str) && (recognizeContext = (RecognizeContext) GsonUtils.toBean(str, RecognizeContext.class)) != null) {
            KitLog.debug(TAG, "constructDmServiceParam recognizeContext: {}, contexts: {}", recognizeContext, Integer.valueOf(recognizeContext.getContexts().size()));
            dialogRequestParam.addContexts(recognizeContext.getContexts());
            dialogRequestParam.addEvents(recognizeContext.getEvents());
        }
        return dialogRequestParam;
    }

    public static boolean lambda$constructDmServiceParam$3(String str, String str2) {
        return !TextUtils.isEmpty(str);
    }

    public static NluResult lambda$constructDmServiceParam$4(String str, String str2) {
        return (NluResult) GsonUtils.toBean(str, NluResult.class);
    }

    public static boolean lambda$constructDmServiceParam$5(String str) {
        return str != null;
    }

    public static boolean lambda$handleBusinessAbort$1(Intent intent) {
        return TextUtils.equals(RecognizerIntent.STOP_ALL_BUSINESS, SecureIntentUtil.getSecureIntentString(intent, RecognizerIntent.EXT_STOP_TYPE));
    }

    public void lambda$handleBusinessAbort$2(Session session, Intent intent) {
        notifyDmAbortBusiness(session);
    }

    public void lambda$startIntentionHandler$0(Session session, String str) {
        ModuleInstanceFactory.Ability.recognize().startDialogProcess(session, constructDmServiceParam(session, str, ""), null);
    }

    private void notifyDmAbortBusiness(Session session) {
        KitLog.info(TAG, "notifyDmAbortBusiness");
        VoiceEvent voiceEvent = new VoiceEvent();
        Header header = new Header("AbortBusiness", "System");
        Payload payload = new Payload();
        HeaderPayload headerPayload = new HeaderPayload();
        headerPayload.setHeader(header);
        headerPayload.setPayload(payload);
        voiceEvent.getEvents().add(headerPayload);
        ModuleInstanceFactory.Ability.recognize().updateVoiceEvent(session, GsonUtils.toJson(voiceEvent));
    }

    private void preCheckDirectives(VoiceKitMessage voiceKitMessage) {
        if (voiceKitMessage == null) {
            KitLog.warn(TAG, "preCheckDirectives msg is null");
            return;
        }
        List<HeaderPayload> directives = voiceKitMessage.getDirectives();
        List<HeaderPayload> preCheckDirectives = this.directivesManager.preCheckDirectives(voiceKitMessage, new ArrayList(directives));
        if (preCheckDirectives == null || preCheckDirectives.isEmpty()) {
            KitLog.debug(TAG, "preCheckDirectives is null or empty", new Object[0]);
            return;
        }
        Collections.reverse(preCheckDirectives);
        Iterator<HeaderPayload> it = preCheckDirectives.iterator();
        while (it.hasNext()) {
            directives.add(0, it.next());
        }
    }

    @Override
    public void handleBusinessAbort(AssistantMessage<?> assistantMessage) {
        if (assistantMessage == null) {
            KitLog.warn(TAG, "handleBusinessAbort message is null");
        } else {
            final CommonHeader header = assistantMessage.getHeader();
            assistantMessage.getBody(Intent.class).filter(new Predicate() {
                @Override
                public final boolean test(Object obj) {
                    boolean lambda$handleBusinessAbort$1;
                    lambda$handleBusinessAbort$1 = VoiceIntentionHandlerImpl.lambda$handleBusinessAbort$1((Intent) obj);
                    return lambda$handleBusinessAbort$1;
                }
            }).ifPresent(new Consumer() {
                @Override
                public final void accept(Object obj) {
                    VoiceIntentionHandlerImpl.this.lambda$handleBusinessAbort$2(header, (Intent) obj);
                }
            });
        }
    }

    @Override
    public void handleCallDecisionService(AssistantMessage<?> assistantMessage) {
        KitLog.debug(TAG, "intentHandler externalCallDs", new Object[0]);
        if (assistantMessage == null) {
            KitLog.info(TAG, "externalCallDs message is null");
            return;
        }
        Object body = assistantMessage.getBody();
        if (body instanceof DecisionServiceParams) {
            bhb.a().b((DecisionServiceParams) body);
        } else {
            KitLog.warn(TAG, "message body data type not match");
        }
    }

    @Override
    public void handleDisResult(AssistantMessage<?> assistantMessage) {
        KitLog.info(TAG, "handleDisResult");
        if (assistantMessage == null) {
            KitLog.info(TAG, "disResult is null");
            return;
        }
        if (assistantMessage.getBody() instanceof String) {
            this.intentionHandlerCallBack.onDisResult((CmdData) GsonUtils.toBean((String) assistantMessage.getBody(), CmdData.class));
            return;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(UNEXPECTED_INTENTION_LOG_PREFIX);
        sb.append(assistantMessage.getBody() == null ? TimeDelayConstants.INIT_IDENTIFICATION : assistantMessage.getBody().getClass().toString());
        KitLog.warn(TAG, sb.toString());
    }

    @Override
    public void handleDmWaitingResult() {
        HeaderPayload buildSpeakDirective = DirectiveUtil.buildSpeakDirective(IAssistantConfig.getInstance().getString(R$string.tts_network_waiting));
        VoiceKitMessage voiceKitMessage = new VoiceKitMessage();
        voiceKitMessage.getDirectives().add(buildSpeakDirective);
        KitLog.info(TAG, String.format(Locale.ROOT, "onDmResult waiting dm %s ", voiceKitMessage.getSecureDmResult()));
        this.intentionHandlerCallBack.onEventResult(voiceKitMessage);
    }

    @Override
    public void handleEventResult(AssistantMessage<?> assistantMessage) {
        if (assistantMessage == null) {
            return;
        }
        Object body = assistantMessage.getBody();
        if (body instanceof VoiceKitMessage) {
            this.intentionHandlerCallBack.onEventResult((VoiceKitMessage) body);
            return;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(UNEXPECTED_INTENTION_LOG_PREFIX);
        sb.append(body == null ? TimeDelayConstants.INIT_IDENTIFICATION : body.getClass().toString());
        KitLog.warn(TAG, sb.toString());
    }

    @Override
    public void handleIntentionExecutorResult(VoiceKitMessage voiceKitMessage) {
        KitLog.debug(TAG, "handleIntentionExecutorResult", new Object[0]);
        if (voiceKitMessage == null) {
            KitLog.warn(TAG, "result is null");
            return;
        }
        String packageName = IAssistantConfig.getInstance().getAppContext().getPackageName();
        Session session = voiceKitMessage.getSession();
        if (session != null) {
            session.setSender(packageName).setReceiver(MessageConstants.Receiver.MSG_RECEIVER_DS);
        }
        DecisionLearnParam.EventInfo eventInfo = new DecisionLearnParam.EventInfo();
        long j = this.intentHandleTime;
        if (j == 0) {
            j = System.currentTimeMillis();
        }
        eventInfo.setTimeStamp(j).setSession(session).setApp(packageName).setOrigText(extractAsrTextFromDm(voiceKitMessage)).setViaGreen(this.isViaGreen).setIntentions(extractIntentionsFromDm(voiceKitMessage));
        ArrayList arrayList = new ArrayList(10);
        arrayList.add(eventInfo);
        bhb.a().c(new DecisionLearnParam().setEventId(voiceKitMessage.getErrorCode() == 0 ? 1006 : 1007).setLocal(true).setEventInfo(arrayList));
        this.intentHandleTime = 0L;
        fgb.a(voiceKitMessage);
    }

    @Override
    public void handleIntentionHandlerPartialResult(AssistantMessage<?> assistantMessage) {
        VoiceKitMessage voiceKitMessage;
        if (assistantMessage == null) {
            return;
        }
        Object body = assistantMessage.getBody();
        if (body instanceof ErrorInfo) {
            ErrorInfo errorInfo = (ErrorInfo) body;
            KitLog.info(TAG, "onDmResult error " + errorInfo.getErrorCode() + Constant.BLANK_SPACE + errorInfo.getErrorMsg());
            voiceKitMessage = VoiceKitMessage.buildErrorInfoMessage(errorInfo, errorInfo.getSession() != null ? errorInfo.getSession() : assistantMessage.getHeader());
        } else {
            if (!(body instanceof VoiceKitMessage)) {
                StringBuilder sb = new StringBuilder();
                sb.append(UNEXPECTED_INTENTION_LOG_PREFIX);
                sb.append(body == null ? TimeDelayConstants.INIT_IDENTIFICATION : body.getClass().toString());
                KitLog.warn(TAG, sb.toString());
                return;
            }
            voiceKitMessage = (VoiceKitMessage) body;
        }
        if (!MessageConstants.MessageName.MSG_NAME_DIALOG_CONTROL.equals(voiceKitMessage.getSession().getMessageName())) {
            preCheckDirectives(voiceKitMessage);
        }
        this.intentionHandlerCallBack.onDmPartialResult(voiceKitMessage);
    }

    @Override
    public void handleIntentionHandlerResult(AssistantMessage<?> assistantMessage) {
        VoiceKitMessage voiceKitMessage;
        if (assistantMessage == null) {
            return;
        }
        Object body = assistantMessage.getBody();
        if (body instanceof ErrorInfo) {
            ErrorInfo errorInfo = (ErrorInfo) body;
            KitLog.info(TAG, "onDmResult error " + errorInfo.getErrorCode() + Constant.BLANK_SPACE + errorInfo.getErrorMsg());
            voiceKitMessage = VoiceKitMessage.buildErrorInfoMessage(errorInfo, errorInfo.getSession() != null ? errorInfo.getSession() : assistantMessage.getHeader());
        } else {
            if (!(body instanceof VoiceKitMessage)) {
                StringBuilder sb = new StringBuilder();
                sb.append(UNEXPECTED_INTENTION_LOG_PREFIX);
                sb.append(body == null ? TimeDelayConstants.INIT_IDENTIFICATION : body.getClass().toString());
                KitLog.warn(TAG, sb.toString());
                return;
            }
            voiceKitMessage = (VoiceKitMessage) body;
        }
        this.intentHandleTime = System.currentTimeMillis();
        if (!MessageConstants.MessageName.MSG_NAME_DIALOG_CONTROL.equals(voiceKitMessage.getSession().getMessageName())) {
            preCheckDirectives(voiceKitMessage);
        }
        this.intentionHandlerCallBack.onResult(voiceKitMessage);
        ModuleInstanceFactory.Ability.caDonationService().donationsResponseAsyn(voiceKitMessage, null);
    }

    @Override
    public void handleLastResultAcquired(AssistantMessage<?> assistantMessage) {
        if (assistantMessage == null) {
            KitLog.warn(TAG, "handleLastResultAcquired assistantMessage is null");
            return;
        }
        CommonHeader header = assistantMessage.getHeader();
        if (header == null) {
            KitLog.warn(TAG, "handleLastResultAcquired session is null");
            return;
        }
        if (header.getInteractionId() < BusinessFlowId.getInstance().getInteractionId()) {
            KitLog.info(TAG, "onLastAsrAcquired not current session");
            return;
        }
        HeaderPayload headerPayload = new HeaderPayload();
        headerPayload.setHeader(new Header("ASREnded", "System"));
        headerPayload.setPayload(new Payload());
        VoiceKitMessage voiceKitMessage = new VoiceKitMessage();
        voiceKitMessage.getDirectives().add(headerPayload);
        voiceKitMessage.setSession(header);
        this.intentionHandlerCallBack.onEventResult(voiceKitMessage);
    }

    @Override
    public void handleUpdateVoiceContextResult(AssistantMessage<?> assistantMessage) {
        if (assistantMessage == null) {
            return;
        }
        Object body = assistantMessage.getBody();
        if (body instanceof VoiceKitMessage) {
            VoiceKitMessage voiceKitMessage = (VoiceKitMessage) body;
            KitLog.info(TAG, String.format(Locale.ROOT, "onDmResult context %s ", voiceKitMessage.getSecureDmResult()));
            preCheckDirectives(voiceKitMessage);
            this.intentionHandlerCallBack.onUpdateVoiceContextResult(voiceKitMessage);
            return;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(UNEXPECTED_INTENTION_LOG_PREFIX);
        sb.append(body == null ? TimeDelayConstants.INIT_IDENTIFICATION : body.getClass().toString());
        KitLog.warn(TAG, sb.toString());
    }

    @Override
    public void init(IntentionHandlerInterface.CallBack callBack, DirectivesManagerInterface directivesManagerInterface) {
        if (callBack == null) {
            KitLog.error(TAG, "IntentionHandlerInterface callBack null");
        }
        this.directivesManager = directivesManagerInterface;
        this.intentionHandlerCallBack = callBack;
    }

    @Override
    public void startIntentionHandler(AssistantMessage<?> assistantMessage) {
        if (assistantMessage == null) {
            return;
        }
        final CommonHeader header = assistantMessage.getHeader();
        Object body = assistantMessage.getBody();
        if (body == null || !(body instanceof Intent)) {
            assistantMessage.getBody(String.class).ifPresent(new Consumer() {
                @Override
                public final void accept(Object obj) {
                    VoiceIntentionHandlerImpl.this.lambda$startIntentionHandler$0(header, (String) obj);
                }
            });
            return;
        }
        Intent intent = (Intent) BaseUtils.getTargetInstance(body, Intent.class).get();
        if (intent.hasExtra("text")) {
            ModuleInstanceFactory.Ability.recognize().startDialogProcess(header, constructDmServiceParam(header, SecureIntentUtil.getSecureIntentString(intent, "text"), TextUtils.equals(SecureIntentUtil.getSecureIntentString(intent, "dataType"), RecognizerIntent.NLU_DATA_TYPE) ? SecureIntentUtil.getSecureIntentString(intent, RecognizerIntent.EXT_VOICE_CONTEXT) : ""), intent);
        } else {
            ModuleInstanceFactory.Ability.recognize().startDialogProcess(header, SecureIntentUtil.getSecureIntentString(intent, RecognizerIntent.EXT_VOICE_CONTEXT), intent);
        }
    }
}