Handle null callbacks, add missing breaks.

The ISystemDataTransferCallback argument can be null, so quietly
handle that case.  Also fix bug where we forgot "break" statements
when processing responses.

Finally, always detach transport before closing streams to avoid
misleading warning logs being emitted from system.

Bug: 237030169
Test: manual
Change-Id: Id6d2d93a1c6872152112f877a2b92f4656277803
This commit is contained in:
Jeff Sharkey
2022-06-30 14:46:09 -06:00
parent 442896b4be
commit 7ae966c3f9
3 changed files with 33 additions and 21 deletions

View File

@@ -1161,17 +1161,17 @@ public final class CompanionDeviceManager {
public void stop() {
mStopped = true;
IoUtils.closeQuietly(mRemoteIn);
IoUtils.closeQuietly(mRemoteOut);
IoUtils.closeQuietly(mLocalIn);
IoUtils.closeQuietly(mLocalOut);
try {
mService.detachSystemDataTransport(mContext.getPackageName(),
mContext.getUserId(), mAssociationId);
} catch (RemoteException e) {
Log.w(LOG_TAG, "Failed to detach transport", e);
}
IoUtils.closeQuietly(mRemoteIn);
IoUtils.closeQuietly(mRemoteOut);
IoUtils.closeQuietly(mLocalIn);
IoUtils.closeQuietly(mLocalOut);
}
/**

View File

@@ -25,6 +25,7 @@ import static android.content.ComponentName.createRelative;
import static com.android.server.companion.Utils.prepareForIpc;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.PendingIntent;
import android.companion.AssociationInfo;
@@ -191,20 +192,9 @@ public class SystemDataTransferProcessor {
// TODO: refactor to work with streams of data
mPermissionControllerManager.getRuntimePermissionBackup(UserHandle.of(userId),
mExecutor, backup -> {
Future<?> result = mTransportManager
Future<?> future = mTransportManager
.requestPermissionRestore(associationId, backup);
try {
result.get(15, TimeUnit.SECONDS);
try {
callback.onResult();
} catch (RemoteException ignored) {
}
} catch (Exception e) {
try {
callback.onError(e.getMessage());
} catch (RemoteException ignored) {
}
}
translateFutureToCallback(future, callback);
});
} finally {
Binder.restoreCallingIdentity(callingIdentityToken);
@@ -225,6 +215,26 @@ public class SystemDataTransferProcessor {
}
}
private static void translateFutureToCallback(@NonNull Future<?> future,
@Nullable ISystemDataTransferCallback callback) {
try {
future.get(15, TimeUnit.SECONDS);
try {
if (callback != null) {
callback.onResult();
}
} catch (RemoteException ignored) {
}
} catch (Exception e) {
try {
if (callback != null) {
callback.onError(e.getMessage());
}
} catch (RemoteException ignored) {
}
}
}
private final ResultReceiver mOnSystemDataTransferRequestConfirmationReceiver =
new ResultReceiver(Handler.getMain()) {
@Override

View File

@@ -260,7 +260,7 @@ public class CompanionTransportManager {
} else if (isResponse(message)) {
processResponse(message, sequence, data);
} else {
Slog.w(TAG, "Unknown message " + Integer.toHexString(message));
Slog.w(TAG, "Unknown message 0x" + Integer.toHexString(message));
}
}
@@ -288,7 +288,7 @@ public class CompanionTransportManager {
break;
}
default: {
Slog.w(TAG, "Unknown request " + Integer.toHexString(message));
Slog.w(TAG, "Unknown request 0x" + Integer.toHexString(message));
sendMessage(MESSAGE_RESPONSE_FAILURE, sequence, EmptyArray.BYTE);
break;
}
@@ -308,12 +308,14 @@ public class CompanionTransportManager {
switch (message) {
case MESSAGE_RESPONSE_SUCCESS: {
future.complete(data);
break;
}
case MESSAGE_RESPONSE_FAILURE: {
future.completeExceptionally(new RuntimeException("Remote failure"));
break;
}
default: {
Slog.w(TAG, "Ignoring unknown response " + Integer.toHexString(message));
Slog.w(TAG, "Ignoring unknown response 0x" + Integer.toHexString(message));
}
}
}