Split shared tzdata code into a separate library

Split shared tzdata code into a separate library with
a host version.

The shared code is used by a host-side test which cannot
depend on the TimeZoneDistroInstaller. The installer also
has framework dependencies making it difficult to compile
for host.

createTimeZoneDistro.sh has been updated now there is a
suitable target to build the required code.

Bug: 31008728
Test: make / cts
Merged-In: Iacefe774106590782d4c8ca186cac35135b779e4
Change-Id: Iea6363d761bcbc028c9b89f34f743ff355b1d267
diff --git a/tzdata/shared2/Android.mk b/tzdata/shared2/Android.mk
new file mode 100644
index 0000000..634386b
--- /dev/null
+++ b/tzdata/shared2/Android.mk
@@ -0,0 +1,46 @@
+# Copyright (C) 2015 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.
+
+LOCAL_PATH:= $(call my-dir)
+
+# Library of support classes for tzdata updates. Used on-device for handling distros and for
+# testing.
+include $(CLEAR_VARS)
+LOCAL_MODULE := tzdata_shared2
+LOCAL_MODULE_TAGS := optional
+LOCAL_SRC_FILES := $(call all-java-files-under, src/main)
+LOCAL_JAVACFLAGS := -encoding UTF-8
+LOCAL_JAVA_LIBRARIES := core-oj core-libart
+LOCAL_NO_STANDARD_LIBRARIES := true
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+# Library of support classes for tzdata updates. Used on host for host-side tests.
+include $(CLEAR_VARS)
+LOCAL_MODULE := tzdata_shared2-host
+LOCAL_MODULE_TAGS := optional
+LOCAL_SRC_FILES := $(call all-java-files-under, src/main)
+LOCAL_JAVACFLAGS := -encoding UTF-8
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+include $(BUILD_HOST_JAVA_LIBRARY)
+
+# Tests for tzdata_update2 code
+include $(CLEAR_VARS)
+LOCAL_MODULE := tzdata_shared2-tests
+LOCAL_MODULE_TAGS := optional
+LOCAL_SRC_FILES := $(call all-java-files-under, src/test)
+LOCAL_JAVACFLAGS := -encoding UTF-8
+LOCAL_STATIC_JAVA_LIBRARIES := tzdata_shared2 junit
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+include $(BUILD_STATIC_JAVA_LIBRARY)
diff --git a/tzdata/update2/src/main/libcore/tzdata/update2/DistroException.java b/tzdata/shared2/src/main/libcore/tzdata/shared2/DistroException.java
similarity index 96%
rename from tzdata/update2/src/main/libcore/tzdata/update2/DistroException.java
rename to tzdata/shared2/src/main/libcore/tzdata/shared2/DistroException.java
index d83e473..d989486 100644
--- a/tzdata/update2/src/main/libcore/tzdata/update2/DistroException.java
+++ b/tzdata/shared2/src/main/libcore/tzdata/shared2/DistroException.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package libcore.tzdata.update2;
+package libcore.tzdata.shared2;
 
 /**
  * A checked exception used in connection with time zone distro creation / installation.
diff --git a/tzdata/update2/src/main/libcore/tzdata/update2/DistroVersion.java b/tzdata/shared2/src/main/libcore/tzdata/shared2/DistroVersion.java
similarity index 98%
rename from tzdata/update2/src/main/libcore/tzdata/update2/DistroVersion.java
rename to tzdata/shared2/src/main/libcore/tzdata/shared2/DistroVersion.java
index 4848db7..3902d36 100644
--- a/tzdata/update2/src/main/libcore/tzdata/update2/DistroVersion.java
+++ b/tzdata/shared2/src/main/libcore/tzdata/shared2/DistroVersion.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package libcore.tzdata.update2;
+package libcore.tzdata.shared2;
 
 import java.nio.charset.StandardCharsets;
 import java.util.Locale;
@@ -63,7 +63,7 @@
      * The length of a well-formed distro version file:
      * {Distro version}|{Rule version}|{Revision}
      */
-    static final int DISTRO_VERSION_FILE_LENGTH = FORMAT_VERSION_STRING_LENGTH + 1
+    public static final int DISTRO_VERSION_FILE_LENGTH = FORMAT_VERSION_STRING_LENGTH + 1
             + RULES_VERSION_LENGTH
             + 1 + REVISION_LENGTH;
 
diff --git a/tzdata/update2/src/main/libcore/tzdata/update2/FileUtils.java b/tzdata/shared2/src/main/libcore/tzdata/shared2/FileUtils.java
similarity index 99%
rename from tzdata/update2/src/main/libcore/tzdata/update2/FileUtils.java
rename to tzdata/shared2/src/main/libcore/tzdata/shared2/FileUtils.java
index 3d9a73a..a170a9e 100644
--- a/tzdata/update2/src/main/libcore/tzdata/update2/FileUtils.java
+++ b/tzdata/shared2/src/main/libcore/tzdata/shared2/FileUtils.java
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package libcore.tzdata.update2;
+package libcore.tzdata.shared2;
 
 import java.io.File;
 import java.io.FileInputStream;
diff --git a/tzdata/update2/src/main/libcore/tzdata/update2/TimeZoneDistro.java b/tzdata/shared2/src/main/libcore/tzdata/shared2/TimeZoneDistro.java
similarity index 99%
rename from tzdata/update2/src/main/libcore/tzdata/update2/TimeZoneDistro.java
rename to tzdata/shared2/src/main/libcore/tzdata/shared2/TimeZoneDistro.java
index 04e3f2a..dd01fb0 100644
--- a/tzdata/update2/src/main/libcore/tzdata/update2/TimeZoneDistro.java
+++ b/tzdata/shared2/src/main/libcore/tzdata/shared2/TimeZoneDistro.java
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package libcore.tzdata.update2;
+package libcore.tzdata.shared2;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
diff --git a/tzdata/update2/src/test/libcore/tzdata/update2/DistroVersionTest.java b/tzdata/shared2/src/test/libcore/tzdata/shared2/DistroVersionTest.java
similarity index 98%
rename from tzdata/update2/src/test/libcore/tzdata/update2/DistroVersionTest.java
rename to tzdata/shared2/src/test/libcore/tzdata/shared2/DistroVersionTest.java
index 79ba9d8..5d3df3f 100644
--- a/tzdata/update2/src/test/libcore/tzdata/update2/DistroVersionTest.java
+++ b/tzdata/shared2/src/test/libcore/tzdata/shared2/DistroVersionTest.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package libcore.tzdata.update2;
+package libcore.tzdata.shared2;
 
 import junit.framework.TestCase;
 
diff --git a/tzdata/update2/src/test/libcore/tzdata/update2/FileUtilsTest.java b/tzdata/shared2/src/test/libcore/tzdata/shared2/FileUtilsTest.java
similarity index 99%
rename from tzdata/update2/src/test/libcore/tzdata/update2/FileUtilsTest.java
rename to tzdata/shared2/src/test/libcore/tzdata/shared2/FileUtilsTest.java
index 470d1c1..3b9ec5b 100644
--- a/tzdata/update2/src/test/libcore/tzdata/update2/FileUtilsTest.java
+++ b/tzdata/shared2/src/test/libcore/tzdata/shared2/FileUtilsTest.java
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package libcore.tzdata.update2;
+package libcore.tzdata.shared2;
 
 import junit.framework.TestCase;
 
diff --git a/tzdata/update2/src/test/libcore/tzdata/update2/TimeZoneDistroTest.java b/tzdata/shared2/src/test/libcore/tzdata/shared2/TimeZoneDistroTest.java
similarity index 99%
rename from tzdata/update2/src/test/libcore/tzdata/update2/TimeZoneDistroTest.java
rename to tzdata/shared2/src/test/libcore/tzdata/shared2/TimeZoneDistroTest.java
index dad1cc4..e5b6a17 100644
--- a/tzdata/update2/src/test/libcore/tzdata/update2/TimeZoneDistroTest.java
+++ b/tzdata/shared2/src/test/libcore/tzdata/shared2/TimeZoneDistroTest.java
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package libcore.tzdata.update2;
+package libcore.tzdata.shared2;
 
 import junit.framework.TestCase;
 
diff --git a/tzdata/testing/Android.mk b/tzdata/testing/Android.mk
index f6b9b3c..d86255b 100644
--- a/tzdata/testing/Android.mk
+++ b/tzdata/testing/Android.mk
@@ -25,6 +25,15 @@
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 include $(BUILD_STATIC_JAVA_LIBRARY)
 
+# Library of test-support classes for tzdata updates. Shared between CTS and other tests.
+include $(CLEAR_VARS)
+LOCAL_MODULE := tzdata-testing-host
+LOCAL_MODULE_TAGS := optional
+LOCAL_SRC_FILES := $(call all-java-files-under, src/main)
+LOCAL_JAVACFLAGS := -encoding UTF-8
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+include $(BUILD_HOST_JAVA_LIBRARY)
+
 # Host version of the above library. For libcore host testing.
 include $(CLEAR_VARS)
 LOCAL_MODULE := tzdata-testing-hostdex
diff --git a/tzdata/tools2/Android.mk b/tzdata/tools2/Android.mk
index 7789700..f91fc6a 100644
--- a/tzdata/tools2/Android.mk
+++ b/tzdata/tools2/Android.mk
@@ -14,14 +14,23 @@
 
 LOCAL_PATH:= $(call my-dir)
 
-# Library of tools classes for tzdata updates. Not required on device, except in tests.
+# Library of tools classes for tzdata updates. Only used in tests.
 include $(CLEAR_VARS)
 LOCAL_MODULE := tzdata_tools2
 LOCAL_MODULE_TAGS := optional
 LOCAL_SRC_FILES := $(call all-java-files-under, src/main)
 LOCAL_JAVACFLAGS := -encoding UTF-8
-LOCAL_JAVA_LIBRARIES := core-oj core-libart
+LOCAL_JAVA_LIBRARIES := core-oj core-libart tzdata_shared2
 LOCAL_NO_STANDARD_LIBRARIES := true
-LOCAL_STATIC_JAVA_LIBRARIES := tzdata_update2
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 include $(BUILD_STATIC_JAVA_LIBRARY)
+
+# Library of tools classes for tzdata updates. Used when generating distros and in host-side tests.
+include $(CLEAR_VARS)
+LOCAL_MODULE := tzdata_tools2-host
+LOCAL_MODULE_TAGS := optional
+LOCAL_SRC_FILES := $(call all-java-files-under, src/main)
+LOCAL_JAVACFLAGS := -encoding UTF-8
+LOCAL_JAVA_LIBRARIES := tzdata_shared2-host
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+include $(BUILD_HOST_JAVA_LIBRARY)
diff --git a/tzdata/tools2/createTimeZoneDistro.sh b/tzdata/tools2/createTimeZoneDistro.sh
index 7e103e7..dc76c3d 100755
--- a/tzdata/tools2/createTimeZoneDistro.sh
+++ b/tzdata/tools2/createTimeZoneDistro.sh
@@ -5,23 +5,19 @@
 # Usage: ./createTimeZoneDistro.sh <tzupdate.properties file> <output file>
 # See libcore.tzdata.update2.tools.CreateTimeZoneDistro for more information.
 
-TOOLS_DIR=src/main/libcore/tzdata/update2/tools
-UPDATE_DIR=../update2/src/main/libcore/tzdata/update2
-GEN_DIR=./gen
-
 # Fail if anything below fails
 set -e
 
-rm -rf ${GEN_DIR}
-mkdir -p ${GEN_DIR}
+if [[ -z "${ANDROID_BUILD_TOP}" ]]; then
+  echo "Configure your environment with build/envsetup.sh and lunch"
+  exit 1
+fi
 
-javac \
-    ${TOOLS_DIR}/CreateTimeZoneDistro.java \
-    ${TOOLS_DIR}/TimeZoneDistroBuilder.java \
-    ${UPDATE_DIR}/DistroException.java \
-    ${UPDATE_DIR}/DistroVersion.java \
-    ${UPDATE_DIR}/FileUtils.java \
-    ${UPDATE_DIR}/TimeZoneDistro.java \
-    -d ${GEN_DIR}
+cd ${ANDROID_BUILD_TOP}
+make tzdata_tools2-host
 
-java -cp ${GEN_DIR} libcore.tzdata.update2.tools.CreateTimeZoneDistro $@
+TOOLS_LIB=${ANDROID_BUILD_TOP}/out/host/common/obj/JAVA_LIBRARIES/tzdata_tools2-host_intermediates/javalib.jar
+SHARED_LIB=${ANDROID_BUILD_TOP}/out/host/common/obj/JAVA_LIBRARIES/tzdata_shared2-host_intermediates/javalib.jar
+
+cd -
+java -cp ${TOOLS_LIB}:${SHARED_LIB} libcore.tzdata.update2.tools.CreateTimeZoneDistro $@
diff --git a/tzdata/tools2/src/main/libcore/tzdata/update2/tools/CreateTimeZoneDistro.java b/tzdata/tools2/src/main/libcore/tzdata/update2/tools/CreateTimeZoneDistro.java
index b2b3fa4..4b70152 100644
--- a/tzdata/tools2/src/main/libcore/tzdata/update2/tools/CreateTimeZoneDistro.java
+++ b/tzdata/tools2/src/main/libcore/tzdata/update2/tools/CreateTimeZoneDistro.java
@@ -23,8 +23,8 @@
 import java.io.OutputStream;
 import java.io.Reader;
 import java.util.Properties;
-import libcore.tzdata.update2.DistroVersion;
-import libcore.tzdata.update2.TimeZoneDistro;
+import libcore.tzdata.shared2.DistroVersion;
+import libcore.tzdata.shared2.TimeZoneDistro;
 
 /**
  * A command-line tool for creating a timezone update distro.
diff --git a/tzdata/tools2/src/main/libcore/tzdata/update2/tools/TimeZoneDistroBuilder.java b/tzdata/tools2/src/main/libcore/tzdata/update2/tools/TimeZoneDistroBuilder.java
index 6577401..4c12e96 100644
--- a/tzdata/tools2/src/main/libcore/tzdata/update2/tools/TimeZoneDistroBuilder.java
+++ b/tzdata/tools2/src/main/libcore/tzdata/update2/tools/TimeZoneDistroBuilder.java
@@ -21,9 +21,9 @@
 import java.io.IOException;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
-import libcore.tzdata.update2.DistroException;
-import libcore.tzdata.update2.DistroVersion;
-import libcore.tzdata.update2.TimeZoneDistro;
+import libcore.tzdata.shared2.DistroException;
+import libcore.tzdata.shared2.DistroVersion;
+import libcore.tzdata.shared2.TimeZoneDistro;
 
 /**
  * A class for creating a {@link TimeZoneDistro} containing timezone update data. Used in real
diff --git a/tzdata/update2/Android.mk b/tzdata/update2/Android.mk
index 9e32862..0e197ba 100644
--- a/tzdata/update2/Android.mk
+++ b/tzdata/update2/Android.mk
@@ -14,13 +14,13 @@
 
 LOCAL_PATH:= $(call my-dir)
 
-# Library of support classes for tzdata updates. Shared between update generation and
-# on-device code.
+# The classes needed to handle installation of time zone updates.
 include $(CLEAR_VARS)
 LOCAL_MODULE := tzdata_update2
 LOCAL_MODULE_TAGS := optional
 LOCAL_SRC_FILES := $(call all-java-files-under, src/main)
 LOCAL_JAVACFLAGS := -encoding UTF-8
+LOCAL_JAVA_LIBRARIES := tzdata_shared2
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 include $(BUILD_STATIC_JAVA_LIBRARY)
 
@@ -30,6 +30,6 @@
 LOCAL_MODULE_TAGS := optional
 LOCAL_SRC_FILES := $(call all-java-files-under, src/test)
 LOCAL_JAVACFLAGS := -encoding UTF-8
-LOCAL_STATIC_JAVA_LIBRARIES := tzdata_update2 tzdata_tools2 tzdata-testing junit
+LOCAL_STATIC_JAVA_LIBRARIES := tzdata_shared2 tzdata_update2 tzdata_tools2 tzdata-testing junit
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 include $(BUILD_STATIC_JAVA_LIBRARY)
diff --git a/tzdata/update2/src/main/libcore/tzdata/update2/TimeZoneDistroInstaller.java b/tzdata/update2/src/main/libcore/tzdata/update2/TimeZoneDistroInstaller.java
index 1b56ebf..52fa2b4 100644
--- a/tzdata/update2/src/main/libcore/tzdata/update2/TimeZoneDistroInstaller.java
+++ b/tzdata/update2/src/main/libcore/tzdata/update2/TimeZoneDistroInstaller.java
@@ -20,6 +20,10 @@
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import libcore.tzdata.shared2.DistroException;
+import libcore.tzdata.shared2.DistroVersion;
+import libcore.tzdata.shared2.FileUtils;
+import libcore.tzdata.shared2.TimeZoneDistro;
 import libcore.util.ZoneInfoDB;
 
 /**
@@ -246,8 +250,7 @@
 
     private DistroVersion readDistroVersion(File distroDir) throws DistroException, IOException {
         Slog.i(logTag, "Reading distro format version");
-        File distroVersionFile =
-                new File(distroDir, TimeZoneDistro.DISTRO_VERSION_FILE_NAME);
+        File distroVersionFile = new File(distroDir, TimeZoneDistro.DISTRO_VERSION_FILE_NAME);
         if (!distroVersionFile.exists()) {
             throw new DistroException("No distro version file found: " + distroVersionFile);
         }
diff --git a/tzdata/update2/src/test/libcore/tzdata/update2/TimeZoneDistroInstallerTest.java b/tzdata/update2/src/test/libcore/tzdata/update2/TimeZoneDistroInstallerTest.java
index 6ae0e56..325b605 100644
--- a/tzdata/update2/src/test/libcore/tzdata/update2/TimeZoneDistroInstallerTest.java
+++ b/tzdata/update2/src/test/libcore/tzdata/update2/TimeZoneDistroInstallerTest.java
@@ -30,6 +30,9 @@
 import java.util.zip.ZipOutputStream;
 import libcore.io.IoUtils;
 import libcore.io.Streams;
+import libcore.tzdata.shared2.DistroVersion;
+import libcore.tzdata.shared2.FileUtils;
+import libcore.tzdata.shared2.TimeZoneDistro;
 import libcore.tzdata.testing.ZoneInfoTestHelper;
 import libcore.tzdata.update2.tools.TimeZoneDistroBuilder;