SipService: move event handling out of system server's main thread
http://b/issue?id=2998047 Change-Id: Ibe0b6a19bb8b453fa852a94b3daf3cb80d7377b0
This commit is contained in:
@@ -35,6 +35,10 @@ import android.net.sip.SipSessionState;
|
|||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.Bundle;
|
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.RemoteException;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
@@ -73,6 +77,8 @@ public final class SipService extends ISipService.Stub {
|
|||||||
private WifiManager.WifiLock mWifiLock;
|
private WifiManager.WifiLock mWifiLock;
|
||||||
private boolean mWifiOnly;
|
private boolean mWifiOnly;
|
||||||
|
|
||||||
|
private MyExecutor mExecutor;
|
||||||
|
|
||||||
// SipProfile URI --> group
|
// SipProfile URI --> group
|
||||||
private Map<String, SipSessionGroupExt> mSipGroups =
|
private Map<String, SipSessionGroupExt> mSipGroups =
|
||||||
new HashMap<String, SipSessionGroupExt>();
|
new HashMap<String, SipSessionGroupExt>();
|
||||||
@@ -103,6 +109,12 @@ public final class SipService extends ISipService.Stub {
|
|||||||
mWifiOnly = SipManager.isSipWifiOnly(context);
|
mWifiOnly = SipManager.isSipWifiOnly(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private MyExecutor getExecutor() {
|
||||||
|
// create mExecutor lazily
|
||||||
|
if (mExecutor == null) mExecutor = new MyExecutor();
|
||||||
|
return mExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized SipProfile[] getListOfProfiles() {
|
public synchronized SipProfile[] getListOfProfiles() {
|
||||||
SipProfile[] profiles = new SipProfile[mSipGroups.size()];
|
SipProfile[] profiles = new SipProfile[mSipGroups.size()];
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@@ -509,6 +521,15 @@ public final class SipService extends ISipService.Stub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
// delegate to mExecutor
|
||||||
|
getExecutor().addTask(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
realRun();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void realRun() {
|
||||||
synchronized (SipService.this) {
|
synchronized (SipService.this) {
|
||||||
SipSessionGroup.SipSessionImpl session = mSession.duplicate();
|
SipSessionGroup.SipSessionImpl session = mSession.duplicate();
|
||||||
if (DEBUG) Log.d(TAG, "~~~ keepalive");
|
if (DEBUG) Log.d(TAG, "~~~ keepalive");
|
||||||
@@ -620,6 +641,15 @@ public final class SipService extends ISipService.Stub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
// delegate to mExecutor
|
||||||
|
getExecutor().addTask(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
realRun();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void realRun() {
|
||||||
mErrorCode = SipErrorCode.NO_ERROR;
|
mErrorCode = SipErrorCode.NO_ERROR;
|
||||||
mErrorMessage = null;
|
mErrorMessage = null;
|
||||||
if (DEBUG) Log.d(TAG, "~~~ registering");
|
if (DEBUG) Log.d(TAG, "~~~ registering");
|
||||||
@@ -829,7 +859,7 @@ public final class SipService extends ISipService.Stub {
|
|||||||
if (connected) {
|
if (connected) {
|
||||||
if (mTask != null) mTask.cancel();
|
if (mTask != null) mTask.cancel();
|
||||||
mTask = new MyTimerTask(type, connected);
|
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
|
// TODO: hold wakup lock so that we can finish change before
|
||||||
// the device goes to sleep
|
// the device goes to sleep
|
||||||
} else {
|
} else {
|
||||||
@@ -852,6 +882,15 @@ public final class SipService extends ISipService.Stub {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
// delegate to mExecutor
|
||||||
|
getExecutor().addTask(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
realRun();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void realRun() {
|
||||||
synchronized (SipService.this) {
|
synchronized (SipService.this) {
|
||||||
if (mTask != this) {
|
if (mTask != this) {
|
||||||
Log.w(TAG, " unexpected task: " + mNetworkType
|
Log.w(TAG, " unexpected task: " + mNetworkType
|
||||||
@@ -1162,4 +1201,30 @@ public final class SipService extends ISipService.Stub {
|
|||||||
return (this == that);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user