1 /* 2 * Copyright (C) 2020 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 android.timezone; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 22 import com.android.i18n.timezone.TimeZoneDataFiles; 23 import com.android.internal.annotations.VisibleForTesting; 24 25 import java.io.IOException; 26 import java.util.Objects; 27 28 /** 29 * Version information associated with the set of time zone data on a device. 30 * 31 * <p>Time Zone Data Sets have a major ({@link #getFormatMajorVersion()}) and minor 32 * ({@link #currentFormatMinorVersion()}) version number: 33 * <ul> 34 * <li>Major version numbers are mutually incompatible. e.g. v2 is not compatible with a v1 or a 35 * v3 device.</li> 36 * <li>Minor version numbers are backwards compatible. e.g. a v2.2 data set will work 37 * on a v2.1 device but not a v2.3 device. The minor version is reset to 1 when the major version 38 * is incremented.</li> 39 * </ul> 40 * 41 * <p>Data sets contain time zone rules and other data associated wtih a tzdb release 42 * ({@link #getRulesVersion()}) and an additional Android-specific revision number 43 * ({@link #getRevision()}). 44 * 45 * <p>See platform/system/timezone/README.android for more information. 46 * @hide 47 */ 48 @VisibleForTesting 49 public final class TzDataSetVersion { 50 51 /** 52 * Returns the major tz data format version supported by this device. 53 */ currentFormatMajorVersion()54 public static int currentFormatMajorVersion() { 55 return com.android.i18n.timezone.TzDataSetVersion.currentFormatMajorVersion(); 56 } 57 58 /** 59 * Returns the minor tz data format version supported by this device. 60 */ currentFormatMinorVersion()61 public static int currentFormatMinorVersion() { 62 return com.android.i18n.timezone.TzDataSetVersion.currentFormatMinorVersion(); 63 } 64 65 /** 66 * Returns true if the version information provided would be compatible with this device, i.e. 67 * with the current system image, and set of active modules. 68 */ isCompatibleWithThisDevice(TzDataSetVersion tzDataSetVersion)69 public static boolean isCompatibleWithThisDevice(TzDataSetVersion tzDataSetVersion) { 70 return com.android.i18n.timezone.TzDataSetVersion.isCompatibleWithThisDevice( 71 tzDataSetVersion.mDelegate); 72 } 73 74 /** 75 * Reads the current Android time zone data set version file. 76 */ 77 @NonNull read()78 public static TzDataSetVersion read() throws IOException, TzDataSetException { 79 try { 80 return new TzDataSetVersion(TimeZoneDataFiles.readTimeZoneModuleVersion()); 81 } catch (com.android.i18n.timezone.TzDataSetVersion.TzDataSetException e) { 82 throw new TzDataSetException(e.getMessage(), e); 83 } 84 } 85 86 /** 87 * A checked exception used in connection with time zone data sets. 88 * @hide 89 */ 90 public static final class TzDataSetException extends Exception { 91 92 /** Creates an instance with a message. */ TzDataSetException(String message)93 public TzDataSetException(String message) { 94 super(message); 95 } 96 97 /** Creates an instance with a message and a cause. */ TzDataSetException(String message, Throwable cause)98 public TzDataSetException(String message, Throwable cause) { 99 super(message, cause); 100 } 101 } 102 103 @NonNull 104 private final com.android.i18n.timezone.TzDataSetVersion mDelegate; 105 TzDataSetVersion(@onNull com.android.i18n.timezone.TzDataSetVersion delegate)106 private TzDataSetVersion(@NonNull com.android.i18n.timezone.TzDataSetVersion delegate) { 107 mDelegate = Objects.requireNonNull(delegate); 108 } 109 110 /** Returns the major version number. See {@link TzDataSetVersion}. */ getFormatMajorVersion()111 public int getFormatMajorVersion() { 112 return mDelegate.getFormatMajorVersion(); 113 } 114 115 /** Returns the minor version number. See {@link TzDataSetVersion}. */ getFormatMinorVersion()116 public int getFormatMinorVersion() { 117 return mDelegate.getFormatMinorVersion(); 118 } 119 120 /** Returns the tzdb version string. See {@link TzDataSetVersion}. */ 121 @NonNull getRulesVersion()122 public String getRulesVersion() { 123 return mDelegate.getRulesVersion(); 124 } 125 126 /** Returns the Android revision. See {@link TzDataSetVersion}. */ getRevision()127 public int getRevision() { 128 return mDelegate.getRevision(); 129 } 130 131 @Override equals(@ullable Object o)132 public boolean equals(@Nullable Object o) { 133 if (this == o) { 134 return true; 135 } 136 if (o == null || getClass() != o.getClass()) { 137 return false; 138 } 139 TzDataSetVersion that = (TzDataSetVersion) o; 140 return mDelegate.equals(that.mDelegate); 141 } 142 143 @Override hashCode()144 public int hashCode() { 145 return Objects.hash(mDelegate); 146 } 147 148 @Override toString()149 public String toString() { 150 return mDelegate.toString(); 151 } 152 } 153