Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1c6446f6 authored by Stuart Scott's avatar Stuart Scott Committed by Android Git Automerger
Browse files

am b7ae6ecc: am 5625516d: am 956b3dc3: Merge "[DS] Call cannot call out from...

am b7ae6ecc: am 5625516d: am 956b3dc3: Merge "[DS] Call cannot call out from Car kit" into lmp-sprout-dev

* commit 'b7ae6ecc':
  [DS] Call cannot call out from Car kit
parents 92248abf b7ae6ecc
Loading
Loading
Loading
Loading
+166 −103
Original line number Diff line number Diff line
@@ -21,6 +21,14 @@ import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.SubscriptionManager;
import android.content.IntentFilter;
import android.content.Intent;
import android.content.BroadcastReceiver;

import com.android.internal.telephony.TelephonyIntents;
import com.android.internal.telephony.PhoneConstants;

import android.util.Log;
import android.bluetooth.BluetoothDevice;

@@ -29,7 +37,7 @@ import android.bluetooth.BluetoothDevice;
// All methods in this class are not thread safe, donot call them from
// multiple threads. Call them from the HeadsetPhoneStateMachine message
// handler only.
class HeadsetPhoneState {
class HeadsetPhoneState extends BroadcastReceiver{
    private static final String TAG = "HeadsetPhoneState";

    private HeadsetStateMachine mStateMachine;
@@ -63,32 +71,79 @@ class HeadsetPhoneState {

    private boolean mListening = false;

    // when HFP Service Level Connection is established
    private boolean mSlcReady = false;

    private Context mContext = null;

    private PhoneStateListener mPhoneStateListener = null;

    HeadsetPhoneState(Context context, HeadsetStateMachine stateMachine) {
        mStateMachine = stateMachine;
        mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        IntentFilter filter = new IntentFilter();
        filter.addAction(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED);

        mContext = context;
        mContext.registerReceiver(this, filter);

    }

    public void cleanup() {
        mContext.unregisterReceiver(this);
        listenForPhoneState(false);
        mTelephonyManager = null;
        mStateMachine = null;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        Log.d(TAG, "onReceive, intent action = " + action);

        if (action.equals(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED)) {
            listenForPhoneState(false);
            listenForPhoneState(true);
        }
    }

    void listenForPhoneState(boolean start) {

        mSlcReady = start;

        if (start) {
            if (!mListening) {
            startListenForPhoneState();
        } else {
            stopListenForPhoneState();
        }

    }

    private void startListenForPhoneState() {
        if (!mListening && mSlcReady) {

            // SUB selection, use sim1 always
            long[] subs = SubscriptionManager.getSubId(PhoneConstants.SIM_ID_1);

            if (subs != null && subs[0] >= 0) {
                mPhoneStateListener = getPhoneStateListener(subs[0]);

                mTelephonyManager.listen(mPhoneStateListener,
                                         PhoneStateListener.LISTEN_SERVICE_STATE |
                                         PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
                mListening = true;
            }
        } else {
        }
    }

    private void stopListenForPhoneState() {
        if (mListening) {

            mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
            mListening = false;
        }
    }
    }

    int getService() {
        return mService;
@@ -177,33 +232,39 @@ class HeadsetPhoneState {
        }
    }

    private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
    private PhoneStateListener getPhoneStateListener(long subId) {
        PhoneStateListener mPhoneStateListener = new PhoneStateListener(subId) {
            @Override
            public void onServiceStateChanged(ServiceState serviceState) {

                mServiceState = serviceState;
                mService = (serviceState.getState() == ServiceState.STATE_IN_SERVICE) ?
                    HeadsetHalConstants.NETWORK_STATE_AVAILABLE :
                    HeadsetHalConstants.NETWORK_STATE_NOT_AVAILABLE;
                setRoam(serviceState.getRoaming() ? HeadsetHalConstants.SERVICE_TYPE_ROAMING
                                                  : HeadsetHalConstants.SERVICE_TYPE_HOME);

                sendDeviceStateChanged();
            }

            @Override
            public void onSignalStrengthsChanged(SignalStrength signalStrength) {

                int prevSignal = mSignal;
            if (mService == HeadsetHalConstants.NETWORK_STATE_NOT_AVAILABLE)
                if (mService == HeadsetHalConstants.NETWORK_STATE_NOT_AVAILABLE) {
                    mSignal = 0;
            else if (signalStrength.isGsm()) {
                } else if (signalStrength.isGsm()) {
                    mSignal = gsmAsuToSignal(signalStrength);
                } else {
                    mSignal = cdmaDbmEcioToSignal(signalStrength);
                }

                // network signal strength is scaled to BT 1-5 levels.
                // This results in a lot of duplicate messages, hence this check
            if (prevSignal != mSignal)
                if (prevSignal != mSignal) {
                    sendDeviceStateChanged();
                }
            }

            /* convert [0,31] ASU signal strength to the [0,5] expected by
             * bluetooth devices. Scale is similar to status bar policy
@@ -278,6 +339,8 @@ class HeadsetPhoneState {
                return (cdmaIconLevel > evdoIconLevel) ?  cdmaIconLevel : evdoIconLevel;
            }
        };
        return mPhoneStateListener;
    }

}