Merge "Track RulesManagerService frameworks/base changes"
am: 7d269cb555

Change-Id: I7022bd9366477da55558bd174ac41773a876d6bf
diff --git a/distro/installer/src/main/com/android/timezone/distro/installer/TimeZoneDistroInstaller.java b/distro/installer/src/main/com/android/timezone/distro/installer/TimeZoneDistroInstaller.java
index bc7f87e..6c33bd7 100644
--- a/distro/installer/src/main/com/android/timezone/distro/installer/TimeZoneDistroInstaller.java
+++ b/distro/installer/src/main/com/android/timezone/distro/installer/TimeZoneDistroInstaller.java
@@ -112,15 +112,15 @@
     public static final String UNINSTALL_TOMBSTONE_FILE_NAME = "STAGED_UNINSTALL_TOMBSTONE";
 
     private final String logTag;
-    private final File systemTzDataFile;
+    private final File baseVersionFile;
     private final File oldStagedDataDir;
     private final File stagedTzDataDir;
     private final File currentTzDataDir;
     private final File workingDir;
 
-    public TimeZoneDistroInstaller(String logTag, File systemTzDataFile, File installDir) {
+    public TimeZoneDistroInstaller(String logTag, File baseVersionFile, File installDir) {
         this.logTag = logTag;
-        this.systemTzDataFile = systemTzDataFile;
+        this.baseVersionFile = baseVersionFile;
         oldStagedDataDir = new File(installDir, OLD_TZ_DATA_DIR_NAME);
         stagedTzDataDir = new File(installDir, STAGED_TZ_DATA_DIR_NAME);
         currentTzDataDir = new File(installDir, CURRENT_TZ_DATA_DIR_NAME);
@@ -177,6 +177,9 @@
                 Slog.i(logTag, "Update not applied: Distro version could not be loaded");
                 return INSTALL_FAIL_BAD_DISTRO_STRUCTURE;
             }
+
+            // The TzDataSetVersion class replaces the DistroVersion class after P. Convert to the
+            // new class so we can use the isCompatibleWithThisDevice() method.
             TzDataSetVersion distroTzDataSetVersion;
             try {
                 distroTzDataSetVersion = new TzDataSetVersion(
@@ -197,7 +200,7 @@
                 return INSTALL_FAIL_BAD_DISTRO_STRUCTURE;
             }
 
-            if (!checkDistroRulesNewerThanSystem(systemTzDataFile, distroVersion)) {
+            if (!checkDistroRulesNewerThanBase(baseVersionFile, distroVersion)) {
                 Slog.i(logTag, "Update not applied: Distro rules version check failed");
                 return INSTALL_FAIL_RULES_TOO_OLD;
             }
@@ -262,7 +265,7 @@
 
     /**
      * Stage an uninstall of the current timezone update in /data which, on reboot, will return the
-     * device to using data from /system. If there was something else already staged it will be
+     * device to using the base data. If there was something else already staged it will be
      * removed by this call.
      *
      * Returns {@link #UNINSTALL_SUCCESS} if staging the uninstallation was
@@ -350,13 +353,26 @@
     }
 
     /**
-     * Reads the timezone rules version present in /system. i.e. the version that would be present
-     * after a factory reset.
+     * Reads the base time zone rules version. i.e. the version that would be present after an
+     * installed update is removed.
      *
      * @throws IOException if there was a problem reading data
      */
-    public String getSystemRulesVersion() throws IOException {
-        return readSystemRulesVersion(systemTzDataFile);
+    public TzDataSetVersion readBaseVersion() throws IOException {
+        return readBaseVersion(baseVersionFile);
+    }
+
+    private TzDataSetVersion readBaseVersion(File baseVersionFile) throws IOException {
+        if (!baseVersionFile.exists()) {
+            Slog.i(logTag, "version file cannot be found in " + baseVersionFile);
+            throw new FileNotFoundException(
+                    "base version file does not exist: " + baseVersionFile);
+        }
+        try {
+            return TzDataSetVersion.readFromFile(baseVersionFile);
+        } catch (TzDataSetException e) {
+            throw new IOException("Unable to read: " + baseVersionFile, e);
+        }
     }
 
     private void deleteBestEffort(File dir) {
@@ -395,34 +411,27 @@
     }
 
     /**
-     * Returns true if the the distro IANA rules version is >= system IANA rules version.
+     * Returns true if the the distro IANA rules version is >= base IANA rules version.
      */
-    private boolean checkDistroRulesNewerThanSystem(
-            File systemTzDataFile, DistroVersion distroVersion) throws IOException {
+    private boolean checkDistroRulesNewerThanBase(
+            File baseVersionFile, DistroVersion distroVersion) throws IOException {
 
-        // We only check the /system tzdata file and assume that other data like ICU is in sync.
-        // There is a CTS test that checks ICU and bionic/libcore are in sync.
-        Slog.i(logTag, "Reading /system rules version");
-        String systemRulesVersion = readSystemRulesVersion(systemTzDataFile);
+        // We only check the base tz_version file and assume that data like ICU is in sync.
+        // There is a CTS test that checks tz_version, ICU and bionic/libcore are in sync.
+        Slog.i(logTag, "Reading base time zone rules version");
+        TzDataSetVersion baseVersion = readBaseVersion(baseVersionFile);
 
+        String baseRulesVersion = baseVersion.rulesVersion;
         String distroRulesVersion = distroVersion.rulesVersion;
-        // canApply = distroRulesVersion >= systemRulesVersion
-        boolean canApply = distroRulesVersion.compareTo(systemRulesVersion) >= 0;
+        // canApply = distroRulesVersion >= baseRulesVersion
+        boolean canApply = distroRulesVersion.compareTo(baseRulesVersion) >= 0;
         if (!canApply) {
             Slog.i(logTag, "Failed rules version check: distroRulesVersion="
-                    + distroRulesVersion + ", systemRulesVersion=" + systemRulesVersion);
+                    + distroRulesVersion + ", baseRulesVersion=" + baseRulesVersion);
         } else {
             Slog.i(logTag, "Passed rules version check: distroRulesVersion="
-                    + distroRulesVersion + ", systemRulesVersion=" + systemRulesVersion);
+                    + distroRulesVersion + ", baseRulesVersion=" + baseRulesVersion);
         }
         return canApply;
     }
-
-    private String readSystemRulesVersion(File systemTzDataFile) throws IOException {
-        if (!systemTzDataFile.exists()) {
-            Slog.i(logTag, "tzdata file cannot be found in /system");
-            throw new FileNotFoundException("system tzdata does not exist: " + systemTzDataFile);
-        }
-        return ZoneInfoDB.TzData.getRulesVersion(systemTzDataFile);
-    }
 }
diff --git a/distro/installer/src/test/com/android/timezone/distro/installer/TimeZoneDistroInstallerTest.java b/distro/installer/src/test/com/android/timezone/distro/installer/TimeZoneDistroInstallerTest.java
index bdbbc70..50bea96 100644
--- a/distro/installer/src/test/com/android/timezone/distro/installer/TimeZoneDistroInstallerTest.java
+++ b/distro/installer/src/test/com/android/timezone/distro/installer/TimeZoneDistroInstallerTest.java
@@ -48,31 +48,36 @@
  */
 public class TimeZoneDistroInstallerTest extends TestCase {
 
-    // OLDER_RULES_VERSION < SYSTEM_RULES_VERSION < NEW_RULES_VERSION < NEWER_RULES_VERSION
+    // OLDER_RULES_VERSION < BASE_RULES_VERSION < NEW_RULES_VERSION < NEWER_RULES_VERSION
     private static final String OLDER_RULES_VERSION = "2030a";
-    private static final String SYSTEM_RULES_VERSION = "2030b";
+    private static final String BASE_RULES_VERSION = "2030b";
     private static final String NEW_RULES_VERSION = "2030c";
     private static final String NEWER_RULES_VERSION = "2030d";
 
     private TimeZoneDistroInstaller installer;
     private File tempDir;
     private File testInstallDir;
-    private File testSystemTzDataDir;
+    private File testBaseDataDir;
 
     @Override
     public void setUp() throws Exception {
         super.setUp();
         tempDir = createUniqueDirectory(null, "tempDir");
         testInstallDir = createSubDirectory(tempDir, "testInstall");
-        testSystemTzDataDir =  createSubDirectory(tempDir, "testSystemTzData");
+        testBaseDataDir =  createSubDirectory(tempDir, "testBaseData");
 
-        // Create a file to represent the tzdata file in the /system partition of the device.
-        File testSystemTzDataFile = new File(testSystemTzDataDir, "tzdata");
-        byte[] systemTzDataBytes = createTzData(SYSTEM_RULES_VERSION);
-        createFile(testSystemTzDataFile, systemTzDataBytes);
+        // Create a tz_version file to indicate the base version of tz data on a device.
+        TzDataSetVersion tzDataSetVersion =
+                new TzDataSetVersion(
+                        TzDataSetVersion.currentFormatMajorVersion(),
+                        TzDataSetVersion.currentFormatMinorVersion(),
+                        BASE_RULES_VERSION,
+                        1 /* revision */);
+        File testBaseVersionFile = new File(testBaseDataDir, TzDataSetVersion.DEFAULT_FILE_NAME);
+        createFile(testBaseVersionFile, tzDataSetVersion.toBytes());
 
         installer = new TimeZoneDistroInstaller(
-                "TimeZoneDistroInstallerTest", testSystemTzDataFile, testInstallDir);
+                "TimeZoneDistroInstallerTest", testBaseVersionFile, testInstallDir);
     }
 
     /**
@@ -100,15 +105,15 @@
         super.tearDown();
     }
 
-    /** Tests the an update on a device will fail if the /system tzdata file cannot be found. */
-    public void testStageInstallWithErrorCode_badSystemFile() throws Exception {
-        File doesNotExist = new File(testSystemTzDataDir, "doesNotExist");
-        TimeZoneDistroInstaller brokenSystemInstaller = new TimeZoneDistroInstaller(
+    /** Tests the an update on a device will fail if the base tz_version file cannot be found. */
+    public void testStageInstallWithErrorCode_badBaseFile() throws Exception {
+        File doesNotExist = new File(testBaseDataDir, "doesNotExist");
+        TimeZoneDistroInstaller brokenBaseInstaller = new TimeZoneDistroInstaller(
                 "TimeZoneDistroInstallerTest", doesNotExist, testInstallDir);
         byte[] distroBytes = createValidTimeZoneDistroBytes(NEW_RULES_VERSION, 1);
 
         try {
-            brokenSystemInstaller.stageInstallWithErrorCode(new TimeZoneDistro(distroBytes));
+            brokenBaseInstaller.stageInstallWithErrorCode(new TimeZoneDistro(distroBytes));
             fail();
         } catch (IOException expected) {}
 
@@ -128,11 +133,11 @@
     }
 
     /**
-     * Tests we can install an update the same version as is in /system.
+     * Tests we can install an update with the same version as the base version.
      */
-    public void testStageInstallWithErrorCode_successfulFirstUpdate_sameVersionAsSystem()
+    public void testStageInstallWithErrorCode_successfulFirstUpdate_sameVersionAsBase()
             throws Exception {
-        byte[] distroBytes = createValidTimeZoneDistroBytes(SYSTEM_RULES_VERSION, 1);
+        byte[] distroBytes = createValidTimeZoneDistroBytes(BASE_RULES_VERSION, 1);
         assertEquals(
                 TimeZoneDistroInstaller.INSTALL_SUCCESS,
                 installer.stageInstallWithErrorCode(new TimeZoneDistro(distroBytes)));
@@ -141,9 +146,9 @@
     }
 
     /**
-     * Tests we cannot install an update older than the version in /system.
+     * Tests we cannot install an update older than the base version.
      */
-    public void testStageInstallWithErrorCode_unsuccessfulFirstUpdate_olderVersionThanSystem()
+    public void testStageInstallWithErrorCode_unsuccessfulFirstUpdate_olderVersionThanBase()
             throws Exception {
         byte[] distroBytes = createValidTimeZoneDistroBytes(OLDER_RULES_VERSION, 1);
         assertEquals(
@@ -180,7 +185,7 @@
 
     /**
      * Tests an update on a device when there is a prior update already applied, but the follow
-     * on update is older than in /system.
+     * on update is older than the base version.
      */
     public void testStageInstallWithErrorCode_unsuccessfulFollowOnUpdate_olderVersion()
             throws Exception {
@@ -494,8 +499,9 @@
         assertInstalledDistro(installedDistroBytes);
     }
 
-    public void testGetSystemRulesVersion() throws Exception {
-        assertEquals(SYSTEM_RULES_VERSION, installer.getSystemRulesVersion());
+    public void testReadBaseRulesVersion() throws Exception {
+        TzDataSetVersion actualBaseVersion = installer.readBaseVersion();
+        assertEquals(BASE_RULES_VERSION, actualBaseVersion.rulesVersion);
     }
 
     public void testGetInstalledDistroVersion() throws Exception {