Merge "[Settings] Query historical data in background" into rvc-dev
diff --git a/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java b/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
index b417c3d..4178f6c 100644
--- a/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
+++ b/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
@@ -47,8 +47,10 @@
 import com.android.settingslib.core.lifecycle.LifecycleObserver;
 import com.android.settingslib.core.lifecycle.events.OnStart;
 import com.android.settingslib.net.DataUsageController;
+import com.android.settingslib.utils.ThreadUtils;
 
 import java.util.List;
+import java.util.concurrent.Future;
 
 /**
  * This is the controller for a data usage header that retrieves carrier data from the new
@@ -99,6 +101,8 @@
 
     private Intent mManageSubscriptionIntent;
 
+    private Future<Long> mHistoricalUsageLevel;
+
     public DataUsageSummaryPreferenceController(Activity activity,
             Lifecycle lifecycle, PreferenceFragmentCompat fragment, int subscriptionId) {
         super(activity, KEY);
@@ -206,13 +210,13 @@
             updateConfiguration(mContext, mSubId, subInfo);
         }
 
+        mHistoricalUsageLevel = ThreadUtils.postOnBackgroundThread(() ->
+                mDataUsageController.getHistoricalUsageLevel(mDefaultTemplate));
+
         final DataUsageController.DataUsageInfo info =
                 mDataUsageController.getDataUsageInfo(mDefaultTemplate);
 
         long usageLevel = info.usageLevel;
-        if (usageLevel <= 0L) {
-            usageLevel = mDataUsageController.getHistoricalUsageLevel(mDefaultTemplate);
-        }
 
         if (subInfo != null) {
             mDataInfoController.updateDataLimit(info, mPolicyEditor.getPolicy(mDefaultTemplate));
@@ -222,7 +226,7 @@
             summaryPreference.setWifiMode(/* isWifiMode */ true, /* usagePeriod */
                     info.period, /* isSingleWifi */ false);
             summaryPreference.setLimitInfo(null);
-            summaryPreference.setUsageNumbers(usageLevel,
+            summaryPreference.setUsageNumbers(displayUsageLevel(usageLevel),
                     /* dataPlanSize */ -1L,
                     /* hasMobileData */ true);
             summaryPreference.setChartEnabled(false);
@@ -235,11 +239,6 @@
         }
 
         refreshDataplanInfo(info, subInfo);
-        if ((mDataplanUse <= 0L) && (mSnapshotTime < 0)) {
-            Log.d(TAG, "Display data usage from history");
-            mDataplanUse = usageLevel;
-            mSnapshotTime = -1L;
-        }
 
         if (info.warningLevel > 0 && info.limitLevel > 0) {
             summaryPreference.setLimitInfo(TextUtils.expandTemplate(
@@ -258,6 +257,12 @@
             summaryPreference.setLimitInfo(null);
         }
 
+        if ((mDataplanUse <= 0L) && (mSnapshotTime < 0)) {
+            Log.d(TAG, "Display data usage from history");
+            mDataplanUse = displayUsageLevel(usageLevel);
+            mSnapshotTime = -1L;
+        }
+
         summaryPreference.setUsageNumbers(mDataplanUse, mDataplanSize, mHasMobileData);
 
         if (mDataBarSize <= 0) {
@@ -272,6 +277,17 @@
                 mDataplanCount, mManageSubscriptionIntent);
     }
 
+    private long displayUsageLevel(long usageLevel) {
+        if (usageLevel > 0) {
+            return usageLevel;
+        }
+        try {
+            usageLevel = mHistoricalUsageLevel.get();
+        } catch (Exception ex) {
+        }
+        return usageLevel;
+    }
+
     // TODO(b/70950124) add test for this method once the robolectric shadow run script is
     // completed (b/3526807)
     private void refreshDataplanInfo(DataUsageController.DataUsageInfo info,
diff --git a/src/com/android/settings/network/telephony/DataUsagePreferenceController.java b/src/com/android/settings/network/telephony/DataUsagePreferenceController.java
index 035a8c1..08524d6 100644
--- a/src/com/android/settings/network/telephony/DataUsagePreferenceController.java
+++ b/src/com/android/settings/network/telephony/DataUsagePreferenceController.java
@@ -45,6 +45,7 @@
 
     private Future<NetworkTemplate> mTemplateFuture;
     private AtomicReference<NetworkTemplate> mTemplate;
+    private Future<Long> mHistoricalUsageLevel;
 
     public DataUsagePreferenceController(Context context, String key) {
         super(context, key);
@@ -127,11 +128,17 @@
         final DataUsageController controller = new DataUsageController(context);
         controller.setSubscriptionId(subId);
 
+        mHistoricalUsageLevel = ThreadUtils.postOnBackgroundThread(() ->
+                controller.getHistoricalUsageLevel(getNetworkTemplate()));
+
         final DataUsageController.DataUsageInfo usageInfo = getDataUsageInfo(controller);
 
         long usageLevel = usageInfo.usageLevel;
         if (usageLevel <= 0L) {
-            usageLevel = controller.getHistoricalUsageLevel(getNetworkTemplate());
+            try {
+                usageLevel = mHistoricalUsageLevel.get();
+            } catch (Exception exception) {
+            }
         }
         if (usageLevel <= 0L) {
             return null;