diff options
author | Santos Cordon <santoscordon@google.com> | 2013-09-11 19:08:51 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2013-09-11 19:08:52 +0000 |
commit | c69ef13bc2b5f948e99752dcdb8e44e78ab201bf (patch) | |
tree | 52207ed3b1aeeb6c3e42cd18e46a03cee426d4a7 | |
parent | a4f0add23f17283d58912170977f211bd4b0faf0 (diff) | |
parent | 3b0b9918292d394b3895f9d98cbbfc17a85b8f70 (diff) |
Merge "Make creations and destruction of InCallUI system queued." into klp-dev
-rw-r--r-- | InCallUI/src/com/android/incallui/CallHandlerService.java | 106 | ||||
-rw-r--r-- | InCallUI/src/com/android/incallui/Log.java | 2 |
2 files changed, 67 insertions, 41 deletions
diff --git a/InCallUI/src/com/android/incallui/CallHandlerService.java b/InCallUI/src/com/android/incallui/CallHandlerService.java index dccdcdf32..65665494f 100644 --- a/InCallUI/src/com/android/incallui/CallHandlerService.java +++ b/InCallUI/src/com/android/incallui/CallHandlerService.java @@ -46,54 +46,51 @@ public class CallHandlerService extends Service { private static final int ON_DISCONNECT_CALL = 6; private static final int ON_BRING_TO_FOREGROUND = 7; private static final int ON_POST_CHAR_WAIT = 8; + private static final int ON_CREATE = 9; + private static final int ON_DESTROY = 10; - private static final int LARGEST_MSG_ID = ON_POST_CHAR_WAIT; + private static final int LARGEST_MSG_ID = ON_DESTROY; private CallList mCallList; private Handler mMainHandler; + private Object mHandlerInitLock = new Object(); private InCallPresenter mInCallPresenter; private AudioModeProvider mAudioModeProvider; @Override public void onCreate() { - Log.i(this, "creating"); + Log.i(this, "onCreate"); super.onCreate(); - mMainHandler = new MainHandler(); - mCallList = CallList.getInstance(); - mAudioModeProvider = AudioModeProvider.getInstance(); - mInCallPresenter = InCallPresenter.getInstance(); + synchronized(mHandlerInitLock) { + if (mMainHandler == null) { + mMainHandler = new MainHandler(); + } + } - mInCallPresenter.setUp(getApplicationContext(), mCallList, mAudioModeProvider); - Log.d(this, "onCreate finished"); + // Creation (and destruction) are sent to the message handler. The reason for this is that + // at any time the service could potentially unbind for both legitimate reasons as well as + // app crashes and it's better to queue up create/destroy because: + // (1) Previous actions like onUpdate/onDisconnect could be queued up and we dont want to + // destroy the system from a different thread in the middle of executing those actions. + // (2) If we queue up destruction we must also queue up creation or else we risk having a + // second "create" happen before the first "destroy". + // (e.g., create1, queue destroy1, create2, do destroy1) + mMainHandler.sendMessage(mMainHandler.obtainMessage(ON_CREATE)); + + // TODO: consider optimization of checking to see if any ON_DESTROY messages exist + // in the queue and in those cases simply remove the pending message. } @Override public void onDestroy() { - Log.i(this, "destroying"); - - // Remove all pending messages before nulling out handler - for (int i = 1; i <= LARGEST_MSG_ID; i++) { - mMainHandler.removeMessages(i); - } - mMainHandler = null; - - // The service gets disconnected under two circumstances: - // 1. When there are no more calls - // 2. When the phone app crashes. - // If (2) happens, we can't leave the UI thinking that there are still - // live calls. So we will tell the callList to clear as a final request. - mCallList.clearOnDisconnect(); - mCallList = null; - - mInCallPresenter.tearDown(); - mInCallPresenter = null; - mAudioModeProvider = null; + Log.i(this, "onDestroy"); - Log.d(this, "onDestroy finished"); + mMainHandler.sendMessage(mMainHandler.obtainMessage(ON_DESTROY)); } + @Override public IBinder onBind(Intent intent) { Log.i(this, "onBind"); @@ -124,7 +121,6 @@ public class CallHandlerService extends Service { @Override public void onDisconnect(Call call) { try { - Log.i(CallHandlerService.this, "onDisconnected: " + call); mMainHandler.sendMessage(mMainHandler.obtainMessage(ON_DISCONNECT_CALL, call)); } catch (Exception e) { Log.e(TAG, "Error processing onDisconnect() call.", e); @@ -134,12 +130,8 @@ public class CallHandlerService extends Service { @Override public void onIncoming(Call call, List<String> textResponses) { try { - Log.i(CallHandlerService.this, "onIncomingCall: " + call); - - // TODO(klp): Add text responses to the call object. Map.Entry<Call, List<String>> incomingCall = new AbstractMap.SimpleEntry<Call, List<String>>(call, textResponses); - Log.d("TEST", mMainHandler.toString()); mMainHandler.sendMessage(mMainHandler.obtainMessage( ON_UPDATE_CALL_WITH_TEXT_RESPONSES, incomingCall)); } catch (Exception e) { @@ -150,9 +142,6 @@ public class CallHandlerService extends Service { @Override public void onUpdate(List<Call> calls) { try { - Log.i(CallHandlerService.this, "onUpdate " + calls.toString()); - - // TODO(klp): Add use of fullUpdate to message mMainHandler.sendMessage(mMainHandler.obtainMessage(ON_UPDATE_MULTI_CALL, calls)); } catch (Exception e) { Log.e(TAG, "Error processing onUpdate() call.", e); @@ -162,7 +151,6 @@ public class CallHandlerService extends Service { @Override public void onAudioModeChange(int mode, boolean muted) { try { - Log.i(CallHandlerService.this, "onAudioModeChange : " + AudioMode.toString(mode)); mMainHandler.sendMessage(mMainHandler.obtainMessage(ON_AUDIO_MODE, mode, muted ? 1 : 0, null)); } catch (Exception e) { @@ -173,9 +161,6 @@ public class CallHandlerService extends Service { @Override public void onSupportedAudioModeChange(int modeMask) { try { - Log.i(CallHandlerService.this, "onSupportedAudioModeChange : " + AudioMode.toString( - modeMask)); - mMainHandler.sendMessage(mMainHandler.obtainMessage(ON_SUPPORTED_AUDIO_MODE, modeMask, 0, null)); } catch (Exception e) { @@ -195,6 +180,32 @@ public class CallHandlerService extends Service { } }; + private void doCreate() { + Log.i(this, "doCreate"); + + mCallList = CallList.getInstance(); + mAudioModeProvider = AudioModeProvider.getInstance(); + mInCallPresenter = InCallPresenter.getInstance(); + + mInCallPresenter.setUp(getApplicationContext(), mCallList, mAudioModeProvider); + } + + public void doDestroy() { + Log.i(this, "doDestroy"); + + // The service gets disconnected under two circumstances: + // 1. When there are no more calls + // 2. When the phone app crashes. + // If (2) happens, we can't leave the UI thinking that there are still + // live calls. So we will tell the callList to clear as a final request. + mCallList.clearOnDisconnect(); + mCallList = null; + + mInCallPresenter.tearDown(); + mInCallPresenter = null; + mAudioModeProvider = null; + } + /** * Handles messages from the service so that they get executed on the main thread, where they * can interact with UI. @@ -221,26 +232,35 @@ public class CallHandlerService extends Service { switch (msg.what) { case ON_UPDATE_CALL: + Log.i(CallHandlerService.this, "onUpdate: " + msg.obj); mCallList.onUpdate((Call) msg.obj); break; case ON_UPDATE_MULTI_CALL: + Log.i(CallHandlerService.this, "onUpdateMulti: " + msg.obj); mCallList.onUpdate((List<Call>) msg.obj); break; case ON_UPDATE_CALL_WITH_TEXT_RESPONSES: AbstractMap.SimpleEntry<Call, List<String>> entry = (AbstractMap.SimpleEntry<Call, List<String>>) msg.obj; + Log.i(CallHandlerService.this, "onIncomingCall: " + entry.getKey()); mCallList.onIncoming(entry.getKey(), entry.getValue()); break; case ON_DISCONNECT_CALL: + Log.i(CallHandlerService.this, "onDisconnected: " + msg.obj); mCallList.onDisconnect((Call) msg.obj); break; case ON_POST_CHAR_WAIT: mInCallPresenter.onPostDialCharWait(msg.arg1, (String) msg.obj); break; case ON_AUDIO_MODE: + Log.i(CallHandlerService.this, "onAudioModeChange : " + + AudioMode.toString(msg.arg1) + ", muted (" + (msg.arg2 == 1) + ")"); mAudioModeProvider.onAudioModeChange(msg.arg1, msg.arg2 == 1); break; case ON_SUPPORTED_AUDIO_MODE: + Log.i(CallHandlerService.this, "onSupportedAudioModeChange : " + AudioMode.toString( + msg.arg1)); + mAudioModeProvider.onSupportedAudioModeChange(msg.arg1); break; case ON_BRING_TO_FOREGROUND: @@ -248,6 +268,12 @@ public class CallHandlerService extends Service { mInCallPresenter.bringToForeground(); } break; + case ON_CREATE: + doCreate(); + break; + case ON_DESTROY: + doDestroy(); + break; default: break; } diff --git a/InCallUI/src/com/android/incallui/Log.java b/InCallUI/src/com/android/incallui/Log.java index bc57ed7d7..3099ea56b 100644 --- a/InCallUI/src/com/android/incallui/Log.java +++ b/InCallUI/src/com/android/incallui/Log.java @@ -30,7 +30,7 @@ public class Log { public static void d(String tag, String msg) { if (DEBUG) { - android.util.Log.d(TAG, tag + msg); + android.util.Log.d(TAG, tag + " - " + msg); } } |