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

Commit f9e2a491 authored by Irfan Sheriff's avatar Irfan Sheriff
Browse files

Fix issue to clear scan alarms

If PNO is set after the device is disconnected, the scan
alarm should be cleared to prevent both PNO and alarm
being active at the same time

Bug: 3495698
Change-Id: Id48c87fef68a34a05799e6b82de4088e0573009f
parent 5404812d
Loading
Loading
Loading
Loading
+28 −11
Original line number Diff line number Diff line
@@ -2730,11 +2730,30 @@ public class WifiStateMachine extends HierarchicalStateMachine {

    class DisconnectedState extends HierarchicalState {
        private boolean mAlarmEnabled = false;
        private long mScanIntervalMs;

        private void setScanAlarm(boolean enabled) {
            if (enabled == mAlarmEnabled) return;
            if (enabled) {
                mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        System.currentTimeMillis() + mScanIntervalMs,
                        mScanIntervalMs,
                        mScanIntent);

                mAlarmEnabled = true;
            } else {
                mAlarmManager.cancel(mScanIntent);
                mAlarmEnabled = false;
            }
        }

        @Override
        public void enter() {
            if (DBG) Log.d(TAG, getName() + "\n");
            EventLog.writeEvent(EVENTLOG_WIFI_STATE_CHANGED, getName());

            mScanIntervalMs = Settings.Secure.getLong(mContext.getContentResolver(),
                    Settings.Secure.WIFI_SCAN_INTERVAL_MS, DEFAULT_SCAN_INTERVAL_MS);
            /*
             * We initiate background scanning if it is enabled, otherwise we
             * initiate an infrequent scan that wakes up the device to ensure
@@ -2751,12 +2770,7 @@ public class WifiStateMachine extends HierarchicalStateMachine {
                    WifiNative.enableBackgroundScan(true);
                }
            } else {
                long scanMs = Settings.Secure.getLong(mContext.getContentResolver(),
                    Settings.Secure.WIFI_SCAN_INTERVAL_MS, DEFAULT_SCAN_INTERVAL_MS);

                mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                    System.currentTimeMillis() + scanMs, scanMs, mScanIntent);
                mAlarmEnabled = true;
                setScanAlarm(true);
            }
        }
        @Override
@@ -2774,7 +2788,13 @@ public class WifiStateMachine extends HierarchicalStateMachine {
                    break;
                case CMD_ENABLE_BACKGROUND_SCAN:
                    mEnableBackgroundScan = (message.arg1 == 1);
                    WifiNative.enableBackgroundScan(mEnableBackgroundScan);
                    if (mEnableBackgroundScan) {
                        WifiNative.enableBackgroundScan(true);
                        setScanAlarm(false);
                    } else {
                        WifiNative.enableBackgroundScan(false);
                        setScanAlarm(true);
                    }
                    break;
                    /* Ignore network disconnect */
                case NETWORK_DISCONNECTION_EVENT:
@@ -2811,10 +2831,7 @@ public class WifiStateMachine extends HierarchicalStateMachine {
            if (mEnableBackgroundScan) {
                WifiNative.enableBackgroundScan(false);
            }
            if (mAlarmEnabled) {
                mAlarmManager.cancel(mScanIntent);
                mAlarmEnabled = false;
            }
            setScanAlarm(false);
        }
    }