1 /* 2 * Copyright (C) 2018 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 libcore.util; 18 19 import libcore.timezone.TimeZoneDataFiles; 20 import libcore.timezone.TzDataSetVersion; 21 import libcore.timezone.TzDataSetVersion.TzDataSetException; 22 import libcore.timezone.ZoneInfoDB; 23 24 import java.io.File; 25 import java.io.IOException; 26 27 /** 28 * Provides APIs for obtaining metadata for the managed core library and lower-level 29 * components like bionic and the runtime. 30 * 31 * @hide 32 */ 33 @libcore.api.CorePlatformApi 34 public class CoreLibraryDebug { 35 36 private static final String CORE_LIBRARY_TIMEZONE_DEBUG_PREFIX = "core_library.timezone."; 37 CoreLibraryDebug()38 private CoreLibraryDebug() {} 39 40 /** 41 * Returns information about the Core Library for debugging. 42 */ 43 @libcore.api.CorePlatformApi getDebugInfo()44 public static DebugInfo getDebugInfo() { 45 DebugInfo debugInfo = new DebugInfo(); 46 populateTimeZoneFilesInfo(debugInfo); 47 populateTimeZoneLibraryReportedVersion(debugInfo); 48 return debugInfo; 49 } 50 51 /** 52 * Adds information about the available time zone file sets on the device to the supplied 53 * {@link DebugInfo}. See also {@link #populateTimeZoneLibraryReportedVersion(DebugInfo)} for a method 54 * that provides information about the time zone files actually in use by libraries. 55 */ populateTimeZoneFilesInfo(DebugInfo debugInfo)56 private static void populateTimeZoneFilesInfo(DebugInfo debugInfo) { 57 String debugKeyPrefix = CORE_LIBRARY_TIMEZONE_DEBUG_PREFIX + "source."; 58 59 // Time zone module tz data set. 60 { 61 String tzDataModulePrefix = debugKeyPrefix + "tzdata_module_"; 62 String versionFileName = TimeZoneDataFiles.getTimeZoneModuleFile( 63 "tz/" + TzDataSetVersion.DEFAULT_FILE_NAME); 64 addTzDataSetVersionDebugInfo(versionFileName, tzDataModulePrefix, debugInfo); 65 } 66 67 // Runtime module tz data set. 68 { 69 String runtimeModulePrefix = debugKeyPrefix + "runtime_module_"; 70 String versionFileName = TimeZoneDataFiles.getRuntimeModuleFile( 71 "tz/" + TzDataSetVersion.DEFAULT_FILE_NAME); 72 addTzDataSetVersionDebugInfo(versionFileName, runtimeModulePrefix, debugInfo); 73 } 74 75 // /system tz data set. 76 { 77 String systemDirPrefix = debugKeyPrefix + "system_"; 78 String versionFileName = 79 TimeZoneDataFiles.getSystemTimeZoneFile(TzDataSetVersion.DEFAULT_FILE_NAME); 80 addTzDataSetVersionDebugInfo(versionFileName, systemDirPrefix, debugInfo); 81 } 82 } 83 addTzDataSetVersionDebugInfo(String tzDataSetVersionFileName, String debugKeyPrefix, DebugInfo debugInfo)84 private static void addTzDataSetVersionDebugInfo(String tzDataSetVersionFileName, 85 String debugKeyPrefix, DebugInfo debugInfo) { 86 File file = new File(tzDataSetVersionFileName); 87 String statusKey = debugKeyPrefix + "status"; 88 if (file.exists()) { 89 try { 90 TzDataSetVersion tzDataSetVersion = 91 TzDataSetVersion.readFromFile(file); 92 String formatVersionString = tzDataSetVersion.formatMajorVersion + "." 93 + tzDataSetVersion.formatMinorVersion; 94 debugInfo.addStringEntry(statusKey, "OK") 95 .addStringEntry(debugKeyPrefix + "formatVersion", formatVersionString) 96 .addStringEntry(debugKeyPrefix + "rulesVersion", 97 tzDataSetVersion.rulesVersion) 98 .addStringEntry(debugKeyPrefix + "revision", 99 tzDataSetVersion.revision); 100 } catch (IOException | TzDataSetException e) { 101 debugInfo.addStringEntry(statusKey, "ERROR"); 102 debugInfo.addStringEntry(debugKeyPrefix + "exception_class", e.getClass().getName()); 103 debugInfo.addStringEntry(debugKeyPrefix + "exception_msg", e.getMessage()); 104 System.logE("Error reading " + file, e); 105 } 106 } else { 107 debugInfo.addStringEntry(statusKey, "NOT_FOUND"); 108 } 109 } 110 populateTimeZoneLibraryReportedVersion(DebugInfo debugInfo)111 private static void populateTimeZoneLibraryReportedVersion(DebugInfo debugInfo) { 112 String debugKeyPrefix = CORE_LIBRARY_TIMEZONE_DEBUG_PREFIX + "lib."; 113 debugInfo.addStringEntry( 114 debugKeyPrefix + "icu4j.tzdb_version", 115 android.icu.util.TimeZone.getTZDataVersion()); 116 debugInfo.addStringEntry( 117 debugKeyPrefix + "libcore.tzdb_version", 118 ZoneInfoDB.getInstance().getVersion()); 119 debugInfo.addStringEntry( 120 debugKeyPrefix + "icu4c.tzdb_version", 121 libcore.icu.ICU.getTZDataVersion()); 122 } 123 } 124