Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ae9dfacf authored by Chenjie Yu's avatar Chenjie Yu
Browse files

log connectivity state change

log when networks become available and lost, for all transport types
In statsd, we can use net id to uniquely identify a network. We should
be able to count connectivity state changes similar to what BatteryStats
does.

Bug: 115561340
Test: cts test
Change-Id: Ieac4d243e27d5d368a77e5607dec51964912240d
parent ea0fb2db
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -147,6 +147,7 @@ message Atom {
        PhoneStateChanged phone_state_changed = 95;
        UserRestrictionChanged user_restriction_changed = 96;
        SettingsUIChanged settings_ui_changed = 97;
        ConnectivityStateChanged connectivity_state_changed = 98;
    }

    // Pulled events will start at field 10000.
@@ -2128,6 +2129,22 @@ message Notification {
    optional int64 visible_millis = 16;
}

/*
 * Logs when a connection becomes available and lost.
 * Logged in StatsCompanionService.java
 */
message ConnectivityStateChanged {
    // Id of the network.
    optional int32 net_id = 1;

    enum State {
        UNKNOWN = 0;
        CONNECTED = 1;
        DISCONNECTED = 2;
    }
    // Connected state of a network.
    optional State state = 2;
}

//////////////////////////////////////////////////////////////////////
// Pulled atoms below this line //
+24 −0
Original line number Diff line number Diff line
@@ -35,6 +35,9 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.hardware.fingerprint.FingerprintManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkRequest;
import android.net.NetworkStats;
import android.net.wifi.IWifiManager;
import android.net.wifi.WifiActivityEnergyInfo;
@@ -264,6 +267,12 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            Slog.e(TAG, "cannot find thermalservice, no throttling push notifications");
        }

        // Default NetworkRequest should cover all transport types.
        final NetworkRequest request = new NetworkRequest.Builder().build();
        final ConnectivityManager connectivityManager =
                (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        connectivityManager.registerNetworkCallback(request, new ConnectivityStatsCallback());

        HandlerThread handlerThread = new HandlerThread(TAG);
        handlerThread.start();
        mHandler = new CompanionHandler(handlerThread.getLooper());
@@ -1862,4 +1871,19 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
                    temp.getValue());
        }
    }

    private static final class ConnectivityStatsCallback extends
            ConnectivityManager.NetworkCallback {
        @Override
        public void onAvailable(Network network) {
            StatsLog.write(StatsLog.CONNECTIVITY_STATE_CHANGED, network.netId,
                    StatsLog.CONNECTIVITY_STATE_CHANGED__STATE__CONNECTED);
        }

        @Override
        public void onLost(Network network) {
            StatsLog.write(StatsLog.CONNECTIVITY_STATE_CHANGED, network.netId,
                    StatsLog.CONNECTIVITY_STATE_CHANGED__STATE__DISCONNECTED);
        }
    }
}