Merge "Telephony.Carriers.MVNO_TYPE is Deprecated." into main
diff --git a/Android.bp b/Android.bp
index 20f8634..f5b593d 100644
--- a/Android.bp
+++ b/Android.bp
@@ -123,6 +123,13 @@
         "androidx.room_room-compiler-plugin",
     ],
 
+    errorprone: {
+        extra_check_modules: ["//external/nullaway:nullaway_plugin"],
+        javacflags: [
+            "-XepOpt:NullAway:AnnotatedPackages=com.android.settings",
+        ],
+    },
+
     libs: [
         "telephony-common",
         "ims-common",
diff --git a/res/xml/app_data_usage.xml b/res/xml/app_data_usage.xml
index 7d4eaab..034015c 100644
--- a/res/xml/app_data_usage.xml
+++ b/res/xml/app_data_usage.xml
@@ -21,7 +21,8 @@
     android:title="@string/data_usage_app_summary_title">
 
     <com.android.settings.datausage.SpinnerPreference
-        android:key="cycle" />
+        android:key="cycle"
+        settings:controller="com.android.settings.datausage.AppDataUsageCycleController" />
 
     <PreferenceCategory
         android:key="app_data_usage_summary_category">
diff --git a/src/com/android/settings/applications/appinfo/AppDataUsagePreferenceController.java b/src/com/android/settings/applications/appinfo/AppDataUsagePreferenceController.java
index 32662a2..baa1daf 100644
--- a/src/com/android/settings/applications/appinfo/AppDataUsagePreferenceController.java
+++ b/src/com/android/settings/applications/appinfo/AppDataUsagePreferenceController.java
@@ -17,7 +17,6 @@
 package com.android.settings.applications.appinfo;
 
 import android.content.Context;
-import android.net.NetworkStats;
 import android.net.NetworkTemplate;
 import android.os.Bundle;
 import android.os.Process;
@@ -34,8 +33,8 @@
 import com.android.settings.SettingsPreferenceFragment;
 import com.android.settings.Utils;
 import com.android.settings.datausage.AppDataUsage;
-import com.android.settings.datausage.DataUsageUtils;
-import com.android.settings.network.SubscriptionUtil;
+import com.android.settings.datausage.lib.NetworkTemplates;
+import com.android.settings.spa.app.appinfo.AppDataUsagePreferenceKt;
 import com.android.settingslib.AppItem;
 import com.android.settingslib.applications.AppUtils;
 import com.android.settingslib.core.lifecycle.LifecycleObserver;
@@ -46,6 +45,10 @@
 
 import java.util.List;
 
+/**
+ * @deprecated Will be removed, use {@link AppDataUsagePreferenceKt} instead.
+ */
+@Deprecated(forRemoval = true)
 public class AppDataUsagePreferenceController extends AppInfoPreferenceControllerBase
         implements LoaderManager.LoaderCallbacks<List<NetworkCycleDataForUid>>, LifecycleObserver,
         OnResume, OnPause {
@@ -92,7 +95,7 @@
 
     @Override
     public Loader<List<NetworkCycleDataForUid>> onCreateLoader(int id, Bundle args) {
-        final NetworkTemplate template = getTemplate(mContext);
+        final NetworkTemplate template = NetworkTemplates.INSTANCE.getDefaultTemplate(mContext);
         final int uid = mParent.getAppEntry().info.uid;
 
         final NetworkCycleDataForUidLoader.Builder builder =
@@ -147,18 +150,6 @@
         return mContext.getString(R.string.computing_size);
     }
 
-    private static NetworkTemplate getTemplate(Context context) {
-        if (SubscriptionUtil.isSimHardwareVisible(context)
-                && DataUsageUtils.hasReadyMobileRadio(context)) {
-            return new NetworkTemplate.Builder(NetworkTemplate.MATCH_MOBILE).setMeteredness(
-                    NetworkStats.METERED_YES).build();
-        }
-        if (DataUsageUtils.hasWifiRadio(context)) {
-            return new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build();
-        }
-        return new NetworkTemplate.Builder(NetworkTemplate.MATCH_ETHERNET).build();
-    }
-
     @VisibleForTesting
     boolean isBandwidthControlEnabled() {
         return Utils.isBandwidthControlEnabled();
diff --git a/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollEnrolling.java b/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollEnrolling.java
index c62ab3b..be2a948 100644
--- a/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollEnrolling.java
+++ b/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollEnrolling.java
@@ -203,6 +203,10 @@
         final int currentDensity = displayDensity.getDefaultDisplayDensityValues()
                 [currentDensityIndex];
         final int defaultDensity = displayDensity.getDefaultDensityForDefaultDisplay();
+
+        if (getResources().getConfiguration().fontScale > 1) {
+            return false;
+        }
         return defaultDensity == currentDensity;
     }
 
diff --git a/src/com/android/settings/datausage/AppDataUsage.java b/src/com/android/settings/datausage/AppDataUsage.java
index 67d33a7..f2e0261 100644
--- a/src/com/android/settings/datausage/AppDataUsage.java
+++ b/src/com/android/settings/datausage/AppDataUsage.java
@@ -17,6 +17,7 @@
 import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
 
 import static com.android.settings.datausage.lib.AppDataUsageRepository.getAppUid;
+import static com.android.settings.datausage.lib.AppDataUsageRepository.getAppUidList;
 
 import android.app.Activity;
 import android.app.settings.SettingsEnums;
@@ -28,19 +29,12 @@
 import android.os.Bundle;
 import android.os.Process;
 import android.os.UserHandle;
-import android.telephony.SubscriptionManager;
 import android.util.ArraySet;
 import android.util.IconDrawableFactory;
 import android.util.Log;
-import android.util.Range;
-import android.view.View;
-import android.widget.AdapterView;
 
 import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
 import androidx.annotation.VisibleForTesting;
-import androidx.loader.app.LoaderManager;
-import androidx.loader.content.Loader;
 import androidx.preference.Preference;
 import androidx.preference.Preference.OnPreferenceChangeListener;
 import androidx.recyclerview.widget.DefaultItemAnimator;
@@ -48,17 +42,20 @@
 
 import com.android.settings.R;
 import com.android.settings.applications.AppInfoBase;
+import com.android.settings.datausage.lib.AppDataUsageDetailsRepository;
+import com.android.settings.datausage.lib.NetworkTemplates;
+import com.android.settings.datausage.lib.NetworkUsageDetailsData;
 import com.android.settings.network.SubscriptionUtil;
 import com.android.settings.widget.EntityHeaderController;
 import com.android.settingslib.AppItem;
 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
 import com.android.settingslib.RestrictedLockUtilsInternal;
 import com.android.settingslib.RestrictedSwitchPreference;
-import com.android.settingslib.net.NetworkCycleDataForUid;
-import com.android.settingslib.net.NetworkCycleDataForUidLoader;
 import com.android.settingslib.net.UidDetail;
 import com.android.settingslib.net.UidDetailProvider;
 
+import kotlin.Unit;
+
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -77,11 +74,8 @@
     private static final String KEY_FOREGROUND_USAGE = "foreground_usage";
     private static final String KEY_BACKGROUND_USAGE = "background_usage";
     private static final String KEY_RESTRICT_BACKGROUND = "restrict_background";
-    private static final String KEY_CYCLE = "cycle";
     private static final String KEY_UNRESTRICTED_DATA = "unrestricted_data_saver";
 
-    private static final int LOADER_APP_USAGE_DATA = 2;
-
     private PackageManager mPackageManager;
     private final ArraySet<String> mPackages = new ArraySet<>();
     private Preference mTotalUsage;
@@ -94,14 +88,10 @@
     CharSequence mLabel;
     @VisibleForTesting
     String mPackageName;
-    private CycleAdapter mCycleAdapter;
 
-    @Nullable
-    private List<NetworkCycleDataForUid> mUsageData;
     @VisibleForTesting
     NetworkTemplate mTemplate;
     private AppItem mAppItem;
-    private SpinnerPreference mCycle;
     private RestrictedSwitchPreference mUnrestrictedData;
     private DataSaverBackend mDataSaverBackend;
     private Context mContext;
@@ -128,15 +118,16 @@
         mSelectedCycle = (args != null) ? args.getLong(ARG_SELECTED_CYCLE) : 0L;
 
         if (mTemplate == null) {
-            mTemplate = DataUsageUtils.getDefaultTemplate(mContext,
-                    SubscriptionManager.getDefaultDataSubscriptionId());
+            mTemplate = NetworkTemplates.INSTANCE.getDefaultTemplate(mContext);
         }
+        final Activity activity = requireActivity();
+        activity.setTitle(NetworkTemplates.getTitleResId(mTemplate));
         if (mAppItem == null) {
             int uid = (args != null) ? args.getInt(AppInfoBase.ARG_PACKAGE_UID, -1)
                     : getActivity().getIntent().getIntExtra(AppInfoBase.ARG_PACKAGE_UID, -1);
             if (uid == -1) {
                 // TODO: Log error.
-                getActivity().finish();
+                activity.finish();
             } else {
                 addUid(uid);
                 mAppItem = new AppItem(uid);
@@ -160,7 +151,8 @@
         mForegroundUsage = findPreference(KEY_FOREGROUND_USAGE);
         mBackgroundUsage = findPreference(KEY_BACKGROUND_USAGE);
 
-        initCycle();
+        final List<Integer> uidList = getAppUidList(mAppItem.uids);
+        initCycle(uidList);
 
         final UidDetailProvider uidDetailProvider = getUidDetailProvider();
 
@@ -191,7 +183,7 @@
             }
             mDataSaverBackend = new DataSaverBackend(mContext);
 
-            use(AppDataUsageListController.class).init(mAppItem.uids);
+            use(AppDataUsageListController.class).init(uidList);
         } else {
             final Context context = getActivity();
             final UidDetail uidDetail = uidDetailProvider.getUidDetail(mAppItem.key, true);
@@ -207,11 +199,9 @@
     }
 
     @Override
-    public void onResume() {
-        super.onResume();
-        // No animations will occur before:
-        //  - LOADER_APP_USAGE_DATA initially updates the cycle
-        //  - updatePrefs() initially updates the preference visibility
+    public void onStart() {
+        super.onStart();
+        // No animations will occur before bindData() initially updates the cycle.
         // This is mainly for the cycle spinner, because when the page is entered from the
         // AppInfoDashboardFragment, there is no way to know whether the cycle data is available
         // before finished the async loading.
@@ -219,11 +209,14 @@
         // setBackPreferenceListAnimatorIfLoaded().
         mIsLoading = true;
         getListView().setItemAnimator(null);
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
         if (mDataSaverBackend != null) {
             mDataSaverBackend.addListener(this);
         }
-        LoaderManager.getInstance(this).restartLoader(LOADER_APP_USAGE_DATA, null /* args */,
-                mUidDataCallbacks);
         updatePrefs();
     }
 
@@ -268,14 +261,16 @@
         return new UidDetailProvider(mContext);
     }
 
-    private void initCycle() {
-        mCycle = findPreference(KEY_CYCLE);
-        mCycleAdapter = new CycleAdapter(mContext, mCycle);
+    @VisibleForTesting
+    void initCycle(List<Integer> uidList) {
+        var controller = use(AppDataUsageCycleController.class);
+        var repository = new AppDataUsageDetailsRepository(mContext, mTemplate, mCycles, uidList);
+        controller.init(repository, data -> {
+            bindData(data);
+            return Unit.INSTANCE;
+        });
         if (mCycles != null) {
-            // If coming from a page like DataUsageList where already has a selected cycle, display
-            // that before loading to reduce flicker.
-            mCycleAdapter.setInitialCycleList(mCycles, mSelectedCycle);
-            mCycle.setHasCycles(true);
+            controller.setInitialCycles(mCycles, mSelectedCycle);
         }
     }
 
@@ -326,22 +321,13 @@
     }
 
     @VisibleForTesting
-    void bindData(int position) {
-        final long backgroundBytes, foregroundBytes;
-        if (mUsageData == null || position >= mUsageData.size()) {
-            backgroundBytes = foregroundBytes = 0;
-            mCycle.setHasCycles(false);
-        } else {
-            mCycle.setHasCycles(true);
-            final NetworkCycleDataForUid data = mUsageData.get(position);
-            backgroundBytes = data.getBackgroudUsage();
-            foregroundBytes = data.getForegroudUsage();
-        }
-        final long totalBytes = backgroundBytes + foregroundBytes;
-
-        mTotalUsage.setSummary(DataUsageUtils.formatDataUsage(mContext, totalBytes));
-        mForegroundUsage.setSummary(DataUsageUtils.formatDataUsage(mContext, foregroundBytes));
-        mBackgroundUsage.setSummary(DataUsageUtils.formatDataUsage(mContext, backgroundBytes));
+    void bindData(@NonNull NetworkUsageDetailsData data) {
+        mIsLoading = false;
+        mTotalUsage.setSummary(DataUsageUtils.formatDataUsage(mContext, data.getTotalUsage()));
+        mForegroundUsage.setSummary(
+                DataUsageUtils.formatDataUsage(mContext, data.getForegroundUsage()));
+        mBackgroundUsage.setSummary(
+                DataUsageUtils.formatDataUsage(mContext, data.getBackgroundUsage()));
     }
 
     private boolean getAppRestrictBackground() {
@@ -391,71 +377,6 @@
         return SettingsEnums.APP_DATA_USAGE;
     }
 
-    private final AdapterView.OnItemSelectedListener mCycleListener =
-            new AdapterView.OnItemSelectedListener() {
-        @Override
-        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
-            bindData(position);
-        }
-
-        @Override
-        public void onNothingSelected(AdapterView<?> parent) {
-            // ignored
-        }
-    };
-
-    @VisibleForTesting
-    final LoaderManager.LoaderCallbacks<List<NetworkCycleDataForUid>> mUidDataCallbacks =
-            new LoaderManager.LoaderCallbacks<>() {
-                @Override
-                @NonNull
-                public Loader<List<NetworkCycleDataForUid>> onCreateLoader(int id, Bundle args) {
-                    final NetworkCycleDataForUidLoader.Builder<?> builder =
-                            NetworkCycleDataForUidLoader.builder(mContext);
-                    builder.setRetrieveDetail(true)
-                            .setNetworkTemplate(mTemplate);
-                    for (int i = 0; i < mAppItem.uids.size(); i++) {
-                        builder.addUid(mAppItem.uids.keyAt(i));
-                    }
-                    if (mCycles != null) {
-                        builder.setCycles(mCycles);
-                    }
-                    return builder.build();
-                }
-
-                @Override
-                public void onLoadFinished(@NonNull Loader<List<NetworkCycleDataForUid>> loader,
-                        List<NetworkCycleDataForUid> data) {
-                    mUsageData = data;
-                    mCycle.setOnItemSelectedListener(mCycleListener);
-                    mCycleAdapter.updateCycleList(data.stream()
-                            .map(cycle -> new Range<>(cycle.getStartTime(), cycle.getEndTime()))
-                            .toList());
-                    if (mSelectedCycle > 0L) {
-                        final int numCycles = data.size();
-                        int position = 0;
-                        for (int i = 0; i < numCycles; i++) {
-                            final NetworkCycleDataForUid cycleData = data.get(i);
-                            if (cycleData.getEndTime() == mSelectedCycle) {
-                                position = i;
-                                break;
-                            }
-                        }
-                        if (position > 0) {
-                            mCycle.setSelection(position);
-                        }
-                        bindData(position);
-                    } else {
-                        bindData(0 /* position */);
-                    }
-                    mIsLoading = false;
-                }
-
-                @Override
-                public void onLoaderReset(@NonNull Loader<List<NetworkCycleDataForUid>> loader) {
-                }
-            };
-
     @Override
     public void onDataSaverChanged(boolean isDataSaving) {
 
diff --git a/src/com/android/settings/datausage/AppDataUsageActivity.java b/src/com/android/settings/datausage/AppDataUsageActivity.java
index 48bedce..9a5a2cb 100644
--- a/src/com/android/settings/datausage/AppDataUsageActivity.java
+++ b/src/com/android/settings/datausage/AppDataUsageActivity.java
@@ -17,9 +17,9 @@
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.os.Bundle;
+import android.provider.Settings;
 import android.util.Log;
 
-import com.android.settings.R;
 import com.android.settings.SettingsActivity;
 import com.android.settingslib.AppItem;
 
@@ -61,14 +61,12 @@
         args.putParcelable(AppDataUsage.ARG_APP_ITEM, appItem);
         intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
         intent.putExtra(EXTRA_SHOW_FRAGMENT, AppDataUsage.class.getName());
-        intent.putExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, R.string.data_usage_app_summary_title);
 
         super.onCreate(savedInstanceState);
     }
 
     @Override
     protected boolean isValidFragment(String fragmentName) {
-        return super.isValidFragment(fragmentName)
-                || AppDataUsage.class.getName().equals(fragmentName);
+        return AppDataUsage.class.getName().equals(fragmentName);
     }
 }
diff --git a/src/com/android/settings/datausage/AppDataUsageCycleController.kt b/src/com/android/settings/datausage/AppDataUsageCycleController.kt
new file mode 100644
index 0000000..b1a0e76
--- /dev/null
+++ b/src/com/android/settings/datausage/AppDataUsageCycleController.kt
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.datausage
+
+import android.content.Context
+import android.view.View
+import android.widget.AdapterView
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleOwner
+import androidx.lifecycle.lifecycleScope
+import androidx.lifecycle.repeatOnLifecycle
+import androidx.preference.PreferenceScreen
+import com.android.settings.core.BasePreferenceController
+import com.android.settings.datausage.lib.AppDataUsageDetailsRepository
+import com.android.settings.datausage.lib.IAppDataUsageDetailsRepository
+import com.android.settings.datausage.lib.NetworkUsageDetailsData
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+
+class AppDataUsageCycleController(context: Context, preferenceKey: String) :
+    BasePreferenceController(context, preferenceKey) {
+
+    private lateinit var repository: IAppDataUsageDetailsRepository
+    private var onUsageDataUpdated: (NetworkUsageDetailsData) -> Unit = {}
+    private lateinit var preference: SpinnerPreference
+    private var cycleAdapter: CycleAdapter? = null
+
+    private var initialCycles: List<Long> = emptyList()
+    private var initialSelectedEndTime: Long = -1
+
+    private var usageDetailsDataList: List<NetworkUsageDetailsData> = emptyList()
+
+    fun init(
+        repository: IAppDataUsageDetailsRepository,
+        onUsageDataUpdated: (NetworkUsageDetailsData) -> Unit,
+    ) {
+        this.repository = repository
+        this.onUsageDataUpdated = onUsageDataUpdated
+    }
+
+    /**
+     * Sets the initial cycles.
+     *
+     * If coming from a page like DataUsageList where already has a selected cycle, display that
+     * before loading to reduce flicker.
+     */
+    fun setInitialCycles(initialCycles: List<Long>, initialSelectedEndTime: Long) {
+        this.initialCycles = initialCycles
+        this.initialSelectedEndTime = initialSelectedEndTime
+    }
+
+    override fun getAvailabilityStatus() = AVAILABLE
+
+    override fun displayPreference(screen: PreferenceScreen) {
+        super.displayPreference(screen)
+        preference = screen.findPreference(preferenceKey)!!
+        if (cycleAdapter == null) {
+            cycleAdapter = CycleAdapter(mContext, preference).apply {
+                if (initialCycles.isNotEmpty()) {
+                    setInitialCycleList(initialCycles, initialSelectedEndTime)
+                    preference.setHasCycles(true)
+                }
+            }
+        }
+    }
+
+    override fun onViewCreated(viewLifecycleOwner: LifecycleOwner) {
+        viewLifecycleOwner.lifecycleScope.launch {
+            viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
+                update()
+            }
+        }
+    }
+
+    private suspend fun update() {
+        usageDetailsDataList = withContext(Dispatchers.Default) {
+            repository.queryDetailsForCycles()
+        }
+        if (usageDetailsDataList.isEmpty()) {
+            preference.setHasCycles(false)
+            onUsageDataUpdated(NetworkUsageDetailsData.AllZero)
+            return
+        }
+
+        preference.setHasCycles(true)
+        cycleAdapter?.updateCycleList(usageDetailsDataList.map { it.range })
+        preference.setOnItemSelectedListener(cycleListener)
+    }
+
+    private val cycleListener = object : AdapterView.OnItemSelectedListener {
+        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
+            usageDetailsDataList.getOrNull(position)?.let(onUsageDataUpdated)
+        }
+
+        override fun onNothingSelected(parent: AdapterView<*>?) {
+            // ignored
+        }
+    }
+}
diff --git a/src/com/android/settings/datausage/AppDataUsageListController.kt b/src/com/android/settings/datausage/AppDataUsageListController.kt
index e39ed7e..85a6e92 100644
--- a/src/com/android/settings/datausage/AppDataUsageListController.kt
+++ b/src/com/android/settings/datausage/AppDataUsageListController.kt
@@ -28,6 +28,7 @@
 import androidx.preference.PreferenceScreen
 import com.android.settings.core.BasePreferenceController
 import com.android.settings.datausage.lib.AppDataUsageRepository.Companion.getAppUid
+import com.android.settings.datausage.lib.AppDataUsageRepository.Companion.getAppUidList
 import com.android.settings.datausage.lib.AppPreferenceRepository
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.launch
@@ -43,8 +44,8 @@
     private var uids: List<Int> = emptyList()
     private lateinit var preference: PreferenceGroup
 
-    fun init(uids: SparseBooleanArray) {
-        this.uids = uids.keyIterator().asSequence().map { getAppUid(it) }.distinct().toList()
+    fun init(uids: List<Int>) {
+        this.uids = uids
     }
 
     override fun getAvailabilityStatus() = AVAILABLE
diff --git a/src/com/android/settings/datausage/BillingCycleSettings.java b/src/com/android/settings/datausage/BillingCycleSettings.java
index c3ddb2e..0c07c19 100644
--- a/src/com/android/settings/datausage/BillingCycleSettings.java
+++ b/src/com/android/settings/datausage/BillingCycleSettings.java
@@ -44,6 +44,7 @@
 
 import com.android.settings.R;
 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
+import com.android.settings.datausage.lib.NetworkTemplates;
 import com.android.settings.network.SubscriptionUtil;
 import com.android.settings.network.telephony.MobileNetworkUtils;
 import com.android.settings.search.BaseSearchIndexProvider;
@@ -128,8 +129,7 @@
         }
 
         if (mNetworkTemplate == null) {
-            mNetworkTemplate = DataUsageUtils.getDefaultTemplate(context,
-                DataUsageUtils.getDefaultSubscriptionId(context));
+            mNetworkTemplate = NetworkTemplates.INSTANCE.getDefaultTemplate(context);
         }
 
         mBillingCycle = findPreference(KEY_BILLING_CYCLE);
diff --git a/src/com/android/settings/datausage/DataUsageSummary.java b/src/com/android/settings/datausage/DataUsageSummary.java
index 4f876ab..5681c92 100644
--- a/src/com/android/settings/datausage/DataUsageSummary.java
+++ b/src/com/android/settings/datausage/DataUsageSummary.java
@@ -36,11 +36,11 @@
 import androidx.preference.PreferenceScreen;
 
 import com.android.settings.R;
+import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.datausage.lib.DataUsageLib;
 import com.android.settings.network.ProxySubscriptionManager;
 import com.android.settings.network.SubscriptionUtil;
 import com.android.settings.network.telephony.MobileNetworkUtils;
-import com.android.settingslib.NetworkPolicyEditor;
 import com.android.settingslib.core.AbstractPreferenceController;
 
 import java.util.ArrayList;
@@ -49,7 +49,7 @@
 /**
  * Settings preference fragment that displays data usage summary.
  */
-public class DataUsageSummary extends DataUsageBaseFragment implements DataUsageEditController {
+public class DataUsageSummary extends DashboardFragment {
 
     private static final String TAG = "DataUsageSummary";
 
@@ -57,14 +57,9 @@
 
     public static final String KEY_RESTRICT_BACKGROUND = "restrict_background";
 
-    private static final String KEY_STATUS_HEADER = "status_header";
-
     // Mobile data keys
     public static final String KEY_MOBILE_USAGE_TITLE = "mobile_category";
 
-    private DataUsageSummaryPreference mSummaryPreference;
-    private DataUsageSummaryPreferenceController mSummaryController;
-    private NetworkTemplate mDefaultTemplate;
     private ProxySubscriptionManager mProxySubscriptionMgr;
 
     @Override
@@ -100,8 +95,6 @@
         if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
             hasMobileData = false;
         }
-        mDefaultTemplate = DataUsageUtils.getDefaultTemplate(context, defaultSubId);
-        mSummaryPreference = findPreference(KEY_STATUS_HEADER);
 
         if (!hasMobileData || !UserManager.get(context).isAdminUser()) {
             removePreference(KEY_RESTRICT_BACKGROUND);
@@ -127,15 +120,6 @@
     }
 
     @Override
-    public boolean onPreferenceTreeClick(Preference preference) {
-        if (preference == findPreference(KEY_STATUS_HEADER)) {
-            BillingCycleSettings.BytesEditorFragment.show(this, false);
-            return false;
-        }
-        return super.onPreferenceTreeClick(preference);
-    }
-
-    @Override
     protected int getPreferenceScreenResId() {
         return R.xml.data_usage;
     }
@@ -153,9 +137,8 @@
             MobileNetworkUtils.isMobileNetworkUserRestricted(context)) {
             return controllers;
         }
-        mSummaryController =
-                new DataUsageSummaryPreferenceController(activity,
-                        DataUsageUtils.getDefaultSubscriptionId(activity));
+        final var mSummaryController = new DataUsageSummaryPreferenceController(activity,
+                DataUsageUtils.getDefaultSubscriptionId(activity));
         controllers.add(mSummaryController);
         return controllers;
     }
@@ -269,22 +252,6 @@
         return SettingsEnums.DATA_USAGE_SUMMARY;
     }
 
-    @Override
-    public NetworkPolicyEditor getNetworkPolicyEditor() {
-        return services.mPolicyEditor;
-    }
-
-    @Override
-    public NetworkTemplate getNetworkTemplate() {
-        return mDefaultTemplate;
-    }
-
-    @Override
-    public void updateDataUsage() {
-        updateState();
-        mSummaryController.updateState(mSummaryPreference);
-    }
-
     private static boolean isGuestUser(Context context) {
         if (context == null) return false;
         final UserManager userManager = context.getSystemService(UserManager.class);
diff --git a/src/com/android/settings/datausage/DataUsageUtils.java b/src/com/android/settings/datausage/DataUsageUtils.java
index 0c6f4c8..2bbf3e2 100644
--- a/src/com/android/settings/datausage/DataUsageUtils.java
+++ b/src/com/android/settings/datausage/DataUsageUtils.java
@@ -17,7 +17,6 @@
 import static android.content.pm.PackageManager.FEATURE_ETHERNET;
 import static android.content.pm.PackageManager.FEATURE_USB_HOST;
 import static android.content.pm.PackageManager.FEATURE_WIFI;
-import static android.telephony.TelephonyManager.SIM_STATE_READY;
 
 import android.app.usage.NetworkStats.Bucket;
 import android.app.usage.NetworkStatsManager;
@@ -49,7 +48,6 @@
 public final class DataUsageUtils {
     static final boolean TEST_RADIOS = false;
     static final String TEST_RADIOS_PROP = "test.radios";
-    private static final boolean LOGD = false;
     private static final String ETHERNET = "ethernet";
     private static final String TAG = "DataUsageUtils";
 
@@ -107,44 +105,6 @@
     }
 
     /**
-     * Test if device has a mobile data radio with SIM in ready state.
-     */
-    public static boolean hasReadyMobileRadio(Context context) {
-        if (DataUsageUtils.TEST_RADIOS) {
-            return SystemProperties.get(DataUsageUtils.TEST_RADIOS_PROP).contains("mobile");
-        }
-        final List<SubscriptionInfo> subInfoList =
-                ProxySubscriptionManager.getInstance(context)
-                .getActiveSubscriptionsInfo();
-        // No activated Subscriptions
-        if (subInfoList == null) {
-            if (LOGD) {
-                Log.d(TAG, "hasReadyMobileRadio: subInfoList=null");
-            }
-            return false;
-        }
-        final TelephonyManager tele = context.getSystemService(TelephonyManager.class);
-        // require both supported network and ready SIM
-        boolean isReady = true;
-        for (SubscriptionInfo subInfo : subInfoList) {
-            isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
-            if (LOGD) {
-                Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
-            }
-        }
-
-        final boolean isDataCapable = tele.isDataCapable();
-        final boolean retVal = isDataCapable && isReady;
-        if (LOGD) {
-            Log.d(TAG, "hasReadyMobileRadio:"
-                    + " telephonManager.isDataCapable()="
-                    + isDataCapable
-                    + " isReady=" + isReady);
-        }
-        return retVal;
-    }
-
-    /**
      * Whether device has a Wi-Fi data radio.
      */
     public static boolean hasWifiRadio(Context context) {
diff --git a/src/com/android/settings/datausage/SpinnerPreference.java b/src/com/android/settings/datausage/SpinnerPreference.java
index a705079..c81ac55 100644
--- a/src/com/android/settings/datausage/SpinnerPreference.java
+++ b/src/com/android/settings/datausage/SpinnerPreference.java
@@ -94,7 +94,6 @@
                 @Override
                 public void onItemSelected(
                         AdapterView<?> parent, View view, int position, long id) {
-                    if (mPosition == position) return;
                     mPosition = position;
                     mCurrentObject = mAdapter.getItem(position);
                     if (mListener != null) {
diff --git a/src/com/android/settings/datausage/lib/AppDataUsageDetailsRepository.kt b/src/com/android/settings/datausage/lib/AppDataUsageDetailsRepository.kt
new file mode 100644
index 0000000..7dc49bb
--- /dev/null
+++ b/src/com/android/settings/datausage/lib/AppDataUsageDetailsRepository.kt
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.datausage.lib
+
+import android.app.usage.NetworkStats
+import android.app.usage.NetworkStatsManager
+import android.content.Context
+import android.net.NetworkTemplate
+import android.util.Log
+import android.util.Range
+import androidx.annotation.VisibleForTesting
+import kotlinx.coroutines.async
+import kotlinx.coroutines.awaitAll
+import kotlinx.coroutines.coroutineScope
+
+interface IAppDataUsageDetailsRepository {
+    suspend fun queryDetailsForCycles(): List<NetworkUsageDetailsData>
+}
+
+class AppDataUsageDetailsRepository @JvmOverloads constructor(
+    context: Context,
+    private val template: NetworkTemplate,
+    private val cycles: List<Long>?,
+    private val uids: List<Int>,
+    private val networkCycleDataRepository: INetworkCycleDataRepository =
+        NetworkCycleDataRepository(context, template)
+) : IAppDataUsageDetailsRepository {
+    private val networkStatsManager = context.getSystemService(NetworkStatsManager::class.java)!!
+
+    override suspend fun queryDetailsForCycles(): List<NetworkUsageDetailsData> = coroutineScope {
+        getCycles().map {
+            async {
+                queryDetails(it)
+            }
+        }.awaitAll().filter { it.totalUsage > 0 }
+    }
+
+    private fun getCycles(): List<Range<Long>> =
+        cycles?.zipWithNext { endTime, startTime -> Range(startTime, endTime) }
+            ?: networkCycleDataRepository.getCycles()
+
+    private fun queryDetails(range: Range<Long>): NetworkUsageDetailsData {
+        var totalUsage = 0L
+        var foregroundUsage = 0L
+        for (uid in uids) {
+            val usage = getUsage(range, uid, NetworkStats.Bucket.STATE_ALL)
+            if (usage > 0L) {
+                totalUsage += usage
+                foregroundUsage +=
+                    getUsage(range, uid, NetworkStats.Bucket.STATE_FOREGROUND)
+            }
+        }
+        return NetworkUsageDetailsData(
+            range = range,
+            totalUsage = totalUsage,
+            foregroundUsage = foregroundUsage,
+            backgroundUsage = totalUsage - foregroundUsage,
+        )
+    }
+
+    @VisibleForTesting
+    fun getUsage(range: Range<Long>, uid: Int, state: Int): Long = try {
+        networkStatsManager.queryDetailsForUidTagState(
+            template, range.lower, range.upper, uid, NetworkStats.Bucket.TAG_NONE, state,
+        ).getTotalUsage()
+    } catch (e: Exception) {
+        Log.e(TAG, "Exception querying network detail.", e)
+        0
+    }
+
+    private fun NetworkStats.getTotalUsage(): Long = use {
+        var bytes = 0L
+        val bucket = NetworkStats.Bucket()
+        while (getNextBucket(bucket)) {
+            bytes += bucket.rxBytes + bucket.txBytes
+        }
+        return bytes
+    }
+
+    private companion object {
+        private const val TAG = "AppDataUsageDetailsRepo"
+    }
+}
diff --git a/src/com/android/settings/datausage/lib/AppDataUsageRepository.kt b/src/com/android/settings/datausage/lib/AppDataUsageRepository.kt
index ccd3e60..bde25ab 100644
--- a/src/com/android/settings/datausage/lib/AppDataUsageRepository.kt
+++ b/src/com/android/settings/datausage/lib/AppDataUsageRepository.kt
@@ -25,7 +25,9 @@
 import android.os.UserHandle
 import android.util.Log
 import android.util.SparseArray
+import android.util.SparseBooleanArray
 import androidx.annotation.VisibleForTesting
+import androidx.core.util.keyIterator
 import com.android.settings.R
 import com.android.settingslib.AppItem
 import com.android.settingslib.net.UidDetailProvider
@@ -196,6 +198,10 @@
         )
 
         @JvmStatic
+        fun getAppUidList(uids: SparseBooleanArray) =
+            uids.keyIterator().asSequence().map { getAppUid(it) }.distinct().toList()
+
+        @JvmStatic
         fun getAppUid(uid: Int): Int {
             if (Process.isSdkSandboxUid(uid)) {
                 // For a sandbox process, get the associated app UID
diff --git a/src/com/android/settings/datausage/lib/NetworkCycleDataRepository.kt b/src/com/android/settings/datausage/lib/NetworkCycleDataRepository.kt
index f10d506..cfd1053 100644
--- a/src/com/android/settings/datausage/lib/NetworkCycleDataRepository.kt
+++ b/src/com/android/settings/datausage/lib/NetworkCycleDataRepository.kt
@@ -33,6 +33,7 @@
 
 interface INetworkCycleDataRepository {
     suspend fun loadCycles(): List<NetworkUsageData>
+    fun getCycles(): List<Range<Long>>
     fun getPolicy(): NetworkPolicy?
     suspend fun querySummary(startTime: Long, endTime: Long): NetworkCycleChartData?
 }
@@ -48,7 +49,7 @@
     override suspend fun loadCycles(): List<NetworkUsageData> =
         getCycles().queryUsage().filter { it.usage > 0 }
 
-    private fun getCycles(): List<Range<Long>> {
+    override fun getCycles(): List<Range<Long>> {
         val policy = getPolicy() ?: return queryCyclesAsFourWeeks()
         return policy.cycleIterator().asSequence().map {
             Range(it.lower.toInstant().toEpochMilli(), it.upper.toInstant().toEpochMilli())
diff --git a/src/com/android/settings/datausage/lib/NetworkTemplates.kt b/src/com/android/settings/datausage/lib/NetworkTemplates.kt
new file mode 100644
index 0000000..9020070
--- /dev/null
+++ b/src/com/android/settings/datausage/lib/NetworkTemplates.kt
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.datausage.lib
+
+import android.content.Context
+import android.net.NetworkTemplate
+import android.telephony.SubscriptionManager
+import androidx.annotation.StringRes
+import com.android.settings.R
+import com.android.settings.datausage.DataUsageUtils
+
+interface INetworkTemplates {
+    /**
+     * Returns the default network template based on the availability of mobile data, Wifi. Returns
+     * ethernet template if both mobile data and Wifi are not available.
+     */
+    fun getDefaultTemplate(context: Context): NetworkTemplate
+}
+
+object NetworkTemplates : INetworkTemplates {
+    @JvmStatic
+    @StringRes
+    fun NetworkTemplate.getTitleResId(): Int =
+        when (matchRule) {
+            NetworkTemplate.MATCH_MOBILE,
+            NetworkTemplate.MATCH_CARRIER -> R.string.cellular_data_usage
+
+            NetworkTemplate.MATCH_WIFI -> R.string.wifi_data_usage
+            NetworkTemplate.MATCH_ETHERNET -> R.string.ethernet_data_usage
+            else -> R.string.data_usage_app_summary_title
+        }
+
+    /**
+     * Returns the default network template based on the availability of mobile data, Wifi. Returns
+     * ethernet template if both mobile data and Wifi are not available.
+     */
+    override fun getDefaultTemplate(context: Context): NetworkTemplate =
+        DataUsageUtils.getDefaultTemplate(
+            context,
+            SubscriptionManager.getDefaultDataSubscriptionId(),
+        )
+}
diff --git a/src/com/android/settings/datausage/lib/NetworkUsageDetailsData.kt b/src/com/android/settings/datausage/lib/NetworkUsageDetailsData.kt
new file mode 100644
index 0000000..19ff81c
--- /dev/null
+++ b/src/com/android/settings/datausage/lib/NetworkUsageDetailsData.kt
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.datausage.lib
+
+import android.util.Range
+
+/**
+ * Details data structure representing usage data in a period.
+ */
+data class NetworkUsageDetailsData(
+    val range: Range<Long>,
+    val totalUsage: Long,
+    val foregroundUsage: Long,
+    val backgroundUsage: Long,
+) {
+    companion object {
+        val AllZero = NetworkUsageDetailsData(
+            range = Range(0, 0),
+            totalUsage = 0,
+            foregroundUsage = 0,
+            backgroundUsage = 0,
+        )
+    }
+}
diff --git a/src/com/android/settings/spa/app/appinfo/AppDataUsagePreference.kt b/src/com/android/settings/spa/app/appinfo/AppDataUsagePreference.kt
index 5210dc7..6c128dc 100644
--- a/src/com/android/settings/spa/app/appinfo/AppDataUsagePreference.kt
+++ b/src/com/android/settings/spa/app/appinfo/AppDataUsagePreference.kt
@@ -18,13 +18,13 @@
 
 import android.content.Context
 import android.content.pm.ApplicationInfo
-import android.net.NetworkStats
 import android.net.NetworkTemplate
 import android.os.Process
 import android.text.format.DateUtils
 import android.text.format.Formatter
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberCoroutineScope
 import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.res.stringResource
 import androidx.lifecycle.compose.collectAsStateWithLifecycle
@@ -32,25 +32,41 @@
 import com.android.settings.Utils
 import com.android.settings.applications.appinfo.AppInfoDashboardFragment
 import com.android.settings.datausage.AppDataUsage
-import com.android.settings.datausage.DataUsageUtils
+import com.android.settings.datausage.lib.INetworkTemplates
+import com.android.settings.datausage.lib.NetworkTemplates
+import com.android.settings.datausage.lib.NetworkTemplates.getTitleResId
 import com.android.settingslib.net.NetworkCycleDataForUid
 import com.android.settingslib.net.NetworkCycleDataForUidLoader
 import com.android.settingslib.spa.framework.compose.toState
 import com.android.settingslib.spa.widget.preference.Preference
 import com.android.settingslib.spa.widget.preference.PreferenceModel
 import com.android.settingslib.spaprivileged.model.app.hasFlag
+import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.flow.SharingStarted
 import kotlinx.coroutines.flow.flow
+import kotlinx.coroutines.flow.map
+import kotlinx.coroutines.flow.shareIn
 import kotlinx.coroutines.withContext
 
 @Composable
-fun AppDataUsagePreference(app: ApplicationInfo) {
+fun AppDataUsagePreference(
+    app: ApplicationInfo,
+    networkTemplates: INetworkTemplates = NetworkTemplates,
+) {
     val context = LocalContext.current
-    val presenter = remember { AppDataUsagePresenter(context, app) }
+    val coroutineScope = rememberCoroutineScope()
+    val presenter = remember {
+        AppDataUsagePresenter(context, app, coroutineScope, networkTemplates)
+    }
     if (!presenter.isAvailableFlow.collectAsStateWithLifecycle(initialValue = false).value) return
 
     Preference(object : PreferenceModel {
-        override val title = stringResource(R.string.data_usage_app_summary_title)
+        override val title = stringResource(
+            presenter.titleResIdFlow.collectAsStateWithLifecycle(
+                initialValue = R.string.summary_placeholder,
+            ).value
+        )
         override val summary = presenter.summaryFlow.collectAsStateWithLifecycle(
             initialValue = stringResource(R.string.computing_size),
         )
@@ -62,6 +78,8 @@
 private class AppDataUsagePresenter(
     private val context: Context,
     private val app: ApplicationInfo,
+    coroutineScope: CoroutineScope,
+    networkTemplates: INetworkTemplates,
 ) {
     val isAvailableFlow = flow { emit(isAvailable()) }
 
@@ -71,10 +89,17 @@
 
     fun isEnabled() = app.hasFlag(ApplicationInfo.FLAG_INSTALLED)
 
-    val summaryFlow = flow { emit(getSummary()) }
+    private val templateFlow = flow {
+        emit(withContext(Dispatchers.IO) {
+            networkTemplates.getDefaultTemplate(context)
+        })
+    }.shareIn(coroutineScope, SharingStarted.WhileSubscribed(), 1)
 
-    private suspend fun getSummary() = withContext(Dispatchers.IO) {
-        val appUsageData = getAppUsageData()
+    val titleResIdFlow = templateFlow.map { it.getTitleResId() }
+    val summaryFlow = templateFlow.map { getSummary(it) }
+
+    private suspend fun getSummary(template: NetworkTemplate) = withContext(Dispatchers.IO) {
+        val appUsageData = getAppUsageData(template)
         val totalBytes = appUsageData.sumOf { it.totalUsage }
         if (totalBytes == 0L) {
             context.getString(R.string.no_data_usage)
@@ -88,15 +113,15 @@
         }
     }
 
-    private suspend fun getAppUsageData(): List<NetworkCycleDataForUid> =
+    private suspend fun getAppUsageData(template: NetworkTemplate): List<NetworkCycleDataForUid> =
         withContext(Dispatchers.IO) {
-            createLoader().loadInBackground() ?: emptyList()
+            createLoader(template).loadInBackground() ?: emptyList()
         }
 
-    private fun createLoader(): NetworkCycleDataForUidLoader =
+    private fun createLoader(template: NetworkTemplate): NetworkCycleDataForUidLoader =
         NetworkCycleDataForUidLoader.builder(context).apply {
             setRetrieveDetail(false)
-            setNetworkTemplate(getTemplate())
+            setNetworkTemplate(template)
             addUid(app.uid)
             if (Process.isApplicationUid(app.uid)) {
                 // Also add in network usage for the app's SDK sandbox
@@ -104,18 +129,6 @@
             }
         }.build()
 
-    private fun getTemplate(): NetworkTemplate = when {
-        DataUsageUtils.hasReadyMobileRadio(context) -> {
-            NetworkTemplate.Builder(NetworkTemplate.MATCH_MOBILE)
-                .setMeteredness(NetworkStats.METERED_YES)
-                .build()
-        }
-        DataUsageUtils.hasWifiRadio(context) -> {
-            NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build()
-        }
-        else -> NetworkTemplate.Builder(NetworkTemplate.MATCH_ETHERNET).build()
-    }
-
     fun startActivity() {
         AppInfoDashboardFragment.startAppInfoFragment(
             AppDataUsage::class.java,
diff --git a/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java b/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java
index e4b91c6..7b7c7a6 100644
--- a/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java
+++ b/tests/robotests/src/com/android/settings/datausage/AppDataUsageTest.java
@@ -41,8 +41,8 @@
 import android.os.Bundle;
 import android.os.Process;
 import android.telephony.SubscriptionManager;
-import android.text.format.DateUtils;
 import android.util.ArraySet;
+import android.util.Range;
 
 import androidx.fragment.app.FragmentActivity;
 import androidx.preference.Preference;
@@ -51,6 +51,7 @@
 import androidx.recyclerview.widget.RecyclerView;
 
 import com.android.settings.applications.AppInfoBase;
+import com.android.settings.datausage.lib.NetworkUsageDetailsData;
 import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settings.testutils.shadow.ShadowDataUsageUtils;
 import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
@@ -61,8 +62,6 @@
 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
 import com.android.settingslib.RestrictedSwitchPreference;
 import com.android.settingslib.core.AbstractPreferenceController;
-import com.android.settingslib.net.NetworkCycleDataForUid;
-import com.android.settingslib.net.NetworkCycleDataForUidLoader;
 import com.android.settingslib.net.UidDetail;
 import com.android.settingslib.net.UidDetailProvider;
 
@@ -80,7 +79,6 @@
 import org.robolectric.shadows.ShadowSubscriptionManager;
 import org.robolectric.util.ReflectionHelpers;
 
-import java.util.ArrayList;
 import java.util.List;
 
 @RunWith(RobolectricTestRunner.class)
@@ -254,44 +252,27 @@
     }
 
     @Test
-    public void bindData_noAppUsageData_shouldHideCycleSpinner() {
-        mFragment = spy(new TestFragment());
-        final SpinnerPreference cycle = mock(SpinnerPreference.class);
-        ReflectionHelpers.setField(mFragment, "mCycle", cycle);
-        final Preference preference = mock(Preference.class);
-        ReflectionHelpers.setField(mFragment, "mBackgroundUsage", preference);
-        ReflectionHelpers.setField(mFragment, "mForegroundUsage", preference);
-        ReflectionHelpers.setField(mFragment, "mTotalUsage", preference);
-        ReflectionHelpers.setField(mFragment, "mContext", RuntimeEnvironment.application);
-
-        mFragment.bindData(0 /* position */);
-
-        verify(cycle).setHasCycles(false);
-    }
-
-    @Test
-    public void bindData_hasAppUsageData_shouldShowCycleSpinnerAndUpdateUsageSummary() {
+    public void bindData_shouldUpdateUsageSummary() {
         mFragment = spy(new TestFragment());
         final Context context = RuntimeEnvironment.application;
         ReflectionHelpers.setField(mFragment, "mContext", context);
         final long backgroundBytes = 1234L;
         final long foregroundBytes = 5678L;
-        final List<NetworkCycleDataForUid> appUsage = new ArrayList<>();
-        appUsage.add(new NetworkCycleDataForUid.Builder()
-                .setBackgroundUsage(backgroundBytes).setForegroundUsage(foregroundBytes).build());
-        ReflectionHelpers.setField(mFragment, "mUsageData", appUsage);
+        final NetworkUsageDetailsData appUsage = new NetworkUsageDetailsData(
+                new Range<>(1L, 2L),
+                backgroundBytes + foregroundBytes,
+                foregroundBytes,
+                backgroundBytes
+        );
         final Preference backgroundPref = mock(Preference.class);
         ReflectionHelpers.setField(mFragment, "mBackgroundUsage", backgroundPref);
         final Preference foregroundPref = mock(Preference.class);
         ReflectionHelpers.setField(mFragment, "mForegroundUsage", foregroundPref);
         final Preference totalPref = mock(Preference.class);
         ReflectionHelpers.setField(mFragment, "mTotalUsage", totalPref);
-        final SpinnerPreference cycle = mock(SpinnerPreference.class);
-        ReflectionHelpers.setField(mFragment, "mCycle", cycle);
 
-        mFragment.bindData(0 /* position */);
+        mFragment.bindData(appUsage);
 
-        verify(cycle).setHasCycles(true);
         verify(totalPref).setSummary(
                 DataUsageUtils.formatDataUsage(context, backgroundBytes + foregroundBytes));
         verify(backgroundPref).setSummary(DataUsageUtils.formatDataUsage(context, backgroundBytes));
@@ -299,125 +280,11 @@
     }
 
     @Test
-    public void onCreateLoader_categoryApp_shouldQueryDataUsageUsingAppKey() {
-        mFragment = new TestFragment();
-        final Context context = RuntimeEnvironment.application;
-        final int testUid = 123123;
-        final AppItem appItem = new AppItem(testUid);
-        appItem.addUid(testUid);
-        appItem.category = AppItem.CATEGORY_APP;
-        ReflectionHelpers.setField(mFragment, "mContext", context);
-        ReflectionHelpers.setField(mFragment, "mAppItem", appItem);
-        ReflectionHelpers.setField(mFragment, "mTemplate",
-                new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build());
-        final long end = System.currentTimeMillis();
-        final long start = end - (DateUtils.WEEK_IN_MILLIS * 4);
-
-        final NetworkCycleDataForUidLoader loader = (NetworkCycleDataForUidLoader)
-                mFragment.mUidDataCallbacks.onCreateLoader(0, Bundle.EMPTY);
-
-        final List<Integer> uids = loader.getUids();
-        assertThat(uids).hasSize(1);
-        assertThat(uids.get(0)).isEqualTo(testUid);
-    }
-
-    @Test
-    public void onCreateLoader_categoryUser_shouldQueryDataUsageUsingAssociatedUids() {
-        mFragment = new TestFragment();
-        final Context context = RuntimeEnvironment.application;
-        final int testUserId = 11;
-        final AppItem appItem = new AppItem(testUserId);
-        appItem.category = AppItem.CATEGORY_USER;
-        appItem.addUid(123);
-        appItem.addUid(456);
-        appItem.addUid(789);
-        ReflectionHelpers.setField(mFragment, "mContext", context);
-        ReflectionHelpers.setField(mFragment, "mAppItem", appItem);
-        ReflectionHelpers.setField(mFragment, "mTemplate",
-                new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build());
-        final long end = System.currentTimeMillis();
-        final long start = end - (DateUtils.WEEK_IN_MILLIS * 4);
-
-        final NetworkCycleDataForUidLoader loader = (NetworkCycleDataForUidLoader)
-                mFragment.mUidDataCallbacks.onCreateLoader(0, Bundle.EMPTY);
-
-        final List<Integer> uids = loader.getUids();
-        assertThat(uids).hasSize(3);
-        assertThat(uids.get(0)).isEqualTo(123);
-        assertThat(uids.get(1)).isEqualTo(456);
-        assertThat(uids.get(2)).isEqualTo(789);
-    }
-
-    @Test
-    public void onCreateLoader_hasCyclesSpecified_shouldQueryDataUsageForSpecifiedCycles() {
-        final long startTime = 1521583200000L;
-        final long endTime = 1521676800000L;
-        ArrayList<Long> testCycles = new ArrayList<>();
-        testCycles.add(endTime);
-        testCycles.add(startTime);
-        final int uid = 123;
-        final AppItem appItem = new AppItem(uid);
-        appItem.category = AppItem.CATEGORY_APP;
-        appItem.addUid(uid);
-
-        mFragment = new TestFragment();
-        ReflectionHelpers.setField(mFragment, "mContext", RuntimeEnvironment.application);
-        ReflectionHelpers.setField(mFragment, "mCycles", testCycles);
-        ReflectionHelpers.setField(mFragment, "mAppItem", appItem);
-        ReflectionHelpers.setField(mFragment, "mTemplate",
-                new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build());
-
-        final NetworkCycleDataForUidLoader loader = (NetworkCycleDataForUidLoader)
-                mFragment.mUidDataCallbacks.onCreateLoader(0 /* id */, Bundle.EMPTY /* args */);
-
-        final ArrayList<Long> cycles = loader.getCycles();
-        assertThat(cycles).hasSize(2);
-        assertThat(cycles.get(0)).isEqualTo(endTime);
-        assertThat(cycles.get(1)).isEqualTo(startTime);
-    }
-
-    @Test
-    public void onLoadFinished_hasSelectedCycleSpecified_shouldSelectSpecifiedCycle() {
-        final long now = System.currentTimeMillis();
-        final long tenDaysAgo = now - (DateUtils.DAY_IN_MILLIS * 10);
-        final long twentyDaysAgo = now - (DateUtils.DAY_IN_MILLIS * 20);
-        final long thirtyDaysAgo = now - (DateUtils.DAY_IN_MILLIS * 30);
-        final List<NetworkCycleDataForUid> data = new ArrayList<>();
-        NetworkCycleDataForUid.Builder builder = new NetworkCycleDataForUid.Builder();
-        builder.setStartTime(thirtyDaysAgo).setEndTime(twentyDaysAgo).setTotalUsage(9876L);
-        data.add(builder.build());
-        builder = new NetworkCycleDataForUid.Builder();
-        builder.setStartTime(twentyDaysAgo).setEndTime(tenDaysAgo).setTotalUsage(5678L);
-        data.add(builder.build());
-        builder = new NetworkCycleDataForUid.Builder();
-        builder.setStartTime(tenDaysAgo).setEndTime(now).setTotalUsage(1234L);
-        data.add(builder.build());
-
-        mFragment = new TestFragment();
-        ReflectionHelpers.setField(mFragment, "mContext", RuntimeEnvironment.application);
-        ReflectionHelpers.setField(mFragment, "mCycleAdapter", mock(CycleAdapter.class));
-        ReflectionHelpers.setField(mFragment, "mSelectedCycle", tenDaysAgo);
-        final Preference backgroundPref = mock(Preference.class);
-        ReflectionHelpers.setField(mFragment, "mBackgroundUsage", backgroundPref);
-        final Preference foregroundPref = mock(Preference.class);
-        ReflectionHelpers.setField(mFragment, "mForegroundUsage", foregroundPref);
-        final Preference totalPref = mock(Preference.class);
-        ReflectionHelpers.setField(mFragment, "mTotalUsage", totalPref);
-        final SpinnerPreference cycle = mock(SpinnerPreference.class);
-        ReflectionHelpers.setField(mFragment, "mCycle", cycle);
-
-        mFragment.mUidDataCallbacks.onLoadFinished(null /* loader */, data);
-
-        verify(cycle).setSelection(1);
-    }
-
-    @Test
     @Config(shadows = {ShadowDataUsageUtils.class, ShadowSubscriptionManager.class,
             ShadowFragment.class})
     public void onCreate_noNetworkTemplateAndInvalidDataSubscription_shouldUseWifiTemplate() {
         ShadowDataUsageUtils.IS_MOBILE_DATA_SUPPORTED = true;
         ShadowDataUsageUtils.IS_WIFI_SUPPORTED = true;
-        ShadowDataUsageUtils.HAS_SIM = false;
         ShadowSubscriptionManager.setDefaultDataSubscriptionId(
                 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
         mFragment = spy(new TestFragment());
@@ -448,6 +315,10 @@
         }
 
         @Override
+        void initCycle(List<Integer> uidList) {
+        }
+
+        @Override
         public boolean isSimHardwareVisible(Context context) {
             return true;
         }
diff --git a/tests/robotests/src/com/android/settings/datausage/DataUsageSummaryTest.java b/tests/robotests/src/com/android/settings/datausage/DataUsageSummaryTest.java
index 4ac7387..2a9cca9 100644
--- a/tests/robotests/src/com/android/settings/datausage/DataUsageSummaryTest.java
+++ b/tests/robotests/src/com/android/settings/datausage/DataUsageSummaryTest.java
@@ -27,15 +27,11 @@
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 
-import android.app.usage.NetworkStatsManager;
 import android.content.Context;
 import android.content.res.Resources;
-import android.net.NetworkPolicyManager;
 import android.os.UserManager;
 import android.telephony.SubscriptionManager;
-import android.telephony.TelephonyManager;
 
-import androidx.fragment.app.FragmentActivity;
 import androidx.test.core.app.ApplicationProvider;
 
 import com.android.settings.R;
@@ -45,7 +41,6 @@
 import com.android.settings.testutils.shadow.ShadowUtils;
 
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -53,13 +48,9 @@
 import org.mockito.Spy;
 import org.mockito.junit.MockitoJUnit;
 import org.mockito.junit.MockitoRule;
-import org.robolectric.Robolectric;
 import org.robolectric.RobolectricTestRunner;
-import org.robolectric.Shadows;
 import org.robolectric.annotation.Config;
-import org.robolectric.shadows.ShadowApplication;
 import org.robolectric.shadows.ShadowSubscriptionManager;
-import org.robolectric.shadows.ShadowTelephonyManager;
 
 @Config(shadows = {
         ShadowUtils.class,
@@ -75,13 +66,6 @@
     Context mContext = ApplicationProvider.getApplicationContext();
     @Mock
     private UserManager mUserManager;
-    @Mock
-    private NetworkPolicyManager mNetworkPolicyManager;
-    @Mock
-    private NetworkStatsManager mNetworkStatsManager;
-    private TelephonyManager mTelephonyManager;
-    private Resources mResources;
-    private FragmentActivity mActivity;
 
     private DataUsageSummary mDataUsageSummary;
 
@@ -95,23 +79,12 @@
         doReturn(mUserManager).when(mContext).getSystemService(UserManager.class);
         doReturn(false).when(mUserManager).isGuestUser();
 
-        ShadowApplication shadowContext = ShadowApplication.getInstance();
         ShadowUserManager.getShadow().setIsAdminUser(true);
-        shadowContext.setSystemService(Context.NETWORK_POLICY_SERVICE, mNetworkPolicyManager);
 
-        mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
-        final ShadowTelephonyManager shadowTelephonyManager = Shadows.shadowOf(mTelephonyManager);
-        shadowTelephonyManager.setTelephonyManagerForSubscriptionId(
-                SubscriptionManager.INVALID_SUBSCRIPTION_ID, mTelephonyManager);
-        shadowTelephonyManager.setTelephonyManagerForSubscriptionId(1, mTelephonyManager);
-        mActivity = spy(Robolectric.buildActivity(FragmentActivity.class).get());
-
-        mResources = spy(mContext.getResources());
+        Resources mResources = spy(mContext.getResources());
         doReturn(mResources).when(mContext).getResources();
         doReturn(true).when(mResources).getBoolean(R.bool.config_show_sim_info);
 
-        doReturn(mNetworkStatsManager).when(mActivity).getSystemService(NetworkStatsManager.class);
-
         mDataUsageSummary = spy(new DataUsageSummary());
         doReturn(mContext).when(mDataUsageSummary).getContext();
         doNothing().when(mDataUsageSummary).enableProxySubscriptionManager(any());
@@ -130,12 +103,10 @@
 
     @Test
     @Config(shadows = ShadowSubscriptionManager.class)
-    @Ignore
     public void configuration_withSim_shouldShowMobileAndWifi() {
         ShadowDataUsageUtils.IS_MOBILE_DATA_SUPPORTED = true;
         ShadowDataUsageUtils.IS_WIFI_SUPPORTED = true;
         ShadowSubscriptionManager.setDefaultDataSubscriptionId(1);
-        ShadowDataUsageUtils.HAS_SIM = true;
 
         final DataUsageSummary dataUsageSummary = spy(new DataUsageSummary());
         doNothing().when(dataUsageSummary).enableProxySubscriptionManager(any());
@@ -156,7 +127,6 @@
     public void configuration_withoutSim_shouldShowWifiSectionOnly() {
         ShadowDataUsageUtils.IS_MOBILE_DATA_SUPPORTED = true;
         ShadowDataUsageUtils.IS_WIFI_SUPPORTED = true;
-        ShadowDataUsageUtils.HAS_SIM = false;
 
         final DataUsageSummary dataUsageSummary = spy(new DataUsageSummary());
         doNothing().when(dataUsageSummary).enableProxySubscriptionManager(any());
@@ -177,7 +147,6 @@
     public void configuration_withoutMobile_shouldShowWifiSectionOnly() {
         ShadowDataUsageUtils.IS_MOBILE_DATA_SUPPORTED = false;
         ShadowDataUsageUtils.IS_WIFI_SUPPORTED = true;
-        ShadowDataUsageUtils.HAS_SIM = false;
 
         final DataUsageSummary dataUsageSummary = spy(new DataUsageSummary());
         doNothing().when(dataUsageSummary).enableProxySubscriptionManager(any());
@@ -199,7 +168,6 @@
     public void configuration_invalidDataSusbscription_shouldShowWifiSectionOnly() {
         ShadowDataUsageUtils.IS_MOBILE_DATA_SUPPORTED = true;
         ShadowDataUsageUtils.IS_WIFI_SUPPORTED = true;
-        ShadowDataUsageUtils.HAS_SIM = false;
         ShadowSubscriptionManager.setDefaultDataSubscriptionId(
                 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
 
diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowDataUsageUtils.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowDataUsageUtils.java
index 9973a2d..dd09b3c 100644
--- a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowDataUsageUtils.java
+++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowDataUsageUtils.java
@@ -13,7 +13,6 @@
 
     public static boolean IS_MOBILE_DATA_SUPPORTED = true;
     public static boolean IS_WIFI_SUPPORTED = true;
-    public static boolean HAS_SIM = true;
     public static int DEFAULT_SUBSCRIPTION_ID = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
 
     @Implementation
@@ -32,10 +31,5 @@
     }
 
     @Implementation
-    protected static boolean hasSim(Context context) {
-        return HAS_SIM;
-    }
-
-    @Implementation
     protected static boolean hasEthernet(Context context) { return false; }
 }
diff --git a/tests/spa_unit/src/com/android/settings/datausage/AppDataUsageCycleControllerTest.kt b/tests/spa_unit/src/com/android/settings/datausage/AppDataUsageCycleControllerTest.kt
new file mode 100644
index 0000000..ea51f01
--- /dev/null
+++ b/tests/spa_unit/src/com/android/settings/datausage/AppDataUsageCycleControllerTest.kt
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.datausage
+
+import android.content.Context
+import android.util.Range
+import androidx.lifecycle.testing.TestLifecycleOwner
+import androidx.preference.PreferenceManager
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.android.settings.datausage.lib.AppDataUsageDetailsRepository
+import com.android.settings.datausage.lib.IAppDataUsageDetailsRepository
+import com.android.settings.datausage.lib.NetworkUsageDetailsData
+import com.google.common.truth.Truth.assertThat
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.runBlocking
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.kotlin.doReturn
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.spy
+import org.mockito.kotlin.stub
+import org.mockito.kotlin.verify
+
+@RunWith(AndroidJUnit4::class)
+class AppDataUsageCycleControllerTest {
+    private val context: Context = ApplicationProvider.getApplicationContext()
+
+    private val controller = AppDataUsageCycleController(context, KEY)
+
+    private val preference = spy(SpinnerPreference(context, null).apply { key = KEY })
+
+    private val preferenceScreen = PreferenceManager(context).createPreferenceScreen(context)
+
+    private val onUsageDataUpdated: (NetworkUsageDetailsData) -> Unit = {}
+
+    @Before
+    fun setUp() {
+        preferenceScreen.addPreference(preference)
+    }
+
+    @Test
+    fun onViewCreated_noUsage_hidePreference(): Unit = runBlocking {
+        val repository = object : IAppDataUsageDetailsRepository {
+            override suspend fun queryDetailsForCycles() = emptyList<NetworkUsageDetailsData>()
+        }
+        controller.init(repository, onUsageDataUpdated)
+        controller.displayPreference(preferenceScreen)
+
+        controller.onViewCreated(TestLifecycleOwner())
+        delay(100)
+
+        assertThat(preference.isVisible).isFalse()
+    }
+
+    @Test
+    fun onViewCreated_hasUsage_showPreference(): Unit = runBlocking {
+        val detailsData = NetworkUsageDetailsData(
+            range = Range(1, 2),
+            totalUsage = 11,
+            foregroundUsage = 1,
+            backgroundUsage = 10,
+        )
+        val repository = object : IAppDataUsageDetailsRepository {
+            override suspend fun queryDetailsForCycles() = listOf(detailsData)
+        }
+        controller.init(repository, onUsageDataUpdated)
+        controller.displayPreference(preferenceScreen)
+
+        controller.onViewCreated(TestLifecycleOwner())
+        delay(100)
+
+        assertThat(preference.isVisible).isTrue()
+    }
+
+    @Test
+    fun setInitialCycles() {
+        val repository = object : IAppDataUsageDetailsRepository {
+            override suspend fun queryDetailsForCycles() = emptyList<NetworkUsageDetailsData>()
+        }
+        controller.init(repository, onUsageDataUpdated)
+        controller.setInitialCycles(
+            initialCycles = listOf(CYCLE2_END_TIME, CYCLE1_END_TIME, CYCLE1_START_TIME),
+            initialSelectedEndTime = CYCLE1_END_TIME,
+        )
+
+        controller.displayPreference(preferenceScreen)
+
+        verify(preference).setSelection(1)
+    }
+
+    private companion object {
+        const val KEY = "test_key"
+        const val CYCLE1_START_TIME = 1694444444000L
+        const val CYCLE1_END_TIME = 1695555555000L
+        const val CYCLE2_END_TIME = 1695566666000L
+    }
+}
diff --git a/tests/spa_unit/src/com/android/settings/datausage/AppDataUsageListControllerTest.kt b/tests/spa_unit/src/com/android/settings/datausage/AppDataUsageListControllerTest.kt
index 6727232..4575a8d 100644
--- a/tests/spa_unit/src/com/android/settings/datausage/AppDataUsageListControllerTest.kt
+++ b/tests/spa_unit/src/com/android/settings/datausage/AppDataUsageListControllerTest.kt
@@ -17,7 +17,6 @@
 package com.android.settings.datausage
 
 import android.content.Context
-import android.util.SparseBooleanArray
 import androidx.lifecycle.testing.TestLifecycleOwner
 import androidx.preference.Preference
 import androidx.preference.PreferenceCategory
@@ -63,9 +62,7 @@
 
     @Test
     fun onViewCreated_singleUid_hidePreference(): Unit = runBlocking {
-        controller.init(SparseBooleanArray().apply {
-            put(UID_0, true)
-        })
+        controller.init(listOf(UID_0))
         controller.displayPreference(preferenceScreen)
 
         controller.onViewCreated(TestLifecycleOwner())
@@ -76,10 +73,7 @@
 
     @Test
     fun onViewCreated_twoUid_showPreference(): Unit = runBlocking {
-        controller.init(SparseBooleanArray().apply {
-            put(UID_0, true)
-            put(UID_1, true)
-        })
+        controller.init(listOf(UID_0, UID_1))
         controller.displayPreference(preferenceScreen)
 
         controller.onViewCreated(TestLifecycleOwner())
diff --git a/tests/spa_unit/src/com/android/settings/datausage/ChartDataUsagePreferenceControllerTest.kt b/tests/spa_unit/src/com/android/settings/datausage/ChartDataUsagePreferenceControllerTest.kt
index 1748f07..e0eb789 100644
--- a/tests/spa_unit/src/com/android/settings/datausage/ChartDataUsagePreferenceControllerTest.kt
+++ b/tests/spa_unit/src/com/android/settings/datausage/ChartDataUsagePreferenceControllerTest.kt
@@ -17,6 +17,7 @@
 package com.android.settings.datausage
 
 import android.content.Context
+import android.util.Range
 import androidx.lifecycle.testing.TestLifecycleOwner
 import androidx.preference.PreferenceScreen
 import androidx.test.core.app.ApplicationProvider
@@ -39,7 +40,7 @@
 
     private val repository = object : INetworkCycleDataRepository {
         override suspend fun loadCycles() = emptyList<NetworkUsageData>()
-
+        override fun getCycles() = emptyList<Range<Long>>()
         override fun getPolicy() = null
 
         override suspend fun querySummary(startTime: Long, endTime: Long) = when {
diff --git a/tests/spa_unit/src/com/android/settings/datausage/DataUsageListHeaderControllerTest.kt b/tests/spa_unit/src/com/android/settings/datausage/DataUsageListHeaderControllerTest.kt
index 35b70d6..581f7ba 100644
--- a/tests/spa_unit/src/com/android/settings/datausage/DataUsageListHeaderControllerTest.kt
+++ b/tests/spa_unit/src/com/android/settings/datausage/DataUsageListHeaderControllerTest.kt
@@ -18,6 +18,7 @@
 
 import android.content.Context
 import android.net.NetworkTemplate
+import android.util.Range
 import android.view.LayoutInflater
 import android.view.View
 import android.widget.Spinner
@@ -48,9 +49,8 @@
 
     private val repository = object : INetworkCycleDataRepository {
         override suspend fun loadCycles() = emptyList<NetworkUsageData>()
-
+        override fun getCycles() = emptyList<Range<Long>>()
         override fun getPolicy() = null
-
         override suspend fun querySummary(startTime: Long, endTime: Long) = null
     }
 
diff --git a/tests/spa_unit/src/com/android/settings/datausage/lib/AppDataUsageDetailsRepositoryTest.kt b/tests/spa_unit/src/com/android/settings/datausage/lib/AppDataUsageDetailsRepositoryTest.kt
new file mode 100644
index 0000000..7072b46
--- /dev/null
+++ b/tests/spa_unit/src/com/android/settings/datausage/lib/AppDataUsageDetailsRepositoryTest.kt
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.datausage.lib
+
+import android.app.usage.NetworkStats.Bucket
+import android.content.Context
+import android.net.NetworkTemplate
+import android.util.Range
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.google.common.truth.Truth.assertThat
+import kotlinx.coroutines.runBlocking
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.kotlin.doReturn
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.spy
+import org.mockito.kotlin.whenever
+
+@RunWith(AndroidJUnit4::class)
+class AppDataUsageDetailsRepositoryTest {
+    private val context: Context = ApplicationProvider.getApplicationContext()
+
+    private val template = mock<NetworkTemplate>()
+
+    private val networkCycleDataRepository = mock<INetworkCycleDataRepository> {
+        on { getCycles() } doReturn listOf(Range(CYCLE1_END_TIME, CYCLE2_END_TIME))
+    }
+
+    @Test
+    fun queryDetailsForCycles_hasCycles(): Unit = runBlocking {
+        val range = Range(CYCLE1_START_TIME, CYCLE1_END_TIME)
+        val repository = spy(
+            AppDataUsageDetailsRepository(
+                context = context,
+                cycles = listOf(CYCLE1_END_TIME, CYCLE1_START_TIME),
+                template = template,
+                uids = listOf(UID),
+                networkCycleDataRepository = networkCycleDataRepository,
+            )
+        ) {
+            doReturn(ALL_USAGE).whenever(mock).getUsage(range, UID, Bucket.STATE_ALL)
+            doReturn(FOREGROUND_USAGE).whenever(mock).getUsage(range, UID, Bucket.STATE_FOREGROUND)
+        }
+
+        val detailsForCycles = repository.queryDetailsForCycles()
+
+        assertThat(detailsForCycles).containsExactly(
+            NetworkUsageDetailsData(
+                range = range,
+                totalUsage = ALL_USAGE,
+                foregroundUsage = FOREGROUND_USAGE,
+                backgroundUsage = ALL_USAGE - FOREGROUND_USAGE,
+            )
+        )
+    }
+
+    @Test
+    fun queryDetailsForCycles_defaultCycles(): Unit = runBlocking {
+        val range = Range(CYCLE1_END_TIME, CYCLE2_END_TIME)
+        val repository = spy(
+            AppDataUsageDetailsRepository(
+                context = context,
+                cycles = null,
+                template = template,
+                uids = listOf(UID),
+                networkCycleDataRepository = networkCycleDataRepository,
+            )
+        ) {
+            doReturn(ALL_USAGE).whenever(mock).getUsage(range, UID, Bucket.STATE_ALL)
+            doReturn(FOREGROUND_USAGE).whenever(mock).getUsage(range, UID, Bucket.STATE_FOREGROUND)
+        }
+
+        val detailsForCycles = repository.queryDetailsForCycles()
+
+        assertThat(detailsForCycles).containsExactly(
+            NetworkUsageDetailsData(
+                range = range,
+                totalUsage = ALL_USAGE,
+                foregroundUsage = FOREGROUND_USAGE,
+                backgroundUsage = ALL_USAGE - FOREGROUND_USAGE,
+            )
+        )
+    }
+
+    private companion object {
+        const val CYCLE1_START_TIME = 1694444444000L
+        const val CYCLE1_END_TIME = 1695555555000L
+        const val CYCLE2_END_TIME = 1695566666000L
+        const val UID = 10000
+
+        const val ALL_USAGE = 10L
+        const val FOREGROUND_USAGE = 2L
+    }
+}
diff --git a/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppDataUsagePreferenceTest.kt b/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppDataUsagePreferenceTest.kt
index 019c143..48010e0 100644
--- a/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppDataUsagePreferenceTest.kt
+++ b/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppDataUsagePreferenceTest.kt
@@ -38,6 +38,7 @@
 import com.android.settings.Utils
 import com.android.settings.applications.appinfo.AppInfoDashboardFragment
 import com.android.settings.datausage.AppDataUsage
+import com.android.settings.datausage.lib.INetworkTemplates
 import com.android.settingslib.net.NetworkCycleDataForUid
 import com.android.settingslib.net.NetworkCycleDataForUidLoader
 import com.android.settingslib.spa.testutils.delay
@@ -106,7 +107,7 @@
 
         setContent(notInstalledApp)
 
-        composeTestRule.onNodeWithText(context.getString(R.string.data_usage_app_summary_title))
+        composeTestRule.onNodeWithText(context.getString(R.string.cellular_data_usage))
             .assertIsDisplayed()
             .assertIsNotEnabled()
     }
@@ -115,7 +116,7 @@
     fun whenAppInstalled_enabled() {
         setContent(APP)
 
-        composeTestRule.onNodeWithText(context.getString(R.string.data_usage_app_summary_title))
+        composeTestRule.onNodeWithText(context.getString(R.string.cellular_data_usage))
             .assertIsDisplayed()
             .assertIsEnabled()
     }
@@ -169,14 +170,19 @@
     private fun setContent(app: ApplicationInfo = APP) {
         composeTestRule.setContent {
             CompositionLocalProvider(LocalContext provides context) {
-                AppDataUsagePreference(app)
+                AppDataUsagePreference(app, TestNetworkTemplates)
             }
         }
         composeTestRule.delay()
     }
 
+    private object TestNetworkTemplates : INetworkTemplates {
+        override fun getDefaultTemplate(context: Context): NetworkTemplate =
+            NetworkTemplate.Builder(NetworkTemplate.MATCH_MOBILE).build()
+    }
+
     private companion object {
-        const val PACKAGE_NAME = "packageName"
+        const val PACKAGE_NAME = "package.name"
         const val UID = 123
         val APP = ApplicationInfo().apply {
             packageName = PACKAGE_NAME