blob: 17a36ad4e6d2ad34fc8b0a415a71788450ddd9ed [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;
23
24import com.android.internal.util.HexDump;
25import com.android.server.connectivity.nano.DataStallEventProto;
26
27/**
28 * Collection of utilities for data stall metrics.
29 *
30 * To see if the logs are properly sent to statsd, execute following command.
31 *
32 * $ adb shell cmd stats print-logs
33 * $ adb logcat | grep statsd OR $ adb logcat -b stats
34 *
35 * @hide
36 */
37public class DataStallStatsUtils {
38 private static final String TAG = DataStallStatsUtils.class.getSimpleName();
39 private static final boolean DBG = false;
40
41 private static int probeResultToEnum(@Nullable final CaptivePortalProbeResult result) {
42 if (result == null) return DataStallEventProto.INVALID;
43
44 // TODO: Add partial connectivity support.
45 if (result.isSuccessful()) {
46 return DataStallEventProto.VALID;
47 } else if (result.isPortal()) {
48 return DataStallEventProto.PORTAL;
49 } else {
50 return DataStallEventProto.INVALID;
51 }
52 }
53
54 /**
55 * Write the metric to {@link StatsLog}.
56 */
57 public static void write(@NonNull final DataStallDetectionStats stats,
58 @NonNull final CaptivePortalProbeResult result) {
59 int validationResult = probeResultToEnum(result);
60 if (DBG) {
61 Log.d(TAG, "write: " + stats + " with result: " + validationResult
62 + ", dns: " + HexDump.toHexString(stats.mDns));
63 }
64 // TODO(b/124613085): Send to Statsd once the public StatsLog API is ready.
65 }
66}