Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net.metrics; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.net.captiveportal.CaptivePortalProbeResult; |
| 22 | import android.util.Log; |
Chiachang Wang | c1908f1 | 2019-03-08 14:41:11 +0800 | [diff] [blame] | 23 | import android.util.StatsLog; |
Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 24 | |
| 25 | import com.android.internal.util.HexDump; |
| 26 | import 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 | */ |
| 38 | public class DataStallStatsUtils { |
| 39 | private static final String TAG = DataStallStatsUtils.class.getSimpleName(); |
Chiachang Wang | c1908f1 | 2019-03-08 14:41:11 +0800 | [diff] [blame] | 40 | private static final int DATA_STALL_EVENT_ID = 121; |
Chiachang Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 41 | 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 Wang | c1908f1 | 2019-03-08 14:41:11 +0800 | [diff] [blame] | 66 | // 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 Wang | f09e3e3 | 2019-02-22 11:13:07 +0800 | [diff] [blame] | 74 | } |
| 75 | } |