2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.server.sip;
19 import android.net.sip.ISipSession;
20 import android.net.sip.ISipSessionListener;
21 import android.net.sip.SipProfile;
22 import android.os.DeadObjectException;
23 import android.util.Log;
25 /** Class to help safely run a callback in a different thread. */
26 class SipSessionListenerProxy extends ISipSessionListener.Stub {
27 private static final String TAG = "SipSession";
29 private ISipSessionListener mListener;
31 public void setListener(ISipSessionListener listener) {
35 public ISipSessionListener getListener() {
39 private void proxy(Runnable runnable) {
40 // One thread for each calling back.
41 // Note: Guarantee ordering if the issue becomes important. Currently,
42 // the chance of handling two callback events at a time is none.
43 new Thread(runnable, "SipSessionCallbackThread").start();
46 public void onCalling(final ISipSession session) {
47 if (mListener == null) return;
48 proxy(new Runnable() {
51 mListener.onCalling(session);
52 } catch (Throwable t) {
53 handle(t, "onCalling()");
59 public void onRinging(final ISipSession session, final SipProfile caller,
60 final String sessionDescription) {
61 if (mListener == null) return;
62 proxy(new Runnable() {
65 mListener.onRinging(session, caller, sessionDescription);
66 } catch (Throwable t) {
67 handle(t, "onRinging()");
73 public void onRingingBack(final ISipSession session) {
74 if (mListener == null) return;
75 proxy(new Runnable() {
78 mListener.onRingingBack(session);
79 } catch (Throwable t) {
80 handle(t, "onRingingBack()");
86 public void onCallEstablished(final ISipSession session,
87 final String sessionDescription) {
88 if (mListener == null) return;
89 proxy(new Runnable() {
92 mListener.onCallEstablished(session, sessionDescription);
93 } catch (Throwable t) {
94 handle(t, "onCallEstablished()");
100 public void onCallEnded(final ISipSession session) {
101 if (mListener == null) return;
102 proxy(new Runnable() {
105 mListener.onCallEnded(session);
106 } catch (Throwable t) {
107 handle(t, "onCallEnded()");
113 public void onCallBusy(final ISipSession session) {
114 if (mListener == null) return;
115 proxy(new Runnable() {
118 mListener.onCallBusy(session);
119 } catch (Throwable t) {
120 handle(t, "onCallBusy()");
126 public void onCallChangeFailed(final ISipSession session,
127 final int errorCode, final String message) {
128 if (mListener == null) return;
129 proxy(new Runnable() {
132 mListener.onCallChangeFailed(session, errorCode, message);
133 } catch (Throwable t) {
134 handle(t, "onCallChangeFailed()");
140 public void onError(final ISipSession session, final int errorCode,
141 final String message) {
142 if (mListener == null) return;
143 proxy(new Runnable() {
146 mListener.onError(session, errorCode, message);
147 } catch (Throwable t) {
148 handle(t, "onError()");
154 public void onRegistering(final ISipSession session) {
155 if (mListener == null) return;
156 proxy(new Runnable() {
159 mListener.onRegistering(session);
160 } catch (Throwable t) {
161 handle(t, "onRegistering()");
167 public void onRegistrationDone(final ISipSession session,
168 final int duration) {
169 if (mListener == null) return;
170 proxy(new Runnable() {
173 mListener.onRegistrationDone(session, duration);
174 } catch (Throwable t) {
175 handle(t, "onRegistrationDone()");
181 public void onRegistrationFailed(final ISipSession session,
182 final int errorCode, final String message) {
183 if (mListener == null) return;
184 proxy(new Runnable() {
187 mListener.onRegistrationFailed(session, errorCode, message);
188 } catch (Throwable t) {
189 handle(t, "onRegistrationFailed()");
195 public void onRegistrationTimeout(final ISipSession session) {
196 if (mListener == null) return;
197 proxy(new Runnable() {
200 mListener.onRegistrationTimeout(session);
201 } catch (Throwable t) {
202 handle(t, "onRegistrationTimeout()");
208 private void handle(Throwable t, String message) {
209 if (t instanceof DeadObjectException) {
211 // This creates race but it's harmless. Just don't log the error
213 } else if (mListener != null) {
214 Log.w(TAG, message, t);