Merge "log connectivity state change"

This commit is contained in:
Chenjie Yu
2018-11-14 15:02:50 +00:00
committed by Android (Google) Code Review
2 changed files with 41 additions and 0 deletions

View File

@@ -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.
@@ -2129,6 +2130,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 //

View File

@@ -41,6 +41,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;
@@ -271,6 +274,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());
@@ -1875,4 +1884,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);
}
}
}