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.NetworkCycleDataForUid;
28 import com.android.settingslib.net.NetworkCycleDataForUidLoader;
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 for a specific app uid.
35  */
36 public class AppSpecificDataUsageManager {
37 
38     /**
39      * Callback when the NetworkDataCyclesForUid result is loaded.
40      */
41     public interface AppSpecificDataLoaderCallback {
42         /**
43          * Called when the data is successfully loaded from
44          * {@link AppSpecificDataUsageManager.NetworkDataCyclesForUidResult}.
45          */
onDataLoaded(List<NetworkCycleDataForUid> networkCycleChartDataForUid)46         void onDataLoaded(List<NetworkCycleDataForUid> networkCycleChartDataForUid);
47     }
48 
49     @VisibleForTesting
50     static final int NETWORK_CYCLE_LOADER_ID = 12;
51 
52     private final Context mContext;
53     private final NetworkTemplate mNetworkTemplate;
54     private final int mUid;
55     private final List<AppSpecificDataUsageManager.AppSpecificDataLoaderCallback> mListeners =
56             new ArrayList<>();
57 
AppSpecificDataUsageManager(Context context, NetworkTemplate networkTemplate, int uid)58     AppSpecificDataUsageManager(Context context, NetworkTemplate networkTemplate, int uid) {
59         mContext = context;
60         mNetworkTemplate = networkTemplate;
61         mUid = uid;
62     }
63 
64     /**
65      * Registers a listener that will be notified once the data is loaded.
66      */
registerListener( AppSpecificDataUsageManager.AppSpecificDataLoaderCallback listener)67     public void registerListener(
68             AppSpecificDataUsageManager.AppSpecificDataLoaderCallback listener) {
69         if (!mListeners.contains(listener)) {
70             mListeners.add(listener);
71         }
72     }
73 
74     /**
75      * Unregisters the listener.
76      */
unregisterListener( AppSpecificDataUsageManager.AppSpecificDataLoaderCallback listener)77     public void unregisterListener(
78             AppSpecificDataUsageManager.AppSpecificDataLoaderCallback listener) {
79         mListeners.remove(listener);
80     }
81 
82     /**
83      * Start calculating the storage stats for app uid.
84      */
startLoading(LoaderManager loaderManager)85     public void startLoading(LoaderManager loaderManager) {
86         loaderManager.restartLoader(NETWORK_CYCLE_LOADER_ID, null,
87                 new NetworkDataCyclesForUidResult());
88     }
89 
onReceive(List<NetworkCycleDataForUid> networkCycleDataLoaderList)90     private void onReceive(List<NetworkCycleDataForUid> networkCycleDataLoaderList) {
91         for (AppSpecificDataUsageManager.AppSpecificDataLoaderCallback listener : mListeners) {
92             listener.onDataLoaded(networkCycleDataLoaderList);
93         }
94     }
95 
96     /**
97      * Callback to calculate network data cycles for app uid.
98      */
99     private class NetworkDataCyclesForUidResult implements LoaderManager.LoaderCallbacks
100             <List<NetworkCycleDataForUid>> {
101         @Override
onCreateLoader(int id, Bundle args)102         public Loader<List<NetworkCycleDataForUid>> onCreateLoader(int id, Bundle args) {
103             NetworkCycleDataForUidLoader.Builder builder =
104                     NetworkCycleDataForUidLoader.builder(mContext)
105                             .setRetrieveDetail(true)
106                             .addUid(mUid);
107             builder.setNetworkTemplate(mNetworkTemplate);
108             return builder.build();
109         }
110 
111         @Override
onLoadFinished(@onNull Loader<List<NetworkCycleDataForUid>> loader, List<NetworkCycleDataForUid> networkCycleDataLoaderList)112         public void onLoadFinished(@NonNull Loader<List<NetworkCycleDataForUid>> loader,
113                 List<NetworkCycleDataForUid> networkCycleDataLoaderList) {
114             onReceive(networkCycleDataLoaderList);
115         }
116 
117         @Override
onLoaderReset(Loader<List<NetworkCycleDataForUid>> loader)118         public void onLoaderReset(Loader<List<NetworkCycleDataForUid>> loader) {}
119     }
120 }
121