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