Added receive call behaviour and also did some refactoring
This commit is contained in:
parent
f72e6e9e53
commit
99c21bf92b
3 changed files with 164 additions and 32 deletions
|
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
79
app/src/main/java/com/ericsson/uecontrol/core/util/CallUtil.java
Executable file
79
app/src/main/java/com/ericsson/uecontrol/core/util/CallUtil.java
Executable file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue