导航菜单

页面标题

页面副标题

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

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

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


package com.huawei.hiassistant.voice.wakeup;

import android.content.Intent;
import android.text.TextUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.huawei.hiassistant.platform.base.VoiceKitSdkContext;
import com.huawei.hiassistant.platform.base.bean.ErrorInfo;
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.VoiceKitMessage;
import com.huawei.hiassistant.platform.base.bean.util.GsonUtils;
import com.huawei.hiassistant.platform.base.module.ModuleInstanceFactory;
import com.huawei.hiassistant.platform.base.module.ability.WakeupAbilityInterface;
import com.huawei.hiassistant.platform.base.northinterface.recognize.RecognizerIntent;
import com.huawei.hiassistant.platform.base.northinterface.wakeup.WakeupIntent;
import com.huawei.hiassistant.platform.base.util.AbilityConnectorThread;
import com.huawei.hiassistant.platform.base.util.BusinessFlowId;
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.platform.base.util.UuidUtils;
import com.huawei.hiassistant.platform.base.util.VoiceBusinessFlowCache;
import com.huawei.hiassistant.platform.base.util.VoiceContextManager;
import com.huawei.hiassistant.voice.abilityconnector.recognizer.RecognizeListener;
import com.huawei.hiassistant.voice.abilityconnector.recognizer.local.nlu.NluResult;
import com.huawei.hiassistant.voice.common.bean.DialogRequestParam;
import com.huawei.hiassistant.voice.common.util.CommonDataUtil;
import com.huawei.hiassistant.voice.wakeup.FreeWakeupProxy;
import com.huawei.hiassistant.voice.wakeup.bean.PreWakeupInfoBean;
import com.huawei.hiassistant.voice.wakeup.listener.PreWakeupListener;
import defpackage.a1c;
import defpackage.bcb;
import defpackage.cob;
import defpackage.dna;
import defpackage.fna;
import defpackage.fyb;
import defpackage.gna;
import defpackage.khb;
import defpackage.lnb;
import defpackage.psb;
import defpackage.qub;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import org.json.JSONException;
import org.json.JSONObject;

public class FreeWakeupProxy implements WakeupAbilityInterface {
    private static final String FREE_WAKEUP_INIT_ENGINE_SUCCESS = "freeWakeupInitEngineSuccess";
    private static final int MAXIMUM_INTERVAL_TIME_MS = 1500;
    private static final String NAMESPACE_SYSTEM = "System";
    private static final String NAME_CLIENT_CONTEXT = "ClientContext";
    private static final String TAG = "FreeWakeupProxy";
    private static final String UPLOAD_WAKEUP_TYPE = "Celitalk";
    private static final long WAIT_CLIENT_CONTE_TIME_OUT = 1500;
    private static final String WAKE_UP_WORD = "小艺小艺";
    fyb asrProxy;
    private String cacheNluRes;
    private RecognizeListener confirmWakeupListener;
    DmFreeWakeupAbilityInterface dmProxy;
    private boolean isAppRecord;
    a1c nluProxy;
    private c preWakeupListener;
    WakeupAbilityInterface.Callback wakeupCallback;
    private boolean isInitAsrSuccess = false;
    private boolean isInitNluSuccess = false;
    private boolean isInitDmSuccess = false;
    private boolean isInitEngineFinished = false;
    private int cacheChannel = -1;
    private int initAsrChannel = -1;
    private CountDownLatch countDownLatch = new CountDownLatch(1);
    private String packageName = IAssistantConfig.getInstance().getAppContext().getPackageName();
    private long lastPreWakeUpTime = -1;

    public class c implements PreWakeupListener {
        public String a;

        public c() {
            this.a = null;
        }

        public static void l(JSONObject jSONObject, WakeupAbilityInterface.Callback callback) {
            callback.onWakeup(true, jSONObject.toString());
        }

        public final void g() {
            FreeWakeupProxy.this.dmProxy.init();
        }

        public final void h(int i) {
            Intent intent = new Intent();
            intent.putExtra(WakeupIntent.EXT_ASR_RESULT_CHANNEL, i);
            VoiceContextManager.getInstance().updateClientContext(intent);
            FreeWakeupProxy.this.countDownLatch.countDown();
        }

        public final void i(PreWakeupInfoBean preWakeupInfoBean, int i, WakeupAbilityInterface.Callback callback) {
            callback.onPreWakeup(true, FreeWakeupProxy.this.assembleLastFreeWakeupResult(preWakeupInfoBean, i));
        }

        public final void j(String str, int i) {
            FreeWakeupProxy.this.doDialog(str, i);
        }

        @Override
        public void onAsrResult(final String str, final int i) {
            KitLog.debug(FreeWakeupProxy.TAG, "onAsrResult {} and channel is {}", str, Integer.valueOf(i));
            this.a = str;
            psb.f().n();
            if (TextUtils.isEmpty(str)) {
                KitLog.debug(FreeWakeupProxy.TAG, "asr : empty", new Object[0]);
                return;
            }
            try {
                if (TextUtils.equals(FreeWakeupProxy.WAKE_UP_WORD, str)) {
                    final JSONObject jSONObject = new JSONObject();
                    jSONObject.put("package_name", FreeWakeupProxy.this.packageName);
                    jSONObject.put(WakeupIntent.EXT_WAKEUP_MODE, WakeupIntent.NORMAL_WAKEUP);
                    jSONObject.put(WakeupIntent.EXT_ASR_RESULT_CHANNEL, i);
                    Optional.ofNullable(FreeWakeupProxy.this.wakeupCallback).ifPresent(new Consumer() {
                        @Override
                        public final void accept(Object obj) {
                            FreeWakeupProxy.c.l(jSONObject, (WakeupAbilityInterface.Callback) obj);
                        }
                    });
                } else {
                    Optional.ofNullable(FreeWakeupProxy.this.nluProxy).ifPresent(new Consumer() {
                        @Override
                        public final void accept(Object obj) {
                            ((a1c) obj).analyzeAssistant(str, i);
                        }
                    });
                }
            } catch (JSONException unused) {
                KitLog.error(FreeWakeupProxy.TAG, "onAsrResult error");
                psb.f().q();
            }
        }

        @Override
        public void onBeginningCacheSpeech() {
            KitLog.debug(FreeWakeupProxy.TAG, "onBeginningCacheSpeech", new Object[0]);
            psb.f().k();
        }

        @Override
        public void onInit(int i) {
            KitLog.debug(FreeWakeupProxy.TAG, "initWakeupEngine {}", Integer.valueOf(i));
            if (i == 0) {
                FreeWakeupProxy.this.isInitAsrSuccess = true;
            }
            if (i == 1) {
                FreeWakeupProxy.this.isInitNluSuccess = true;
            }
            if (FreeWakeupProxy.this.isInitAsrSuccess && FreeWakeupProxy.this.isInitNluSuccess) {
                FreeWakeupProxy.this.isInitEngineFinished = true;
                FreeWakeupProxy.this.processInit();
            }
        }

        @Override
        public void onNluResult(final String str, final int i) {
            KitLog.debug(FreeWakeupProxy.TAG, "onNluResult", new Object[0]);
            final PreWakeupInfoBean m = khb.m(str);
            if (m != null) {
                qub.a().g();
                if (FreeWakeupProxy.this.lastPreWakeUpTime != -1 && System.currentTimeMillis() - FreeWakeupProxy.this.lastPreWakeUpTime <= FreeWakeupProxy.WAIT_CLIENT_CONTE_TIME_OUT) {
                    KitLog.warn(FreeWakeupProxy.TAG, "preWake up interval less than 1500ms");
                    return;
                }
                Optional.ofNullable(FreeWakeupProxy.this.wakeupCallback).ifPresent(new Consumer() {
                    @Override
                    public final void accept(Object obj) {
                        FreeWakeupProxy.c.this.i(m, i, (WakeupAbilityInterface.Callback) obj);
                    }
                });
                FreeWakeupProxy.this.lastPreWakeUpTime = System.currentTimeMillis();
                psb.f().h(this.a, FreeWakeupProxy.UPLOAD_WAKEUP_TYPE);
                if (TextUtils.equals(m.getDateType(), WakeupIntent.EXT_INTENT_DATA)) {
                    if (FreeWakeupProxy.this.countDownLatch.getCount() == 0) {
                        FreeWakeupProxy.this.countDownLatch = new CountDownLatch(1);
                    }
                    ModuleInstanceFactory.Tools.THREAD_POOL.execute(new Runnable() {
                        @Override
                        public final void run() {
                            FreeWakeupProxy.c.this.h(i);
                        }
                    });
                    if (FreeWakeupProxy.this.isInitDmSuccess) {
                        AbilityConnectorThread.Recognize.THREAD.post(new Runnable() {
                            @Override
                            public final void run() {
                                FreeWakeupProxy.c.this.j(str, i);
                            }
                        });
                        return;
                    }
                    KitLog.info(FreeWakeupProxy.TAG, "isInitDmSuccess false, cache nlu result");
                    FreeWakeupProxy.this.cacheNluRes = str;
                    FreeWakeupProxy.this.cacheChannel = i;
                    AbilityConnectorThread.Recognize.THREAD.post(new Runnable() {
                        @Override
                        public final void run() {
                            FreeWakeupProxy.c.this.g();
                        }
                    });
                }
            }
        }

        @Override
        public void onServiceDisconnected() {
            KitLog.debug(FreeWakeupProxy.TAG, "onServiceDisconnected", new Object[0]);
            Optional.ofNullable(FreeWakeupProxy.this.wakeupCallback).ifPresent(new Consumer() {
                @Override
                public final void accept(Object obj) {
                    ((WakeupAbilityInterface.Callback) obj).onServiceDisconnected();
                }
            });
        }
    }

    public FreeWakeupProxy() {
        this.preWakeupListener = new c();
        this.confirmWakeupListener = new b();
    }

    public String assembleLastFreeWakeupResult(PreWakeupInfoBean preWakeupInfoBean, int i) {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("package_name", this.packageName);
            jSONObject.put(WakeupIntent.EXT_WAKEUP_MODE, WakeupIntent.FREE_WAKEUP);
            jSONObject.put("dataType", preWakeupInfoBean.getDateType());
            jSONObject.put(WakeupIntent.EXT_DATA, preWakeupInfoBean.getData());
            jSONObject.put(WakeupIntent.EXT_ASR_RESULT_CHANNEL, i);
            return jSONObject.toString();
        } catch (JSONException unused) {
            KitLog.error(TAG, "FreeWakeupResult error");
            psb.f().q();
            return "";
        }
    }

    private String constructDmServiceParam(Session session, final String str) {
        Optional map = Optional.ofNullable(str).filter(new Predicate() {
            @Override
            public final boolean test(Object obj) {
                boolean lambda$constructDmServiceParam$3;
                lambda$constructDmServiceParam$3 = FreeWakeupProxy.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 = FreeWakeupProxy.lambda$constructDmServiceParam$4(str, (String) obj);
                return lambda$constructDmServiceParam$4;
            }
        });
        DialogRequestParam currentContext = getCurrentContext(new DialogRequestParam(session, false));
        Optional filter = map.map(new bcb()).map(new dna()).filter(new Predicate() {
            @Override
            public final boolean test(Object obj) {
                return Objects.nonNull((String) obj);
            }
        });
        Objects.requireNonNull(currentContext);
        filter.map(new fna(currentContext));
        map.ifPresent(new gna(currentContext));
        if (session == null || !session.isFullDuplexMode()) {
            currentContext.getSession().setMessageName(MessageConstants.MessageName.MSG_NAME_START_RECOGNIZE).setReceiver(MessageConstants.Receiver.MSG_RECEIVER_CLOUD_DM);
        } else {
            currentContext.getSession().setMessageName(MessageConstants.MessageName.MSG_NAME_START_RECOGNIZE_BY_FULLDUPLEX).setReceiver(MessageConstants.Receiver.MSG_RECEIVER_CLOUD_DM);
        }
        String json = GsonUtils.toJson(currentContext);
        KitLog.debug(TAG, "constructDmServiceParam dmServiceParam ={}", json);
        return json;
    }

    public void doDialog(String str, int i) {
        try {
            this.countDownLatch.await(WAIT_CLIENT_CONTE_TIME_OUT, TimeUnit.MILLISECONDS);
        } catch (InterruptedException unused) {
            KitLog.warn(TAG, "wait client context update time out.");
        }
        String uuid = UuidUtils.getUuid();
        BusinessFlowId.getInstance().setFreeWakeupSessionId(uuid);
        Session session = new Session();
        session.setSessionId(uuid);
        session.setMessageId(uuid);
        session.setFullDuplexMode(true);
        Session sessionInstanceWithInitialData = CommonDataUtil.getSessionInstanceWithInitialData(session);
        Intent intent = new Intent();
        intent.putExtra(WakeupIntent.EXT_ASR_RESULT_CHANNEL, i);
        this.dmProxy.startDialogProcess(session, constructDmServiceParam(sessionInstanceWithInitialData, str), intent);
    }

    private DialogRequestParam getCurrentContext(DialogRequestParam dialogRequestParam) {
        RecognizeContext recognizeContext;
        JsonObject jsonObject;
        if (dialogRequestParam == null) {
            KitLog.error(TAG, "getCurrentContext dialogRequestParam is null");
            return dialogRequestParam;
        }
        String 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) {
            HeaderPayload contextsPayload = recognizeContext.getContextsPayload(NAME_CLIENT_CONTEXT, NAMESPACE_SYSTEM);
            if (contextsPayload != null && (jsonObject = contextsPayload.getPayload().getJsonObject()) != null) {
                jsonObject.add("startMode", new JsonPrimitive("celiaTalk"));
                jsonObject.addProperty("firstDialog", Boolean.TRUE);
            }
            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 void processInit() {
        KitLog.debug(TAG, "processInit", new Object[0]);
        final Intent intent = new Intent();
        intent.putExtra(WakeupIntent.EXT_ASR_INIT_CHANNEL_MASK, this.initAsrChannel);
        Optional.ofNullable(this.asrProxy).ifPresent(new Consumer() {
            @Override
            public final void accept(Object obj) {
                ((fyb) obj).startRecognize(null, intent);
            }
        });
        try {
            this.wakeupCallback.onInit(new JSONObject().put("initResult", FREE_WAKEUP_INIT_ENGINE_SUCCESS).put(WakeupIntent.EXT_WAKEUP_SELF_RECORD, this.isAppRecord).toString());
        } catch (JSONException unused) {
            KitLog.error(TAG, "init exception");
        }
    }

    @Override
    public void destroy() {
        KitLog.debug(TAG, "destroy", new Object[0]);
        recycleWakeupEngine();
    }

    @Override
    public void initWakeupEngine(Intent intent) {
        KitLog.debug(TAG, "initWakeupEngine", new Object[0]);
        this.isAppRecord = SecureIntentUtil.getSecureIntentBoolean(intent, WakeupIntent.EXT_WAKEUP_SELF_RECORD, false);
        String secureIntentString = SecureIntentUtil.getSecureIntentString(intent, RecognizerIntent.EXT_DEVICE_ID_3RD, "");
        this.initAsrChannel = SecureIntentUtil.getSecureIntentInt(intent, WakeupIntent.EXT_ASR_INIT_CHANNEL_MASK, -1);
        if (!TextUtils.isEmpty(secureIntentString)) {
            VoiceKitSdkContext.getInstance().setRecognizeParam(RecognizerIntent.EXT_DEVICE_ID_3RD, secureIntentString);
        }
        this.asrProxy = new fyb(this.preWakeupListener);
        this.nluProxy = new a1c(this.preWakeupListener);
        this.dmProxy = new cob(this.confirmWakeupListener);
        this.asrProxy.initAsrEngine();
        this.nluProxy.initNluEngine();
    }

    @Override
    public boolean isInitEngineFinished() {
        return this.isInitEngineFinished;
    }

    @Override
    public void recycleWakeupEngine() {
        KitLog.debug(TAG, "recycleWakeupEngine", new Object[0]);
        this.wakeupCallback.onRecycleWakeupEngine();
        qub.a().g();
        psb.f().q();
        psb.f().n();
        this.cacheNluRes = null;
        this.asrProxy.cancelRecognize();
        this.asrProxy.destroy();
        this.nluProxy.destroy();
        this.dmProxy.destroy();
        this.isInitAsrSuccess = false;
        this.isInitNluSuccess = false;
        this.isInitDmSuccess = false;
        this.isInitEngineFinished = false;
    }

    @Override
    public void registerCallback(WakeupAbilityInterface.Callback callback) {
        this.wakeupCallback = callback;
    }

    @Override
    public void writeAudio(final byte[] bArr) {
        KitLog.debug(TAG, "writeAudio single", new Object[0]);
        if (this.isInitEngineFinished) {
            Optional.ofNullable(this.asrProxy).ifPresent(new Consumer() {
                @Override
                public final void accept(Object obj) {
                    ((fyb) obj).writeAudio(bArr);
                }
            });
            psb.f().i(bArr);
        }
    }

    @Override
    public void writeAudio(final byte[] bArr, final int i) {
        KitLog.debug(TAG, "writeAudio multipath and type is {}", Integer.valueOf(i));
        if (this.isInitEngineFinished) {
            Optional.ofNullable(this.asrProxy).ifPresent(new Consumer() {
                @Override
                public final void accept(Object obj) {
                    ((fyb) obj).writeAudio(bArr, i);
                }
            });
        }
    }

    @Override
    public void initConnector() {
    }

    public class b extends lnb {
        public b() {
        }

        public static void d(JSONObject jSONObject, WakeupAbilityInterface.Callback callback) {
            callback.onWakeup(true, jSONObject.toString());
        }

        public final void c() {
            KitLog.debug(FreeWakeupProxy.TAG, "process cache task", new Object[0]);
            FreeWakeupProxy freeWakeupProxy = FreeWakeupProxy.this;
            freeWakeupProxy.doDialog(freeWakeupProxy.cacheNluRes, FreeWakeupProxy.this.cacheChannel);
        }

        @Override
        public void onInit() {
            FreeWakeupProxy.this.isInitDmSuccess = true;
            if (TextUtils.isEmpty(FreeWakeupProxy.this.cacheNluRes)) {
                return;
            }
            AbilityConnectorThread.Recognize.THREAD.post(new Runnable() {
                @Override
                public final void run() {
                    FreeWakeupProxy.b.this.c();
                }
            });
        }

        @Override
        public void onResult(VoiceKitMessage voiceKitMessage) {
            KitLog.info(FreeWakeupProxy.TAG, "onResult");
            VoiceBusinessFlowCache.getInstance().cache(voiceKitMessage);
            try {
                final JSONObject jSONObject = new JSONObject();
                jSONObject.put("package_name", FreeWakeupProxy.this.packageName);
                jSONObject.put(WakeupIntent.EXT_WAKEUP_MODE, WakeupIntent.FREE_WAKEUP);
                jSONObject.put(WakeupIntent.FREE_WAKEUP_SESSION_ID, voiceKitMessage.getSession().getSessionId());
                jSONObject.put("dataType", WakeupIntent.EXT_DIRECTIVE_DATA);
                jSONObject.put(WakeupIntent.EXT_DATA, GsonUtils.toJson(voiceKitMessage));
                jSONObject.put(WakeupIntent.EXT_ASR_RESULT_CHANNEL, voiceKitMessage.getChannel());
                Optional.ofNullable(FreeWakeupProxy.this.wakeupCallback).ifPresent(new Consumer() {
                    @Override
                    public final void accept(Object obj) {
                        FreeWakeupProxy.b.d(jSONObject, (WakeupAbilityInterface.Callback) obj);
                    }
                });
            } catch (JSONException unused) {
                KitLog.error(FreeWakeupProxy.TAG, "onNluResult error");
            }
        }

        @Override
        public void onError(ErrorInfo errorInfo) {
        }
    }
}