diff options
author | Sailesh Nepal <sail@google.com> | 2014-07-18 14:21:23 -0700 |
---|---|---|
committer | Sailesh Nepal <sail@google.com> | 2014-07-18 14:21:23 -0700 |
commit | 480315939d4a321992e0288d19797f30e4561e62 (patch) | |
tree | 498316608bdf7059b6a2337e25811e640a38ee0a /telecomm/java/android | |
parent | ba051aa6f8b32fd6527fc88d8848cd651b1e83ae (diff) | |
download | frameworks_base-480315939d4a321992e0288d19797f30e4561e62.zip frameworks_base-480315939d4a321992e0288d19797f30e4561e62.tar.gz frameworks_base-480315939d4a321992e0288d19797f30e4561e62.tar.bz2 |
Use main thread for RemoteConnection callbacks
Currently call backs from RemoteConnection can happen
on non-main threads. This CL updates the code to always
use the main thread.
Change-Id: I134c0fbdba1f916f49676c0c6696ac63bcb7c513
Diffstat (limited to 'telecomm/java/android')
-rw-r--r-- | telecomm/java/android/telecomm/RemoteConnectionService.java | 295 |
1 files changed, 229 insertions, 66 deletions
diff --git a/telecomm/java/android/telecomm/RemoteConnectionService.java b/telecomm/java/android/telecomm/RemoteConnectionService.java index c2b574c..ed76a48 100644 --- a/telecomm/java/android/telecomm/RemoteConnectionService.java +++ b/telecomm/java/android/telecomm/RemoteConnectionService.java @@ -19,11 +19,14 @@ package android.telecomm; import android.content.ComponentName; import android.net.Uri; import android.os.IBinder.DeathRecipient; +import android.os.Handler; +import android.os.Message; import android.os.RemoteException; import android.telephony.DisconnectCause; import android.text.TextUtils; +import com.android.internal.os.SomeArgs; import com.android.internal.telecomm.IConnectionService; import com.android.internal.telecomm.IConnectionServiceAdapter; import com.android.internal.telecomm.ICallVideoProvider; @@ -39,6 +42,28 @@ import java.util.UUID; * @hide */ final class RemoteConnectionService implements DeathRecipient { + private static final int MSG_HANDLE_CREATE_CONNECTION_SUCCESSFUL = 1; + private static final int MSG_HANDLE_CREATE_CONNECTION_FAILED = 2; + private static final int MSG_HANDLE_CREATE_CONNECTION_CANCELLED = 3; + private static final int MSG_SET_ACTIVE = 4; + private static final int MSG_SET_RINGING = 5; + private static final int MSG_SET_DIALING = 6; + private static final int MSG_SET_DISCONNECTED = 7; + private static final int MSG_SET_ON_HOLD = 8; + private static final int MSG_SET_REQUESTING_RINGBACK = 9; + private static final int MSG_SET_CALL_CAPABILITIES = 10; + private static final int MSG_SET_IS_CONFERENCED = 11; + private static final int MSG_ADD_CONFERENCE_CALL = 12; + private static final int MSG_REMOVE_CALL = 13; + private static final int MSG_ON_POST_DIAL_WAIT = 14; + private static final int MSG_QUERY_REMOTE_CALL_SERVICES = 15; + private static final int MSG_SET_VIDEO_STATE = 16; + private static final int MSG_SET_CALL_VIDEO_PROVIDER = 17; + private static final int MSG_SET_AUDIO_MODE_IS_VOIP = 18; + private static final int MSG_SET_STATUS_HINTS = 19; + private static final int MSG_SET_HANDLE = 20; + private static final int MSG_SET_CALLER_DISPLAY_NAME = 21; + private final IConnectionService mConnectionService; private final ComponentName mComponentName; @@ -48,89 +73,219 @@ final class RemoteConnectionService implements DeathRecipient { // Remote connection services only support a single connection. private RemoteConnection mConnection; + private final Handler mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case MSG_HANDLE_CREATE_CONNECTION_SUCCESSFUL: { + ConnectionRequest request = (ConnectionRequest) msg.obj; + if (isPendingConnection(request.getCallId())) { + mConnection = new RemoteConnection(mConnectionService, request.getCallId()); + mPendingResponse.onSuccess(request, mConnection); + clearPendingInformation(); + } + break; + } + case MSG_HANDLE_CREATE_CONNECTION_FAILED: { + SomeArgs args = (SomeArgs) msg.obj; + try { + ConnectionRequest request = (ConnectionRequest) args.arg1; + if (isPendingConnection(request.getCallId())) { + mPendingResponse.onFailure(request, args.argi1, (String) args.arg2); + mConnectionId = null; + clearPendingInformation(); + } + } finally { + args.recycle(); + } + break; + } + case MSG_HANDLE_CREATE_CONNECTION_CANCELLED: { + ConnectionRequest request = (ConnectionRequest) msg.obj; + if (isPendingConnection(request.getCallId())) { + mPendingResponse.onCancel(request); + mConnectionId = null; + clearPendingInformation(); + } + break; + } + case MSG_SET_ACTIVE: + if (isCurrentConnection(msg.obj)) { + mConnection.setState(Connection.State.ACTIVE); + } + break; + case MSG_SET_RINGING: + if (isCurrentConnection(msg.obj)) { + mConnection.setState(Connection.State.RINGING); + } + break; + case MSG_SET_DIALING: + if (isCurrentConnection(msg.obj)) { + mConnection.setState(Connection.State.DIALING); + } + break; + case MSG_SET_DISCONNECTED: { + SomeArgs args = (SomeArgs) msg.obj; + try { + if (isCurrentConnection(args.arg1)) { + mConnection.setDisconnected(args.argi1, (String) args.arg2); + } + } finally { + args.recycle(); + } + break; + } + case MSG_SET_ON_HOLD: + if (isCurrentConnection(msg.obj)) { + mConnection.setState(Connection.State.HOLDING); + } + break; + case MSG_SET_REQUESTING_RINGBACK: + if (isCurrentConnection(msg.obj)) { + mConnection.setRequestingRingback(msg.arg1 == 1); + } + break; + case MSG_SET_CALL_CAPABILITIES: + if (isCurrentConnection(msg.obj)) { + mConnection.setCallCapabilities(msg.arg1); + } + break; + case MSG_SET_IS_CONFERENCED: + // not supported for remote connections. + break; + case MSG_ADD_CONFERENCE_CALL: + // not supported for remote connections. + break; + case MSG_REMOVE_CALL: + if (isCurrentConnection(msg.obj)) { + destroyConnection(); + } + break; + case MSG_ON_POST_DIAL_WAIT: { + SomeArgs args = (SomeArgs) msg.obj; + try { + if (isCurrentConnection(args.arg1)) { + mConnection.setPostDialWait((String) args.arg2); + } + } finally { + args.recycle(); + } + break; + } + case MSG_QUERY_REMOTE_CALL_SERVICES: + // Not supported from remote connection service. + break; + case MSG_SET_VIDEO_STATE: + if (isCurrentConnection(msg.obj)) { + mConnection.setVideoState(msg.arg1); + } + break; + case MSG_SET_CALL_VIDEO_PROVIDER: + // not supported for remote connections. + break; + case MSG_SET_AUDIO_MODE_IS_VOIP: + if (isCurrentConnection(msg.obj)) { + mConnection.setAudioModeIsVoip(msg.arg1 == 1); + } + break; + case MSG_SET_STATUS_HINTS: { + SomeArgs args = (SomeArgs) msg.obj; + try { + if (isCurrentConnection(args.arg1)) { + mConnection.setStatusHints((StatusHints) args.arg2); + } + } finally { + args.recycle(); + } + break; + } + case MSG_SET_HANDLE: { + SomeArgs args = (SomeArgs) msg.obj; + try { + if (isCurrentConnection(args.arg1)) { + mConnection.setHandle((Uri) args.arg2, args.argi1); + } + } finally { + args.recycle(); + } + break; + } + case MSG_SET_CALLER_DISPLAY_NAME: { + SomeArgs args = (SomeArgs) msg.obj; + try { + if (isCurrentConnection(msg.arg1)) { + mConnection.setCallerDisplayName((String) args.arg2, args.argi1); + } + } finally { + args.recycle(); + } + break; + } + } + } + }; + private final IConnectionServiceAdapter mAdapter = new IConnectionServiceAdapter.Stub() { @Override public void handleCreateConnectionSuccessful(ConnectionRequest request) { - if (isPendingConnection(request.getCallId())) { - mConnection = new RemoteConnection(mConnectionService, request.getCallId()); - mPendingResponse.onSuccess(request, mConnection); - clearPendingInformation(); - } + mHandler.obtainMessage(MSG_HANDLE_CREATE_CONNECTION_SUCCESSFUL, request).sendToTarget(); } @Override public void handleCreateConnectionFailed( ConnectionRequest request, int errorCode, String errorMessage) { - if (isPendingConnection(request.getCallId())) { - mPendingResponse.onFailure(request, errorCode, errorMessage); - mConnectionId = null; - clearPendingInformation(); - } + SomeArgs args = SomeArgs.obtain(); + args.arg1 = request; + args.argi1 = errorCode; + args.arg2 = errorMessage; + mHandler.obtainMessage(MSG_HANDLE_CREATE_CONNECTION_FAILED, args).sendToTarget(); } @Override public void handleCreateConnectionCancelled(ConnectionRequest request) { - if (isPendingConnection(request.getCallId())) { - mPendingResponse.onCancel(request); - mConnectionId = null; - clearPendingInformation(); - } + mHandler.obtainMessage(MSG_HANDLE_CREATE_CONNECTION_CANCELLED, request).sendToTarget(); } @Override public void setActive(String connectionId) { - if (isCurrentConnection(connectionId)) { - mConnection.setState(Connection.State.ACTIVE); - } + mHandler.obtainMessage(MSG_SET_ACTIVE, connectionId).sendToTarget(); } @Override public void setRinging(String connectionId) { - if (isCurrentConnection(connectionId)) { - mConnection.setState(Connection.State.RINGING); - } - } - - @Override - public void setCallVideoProvider( - String connectionId, ICallVideoProvider callVideoProvider) { - // not supported for remote connections. + mHandler.obtainMessage(MSG_SET_RINGING, connectionId).sendToTarget(); } @Override public void setDialing(String connectionId) { - if (isCurrentConnection(connectionId)) { - mConnection.setState(Connection.State.DIALING); - } + mHandler.obtainMessage(MSG_SET_DIALING, connectionId).sendToTarget(); } @Override public void setDisconnected( String connectionId, int disconnectCause, String disconnectMessage) { - if (isCurrentConnection(connectionId)) { - mConnection.setDisconnected(disconnectCause, disconnectMessage); - } + SomeArgs args = SomeArgs.obtain(); + args.arg1 = connectionId; + args.arg2 = disconnectMessage; + args.argi1 = disconnectCause; + mHandler.obtainMessage(MSG_SET_DISCONNECTED, args).sendToTarget(); } @Override public void setOnHold(String connectionId) { - if (isCurrentConnection(connectionId)) { - mConnection.setState(Connection.State.HOLDING); - } + mHandler.obtainMessage(MSG_SET_ON_HOLD, connectionId).sendToTarget(); } @Override - public void setRequestingRingback(String connectionId, boolean isRequestingRingback) { - if (isCurrentConnection(connectionId)) { - mConnection.setRequestingRingback(isRequestingRingback); - } + public void setRequestingRingback(String connectionId, boolean ringback) { + mHandler.obtainMessage(MSG_SET_REQUESTING_RINGBACK, ringback ? 1 : 0, 0, connectionId) + .sendToTarget(); } @Override public void setCallCapabilities(String connectionId, int callCapabilities) { - if (isCurrentConnection(connectionId)) { - mConnection.setCallCapabilities(callCapabilities); - } + mHandler.obtainMessage(MSG_SET_CALL_CAPABILITIES, callCapabilities, 0, connectionId) + .sendToTarget(); } @Override @@ -145,16 +300,15 @@ final class RemoteConnectionService implements DeathRecipient { @Override public void removeCall(String connectionId) { - if (isCurrentConnection(connectionId)) { - destroyConnection(); - } + mHandler.obtainMessage(MSG_REMOVE_CALL, connectionId).sendToTarget(); } @Override public void onPostDialWait(String connectionId, String remainingDigits) { - if (isCurrentConnection(connectionId)) { - mConnection.setPostDialWait(remainingDigits); - } + SomeArgs args = SomeArgs.obtain(); + args.arg1 = connectionId; + args.arg2 = remainingDigits; + mHandler.obtainMessage(MSG_ON_POST_DIAL_WAIT, args).sendToTarget(); } @Override @@ -168,38 +322,46 @@ final class RemoteConnectionService implements DeathRecipient { @Override public void setVideoState(String connectionId, int videoState) { - if (isCurrentConnection(connectionId)) { - mConnection.setVideoState(videoState); - } + mHandler.obtainMessage(MSG_SET_VIDEO_STATE, videoState, 0, connectionId).sendToTarget(); + } + + @Override + public void setCallVideoProvider( + String connectionId, ICallVideoProvider callVideoProvider) { + // not supported for remote connections. } @Override public final void setAudioModeIsVoip(String connectionId, boolean isVoip) { - if (isCurrentConnection(connectionId)) { - mConnection.setAudioModeIsVoip(isVoip); - } + mHandler.obtainMessage(MSG_SET_AUDIO_MODE_IS_VOIP, isVoip ? 1 : 0, 0, + connectionId).sendToTarget(); } @Override public final void setStatusHints(String connectionId, StatusHints statusHints) { - if (isCurrentConnection(connectionId)) { - mConnection.setStatusHints(statusHints); - } + SomeArgs args = SomeArgs.obtain(); + args.arg1 = connectionId; + args.arg2 = statusHints; + mHandler.obtainMessage(MSG_SET_STATUS_HINTS, args).sendToTarget(); } @Override public final void setHandle(String connectionId, Uri handle, int presentation) { - if (isCurrentConnection(connectionId)) { - mConnection.setHandle(handle, presentation); - } + SomeArgs args = SomeArgs.obtain(); + args.arg1 = connectionId; + args.arg2 = handle; + args.argi1 = presentation; + mHandler.obtainMessage(MSG_SET_HANDLE, args).sendToTarget(); } @Override public final void setCallerDisplayName( String connectionId, String callerDisplayName, int presentation) { - if (isCurrentConnection(connectionId)) { - mConnection.setCallerDisplayName(callerDisplayName, presentation); - } + SomeArgs args = SomeArgs.obtain(); + args.arg1 = connectionId; + args.arg2 = callerDisplayName; + args.argi1 = presentation; + mHandler.obtainMessage(MSG_SET_CALLER_DISPLAY_NAME, args).sendToTarget(); } }; @@ -276,8 +438,9 @@ final class RemoteConnectionService implements DeathRecipient { return TextUtils.equals(mConnectionId, id) && mPendingResponse != null; } - private boolean isCurrentConnection(String id) { - return mConnection != null && TextUtils.equals(mConnectionId, id); + private boolean isCurrentConnection(Object obj) { + return obj instanceof String && mConnection != null && + TextUtils.equals(mConnectionId, (String) obj); } private void clearPendingInformation() { |