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

Commit 40560120 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 toast when connecting to particular APs"

parents 22f7a126 5e9823cb
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -2662,4 +2662,6 @@
    <string name="sim_pin_disable_failed">无法关闭PIN锁。</string>
    <string name="sim_pin_attempts">\n您还有%s次尝试机会</string>
    <string name="switch_data_subscription">即将切换到卡%1$s进行数据业务.</string>
    <string name="notify_connect">连接CMCC告警</string>
    <string name="notify_connect_summary">连接到CMCC热点后提示</string>
</resources>
+3 −0
Original line number Diff line number Diff line
@@ -86,4 +86,7 @@

    <!-- Whether to hide wifi hotspot -->
    <bool name="hide_wifi_hotspot">false</bool>

    <!-- Whether notify user that current connected ap is CMCC -->
    <bool name="connect_to_cmcc_notify">false</bool>
</resources>
+2 −0
Original line number Diff line number Diff line
@@ -6257,4 +6257,6 @@
    <string name="hotspot_turn_on_hint">To use WLAN hotspot, turn it on.</string>
    <string name="switch_data_subscription">Data services are now switched to SIM%1$s.</string>
    <string name="switch_data_subscription">Default data preference set to SIM%1$s.</string>
    <string name="notify_connect">CMCC WLAN prompt</string>
    <string name="notify_connect_summary">Prompt me when access to CMCC WLAN</string>
</resources>
+6 −0
Original line number Diff line number Diff line
@@ -60,6 +60,12 @@
            android:summary="@string/wifi_auto_connect_type_summary"
            android:persistent="false"/>

    <CheckBoxPreference
            android:key="notify_ap_connected"
            android:title="@string/notify_connect"
            android:summary="@string/notify_connect_summary"
            android:persistent="false"/>

    <!-- android:dependency="enable_wifi" -->
    <ListPreference
            android:key="sleep_policy"
+158 −87
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.wifi;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -68,20 +69,38 @@ public class AdvancedWifiSettings extends SettingsPreferenceFragment
    private static final String KEY_WPS_PUSH = "wps_push_button";
    private static final String KEY_WPS_PIN = "wps_pin_entry";
    private static final String KEY_SUSPEND_OPTIMIZATIONS = "suspend_optimizations";
    private static final String KEY_AUTO_CONNECT_TYPE = "auto_connect_type";

    private static final String KEY_CURRENT_GATEWAY = "current_gateway";
    private static final String KEY_CURRENT_NETMASK = "current_netmask";
    private static final String KEY_WIFI_GSM_CONNECT_TYPE = "wifi_gsm_connect_type";
    private static final String KEY_GSM_WIFI_CONNECT_TYPE = "gsm_wifi_connect_type";

    private static final String KEY_AUTO_CONNECT_ENABLE = "auto_connect_type";
    private static final String WIFI_AUTO_CONNECT_TYPE = "wifi_autoconn_type";
    private static final int AUTO_CONNECT_ENABLED = 0;
    private static final int AUTO_CONNECT_FATAL_VALUE = -1;
    private static final int AUTO_CONNECT_DEFAULT_VALUE = AUTO_CONNECT_ENABLED;

    private static final String KEY_CELLULAR_WLAN = "gsm_wifi_connect_type";
    private static final String DATA_TO_WIFI_CONNECT_TYPE = "data_to_wifi_connect_type";
    private static final int DATA_WIFI_CONNECT_TYPE_AUTO = AUTO_CONNECT_ENABLED;
    private static final int DATA_WIFI_CONNECT_TYPE_MANUAL = 1;
    private static final int DATA_WIFI_CONNECT_TYPE_ASK = 2;
    private static final int CELLULAR_WLAN_DEFAULT_VALUE = AUTO_CONNECT_ENABLED;

    private static final String KEY_WLAN_CELLULAR_HINT = "wifi_gsm_connect_type";
    private static final String WLAN_CELLULAR_HINT = "wifi_gsm_connect_type";

    private static final String KEY_CONNECT_NOTIFY = "notify_ap_connected";
    private static final String NOTIFY_USER_CONNECT = "notify_user_when_connect_cmcc";

    private static final int NOTIFY_USER = 0;
    private static final int DO_NOT_NOTIFY_USER = -1;

    private WifiManager mWifiManager;
    private NetworkScoreManager mNetworkScoreManager;
    private static final int WPS_PBC_DIALOG_ID = 1;
    private static final int WPS_PIN_DIALOG_ID = 2;

    CheckBoxPreference AutoPref;
    ListPreference cell2wifiPref;
    CheckBoxPreference mAutoConnectionEnablePref;
    ListPreference mCellularToWlanPref;

    private IntentFilter mFilter;
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@@ -235,54 +254,94 @@ public class AdvancedWifiSettings extends SettingsPreferenceFragment
            updateSleepPolicySummary(sleepPolicyPref, stringValue);
        }

        CheckBoxPreference wifi2cellPref =
                (CheckBoxPreference) findPreference(KEY_WIFI_GSM_CONNECT_TYPE);
        if (wifi2cellPref != null) {
            if (getResources().getBoolean(R.bool.wifi_to_cell)) {
                wifi2cellPref.setChecked(Settings.System.getInt(getContentResolver(),
                        getResources().getString(R.string.wifi2cell_connect_type),
                        getResources().getInteger(R.integer.wifi2cell_connect_type_ask))
                        == getResources().getInteger(R.integer.wifi2cell_connect_type_ask));
                wifi2cellPref.setOnPreferenceChangeListener(this);
        mAutoConnectionEnablePref =
                (CheckBoxPreference) findPreference(KEY_AUTO_CONNECT_ENABLE);
        if (mAutoConnectionEnablePref != null) {
            if (getResources().getBoolean(R.bool.config_auto_connect_wifi_enabled)) {
                mAutoConnectionEnablePref.setChecked(isAutoConnectEnabled());
                mAutoConnectionEnablePref.setOnPreferenceChangeListener(this);
            } else {
                getPreferenceScreen().removePreference(wifi2cellPref);
                getPreferenceScreen().removePreference(mAutoConnectionEnablePref);
            }
        }

        mCellularToWlanPref =
                (ListPreference) findPreference(KEY_CELLULAR_WLAN);
        if (mCellularToWlanPref != null) {
            if (getResources().getBoolean(R.bool.cell_to_wifi)) {
                int value = getCellularToWlanValue();
                mCellularToWlanPref.setValue(String.valueOf(value));
                updateCellToWlanSummary(mCellularToWlanPref, value);
                mCellularToWlanPref.setOnPreferenceChangeListener(this);
            } else {
            Log.d(TAG, "Fail to get wifi2cell pref");
                getPreferenceScreen().removePreference(mCellularToWlanPref);
            }
        }

        AutoPref = (CheckBoxPreference) findPreference(KEY_AUTO_CONNECT_TYPE);
        if (AutoPref != null) {
            if (getResources().getBoolean(R.bool.config_auto_connect_wifi_enabled)) {
                AutoPref.setChecked(Settings.System.getInt(getContentResolver(),
                        getResources().getString(R.string.wifi_autoconn_type),
                        getResources().getInteger(R.integer.wifi_autoconn_type_auto)) ==
                        getResources().getInteger(R.integer.wifi_autoconn_type_auto));
                AutoPref.setOnPreferenceChangeListener(this);
        CheckBoxPreference wlanToCellularPref =
                (CheckBoxPreference) findPreference(KEY_WLAN_CELLULAR_HINT);
        if (wlanToCellularPref != null) {
            if (getResources().getBoolean(R.bool.wifi_to_cell)) {
                wlanToCellularPref.setChecked(isWlanToCellHintEnable());
                wlanToCellularPref.setOnPreferenceChangeListener(this);
            } else {
                getPreferenceScreen().removePreference(AutoPref);
                getPreferenceScreen().removePreference(wlanToCellularPref);
            }
        }

        CheckBoxPreference notifyConnectedApPref =
                (CheckBoxPreference) findPreference(KEY_CONNECT_NOTIFY);
        if (notifyConnectedApPref != null) {
            if (getResources().getBoolean(R.bool.connect_to_cmcc_notify)) {
                notifyConnectedApPref.setChecked(ifNotifyConnect());
                notifyConnectedApPref.setOnPreferenceChangeListener(this);
            } else {
            Log.d(TAG, "Fail to get auto connect pref");
                getPreferenceScreen().removePreference(notifyConnectedApPref);
            }
        }
    }

    private boolean isWlanToCellHintEnable() {
        return Settings.System.getInt(getActivity().getContentResolver(),
                WLAN_CELLULAR_HINT, NOTIFY_USER) == NOTIFY_USER;
    }

        String data2wifiKey = getActivity().getString(R.string.data_to_wifi_connect_key);
        String data2wifiValueAuto = getActivity().getString(
                R.string.data_to_wifi_connect_value_auto);
        cell2wifiPref = (ListPreference) findPreference(KEY_GSM_WIFI_CONNECT_TYPE);
    private void setWlanToCellularHintEnable(boolean enable) {
        if (enable) {
            Settings.System.putInt(getActivity().getContentResolver(),
                    WLAN_CELLULAR_HINT, NOTIFY_USER);
        } else {
            Settings.System.putInt(getActivity().getContentResolver(),
                    WLAN_CELLULAR_HINT, DO_NOT_NOTIFY_USER);
        }
    }

        if (cell2wifiPref != null) {
            if (getResources().getBoolean(R.bool.cell_to_wifi)) {
                int value = Settings.System.getInt(getContentResolver(), data2wifiKey,
                        Integer.parseInt(data2wifiValueAuto));
                cell2wifiPref.setValue(String.valueOf(value));
                updateCellToWifiSummary(cell2wifiPref, value);
                cell2wifiPref.setOnPreferenceChangeListener(this);
    private boolean ifNotifyConnect() {
        return Settings.System.getInt(getActivity().getContentResolver(),
                NOTIFY_USER_CONNECT, NOTIFY_USER) == NOTIFY_USER;
    }

    private boolean isAutoConnectEnabled() {
        return Settings.System.getInt(getActivity().getContentResolver(),
                WIFI_AUTO_CONNECT_TYPE, AUTO_CONNECT_ENABLED) == AUTO_CONNECT_ENABLED;
    }

    private void setAutoConnectTypeEnabled(boolean enable) {
        if (enable) {
            Settings.System.putInt(getActivity().getContentResolver(),
                    WIFI_AUTO_CONNECT_TYPE, AUTO_CONNECT_ENABLED);
        } else {
                getPreferenceScreen().removePreference(cell2wifiPref);
            Settings.System.putInt(getActivity().getContentResolver(),
                    WIFI_AUTO_CONNECT_TYPE, AUTO_CONNECT_FATAL_VALUE);
        }
    }

    private int getCellularToWlanValue() {
        if (isAutoConnectEnabled()) {
            return DATA_WIFI_CONNECT_TYPE_AUTO;
        } else {
            Log.d(TAG, "Fail to get cellular2wifi pref");
            return Settings.System.getInt(getContentResolver(), DATA_TO_WIFI_CONNECT_TYPE,
                    DATA_WIFI_CONNECT_TYPE_AUTO);
        }
    }

@@ -306,7 +365,7 @@ public class AdvancedWifiSettings extends SettingsPreferenceFragment
        Log.e(TAG, "Invalid sleep policy value: " + value);
    }

    private void updateCellToWifiSummary(Preference preference, int index) {
    private void updateCellToWlanSummary(Preference preference, int index) {
        String[] summaries = getResources().getStringArray(R.array.cellcular2wifi_entries);
        preference.setSummary(summaries[index]);
    }
@@ -386,65 +445,77 @@ public class AdvancedWifiSettings extends SettingsPreferenceFragment
            }
        }

        if (KEY_WIFI_GSM_CONNECT_TYPE.equals(key)) {
            Log.d(TAG, "wifi2cell connect type is " + newValue);
        if (KEY_WLAN_CELLULAR_HINT.equals(key)) {
            boolean checked = ((Boolean) newValue).booleanValue();
            Settings.System.putInt(getContentResolver(),
                    getResources().getString(R.string.wifi2cell_connect_type),
                    checked ? getResources().getInteger(R.integer.wifi2cell_connect_type_ask)
                            : getResources().getInteger(R.integer.wifi2cell_connect_type_auto));
            setWlanToCellularHintEnable(checked);
        }

        if (KEY_AUTO_CONNECT_TYPE.equals(key)) {
        if (KEY_AUTO_CONNECT_ENABLE.equals(key)) {
            boolean checked = ((Boolean) newValue).booleanValue();
            setAutoPreference(checked);
            setAutoConnectTypeEnabled(checked);
            updateCellToWifiPref(checked);
            if (checked) {
                String data2wifiValueAuto = getActivity().getString(
                        R.string.data_to_wifi_connect_value_auto);
                setCellToWifiPreference(Integer.parseInt(data2wifiValueAuto));
            } else {
                String data2wifiValueManul = getActivity().getString(
                        R.string.data_to_wifi_connect_value_manul);
                setCellToWifiPreference(Integer.parseInt(data2wifiValueManul));
                WifiManager wm = (WifiManager) getActivity().getSystemService(Service.WIFI_SERVICE);
                if (wm.isWifiEnabled()) {
                    wm.reconnect();
                }
            }

        }

        if (KEY_GSM_WIFI_CONNECT_TYPE.equals(key)) {
            Log.d(TAG, "Gsm to Wifi connect type is " + newValue);
            String data2wifiValueAuto = getActivity().getString(
                    R.string.data_to_wifi_connect_value_auto);
            String data2wifiValueManul = getActivity().getString(
                    R.string.data_to_wifi_connect_value_manul);
            try {
        if (KEY_CELLULAR_WLAN.equals(key)) {
            int value = Integer.parseInt(((String) newValue));
                setCellToWifiPreference(value);
                if (value == Integer.parseInt(data2wifiValueAuto)) {
                    setAutoPreference(true);
                } else if (value == Integer.parseInt(data2wifiValueManul)) {
                    setAutoPreference(false);
            setCellToWlanType(value);
            mCellularToWlanPref.setValue(String.valueOf(value));
            updateCellToWlanSummary(mCellularToWlanPref, value);
            updateAutoConnectPref(value == DATA_WIFI_CONNECT_TYPE_AUTO);
        }

        if (KEY_CONNECT_NOTIFY.equals(key)) {
            boolean checked = ((Boolean) newValue).booleanValue();
            setApConnectedNotify(checked);
        }

        return true;
    }

    private void setApConnectedNotify(boolean needNotify) {
        if (needNotify) {
            Settings.System.putInt(getActivity().getContentResolver(),
                    NOTIFY_USER_CONNECT, NOTIFY_USER);
        } else {
            Settings.System.putInt(getActivity().getContentResolver(),
                    NOTIFY_USER_CONNECT, DO_NOT_NOTIFY_USER);
        }
    }

    private void setCellToWlanType(int value) {
        try {
            Settings.System.putInt(getContentResolver(), DATA_TO_WIFI_CONNECT_TYPE,
                    value);
        } catch (NumberFormatException e) {
            Toast.makeText(getActivity(), R.string.wifi_setting_connect_type_error,
                    Toast.LENGTH_SHORT).show();
        }
    }
        return true;
    }

    private void setAutoPreference(boolean check) {
        Settings.System.putInt(getContentResolver(),
                getResources().getString(R.string.wifi_autoconn_type),
                check ? getResources().getInteger(R.integer.wifi_autoconn_type_auto)
                        : getResources().getInteger(R.integer.wifi_autoconn_type_manual));
        AutoPref.setChecked(check);
    private void updateCellToWifiPref(boolean isAutoEnabled) {
        int value = DATA_WIFI_CONNECT_TYPE_AUTO;
        if (!isAutoEnabled) {
            value = Settings.System.getInt(getContentResolver(), DATA_TO_WIFI_CONNECT_TYPE,
                    DATA_WIFI_CONNECT_TYPE_AUTO);
            if (DATA_WIFI_CONNECT_TYPE_AUTO == value) {
                Settings.System.putInt(getContentResolver(), DATA_TO_WIFI_CONNECT_TYPE,
                        DATA_WIFI_CONNECT_TYPE_MANUAL);
                value = DATA_WIFI_CONNECT_TYPE_MANUAL;
            }
        }
        mCellularToWlanPref.setValue(String.valueOf(value));
        updateCellToWlanSummary(mCellularToWlanPref, value);
    }

    private void setCellToWifiPreference(int value) {
        String data2wifiKey = getActivity().getString(R.string.data_to_wifi_connect_key);
        Settings.System.putInt(getContentResolver(), data2wifiKey, value);
        cell2wifiPref.setValue(String.valueOf(value));
        updateCellToWifiSummary(cell2wifiPref, value);
    private void updateAutoConnectPref(boolean isAutoMode) {
        setAutoConnectTypeEnabled(isAutoMode);
        mAutoConnectionEnablePref.setChecked(isAutoMode);
    }

    private void refreshWifiInfo() {