PhoneStatusListener

This commit is contained in:
Jens 2023-04-09 00:08:42 +02:00
parent 236e884c8f
commit aacb1c639d

View File

@ -10,10 +10,13 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyCallback;
import android.telephony.TelephonyManager;
import android.util.Log;
import androidx.annotation.RequiresApi;
import com.jens.automation2.ActivityPermissions;
import com.jens.automation2.AutomationService;
import com.jens.automation2.Miscellaneous;
@ -371,34 +374,16 @@ public class PhoneStatusListener implements AutomationListenerInterface, Executo
return new Trigger_Enum[] { Trigger_Enum.phoneCall };
}
public class PhoneCallback extends PhoneStateListener {
//--------------------------------------------------
// Constants
//--------------------------------------------------
public static class PhoneCallback extends PhoneStateListener
{
static PhoneCallback instance;
public static final String LOG_TAG = "PhoneCallback";
private TelephonyManager mTelephonyManager;
//--------------------------------------------------
// Attributes
//--------------------------------------------------
private final TextView mTextView;
//--------------------------------------------------
// Constructor
//--------------------------------------------------
public PhoneCallback(TextView textView) {
mTextView = textView;
}
//--------------------------------------------------
// Methods
//--------------------------------------------------
private String serviceStateToString(int serviceState) {
switch (serviceState) {
/*private String serviceStateToString(int serviceState)
{
switch (serviceState)
{
case ServiceState.STATE_IN_SERVICE:
return "STATE_IN_SERVICE";
case ServiceState.STATE_OUT_OF_SERVICE:
@ -410,10 +395,12 @@ public class PhoneStatusListener implements AutomationListenerInterface, Executo
default:
return "UNKNOWN_STATE";
}
}
}*/
private String callStateToString(int state) {
switch (state) {
/*private String callStateToString(int state)
{
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
return "\nonCallStateChanged: CALL_STATE_IDLE, ";
case TelephonyManager.CALL_STATE_RINGING:
@ -423,22 +410,21 @@ public class PhoneStatusListener implements AutomationListenerInterface, Executo
default:
return "\nUNKNOWN_STATE: " + state + ", ";
}
}
}*/
//--------------------------------------------------
// PhoneStateListener
//--------------------------------------------------
@Override
public void onCellInfoChanged(List<CellInfo> cellInfo) {
/*@Override
public void onCellInfoChanged(List<CellInfo> cellInfo)
{
super.onCellInfoChanged(cellInfo);
Log.i(LOG_TAG, "onCellInfoChanged: " + cellInfo);
}
}*/
@Override
public void onDataActivity(int direction) {
public void onDataActivity(int direction)
{
super.onDataActivity(direction);
switch (direction) {
switch (direction)
{
case TelephonyManager.DATA_ACTIVITY_NONE:
Log.i(LOG_TAG, "onDataActivity: DATA_ACTIVITY_NONE");
break;
@ -460,8 +446,9 @@ public class PhoneStatusListener implements AutomationListenerInterface, Executo
}
}
@Override
public void onServiceStateChanged(ServiceState serviceState) {
/*@Override
public void onServiceStateChanged(ServiceState serviceState)
{
super.onServiceStateChanged(serviceState);
String message = "onServiceStateChanged: " + serviceState + "\n";
message += "onServiceStateChanged: getOperatorAlphaLong " + serviceState.getOperatorAlphaLong() + "\n";
@ -471,28 +458,32 @@ public class PhoneStatusListener implements AutomationListenerInterface, Executo
message += "onServiceStateChanged: getRoaming " + serviceState.getRoaming() + "\n";
message += "onServiceStateChanged: " + serviceStateToString(serviceState.getState());
Log.i(LOG_TAG, message);
}
}*/
@Override
public void onCallStateChanged(int state, String incomingNumber) {
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
callStateToString(state);
String message = callStateToString(state) + "incomingNumber: " + incomingNumber;
mTextView.setText(message);
// callStateToString(state);
// String message = callStateToString(state) + "incomingNumber: " + incomingNumber;
}
@Override
public void onCellLocationChanged(CellLocation location) {
/*@Override
public void onCellLocationChanged(CellLocation location)
{
super.onCellLocationChanged(location);
String message = "";
if (location instanceof GsmCellLocation) {
if (location instanceof GsmCellLocation)
{
GsmCellLocation gcLoc = (GsmCellLocation) location;
message += "onCellLocationChanged: GsmCellLocation " + gcLoc + "\n";
message += "onCellLocationChanged: GsmCellLocation getCid " + gcLoc.getCid() + "\n";
message += "onCellLocationChanged: GsmCellLocation getLac " + gcLoc.getLac() + "\n";
message += "onCellLocationChanged: GsmCellLocation getPsc" + gcLoc.getPsc(); // Requires min API 9
Log.i(LOG_TAG, message);
} else if (location instanceof CdmaCellLocation) {
}
else if (location instanceof CdmaCellLocation)
{
CdmaCellLocation ccLoc = (CdmaCellLocation) location;
message += "onCellLocationChanged: CdmaCellLocation " + ccLoc + "\n";;
message += "onCellLocationChanged: CdmaCellLocation getBaseStationId " + ccLoc.getBaseStationId() + "\n";;
@ -501,19 +492,39 @@ public class PhoneStatusListener implements AutomationListenerInterface, Executo
message += "onCellLocationChanged: CdmaCellLocation getNetworkId " + ccLoc.getNetworkId() + "\n";;
message += "onCellLocationChanged: CdmaCellLocation getSystemId " + ccLoc.getSystemId();
Log.i(LOG_TAG, message);
} else {
}
else
{
Log.i(LOG_TAG, "onCellLocationChanged: " + location);
}
}
}*/
@Override
public void onCallForwardingIndicatorChanged(boolean changed) {
/*@Override
public void onCallForwardingIndicatorChanged(boolean changed)
{
super.onCallForwardingIndicatorChanged(changed);
}
@Override
public void onMessageWaitingIndicatorChanged(boolean changed) {
public void onMessageWaitingIndicatorChanged(boolean changed)
{
super.onMessageWaitingIndicatorChanged(changed);
}
}*/
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void callPhoneManager(Context context)
{
mTelephonyManager = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
mTelephonyManager.listen(new PhoneCallback(),
PhoneStateListener.LISTEN_CALL_STATE
| PhoneStateListener.LISTEN_CELL_INFO // Requires API 17
| PhoneStateListener.LISTEN_CELL_LOCATION
| PhoneStateListener.LISTEN_DATA_ACTIVITY
| PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
| PhoneStateListener.LISTEN_SERVICE_STATE
| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS
| PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR
| PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR);
}
}