导航菜单

页面标题

页面副标题

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

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

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


package com.huawei.hiassistant.voice.assistantcommander.networkcheck;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.huawei.hiassistant.platform.base.bean.recognize.HeaderPayload;
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.commander.networkcheck.NetworkCheckInterface;
import com.huawei.hiassistant.platform.base.module.commander.networkcheck.NetworkCheckResultBean;
import com.huawei.hiassistant.platform.base.northinterface.recognize.RecognizerIntent;
import com.huawei.hiassistant.platform.base.northinterface.wakeup.WakeupIntent;
import com.huawei.hiassistant.platform.base.report.OperationReportUtils;
import com.huawei.hiassistant.platform.base.report.fault.FaultEventReportConstants;
import com.huawei.hiassistant.platform.base.util.BaseUtils;
import com.huawei.hiassistant.platform.base.util.BusinessFlowId;
import com.huawei.hiassistant.platform.base.util.IAssistantConfig;
import com.huawei.hiassistant.platform.base.util.IassistantThreadPool;
import com.huawei.hiassistant.platform.base.util.KitLog;
import com.huawei.hiassistant.platform.base.util.NetworkUtil;
import com.huawei.hiassistant.platform.base.util.SecureIntentUtil;
import com.huawei.hiassistant.platform.base.util.UuidUtils;
import com.huawei.hiassistant.voice.abilityconnector.recognizer.cloud.common.HiVoiceConstants;
import com.huawei.hiassistant.voice.abilityconnector.recognizer.cloud.http.HttpConfig;
import com.huawei.hiassistant.voice.abilityconnector.recognizer.cloud.http.HttpReqSender;
import com.huawei.hiassistant.voice.abilityconnector.recognizer.cloud.http.HttpResponse;
import com.huawei.hiassistant.voice.abilityconnector.recognizer.cloud.http.HttpRspCallback;
import com.huawei.hiassistant.voice.abilityconnector.recognizer.cloud.http.OkClientMgr;
import com.huawei.hiassistant.voice.assistantcommander.networkcheck.NetworkChecker;
import com.huawei.hms.network.httpclient.Response;
import com.huawei.hms.network.httpclient.ResponseBody;
import com.huawei.hms.network.httpclient.Submit;
import defpackage.aob;
import defpackage.ghb;
import defpackage.joa;
import defpackage.osb;
import defpackage.yna;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.json.JSONObject;

public class NetworkChecker implements NetworkCheckInterface {
    private static final int CALCULATE_FREQUENCY_MSG = 3;
    public static final int DEFAULT_HEARTBEAT_TIMEOUT = 1000;
    private static final int HEARTBEAT_TIMEOUT_MSG = 1;
    private static final int MAX_RESULT_COUNT = 50;
    private static final int RECOVERING_LIMIT = 4;
    private static final String REPORT_90006_CONDITION_EVERY_20 = "1";
    private static final String REPORT_90006_CONDITION_SESSION_END = "0";
    private static final int RESPONSE_OK = 200;
    private static final int SEND_HEARTBEAT_MSG = 0;
    private static final String TAG = "NetworkChecker";
    private static final long UNIT = 1000;
    private static final int UPDATE_NETWORK_STATUS_MSG = 2;
    private static final int VALID_RESPONSE_SIZE_LONG = 1024;
    private static final int VALID_RESPONSE_SIZE_SHORT = 1;
    private static final int WEAKENING_LIMIT = 2;
    private int cloudDetectRespTimeout;
    private String grsUrl;
    private Handler mHandler;
    private osb networkContinuousDetector;
    private aob checkerConfig = new aob();
    private HttpReqSender httpReqSender = new HttpReqSender();
    private AtomicBoolean isInit = new AtomicBoolean(false);
    private AtomicBoolean isStart = new AtomicBoolean(false);
    private AtomicBoolean isDetecting = new AtomicBoolean(false);
    private volatile NetworkCheckInterface.HiVoiceHeartbeatResult currentNetWorkStatus = NetworkCheckInterface.HiVoiceHeartbeatResult.NO_ENOUGH_VALID_DATA;
    private volatile long detectStartTime = 0;
    private short globalHeartbeatCount = 0;
    private int relativesHeartbeatCount = 0;
    private List<NetworkCheckInterface.HiVoiceHeartbeatResult> networkCheckerList = Collections.synchronizedList(new ArrayList());
    private List<Integer> networkCheckCostList = Collections.synchronizedList(new ArrayList());
    private int totalCount = 0;
    private int totalFailedCount = 0;
    private int lastMaxContinueFailCount = 0;
    private int maxContinuousFailedCount = 0;
    private NetworkCheckInterface.NetworkCheckCallbackInterface heartbeatCallback = new NetworkCheckInterface.NetworkCheckCallbackInterface() {
        @Override
        public void onUpdateHeartbeatCost(NetworkCheckResultBean networkCheckResultBean) {
            KitLog.error(NetworkChecker.TAG, "unexpected method onUpdateHeartbeatCost call");
        }

        @Override
        public void onUpdateNetworkStatus(NetworkCheckResultBean networkCheckResultBean) {
            KitLog.error(NetworkChecker.TAG, "unexpected method onUpdateNetworkStatus call");
        }
    };
    private HttpRspCallback httpCallback = new NetworkCheckHttpRspCallback();

    public class NetworkCheckHandler extends Handler {
        public NetworkCheckHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message message) {
            if (message == null) {
                return;
            }
            if (!NetworkChecker.this.isInit.get()) {
                KitLog.debug(NetworkChecker.TAG, "Heartbeat isInit: false.", new Object[0]);
                return;
            }
            int i = message.what;
            if (i == 0) {
                KitLog.debug(NetworkChecker.TAG, "detect.", new Object[0]);
                NetworkChecker.this.detectStartTime = System.currentTimeMillis();
                NetworkChecker.this.mHandler.sendMessageDelayed(NetworkChecker.this.mHandler.obtainMessage(1), NetworkChecker.this.cloudDetectRespTimeout);
                NetworkChecker.this.doHeartbeatAsync();
                return;
            }
            if (i == 1) {
                NetworkChecker.this.isDetecting.set(false);
                KitLog.error(NetworkChecker.TAG, "heartbeat timeout.");
                NetworkChecker.this.httpReqSender.cancelRequest(HttpConfig.HTTP_GET_TAG);
                NetworkChecker networkChecker = NetworkChecker.this;
                networkChecker.processCheckResult(new NetworkCheckResultBean(networkChecker.detectStartTime, NetworkCheckInterface.HiVoiceHeartbeatResult.UNAVAILABLE, NetworkChecker.this.cloudDetectRespTimeout, NetworkCheckInterface.HiVoiceHeartbeatType.DETECT_RESULT));
                NetworkChecker.this.detect();
                return;
            }
            if (i != 2) {
                if (i != 3) {
                    return;
                }
                NetworkChecker.this.mHandler.sendEmptyMessageDelayed(0, ghb.a(NetworkChecker.this.relativesHeartbeatCount) * 1000);
                return;
            }
            Object obj = message.obj;
            if (obj instanceof NetworkCheckResultBean) {
                NetworkChecker.this.processCheckResult((NetworkCheckResultBean) obj);
            }
        }
    }

    public class NetworkCheckHttpRspCallback extends HttpRspCallback {
        private NetworkCheckHttpRspCallback() {
        }

        @Override
        public void onError(Submit<ResponseBody> submit, Exception exc, int i, String str, Bundle bundle) {
            NetworkChecker.this.isDetecting.set(false);
            NetworkChecker.this.mHandler.removeMessages(1);
            long currentTimeMillis = System.currentTimeMillis() - NetworkChecker.this.detectStartTime;
            KitLog.error(NetworkChecker.TAG, "onError, exception: " + exc.getClass());
            NetworkChecker.this.mHandler.obtainMessage(2, new NetworkCheckResultBean(NetworkChecker.this.detectStartTime, NetworkCheckInterface.HiVoiceHeartbeatResult.UNAVAILABLE, currentTimeMillis, NetworkCheckInterface.HiVoiceHeartbeatType.DETECT_RESULT)).sendToTarget();
            NetworkChecker.this.detect();
        }

        @Override
        public void onParseData(Map map, JSONObject jSONObject, String str, Bundle bundle) {
            NetworkChecker.this.isDetecting.set(false);
            NetworkChecker.this.mHandler.removeMessages(1);
            long currentTimeMillis = System.currentTimeMillis() - NetworkChecker.this.detectStartTime;
            NetworkChecker.this.parseBoundary(jSONObject);
            NetworkCheckInterface.HiVoiceHeartbeatResult hiVoiceHeartbeatResult = NetworkCheckInterface.HiVoiceHeartbeatResult.AVAILABLE;
            if (NetworkChecker.this.checkerConfig.g() && currentTimeMillis < NetworkChecker.this.checkerConfig.l()) {
                hiVoiceHeartbeatResult = NetworkCheckInterface.HiVoiceHeartbeatResult.FAST_HANDOVER;
            }
            NetworkChecker.this.mHandler.obtainMessage(2, new NetworkCheckResultBean(NetworkChecker.this.detectStartTime, hiVoiceHeartbeatResult, currentTimeMillis, NetworkCheckInterface.HiVoiceHeartbeatType.DETECT_RESULT)).sendToTarget();
            NetworkChecker.this.detect();
        }

        @Override
        public void onParseFailed(HttpResponse httpResponse, String str, Bundle bundle) {
            NetworkChecker.this.isDetecting.set(false);
            NetworkChecker.this.mHandler.removeMessages(1);
            long currentTimeMillis = System.currentTimeMillis() - NetworkChecker.this.detectStartTime;
            if (httpResponse == null) {
                NetworkChecker.this.mHandler.obtainMessage(2, new NetworkCheckResultBean(NetworkChecker.this.detectStartTime, NetworkCheckInterface.HiVoiceHeartbeatResult.UNAVAILABLE, currentTimeMillis, NetworkCheckInterface.HiVoiceHeartbeatType.DETECT_RESULT)).sendToTarget();
            } else if (httpResponse.get().getCode() == 503) {
                KitLog.error(NetworkChecker.TAG, "http rate limiting, need stop detect.");
                NetworkChecker.this.mHandler.obtainMessage(2, new NetworkCheckResultBean(NetworkChecker.this.detectStartTime, NetworkCheckInterface.HiVoiceHeartbeatResult.NO_ENOUGH_VALID_DATA, currentTimeMillis, NetworkCheckInterface.HiVoiceHeartbeatType.DETECT_RESULT)).sendToTarget();
                NetworkChecker.this.destroy();
                return;
            } else if (httpResponse.get().getCode() == 200) {
                KitLog.warn(NetworkChecker.TAG, "no response body.");
                NetworkChecker.this.heartbeatCallback.onUpdateNetworkStatus(new NetworkCheckResultBean(NetworkChecker.this.detectStartTime, NetworkCheckInterface.HiVoiceHeartbeatResult.AVAILABLE, currentTimeMillis, NetworkCheckInterface.HiVoiceHeartbeatType.DETECT_RESULT));
            } else {
                NetworkChecker.this.mHandler.obtainMessage(2, new NetworkCheckResultBean(NetworkChecker.this.detectStartTime, NetworkCheckInterface.HiVoiceHeartbeatResult.UNAVAILABLE, currentTimeMillis, NetworkCheckInterface.HiVoiceHeartbeatType.DETECT_RESULT)).sendToTarget();
            }
            NetworkChecker.this.detect();
        }
    }

    private boolean checkIfNetworkFailed() {
        if (this.networkCheckerList.size() < 2) {
            return false;
        }
        long count = this.networkCheckerList.subList(0, 2).stream().filter(new Predicate() {
            @Override
            public final boolean test(Object obj) {
                boolean lambda$checkIfNetworkFailed$2;
                lambda$checkIfNetworkFailed$2 = NetworkChecker.lambda$checkIfNetworkFailed$2((NetworkCheckInterface.HiVoiceHeartbeatResult) obj);
                return lambda$checkIfNetworkFailed$2;
            }
        }).count();
        KitLog.info(TAG, "checkIfNetworkFailed: " + count);
        return count == 2;
    }

    private boolean checkIfNetworkSuccess() {
        int min = Math.min(this.networkCheckerList.size(), this.checkerConfig.j());
        long count = this.networkCheckerList.subList(0, min).stream().filter(new Predicate() {
            @Override
            public final boolean test(Object obj) {
                boolean lambda$checkIfNetworkSuccess$1;
                lambda$checkIfNetworkSuccess$1 = NetworkChecker.lambda$checkIfNetworkSuccess$1((NetworkCheckInterface.HiVoiceHeartbeatResult) obj);
                return lambda$checkIfNetworkSuccess$1;
            }
        }).count();
        KitLog.info(TAG, "checkIfNetworkSuccess: " + count);
        return count == ((long) min);
    }

    private void checkSignalStrength() {
        NetworkCheckInterface.HiVoiceHeartbeatResult hiVoiceHeartbeatResult;
        if (NetworkUtil.getNetworkType(IAssistantConfig.getInstance().getAppContext()) == 0) {
            int wifiRssi = NetworkUtil.getWifiRssi();
            KitLog.debug(TAG, "wifi rssi:{}", Integer.valueOf(wifiRssi));
            hiVoiceHeartbeatResult = (!this.checkerConfig.g() || wifiRssi <= this.checkerConfig.n()) ? wifiRssi > this.checkerConfig.p() ? NetworkCheckInterface.HiVoiceHeartbeatResult.AVAILABLE : NetworkCheckInterface.HiVoiceHeartbeatResult.UNAVAILABLE : NetworkCheckInterface.HiVoiceHeartbeatResult.FAST_HANDOVER;
        } else {
            int rsrp = ModuleInstanceFactory.Commander.networkCheckProvider().getRsrp();
            KitLog.debug(TAG, "cell rsrp:{}", Integer.valueOf(rsrp));
            hiVoiceHeartbeatResult = (!this.checkerConfig.g() || rsrp <= this.checkerConfig.r()) ? rsrp > this.checkerConfig.s() ? NetworkCheckInterface.HiVoiceHeartbeatResult.AVAILABLE : NetworkCheckInterface.HiVoiceHeartbeatResult.UNAVAILABLE : NetworkCheckInterface.HiVoiceHeartbeatResult.FAST_HANDOVER;
        }
        NetworkCheckInterface.HiVoiceHeartbeatResult hiVoiceHeartbeatResult2 = hiVoiceHeartbeatResult;
        this.isDetecting.set(false);
        this.mHandler.removeMessages(1);
        this.mHandler.obtainMessage(2, new NetworkCheckResultBean(this.detectStartTime, hiVoiceHeartbeatResult2, System.currentTimeMillis() - this.detectStartTime, NetworkCheckInterface.HiVoiceHeartbeatType.SIGNAL_STRENGTH_RESULT)).sendToTarget();
        detect();
    }

    private Bundle createNetworkCheckBundle() {
        Bundle bundle = new Bundle();
        bundle.putShort("id", this.globalHeartbeatCount);
        bundle.putString("dataType", this.globalHeartbeatCount % 2 == 0 ? "Short" : "Long");
        bundle.putString("recognizeUrl", this.grsUrl);
        bundle.putString("sessionId", BusinessFlowId.getInstance().getSessionId());
        bundle.putString("interactionId", String.valueOf((int) BusinessFlowId.getInstance().getInteractionId()));
        bundle.putString(FaultEventReportConstants.DIALOG_ID, String.valueOf(BusinessFlowId.getInstance().getDialogId()));
        bundle.putString("messageId", UuidUtils.getUuid());
        bundle.putString("requestEvent", HiVoiceConstants.EventName.EVENT_NETWORK_CHECK);
        return bundle;
    }

    public void detect() {
        if (this.isInit.get() && this.isStart.get()) {
            if (this.isDetecting.get()) {
                KitLog.info(TAG, "isDetecting: true, return.");
                return;
            }
            this.mHandler.removeMessages(1);
            this.mHandler.removeMessages(0);
            Message obtainMessage = this.mHandler.obtainMessage(0);
            if (this.globalHeartbeatCount == 0) {
                this.mHandler.sendMessage(obtainMessage);
            } else {
                this.mHandler.sendEmptyMessage(3);
            }
            this.globalHeartbeatCount = (short) (this.globalHeartbeatCount + 1);
        }
    }

    public void doHeartbeatAsync() {
        this.isDetecting.set(true);
        if (this.checkerConfig.d() && isIdleStatus()) {
            checkSignalStrength();
        } else {
            this.httpReqSender.doGetCmdAsync(this.httpCallback, createNetworkCheckBundle());
        }
    }

    private static String getUrl(String str) {
        return str + HttpConfig.HEART_URI;
    }

    private void initConfig(Intent intent) {
        this.checkerConfig.c(SecureIntentUtil.getSecureIntentBoolean(intent, RecognizerIntent.EXT_CHECK_SIGNAL_STRENGTH, false));
        this.checkerConfig.f(SecureIntentUtil.getSecureIntentBoolean(intent, RecognizerIntent.EXT_ALLOW_FAST_HANDOVER, false));
        aob aobVar = this.checkerConfig;
        aobVar.a(SecureIntentUtil.getSecureIntentInt(intent, RecognizerIntent.EXT_SWITCH_TO_IDLE_DELAY, aobVar.h()));
        aob aobVar2 = this.checkerConfig;
        aobVar2.e(SecureIntentUtil.getSecureIntentInt(intent, RecognizerIntent.EXT_NETWORK_RECOVERING_LIMIT, aobVar2.j()));
        aob aobVar3 = this.checkerConfig;
        aobVar3.b(SecureIntentUtil.getSecureIntentLong(intent, RecognizerIntent.EXT_EXCELLENT_NETWORK_LATENCY, aobVar3.l()));
        aob aobVar4 = this.checkerConfig;
        aobVar4.i(SecureIntentUtil.getSecureIntentInt(intent, RecognizerIntent.EXT_EXCELLENT_WIFI_RSSI, aobVar4.n()));
        aob aobVar5 = this.checkerConfig;
        aobVar5.k(SecureIntentUtil.getSecureIntentInt(intent, RecognizerIntent.EXT_DEMARCATION_WIFI_RSSI, aobVar5.p()));
        aob aobVar6 = this.checkerConfig;
        aobVar6.m(SecureIntentUtil.getSecureIntentInt(intent, RecognizerIntent.EXT_EXCELLENT_CELL_RSRP, aobVar6.r()));
        aob aobVar7 = this.checkerConfig;
        aobVar7.o(SecureIntentUtil.getSecureIntentInt(intent, RecognizerIntent.EXT_DEMARCATION_CELL_RSRP, aobVar7.s()));
        this.checkerConfig.q(SecureIntentUtil.getSecureIntentInt(intent, RecognizerIntent.EXT_CONTINUOUS_DETECT_INTERVAL, 0));
    }

    private boolean isIdleStatus() {
        boolean idleStatus = ModuleInstanceFactory.Commander.networkCheckProvider().getIdleStatus();
        long lastChangeToIdleTime = ModuleInstanceFactory.Commander.networkCheckProvider().getLastChangeToIdleTime();
        return idleStatus && lastChangeToIdleTime > 0 && System.currentTimeMillis() - lastChangeToIdleTime > ((long) this.checkerConfig.h()) * 1000;
    }

    public static boolean isSynNetworkAvaible(long j, final String str) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        boolean z = false;
        final AtomicBoolean atomicBoolean = new AtomicBoolean(false);
        IassistantThreadPool.getInstance().execute(new Runnable() {
            @Override
            public final void run() {
                NetworkChecker.lambda$isSynNetworkAvaible$8(str, atomicBoolean, countDownLatch);
            }
        });
        try {
            z = countDownLatch.await(j, TimeUnit.MILLISECONDS);
            if (countDownLatch.getCount() != 0) {
                KitLog.warn(TAG, "checkNetworkState heartbeat timeout");
            }
        } catch (InterruptedException unused) {
            KitLog.error(TAG, "checkNetworkState countDownLatch InterruptedException");
        }
        if (!z || !atomicBoolean.get()) {
            OkClientMgr.getInstance().cancelWithTag(getUrl(str));
        }
        return atomicBoolean.get();
    }

    public static boolean lambda$checkIfNetworkFailed$2(NetworkCheckInterface.HiVoiceHeartbeatResult hiVoiceHeartbeatResult) {
        return hiVoiceHeartbeatResult == NetworkCheckInterface.HiVoiceHeartbeatResult.UNAVAILABLE;
    }

    public static boolean lambda$checkIfNetworkSuccess$1(NetworkCheckInterface.HiVoiceHeartbeatResult hiVoiceHeartbeatResult) {
        return hiVoiceHeartbeatResult == NetworkCheckInterface.HiVoiceHeartbeatResult.AVAILABLE;
    }

    public static void lambda$isSynNetworkAvaible$8(String str, AtomicBoolean atomicBoolean, CountDownLatch countDownLatch) {
        long currentTimeMillis = System.currentTimeMillis();
        try {
            getUrl(str);
            Optional<Response> doGet = OkClientMgr.getInstance().doGet(getUrl(str));
            KitLog.info(TAG, "cost time is " + (System.currentTimeMillis() - currentTimeMillis));
            if (doGet.isPresent() && doGet.get().isSuccessful()) {
                atomicBoolean.set(true);
            }
            countDownLatch.countDown();
        } catch (IOException e) {
            KitLog.error(TAG, "checkNetworkState IOException");
            BaseUtils.printExceptionInfo(e);
        } catch (IllegalArgumentException unused) {
            KitLog.error(TAG, "checkNetworkState IllegalArgumentException");
        }
    }

    public static boolean lambda$parseBoundary$3(List list) {
        return !list.isEmpty();
    }

    public static HeaderPayload lambda$parseBoundary$4(List list) {
        return (HeaderPayload) list.get(0);
    }

    public static boolean lambda$parseBoundary$5(JsonObject jsonObject) {
        return jsonObject.has(WakeupIntent.EXT_DATA);
    }

    public static JsonElement lambda$parseBoundary$6(JsonObject jsonObject) {
        return jsonObject.get(WakeupIntent.EXT_DATA);
    }

    public static void lambda$parseBoundary$7(JsonElement jsonElement) {
        int length = jsonElement.getAsString().length();
        if (length == 1024 || length == 1) {
            return;
        }
        KitLog.warn(TAG, "response length is not equal 1024 or 1: " + jsonElement.getAsString());
    }

    public static String lambda$reportNetCheckResult$0(NetworkCheckInterface.HiVoiceHeartbeatResult hiVoiceHeartbeatResult) {
        return String.valueOf(hiVoiceHeartbeatResult.getValue());
    }

    public void parseBoundary(JSONObject jSONObject) {
        if (jSONObject != null) {
            Optional.ofNullable((VoiceKitMessage) GsonUtils.toBean(jSONObject.toString(), VoiceKitMessage.class)).map(new Function() {
                @Override
                public final Object apply(Object obj) {
                    return ((VoiceKitMessage) obj).getContexts();
                }
            }).filter(new Predicate() {
                @Override
                public final boolean test(Object obj) {
                    boolean lambda$parseBoundary$3;
                    lambda$parseBoundary$3 = NetworkChecker.lambda$parseBoundary$3((List) obj);
                    return lambda$parseBoundary$3;
                }
            }).map(new Function() {
                @Override
                public final Object apply(Object obj) {
                    HeaderPayload lambda$parseBoundary$4;
                    lambda$parseBoundary$4 = NetworkChecker.lambda$parseBoundary$4((List) obj);
                    return lambda$parseBoundary$4;
                }
            }).map(new yna()).map(new joa()).filter(new Predicate() {
                @Override
                public final boolean test(Object obj) {
                    boolean lambda$parseBoundary$5;
                    lambda$parseBoundary$5 = NetworkChecker.lambda$parseBoundary$5((JsonObject) obj);
                    return lambda$parseBoundary$5;
                }
            }).map(new Function() {
                @Override
                public final Object apply(Object obj) {
                    JsonElement lambda$parseBoundary$6;
                    lambda$parseBoundary$6 = NetworkChecker.lambda$parseBoundary$6((JsonObject) obj);
                    return lambda$parseBoundary$6;
                }
            }).ifPresent(new Consumer() {
                @Override
                public final void accept(Object obj) {
                    NetworkChecker.lambda$parseBoundary$7((JsonElement) obj);
                }
            });
        } else {
            KitLog.warn(TAG, "response is null.");
        }
    }

    public void processCheckResult(NetworkCheckResultBean networkCheckResultBean) {
        updateNetworkStatus(networkCheckResultBean.getStartTime(), networkCheckResultBean.getIsAvailable(), networkCheckResultBean.getHeartbeatCost(), networkCheckResultBean.getType());
    }

    private void reportNetCheckResult(String str) {
        OperationReportUtils.getInstance().getNetworkChecker90006().setAllResult(String.join("", (List) this.networkCheckerList.stream().map(new Function() {
            @Override
            public final Object apply(Object obj) {
                String lambda$reportNetCheckResult$0;
                lambda$reportNetCheckResult$0 = NetworkChecker.lambda$reportNetCheckResult$0((NetworkCheckInterface.HiVoiceHeartbeatResult) obj);
                return lambda$reportNetCheckResult$0;
            }
        }).collect(Collectors.toList())));
        OperationReportUtils.getInstance().getNetworkChecker90006().setFailedCount(this.totalFailedCount);
        OperationReportUtils.getInstance().getNetworkChecker90006().setMaxContinuousFailedCount(Math.max(this.maxContinuousFailedCount, this.lastMaxContinueFailCount));
        OperationReportUtils.getInstance().getNetworkChecker90006().setStartCondition(str);
        OperationReportUtils.getInstance().getNetworkChecker90006().setNetworkCheckCostList(this.networkCheckCostList);
        OperationReportUtils.getInstance().reportNetworkChecker90006();
    }

    private void updateFailedCount(NetworkCheckInterface.HiVoiceHeartbeatResult hiVoiceHeartbeatResult) {
        if (hiVoiceHeartbeatResult != NetworkCheckInterface.HiVoiceHeartbeatResult.UNAVAILABLE) {
            this.lastMaxContinueFailCount = Math.max(this.lastMaxContinueFailCount, this.maxContinuousFailedCount);
            this.maxContinuousFailedCount = 0;
        } else {
            this.totalFailedCount++;
            int i = this.maxContinuousFailedCount + 1;
            this.maxContinuousFailedCount = i;
            this.lastMaxContinueFailCount = Math.max(this.lastMaxContinueFailCount, i);
        }
    }

    private void updateNetworkStatus(long j, NetworkCheckInterface.HiVoiceHeartbeatResult hiVoiceHeartbeatResult, long j2, NetworkCheckInterface.HiVoiceHeartbeatType hiVoiceHeartbeatType) {
        NetworkCheckInterface.HiVoiceHeartbeatResult hiVoiceHeartbeatResult2;
        boolean z;
        if (this.isInit.get() && this.isStart.get()) {
            KitLog.debug(TAG, "updateNetworkStatus", new Object[0]);
            if (hiVoiceHeartbeatResult == NetworkCheckInterface.HiVoiceHeartbeatResult.FAST_HANDOVER) {
                hiVoiceHeartbeatResult2 = NetworkCheckInterface.HiVoiceHeartbeatResult.AVAILABLE;
                z = true;
            } else {
                hiVoiceHeartbeatResult2 = hiVoiceHeartbeatResult;
                z = false;
            }
            updateFailedCount(hiVoiceHeartbeatResult2);
            this.networkCheckerList.add(0, hiVoiceHeartbeatResult2);
            if (this.networkCheckCostList.size() < 20) {
                this.networkCheckCostList.add(Integer.valueOf((int) j2));
            }
            OperationReportUtils.getInstance().getRecognizerSwitchCollection90005().setLastNetCheckTime(j);
            OperationReportUtils.getInstance().getRecognizerSwitchCollection90005().setLastNetCheckRes(String.valueOf(hiVoiceHeartbeatResult2.getValue()));
            OperationReportUtils.getInstance().getRecognizerSwitchCollection90005().addNetCheckResult(hiVoiceHeartbeatResult2);
            OperationReportUtils.getInstance().getSwitchToHiaiCollection90007().setLastNetCheckTime(j);
            OperationReportUtils.getInstance().getSwitchToHiaiCollection90007().setLastNetCheckRes(String.valueOf(hiVoiceHeartbeatResult2.getValue()));
            if (hiVoiceHeartbeatResult2 == NetworkCheckInterface.HiVoiceHeartbeatResult.NO_ENOUGH_VALID_DATA) {
                this.heartbeatCallback.onUpdateNetworkStatus(new NetworkCheckResultBean(j, hiVoiceHeartbeatResult2, j2, hiVoiceHeartbeatType));
                return;
            }
            if (this.networkCheckerList.size() > 50) {
                List<NetworkCheckInterface.HiVoiceHeartbeatResult> list = this.networkCheckerList;
                list.remove(list.size() - 1);
            }
            int i = this.totalCount + 1;
            this.totalCount = i;
            if (i % 50 == 0) {
                reportNetCheckResult("1");
            }
            this.heartbeatCallback.onUpdateHeartbeatCost(new NetworkCheckResultBean(j, hiVoiceHeartbeatResult2, j2, hiVoiceHeartbeatType));
            if (z && hiVoiceHeartbeatResult2 != ModuleInstanceFactory.Commander.networkCheckProvider().getHeartbeatResult()) {
                this.heartbeatCallback.onUpdateNetworkStatus(new NetworkCheckResultBean(j, hiVoiceHeartbeatResult2, j2, hiVoiceHeartbeatType));
                this.relativesHeartbeatCount = 0;
            } else {
                if (!checkIfNetworkSuccess() && !checkIfNetworkFailed()) {
                    this.relativesHeartbeatCount = 0;
                    return;
                }
                if (hiVoiceHeartbeatResult2 != ModuleInstanceFactory.Commander.networkCheckProvider().getHeartbeatResult()) {
                    this.heartbeatCallback.onUpdateNetworkStatus(new NetworkCheckResultBean(j, hiVoiceHeartbeatResult2, j2, hiVoiceHeartbeatType));
                }
                this.relativesHeartbeatCount++;
            }
        }
    }

    @Override
    public void businessTriggerDetect() {
        if (this.isInit.get() && !this.isDetecting.get()) {
            if (!this.isStart.get()) {
                KitLog.info(TAG, "startDetect in businessTriggerDetect");
                startDetect();
                return;
            }
            KitLog.debug(TAG, "businessTriggerDetect.", new Object[0]);
            boolean isNetworkAvailable = NetworkUtil.isNetworkAvailable(IAssistantConfig.getInstance().getAppContext());
            boolean isHiVoiceAvailable = ModuleInstanceFactory.Commander.networkCheckProvider().isHiVoiceAvailable();
            KitLog.info(TAG, "netd isNetworkAvailable: " + isNetworkAvailable + ", isHiVoiceCloudAvailable: " + isHiVoiceAvailable);
            if (!isNetworkAvailable || isHiVoiceAvailable) {
                return;
            }
            this.mHandler.removeMessages(1);
            this.mHandler.removeMessages(0);
            this.mHandler.sendMessage(this.mHandler.obtainMessage(0));
            this.globalHeartbeatCount = (short) (this.globalHeartbeatCount + 1);
        }
    }

    @Override
    public void destroy() {
        if (this.isInit.get()) {
            KitLog.debug(TAG, "destroy", new Object[0]);
            osb osbVar = this.networkContinuousDetector;
            if (osbVar != null) {
                osbVar.i();
            }
            reportNetCheckResult("0");
            this.mHandler.removeMessages(1);
            this.mHandler.removeMessages(0);
            this.globalHeartbeatCount = (short) 0;
            this.relativesHeartbeatCount = 0;
            this.isInit.set(false);
            this.isStart.set(false);
            this.isDetecting.set(false);
            this.detectStartTime = 0L;
            this.currentNetWorkStatus = NetworkCheckInterface.HiVoiceHeartbeatResult.NO_ENOUGH_VALID_DATA;
            this.networkCheckerList.clear();
            this.networkCheckCostList.clear();
            this.totalCount = 0;
            this.totalFailedCount = 0;
            this.lastMaxContinueFailCount = 0;
            this.maxContinuousFailedCount = 0;
        }
    }

    @Override
    public void init(String str, Intent intent, NetworkCheckInterface.NetworkCheckCallbackInterface networkCheckCallbackInterface) {
        if (this.isInit.get()) {
            KitLog.warn(TAG, "Repetitive init, return.");
            return;
        }
        if (TextUtils.isEmpty(str)) {
            KitLog.error(TAG, "url is empty.");
            return;
        }
        initConfig(intent);
        KitLog.debug(TAG, "init", new Object[0]);
        this.grsUrl = str;
        this.mHandler = new NetworkCheckHandler(Looper.myLooper());
        this.cloudDetectRespTimeout = SecureIntentUtil.getSecureIntentInt(intent, RecognizerIntent.EXT_CLOUD_DETECT_RESP_TIMEOUT, 1000);
        this.heartbeatCallback = networkCheckCallbackInterface;
        if (this.checkerConfig.t() > 0) {
            osb osbVar = new osb();
            this.networkContinuousDetector = osbVar;
            osbVar.g(str, this.checkerConfig.t() * 1000, this.cloudDetectRespTimeout);
        }
        this.isInit.set(true);
    }

    @Override
    public void insertValidHeartbeat(NetworkCheckInterface.HiVoiceHeartbeatResult hiVoiceHeartbeatResult) {
        if (this.isInit.get() && this.isStart.get()) {
            KitLog.info(TAG, "insertValidHeartbeat: " + hiVoiceHeartbeatResult);
            this.mHandler.removeMessages(1);
            this.mHandler.removeMessages(0);
            this.mHandler.obtainMessage(2, new NetworkCheckResultBean(System.currentTimeMillis(), hiVoiceHeartbeatResult, -1L, NetworkCheckInterface.HiVoiceHeartbeatType.BUSINESS_INSERT)).sendToTarget();
            detect();
        }
    }

    @Override
    public boolean isSignalStrengthAvailable() {
        if (!this.checkerConfig.d()) {
            return true;
        }
        if (NetworkUtil.getNetworkType(IAssistantConfig.getInstance().getAppContext()) == 0) {
            int wifiRssi = NetworkUtil.getWifiRssi();
            KitLog.debug(TAG, "is Wifi Available rssi:{}", Integer.valueOf(wifiRssi));
            return wifiRssi > this.checkerConfig.p();
        }
        int rsrp = ModuleInstanceFactory.Commander.networkCheckProvider().getRsrp();
        KitLog.debug(TAG, "is Cell Available rsrp:{}", Integer.valueOf(rsrp));
        return rsrp > this.checkerConfig.s();
    }

    @Override
    public void startDetect() {
        if (!NetworkUtil.isNetworkAvailable(IAssistantConfig.getInstance().getAppContext())) {
            KitLog.warn(TAG, "network is disconnected, can't startDetect.");
            return;
        }
        if (!this.isInit.get() || this.isStart.get()) {
            return;
        }
        osb osbVar = this.networkContinuousDetector;
        if (osbVar != null) {
            osbVar.c();
        }
        this.isStart.set(true);
        detect();
    }

    @Override
    public void stopDetect() {
        if (!this.isInit.get()) {
            KitLog.info(TAG, "not init yet, return.");
            return;
        }
        KitLog.info(TAG, "stopDetect");
        this.isStart.set(false);
        this.isDetecting.set(false);
        this.mHandler.removeMessages(1);
        this.mHandler.removeMessages(0);
        this.relativesHeartbeatCount = 0;
        this.globalHeartbeatCount = (short) 0;
        this.detectStartTime = 0L;
        this.currentNetWorkStatus = NetworkCheckInterface.HiVoiceHeartbeatResult.NO_ENOUGH_VALID_DATA;
        this.networkCheckerList.clear();
        this.networkCheckCostList.clear();
        this.totalCount = 0;
        this.totalFailedCount = 0;
        this.lastMaxContinueFailCount = 0;
        this.maxContinuousFailedCount = 0;
    }
}