All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yy.httpproxy.stats.Connectivity Maven / Gradle / Ivy

There is a newer version: 1.1.28
Show newest version
package com.yy.httpproxy.stats;

import com.yy.httpproxy.util.Log;

import java.util.ArrayList;
import java.util.List;

public class Connectivity {
    private static String TAG = "ConnectivityStats";

    static class ConnectionEvent {
        int type = 0;
        long timestamp = System.currentTimeMillis();

        ConnectionEvent(int type) {
            this.type = type;
        }
    }

    public static class ConnectionTimes {
        long timeConnected;
        long timeTotal;
    }

    private List events = new ArrayList<>();

    public Connectivity() {
        onDisconnect();
    }

    public void onConnect() {
        Log.d(TAG, "onConnect");
        events.add(new ConnectionEvent(1));
    }

    public void onDisconnect() {
        Log.d(TAG, "onDisconnect");
        events.add(new ConnectionEvent(0));
    }

    public ConnectionTimes getResult() {
        ConnectionEvent latest = events.get(events.size() - 1);
        latest = new ConnectionEvent(latest.type);
        ConnectionEvent current = latest;
        long timeConnected = 0;
        long timeTotal = 0;

        for (int i = events.size() - 1; i >= 0; i--) {
            ConnectionEvent event = events.get(i);
            long span = latest.timestamp - event.timestamp;
            if (event.type == 1) {
                timeConnected += span;
            }
            timeTotal += span;
            latest = event;
        }
        events.clear();
        events.add(current);

        ConnectionTimes result = new ConnectionTimes();
        result.timeConnected = timeConnected / 1000;
        result.timeTotal = timeTotal / 1000;
        Log.d(TAG, "get connectivity result connected " + result.timeConnected + " total :" + result.timeTotal );
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy