Use SystemUpdateManager to provide system update info.

Change-Id: I38b631cda8aa63e478c74625eba3d18f9a5723bd
Fixes: 36874007
Test: robotests
diff --git a/res/values/strings.xml b/res/values/strings.xml
index f86cc56..93b1bef 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -8343,8 +8343,10 @@
     <!-- Backup disabled summary [CHAR LIMIT=NONE] -->
     <string name="backup_disabled">Back up disabled</string>
 
-    <!-- Summary of device info page [CHAR LIMIT=NONE] -->
-    <string name="about_summary">Updated to Android <xliff:g id="version" example="6.0">%1$s</xliff:g></string>
+    <!-- Summary of Android version info [CHAR LIMIT=NONE] -->
+    <string name="android_version_summary">Updated to Android <xliff:g id="version" example="6.0">%1$s</xliff:g></string>
+    <!-- Summary of Android version info (when there is a pending upgrade available) [CHAR LIMIT=NONE] -->
+    <string name="android_version_pending_update_summary">Update available</string>
 
     <!-- Title for dialog displayed when user clicks on a setting locked by an admin [CHAR LIMIT=30] -->
     <string name="disabled_by_policy_title">Action not allowed</string>
diff --git a/res/xml/system_dashboard_fragment.xml b/res/xml/system_dashboard_fragment.xml
index 73aab4b..d8459dd 100644
--- a/res/xml/system_dashboard_fragment.xml
+++ b/res/xml/system_dashboard_fragment.xml
@@ -55,7 +55,7 @@
         android:summary="@string/summary_placeholder"
         android:icon="@drawable/ic_system_update"
         android:order="-30"
-        settings:controller="com.android.settings.deviceinfo.SystemUpdatePreferenceController">
+        settings:controller="com.android.settings.system.SystemUpdatePreferenceController">
         <intent android:action="android.settings.SYSTEM_UPDATE_SETTINGS" />
     </Preference>
 
@@ -63,7 +63,7 @@
         android:key="additional_system_update_settings"
         android:title="@string/additional_system_update_settings_list_item_title"
         android:order="-31"
-        settings:controller="com.android.settings.deviceinfo.AdditionalSystemUpdatePreferenceController">
+        settings:controller="com.android.settings.system.AdditionalSystemUpdatePreferenceController">
         <intent android:action="android.intent.action.MAIN"
                 android:targetPackage="@string/additional_system_update"
                 android:targetClass="@string/additional_system_update_menu" />
diff --git a/src/com/android/settings/deviceinfo/AdditionalSystemUpdatePreferenceController.java b/src/com/android/settings/system/AdditionalSystemUpdatePreferenceController.java
similarity index 88%
rename from src/com/android/settings/deviceinfo/AdditionalSystemUpdatePreferenceController.java
rename to src/com/android/settings/system/AdditionalSystemUpdatePreferenceController.java
index f91ed4e..1fbf835 100644
--- a/src/com/android/settings/deviceinfo/AdditionalSystemUpdatePreferenceController.java
+++ b/src/com/android/settings/system/AdditionalSystemUpdatePreferenceController.java
@@ -13,13 +13,11 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.android.settings.deviceinfo;
+package com.android.settings.system;
 
 import android.content.Context;
 
 import com.android.settings.core.BasePreferenceController;
-import com.android.settings.core.PreferenceControllerMixin;
-import com.android.settingslib.core.AbstractPreferenceController;
 
 public class AdditionalSystemUpdatePreferenceController extends BasePreferenceController {
 
diff --git a/src/com/android/settings/system/SystemDashboardFragment.java b/src/com/android/settings/system/SystemDashboardFragment.java
index 88cafb0..deabf54 100644
--- a/src/com/android/settings/system/SystemDashboardFragment.java
+++ b/src/com/android/settings/system/SystemDashboardFragment.java
@@ -26,8 +26,6 @@
 import com.android.settings.R;
 import com.android.settings.backup.BackupSettingsActivityPreferenceController;
 import com.android.settings.dashboard.DashboardFragment;
-import com.android.settings.deviceinfo.AdditionalSystemUpdatePreferenceController;
-import com.android.settings.deviceinfo.SystemUpdatePreferenceController;
 import com.android.settings.gestures.GesturesSettingPreferenceController;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Indexable;
diff --git a/src/com/android/settings/deviceinfo/SystemUpdatePreferenceController.java b/src/com/android/settings/system/SystemUpdatePreferenceController.java
similarity index 73%
rename from src/com/android/settings/deviceinfo/SystemUpdatePreferenceController.java
rename to src/com/android/settings/system/SystemUpdatePreferenceController.java
index 2806275..20f43ef 100644
--- a/src/com/android/settings/deviceinfo/SystemUpdatePreferenceController.java
+++ b/src/com/android/settings/system/SystemUpdatePreferenceController.java
@@ -13,14 +13,17 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.android.settings.deviceinfo;
+package com.android.settings.system;
 
 import static android.content.Context.CARRIER_CONFIG_SERVICE;
+import static android.content.Context.SYSTEM_UPDATE_SERVICE;
 
 import android.content.Context;
 import android.content.Intent;
 import android.os.Build;
+import android.os.Bundle;
 import android.os.PersistableBundle;
+import android.os.SystemUpdateManager;
 import android.os.UserManager;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceScreen;
@@ -39,10 +42,12 @@
     private static final String KEY_SYSTEM_UPDATE_SETTINGS = "system_update_settings";
 
     private final UserManager mUm;
+    private final SystemUpdateManager mUpdateManager;
 
     public SystemUpdatePreferenceController(Context context) {
         super(context, KEY_SYSTEM_UPDATE_SETTINGS);
         mUm = UserManager.get(context);
+        mUpdateManager = (SystemUpdateManager) context.getSystemService(SYSTEM_UPDATE_SERVICE);
     }
 
     @Override
@@ -84,7 +89,27 @@
 
     @Override
     public String getSummary() {
-        return mContext.getString(R.string.about_summary, Build.VERSION.RELEASE);
+        final Bundle updateInfo = mUpdateManager.retrieveSystemUpdateInfo();
+        String summary = mContext.getString(R.string.android_version_summary,
+                Build.VERSION.RELEASE);
+        switch (updateInfo.getInt(SystemUpdateManager.KEY_STATUS)) {
+            case SystemUpdateManager.STATUS_WAITING_DOWNLOAD:
+            case SystemUpdateManager.STATUS_IN_PROGRESS:
+            case SystemUpdateManager.STATUS_WAITING_INSTALL:
+            case SystemUpdateManager.STATUS_WAITING_REBOOT:
+                summary = mContext.getString(R.string.android_version_pending_update_summary);
+                break;
+            case SystemUpdateManager.STATUS_UNKNOWN:
+                Log.d(TAG, "Update statue unknown");
+                // fall through to next branch
+            case SystemUpdateManager.STATUS_IDLE:
+                final String version = updateInfo.getString(SystemUpdateManager.KEY_TITLE);
+                if (!TextUtils.isEmpty(version)) {
+                    summary = mContext.getString(R.string.android_version_summary, version);
+                }
+                break;
+        }
+        return summary;
     }
 
     /**
diff --git a/tests/robotests/src/android/os/SystemUpdateManager.java b/tests/robotests/src/android/os/SystemUpdateManager.java
new file mode 100644
index 0000000..f81df36
--- /dev/null
+++ b/tests/robotests/src/android/os/SystemUpdateManager.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2018 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 android.os;
+
+/**
+ * Duplicate class for platform SystemUpdateManager to get around Robolectric sdk problem.
+ */
+public class SystemUpdateManager {
+
+    public static final String KEY_STATUS = "status";
+    public static final String KEY_TITLE = "title";
+
+    public static final int STATUS_UNKNOWN = 0;
+    public static final int STATUS_IDLE = 1;
+    public static final int STATUS_WAITING_DOWNLOAD = 2;
+    public static final int STATUS_IN_PROGRESS = 3;
+    public static final int STATUS_WAITING_INSTALL = 4;
+    public static final int STATUS_WAITING_REBOOT = 5;
+
+    public Bundle retrieveSystemUpdateInfo() {
+        return null;
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/deviceinfo/AdditionalSystemUpdatePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/system/AdditionalSystemUpdatePreferenceControllerTest.java
similarity index 94%
rename from tests/robotests/src/com/android/settings/deviceinfo/AdditionalSystemUpdatePreferenceControllerTest.java
rename to tests/robotests/src/com/android/settings/system/AdditionalSystemUpdatePreferenceControllerTest.java
index e5708ba..43f48c0 100644
--- a/tests/robotests/src/com/android/settings/deviceinfo/AdditionalSystemUpdatePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/system/AdditionalSystemUpdatePreferenceControllerTest.java
@@ -13,20 +13,18 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.android.settings.deviceinfo;
+package com.android.settings.system;
 
 import static com.google.common.truth.Truth.assertThat;
 
 import android.content.Context;
 
-import com.android.settings.R;
 import com.android.settings.TestConfig;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
diff --git a/tests/robotests/src/com/android/settings/deviceinfo/SystemUpdatePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/system/SystemUpdatePreferenceControllerTest.java
similarity index 64%
rename from tests/robotests/src/com/android/settings/deviceinfo/SystemUpdatePreferenceControllerTest.java
rename to tests/robotests/src/com/android/settings/system/SystemUpdatePreferenceControllerTest.java
index b5b84da..95a18a1 100644
--- a/tests/robotests/src/com/android/settings/deviceinfo/SystemUpdatePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/system/SystemUpdatePreferenceControllerTest.java
@@ -13,17 +13,20 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.android.settings.deviceinfo;
+package com.android.settings.system;
 
+import static android.os.SystemUpdateManager.KEY_STATUS;
+import static android.os.SystemUpdateManager.KEY_TITLE;
+import static android.os.SystemUpdateManager.STATUS_IDLE;
+import static android.os.SystemUpdateManager.STATUS_UNKNOWN;
+import static android.os.SystemUpdateManager.STATUS_WAITING_DOWNLOAD;
 import static com.google.common.truth.Truth.assertThat;
-
-import static org.mockito.Answers.RETURNS_DEEP_STUBS;
-import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 
 import android.content.Context;
 import android.os.Build;
-import android.os.UserManager;
+import android.os.Bundle;
+import android.os.SystemUpdateManager;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceScreen;
 
@@ -55,6 +58,8 @@
 
     @Mock
     private PreferenceScreen mScreen;
+    @Mock
+    private SystemUpdateManager mSystemUpdateManager;
 
     private Context mContext;
     private SystemUpdatePreferenceController mController;
@@ -64,7 +69,8 @@
     public void setUp() {
         MockitoAnnotations.initMocks(this);
         mContext = RuntimeEnvironment.application;
-
+        ShadowApplication.getInstance().setSystemService(Context.SYSTEM_UPDATE_SERVICE,
+                mSystemUpdateManager);
         mController = new SystemUpdatePreferenceController(mContext);
         mPreference = new Preference(RuntimeEnvironment.application);
         mPreference.setKey(mController.getPreferenceKey());
@@ -118,11 +124,41 @@
     }
 
     @Test
-    public void updateState_shouldSetToAndroidVersion() {
+    public void updateState_systemUpdateStatusUnknown_shouldSetToAndroidVersion() {
+        final Bundle bundle = new Bundle();
+        bundle.putInt(KEY_STATUS, STATUS_UNKNOWN);
+        when(mSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle);
+
         mController.updateState(mPreference);
 
-        assertThat(mPreference.getSummary())
-                .isEqualTo(RuntimeEnvironment.application.getString(R.string.about_summary,
-                        Build.VERSION.RELEASE));
+        assertThat(mPreference.getSummary()).isEqualTo(
+                mContext.getString(R.string.android_version_summary, Build.VERSION.RELEASE));
+    }
+
+    @Test
+    public void updateState_systemUpdateStatusIdle_shouldSetToAndroidVersion() {
+        final String testReleaseName = "ANDROID TEST VERSION";
+
+        final Bundle bundle = new Bundle();
+        bundle.putInt(KEY_STATUS, STATUS_IDLE);
+        bundle.putString(KEY_TITLE, testReleaseName);
+        when(mSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.getSummary()).isEqualTo(
+                mContext.getString(R.string.android_version_summary, testReleaseName));
+    }
+
+    @Test
+    public void updateState_systemUpdateInProgress_shouldSetToUpdatePending() {
+        final Bundle bundle = new Bundle();
+        bundle.putInt(KEY_STATUS, STATUS_WAITING_DOWNLOAD);
+        when(mSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.getSummary()).isEqualTo(
+                mContext.getString(R.string.android_version_pending_update_summary));
     }
 }
\ No newline at end of file