Merge "[Settings] Move some work into background thread" into rvc-dev
diff --git a/src/com/android/settings/dashboard/DashboardFragment.java b/src/com/android/settings/dashboard/DashboardFragment.java
index e4041fb..8084038 100644
--- a/src/com/android/settings/dashboard/DashboardFragment.java
+++ b/src/com/android/settings/dashboard/DashboardFragment.java
@@ -320,6 +320,14 @@
         addPreferencesFromResource(resId);
         final PreferenceScreen screen = getPreferenceScreen();
         screen.setOnExpandButtonClickListener(this);
+        displayResourceTilesToScreen(screen);
+    }
+
+    /**
+     * Perform {@link AbstractPreferenceController#displayPreference(PreferenceScreen)}
+     * on all {@link AbstractPreferenceController}s.
+     */
+    protected void displayResourceTilesToScreen(PreferenceScreen screen) {
         mPreferenceControllers.values().stream().flatMap(Collection::stream).forEach(
                 controller -> controller.displayPreference(screen));
     }
diff --git a/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java b/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
index c0ee0a5..135abfa 100644
--- a/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
+++ b/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
@@ -64,7 +64,7 @@
     private static final float RELATIVE_SIZE_LARGE = 1.25f * 1.25f;  // (1/0.8)^2
     private static final float RELATIVE_SIZE_SMALL = 1.0f / RELATIVE_SIZE_LARGE;  // 0.8^2
 
-    private final EntityHeaderController mEntityHeaderController;
+    private EntityHeaderController mEntityHeaderController;
     private final Lifecycle mLifecycle;
     private final PreferenceFragmentCompat mFragment;
     protected DataUsageController mDataUsageController;
@@ -103,8 +103,6 @@
             Lifecycle lifecycle, PreferenceFragmentCompat fragment, int subscriptionId) {
         super(activity, KEY);
 
-        mEntityHeaderController = EntityHeaderController.newInstance(activity,
-                fragment, null);
         mLifecycle = lifecycle;
         mFragment = fragment;
         init(subscriptionId);
@@ -165,6 +163,10 @@
 
     @Override
     public void onStart() {
+        if (mEntityHeaderController == null) {
+            mEntityHeaderController =
+                    EntityHeaderController.newInstance((Activity) mContext, mFragment, null);
+        }
         RecyclerView view = mFragment.getListView();
         mEntityHeaderController.setRecyclerView(view, mLifecycle);
         mEntityHeaderController.styleActionBar((Activity) mContext);
@@ -197,16 +199,20 @@
     public void updateState(Preference preference) {
         DataUsageSummaryPreference summaryPreference = (DataUsageSummaryPreference) preference;
 
-        final DataUsageController.DataUsageInfo info;
+        final boolean isSimCardAdded = hasSim();
+        if (!isSimCardAdded) {
+            mDefaultTemplate = NetworkTemplate.buildTemplateWifiWildcard();
+        }
+
+        final DataUsageController.DataUsageInfo info =
+                mDataUsageController.getDataUsageInfo(mDefaultTemplate);
+
         final SubscriptionInfo subInfo = getSubscriptionInfo(mSubId);
-        if (hasSim()) {
-            info = mDataUsageController.getDataUsageInfo(mDefaultTemplate);
+        if (isSimCardAdded) {
             mDataInfoController.updateDataLimit(info, mPolicyEditor.getPolicy(mDefaultTemplate));
             summaryPreference.setWifiMode(/* isWifiMode */ false,
                     /* usagePeriod */ null, /* isSingleWifi */ false);
         } else {
-            info = mDataUsageController.getDataUsageInfo(
-                    NetworkTemplate.buildTemplateWifiWildcard());
             summaryPreference.setWifiMode(/* isWifiMode */ true, /* usagePeriod */
                     info.period, /* isSingleWifi */ false);
             summaryPreference.setLimitInfo(null);
diff --git a/tests/robotests/src/com/android/settings/display/NightDisplayAutoModePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/NightDisplayAutoModePreferenceControllerTest.java
index d6500dbb..7c046f8 100644
--- a/tests/robotests/src/com/android/settings/display/NightDisplayAutoModePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/NightDisplayAutoModePreferenceControllerTest.java
@@ -16,16 +16,20 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+
 import android.content.Context;
 import android.hardware.display.ColorDisplayManager;
+import android.location.LocationManager;
 
 import com.android.settings.testutils.shadow.SettingsShadowResources;
 
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Mockito;
 import org.robolectric.RobolectricTestRunner;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
@@ -36,10 +40,14 @@
 
     private Context mContext;
     private NightDisplayAutoModePreferenceController mController;
+    private LocationManager mLocationManager;
 
     @Before
     public void setUp() {
-        mContext = RuntimeEnvironment.application;
+        mContext = Mockito.spy(RuntimeEnvironment.application);
+        mLocationManager = Mockito.mock(LocationManager.class);
+        when(mLocationManager.isLocationEnabled()).thenReturn(true);
+        when(mContext.getSystemService(eq(LocationManager.class))).thenReturn(mLocationManager);
         mController = new NightDisplayAutoModePreferenceController(mContext,
             "night_display_auto_mode");
     }
@@ -64,7 +72,6 @@
     }
 
     @Test
-    @Ignore
     public void onPreferenceChange_changesAutoMode() {
         mController.onPreferenceChange(null,
                 String.valueOf(ColorDisplayManager.AUTO_MODE_TWILIGHT));
diff --git a/tests/robotests/src/com/android/settings/display/darkmode/DarkModeScheduleSelectorControllerTest.java b/tests/robotests/src/com/android/settings/display/darkmode/DarkModeScheduleSelectorControllerTest.java
index 92ecdc8..1bbbe7d 100644
--- a/tests/robotests/src/com/android/settings/display/darkmode/DarkModeScheduleSelectorControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/darkmode/DarkModeScheduleSelectorControllerTest.java
@@ -37,7 +37,6 @@
 import com.android.settings.R;
 
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
@@ -103,16 +102,6 @@
     }
 
     @Test
-    @Ignore
-    public void nightMode_selectNightMode_locationOff() {
-        when(mLocationManager.isLocationEnabled()).thenReturn(false);
-        mController.onPreferenceChange(mPreference,
-                mContext.getString(R.string.dark_ui_auto_mode_never));
-        assertFalse(mController.onPreferenceChange(mPreference,
-                mContext.getString(R.string.dark_ui_auto_mode_auto)));
-    }
-
-    @Test
     public void nightMode_updateStateNone_dropDownValueChangedToAuto() {
         when(mUiService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_AUTO);
         mController.displayPreference(mScreen);