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

Commit 68b0057f authored by Weng Su's avatar Weng Su
Browse files

Smart Router settings UI changes

- Remove "AP band" preference

- Add "Maximize Compatibility" switch preference
  - Use bridged mode API for new devices (after P21)

- Screenshot:
  https://screenshot.googleplex.com/84X9Av8gVj3idjB

Bug: 168052744
Test: manual test
atest WifiTetherMaximizeCompatibilityPreferenceControllerTest
make RunSettingsRoboTests ROBOTEST_FILTER=WifiTetherSettingsTest

Change-Id: Ib74156c0fa6eccdd13239854047b1fb4e49a293c
parent a922c902
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -2416,6 +2416,10 @@
    <string name="wifi_hotspot_auto_off_title">Turn off hotspot automatically</string>
    <!-- Summary for the toggle to turn off hotspot automatically [CHAR LIMIT=NONE]-->
    <string name="wifi_hotspot_auto_off_summary">When no devices are connected</string>
    <!-- Title for the toggle to enable/disable the maximize compatibility [CHAR LIMIT=NONE]-->
    <string name="wifi_hotspot_maximize_compatibility">Maximize compatibility</string>
    <!-- Summary for the toggle to show the maximize compatibility warning message [CHAR LIMIT=NONE]-->
    <string name="wifi_hotspot_maximize_compatibility_summary">This may reduce speed for devices connected to this hotspot and use more power</string>
    <!-- Summary text when turning hotspot on -->
    <string name="wifi_tether_starting">Turning hotspot on\u2026</string>
+5 −4
Original line number Diff line number Diff line
@@ -37,12 +37,13 @@
        android:persistent="false"
        android:title="@string/wifi_hotspot_password_title"/>

    <ListPreference
        android:key="wifi_tether_network_ap_band"
        android:title="@string/wifi_hotspot_ap_band_title"/>

    <SwitchPreference
        android:key="wifi_tether_auto_turn_off"
        android:title="@string/wifi_hotspot_auto_off_title"
        android:summary="@string/wifi_hotspot_auto_off_summary"/>

    <SwitchPreference
        android:key="wifi_tether_maximize_compatibility"
        android:title="@string/wifi_hotspot_maximize_compatibility"
        android:summary="@string/wifi_hotspot_maximize_compatibility_summary"/>
</PreferenceScreen>
+124 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.wifi.tether;

import android.content.Context;
import android.net.wifi.SoftApConfiguration;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.SwitchPreference;

/**
 * This controller helps to manage the state of maximize compatibility switch preference.
 */
public class WifiTetherMaximizeCompatibilityPreferenceController extends
        WifiTetherBasePreferenceController {

    private static final String TAG = "WifiTetherMaximizeCompatibilityPref";
    public static final String PREF_KEY = "wifi_tether_maximize_compatibility";

    private boolean mIsChecked;

    public WifiTetherMaximizeCompatibilityPreferenceController(Context context,
            WifiTetherBasePreferenceController.OnTetherConfigUpdateListener listener) {
        super(context, listener);
        mIsChecked = isMaximizeCompatibilityEnabled();
    }

    @Override
    public String getPreferenceKey() {
        return PREF_KEY;
    }

    @Override
    public void updateDisplay() {
        if (mPreference == null) {
            return;
        }
        mPreference.setEnabled(is5GhzBandSupported());
        ((SwitchPreference) mPreference).setChecked(mIsChecked);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        mIsChecked = (Boolean) newValue;
        if (mListener != null) {
            mListener.onTetherConfigUpdated(this);
        }
        return true;
    }

    private boolean is5GhzBandSupported() {
        if (mWifiManager == null) {
            return false;
        }
        if (!mWifiManager.is5GHzBandSupported() || mWifiManager.getCountryCode() == null) {
            return false;
        }
        return true;
    }

    @VisibleForTesting
    boolean isMaximizeCompatibilityEnabled() {
        if (mWifiManager == null) {
            return false;
        }
        final SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
        if (config == null) {
            return false;
        }
        if (mWifiManager.isBridgedApConcurrencySupported()) {
            final boolean isEnabled = config.isBridgedModeOpportunisticShutdownEnabled();
            Log.d(TAG, "isBridgedModeOpportunisticShutdownEnabled:" + isEnabled);
            return isEnabled;
        }

        // If the BridgedAp Concurrency is not supported in early Pixel devices (e.g. Pixel 2~5),
        // show toggle on if the band includes SoftApConfiguration.BAND_5GHZ.
        final int band = config.getBand();
        Log.d(TAG, "getBand:" + band);
        return (band & SoftApConfiguration.BAND_5GHZ) > 0;
    }

    /**
     * Setup the Maximize Compatibility setting to the SoftAp Configuration
     *
     * @param builder The builder to build the SoftApConfiguration.
     */
    public void setupMaximizeCompatibility(SoftApConfiguration.Builder builder) {
        if (builder == null) {
            return;
        }
        final boolean enabled = mIsChecked;
        if (mWifiManager.isBridgedApConcurrencySupported()) {
            int[] bands = {
                    SoftApConfiguration.BAND_2GHZ,
                    SoftApConfiguration.BAND_2GHZ | SoftApConfiguration.BAND_5GHZ};
            builder.setBands(bands);
            Log.d(TAG, "setBridgedModeOpportunisticShutdownEnabled:" + enabled);
            builder.setBridgedModeOpportunisticShutdownEnabled(enabled);
        } else {
            int band = enabled
                    ? SoftApConfiguration.BAND_2GHZ | SoftApConfiguration.BAND_5GHZ
                    : SoftApConfiguration.BAND_2GHZ;
            Log.d(TAG, "setBand:" + band);
            builder.setBand(band);
        }
    }
}
+19 −28
Original line number Diff line number Diff line
@@ -54,8 +54,7 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
    private static final String TAG = "WifiTetherSettings";
    private static final IntentFilter TETHER_STATE_CHANGE_FILTER;
    private static final String KEY_WIFI_TETHER_SCREEN = "wifi_tether_settings_screen";
    private static final int EXPANDED_CHILD_COUNT_WITH_SECURITY_NON = 3;
    private static final int EXPANDED_CHILD_COUNT_DEFAULT = 4;
    private static final int EXPANDED_CHILD_COUNT_DEFAULT = 3;

    @VisibleForTesting
    static final String KEY_WIFI_TETHER_NETWORK_NAME = "wifi_tether_network_name";
@@ -64,13 +63,14 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
    @VisibleForTesting
    static final String KEY_WIFI_TETHER_AUTO_OFF = "wifi_tether_auto_turn_off";
    @VisibleForTesting
    static final String KEY_WIFI_TETHER_NETWORK_AP_BAND = "wifi_tether_network_ap_band";
    static final String KEY_WIFI_TETHER_MAXIMIZE_COMPATIBILITY =
            WifiTetherMaximizeCompatibilityPreferenceController.PREF_KEY;

    private WifiTetherSwitchBarController mSwitchBarController;
    private WifiTetherSSIDPreferenceController mSSIDPreferenceController;
    private WifiTetherPasswordPreferenceController mPasswordPreferenceController;
    private WifiTetherApBandPreferenceController mApBandPreferenceController;
    private WifiTetherSecurityPreferenceController mSecurityPreferenceController;
    private WifiTetherMaximizeCompatibilityPreferenceController mMaxCompatibilityPrefController;

    private WifiManager mWifiManager;
    private boolean mRestartWifiApAfterConfigChange;
@@ -116,7 +116,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
        mSSIDPreferenceController = use(WifiTetherSSIDPreferenceController.class);
        mSecurityPreferenceController = use(WifiTetherSecurityPreferenceController.class);
        mPasswordPreferenceController = use(WifiTetherPasswordPreferenceController.class);
        mApBandPreferenceController = use(WifiTetherApBandPreferenceController.class);
        mMaxCompatibilityPrefController =
                use(WifiTetherMaximizeCompatibilityPreferenceController.class);
    }

    @Override
@@ -180,10 +181,9 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
        controllers.add(new WifiTetherSSIDPreferenceController(context, listener));
        controllers.add(new WifiTetherSecurityPreferenceController(context, listener));
        controllers.add(new WifiTetherPasswordPreferenceController(context, listener));
        controllers.add(new WifiTetherApBandPreferenceController(context, listener));
        controllers.add(
                new WifiTetherAutoOffPreferenceController(context, KEY_WIFI_TETHER_AUTO_OFF));

        controllers.add(new WifiTetherMaximizeCompatibilityPreferenceController(context, listener));
        return controllers;
    }

@@ -219,7 +219,7 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
                    mPasswordPreferenceController.getPasswordValidated(securityType),
                    securityType);
        }
        configBuilder.setBand(mApBandPreferenceController.getBandIndex());
        mMaxCompatibilityPrefController.setupMaximizeCompatibility(configBuilder);
        return configBuilder.build();
    }

@@ -229,14 +229,10 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
    }

    private void updateDisplayWithNewConfig() {
        use(WifiTetherSSIDPreferenceController.class)
                .updateDisplay();
        use(WifiTetherSecurityPreferenceController.class)
                .updateDisplay();
        use(WifiTetherPasswordPreferenceController.class)
                .updateDisplay();
        use(WifiTetherApBandPreferenceController.class)
                .updateDisplay();
        use(WifiTetherSSIDPreferenceController.class).updateDisplay();
        use(WifiTetherSecurityPreferenceController.class).updateDisplay();
        use(WifiTetherPasswordPreferenceController.class).updateDisplay();
        use(WifiTetherMaximizeCompatibilityPreferenceController.class).updateDisplay();
    }

    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
@@ -250,7 +246,7 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
                        keys.add(KEY_WIFI_TETHER_NETWORK_NAME);
                        keys.add(KEY_WIFI_TETHER_NETWORK_PASSWORD);
                        keys.add(KEY_WIFI_TETHER_AUTO_OFF);
                        keys.add(KEY_WIFI_TETHER_NETWORK_AP_BAND);
                        keys.add(KEY_WIFI_TETHER_MAXIMIZE_COMPATIBILITY);
                    }

                    // Remove duplicate
@@ -294,22 +290,17 @@ public class WifiTetherSettings extends RestrictedDashboardFragment

    private void reConfigInitialExpandedChildCount() {
        final PreferenceGroup screen = getPreferenceScreen();
        if (mSecurityPreferenceController.getSecurityType()
                == SoftApConfiguration.SECURITY_TYPE_OPEN) {
            screen.setInitialExpandedChildrenCount(EXPANDED_CHILD_COUNT_WITH_SECURITY_NON);
            return;
        if (screen != null) {
            screen.setInitialExpandedChildrenCount(getInitialExpandedChildCount());
        }
        screen.setInitialExpandedChildrenCount(EXPANDED_CHILD_COUNT_DEFAULT);
    }

    @Override
    public int getInitialExpandedChildCount() {
        if (mSecurityPreferenceController == null) {
            return EXPANDED_CHILD_COUNT_DEFAULT;
        if (mSecurityPreferenceController != null && mSecurityPreferenceController.getSecurityType()
                == SoftApConfiguration.SECURITY_TYPE_OPEN) {
            return (EXPANDED_CHILD_COUNT_DEFAULT - 1);
        }

        return (mSecurityPreferenceController.getSecurityType()
                == SoftApConfiguration.SECURITY_TYPE_OPEN)
            ? EXPANDED_CHILD_COUNT_WITH_SECURITY_NON : EXPANDED_CHILD_COUNT_DEFAULT;
        return EXPANDED_CHILD_COUNT_DEFAULT;
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -101,7 +101,7 @@ public class WifiTetherSettingsTest {
        assertThat(niks).doesNotContain(WifiTetherSettings.KEY_WIFI_TETHER_NETWORK_NAME);
        assertThat(niks).doesNotContain(WifiTetherSettings.KEY_WIFI_TETHER_NETWORK_PASSWORD);
        assertThat(niks).doesNotContain(WifiTetherSettings.KEY_WIFI_TETHER_AUTO_OFF);
        assertThat(niks).doesNotContain(WifiTetherSettings.KEY_WIFI_TETHER_NETWORK_AP_BAND);
        assertThat(niks).doesNotContain(WifiTetherSettings.KEY_WIFI_TETHER_MAXIMIZE_COMPATIBILITY);
    }

    @Test
@@ -115,7 +115,7 @@ public class WifiTetherSettingsTest {
        assertThat(niks).contains(WifiTetherSettings.KEY_WIFI_TETHER_NETWORK_NAME);
        assertThat(niks).contains(WifiTetherSettings.KEY_WIFI_TETHER_NETWORK_PASSWORD);
        assertThat(niks).contains(WifiTetherSettings.KEY_WIFI_TETHER_AUTO_OFF);
        assertThat(niks).contains(WifiTetherSettings.KEY_WIFI_TETHER_NETWORK_AP_BAND);
        assertThat(niks).contains(WifiTetherSettings.KEY_WIFI_TETHER_MAXIMIZE_COMPATIBILITY);
    }

    @Test
Loading