Fixes how unbindService works

We shouldn't create a new object to do unbinding.

Bug: 172780686
Test: TreeHugger; try server based ror, no exception for unbind

Change-Id: I4f3cc29380125c21ae7a3b975282244a4de25db9
This commit is contained in:
Jiachen Zhao
2021-01-26 16:22:52 -08:00
committed by Tianjie
parent 2befe56df0
commit cf7b9b9a3d

View File

@@ -106,6 +106,8 @@ public class ResumeOnRebootServiceProvider {
private final Context mContext;
private final ComponentName mComponentName;
private IResumeOnRebootService mBinder;
@Nullable
ServiceConnection mServiceConnection;
private ResumeOnRebootServiceConnection(Context context,
@NonNull ComponentName componentName) {
@@ -115,17 +117,9 @@ public class ResumeOnRebootServiceProvider {
/** Unbind from the service */
public void unbindService() {
mContext.unbindService(new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBinder = null;
}
});
if (mServiceConnection != null) {
mContext.unbindService(mServiceConnection);
}
}
/** Bind to the service */
@@ -134,17 +128,19 @@ public class ResumeOnRebootServiceProvider {
CountDownLatch connectionLatch = new CountDownLatch(1);
Intent intent = new Intent();
intent.setComponent(mComponentName);
final boolean success = mContext.bindServiceAsUser(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBinder = IResumeOnRebootService.Stub.asInterface(service);
connectionLatch.countDown();
}
mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBinder = IResumeOnRebootService.Stub.asInterface(service);
connectionLatch.countDown();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
},
@Override
public void onServiceDisconnected(ComponentName name) {
mBinder = null;
}
};
final boolean success = mContext.bindServiceAsUser(intent, mServiceConnection,
Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,
BackgroundThread.getHandler(), UserHandle.SYSTEM);