                                                                     
                                                                     
                                                                     
                                             
 /*****************************************************************************
  *  Copyright (c) 2011 Meta Watch Ltd.                                       *
  *  www.MetaWatch.org                                                        *
  *                                                                           *
  =============================================================================
  *                                                                           *
  *  Licensed under the Apache License, Version 2.0 (the "License");          *
  *  you may not use this file except in compliance with the License.         *
  *  You may obtain a copy of the License at                                  *
  *                                                                           *
  *    http://www.apache.org/licenses/LICENSE-2.0                             *
  *                                                                           *
  *  Unless required by applicable law or agreed to in writing, software      *
  *  distributed under the License is distributed on an "AS IS" BASIS,        *
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
  *  See the License for the specific language governing permissions and      *
  *  limitations under the License.                                           *
  *                                                                           *
  *****************************************************************************/

 /*****************************************************************************
  * CallStateListener.java                                                    *
  * CallStateListener                                                         *
  * Listener waiting for incoming call                                        *
  *                                                                           *
  *                                                                           *
  *****************************************************************************/

package org.metawatch.manager;

import org.metawatch.manager.MetaWatchService.Preferences;
import org.metawatch.manager.actions.ActionManager;

import android.content.Context;
import android.media.AudioManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;


class CallStateListener extends PhoneStateListener {
	
	Context context;
	
	public CallStateListener(Context ctx) {
		super();
		context = ctx;
	}

	@Override
	public void onCallStateChanged(int state, String incomingNumber) {
		super.onCallStateChanged(state, incomingNumber);
		
		if (Preferences.logging) Log.d(MetaWatch.TAG, "onCallStateChanged "+state+" "+incomingNumber);
		
		if (!MetaWatchService.Preferences.notifyCall)
			return;
		
		if (incomingNumber == null)
			incomingNumber = "";

		switch (state) {
			case TelephonyManager.CALL_STATE_RINGING: 
				Call.inCall = true;
				Call.phoneNumber = incomingNumber;
				Call.startRinging(context, incomingNumber);
				break;
			case TelephonyManager.CALL_STATE_IDLE: 
				Call.inCall = false;
				Call.phoneNumber = null;
				Call.endRinging(context);
				
				if (Preferences.autoSpeakerphone) {
					MediaControl.setSpeakerphone(context, Call.previousSpeakerphoneState);
				}
				if (Preferences.showActionsInCall) {
					Idle.toPage(context, 0);
				}
				if (Call.previousRingerMode!=-1) {
					AudioManager as = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
					as.setRingerMode(Call.previousRingerMode);
					Call.previousRingerMode = -1;
				}
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK: 
				Call.endRinging(context);
				if (Preferences.showActionsInCall) {
					ActionManager.displayCallActions(context);
				}
				break;
		}

	}
}

@@ -2806,16 +2806,17 @@
 ctx;%0A%09%7D%0A
+%09
 %0A%09@Overr
@@ -3396,28 +3396,8 @@
 %09%09%09%09
-Call.inCall = false;
 %0A%09%09%09
@@ -3453,24 +3453,48 @@
 text);%0A%09%09%09%09%0A
+%09%09%09%09if (Call.inCall) %7B%0A%09
 %09%09%09%09if (Pref
@@ -3517,24 +3517,25 @@
 kerphone) %7B%0A
+%09
 %09%09%09%09%09MediaCo
@@ -3598,26 +3598,28 @@
 State);%0A%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09if (Pref
@@ -3652,16 +3652,17 @@
  %7B%0A%09%09%09%09%09
+%09
 Idle.toP
@@ -3678,26 +3678,28 @@
 xt, 0);%0A%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09if (Call
@@ -3721,24 +3721,25 @@
 Mode!=-1) %7B%0A
+%09
 %09%09%09%09%09AudioMa
@@ -3817,16 +3817,17 @@
 );%0A%09%09%09%09%09
+%09
 as.setRi
@@ -3862,24 +3862,25 @@
 Mode);%0A%09%09%09%09%09
+%09
 Call.previou
@@ -3897,25 +3897,62 @@
 e = -1;%0A%09%09%09%09
-%7D
+%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09%0A%09%09%09%09Call.inCall = false;
 %0A%09%09%09%09break;%0A

Stop the auto speakerphone enabling at app startup, causing audio oddities! (fixes issues #41 and #43)