From 99c21bf92b1226d71f04e8f0d49c1afc6e1cb06b Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Mon, 11 Aug 2014 11:34:41 +0200 Subject: [PATCH] Added receive call behaviour and also did some refactoring --- .../behaviour/UeBehaviourReceiveCall.java | 58 ++++++++++++++ .../core/behaviour/UeBehaviourSpeechCall.java | 59 +++++++------- .../uecontrol/core/util/CallUtil.java | 79 +++++++++++++++++++ 3 files changed, 164 insertions(+), 32 deletions(-) create mode 100755 app/src/main/java/com/ericsson/uecontrol/core/behaviour/UeBehaviourReceiveCall.java create mode 100755 app/src/main/java/com/ericsson/uecontrol/core/util/CallUtil.java diff --git a/app/src/main/java/com/ericsson/uecontrol/core/behaviour/UeBehaviourReceiveCall.java b/app/src/main/java/com/ericsson/uecontrol/core/behaviour/UeBehaviourReceiveCall.java new file mode 100755 index 0000000..d24438e --- /dev/null +++ b/app/src/main/java/com/ericsson/uecontrol/core/behaviour/UeBehaviourReceiveCall.java @@ -0,0 +1,58 @@ +package com.ericsson.uecontrol.core.behaviour; + +import android.content.Context; +import android.telephony.TelephonyManager; + +import com.ericsson.uecontrol.core.UeBehaviour; +import com.ericsson.uecontrol.core.util.CallUtil; +import com.ericsson.uecontrol.gui.MainActivity; +import com.ericsson.uecontrol.gui.util.Configurator.Configurable; + +import org.apache.log4j.Logger; + +import java.lang.reflect.InvocationTargetException; + +/** + * This behaviour simulates an idle period for the device. + * + * Created by ezivkoc on 2014-07-15. + */ +public class UeBehaviourReceiveCall extends UeBehaviour{ + private static final Logger log = Logger.getLogger(UeBehaviourReceiveCall.class); + public static final int SLEEP_PERIOD = 200; + + + + @Override + protected void execute() throws InterruptedException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Context context = MainActivity.getContext(); + TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + + // Wait to receive Call + while(tm.getCallState() != TelephonyManager.CALL_STATE_RINGING ){ + if(super.stopExecution()) + return; + Thread.sleep(SLEEP_PERIOD); + } + log.debug("Phone is ringing, answering call"); + + // Wait for call to end + while(tm.getCallState() != TelephonyManager.CALL_STATE_IDLE){ + if(super.stopExecution()) + return; + Thread.sleep(SLEEP_PERIOD); + } + log.debug("Call has ended"); + } + + + @Override + public String getName() { + return "Receive Call"; + } + + @Override + public String toString() { + return "Will wait for a call and then answer it"; + } +} diff --git a/app/src/main/java/com/ericsson/uecontrol/core/behaviour/UeBehaviourSpeechCall.java b/app/src/main/java/com/ericsson/uecontrol/core/behaviour/UeBehaviourSpeechCall.java index c0e0e9b..808acaf 100755 --- a/app/src/main/java/com/ericsson/uecontrol/core/behaviour/UeBehaviourSpeechCall.java +++ b/app/src/main/java/com/ericsson/uecontrol/core/behaviour/UeBehaviourSpeechCall.java @@ -6,6 +6,7 @@ import android.net.Uri; import android.telephony.TelephonyManager; import com.ericsson.uecontrol.core.UeBehaviour; +import com.ericsson.uecontrol.core.util.CallUtil; import com.ericsson.uecontrol.gui.MainActivity; import com.ericsson.uecontrol.gui.util.Configurator.Configurable; @@ -21,7 +22,7 @@ import java.lang.reflect.Method; */ public class UeBehaviourSpeechCall extends UeBehaviour{ private static final Logger log = Logger.getLogger(UeBehaviourSpeechCall.class); - public static final int SLEEP_PERIOD = 100; + public static final int SLEEP_PERIOD = 200; @Configurable("Phone Number") private String phoneNumber; @@ -39,45 +40,39 @@ public class UeBehaviourSpeechCall extends UeBehaviour{ @Override protected void execute() throws InterruptedException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { - startCall(); + Context context = MainActivity.getContext(); + TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + // Start Call + log.debug("Calling phone"); + CallUtil.startCall(phoneNumber); + + // Wait for call to start + log.debug("Waiting for an answer"); + while(tm.getCallState() != TelephonyManager.CALL_STATE_OFFHOOK){ + if(super.stopExecution()) + return; + Thread.sleep(SLEEP_PERIOD); + } + + log.debug("Starting call timer"); int elapsedTime = 0; while(elapsedTime < length){ + if(super.stopExecution()) + return; super.setProgress((float)elapsedTime/length); - if(super.stopExecution()) break; Thread.sleep(SLEEP_PERIOD); elapsedTime += SLEEP_PERIOD; } - endCall(); - } + CallUtil.endCall(); - - private void startCall(){ - log.debug("Starting Speech Call"); - Context context = MainActivity.getContext(); - Uri number = Uri.parse("tel:" + phoneNumber); - - Intent intent = new Intent(); - //intent.setAction(Intent.ACTION_DIAL); - intent.setAction(Intent.ACTION_CALL); - intent.setData(number); - context.startActivity(intent); - } - - private void endCall() throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, NoSuchMethodException { - log.debug("Ending Speech Call"); - Context context = MainActivity.getContext(); - TelephonyManager tm = (TelephonyManager) context - .getSystemService(Context.TELEPHONY_SERVICE); - Class c = Class.forName(tm.getClass().getName()); - Method m = c.getDeclaredMethod("getITelephony"); - m.setAccessible(true); - - Object telephonyService = m.invoke(tm); // Get the internal ITelephony object - c = Class.forName(telephonyService.getClass().getName()); // Get its class - m = c.getDeclaredMethod("endCall"); // Get the "endCall()" method - m.setAccessible(true); // Make it accessible - m.invoke(telephonyService); // invoke endCall() + log.debug("Waiting for call to end"); + // Wait for call to end + while(tm.getCallState() != TelephonyManager.CALL_STATE_IDLE){ + if(super.stopExecution()) + return; + Thread.sleep(SLEEP_PERIOD); + } } diff --git a/app/src/main/java/com/ericsson/uecontrol/core/util/CallUtil.java b/app/src/main/java/com/ericsson/uecontrol/core/util/CallUtil.java new file mode 100755 index 0000000..e898f52 --- /dev/null +++ b/app/src/main/java/com/ericsson/uecontrol/core/util/CallUtil.java @@ -0,0 +1,79 @@ +package com.ericsson.uecontrol.core.util; + +import android.content.Context; +import android.content.Intent; +import android.media.AudioManager; +import android.net.Uri; +import android.telephony.TelephonyManager; + +import com.ericsson.uecontrol.gui.MainActivity; + +import org.apache.log4j.Logger; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Created by ezivkoc on 2014-08-11. + */ +public class CallUtil { + private static final Logger log = Logger.getLogger(CallUtil.class); + + + public static void startCall(String phoneNumber){ + log.debug("Starting Speech Call"); + Context context = MainActivity.getContext(); + Uri number = Uri.parse("tel:" + phoneNumber); + + Intent intent = new Intent(); + //intent.setAction(Intent.ACTION_DIAL); + intent.setAction(Intent.ACTION_CALL); + intent.setData(number); + context.startActivity(intent); + } + + public static void enableSpeakerPhone(boolean enable) { + Context context = MainActivity.getContext(); + AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + audioManager.setSpeakerphoneOn(enable); + } + + + private static void answerPhoneAidl() throws Exception { + log.debug("Answering Speech Call"); + + Object telephonyService = getTelephonyService(); + Class c = Class.forName(telephonyService.getClass().getName()); + // Silence ringer + Method m = c.getDeclaredMethod("silenceRinger"); + m.setAccessible(true); + m.invoke(telephonyService); + // Answer Call + m = c.getDeclaredMethod("answerRingingCall"); + m.setAccessible(true); + m.invoke(telephonyService); + } + + + public static void endCall() throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, NoSuchMethodException { + log.debug("Ending Speech Call"); + + Object telephonyService = getTelephonyService(); + Class c = Class.forName(telephonyService.getClass().getName()); // Get its class + Method m = c.getDeclaredMethod("endCall"); // Get the "endCall()" method + m.setAccessible(true); // Make it accessible + m.invoke(telephonyService); // invoke endCall() + } + + + private static Object getTelephonyService() throws InvocationTargetException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException { + Context context = MainActivity.getContext(); + + TelephonyManager tm = (TelephonyManager) context + .getSystemService(Context.TELEPHONY_SERVICE); + Class c = Class.forName(tm.getClass().getName()); + Method m = c.getDeclaredMethod("getITelephony"); + m.setAccessible(true); + return m.invoke(tm); + } +}