Merge "Handle bindService errors in ObservableServiceConnection" into tm-qpr-dev am: 5846d27755
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/21853359 Change-Id: I0d3cfc9afabd5a65de8b4b749d52a051c49396cc Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -143,7 +143,13 @@ public class ObservableServiceConnection<T> implements ServiceConnection {
|
|||||||
* @return {@code true} if initiating binding succeed, {@code false} otherwise.
|
* @return {@code true} if initiating binding succeed, {@code false} otherwise.
|
||||||
*/
|
*/
|
||||||
public boolean bind() {
|
public boolean bind() {
|
||||||
final boolean bindResult = mContext.bindService(mServiceIntent, mFlags, mExecutor, this);
|
boolean bindResult = false;
|
||||||
|
try {
|
||||||
|
bindResult = mContext.bindService(mServiceIntent, mFlags, mExecutor, this);
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
Log.d(TAG, "Could not bind to service", e);
|
||||||
|
mContext.unbindService(this);
|
||||||
|
}
|
||||||
mBoundCalled = true;
|
mBoundCalled = true;
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, "bind. bound:" + bindResult);
|
Log.d(TAG, "bind. bound:" + bindResult);
|
||||||
@@ -197,7 +203,7 @@ public class ObservableServiceConnection<T> implements ServiceConnection {
|
|||||||
Log.d(TAG, "removeCallback:" + callback);
|
Log.d(TAG, "removeCallback:" + callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
mExecutor.execute(()-> mCallbacks.removeIf(el -> el.get() == callback));
|
mExecutor.execute(() -> mCallbacks.removeIf(el-> el.get() == callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onDisconnected(@DisconnectReason int reason) {
|
private void onDisconnected(@DisconnectReason int reason) {
|
||||||
|
|||||||
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package com.android.systemui.util.service;
|
package com.android.systemui.util.service;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.anyInt;
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.clearInvocations;
|
import static org.mockito.Mockito.clearInvocations;
|
||||||
@@ -169,4 +171,19 @@ public class ObservableServiceConnectionTest extends SysuiTestCase {
|
|||||||
verify(mCallback).onDisconnected(eq(connection),
|
verify(mCallback).onDisconnected(eq(connection),
|
||||||
eq(ObservableServiceConnection.DISCONNECT_REASON_UNBIND));
|
eq(ObservableServiceConnection.DISCONNECT_REASON_UNBIND));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBindServiceThrowsError() {
|
||||||
|
ObservableServiceConnection<Foo> connection = new ObservableServiceConnection<>(mContext,
|
||||||
|
mIntent, mExecutor, mTransformer);
|
||||||
|
connection.addCallback(mCallback);
|
||||||
|
|
||||||
|
when(mContext.bindService(eq(mIntent), anyInt(), eq(mExecutor), eq(connection)))
|
||||||
|
.thenThrow(new SecurityException());
|
||||||
|
|
||||||
|
// Verify that the exception was caught and that bind returns false, and we properly
|
||||||
|
// unbind.
|
||||||
|
assertThat(connection.bind()).isFalse();
|
||||||
|
verify(mContext).unbindService(connection);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user