1 /* 2 * Copyright (C) 2023 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.metrics; 18 19 import android.annotation.NonNull; 20 import android.app.StatsManager; 21 import android.content.Context; 22 import android.os.Handler; 23 import android.util.Log; 24 import android.util.StatsEvent; 25 26 import com.android.modules.utils.HandlerExecutor; 27 28 import java.util.List; 29 import java.util.function.Supplier; 30 31 /** 32 * A class to register, sample and send connectivity state metrics. 33 */ 34 public class ConnectivitySampleMetricsHelper implements StatsManager.StatsPullAtomCallback { 35 private static final String TAG = ConnectivitySampleMetricsHelper.class.getSimpleName(); 36 37 final Supplier<StatsEvent> mDelegate; 38 39 /** 40 * Start collecting metrics. 41 * @param context some context to get services 42 * @param connectivityServiceHandler the connectivity service handler 43 * @param atomTag the tag to collect metrics from 44 * @param delegate a method returning data when called on the handler thread 45 */ 46 // Unfortunately it seems essentially impossible to unit test this method. The only thing 47 // to test is that there is a call to setPullAtomCallback, but StatsManager is final and 48 // can't be mocked without mockito-extended. Using mockito-extended in FrameworksNetTests 49 // would have a very large impact on performance, while splitting the unit test for this 50 // class in a separate target would make testing very hard to manage. Therefore, there 51 // can unfortunately be no unit tests for this method, but at least it is very simple. start(@onNull final Context context, @NonNull final Handler connectivityServiceHandler, final int atomTag, @NonNull final Supplier<StatsEvent> delegate)52 public static void start(@NonNull final Context context, 53 @NonNull final Handler connectivityServiceHandler, 54 final int atomTag, 55 @NonNull final Supplier<StatsEvent> delegate) { 56 final ConnectivitySampleMetricsHelper metrics = 57 new ConnectivitySampleMetricsHelper(delegate); 58 final StatsManager mgr = context.getSystemService(StatsManager.class); 59 if (null == mgr) return; // No metrics for you 60 mgr.setPullAtomCallback(atomTag, null /* metadata */, 61 new HandlerExecutor(connectivityServiceHandler), metrics); 62 } 63 ConnectivitySampleMetricsHelper(@onNull final Supplier<StatsEvent> delegate)64 public ConnectivitySampleMetricsHelper(@NonNull final Supplier<StatsEvent> delegate) { 65 mDelegate = delegate; 66 } 67 68 @Override onPullAtom(final int atomTag, final List<StatsEvent> data)69 public int onPullAtom(final int atomTag, final List<StatsEvent> data) { 70 Log.d(TAG, "Sampling data for atom : " + atomTag); 71 data.add(mDelegate.get()); 72 return StatsManager.PULL_SUCCESS; 73 } 74 } 75