• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 com.android.networkstack.metrics;
18 
19 import android.stats.connectivity.NetworkQuirkEvent;
20 
21 import androidx.annotation.VisibleForTesting;
22 
23 /**
24  * Class to record the network Quirk event into statsd.
25  * @hide
26  */
27 public class NetworkQuirkMetrics {
28     private final Dependencies mDependencies;
29     private final NetworkStackQuirkReported.Builder mStatsBuilder =
30             NetworkStackQuirkReported.newBuilder();
31     /**
32      * Dependencies of {@link NetworkQuirkMetrics}, useful for testing.
33      */
34     @VisibleForTesting
35     public static class Dependencies {
36         /**
37          * @see NetworkStackStatsLog#write.
38          */
writeStats(int event)39         public void writeStats(int event) {
40             NetworkStackStatsLog.write(NetworkStackStatsLog.NETWORK_STACK_QUIRK_REPORTED,
41                     0, event);
42         }
43     }
44 
45     /**
46      * Get a NetworkQuirkMetrics instance.
47      */
NetworkQuirkMetrics()48     public NetworkQuirkMetrics() {
49         this(new Dependencies());
50     }
51 
52     @VisibleForTesting
NetworkQuirkMetrics(Dependencies deps)53     public NetworkQuirkMetrics(Dependencies deps) {
54         mDependencies = deps;
55     }
56 
57     /**
58      * Write the network Quirk Event into mStatsBuilder.
59      */
setEvent(NetworkQuirkEvent event)60     public void setEvent(NetworkQuirkEvent event) {
61         mStatsBuilder.setEvent(event);
62     }
63 
64     /**
65      * Write the NetworkStackQuirkReported proto into statsd.
66      */
statsWrite()67     public NetworkStackQuirkReported statsWrite() {
68         final NetworkStackQuirkReported stats = mStatsBuilder.build();
69         mDependencies.writeStats(stats.getEvent().getNumber());
70         return stats;
71     }
72 }
73