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 package com.android.car.settings.datausage; 17 18 import android.content.Context; 19 import android.net.NetworkTemplate; 20 import android.os.Bundle; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.VisibleForTesting; 24 import androidx.loader.app.LoaderManager; 25 import androidx.loader.content.Loader; 26 27 import com.android.settingslib.net.NetworkCycleChartData; 28 import com.android.settingslib.net.NetworkCycleChartDataLoader; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 33 /** 34 * Class to manage the callbacks needed to calculate data usage cycles. 35 */ 36 public class DataUsageCycleManager { 37 38 /** 39 * Callback for when NetworkDataCycles is loaded. 40 */ 41 public interface DataUsageCycleLoaderCallback { 42 /** 43 * Called when the data is successfully loaded from 44 * {@link DataUsageCycleManager.NetworkDataCyclesResult}. 45 */ onDataLoaded(List<NetworkCycleChartData> networkCycleChartData)46 void onDataLoaded(List<NetworkCycleChartData> networkCycleChartData); 47 } 48 49 @VisibleForTesting 50 static final int NETWORK_CYCLE_LOADER_ID = 11; 51 52 private final Context mContext; 53 private final NetworkTemplate mNetworkTemplate; 54 private final List<DataUsageCycleManager.DataUsageCycleLoaderCallback> mListeners = 55 new ArrayList<>(); 56 DataUsageCycleManager(Context context, NetworkTemplate networkTemplate)57 DataUsageCycleManager(Context context, NetworkTemplate networkTemplate) { 58 mContext = context; 59 mNetworkTemplate = networkTemplate; 60 } 61 62 /** 63 * Registers a listener that will be notified once the data is loaded. 64 */ registerListener(DataUsageCycleManager.DataUsageCycleLoaderCallback listener)65 public void registerListener(DataUsageCycleManager.DataUsageCycleLoaderCallback listener) { 66 if (!mListeners.contains(listener)) { 67 mListeners.add(listener); 68 } 69 } 70 71 /** 72 * Unregisters the listener. 73 */ unregisterListener(DataUsageCycleManager.DataUsageCycleLoaderCallback listener)74 public void unregisterListener(DataUsageCycleManager.DataUsageCycleLoaderCallback listener) { 75 mListeners.remove(listener); 76 } 77 78 /** 79 * Start calculating the storage stats. 80 */ startLoading(LoaderManager loaderManager)81 public void startLoading(LoaderManager loaderManager) { 82 loaderManager.restartLoader(NETWORK_CYCLE_LOADER_ID, null, new NetworkDataCyclesResult()); 83 } 84 onReceive(List<NetworkCycleChartData> networkCycleDataLoaderList)85 private void onReceive(List<NetworkCycleChartData> networkCycleDataLoaderList) { 86 for (DataUsageCycleManager.DataUsageCycleLoaderCallback listener : mListeners) { 87 listener.onDataLoaded(networkCycleDataLoaderList); 88 } 89 } 90 91 /** 92 * Callback to calculate network data cycles. 93 */ 94 private class NetworkDataCyclesResult implements LoaderManager.LoaderCallbacks 95 <List<NetworkCycleChartData>> { 96 @Override onCreateLoader(int id, Bundle args)97 public Loader<List<NetworkCycleChartData>> onCreateLoader(int id, Bundle args) { 98 return NetworkCycleChartDataLoader.builder(mContext) 99 .setNetworkTemplate(mNetworkTemplate) 100 .build(); 101 } 102 103 @Override onLoadFinished(@onNull Loader<List<NetworkCycleChartData>> loader, List<NetworkCycleChartData> networkCycleDataLoaderList)104 public void onLoadFinished(@NonNull Loader<List<NetworkCycleChartData>> loader, 105 List<NetworkCycleChartData> networkCycleDataLoaderList) { 106 onReceive(networkCycleDataLoaderList); 107 } 108 109 @Override onLoaderReset(Loader<List<NetworkCycleChartData>> loader)110 public void onLoaderReset(Loader<List<NetworkCycleChartData>> loader) {} 111 } 112 } 113