A few fixes for RotationResolver
This is a follow-up change of ag/13056186 to resolve some of the comments. Bug: 178405054 Test: unit tests Change-Id: I04015b432b9bac96561e779cc4aa76155e6168e9
This commit is contained in:
@@ -20,6 +20,7 @@ import android.annotation.NonNull;
|
||||
import android.annotation.SystemApi;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.view.Surface;
|
||||
|
||||
/**
|
||||
* This class represents a request to an {@link RotationResolverService}. The request contains
|
||||
@@ -54,7 +55,7 @@ public final class RotationResolutionRequest implements Parcelable {
|
||||
mTimeoutMillis = timeoutMillis;
|
||||
}
|
||||
|
||||
public int getProposedRotation() {
|
||||
@Surface.Rotation public int getProposedRotation() {
|
||||
return mProposedRotation;
|
||||
}
|
||||
|
||||
|
||||
@@ -146,11 +146,8 @@ public abstract class RotationResolverService extends Service {
|
||||
}
|
||||
mPendingCallback = new RotationResolverCallbackWrapper(callback, this);
|
||||
mCancellationSignal = CancellationSignal.fromTransport(transport);
|
||||
try {
|
||||
onResolveRotation(request, mCancellationSignal, mPendingCallback);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
reportFailures(callback, ROTATION_RESULT_FAILURE_CANCELLED);
|
||||
}
|
||||
|
||||
onResolveRotation(request, mCancellationSignal, mPendingCallback);
|
||||
}
|
||||
|
||||
@MainThread
|
||||
|
||||
@@ -45,6 +45,8 @@ import com.android.internal.annotations.GuardedBy;
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.internal.infra.ServiceConnector;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
|
||||
/** Manages the connection to the remote rotation resolver service. */
|
||||
class RemoteRotationResolverService extends ServiceConnector.Impl<IRotationResolverService> {
|
||||
@@ -128,13 +130,20 @@ class RemoteRotationResolverService extends ServiceConnector.Impl<IRotationResol
|
||||
mProposedRotation = proposedRotation;
|
||||
mCurrentRotation = currentRotation;
|
||||
mPackageName = packageName;
|
||||
mIRotationResolverCallback = new RotationResolverCallback();
|
||||
mIRotationResolverCallback = new RotationResolverCallback(this);
|
||||
mCancellationSignalInternal = cancellationSignal;
|
||||
mRequestStartTimeMillis = SystemClock.elapsedRealtime();
|
||||
}
|
||||
|
||||
|
||||
void cancelInternal() {
|
||||
synchronized (mLock) {
|
||||
if (mIsFulfilled) {
|
||||
Slog.v(TAG, "Trying to cancel the request that has been already fulfilled.");
|
||||
return;
|
||||
}
|
||||
mIsFulfilled = true;
|
||||
}
|
||||
Handler.getMain().post(() -> {
|
||||
synchronized (mLock) {
|
||||
try {
|
||||
@@ -147,9 +156,6 @@ class RemoteRotationResolverService extends ServiceConnector.Impl<IRotationResol
|
||||
}
|
||||
}
|
||||
});
|
||||
synchronized (mLock) {
|
||||
mIsFulfilled = true;
|
||||
}
|
||||
mCallbackInternal.onFailure(ROTATION_RESULT_FAILURE_CANCELLED);
|
||||
}
|
||||
|
||||
@@ -160,44 +166,53 @@ class RemoteRotationResolverService extends ServiceConnector.Impl<IRotationResol
|
||||
ipw.decreaseIndent();
|
||||
}
|
||||
|
||||
private class RotationResolverCallback extends IRotationResolverCallback.Stub {
|
||||
private static class RotationResolverCallback extends IRotationResolverCallback.Stub {
|
||||
private WeakReference<RotationRequest> mRequestWeakReference;
|
||||
|
||||
RotationResolverCallback(RotationRequest request) {
|
||||
this.mRequestWeakReference = new WeakReference<>(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(int rotation) {
|
||||
synchronized (mLock) {
|
||||
if (mIsFulfilled) {
|
||||
final RotationRequest request = mRequestWeakReference.get();
|
||||
synchronized (request.mLock) {
|
||||
if (request.mIsFulfilled) {
|
||||
Slog.w(TAG, "Callback received after the rotation request is fulfilled.");
|
||||
return;
|
||||
}
|
||||
mIsFulfilled = true;
|
||||
mCallbackInternal.onSuccess(rotation);
|
||||
request.mIsFulfilled = true;
|
||||
request.mCallbackInternal.onSuccess(rotation);
|
||||
final long timeToCalculate =
|
||||
SystemClock.elapsedRealtime() - mRequestStartTimeMillis;
|
||||
logRotationStats(mProposedRotation, mCurrentRotation, rotation,
|
||||
SystemClock.elapsedRealtime() - request.mRequestStartTimeMillis;
|
||||
logRotationStats(request.mProposedRotation, request.mCurrentRotation, rotation,
|
||||
timeToCalculate);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(int error) {
|
||||
synchronized (mLock) {
|
||||
if (mIsFulfilled) {
|
||||
final RotationRequest request = mRequestWeakReference.get();
|
||||
synchronized (request.mLock) {
|
||||
if (request.mIsFulfilled) {
|
||||
Slog.w(TAG, "Callback received after the rotation request is fulfilled.");
|
||||
return;
|
||||
}
|
||||
mIsFulfilled = true;
|
||||
mCallbackInternal.onFailure(error);
|
||||
request.mIsFulfilled = true;
|
||||
request.mCallbackInternal.onFailure(error);
|
||||
final long timeToCalculate =
|
||||
SystemClock.elapsedRealtime() - mRequestStartTimeMillis;
|
||||
logRotationStats(mProposedRotation, mCurrentRotation, RESOLUTION_FAILURE,
|
||||
timeToCalculate);
|
||||
SystemClock.elapsedRealtime() - request.mRequestStartTimeMillis;
|
||||
logRotationStats(request.mProposedRotation, request.mCurrentRotation,
|
||||
RESOLUTION_FAILURE, timeToCalculate);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancellable(@NonNull ICancellationSignal cancellation) {
|
||||
synchronized (mLock) {
|
||||
mCancellation = cancellation;
|
||||
if (mCancellationSignalInternal.isCanceled()) {
|
||||
final RotationRequest request = mRequestWeakReference.get();
|
||||
synchronized (request.mLock) {
|
||||
request.mCancellation = cancellation;
|
||||
if (request.mCancellationSignalInternal.isCanceled()) {
|
||||
// Dispatch the cancellation signal if the client has cancelled the request.
|
||||
try {
|
||||
cancellation.cancel();
|
||||
|
||||
@@ -122,14 +122,9 @@ final class RotationResolverManagerPerUserService extends
|
||||
}
|
||||
});
|
||||
|
||||
if (mRemoteService != null) {
|
||||
mRemoteService.resolveRotationLocked(mCurrentRequest);
|
||||
mCurrentRequest.mIsDispatched = true;
|
||||
} else {
|
||||
Slog.w(TAG, "Remote service is not available at this moment.");
|
||||
callbackInternal.onFailure(ROTATION_RESULT_FAILURE_CANCELLED);
|
||||
cancelLocked();
|
||||
}
|
||||
|
||||
mRemoteService.resolveRotationLocked(mCurrentRequest);
|
||||
mCurrentRequest.mIsDispatched = true;
|
||||
}
|
||||
|
||||
@GuardedBy("mLock")
|
||||
@@ -198,15 +193,6 @@ final class RotationResolverManagerPerUserService extends
|
||||
if (mCurrentRequest == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mCurrentRequest.mIsFulfilled) {
|
||||
if (isVerbose()) {
|
||||
Slog.d(TAG, "Trying to cancel the request that has been already fulfilled.");
|
||||
}
|
||||
mCurrentRequest = null;
|
||||
return;
|
||||
}
|
||||
|
||||
mCurrentRequest.cancelInternal();
|
||||
mCurrentRequest = null;
|
||||
}
|
||||
|
||||
@@ -191,7 +191,7 @@ public class RotationResolverManagerService extends
|
||||
TAG);
|
||||
final RotationResolverManagerPerUserService service = getServiceForUserLocked(
|
||||
UserHandle.getCallingUserId());
|
||||
new RotationResolverShellCommend(service).exec(this, in, out, err, args, callback,
|
||||
new RotationResolverShellCommand(service).exec(this, in, out, err, args, callback,
|
||||
resultReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,13 +26,13 @@ import android.view.Surface;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
|
||||
final class RotationResolverShellCommend extends ShellCommand {
|
||||
final class RotationResolverShellCommand extends ShellCommand {
|
||||
private static final int INITIAL_RESULT_CODE = -1;
|
||||
|
||||
@NonNull
|
||||
private final RotationResolverManagerPerUserService mService;
|
||||
|
||||
RotationResolverShellCommend(@NonNull RotationResolverManagerPerUserService service) {
|
||||
RotationResolverShellCommand(@NonNull RotationResolverManagerPerUserService service) {
|
||||
mService = service;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user