Home | History | Annotate | Download | only in analytics
      1 /*
      2  * Copyright 2016, 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.managedprovisioning.analytics;
     18 
     19 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_NETWORK_TYPE;
     20 import static com.android.internal.util.Preconditions.checkNotNull;
     21 
     22 import android.content.Context;
     23 import android.net.NetworkInfo;
     24 
     25 import com.android.internal.annotations.VisibleForTesting;
     26 import com.android.managedprovisioning.common.Utils;
     27 
     28 /**
     29  * Utility class to log the network type used while provisioning.
     30  */
     31 public class NetworkTypeLogger {
     32 
     33     public static final String NETWORK_TYPE_NOT_CONNECTED = "network_type_not_connected";
     34 
     35     private final Context mContext;
     36     private final MetricsLoggerWrapper mMetricsLoggerWrapper;
     37     private final Utils mUtils;
     38 
     39     public NetworkTypeLogger(Context context) {
     40         this(context, new Utils(), new MetricsLoggerWrapper());
     41     }
     42 
     43     @VisibleForTesting
     44     NetworkTypeLogger(
     45             Context context,
     46             Utils utils,
     47             MetricsLoggerWrapper metricsLoggerWrapper) {
     48         mContext = checkNotNull(context);
     49         mUtils = checkNotNull(utils);
     50         mMetricsLoggerWrapper = checkNotNull(metricsLoggerWrapper);
     51     }
     52 
     53     /**
     54      * Logs the network type to which the device is connected.
     55      */
     56     public void log() {
     57         final NetworkInfo networkInfo = mUtils.getActiveNetworkInfo(mContext);
     58         if (mUtils.isConnectedToNetwork(mContext)) {
     59             final int networkType = networkInfo.getType();
     60             mMetricsLoggerWrapper.logAction(mContext, PROVISIONING_NETWORK_TYPE, networkType);
     61         } else {
     62             mMetricsLoggerWrapper.logAction(mContext, PROVISIONING_NETWORK_TYPE,
     63                     NETWORK_TYPE_NOT_CONNECTED);
     64         }
     65     }
     66 }
     67