Merge "Use the date format to show the system update info" into qt-r1-dev
am: a076039803

Change-Id: I12eab14b3a406348d13436df5843727f0dccf71d
diff --git a/src/com/android/settings/deviceinfo/firmwareversion/MainlineModuleVersionPreferenceController.java b/src/com/android/settings/deviceinfo/firmwareversion/MainlineModuleVersionPreferenceController.java
index ff9352a..fd54f19 100644
--- a/src/com/android/settings/deviceinfo/firmwareversion/MainlineModuleVersionPreferenceController.java
+++ b/src/com/android/settings/deviceinfo/firmwareversion/MainlineModuleVersionPreferenceController.java
@@ -21,6 +21,7 @@
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.text.TextUtils;
+import android.text.format.DateFormat;
 import android.util.Log;
 
 import androidx.annotation.VisibleForTesting;
@@ -28,9 +29,20 @@
 
 import com.android.settings.core.BasePreferenceController;
 
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+import java.util.Optional;
+import java.util.TimeZone;
+
 public class MainlineModuleVersionPreferenceController extends BasePreferenceController {
 
     private static final String TAG = "MainlineModuleControl";
+    private static final List<String> VERSION_NAME_DATE_PATTERNS = Arrays.asList("yyyy-MM-dd",
+            "yyyy-MM");
 
     @VisibleForTesting
     static final Intent MODULE_UPDATE_INTENT =
@@ -81,6 +93,30 @@
 
     @Override
     public CharSequence getSummary() {
-        return mModuleVersion;
+        if (TextUtils.isEmpty(mModuleVersion)) {
+            return mModuleVersion;
+        }
+
+        final Optional<Date> parsedDate = parseDateFromVersionName(mModuleVersion);
+        if (!parsedDate.isPresent()) {
+            Log.w("Could not parse mainline versionName (%s) as date.", mModuleVersion);
+            return mModuleVersion;
+        }
+
+        return DateFormat.getLongDateFormat(mContext).format(parsedDate.get());
+    }
+
+    private Optional<Date> parseDateFromVersionName(String text) {
+        for (String pattern : VERSION_NAME_DATE_PATTERNS) {
+            try {
+                final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern,
+                        Locale.getDefault());
+                simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
+                return Optional.of(simpleDateFormat.parse(text));
+            } catch (ParseException e) {
+                // ignore and try next pattern
+            }
+        }
+        return Optional.empty();
     }
 }
diff --git a/tests/robotests/src/com/android/settings/deviceinfo/firmwareversion/MainlineModuleVersionPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/firmwareversion/MainlineModuleVersionPreferenceControllerTest.java
index adddc96..950d962 100644
--- a/tests/robotests/src/com/android/settings/deviceinfo/firmwareversion/MainlineModuleVersionPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/deviceinfo/firmwareversion/MainlineModuleVersionPreferenceControllerTest.java
@@ -31,12 +31,9 @@
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
-import android.util.FeatureFlagUtils;
 
 import androidx.preference.Preference;
 
-import com.android.settings.core.FeatureFlags;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -91,7 +88,7 @@
 
     @Test
     public void getAvailabilityStatus_hasMainlineModulePackageInfo_available() throws Exception {
-        setupModulePackage();
+        setupModulePackage("test version 123");
 
         final MainlineModuleVersionPreferenceController controller =
                 new MainlineModuleVersionPreferenceController(mContext, "key");
@@ -101,7 +98,7 @@
 
     @Test
     public void updateStates_canHandleIntent_setIntentToPreference() throws Exception {
-        setupModulePackage();
+        setupModulePackage("test version 123");
         when(mPackageManager.resolveActivity(MODULE_UPDATE_INTENT, 0))
                 .thenReturn(new ResolveInfo());
 
@@ -115,7 +112,7 @@
 
     @Test
     public void updateStates_cannotHandleIntent_setNullToPreference() throws Exception {
-        setupModulePackage();
+        setupModulePackage("test version 123");
         when(mPackageManager.resolveActivity(MODULE_UPDATE_INTENT, 0))
                 .thenReturn(null);
 
@@ -127,9 +124,38 @@
         assertThat(mPreference.getIntent()).isNull();
     }
 
-    private void setupModulePackage() throws Exception {
+    @Test
+    public void getSummary_versionIsNull_returnNull() throws Exception {
+        setupModulePackage(null);
+
+        final MainlineModuleVersionPreferenceController controller =
+                new MainlineModuleVersionPreferenceController(mContext, "key");
+
+        assertThat(controller.getSummary()).isNull();
+    }
+
+    @Test
+    public void getSummary_versionIsMonth_returnMonth() throws Exception {
+        setupModulePackage("2019-05");
+
+        final MainlineModuleVersionPreferenceController controller =
+                new MainlineModuleVersionPreferenceController(mContext, "key");
+
+        assertThat(controller.getSummary()).isEqualTo("May 01, 2019");
+    }
+
+    @Test
+    public void getSummary_versionIsDate_returnDate() throws Exception {
+        setupModulePackage("2019-05-13");
+
+        final MainlineModuleVersionPreferenceController controller =
+                new MainlineModuleVersionPreferenceController(mContext, "key");
+
+        assertThat(controller.getSummary()).isEqualTo("May 13, 2019");
+    }
+
+    private void setupModulePackage(String version) throws Exception {
         final String provider = "test.provider";
-        final String version = "test version 123";
         final PackageInfo info = new PackageInfo();
         info.versionName = version;
         when(mContext.getString(