diff options
author | Hung-ying Tyan <tyanh@google.com> | 2010-09-19 00:26:38 +0800 |
---|---|---|
committer | Hung-ying Tyan <tyanh@google.com> | 2010-09-20 10:10:34 +0800 |
commit | b17eae9e227475a323f61519abc8a7d35ddf8828 (patch) | |
tree | 13a4b80f48846318e86026c77af670237b1b90e9 | |
parent | 97963794af1e18674dd111e3ad344d90b16c922c (diff) | |
download | frameworks_base-b17eae9e227475a323f61519abc8a7d35ddf8828.zip frameworks_base-b17eae9e227475a323f61519abc8a7d35ddf8828.tar.gz frameworks_base-b17eae9e227475a323f61519abc8a7d35ddf8828.tar.bz2 |
SipService: move event handling out of system server's main thread
http://b/issue?id=2998047
Change-Id: Ibe0b6a19bb8b453fa852a94b3daf3cb80d7377b0
-rw-r--r-- | services/java/com/android/server/sip/SipService.java | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/services/java/com/android/server/sip/SipService.java b/services/java/com/android/server/sip/SipService.java index 3f47be4..a2ebc69 100644 --- a/services/java/com/android/server/sip/SipService.java +++ b/services/java/com/android/server/sip/SipService.java @@ -35,6 +35,10 @@ import android.net.sip.SipSessionState; import android.net.wifi.WifiManager; import android.os.Binder; import android.os.Bundle; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.Looper; +import android.os.Message; import android.os.RemoteException; import android.os.SystemClock; import android.text.TextUtils; @@ -73,6 +77,8 @@ public final class SipService extends ISipService.Stub { private WifiManager.WifiLock mWifiLock; private boolean mWifiOnly; + private MyExecutor mExecutor; + // SipProfile URI --> group private Map<String, SipSessionGroupExt> mSipGroups = new HashMap<String, SipSessionGroupExt>(); @@ -103,6 +109,12 @@ public final class SipService extends ISipService.Stub { mWifiOnly = SipManager.isSipWifiOnly(context); } + private MyExecutor getExecutor() { + // create mExecutor lazily + if (mExecutor == null) mExecutor = new MyExecutor(); + return mExecutor; + } + public synchronized SipProfile[] getListOfProfiles() { SipProfile[] profiles = new SipProfile[mSipGroups.size()]; int i = 0; @@ -509,6 +521,15 @@ public final class SipService extends ISipService.Stub { } public void run() { + // delegate to mExecutor + getExecutor().addTask(new Runnable() { + public void run() { + realRun(); + } + }); + } + + private void realRun() { synchronized (SipService.this) { SipSessionGroup.SipSessionImpl session = mSession.duplicate(); if (DEBUG) Log.d(TAG, "~~~ keepalive"); @@ -620,6 +641,15 @@ public final class SipService extends ISipService.Stub { } public void run() { + // delegate to mExecutor + getExecutor().addTask(new Runnable() { + public void run() { + realRun(); + } + }); + } + + private void realRun() { mErrorCode = SipErrorCode.NO_ERROR; mErrorMessage = null; if (DEBUG) Log.d(TAG, "~~~ registering"); @@ -829,7 +859,7 @@ public final class SipService extends ISipService.Stub { if (connected) { if (mTask != null) mTask.cancel(); mTask = new MyTimerTask(type, connected); - mTimer.schedule(mTask, 3 * 1000L); + mTimer.schedule(mTask, 2 * 1000L); // TODO: hold wakup lock so that we can finish change before // the device goes to sleep } else { @@ -852,6 +882,15 @@ public final class SipService extends ISipService.Stub { @Override public void run() { + // delegate to mExecutor + getExecutor().addTask(new Runnable() { + public void run() { + realRun(); + } + }); + } + + private void realRun() { synchronized (SipService.this) { if (mTask != this) { Log.w(TAG, " unexpected task: " + mNetworkType @@ -1162,4 +1201,30 @@ public final class SipService extends ISipService.Stub { return (this == that); } } + + // Single-threaded executor + private static class MyExecutor extends Handler { + MyExecutor() { + super(createLooper()); + } + + private static Looper createLooper() { + HandlerThread thread = new HandlerThread("SipService"); + thread.start(); + return thread.getLooper(); + } + + void addTask(Runnable task) { + Message.obtain(this, 0/* don't care */, task).sendToTarget(); + } + + @Override + public void handleMessage(Message msg) { + if (msg.obj instanceof Runnable) { + ((Runnable) msg.obj).run(); + } else { + Log.w(TAG, "can't handle msg: " + msg); + } + } + } } |