1 /* 2 * Copyright (C) 2022 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.transport; 18 19 import android.app.backup.BackupTransport; 20 import android.os.RemoteException; 21 import android.util.Slog; 22 23 import com.android.internal.annotations.GuardedBy; 24 import com.android.internal.annotations.VisibleForTesting; 25 import com.android.internal.backup.ITransportStatusCallback; 26 import com.android.server.backup.BackupAndRestoreFeatureFlags; 27 28 public class TransportStatusCallback extends ITransportStatusCallback.Stub { 29 private static final String TAG = "TransportStatusCallback"; 30 private static final int OPERATION_STATUS_DEFAULT = 0; 31 32 private final long mOperationTimeout; 33 34 @GuardedBy("this") 35 private int mOperationStatus = OPERATION_STATUS_DEFAULT; 36 @GuardedBy("this") 37 private boolean mHasCompletedOperation = false; 38 TransportStatusCallback()39 public TransportStatusCallback() { 40 mOperationTimeout = BackupAndRestoreFeatureFlags.getBackupTransportCallbackTimeoutMillis(); 41 } 42 43 @VisibleForTesting TransportStatusCallback(int operationTimeout)44 TransportStatusCallback(int operationTimeout) { 45 mOperationTimeout = operationTimeout; 46 } 47 48 @Override onOperationCompleteWithStatus(int status)49 public synchronized void onOperationCompleteWithStatus(int status) throws RemoteException { 50 mHasCompletedOperation = true; 51 mOperationStatus = status; 52 53 notifyAll(); 54 } 55 56 @Override onOperationComplete()57 public synchronized void onOperationComplete() throws RemoteException { 58 onOperationCompleteWithStatus(OPERATION_STATUS_DEFAULT); 59 } 60 getOperationStatus()61 synchronized int getOperationStatus() { 62 if (mHasCompletedOperation) { 63 return mOperationStatus; 64 } 65 66 long timeoutLeft = mOperationTimeout; 67 try { 68 while (!mHasCompletedOperation && timeoutLeft > 0) { 69 long waitStartTime = System.currentTimeMillis(); 70 wait(timeoutLeft); 71 if (mHasCompletedOperation) { 72 return mOperationStatus; 73 } 74 timeoutLeft -= System.currentTimeMillis() - waitStartTime; 75 } 76 77 Slog.w(TAG, "Couldn't get operation status from transport"); 78 } catch (InterruptedException e) { 79 Slog.w(TAG, "Couldn't get operation status from transport: ", e); 80 } 81 82 return BackupTransport.TRANSPORT_ERROR; 83 } 84 reset()85 synchronized void reset() { 86 mHasCompletedOperation = false; 87 mOperationStatus = OPERATION_STATUS_DEFAULT; 88 } 89 } 90