package com.zxhyis.toolss.ui.page;

import android.net.TrafficStats;
import android.os.SystemClock;
import android.util.Log;

public class NetworkSpeed {

    private  long f39818b;

    /* renamed from: a  reason: collision with root package name */
    public long f28160a = 0L;

    public boolean monitorSwitch = false;

    /* renamed from: b  reason: collision with root package name */
    public volatile boolean f28161b = false;

    /* renamed from: f  reason: collision with root package name */
    public volatile long f28155f = 0;

    public volatile long preElapsedRealtime;

    public volatile long totalRxBytes;
    public volatile long totalTxBytes;

    public volatile long totalRxSpeed;//下载
    public volatile long totalTxSpeed;//上传

    // 开启网速监控
    public final void startMoritor() {
        monitorSwitch = true;
        if(preElapsedRealtime == 0){
            preElapsedRealtime = SystemClock.elapsedRealtime();
            this.totalRxBytes = TrafficStats.getTotalRxBytes();
            this.totalTxBytes = TrafficStats.getTotalTxBytes();
        }

        while (monitorSwitch) {
            try {
                long totalRxBytes = TrafficStats.getTotalRxBytes();
                long totalTxBytes = TrafficStats.getTotalTxBytes();
                // SystemClock.elapsedRealtime() 的计时起点是系统启动时间，即在开机后第一次调用该方法时返回0. 此处使用 SystemClock.elapsedRealtime() 来计算时间间隔
                long elapsedRealtime = SystemClock.elapsedRealtime();
                long j10 = (elapsedRealtime - this.preElapsedRealtime) / 1000;

                long totalTxSpeed = (long) ((totalTxBytes - this.totalTxBytes) / j10);
                long totalRxSpeed = (long) ((totalRxBytes - this.totalRxBytes) / j10);

                this.totalRxBytes = totalRxBytes;
                this.totalTxBytes = totalTxBytes;

                this.totalTxSpeed = totalTxSpeed;
                this.totalRxSpeed = totalRxSpeed;

                this.preElapsedRealtime = elapsedRealtime;

                Log.i("networkSpeedMonitor", "Tx: " + totalTxSpeed + ", Rx: " + totalRxSpeed);

            } catch (Exception e10) {
            }
            waitAMoment(4000L);
        }

    }
    // 停止网速监控
    public final void stopMonitor() {
        this.monitorSwitch = false;
    }

    public static void waitAMoment(long j10) {
        try {
            Thread.sleep(j10);
        } catch (InterruptedException e10) {
        }
    }
}
