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

Commit ec61e0d9 authored by Franck Lenormand's avatar Franck Lenormand Committed by Andre Eisenbach
Browse files

Prevent javacrash when TELEPHONY_SERVICE is not found



The API context.getSystemService can return NULL
A Javacrash can happend when trying to obtain the TELEPHONY_SERVICE
and getting null instead.

The patch warn about the value of the instance of the service returned.
It also log and prevent its use in function startListenForPhoneState and
stopListenForPhoneState.

If the TELEPHONY_SERVICE is not available, the internal variables wont
change.

Change-Id: I9e4abc88a58f7f037bbc4f3f7c1cf1b44328ce38
Signed-off-by: default avatarfranck Lenormand <franckx.lenormand@intel.com>
Signed-off-by: default avatarZhiquan Liu <zhiquan.liu@intel.com>
parent 6dfb1505
Loading
Loading
Loading
Loading
+20 −7
Original line number Diff line number Diff line
@@ -87,6 +87,10 @@ class HeadsetPhoneState {
    HeadsetPhoneState(Context context, HeadsetStateMachine stateMachine) {
        mStateMachine = stateMachine;
        mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (mTelephonyManager == null) {
            Log.e(TAG, "getSystemService(Context.TELEPHONY_SERVICE) failed, "
                  + "cannot register for SubscriptionInfo changes");
        }
        mContext = context;

        // Register for SubscriptionInfo list changes which is guaranteed
@@ -123,7 +127,10 @@ class HeadsetPhoneState {

            if (SubscriptionManager.isValidSubscriptionId(subId)) {
                mPhoneStateListener = getPhoneStateListener(subId);

                if (mTelephonyManager == null) {
                    Log.e(TAG, "mTelephonyManager is null, "
                         + "cannot start listening for phone state changes");
                } else {
                    mTelephonyManager.listen(mPhoneStateListener,
                                             PhoneStateListener.LISTEN_SERVICE_STATE |
                                             PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
@@ -131,14 +138,20 @@ class HeadsetPhoneState {
                }
            }
        }
    }

    private void stopListenForPhoneState() {
        if (mListening) {

            if (mTelephonyManager == null) {
                Log.e(TAG, "mTelephonyManager is null, "
                     + "cannot send request to stop listening for phone state changes");
            } else {
                mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
                mListening = false;
            }
        }
    }

    int getService() {
        return mService;