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

Commit 8f189f71 authored by Linux Build Service Account's avatar Linux Build Service Account Committed by Gerrit - the friendly Code Review server
Browse files

Merge "Wifi: Add a feature to control wifi auto connect."

parents c3d7847f bb730b0c
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -2417,6 +2417,25 @@ public final class Settings {
         */
        public static final String EGG_MODE = "egg_mode";

        /**
         * Whether wifi settings will connect to access point automatically
         * 0 = automatically
         * 1 = manually
         * @hide
         */
        public static final String WIFI_AUTO_CONNECT_TYPE = "wifi_auto_connect_type";

        /**
         * Whether wifi settings will connect to access point automatically when
         * network from mobile network transform to Wifi network
         * 0 = automatically
         * 1 = manually
         * 2 = always ask
         *
         * @hide
         */
        public static final String DATA_TO_WIFI_CONNECT_TYPE = "data_to_wifi_connect_type";

         /**
         * Settings to backup. This is here so that it's in the same place as the settings
         * keys and easy to update.

core/res/res/values/bools.xml

100644 → 100755
+1 −0
Original line number Diff line number Diff line
@@ -27,4 +27,5 @@
    <bool name="action_bar_expanded_action_views_exclusive">true</bool>
    <bool name="target_honeycomb_needs_options_menu">true</bool>
    <bool name="flip_controller_fallback_keys">false</bool>
    <bool name="wifi_autocon">false</bool>
</resources>
+3 −0
Original line number Diff line number Diff line
@@ -1799,4 +1799,7 @@
  <java-symbol type="bool" name="config_setup_mms_data" />

  <java-symbol type="string" name="config_partial_segment_expire_age" />

  <!-- for wifi auto connection -->
  <java-symbol type="bool" name="wifi_autocon" />
</resources>
+60 −0
Original line number Diff line number Diff line
@@ -15,11 +15,14 @@
 */

package android.net.wifi;
import com.android.internal.R;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.NetworkInfo;
import android.net.NetworkUtils;
import android.net.NetworkInfo.DetailedState;
import android.net.ProxyProperties;
@@ -36,6 +39,7 @@ import android.os.Message;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.UserHandle;
import android.provider.Settings;
import android.security.KeyStore;
import android.text.TextUtils;
import android.util.LocalLog;
@@ -161,6 +165,9 @@ class WifiConfigStore {
    private WifiNative mWifiNative;
    private final KeyStore mKeyStore = KeyStore.getInstance();

    private static final int WIFI_AUTO_CONNECT_TYPE_AUTO = 0;
    private static final int DATA_TO_WIFI_CONNECT_TYPE_AUTO = 0;

    WifiConfigStore(Context c, WifiNative wn) {
        mContext = c;
        mWifiNative = wn;
@@ -213,11 +220,64 @@ class WifiConfigStore {
        return networks;
    }

    boolean isWifiAuto() {
        int autoConnectPolicy = Settings.System
                .getInt(mContext.getContentResolver(), Settings.System.WIFI_AUTO_CONNECT_TYPE,
                        WIFI_AUTO_CONNECT_TYPE_AUTO);
        return (autoConnectPolicy == WIFI_AUTO_CONNECT_TYPE_AUTO);
    }

    boolean isDataToWifiAuto() {
        int gsmToWifiPolicy = Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.DATA_TO_WIFI_CONNECT_TYPE, DATA_TO_WIFI_CONNECT_TYPE_AUTO);
        return (gsmToWifiPolicy == DATA_TO_WIFI_CONNECT_TYPE_AUTO);
    }

    int existActiveNetwork() {
        ConnectivityManager cm = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info == null)
            return -1;
        return info.getType();
    }

    boolean isWifiAutoConn() {
        // If no active network(info == null) and wifi connection type is auto
        // connect, auto connect to wifi.
        return isWifiAuto() && (existActiveNetwork() == -1);
    }

    boolean isDataToWifiAutoConn() {
        // If the active network type is mobile, wifi connection type is auto
        // connect and GSM to WLAN connection type is auto connect,
        // auto connect to wifi.
        return isWifiAuto()
                && ((existActiveNetwork()
                    == ConnectivityManager.TYPE_MOBILE) &&
                isDataToWifiAuto());

    }

    boolean shouldAutoConnect() {
        if(isWifiAutoConn() || isDataToWifiAutoConn()){
            Log.d(TAG, " wifi or gsm to wlan conn type is auto , should auto connect");
            return true;
        }
        Log.d(TAG, "Shouldn't auto connect");
        return false;
    }

    /**
     * enable all networks and save config. This will be a no-op if the list
     * of configured networks indicates all networks as being enabled
     */
    void enableAllNetworks() {
        if (mContext.getResources().getBoolean(R.bool.wifi_autocon)
                && !(isWifiAuto() && isDataToWifiAuto())) {
            return;
        }

        boolean networkEnabledStateChanged = false;
        for(WifiConfiguration config : mConfiguredNetworks.values()) {
            if(config != null && config.status == Status.DISABLED) {
+26 −0
Original line number Diff line number Diff line
@@ -109,6 +109,7 @@ import java.util.regex.Pattern;
 */
public class WifiStateMachine extends StateMachine {

    private static final String TAG = "WifiStateMachine";
    private static final String NETWORKTYPE = "WIFI";
    private static final boolean DBG = false;

@@ -2260,6 +2261,15 @@ public class WifiStateMachine extends StateMachine {
    private void handleNetworkDisconnect() {
        if (DBG) log("Stopping DHCP and clearing IP");

        // when set wifi connection type is manual, it will disable all
        // network to auto connect. but if connect a hotspot manually,
        // the hotspot will be enable and it will be auto connect in next time
        // so need to disable it again to avoid to auto connect.
        if (mContext.getResources().getBoolean(R.bool.wifi_autocon)
                && !mWifiConfigStore.shouldAutoConnect()) {
            disableLastNetwork();
        }

        stopDhcp();

        try {
@@ -2770,6 +2780,14 @@ public class WifiStateMachine extends StateMachine {
                    mWifiConfigStore.loadAndEnableAllNetworks();
                    initializeWpsDetails();

                    // if don't set auto connect wifi, should disable all
                    // wifi ap to prevent wifi connect automatically when open
                    // wifi switch.
                    if (mContext.getResources().getBoolean(R.bool.wifi_autocon)
                        && !mWifiConfigStore.shouldAutoConnect()) {
                        mWifiConfigStore.disableAllNetworks();
                    }

                    sendSupplicantConnectionChangedBroadcast(true);
                    transitionTo(mDriverStartedState);
                    break;
@@ -4448,4 +4466,12 @@ public class WifiStateMachine extends StateMachine {
        msg.arg2 = srcMsg.arg2;
        return msg;
    }

    void disableLastNetwork() {
        if (getCurrentState() != mSupplicantStoppingState) {
            mWifiConfigStore.disableNetwork(mLastNetworkId,
                    WifiConfiguration.DISABLED_UNKNOWN_REASON);
        }
    }

}