1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.i18n.timezone; 18 19 import android.icu.platform.AndroidDataFiles; 20 21 import java.io.File; 22 import java.io.IOException; 23 24 /** 25 * Utility methods associated with finding updateable time zone data files. ICU4C and ICU4J also 26 * read files affected by time zone updates. That logic is kept in {@link AndroidDataFiles} 27 * and should be updated if file locations or lookup order changes. 28 * 29 * @hide 30 */ 31 @libcore.api.CorePlatformApi 32 public final class TimeZoneDataFiles { 33 private static final String ANDROID_ROOT_ENV = AndroidDataFiles.ANDROID_ROOT_ENV; 34 private static final String ANDROID_TZDATA_ROOT_ENV = AndroidDataFiles.ANDROID_TZDATA_ROOT_ENV; 35 TimeZoneDataFiles()36 private TimeZoneDataFiles() {} 37 38 /** 39 * Returns time zone file paths for the specified file name in an array in the order they 40 * should be tried. See {@link AndroidDataFiles#generateIcuDataPath()} for ICU files instead. 41 * <ul> 42 * <li>[0] - the location of the file from the time zone module under /apex (must exist).</li> 43 * </ul> 44 */ 45 // VisibleForTesting getTimeZoneFilePaths(String fileName)46 public static String[] getTimeZoneFilePaths(String fileName) { 47 return new String[] { getTimeZoneModuleTzFile(fileName) }; 48 } 49 getTimeZoneModuleTzFile(String fileName)50 public static String getTimeZoneModuleTzFile(String fileName) { 51 return getTimeZoneModuleFile("tz/" + fileName); 52 } 53 54 // Remove from CorePlatformApi when all users in platform code are removed. http://b/123398797 55 @libcore.api.CorePlatformApi getTimeZoneModuleTzVersionFile()56 public static String getTimeZoneModuleTzVersionFile() { 57 return getTimeZoneModuleTzFile(TzDataSetVersion.DEFAULT_FILE_NAME); 58 } 59 60 /** 61 * Reads the version of time zone data supplied by the time zone data module. 62 */ 63 @libcore.api.CorePlatformApi readTimeZoneModuleVersion()64 public static TzDataSetVersion readTimeZoneModuleVersion() 65 throws IOException, TzDataSetVersion.TzDataSetException { 66 String tzVersionFileName = getTimeZoneModuleTzFile(TzDataSetVersion.DEFAULT_FILE_NAME); 67 return TzDataSetVersion.readFromFile(new File(tzVersionFileName)); 68 } 69 70 // VisibleForTesting getTimeZoneModuleFile(String fileName)71 public static String getTimeZoneModuleFile(String fileName) { 72 return System.getenv(ANDROID_TZDATA_ROOT_ENV) + "/etc/" + fileName; 73 } 74 getSystemTzFile(String fileName)75 public static String getSystemTzFile(String fileName) { 76 return getEnvironmentPath(ANDROID_ROOT_ENV, "/usr/share/zoneinfo/" + fileName); 77 } 78 79 // VisibleForTesting getSystemIcuFile(String fileName)80 public static String getSystemIcuFile(String fileName) { 81 return getEnvironmentPath(ANDROID_ROOT_ENV, "/usr/icu/" + fileName); 82 } 83 84 /** 85 * Creates a path by combining the value of an environment variable with a relative path. 86 * Returns {@code null} if the environment variable is not set. 87 */ getEnvironmentPath(String environmentVariable, String path)88 private static String getEnvironmentPath(String environmentVariable, String path) { 89 String variable = System.getenv(environmentVariable); 90 if (variable == null) { 91 return null; 92 } 93 return variable + path; 94 } 95 } 96