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