Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 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.server.timezone; |
| 18 | |
| 19 | import com.android.internal.annotations.VisibleForTesting; |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 20 | import com.android.server.EventLogTags; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 21 | import com.android.server.SystemService; |
Neil Fuller | a6a71d0 | 2017-06-13 15:12:17 +0100 | [diff] [blame] | 22 | import com.android.timezone.distro.DistroException; |
| 23 | import com.android.timezone.distro.DistroVersion; |
| 24 | import com.android.timezone.distro.StagedDistroOperation; |
Neil Fuller | 54525bf | 2017-06-22 14:10:29 +0100 | [diff] [blame] | 25 | import com.android.timezone.distro.TimeZoneDistro; |
Neil Fuller | 5ca8b5b | 2017-06-29 12:39:49 +0100 | [diff] [blame] | 26 | import com.android.timezone.distro.installer.TimeZoneDistroInstaller; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 27 | |
| 28 | import android.app.timezone.Callback; |
| 29 | import android.app.timezone.DistroFormatVersion; |
| 30 | import android.app.timezone.DistroRulesVersion; |
| 31 | import android.app.timezone.ICallback; |
| 32 | import android.app.timezone.IRulesManager; |
| 33 | import android.app.timezone.RulesManager; |
| 34 | import android.app.timezone.RulesState; |
| 35 | import android.content.Context; |
| 36 | import android.os.ParcelFileDescriptor; |
| 37 | import android.os.RemoteException; |
| 38 | import android.util.Slog; |
| 39 | |
| 40 | import java.io.File; |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 41 | import java.io.FileDescriptor; |
Neil Fuller | 54525bf | 2017-06-22 14:10:29 +0100 | [diff] [blame] | 42 | import java.io.FileInputStream; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 43 | import java.io.IOException; |
Neil Fuller | 54525bf | 2017-06-22 14:10:29 +0100 | [diff] [blame] | 44 | import java.io.InputStream; |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 45 | import java.io.PrintWriter; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 46 | import java.util.Arrays; |
| 47 | import java.util.concurrent.Executor; |
| 48 | import java.util.concurrent.atomic.AtomicBoolean; |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 49 | import libcore.icu.ICU; |
Neil Fuller | 0ac8df0 | 2018-11-16 16:19:26 +0000 | [diff] [blame^] | 50 | import libcore.timezone.TzDataSetVersion; |
Neil Fuller | c903614 | 2017-10-04 17:04:38 +0100 | [diff] [blame] | 51 | import libcore.util.TimeZoneFinder; |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 52 | import libcore.util.ZoneInfoDB; |
| 53 | |
| 54 | import static android.app.timezone.RulesState.DISTRO_STATUS_INSTALLED; |
| 55 | import static android.app.timezone.RulesState.DISTRO_STATUS_NONE; |
| 56 | import static android.app.timezone.RulesState.DISTRO_STATUS_UNKNOWN; |
| 57 | import static android.app.timezone.RulesState.STAGED_OPERATION_INSTALL; |
| 58 | import static android.app.timezone.RulesState.STAGED_OPERATION_NONE; |
| 59 | import static android.app.timezone.RulesState.STAGED_OPERATION_UNINSTALL; |
| 60 | import static android.app.timezone.RulesState.STAGED_OPERATION_UNKNOWN; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 61 | |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 62 | public final class RulesManagerService extends IRulesManager.Stub { |
| 63 | |
| 64 | private static final String TAG = "timezone.RulesManagerService"; |
| 65 | |
| 66 | /** The distro format supported by this device. */ |
| 67 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE) |
| 68 | static final DistroFormatVersion DISTRO_FORMAT_VERSION_SUPPORTED = |
| 69 | new DistroFormatVersion( |
Neil Fuller | 0ac8df0 | 2018-11-16 16:19:26 +0000 | [diff] [blame^] | 70 | TzDataSetVersion.currentFormatMajorVersion(), |
| 71 | TzDataSetVersion.currentFormatMinorVersion()); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 72 | |
| 73 | public static class Lifecycle extends SystemService { |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 74 | public Lifecycle(Context context) { |
| 75 | super(context); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void onStart() { |
Neil Fuller | cd1a109 | 2017-09-13 21:59:59 +0100 | [diff] [blame] | 80 | RulesManagerService service = RulesManagerService.create(getContext()); |
| 81 | service.start(); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 82 | |
Neil Fuller | cd1a109 | 2017-09-13 21:59:59 +0100 | [diff] [blame] | 83 | // Publish the binder service so it can be accessed from other (appropriately |
| 84 | // permissioned) processes. |
| 85 | publishBinderService(Context.TIME_ZONE_RULES_MANAGER_SERVICE, service); |
| 86 | |
| 87 | // Publish the service instance locally so we can use it directly from within the system |
| 88 | // server from TimeZoneUpdateIdler. |
| 89 | publishLocalService(RulesManagerService.class, service); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE) |
| 94 | static final String REQUIRED_UPDATER_PERMISSION = |
| 95 | android.Manifest.permission.UPDATE_TIME_ZONE_RULES; |
Neil Fuller | b214bc4 | 2017-12-18 16:57:22 +0000 | [diff] [blame] | 96 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE) |
| 97 | static final String REQUIRED_QUERY_PERMISSION = |
| 98 | android.Manifest.permission.QUERY_TIME_ZONE_RULES; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 99 | private static final File SYSTEM_TZ_DATA_FILE = new File("/system/usr/share/zoneinfo/tzdata"); |
| 100 | private static final File TZ_DATA_DIR = new File("/data/misc/zoneinfo"); |
| 101 | |
| 102 | private final AtomicBoolean mOperationInProgress = new AtomicBoolean(false); |
| 103 | private final PermissionHelper mPermissionHelper; |
| 104 | private final PackageTracker mPackageTracker; |
| 105 | private final Executor mExecutor; |
Neil Fuller | b144227 | 2017-12-18 15:59:50 +0000 | [diff] [blame] | 106 | private final RulesManagerIntentHelper mIntentHelper; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 107 | private final TimeZoneDistroInstaller mInstaller; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 108 | |
| 109 | private static RulesManagerService create(Context context) { |
| 110 | RulesManagerServiceHelperImpl helper = new RulesManagerServiceHelperImpl(context); |
| 111 | return new RulesManagerService( |
| 112 | helper /* permissionHelper */, |
| 113 | helper /* executor */, |
Neil Fuller | b144227 | 2017-12-18 15:59:50 +0000 | [diff] [blame] | 114 | helper /* intentHelper */, |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 115 | PackageTracker.create(context), |
| 116 | new TimeZoneDistroInstaller(TAG, SYSTEM_TZ_DATA_FILE, TZ_DATA_DIR)); |
| 117 | } |
| 118 | |
| 119 | // A constructor that can be used by tests to supply mocked / faked dependencies. |
Neil Fuller | b144227 | 2017-12-18 15:59:50 +0000 | [diff] [blame] | 120 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE) |
| 121 | RulesManagerService(PermissionHelper permissionHelper, Executor executor, |
| 122 | RulesManagerIntentHelper intentHelper, PackageTracker packageTracker, |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 123 | TimeZoneDistroInstaller timeZoneDistroInstaller) { |
| 124 | mPermissionHelper = permissionHelper; |
| 125 | mExecutor = executor; |
Neil Fuller | b144227 | 2017-12-18 15:59:50 +0000 | [diff] [blame] | 126 | mIntentHelper = intentHelper; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 127 | mPackageTracker = packageTracker; |
| 128 | mInstaller = timeZoneDistroInstaller; |
| 129 | } |
| 130 | |
| 131 | public void start() { |
Neil Fuller | 3582259 | 2017-12-11 14:39:03 +0000 | [diff] [blame] | 132 | // Return value deliberately ignored: no action required on failure to start. |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 133 | mPackageTracker.start(); |
| 134 | } |
| 135 | |
| 136 | @Override // Binder call |
| 137 | public RulesState getRulesState() { |
Neil Fuller | b214bc4 | 2017-12-18 16:57:22 +0000 | [diff] [blame] | 138 | mPermissionHelper.enforceCallerHasPermission(REQUIRED_QUERY_PERMISSION); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 139 | |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 140 | return getRulesStateInternal(); |
| 141 | } |
| 142 | |
| 143 | /** Like {@link #getRulesState()} without the permission check. */ |
| 144 | private RulesState getRulesStateInternal() { |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 145 | synchronized(this) { |
| 146 | String systemRulesVersion; |
| 147 | try { |
| 148 | systemRulesVersion = mInstaller.getSystemRulesVersion(); |
| 149 | } catch (IOException e) { |
| 150 | Slog.w(TAG, "Failed to read system rules", e); |
| 151 | return null; |
| 152 | } |
| 153 | |
Neil Fuller | ad3e133 | 2018-01-22 12:17:14 +0000 | [diff] [blame] | 154 | // Determine the installed distro state. This should be possible regardless of whether |
| 155 | // there's an operation in progress. |
| 156 | DistroVersion installedDistroVersion; |
| 157 | int distroStatus = DISTRO_STATUS_UNKNOWN; |
| 158 | DistroRulesVersion installedDistroRulesVersion = null; |
| 159 | try { |
| 160 | installedDistroVersion = mInstaller.getInstalledDistroVersion(); |
| 161 | if (installedDistroVersion == null) { |
| 162 | distroStatus = DISTRO_STATUS_NONE; |
| 163 | installedDistroRulesVersion = null; |
| 164 | } else { |
| 165 | distroStatus = DISTRO_STATUS_INSTALLED; |
| 166 | installedDistroRulesVersion = new DistroRulesVersion( |
| 167 | installedDistroVersion.rulesVersion, |
| 168 | installedDistroVersion.revision); |
| 169 | } |
| 170 | } catch (DistroException | IOException e) { |
| 171 | Slog.w(TAG, "Failed to read installed distro.", e); |
| 172 | } |
| 173 | |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 174 | boolean operationInProgress = this.mOperationInProgress.get(); |
| 175 | |
| 176 | // Determine the staged operation status, if possible. |
| 177 | DistroRulesVersion stagedDistroRulesVersion = null; |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 178 | int stagedOperationStatus = STAGED_OPERATION_UNKNOWN; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 179 | if (!operationInProgress) { |
| 180 | StagedDistroOperation stagedDistroOperation; |
| 181 | try { |
| 182 | stagedDistroOperation = mInstaller.getStagedDistroOperation(); |
| 183 | if (stagedDistroOperation == null) { |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 184 | stagedOperationStatus = STAGED_OPERATION_NONE; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 185 | } else if (stagedDistroOperation.isUninstall) { |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 186 | stagedOperationStatus = STAGED_OPERATION_UNINSTALL; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 187 | } else { |
| 188 | // Must be an install. |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 189 | stagedOperationStatus = STAGED_OPERATION_INSTALL; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 190 | DistroVersion stagedDistroVersion = stagedDistroOperation.distroVersion; |
| 191 | stagedDistroRulesVersion = new DistroRulesVersion( |
| 192 | stagedDistroVersion.rulesVersion, |
| 193 | stagedDistroVersion.revision); |
| 194 | } |
| 195 | } catch (DistroException | IOException e) { |
| 196 | Slog.w(TAG, "Failed to read staged distro.", e); |
| 197 | } |
| 198 | } |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 199 | return new RulesState(systemRulesVersion, DISTRO_FORMAT_VERSION_SUPPORTED, |
| 200 | operationInProgress, stagedOperationStatus, stagedDistroRulesVersion, |
| 201 | distroStatus, installedDistroRulesVersion); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | @Override |
Neil Fuller | 54525bf | 2017-06-22 14:10:29 +0100 | [diff] [blame] | 206 | public int requestInstall(ParcelFileDescriptor distroParcelFileDescriptor, |
| 207 | byte[] checkTokenBytes, ICallback callback) { |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 208 | |
Neil Fuller | 54525bf | 2017-06-22 14:10:29 +0100 | [diff] [blame] | 209 | boolean closeParcelFileDescriptorOnExit = true; |
| 210 | try { |
| 211 | mPermissionHelper.enforceCallerHasPermission(REQUIRED_UPDATER_PERMISSION); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 212 | |
Neil Fuller | 54525bf | 2017-06-22 14:10:29 +0100 | [diff] [blame] | 213 | CheckToken checkToken = null; |
| 214 | if (checkTokenBytes != null) { |
| 215 | checkToken = createCheckTokenOrThrow(checkTokenBytes); |
| 216 | } |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 217 | EventLogTags.writeTimezoneRequestInstall(toStringOrNull(checkToken)); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 218 | |
Neil Fuller | 54525bf | 2017-06-22 14:10:29 +0100 | [diff] [blame] | 219 | synchronized (this) { |
| 220 | if (distroParcelFileDescriptor == null) { |
| 221 | throw new NullPointerException("distroParcelFileDescriptor == null"); |
| 222 | } |
| 223 | if (callback == null) { |
| 224 | throw new NullPointerException("observer == null"); |
| 225 | } |
| 226 | if (mOperationInProgress.get()) { |
| 227 | return RulesManager.ERROR_OPERATION_IN_PROGRESS; |
| 228 | } |
| 229 | mOperationInProgress.set(true); |
| 230 | |
| 231 | // Execute the install asynchronously. |
| 232 | mExecutor.execute( |
| 233 | new InstallRunnable(distroParcelFileDescriptor, checkToken, callback)); |
| 234 | |
| 235 | // The InstallRunnable now owns the ParcelFileDescriptor, so it will close it after |
| 236 | // it executes (and we do not have to). |
| 237 | closeParcelFileDescriptorOnExit = false; |
| 238 | |
| 239 | return RulesManager.SUCCESS; |
| 240 | } |
| 241 | } finally { |
| 242 | // We should close() the local ParcelFileDescriptor we were passed if it hasn't been |
| 243 | // passed to another thread to handle. |
| 244 | if (distroParcelFileDescriptor != null && closeParcelFileDescriptorOnExit) { |
| 245 | try { |
| 246 | distroParcelFileDescriptor.close(); |
| 247 | } catch (IOException e) { |
| 248 | Slog.w(TAG, "Failed to close distroParcelFileDescriptor", e); |
| 249 | } |
| 250 | } |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | private class InstallRunnable implements Runnable { |
| 255 | |
Neil Fuller | 54525bf | 2017-06-22 14:10:29 +0100 | [diff] [blame] | 256 | private final ParcelFileDescriptor mDistroParcelFileDescriptor; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 257 | private final CheckToken mCheckToken; |
| 258 | private final ICallback mCallback; |
| 259 | |
Neil Fuller | 54525bf | 2017-06-22 14:10:29 +0100 | [diff] [blame] | 260 | InstallRunnable(ParcelFileDescriptor distroParcelFileDescriptor, CheckToken checkToken, |
| 261 | ICallback callback) { |
| 262 | mDistroParcelFileDescriptor = distroParcelFileDescriptor; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 263 | mCheckToken = checkToken; |
| 264 | mCallback = callback; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public void run() { |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 269 | EventLogTags.writeTimezoneInstallStarted(toStringOrNull(mCheckToken)); |
| 270 | |
Neil Fuller | 54525bf | 2017-06-22 14:10:29 +0100 | [diff] [blame] | 271 | boolean success = false; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 272 | // Adopt the ParcelFileDescriptor into this try-with-resources so it is closed |
| 273 | // when we are done. |
Neil Fuller | 54525bf | 2017-06-22 14:10:29 +0100 | [diff] [blame] | 274 | try (ParcelFileDescriptor pfd = mDistroParcelFileDescriptor) { |
| 275 | // The ParcelFileDescriptor owns the underlying FileDescriptor and we'll close |
| 276 | // it at the end of the try-with-resources. |
| 277 | final boolean isFdOwner = false; |
| 278 | InputStream is = new FileInputStream(pfd.getFileDescriptor(), isFdOwner); |
| 279 | |
| 280 | TimeZoneDistro distro = new TimeZoneDistro(is); |
Neil Fuller | fe3b118 | 2017-06-19 12:56:08 +0100 | [diff] [blame] | 281 | int installerResult = mInstaller.stageInstallWithErrorCode(distro); |
Neil Fuller | b144227 | 2017-12-18 15:59:50 +0000 | [diff] [blame] | 282 | |
| 283 | // Notify interested parties that something is staged. |
| 284 | sendInstallNotificationIntentIfRequired(installerResult); |
| 285 | |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 286 | int resultCode = mapInstallerResultToApiCode(installerResult); |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 287 | EventLogTags.writeTimezoneInstallComplete(toStringOrNull(mCheckToken), resultCode); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 288 | sendFinishedStatus(mCallback, resultCode); |
| 289 | |
| 290 | // All the installer failure modes are currently non-recoverable and won't be |
| 291 | // improved by trying again. Therefore success = true. |
| 292 | success = true; |
| 293 | } catch (Exception e) { |
| 294 | Slog.w(TAG, "Failed to install distro.", e); |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 295 | EventLogTags.writeTimezoneInstallComplete( |
| 296 | toStringOrNull(mCheckToken), Callback.ERROR_UNKNOWN_FAILURE); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 297 | sendFinishedStatus(mCallback, Callback.ERROR_UNKNOWN_FAILURE); |
| 298 | } finally { |
| 299 | // Notify the package tracker that the operation is now complete. |
| 300 | mPackageTracker.recordCheckResult(mCheckToken, success); |
| 301 | |
| 302 | mOperationInProgress.set(false); |
| 303 | } |
| 304 | } |
| 305 | |
Neil Fuller | b144227 | 2017-12-18 15:59:50 +0000 | [diff] [blame] | 306 | private void sendInstallNotificationIntentIfRequired(int installerResult) { |
| 307 | if (installerResult == TimeZoneDistroInstaller.INSTALL_SUCCESS) { |
| 308 | mIntentHelper.sendTimeZoneOperationStaged(); |
| 309 | } |
| 310 | } |
| 311 | |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 312 | private int mapInstallerResultToApiCode(int installerResult) { |
| 313 | switch (installerResult) { |
| 314 | case TimeZoneDistroInstaller.INSTALL_SUCCESS: |
| 315 | return Callback.SUCCESS; |
| 316 | case TimeZoneDistroInstaller.INSTALL_FAIL_BAD_DISTRO_STRUCTURE: |
| 317 | return Callback.ERROR_INSTALL_BAD_DISTRO_STRUCTURE; |
| 318 | case TimeZoneDistroInstaller.INSTALL_FAIL_RULES_TOO_OLD: |
| 319 | return Callback.ERROR_INSTALL_RULES_TOO_OLD; |
| 320 | case TimeZoneDistroInstaller.INSTALL_FAIL_BAD_DISTRO_FORMAT_VERSION: |
| 321 | return Callback.ERROR_INSTALL_BAD_DISTRO_FORMAT_VERSION; |
| 322 | case TimeZoneDistroInstaller.INSTALL_FAIL_VALIDATION_ERROR: |
| 323 | return Callback.ERROR_INSTALL_VALIDATION_ERROR; |
| 324 | default: |
| 325 | return Callback.ERROR_UNKNOWN_FAILURE; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public int requestUninstall(byte[] checkTokenBytes, ICallback callback) { |
| 332 | mPermissionHelper.enforceCallerHasPermission(REQUIRED_UPDATER_PERMISSION); |
| 333 | |
| 334 | CheckToken checkToken = null; |
| 335 | if (checkTokenBytes != null) { |
| 336 | checkToken = createCheckTokenOrThrow(checkTokenBytes); |
| 337 | } |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 338 | EventLogTags.writeTimezoneRequestUninstall(toStringOrNull(checkToken)); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 339 | synchronized(this) { |
| 340 | if (callback == null) { |
| 341 | throw new NullPointerException("callback == null"); |
| 342 | } |
| 343 | |
| 344 | if (mOperationInProgress.get()) { |
| 345 | return RulesManager.ERROR_OPERATION_IN_PROGRESS; |
| 346 | } |
| 347 | mOperationInProgress.set(true); |
| 348 | |
| 349 | // Execute the uninstall asynchronously. |
| 350 | mExecutor.execute(new UninstallRunnable(checkToken, callback)); |
| 351 | |
| 352 | return RulesManager.SUCCESS; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | private class UninstallRunnable implements Runnable { |
| 357 | |
| 358 | private final CheckToken mCheckToken; |
| 359 | private final ICallback mCallback; |
| 360 | |
Neil Fuller | a47c363 | 2017-07-26 13:56:25 +0100 | [diff] [blame] | 361 | UninstallRunnable(CheckToken checkToken, ICallback callback) { |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 362 | mCheckToken = checkToken; |
| 363 | mCallback = callback; |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public void run() { |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 368 | EventLogTags.writeTimezoneUninstallStarted(toStringOrNull(mCheckToken)); |
Neil Fuller | 8e27c92 | 2017-09-14 09:34:56 +0100 | [diff] [blame] | 369 | boolean packageTrackerStatus = false; |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 370 | try { |
Neil Fuller | 8e27c92 | 2017-09-14 09:34:56 +0100 | [diff] [blame] | 371 | int uninstallResult = mInstaller.stageUninstall(); |
Neil Fuller | b144227 | 2017-12-18 15:59:50 +0000 | [diff] [blame] | 372 | |
| 373 | // Notify interested parties that something is staged. |
| 374 | sendUninstallNotificationIntentIfRequired(uninstallResult); |
| 375 | |
Neil Fuller | 8e27c92 | 2017-09-14 09:34:56 +0100 | [diff] [blame] | 376 | packageTrackerStatus = (uninstallResult == TimeZoneDistroInstaller.UNINSTALL_SUCCESS |
| 377 | || uninstallResult == TimeZoneDistroInstaller.UNINSTALL_NOTHING_INSTALLED); |
| 378 | |
| 379 | // Right now we just have Callback.SUCCESS / Callback.ERROR_UNKNOWN_FAILURE for |
| 380 | // uninstall. All clients should be checking against SUCCESS. More granular failures |
| 381 | // may be added in future. |
| 382 | int callbackResultCode = |
| 383 | packageTrackerStatus ? Callback.SUCCESS : Callback.ERROR_UNKNOWN_FAILURE; |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 384 | EventLogTags.writeTimezoneUninstallComplete( |
Neil Fuller | 8e27c92 | 2017-09-14 09:34:56 +0100 | [diff] [blame] | 385 | toStringOrNull(mCheckToken), callbackResultCode); |
| 386 | sendFinishedStatus(mCallback, callbackResultCode); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 387 | } catch (Exception e) { |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 388 | EventLogTags.writeTimezoneUninstallComplete( |
| 389 | toStringOrNull(mCheckToken), Callback.ERROR_UNKNOWN_FAILURE); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 390 | Slog.w(TAG, "Failed to uninstall distro.", e); |
| 391 | sendFinishedStatus(mCallback, Callback.ERROR_UNKNOWN_FAILURE); |
| 392 | } finally { |
| 393 | // Notify the package tracker that the operation is now complete. |
Neil Fuller | 8e27c92 | 2017-09-14 09:34:56 +0100 | [diff] [blame] | 394 | mPackageTracker.recordCheckResult(mCheckToken, packageTrackerStatus); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 395 | |
| 396 | mOperationInProgress.set(false); |
| 397 | } |
| 398 | } |
Neil Fuller | b144227 | 2017-12-18 15:59:50 +0000 | [diff] [blame] | 399 | |
| 400 | private void sendUninstallNotificationIntentIfRequired(int uninstallResult) { |
| 401 | switch (uninstallResult) { |
| 402 | case TimeZoneDistroInstaller.UNINSTALL_SUCCESS: |
| 403 | mIntentHelper.sendTimeZoneOperationStaged(); |
| 404 | break; |
| 405 | case TimeZoneDistroInstaller.UNINSTALL_NOTHING_INSTALLED: |
| 406 | mIntentHelper.sendTimeZoneOperationUnstaged(); |
| 407 | break; |
| 408 | case TimeZoneDistroInstaller.UNINSTALL_FAIL: |
| 409 | default: |
| 410 | // No-op - unknown or nothing to notify about. |
| 411 | } |
| 412 | } |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | private void sendFinishedStatus(ICallback callback, int resultCode) { |
| 416 | try { |
| 417 | callback.onFinished(resultCode); |
| 418 | } catch (RemoteException e) { |
| 419 | Slog.e(TAG, "Unable to notify observer of result", e); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public void requestNothing(byte[] checkTokenBytes, boolean success) { |
| 425 | mPermissionHelper.enforceCallerHasPermission(REQUIRED_UPDATER_PERMISSION); |
| 426 | CheckToken checkToken = null; |
| 427 | if (checkTokenBytes != null) { |
| 428 | checkToken = createCheckTokenOrThrow(checkTokenBytes); |
| 429 | } |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 430 | EventLogTags.writeTimezoneRequestNothing(toStringOrNull(checkToken)); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 431 | mPackageTracker.recordCheckResult(checkToken, success); |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 432 | EventLogTags.writeTimezoneNothingComplete(toStringOrNull(checkToken)); |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 435 | @Override |
| 436 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 437 | if (!mPermissionHelper.checkDumpPermission(TAG, pw)) { |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | RulesState rulesState = getRulesStateInternal(); |
| 442 | if (args != null && args.length == 2) { |
| 443 | // Formatting options used for automated tests. The format is less free-form than |
| 444 | // the -format options, which are intended to be easier to parse. |
| 445 | if ("-format_state".equals(args[0]) && args[1] != null) { |
| 446 | for (char c : args[1].toCharArray()) { |
| 447 | switch (c) { |
Neil Fuller | a47c363 | 2017-07-26 13:56:25 +0100 | [diff] [blame] | 448 | case 'p': { |
| 449 | // Report operation in progress |
| 450 | String value = "Unknown"; |
| 451 | if (rulesState != null) { |
| 452 | value = Boolean.toString(rulesState.isOperationInProgress()); |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 453 | } |
Neil Fuller | a47c363 | 2017-07-26 13:56:25 +0100 | [diff] [blame] | 454 | pw.println("Operation in progress: " + value); |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 455 | break; |
Neil Fuller | a47c363 | 2017-07-26 13:56:25 +0100 | [diff] [blame] | 456 | } |
| 457 | case 's': { |
| 458 | // Report system image rules version |
| 459 | String value = "Unknown"; |
| 460 | if (rulesState != null) { |
| 461 | value = rulesState.getSystemRulesVersion(); |
| 462 | } |
| 463 | pw.println("System rules version: " + value); |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 464 | break; |
Neil Fuller | a47c363 | 2017-07-26 13:56:25 +0100 | [diff] [blame] | 465 | } |
| 466 | case 'c': { |
| 467 | // Report current installation state |
| 468 | String value = "Unknown"; |
| 469 | if (rulesState != null) { |
| 470 | value = distroStatusToString(rulesState.getDistroStatus()); |
| 471 | } |
| 472 | pw.println("Current install state: " + value); |
| 473 | break; |
| 474 | } |
| 475 | case 'i': { |
| 476 | // Report currently installed version |
| 477 | String value = "Unknown"; |
| 478 | if (rulesState != null) { |
| 479 | DistroRulesVersion installedRulesVersion = |
| 480 | rulesState.getInstalledDistroRulesVersion(); |
| 481 | if (installedRulesVersion == null) { |
| 482 | value = "<None>"; |
| 483 | } else { |
| 484 | value = installedRulesVersion.toDumpString(); |
| 485 | } |
| 486 | } |
| 487 | pw.println("Installed rules version: " + value); |
| 488 | break; |
| 489 | } |
| 490 | case 'o': { |
| 491 | // Report staged operation type |
| 492 | String value = "Unknown"; |
| 493 | if (rulesState != null) { |
| 494 | int stagedOperationType = rulesState.getStagedOperationType(); |
| 495 | value = stagedOperationToString(stagedOperationType); |
| 496 | } |
| 497 | pw.println("Staged operation: " + value); |
| 498 | break; |
| 499 | } |
| 500 | case 't': { |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 501 | // Report staged version (i.e. the one that will be installed next boot |
| 502 | // if the staged operation is an install). |
Neil Fuller | a47c363 | 2017-07-26 13:56:25 +0100 | [diff] [blame] | 503 | String value = "Unknown"; |
| 504 | if (rulesState != null) { |
| 505 | DistroRulesVersion stagedDistroRulesVersion = |
| 506 | rulesState.getStagedDistroRulesVersion(); |
| 507 | if (stagedDistroRulesVersion == null) { |
| 508 | value = "<None>"; |
| 509 | } else { |
| 510 | value = stagedDistroRulesVersion.toDumpString(); |
| 511 | } |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 512 | } |
Neil Fuller | a47c363 | 2017-07-26 13:56:25 +0100 | [diff] [blame] | 513 | pw.println("Staged rules version: " + value); |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 514 | break; |
Neil Fuller | a47c363 | 2017-07-26 13:56:25 +0100 | [diff] [blame] | 515 | } |
| 516 | case 'a': { |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 517 | // Report the active rules version (i.e. the rules in use by the current |
| 518 | // process). |
Neil Fuller | c903614 | 2017-10-04 17:04:38 +0100 | [diff] [blame] | 519 | pw.println("Active rules version (ICU, ZoneInfoDB, TimeZoneFinder): " |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 520 | + ICU.getTZDataVersion() + "," |
Neil Fuller | c903614 | 2017-10-04 17:04:38 +0100 | [diff] [blame] | 521 | + ZoneInfoDB.getInstance().getVersion() + "," |
| 522 | + TimeZoneFinder.getInstance().getIanaVersion()); |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 523 | break; |
Neil Fuller | a47c363 | 2017-07-26 13:56:25 +0100 | [diff] [blame] | 524 | } |
| 525 | default: { |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 526 | pw.println("Unknown option: " + c); |
Neil Fuller | a47c363 | 2017-07-26 13:56:25 +0100 | [diff] [blame] | 527 | } |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | return; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | pw.println("RulesManagerService state: " + toString()); |
Neil Fuller | c903614 | 2017-10-04 17:04:38 +0100 | [diff] [blame] | 535 | pw.println("Active rules version (ICU, ZoneInfoDB, TimeZoneFinder): " |
| 536 | + ICU.getTZDataVersion() + "," |
| 537 | + ZoneInfoDB.getInstance().getVersion() + "," |
| 538 | + TimeZoneFinder.getInstance().getIanaVersion()); |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 539 | pw.println("Distro state: " + rulesState.toString()); |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 540 | mPackageTracker.dump(pw); |
| 541 | } |
| 542 | |
Neil Fuller | cd1a109 | 2017-09-13 21:59:59 +0100 | [diff] [blame] | 543 | /** |
| 544 | * Called when the device is considered idle. |
| 545 | */ |
| 546 | void notifyIdle() { |
| 547 | // No package has changed: we are just triggering because the device is idle and there |
| 548 | // *might* be work to do. |
| 549 | final boolean packageChanged = false; |
| 550 | mPackageTracker.triggerUpdateIfNeeded(packageChanged); |
| 551 | } |
| 552 | |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 553 | @Override |
| 554 | public String toString() { |
| 555 | return "RulesManagerService{" + |
| 556 | "mOperationInProgress=" + mOperationInProgress + |
| 557 | '}'; |
| 558 | } |
| 559 | |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 560 | private static CheckToken createCheckTokenOrThrow(byte[] checkTokenBytes) { |
| 561 | CheckToken checkToken; |
| 562 | try { |
| 563 | checkToken = CheckToken.fromByteArray(checkTokenBytes); |
| 564 | } catch (IOException e) { |
| 565 | throw new IllegalArgumentException("Unable to read token bytes " |
| 566 | + Arrays.toString(checkTokenBytes), e); |
| 567 | } |
| 568 | return checkToken; |
| 569 | } |
Neil Fuller | 87b1128 | 2017-06-23 16:43:45 +0100 | [diff] [blame] | 570 | |
| 571 | private static String distroStatusToString(int distroStatus) { |
| 572 | switch(distroStatus) { |
| 573 | case DISTRO_STATUS_NONE: |
| 574 | return "None"; |
| 575 | case DISTRO_STATUS_INSTALLED: |
| 576 | return "Installed"; |
| 577 | case DISTRO_STATUS_UNKNOWN: |
| 578 | default: |
| 579 | return "Unknown"; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | private static String stagedOperationToString(int stagedOperationType) { |
| 584 | switch(stagedOperationType) { |
| 585 | case STAGED_OPERATION_NONE: |
| 586 | return "None"; |
| 587 | case STAGED_OPERATION_UNINSTALL: |
| 588 | return "Uninstall"; |
| 589 | case STAGED_OPERATION_INSTALL: |
| 590 | return "Install"; |
| 591 | case STAGED_OPERATION_UNKNOWN: |
| 592 | default: |
| 593 | return "Unknown"; |
| 594 | } |
| 595 | } |
Neil Fuller | d857f67 | 2017-07-20 11:00:35 +0100 | [diff] [blame] | 596 | |
| 597 | private static String toStringOrNull(Object obj) { |
| 598 | return obj == null ? null : obj.toString(); |
| 599 | } |
Neil Fuller | 68f6666 | 2017-03-16 18:32:21 +0000 | [diff] [blame] | 600 | } |