blob: d1ca109036b489b17ded1fc458b0d854198b73f9 [file] [log] [blame]
Chiachang Wangf09e3e32019-02-22 11:13:07 +08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net.metrics;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.net.captiveportal.CaptivePortalProbeResult;
22import android.util.Log;
Chiachang Wangc1908f12019-03-08 14:41:11 +080023import android.util.StatsLog;
Chiachang Wangf09e3e32019-02-22 11:13:07 +080024
25import com.android.internal.util.HexDump;
26import com.android.server.connectivity.nano.DataStallEventProto;
27
28/**
29 * Collection of utilities for data stall metrics.
30 *
31 * To see if the logs are properly sent to statsd, execute following command.
32 *
33 * $ adb shell cmd stats print-logs
34 * $ adb logcat | grep statsd OR $ adb logcat -b stats
35 *
36 * @hide
37 */
38public class DataStallStatsUtils {
39 private static final String TAG = DataStallStatsUtils.class.getSimpleName();
Chiachang Wangc1908f12019-03-08 14:41:11 +080040 private static final int DATA_STALL_EVENT_ID = 121;
Chiachang Wangf09e3e32019-02-22 11:13:07 +080041 private static final boolean DBG = false;
42
43 private static int probeResultToEnum(@Nullable final CaptivePortalProbeResult result) {
44 if (result == null) return DataStallEventProto.INVALID;
45
46 // TODO: Add partial connectivity support.
47 if (result.isSuccessful()) {
48 return DataStallEventProto.VALID;
49 } else if (result.isPortal()) {
50 return DataStallEventProto.PORTAL;
51 } else {
52 return DataStallEventProto.INVALID;
53 }
54 }
55
56 /**
57 * Write the metric to {@link StatsLog}.
58 */
59 public static void write(@NonNull final DataStallDetectionStats stats,
60 @NonNull final CaptivePortalProbeResult result) {
61 int validationResult = probeResultToEnum(result);
62 if (DBG) {
63 Log.d(TAG, "write: " + stats + " with result: " + validationResult
64 + ", dns: " + HexDump.toHexString(stats.mDns));
65 }
Chiachang Wangc1908f12019-03-08 14:41:11 +080066 // TODO(b/124613085): Update API once the public StatsLog API is ready.
67 StatsLog.write(DATA_STALL_EVENT_ID,
68 stats.mEvaluationType,
69 validationResult,
70 stats.mNetworkType,
71 stats.mWifiInfo,
72 stats.mCellularInfo,
73 stats.mDns);
Chiachang Wangf09e3e32019-02-22 11:13:07 +080074 }
75}