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