1 /* 2 * Copyright 2022 Google LLC 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 package com.google.android.libraries.mobiledatadownload.internal.logging; 17 18 import static com.google.common.util.concurrent.Futures.immediateVoidFuture; 19 import static com.google.common.util.concurrent.MoreExecutors.directExecutor; 20 21 import android.content.Context; 22 import com.google.android.libraries.mobiledatadownload.Flags; 23 import com.google.android.libraries.mobiledatadownload.annotations.InstanceId; 24 import com.google.android.libraries.mobiledatadownload.internal.ApplicationContext; 25 import com.google.android.libraries.mobiledatadownload.tracing.PropagatedFutures; 26 import com.google.common.base.Optional; 27 import com.google.common.util.concurrent.ListenableFuture; 28 import com.google.mobiledatadownload.internal.MetadataProto.FileGroupLoggingState; 29 import java.util.List; 30 import javax.inject.Inject; 31 32 /** 33 * Log MDD network stats at daily maintenance. For each file group, it will log the total bytes 34 * downloaded on Wifi and Cellular and also total bytes downloaded by MDD on Wifi and Cellular. 35 */ 36 public class NetworkLogger { 37 38 private final EventLogger eventLogger; 39 private final Flags flags; 40 private final LoggingStateStore loggingStateStore; 41 42 @Inject NetworkLogger( @pplicationContext Context context, EventLogger eventLogger, @InstanceId Optional<String> instanceIdOptional, Flags flags, LoggingStateStore loggingStateStore)43 public NetworkLogger( 44 @ApplicationContext Context context, 45 EventLogger eventLogger, 46 @InstanceId Optional<String> instanceIdOptional, 47 Flags flags, 48 LoggingStateStore loggingStateStore) { 49 this.eventLogger = eventLogger; 50 this.flags = flags; 51 this.loggingStateStore = loggingStateStore; 52 } 53 log()54 public ListenableFuture<Void> log() { 55 if (!flags.logNetworkStats()) { 56 return immediateVoidFuture(); 57 } 58 59 // Clear the accumulated network usage even if the device isn't logging, otherwise with 1% 60 // sampling, we could potentially log network usage for up to 100 days. 61 ListenableFuture<List<FileGroupLoggingState>> allDataUsageFuture = 62 loggingStateStore.getAndResetAllDataUsage(); 63 64 return eventLogger.logMddNetworkStats( 65 () -> 66 PropagatedFutures.transform( 67 allDataUsageFuture, this::buildNetworkStats, directExecutor())); 68 } 69 buildNetworkStats(List<FileGroupLoggingState> allDataUsage)70 private Void buildNetworkStats(List<FileGroupLoggingState> allDataUsage) { 71 long totalMddWifiCount = 0; 72 long totalMddCellularCount = 0; 73 Void networkStatsBuilder = null; 74 75 return networkStatsBuilder; 76 } 77 } 78