Merge "Unbind MBMS service after calling close"

am: d00563735f

Change-Id: I9392f5d1c2922d373b0aef812cbc43ba1ef05848
This commit is contained in:
Hall Liu
2019-10-02 15:11:20 -07:00
committed by android-build-merger
3 changed files with 196 additions and 157 deletions

View File

@@ -243,6 +243,7 @@ public class MbmsDownloadSession implements AutoCloseable {
}; };
private AtomicReference<IMbmsDownloadService> mService = new AtomicReference<>(null); private AtomicReference<IMbmsDownloadService> mService = new AtomicReference<>(null);
private ServiceConnection mServiceConnection;
private final InternalDownloadSessionCallback mInternalCallback; private final InternalDownloadSessionCallback mInternalCallback;
private final Map<DownloadStatusListener, InternalDownloadStatusListener> private final Map<DownloadStatusListener, InternalDownloadStatusListener>
mInternalDownloadStatusListeners = new HashMap<>(); mInternalDownloadStatusListeners = new HashMap<>();
@@ -318,56 +319,66 @@ public class MbmsDownloadSession implements AutoCloseable {
} }
private int bindAndInitialize() { private int bindAndInitialize() {
return MbmsUtils.startBinding(mContext, MBMS_DOWNLOAD_SERVICE_ACTION, mServiceConnection = new ServiceConnection() {
new ServiceConnection() { @Override
@Override public void onServiceConnected(ComponentName name, IBinder service) {
public void onServiceConnected(ComponentName name, IBinder service) { IMbmsDownloadService downloadService =
IMbmsDownloadService downloadService = IMbmsDownloadService.Stub.asInterface(service);
IMbmsDownloadService.Stub.asInterface(service); int result;
int result; try {
try { result = downloadService.initialize(mSubscriptionId, mInternalCallback);
result = downloadService.initialize(mSubscriptionId, mInternalCallback); } catch (RemoteException e) {
} catch (RemoteException e) { Log.e(LOG_TAG, "Service died before initialization");
Log.e(LOG_TAG, "Service died before initialization"); sIsInitialized.set(false);
sIsInitialized.set(false); return;
return; } catch (RuntimeException e) {
} catch (RuntimeException e) { Log.e(LOG_TAG, "Runtime exception during initialization");
Log.e(LOG_TAG, "Runtime exception during initialization"); sendErrorToApp(
sendErrorToApp( MbmsErrors.InitializationErrors.ERROR_UNABLE_TO_INITIALIZE,
MbmsErrors.InitializationErrors.ERROR_UNABLE_TO_INITIALIZE, e.toString());
e.toString()); sIsInitialized.set(false);
sIsInitialized.set(false); return;
return; }
} if (result == MbmsErrors.UNKNOWN) {
if (result == MbmsErrors.UNKNOWN) { // Unbind and throw an obvious error
// Unbind and throw an obvious error close();
close(); throw new IllegalStateException("Middleware must not return an"
throw new IllegalStateException("Middleware must not return an" + " unknown error code");
+ " unknown error code"); }
} if (result != MbmsErrors.SUCCESS) {
if (result != MbmsErrors.SUCCESS) { sendErrorToApp(result, "Error returned during initialization");
sendErrorToApp(result, "Error returned during initialization"); sIsInitialized.set(false);
sIsInitialized.set(false); return;
return; }
} try {
try { downloadService.asBinder().linkToDeath(mDeathRecipient, 0);
downloadService.asBinder().linkToDeath(mDeathRecipient, 0); } catch (RemoteException e) {
} catch (RemoteException e) { sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST,
sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST, "Middleware lost during initialization");
"Middleware lost during initialization"); sIsInitialized.set(false);
sIsInitialized.set(false); return;
return; }
} mService.set(downloadService);
mService.set(downloadService); }
}
@Override @Override
public void onServiceDisconnected(ComponentName name) { public void onServiceDisconnected(ComponentName name) {
Log.w(LOG_TAG, "bindAndInitialize: Remote service disconnected"); Log.w(LOG_TAG, "bindAndInitialize: Remote service disconnected");
sIsInitialized.set(false); sIsInitialized.set(false);
mService.set(null); mService.set(null);
} }
});
@Override
public void onNullBinding(ComponentName name) {
Log.w(LOG_TAG, "bindAndInitialize: Remote service returned null");
sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST,
"Middleware service binding returned null");
sIsInitialized.set(false);
mService.set(null);
mContext.unbindService(this);
}
};
return MbmsUtils.startBinding(mContext, MBMS_DOWNLOAD_SERVICE_ACTION, mServiceConnection);
} }
/** /**
@@ -965,17 +976,19 @@ public class MbmsDownloadSession implements AutoCloseable {
public void close() { public void close() {
try { try {
IMbmsDownloadService downloadService = mService.get(); IMbmsDownloadService downloadService = mService.get();
if (downloadService == null) { if (downloadService == null || mServiceConnection == null) {
Log.i(LOG_TAG, "Service already dead"); Log.i(LOG_TAG, "Service already dead");
return; return;
} }
downloadService.dispose(mSubscriptionId); downloadService.dispose(mSubscriptionId);
mContext.unbindService(mServiceConnection);
} catch (RemoteException e) { } catch (RemoteException e) {
// Ignore // Ignore
Log.i(LOG_TAG, "Remote exception while disposing of service"); Log.i(LOG_TAG, "Remote exception while disposing of service");
} finally { } finally {
mService.set(null); mService.set(null);
sIsInitialized.set(false); sIsInitialized.set(false);
mServiceConnection = null;
mInternalCallback.stop(); mInternalCallback.stop();
} }
} }

View File

@@ -80,6 +80,7 @@ public class MbmsGroupCallSession implements AutoCloseable {
}; };
private InternalGroupCallSessionCallback mInternalCallback; private InternalGroupCallSessionCallback mInternalCallback;
private ServiceConnection mServiceConnection;
private Set<GroupCall> mKnownActiveGroupCalls = new ArraySet<>(); private Set<GroupCall> mKnownActiveGroupCalls = new ArraySet<>();
private final Context mContext; private final Context mContext;
@@ -163,7 +164,7 @@ public class MbmsGroupCallSession implements AutoCloseable {
public void close() { public void close() {
try { try {
IMbmsGroupCallService groupCallService = mService.get(); IMbmsGroupCallService groupCallService = mService.get();
if (groupCallService == null) { if (groupCallService == null || mServiceConnection == null) {
// Ignore and return, assume already disposed. // Ignore and return, assume already disposed.
return; return;
} }
@@ -172,11 +173,13 @@ public class MbmsGroupCallSession implements AutoCloseable {
s.getCallback().stop(); s.getCallback().stop();
} }
mKnownActiveGroupCalls.clear(); mKnownActiveGroupCalls.clear();
mContext.unbindService(mServiceConnection);
} catch (RemoteException e) { } catch (RemoteException e) {
// Ignore for now // Ignore for now
} finally { } finally {
mService.set(null); mService.set(null);
sIsInitialized.set(false); sIsInitialized.set(false);
mServiceConnection = null;
mInternalCallback.stop(); mInternalCallback.stop();
} }
} }
@@ -244,59 +247,69 @@ public class MbmsGroupCallSession implements AutoCloseable {
} }
private int bindAndInitialize() { private int bindAndInitialize() {
return MbmsUtils.startBinding(mContext, MBMS_GROUP_CALL_SERVICE_ACTION, mServiceConnection = new ServiceConnection() {
new ServiceConnection() { @Override
@Override public void onServiceConnected(ComponentName name, IBinder service) {
public void onServiceConnected(ComponentName name, IBinder service) { IMbmsGroupCallService groupCallService =
IMbmsGroupCallService groupCallService = IMbmsGroupCallService.Stub.asInterface(service);
IMbmsGroupCallService.Stub.asInterface(service); int result;
int result; try {
try { result = groupCallService.initialize(mInternalCallback,
result = groupCallService.initialize(mInternalCallback, mSubscriptionId);
mSubscriptionId); } catch (RemoteException e) {
} catch (RemoteException e) { Log.e(LOG_TAG, "Service died before initialization");
Log.e(LOG_TAG, "Service died before initialization"); mInternalCallback.onError(
mInternalCallback.onError( MbmsErrors.InitializationErrors.ERROR_UNABLE_TO_INITIALIZE,
MbmsErrors.InitializationErrors.ERROR_UNABLE_TO_INITIALIZE, e.toString());
e.toString()); sIsInitialized.set(false);
sIsInitialized.set(false); return;
return; } catch (RuntimeException e) {
} catch (RuntimeException e) { Log.e(LOG_TAG, "Runtime exception during initialization");
Log.e(LOG_TAG, "Runtime exception during initialization"); mInternalCallback.onError(
mInternalCallback.onError( MbmsErrors.InitializationErrors.ERROR_UNABLE_TO_INITIALIZE,
MbmsErrors.InitializationErrors.ERROR_UNABLE_TO_INITIALIZE, e.toString());
e.toString()); sIsInitialized.set(false);
sIsInitialized.set(false); return;
return; }
} if (result == MbmsErrors.UNKNOWN) {
if (result == MbmsErrors.UNKNOWN) { // Unbind and throw an obvious error
// Unbind and throw an obvious error close();
close(); throw new IllegalStateException("Middleware must not return"
throw new IllegalStateException("Middleware must not return" + " an unknown error code");
+ " an unknown error code"); }
} if (result != MbmsErrors.SUCCESS) {
if (result != MbmsErrors.SUCCESS) { mInternalCallback.onError(result,
mInternalCallback.onError(result, "Error returned during initialization");
"Error returned during initialization"); sIsInitialized.set(false);
sIsInitialized.set(false); return;
return; }
} try {
try { groupCallService.asBinder().linkToDeath(mDeathRecipient, 0);
groupCallService.asBinder().linkToDeath(mDeathRecipient, 0); } catch (RemoteException e) {
} catch (RemoteException e) { mInternalCallback.onError(MbmsErrors.ERROR_MIDDLEWARE_LOST,
mInternalCallback.onError(MbmsErrors.ERROR_MIDDLEWARE_LOST, "Middleware lost during initialization");
"Middleware lost during initialization"); sIsInitialized.set(false);
sIsInitialized.set(false); return;
return; }
} mService.set(groupCallService);
mService.set(groupCallService); }
}
@Override @Override
public void onServiceDisconnected(ComponentName name) { public void onServiceDisconnected(ComponentName name) {
sIsInitialized.set(false); sIsInitialized.set(false);
mService.set(null); mService.set(null);
} }
});
@Override
public void onNullBinding(ComponentName name) {
Log.w(LOG_TAG, "bindAndInitialize: Remote service returned null");
mInternalCallback.onError(MbmsErrors.ERROR_MIDDLEWARE_LOST,
"Middleware service binding returned null");
sIsInitialized.set(false);
mService.set(null);
mContext.unbindService(this);
}
};
return MbmsUtils.startBinding(mContext, MBMS_GROUP_CALL_SERVICE_ACTION, mServiceConnection);
} }
} }

View File

@@ -82,6 +82,7 @@ public class MbmsStreamingSession implements AutoCloseable {
}; };
private InternalStreamingSessionCallback mInternalCallback; private InternalStreamingSessionCallback mInternalCallback;
private ServiceConnection mServiceConnection;
private Set<StreamingService> mKnownActiveStreamingServices = new ArraySet<>(); private Set<StreamingService> mKnownActiveStreamingServices = new ArraySet<>();
private final Context mContext; private final Context mContext;
@@ -168,7 +169,7 @@ public class MbmsStreamingSession implements AutoCloseable {
public void close() { public void close() {
try { try {
IMbmsStreamingService streamingService = mService.get(); IMbmsStreamingService streamingService = mService.get();
if (streamingService == null) { if (streamingService == null || mServiceConnection == null) {
// Ignore and return, assume already disposed. // Ignore and return, assume already disposed.
return; return;
} }
@@ -177,11 +178,13 @@ public class MbmsStreamingSession implements AutoCloseable {
s.getCallback().stop(); s.getCallback().stop();
} }
mKnownActiveStreamingServices.clear(); mKnownActiveStreamingServices.clear();
mContext.unbindService(mServiceConnection);
} catch (RemoteException e) { } catch (RemoteException e) {
// Ignore for now // Ignore for now
} finally { } finally {
mService.set(null); mService.set(null);
sIsInitialized.set(false); sIsInitialized.set(false);
mServiceConnection = null;
mInternalCallback.stop(); mInternalCallback.stop();
} }
} }
@@ -286,59 +289,69 @@ public class MbmsStreamingSession implements AutoCloseable {
} }
private int bindAndInitialize() { private int bindAndInitialize() {
return MbmsUtils.startBinding(mContext, MBMS_STREAMING_SERVICE_ACTION, mServiceConnection = new ServiceConnection() {
new ServiceConnection() { @Override
@Override public void onServiceConnected(ComponentName name, IBinder service) {
public void onServiceConnected(ComponentName name, IBinder service) { IMbmsStreamingService streamingService =
IMbmsStreamingService streamingService = IMbmsStreamingService.Stub.asInterface(service);
IMbmsStreamingService.Stub.asInterface(service); int result;
int result; try {
try { result = streamingService.initialize(mInternalCallback,
result = streamingService.initialize(mInternalCallback, mSubscriptionId);
mSubscriptionId); } catch (RemoteException e) {
} catch (RemoteException e) { Log.e(LOG_TAG, "Service died before initialization");
Log.e(LOG_TAG, "Service died before initialization"); sendErrorToApp(
sendErrorToApp( MbmsErrors.InitializationErrors.ERROR_UNABLE_TO_INITIALIZE,
MbmsErrors.InitializationErrors.ERROR_UNABLE_TO_INITIALIZE, e.toString());
e.toString()); sIsInitialized.set(false);
sIsInitialized.set(false); return;
return; } catch (RuntimeException e) {
} catch (RuntimeException e) { Log.e(LOG_TAG, "Runtime exception during initialization");
Log.e(LOG_TAG, "Runtime exception during initialization"); sendErrorToApp(
sendErrorToApp( MbmsErrors.InitializationErrors.ERROR_UNABLE_TO_INITIALIZE,
MbmsErrors.InitializationErrors.ERROR_UNABLE_TO_INITIALIZE, e.toString());
e.toString()); sIsInitialized.set(false);
sIsInitialized.set(false); return;
return; }
} if (result == MbmsErrors.UNKNOWN) {
if (result == MbmsErrors.UNKNOWN) { // Unbind and throw an obvious error
// Unbind and throw an obvious error close();
close(); throw new IllegalStateException("Middleware must not return"
throw new IllegalStateException("Middleware must not return" + " an unknown error code");
+ " an unknown error code"); }
} if (result != MbmsErrors.SUCCESS) {
if (result != MbmsErrors.SUCCESS) { sendErrorToApp(result, "Error returned during initialization");
sendErrorToApp(result, "Error returned during initialization"); sIsInitialized.set(false);
sIsInitialized.set(false); return;
return; }
} try {
try { streamingService.asBinder().linkToDeath(mDeathRecipient, 0);
streamingService.asBinder().linkToDeath(mDeathRecipient, 0); } catch (RemoteException e) {
} catch (RemoteException e) { sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST,
sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST, "Middleware lost during initialization");
"Middleware lost during initialization"); sIsInitialized.set(false);
sIsInitialized.set(false); return;
return; }
} mService.set(streamingService);
mService.set(streamingService); }
}
@Override @Override
public void onServiceDisconnected(ComponentName name) { public void onServiceDisconnected(ComponentName name) {
sIsInitialized.set(false); sIsInitialized.set(false);
mService.set(null); mService.set(null);
} }
});
@Override
public void onNullBinding(ComponentName name) {
Log.w(LOG_TAG, "bindAndInitialize: Remote service returned null");
sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST,
"Middleware service binding returned null");
sIsInitialized.set(false);
mService.set(null);
mContext.unbindService(this);
}
};
return MbmsUtils.startBinding(mContext, MBMS_STREAMING_SERVICE_ACTION, mServiceConnection);
} }
private void sendErrorToApp(int errorCode, String message) { private void sendErrorToApp(int errorCode, String message) {