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.backup.restore; 18 19 import static com.android.server.backup.BackupManagerService.DEBUG; 20 import static com.android.server.backup.BackupManagerService.MORE_DEBUG; 21 22 import android.util.Slog; 23 24 import com.android.internal.util.Preconditions; 25 import com.android.server.backup.BackupAgentTimeoutParameters; 26 import com.android.server.backup.BackupManagerService; 27 import com.android.server.backup.BackupRestoreTask; 28 29 import java.util.concurrent.CountDownLatch; 30 import java.util.concurrent.TimeUnit; 31 32 /** 33 * Used for synchronizing doRestoreFinished during adb restore. 34 */ 35 public class AdbRestoreFinishedLatch implements BackupRestoreTask { 36 37 private static final String TAG = "AdbRestoreFinishedLatch"; 38 private BackupManagerService backupManagerService; 39 final CountDownLatch mLatch; 40 private final int mCurrentOpToken; 41 private final BackupAgentTimeoutParameters mAgentTimeoutParameters; 42 43 public AdbRestoreFinishedLatch(BackupManagerService backupManagerService, 44 int currentOpToken) { 45 this.backupManagerService = backupManagerService; 46 mLatch = new CountDownLatch(1); 47 mCurrentOpToken = currentOpToken; 48 mAgentTimeoutParameters = Preconditions.checkNotNull( 49 backupManagerService.getAgentTimeoutParameters(), 50 "Timeout parameters cannot be null"); 51 } 52 53 void await() { 54 boolean latched = false; 55 long fullBackupAgentTimeoutMillis = 56 mAgentTimeoutParameters.getFullBackupAgentTimeoutMillis(); 57 try { 58 latched = mLatch.await(fullBackupAgentTimeoutMillis, TimeUnit.MILLISECONDS); 59 } catch (InterruptedException e) { 60 Slog.w(TAG, "Interrupted!"); 61 } 62 } 63 64 @Override 65 public void execute() { 66 // Unused 67 } 68 69 @Override 70 public void operationComplete(long result) { 71 if (MORE_DEBUG) { 72 Slog.w(TAG, "adb onRestoreFinished() complete"); 73 } 74 mLatch.countDown(); 75 backupManagerService.removeOperation(mCurrentOpToken); 76 } 77 78 @Override 79 public void handleCancel(boolean cancelAll) { 80 if (DEBUG) { 81 Slog.w(TAG, "adb onRestoreFinished() timed out"); 82 } 83 mLatch.countDown(); 84 backupManagerService.removeOperation(mCurrentOpToken); 85 } 86 } 87