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

Commit fcbced38 authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

DO NOT MERGE Debounce wifi country code Info

We're getting signals from the radio and it sometimes drops out for
a while.  This change will delay applying an empty country code
for 15sec but apply non-empty country codes immediately.  It uses a
sequence number so we only apply the most recent change, even if
it's delayed.

Also secures the wifi call to set the country code as we can't
allow apps to set it willy-nilly.

bug:11062898
Change-Id: I610039a833e26d4c5c8b7b9ac1b7546f3c83446a
parent f175b19a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -870,7 +870,7 @@ public final class WifiService extends IWifiManager.Stub {
    public void setCountryCode(String countryCode, boolean persist) {
        Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
                " with persist set to " + persist);
        enforceChangePermission();
        enforceConnectivityInternalPermission();
        final long token = Binder.clearCallingIdentity();
        try {
            mWifiStateMachine.setCountryCode(countryCode, persist);
+35 −17
Original line number Diff line number Diff line
@@ -233,6 +233,10 @@ public class WifiStateMachine extends StateMachine {
    private DhcpStateMachine mDhcpStateMachine;
    private boolean mDhcpActive = false;

    // Delay in switching to null country code (non-null has no delay)
    private final int COUNTRY_CODE_DELAY_MS = 15000;
    private final AtomicInteger mCountryCodeSequence = new AtomicInteger();

    private class InterfaceObserver extends BaseNetworkObserver {
        private WifiStateMachine mWifiStateMachine;

@@ -1534,14 +1538,15 @@ public class WifiStateMachine extends StateMachine {
     * @param persist {@code true} if the setting should be remembered.
     */
    public void setCountryCode(String countryCode, boolean persist) {
        if (persist) {
            mPersistedCountryCode = countryCode;
            Settings.Global.putString(mContext.getContentResolver(),
                    Settings.Global.WIFI_COUNTRY_CODE,
                    countryCode);
        // If it's a country code, apply immediately,
        // If it's empty, delay it in case it's a momentary dropout
        int countryCodeSequence = mCountryCodeSequence.incrementAndGet();
        if (TextUtils.isEmpty(countryCode)) {
            sendMessageDelayed(CMD_SET_COUNTRY_CODE, countryCodeSequence, persist ? 1 : 0,
                    countryCode, COUNTRY_CODE_DELAY_MS);
        } else {
            sendMessage(CMD_SET_COUNTRY_CODE, countryCodeSequence, persist ? 1 : 0, countryCode);
        }
        sendMessage(CMD_SET_COUNTRY_CODE, countryCode);
        mWifiP2pChannel.sendMessage(WifiP2pService.SET_COUNTRY_CODE, countryCode);
    }

    /**
@@ -2507,7 +2512,9 @@ public class WifiStateMachine extends StateMachine {
                        // to the driver happened between mPersistedCountryCode getting set
                        // and now, so simply persisting it here would mean we have sent
                        // nothing to the driver.  Send the cmd so it might be set now.
                        sendMessageAtFrontOfQueue(CMD_SET_COUNTRY_CODE, countryCode);
                        int sequenceNum = mCountryCodeSequence.incrementAndGet();
                        sendMessageAtFrontOfQueue(CMD_SET_COUNTRY_CODE,
                                sequenceNum, 0, countryCode);
                    }
                    break;
                case CMD_SET_BATCHED_SCAN:
@@ -3090,8 +3097,19 @@ public class WifiStateMachine extends StateMachine {
                    break;
                case CMD_SET_COUNTRY_CODE:
                    String country = (String) message.obj;
                    final boolean persist = (message.arg2 == 1);
                    final int sequence = message.arg1;
                    if (sequence != mCountryCodeSequence.get()) {
                        if (DBG) log("set country code ignored due to sequence num");
                        break;
                    }
                    if (DBG) log("set country code " + country);
                    if (country != null) {
                    if (persist) {
                        mPersistedCountryCode = country;
                        Settings.Global.putString(mContext.getContentResolver(),
                                Settings.Global.WIFI_COUNTRY_CODE,
                                country);
                    }
                    country = country.toUpperCase(Locale.ROOT);
                    if (mLastSetCountryCode == null
                            || country.equals(mLastSetCountryCode) == false) {
@@ -3101,7 +3119,7 @@ public class WifiStateMachine extends StateMachine {
                            loge("Failed to set country code " + country);
                        }
                    }
                    }
                    mWifiP2pChannel.sendMessage(WifiP2pService.SET_COUNTRY_CODE, country);
                    break;
                case CMD_SET_FREQUENCY_BAND:
                    int band =  message.arg1;