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

Commit c5a8073d authored by Suresh Koleti's avatar Suresh Koleti Committed by Gerrit - the friendly Code Review server
Browse files

IMS: Gray out Wifi calling settings during call

- Gray out WiFi calling settings when call is in progress on any
  subscription.
- Listen for call state changed on both the subscriptions to
  support multi sim use cases

Change-Id: I61d33b817aa2826a99f154a0e551016cfe8f5975
CRs-Fixed: 1053896
parent 0dec0be8
Loading
Loading
Loading
Loading
+77 −23
Original line number Diff line number Diff line
@@ -29,6 +29,8 @@ import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.telephony.CarrierConfigManager;
import android.telephony.PhoneStateListener;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Switch;
@@ -60,16 +62,31 @@ public class WifiCallingSettings extends SettingsPreferenceFragment
    private ListPreference mButtonWfcMode;
    private TextView mEmptyView;

    private int[] mCallState = null;
    private PhoneStateListener[] mPhoneStateListener = null;
    private int mPhoneCount;
    private boolean mValidListener = false;
    private boolean mEditableWfcMode = true;

    private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
    private void initPhoneStateListeners(int phoneId) {
        SubscriptionManager subMgr = SubscriptionManager.from(getActivity());
        final SubscriptionInfo subInfo = subMgr.getActiveSubscriptionInfoForSimSlotIndex(phoneId);

        if (subInfo == null) {
            Log.e(TAG, "initPhoneStateListeners subInfo : " + subInfo +
                    " for phone Id: " + phoneId);
            return;
        }

        int subId = subInfo.getSubscriptionId();
        final int i = phoneId;
        /*
         * Enable/disable controls when in/out of a call and depending on
         * TTY mode and TTY support over VoLTE.
         * @see android.telephony.PhoneStateListener#onCallStateChanged(int,
         * java.lang.String)
         */
        mPhoneStateListener[phoneId]  = new PhoneStateListener(subId) {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                final SettingsActivity activity = (SettingsActivity) getActivity();
@@ -79,16 +96,16 @@ public class WifiCallingSettings extends SettingsPreferenceFragment
                boolean isWfcEnabled = switchBar.getSwitch().isChecked()
                        && isNonTtyOrTtyOnVolteEnabled;

            switchBar.setEnabled((state == TelephonyManager.CALL_STATE_IDLE)
                    && isNonTtyOrTtyOnVolteEnabled);
                mCallState[i] = state;
                switchBar.setEnabled(isCallStateIdle() && isNonTtyOrTtyOnVolteEnabled);

                Preference pref = getPreferenceScreen().findPreference(BUTTON_WFC_MODE);
                if (pref != null) {
                pref.setEnabled(isWfcEnabled
                        && (state == TelephonyManager.CALL_STATE_IDLE));
                    pref.setEnabled(isWfcEnabled && isCallStateIdle());
                }
            }
        };
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
@@ -182,6 +199,10 @@ public class WifiCallingSettings extends SettingsPreferenceFragment
            mButtonWfcMode.setEntries(R.array.wifi_calling_mode_choices_without_wifi_only);
            mButtonWfcMode.setEntryValues(R.array.wifi_calling_mode_values_without_wifi_only);
        }

        mPhoneCount = TelephonyManager.getDefault().getPhoneCount();
        mPhoneStateListener = new PhoneStateListener[mPhoneCount];
        mCallState = new int[mPhoneCount];
    }

    @Override
@@ -191,8 +212,7 @@ public class WifiCallingSettings extends SettingsPreferenceFragment
        final Context context = getActivity();

        if (ImsManager.isWfcEnabledByPlatform(context)) {
            TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
            registerPhoneStateListeners(context);

            mSwitchBar.addOnSwitchChangeListener(this);

@@ -224,8 +244,7 @@ public class WifiCallingSettings extends SettingsPreferenceFragment
        if (mValidListener) {
            mValidListener = false;

            TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
            unRegisterPhoneStateListeners(context);

            mSwitchBar.removeOnSwitchChangeListener(this);
        }
@@ -233,6 +252,30 @@ public class WifiCallingSettings extends SettingsPreferenceFragment
        context.unregisterReceiver(mIntentReceiver);
    }

    private void registerPhoneStateListeners(Context context) {
        TelephonyManager tm =
                (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        for (int i = 0; i < mPhoneCount; i++) {
            initPhoneStateListeners(i);
            if (mPhoneStateListener[i] != null) {
                Log.d(TAG, "Register for call state change for phone Id: " + i);
                tm.listen(mPhoneStateListener[i], PhoneStateListener.LISTEN_CALL_STATE);
            }
        }
    }

    private void unRegisterPhoneStateListeners(Context context) {
        TelephonyManager tm =
               (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        for (int i = 0; i < mPhoneCount; i++) {
            if (mPhoneStateListener[i] != null) {
                Log.d(TAG, "unRegister for call state change for phone Id: " + i);
                tm.listen(mPhoneStateListener[i], PhoneStateListener.LISTEN_NONE);
                mPhoneStateListener[i] = null;
            }
        }
    }

    /**
     * Listens to the state change of the switch.
     */
@@ -299,4 +342,15 @@ public class WifiCallingSettings extends SettingsPreferenceFragment
        }
        return resId;
    }

    private boolean isCallStateIdle() {
        boolean callStateIdle = true;
        for (int i = 0; i < mCallState.length; i++) {
            if (TelephonyManager.CALL_STATE_IDLE != mCallState[i]) {
                callStateIdle = false;
            }
        }
        Log.d(TAG, "isCallStateIdle " + callStateIdle);
        return callStateIdle;
    }
}