<commit_before>package sk.henrichg.phoneprofilesplus;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Handler;
import android.os.PowerManager;
import android.os.SystemClock;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneCallsListener extends PhoneStateListener {

    final Context savedContext;

    int lastState = TelephonyManager.CALL_STATE_IDLE;
    boolean inCall;
    boolean isIncoming;

    boolean networkRoaming;
    boolean dataRoaming;

    int simSlot;

    private static AudioManager audioManager = null;

    private static boolean savedSpeakerphone = false;
    private static boolean speakerphoneSelected = false;

    //static boolean linkUnlinkExecuted = false;
    //static boolean speakerphoneOnExecuted = false;

    private static final int SERVICE_PHONE_EVENT_START = 1;
    private static final int SERVICE_PHONE_EVENT_ANSWER = 2;
    private static final int SERVICE_PHONE_EVENT_END = 3;

    static final int LINKMODE_NONE = 0;
    static final int LINKMODE_LINK = 1;
    static final int LINKMODE_UNLINK = 2;

    PhoneCallsListener(Context context, int simSlot) {
        this.savedContext = context.getApplicationContext();
        this.simSlot = simSlot;
    }

    public void onCallStateChanged (int state, String phoneNumber) {

        if (PPApplication.getApplicationStarted(true)) {
            if(lastState == state){
                //No change, de-bounce extras
                return;
            }

            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
//                    PPApplication.logE("PhoneCallsListener.onCallStateChanged", "state=CALL_STATE_RINGING");
                    //PPPEApplication.logE("PhoneCallsListener.PhoneCallStartEndDetector", "incomingNumber="+incomingNumber);
                    inCall = false;
                    isIncoming = true;
                    onIncomingCallStarted(/*incomingNumber, eventTime*/);
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
//                    PPApplication.logE("PhoneCallsListener.onCallStateChanged", "state=CALL_STATE_OFFHOOK");
                    //Transition of ringing->off hook are pickups of incoming calls.  Nothing down on them
                    if(lastState != TelephonyManager.CALL_STATE_RINGING){
                        inCall = true;
                        isIncoming = false;
                        onOutgoingCallAnswered(/*savedNumber, eventTime*/);
                    }
                    else
                    {
                        inCall = true;
                        isIncoming = true;
                        onIncomingCallAnswered(/*savedNumber, eventTime*/);
                    }
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
//                    PPApplication.logE("PhoneCallsListener.onCallStateChanged", "state=CALL_STATE_IDLE");
                    //Went to idle-  this is the end of a call.  What type depends on previous state(s)
                    if(!inCall){
                        //Ring but no pickup-  a miss
                        onMissedCall(/*savedNumber, eventTime*/);
                    }
                    else
                    {
                        if(isIncoming){
                            onIncomingCallEnded(/*savedNumber, eventTime*/);
                        }
                        else{
                            onOutgoingCallEnded(/*savedNumber, eventTime*/);
                        }
                        inCall = false;
                    }
                    break;
            }
            lastState = state;
        }
    }

    public void onServiceStateChanged(ServiceState serviceState) {
        super.onServiceStateChanged(serviceState);

        /*
        TelephonyManager telephonyManager;
        if (forSimCard == 1)
            telephonyManager = PPApplication.telephonyManagerSIM1;
        else
        if (forSimCard == 2)
            telephonyManager = PPApplication.telephonyManagerSIM2;
        else
            telephonyManager = PPApplication.telephonyManagerDefault;

        if (telephonyManager.isNetworkRoaming()) {
            // In Roaming
            Log.e("PhoneCallsListener.onServiceStateChanged", "is in roaming - telephony manager - network");
        } else {
            // Not in Roaming
            Log.e("PhoneCallsListener.onServiceStateChanged", "is NOT in roaming - telephony manager - network");
        }
        if (serviceState.getDataRoaming()) {
            Log.e("PhoneCallsListener.onServiceStateChanged", "is in roaming - service state - data");
        } else {
            Log.e("PhoneCallsListener.onServiceStateChanged", "is NOT in roaming - service state - data");
        }
        */

        /*if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            Object ret = HiddenApiBypass.invoke(ServiceState.class, serviceState, "getVoiceRoaming");
            Log.e("PhoneCallsListener.onServiceStateChanged", "ret="+ret);
        }*/
        // You can also check roaming state using this
        if (serviceState.getRoaming()) {
            // In Roaming
            Log.e("PhoneCallsListener.onServiceStateChanged", "is in roaming - service state - network");
            networkRoaming = true;
        } else {
            // Not in Roaming
            Log.e("PhoneCallsListener.onServiceStateChanged", "is NOT in roaming - service state - network");
            networkRoaming = false;
        }
        if (serviceState.getDataRoaming()) {
            Log.e("PhoneCallsListener.onServiceStateChanged", "is in roaming - service state - data");
            dataRoaming = true;
        } else {
            Log.e("PhoneCallsListener.onServiceStateChanged", "is NOT in roaming - service state - data");
            dataRoaming = false;
        }

        /*
        int dataRoaming = Settings.Global.getInt(savedContext.getContentResolver(), Settings.Global.DATA_ROAMING, 0);
        Log.e("PhoneCallsListener.onServiceStateChanged", "dataRoaming="+dataRoaming);
        if (dataRoaming == 1) {
            Log.e("PhoneCallsListener.onServiceStateChanged", "is in roaming - settings global - data");
        } else {
            Log.e("PhoneCallsListener.onServiceStateChanged", "is NOT in roaming - settings global - data");
        }
        */

        EventPreferencesRoaming.setEventRoamingInSIMSlot(savedContext, simSlot, networkRoaming, dataRoaming);
        if (Event.getGlobalEventsRunning()) {
            //if (useHandler) {
            final Context appContext = savedContext.getApplicationContext();
            PPApplication.startHandlerThreadBroadcast(/*"PeriodicEventEndBroadcastReceiver.doWork"*/);
            final Handler __handler = new Handler(PPApplication.handlerThreadBroadcast.getLooper());<commit_msg>Added event senzor "Roaming sensor". (17)<commit_after>package sk.henrichg.phoneprofilesplus;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Handler;
import android.os.PowerManager;
import android.os.SystemClock;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;

public class PhoneCallsListener extends PhoneStateListener {

    final Context savedContext;

    int lastState = TelephonyManager.CALL_STATE_IDLE;
    boolean inCall;
    boolean isIncoming;

    boolean networkRoaming;
    boolean dataRoaming;

    int simSlot;

    private static AudioManager audioManager = null;

    private static boolean savedSpeakerphone = false;
    private static boolean speakerphoneSelected = false;

    //static boolean linkUnlinkExecuted = false;
    //static boolean speakerphoneOnExecuted = false;

    private static final int SERVICE_PHONE_EVENT_START = 1;
    private static final int SERVICE_PHONE_EVENT_ANSWER = 2;
    private static final int SERVICE_PHONE_EVENT_END = 3;

    static final int LINKMODE_NONE = 0;
    static final int LINKMODE_LINK = 1;
    static final int LINKMODE_UNLINK = 2;

    PhoneCallsListener(Context context, int simSlot) {
        this.savedContext = context.getApplicationContext();
        this.simSlot = simSlot;
    }

    public void onCallStateChanged (int state, String phoneNumber) {

        if (PPApplication.getApplicationStarted(true)) {
            if(lastState == state){
                //No change, de-bounce extras
                return;
            }

            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
//                    PPApplication.logE("PhoneCallsListener.onCallStateChanged", "state=CALL_STATE_RINGING");
                    //PPPEApplication.logE("PhoneCallsListener.PhoneCallStartEndDetector", "incomingNumber="+incomingNumber);
                    inCall = false;
                    isIncoming = true;
                    onIncomingCallStarted(/*incomingNumber, eventTime*/);
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
//                    PPApplication.logE("PhoneCallsListener.onCallStateChanged", "state=CALL_STATE_OFFHOOK");
                    //Transition of ringing->off hook are pickups of incoming calls.  Nothing down on them
                    if(lastState != TelephonyManager.CALL_STATE_RINGING){
                        inCall = true;
                        isIncoming = false;
                        onOutgoingCallAnswered(/*savedNumber, eventTime*/);
                    }
                    else
                    {
                        inCall = true;
                        isIncoming = true;
                        onIncomingCallAnswered(/*savedNumber, eventTime*/);
                    }
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
//                    PPApplication.logE("PhoneCallsListener.onCallStateChanged", "state=CALL_STATE_IDLE");
                    //Went to idle-  this is the end of a call.  What type depends on previous state(s)
                    if(!inCall){
                        //Ring but no pickup-  a miss
                        onMissedCall(/*savedNumber, eventTime*/);
                    }
                    else
                    {
                        if(isIncoming){
                            onIncomingCallEnded(/*savedNumber, eventTime*/);
                        }
                        else{
                            onOutgoingCallEnded(/*savedNumber, eventTime*/);
                        }
                        inCall = false;
                    }
                    break;
            }
            lastState = state;
        }
    }

    public void onServiceStateChanged(ServiceState serviceState) {
        super.onServiceStateChanged(serviceState);

        /*
        TelephonyManager telephonyManager;
        if (simSlot == 1)
            telephonyManager = PPApplication.telephonyManagerSIM1;
        else
        if (simSlot == 2)
            telephonyManager = PPApplication.telephonyManagerSIM2;
        else
            telephonyManager = PPApplication.telephonyManagerDefault;

        if (telephonyManager.isNetworkRoaming()) {
            // In Roaming
            Log.e("PhoneCallsListener.onServiceStateChanged", "is in roaming - telephony manager - network");
        } else {
            // Not in Roaming
            Log.e("PhoneCallsListener.onServiceStateChanged", "is NOT in roaming - telephony manager - network");
        }
        if (serviceState.getDataRoaming()) {
            Log.e("PhoneCallsListener.onServiceStateChanged", "is in roaming - service state - data");
        } else {
            Log.e("PhoneCallsListener.onServiceStateChanged", "is NOT in roaming - service state - data");
        }
        */

        /*if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            Object ret = HiddenApiBypass.invoke(ServiceState.class, serviceState, "getVoiceRoaming");
            Log.e("PhoneCallsListener.onServiceStateChanged", "ret="+ret);
        }*/
        // You can also check roaming state using this
        if (serviceState.getRoaming()) {
            // In Roaming
//            Log.e("PhoneCallsListener.onServiceStateChanged", "is in roaming - service state - network");
            networkRoaming = true;
        } else {
            // Not in Roaming
//            Log.e("PhoneCallsListener.onServiceStateChanged", "is NOT in roaming - service state - network");
            networkRoaming = false;
        }
        if (serviceState.getDataRoaming()) {
//            Log.e("PhoneCallsListener.onServiceStateChanged", "is in roaming - service state - data");
            dataRoaming = true;
        } else {
//            Log.e("PhoneCallsListener.onServiceStateChanged", "is NOT in roaming - service state - data");
            dataRoaming = false;
        }

        /*
        int dataRoaming = Settings.Global.getInt(savedContext.getContentResolver(), Settings.Global.DATA_ROAMING, 0);
        Log.e("PhoneCallsListener.onServiceStateChanged", "dataRoaming="+dataRoaming);
        if (dataRoaming == 1) {
            Log.e("PhoneCallsListener.onServiceStateChanged", "is in roaming - settings global - data");
        } else {
            Log.e("PhoneCallsListener.onServiceStateChanged", "is NOT in roaming - settings global - data");
        }
        */

        EventPreferencesRoaming.setEventRoamingInSIMSlot(savedContext, simSlot, networkRoaming, dataRoaming);
        if (Event.getGlobalEventsRunning()) {
            //if (useHandler) {
            final Context appContext = savedContext.getApplicationContext();
            PPApplication.startHandlerThreadBroadcast(/*"PeriodicEventEndBroadcastReceiver.doWork"*/);
            final Handler __handler = new Handler(PPApplication.handlerThreadBroadcast.getLooper());
{'fraction_non_alphanumeric': 0.08556605784947766, 'fraction_numerical': 0.001847772013360813, 'mean_word_length': 1.9959548648073238, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3232542', 'n_tokens_mistral': 3650, 'n_tokens_neox': 3499, 'n_words': 772}