Use the date format to show the system update info
Use the "July 13, 2019" or similar to display if the value is a valid date data. Bug: 137089104 Test: visual test & robotest Change-Id: Ie4bab2617c1cd6fd956bf6d1a22ce96e6b0b58d0
This commit is contained in:
@@ -21,6 +21,7 @@ import android.content.Intent;
|
|||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.text.format.DateFormat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
@@ -28,9 +29,20 @@ import androidx.preference.Preference;
|
|||||||
|
|
||||||
import com.android.settings.core.BasePreferenceController;
|
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 {
|
public class MainlineModuleVersionPreferenceController extends BasePreferenceController {
|
||||||
|
|
||||||
private static final String TAG = "MainlineModuleControl";
|
private static final String TAG = "MainlineModuleControl";
|
||||||
|
private static final List<String> VERSION_NAME_DATE_PATTERNS = Arrays.asList("yyyy-MM-dd",
|
||||||
|
"yyyy-MM");
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final Intent MODULE_UPDATE_INTENT =
|
static final Intent MODULE_UPDATE_INTENT =
|
||||||
@@ -81,6 +93,30 @@ public class MainlineModuleVersionPreferenceController extends BasePreferenceCon
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CharSequence getSummary() {
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -31,12 +31,9 @@ import android.content.Context;
|
|||||||
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
import android.util.FeatureFlagUtils;
|
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
|
|
||||||
import com.android.settings.core.FeatureFlags;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -91,7 +88,7 @@ public class MainlineModuleVersionPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAvailabilityStatus_hasMainlineModulePackageInfo_available() throws Exception {
|
public void getAvailabilityStatus_hasMainlineModulePackageInfo_available() throws Exception {
|
||||||
setupModulePackage();
|
setupModulePackage("test version 123");
|
||||||
|
|
||||||
final MainlineModuleVersionPreferenceController controller =
|
final MainlineModuleVersionPreferenceController controller =
|
||||||
new MainlineModuleVersionPreferenceController(mContext, "key");
|
new MainlineModuleVersionPreferenceController(mContext, "key");
|
||||||
@@ -101,7 +98,7 @@ public class MainlineModuleVersionPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateStates_canHandleIntent_setIntentToPreference() throws Exception {
|
public void updateStates_canHandleIntent_setIntentToPreference() throws Exception {
|
||||||
setupModulePackage();
|
setupModulePackage("test version 123");
|
||||||
when(mPackageManager.resolveActivity(MODULE_UPDATE_INTENT, 0))
|
when(mPackageManager.resolveActivity(MODULE_UPDATE_INTENT, 0))
|
||||||
.thenReturn(new ResolveInfo());
|
.thenReturn(new ResolveInfo());
|
||||||
|
|
||||||
@@ -115,7 +112,7 @@ public class MainlineModuleVersionPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateStates_cannotHandleIntent_setNullToPreference() throws Exception {
|
public void updateStates_cannotHandleIntent_setNullToPreference() throws Exception {
|
||||||
setupModulePackage();
|
setupModulePackage("test version 123");
|
||||||
when(mPackageManager.resolveActivity(MODULE_UPDATE_INTENT, 0))
|
when(mPackageManager.resolveActivity(MODULE_UPDATE_INTENT, 0))
|
||||||
.thenReturn(null);
|
.thenReturn(null);
|
||||||
|
|
||||||
@@ -127,9 +124,38 @@ public class MainlineModuleVersionPreferenceControllerTest {
|
|||||||
assertThat(mPreference.getIntent()).isNull();
|
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 provider = "test.provider";
|
||||||
final String version = "test version 123";
|
|
||||||
final PackageInfo info = new PackageInfo();
|
final PackageInfo info = new PackageInfo();
|
||||||
info.versionName = version;
|
info.versionName = version;
|
||||||
when(mContext.getString(
|
when(mContext.getString(
|
||||||
|
Reference in New Issue
Block a user