Merge "Fixes fail test cases in AccessibilitySettingsTest" into udc-dev
diff --git a/src/com/android/settings/datausage/BillingCycleSettings.java b/src/com/android/settings/datausage/BillingCycleSettings.java
index 395d1c7..3047d73 100644
--- a/src/com/android/settings/datausage/BillingCycleSettings.java
+++ b/src/com/android/settings/datausage/BillingCycleSettings.java
@@ -27,6 +27,7 @@
 import android.net.NetworkPolicy;
 import android.net.NetworkTemplate;
 import android.os.Bundle;
+import android.provider.Settings;
 import android.text.method.NumberKeyListener;
 import android.util.Log;
 import android.view.LayoutInflater;
@@ -54,6 +55,7 @@
 
 import java.text.NumberFormat;
 import java.text.ParseException;
+import java.util.Optional;
 import java.util.TimeZone;
 
 @SearchIndexable
@@ -115,6 +117,18 @@
 
         Bundle args = getArguments();
         mNetworkTemplate = args.getParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE);
+        if (mNetworkTemplate == null && getIntent() != null) {
+            mNetworkTemplate = getIntent().getParcelableExtra(Settings.EXTRA_NETWORK_TEMPLATE);
+        }
+
+        if (mNetworkTemplate == null) {
+            Optional<NetworkTemplate> mobileNetworkTemplateFromSim =
+                    DataUsageUtils.getMobileNetworkTemplateFromSubId(context, getIntent());
+            if (mobileNetworkTemplateFromSim.isPresent()) {
+                mNetworkTemplate = mobileNetworkTemplateFromSim.get();
+            }
+        }
+
         if (mNetworkTemplate == null) {
             mNetworkTemplate = DataUsageUtils.getDefaultTemplate(context,
                 DataUsageUtils.getDefaultSubscriptionId(context));
diff --git a/src/com/android/settings/datausage/DataUsageList.java b/src/com/android/settings/datausage/DataUsageList.java
index 1499ded..68751e5 100644
--- a/src/com/android/settings/datausage/DataUsageList.java
+++ b/src/com/android/settings/datausage/DataUsageList.java
@@ -69,6 +69,7 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 
 /**
  * Panel showing data usage history across various networks, including options
@@ -266,6 +267,14 @@
             mSubId = intent.getIntExtra(Settings.EXTRA_SUB_ID,
                     SubscriptionManager.INVALID_SUBSCRIPTION_ID);
             mTemplate = intent.getParcelableExtra(Settings.EXTRA_NETWORK_TEMPLATE);
+
+            if (mTemplate == null) {
+                Optional<NetworkTemplate> mobileNetworkTemplateFromSim =
+                        DataUsageUtils.getMobileNetworkTemplateFromSubId(getContext(), getIntent());
+                if (mobileNetworkTemplateFromSim.isPresent()) {
+                    mTemplate = mobileNetworkTemplateFromSim.get();
+                }
+            }
         }
     }
 
diff --git a/src/com/android/settings/datausage/DataUsageUtils.java b/src/com/android/settings/datausage/DataUsageUtils.java
index 9fec27e..cce7ca1 100644
--- a/src/com/android/settings/datausage/DataUsageUtils.java
+++ b/src/com/android/settings/datausage/DataUsageUtils.java
@@ -22,11 +22,13 @@
 import android.app.usage.NetworkStats.Bucket;
 import android.app.usage.NetworkStatsManager;
 import android.content.Context;
+import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.net.ConnectivityManager;
 import android.net.NetworkTemplate;
 import android.os.RemoteException;
 import android.os.SystemProperties;
+import android.provider.Settings;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
@@ -39,6 +41,7 @@
 import com.android.settings.network.ProxySubscriptionManager;
 
 import java.util.List;
+import java.util.Optional;
 
 /**
  * Utility methods for data usage classes.
@@ -193,4 +196,22 @@
         }
     }
 
+    /**
+     * Returns a mobile NetworkTemplate if EXTRA_SUB_ID of the Intent is available and the subId
+     * is valid & hasMobileData. Otherwise, returns empty data.
+     */
+    public static Optional<NetworkTemplate> getMobileNetworkTemplateFromSubId(Context context,
+            Intent intent) {
+        if (intent == null || !intent.hasExtra(Settings.EXTRA_SUB_ID)) {
+            return Optional.empty();
+        }
+
+        int subId = intent.getIntExtra(Settings.EXTRA_SUB_ID,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        if (SubscriptionManager.isValidSubscriptionId(subId) && hasMobileData(context)) {
+            return Optional.of(DataUsageLib.getMobileTemplate(context, subId));
+        }
+
+        return  Optional.empty();
+    }
 }