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

Commit 92340eb8 authored by Zhen Zhang's avatar Zhen Zhang Committed by Android (Google) Code Review
Browse files

Merge "Add ethernet tether option into AllInOneTetherSettings" into rvc-dev

parents 24205d46 c10ec56e
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -3776,14 +3776,31 @@
    <string name="tether_settings_summary_usb_tethering_only">USB only</string>
    <!-- Tethering setting summary when only Bluetooth tethering is on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_bluetooth_tethering_only">Bluetooth only</string>
    <!-- Tethering setting summary when only Ethernet tethering is on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_ethernet_tethering_only">Ethernet only</string>
    <!-- Tethering setting summary when Wi-Fi hotspot and USB tethering are on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_hotspot_and_usb">Hotspot, USB</string>
    <!-- Tethering setting summary when Wi-Fi hotspot and Bluetooth tethering are on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_hotspot_and_bluetooth">Hotspot, Bluetooth</string>
    <!-- Tethering setting summary when Wi-Fi hotspot and Ethernet tethering are on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_hotspot_and_ethernet">Hotspot, Ethernet</string>
    <!-- Tethering setting summary when USB and Bluetooth tethering are on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_usb_and_bluetooth">USB, Bluetooth</string>
    <!-- Tethering setting summary when USB and Ethernet tethering are on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_usb_and_ethernet">USB, Ethernet</string>
    <!-- Tethering setting summary when Bluetooth and Ethernet tethering are on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_bluetooth_and_ethernet">Bluetooth, Ethernet</string>
    <!-- Tethering setting summary when Wi-Fi hotspot and USB and Bluetooth tethering are on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_hotspot_and_usb_and_bluetooth">Hotspot, USB, Bluetooth</string>
    <!-- Tethering setting summary when Wi-Fi hotspot and USB and Ethernet tethering are on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_hotspot_and_usb_and_ethernet">Hotspot, USB, Ethernet</string>
    <!-- Tethering setting summary when Wi-Fi hotspot and Bluetooth and Ethernet tethering are on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_hotspot_and_bluetooth_and_ethernet">Hotspot, Bluetooth, Ethernet</string>
    <!-- Tethering setting summary when USB and Bluetooth and Ethernet tethering are on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_usb_and_bluetooth_and_ethernet">USB, Bluetooth, Ethernet</string>
    <!-- Tethering setting summary when Wi-Fi hotspot and USB and Bluetooth and Ethernet tethering are on [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_all">Hotspot, USB, Bluetooth, Ethernet</string>
    <!-- Tethering setting summary when hotspot and tethering are off [CHAR LIMIT=NONE]-->
    <string name="tether_settings_summary_off">Not sharing internet with other devices</string>
@@ -3797,8 +3814,16 @@
    <string name="disable_wifi_hotspot_when_usb_on">Only share internet via USB</string>
    <!-- Don't use Wi-Fi hotspot summary when Bluetooth tethering is chosen [CHAR LIMIT=NONE]-->
    <string name="disable_wifi_hotspot_when_bluetooth_on">Only share internet via Bluetooth</string>
    <!-- Don't use Wi-Fi hotspot summary when Ethernet tethering is chosen [CHAR LIMIT=NONE]-->
    <string name="disable_wifi_hotspot_when_ethernet_on">Only share internet via Ethernet</string>
    <!-- Don't use Wi-Fi hotspot summary when USB tethering and Bluetooth tethering are chosen [CHAR LIMIT=NONE]-->
    <string name="disable_wifi_hotspot_when_usb_and_bluetooth_on">Only share internet via USB and Bluetooth</string>
    <!-- Don't use Wi-Fi hotspot summary when USB tethering and Ethernet tethering are chosen [CHAR LIMIT=NONE]-->
    <string name="disable_wifi_hotspot_when_usb_and_ethernet_on">Only share internet via USB and Ethernet</string>
    <!-- Don't use Wi-Fi hotspot summary when Bluetooth tethering and Ethernet tethering are chosen [CHAR LIMIT=NONE]-->
    <string name="disable_wifi_hotspot_when_bluetooth_and_ethernet_on">Only share internet via Bluetooth and Ethernet</string>
    <!-- Don't use Wi-Fi hotspot summary when USB, Bluetooth and Ethernet tethering are chosen [CHAR LIMIT=NONE]-->
    <string name="disable_wifi_hotspot_when_usb_and_bluetooth_and_ethernet_on">Only share internet via USB, Bluetooth and Ethernet</string>
    <!-- USB Tethering options -->
    <string name="usb_title">USB</string>
+7 −0
Original line number Diff line number Diff line
@@ -71,6 +71,13 @@
            settings:controller="com.android.settings.network.BluetoothTetherPreferenceController"
            settings:keywords="@string/keywords_hotspot_tethering" />

        <SwitchPreference
            android:key="enable_ethernet_tethering_2"
            android:title="@string/ethernet_tether_checkbox_text"
            android:summary="@string/ethernet_tethering_subtext"
            settings:controller="com.android.settings.network.EthernetTetherPreferenceController"
            settings:keywords="@string/keywords_hotspot_tethering"/>

        <SwitchPreference
            android:key="disable_wifi_tethering"
            android:title="@string/disable_wifi_hotspot_title"
+27 −16
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import com.android.settings.core.FeatureFlags;
import com.android.settings.dashboard.RestrictedDashboardFragment;
import com.android.settings.datausage.DataSaverBackend;
import com.android.settings.network.BluetoothTetherPreferenceController;
import com.android.settings.network.EthernetTetherPreferenceController;
import com.android.settings.network.TetherEnabler;
import com.android.settings.network.UsbTetherPreferenceController;
import com.android.settings.network.WifiTetherDisablePreferenceController;
@@ -91,12 +92,17 @@ public class AllInOneTetherSettings extends RestrictedDashboardFragment

    private static final String KEY_DATA_SAVER_FOOTER = "disabled_on_data_saver" + DEDUP_POSTFIX;
    private static final String KEY_WIFI_TETHER_GROUP = "wifi_tether_settings_group";
    public static final String WIFI_TETHER_DISABLE_KEY = "disable_wifi_tethering";
    public static final String USB_TETHER_KEY = "enable_usb_tethering";
    public static final String BLUETOOTH_TETHER_KEY = "enable_bluetooth_tethering" + DEDUP_POSTFIX;
    public static final String ETHERNET_TETHER_KEY = "enable_ethernet_tethering" + DEDUP_POSTFIX;

    @VisibleForTesting
    static final int EXPANDED_CHILD_COUNT_DEFAULT = 3;
    @VisibleForTesting
    static final int EXPANDED_CHILD_COUNT_WITH_SECURITY_NON = 2;
    @VisibleForTesting
    static final int EXPANDED_CHILD_COUNT_WITHOUT_WIFI_CONFIG = 3;
    static final int EXPANDED_CHILD_COUNT_MAX = Integer.MAX_VALUE;
    private static final String TAG = "AllInOneTetherSettings";

    private boolean mUnavailable;
@@ -114,16 +120,17 @@ public class AllInOneTetherSettings extends RestrictedDashboardFragment
    private WifiTetherApBandPreferenceController mApBandPreferenceController;
    private WifiTetherSecurityPreferenceController mSecurityPreferenceController;
    private PreferenceGroup mWifiTetherGroup;
    private boolean mBluetoothTethering;
    private boolean mUsbTethering;
    private boolean mWifiTethering;
    private boolean mShouldShowWifiConfig = true;
    private boolean mHasShownAdvance;
    private TetherEnabler mTetherEnabler;
    private final TetherEnabler.OnTetherStateUpdateListener mStateUpdateListener =
    @VisibleForTesting
    final TetherEnabler.OnTetherStateUpdateListener mStateUpdateListener =
            state -> {
                mBluetoothTethering = TetherEnabler.isBluetoothTethering(state);
                mUsbTethering = TetherEnabler.isUsbTethering(state);
                mWifiTethering = TetherEnabler.isWifiTethering(state);
                mWifiTetherGroup.setVisible(shouldShowWifiConfig());
                mShouldShowWifiConfig = TetherEnabler.isTethering(state, TETHERING_WIFI)
                        || state == TetherEnabler.TETHERING_OFF;
                getPreferenceScreen().setInitialExpandedChildrenCount(
                        getInitialExpandedChildCount());
                mWifiTetherGroup.setVisible(mShouldShowWifiConfig);
            };

    private final BroadcastReceiver mTetherChangeReceiver = new BroadcastReceiver() {
@@ -182,13 +189,13 @@ public class AllInOneTetherSettings extends RestrictedDashboardFragment
        mApBandPreferenceController = use(WifiTetherApBandPreferenceController.class);
        getSettingsLifecycle().addObserver(use(UsbTetherPreferenceController.class));
        getSettingsLifecycle().addObserver(use(BluetoothTetherPreferenceController.class));
        getSettingsLifecycle().addObserver(use(EthernetTetherPreferenceController.class));
        getSettingsLifecycle().addObserver(use(WifiTetherDisablePreferenceController.class));
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        mDataSaverBackend = new DataSaverBackend(getContext());
        mDataSaverEnabled = mDataSaverBackend.isDataSaverEnabled();
        mDataSaverFooter = findPreference(KEY_DATA_SAVER_FOOTER);
@@ -226,6 +233,7 @@ public class AllInOneTetherSettings extends RestrictedDashboardFragment
        getSettingsLifecycle().addObserver(mTetherEnabler);
        use(UsbTetherPreferenceController.class).setTetherEnabler(mTetherEnabler);
        use(BluetoothTetherPreferenceController.class).setTetherEnabler(mTetherEnabler);
        use(EthernetTetherPreferenceController.class).setTetherEnabler(mTetherEnabler);
        use(WifiTetherDisablePreferenceController.class).setTetherEnabler(mTetherEnabler);
        switchBar.show();
    }
@@ -379,14 +387,11 @@ public class AllInOneTetherSettings extends RestrictedDashboardFragment
        mApBandPreferenceController.updateDisplay();
    }

    private boolean shouldShowWifiConfig() {
        return mWifiTethering || (!mBluetoothTethering && !mUsbTethering);
    }

    @Override
    public int getInitialExpandedChildCount() {
        if (!shouldShowWifiConfig()) {
            return EXPANDED_CHILD_COUNT_WITHOUT_WIFI_CONFIG;
        if (mHasShownAdvance || !mShouldShowWifiConfig) {
            mHasShownAdvance = true;
            return EXPANDED_CHILD_COUNT_MAX;
        }

        if (mSecurityPreferenceController == null) {
@@ -398,6 +403,12 @@ public class AllInOneTetherSettings extends RestrictedDashboardFragment
                ? EXPANDED_CHILD_COUNT_WITH_SECURITY_NON : EXPANDED_CHILD_COUNT_DEFAULT;
    }

    @Override
    public void onExpandButtonClick() {
        super.onExpandButtonClick();
        mHasShownAdvance = true;
    }

    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider(R.xml.all_tether_prefs) {

+49 −43
Original line number Diff line number Diff line
@@ -17,6 +17,11 @@ package com.android.settings.network;

import static android.os.UserManager.DISALLOW_CONFIG_TETHERING;

import static com.android.settings.network.TetherEnabler.TETHERING_BLUETOOTH_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_ETHERNET_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_OFF;
import static com.android.settings.network.TetherEnabler.TETHERING_USB_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_WIFI_ON;
import static com.android.settingslib.RestrictedLockUtilsInternal.checkIfRestrictionEnforced;

import android.bluetooth.BluetoothAdapter;
@@ -52,19 +57,7 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
        LifecycleObserver, TetherEnabler.OnTetherStateUpdateListener {
    private static final String TAG = "AllInOneTetherPreferenceController";

    private static final byte TETHERING_TYPE_HOTSPOT_ONLY = 1;
    private static final byte TETHERING_TYPE_USB_ONLY = 1 << 1;
    private static final byte TETHERING_TYPE_BLUETOOTH_ONLY = 1 << 2;
    private static final byte TETHERING_TYPE_HOTSPOT_AND_USB =
            TETHERING_TYPE_HOTSPOT_ONLY | TETHERING_TYPE_USB_ONLY;
    private static final byte TETHERING_TYPE_HOTSPOT_AND_BLUETOOTH =
            TETHERING_TYPE_HOTSPOT_ONLY | TETHERING_TYPE_BLUETOOTH_ONLY;
    private static final byte TETHERING_TYPE_USB_AND_BLUETOOTH =
            TETHERING_TYPE_USB_ONLY | TETHERING_TYPE_BLUETOOTH_ONLY;
    private static final byte TETHERING_TYPE_HOTSPOT_AND_USB_AND_BLUETOOTH =
            TETHERING_TYPE_HOTSPOT_ONLY | TETHERING_TYPE_USB_ONLY | TETHERING_TYPE_BLUETOOTH_ONLY;
    // A bitwise value that stands for the current tethering interface type.
    private int mTetheringType;
    private int mTetheringState;

    private final boolean mAdminDisallowedTetherConfig;
    private final AtomicReference<BluetoothPan> mBluetoothPan;
@@ -124,34 +117,51 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController

    @Override
    public CharSequence getSummary() {
        if (mPreference != null && mPreference.isChecked()) {
            switch (mTetheringType) {
                case TETHERING_TYPE_HOTSPOT_ONLY:
        switch (mTetheringState) {
            case TETHERING_OFF:
                return mContext.getString(R.string.tether_settings_summary_off);
            case TETHERING_WIFI_ON:
                return mContext.getString(R.string.tether_settings_summary_hotspot_only);
                case TETHERING_TYPE_USB_ONLY:
            case TETHERING_USB_ON:
                return mContext.getString(R.string.tether_settings_summary_usb_tethering_only);
                case TETHERING_TYPE_BLUETOOTH_ONLY:
            case TETHERING_BLUETOOTH_ON:
                return mContext.getString(
                        R.string.tether_settings_summary_bluetooth_tethering_only);
                case TETHERING_TYPE_HOTSPOT_AND_USB:
            case TETHERING_ETHERNET_ON:
                return mContext.getString(R.string.tether_settings_summary_ethernet_tethering_only);
            case TETHERING_WIFI_ON | TETHERING_USB_ON:
                return mContext.getString(R.string.tether_settings_summary_hotspot_and_usb);
                case TETHERING_TYPE_HOTSPOT_AND_BLUETOOTH:
                    return mContext.getString(
                            R.string.tether_settings_summary_hotspot_and_bluetooth);
                case TETHERING_TYPE_USB_AND_BLUETOOTH:
            case TETHERING_WIFI_ON | TETHERING_BLUETOOTH_ON:
                return mContext.getString(R.string.tether_settings_summary_hotspot_and_bluetooth);
            case TETHERING_WIFI_ON | TETHERING_ETHERNET_ON:
                return mContext.getString(R.string.tether_settings_summary_hotspot_and_ethernet);
            case TETHERING_USB_ON | TETHERING_BLUETOOTH_ON:
                return mContext.getString(R.string.tether_settings_summary_usb_and_bluetooth);
                case TETHERING_TYPE_HOTSPOT_AND_USB_AND_BLUETOOTH:
            case TETHERING_USB_ON | TETHERING_ETHERNET_ON:
                return mContext.getString(R.string.tether_settings_summary_usb_and_ethernet);
            case TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON:
                return mContext.getString(R.string.tether_settings_summary_bluetooth_and_ethernet);
            case TETHERING_WIFI_ON | TETHERING_USB_ON | TETHERING_BLUETOOTH_ON:
                return mContext.getString(
                        R.string.tether_settings_summary_hotspot_and_usb_and_bluetooth);
            case TETHERING_WIFI_ON | TETHERING_USB_ON | TETHERING_ETHERNET_ON:
                return mContext.getString(
                        R.string.tether_settings_summary_hotspot_and_usb_and_ethernet);
            case TETHERING_WIFI_ON | TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON:
                return mContext.getString(
                        R.string.tether_settings_summary_hotspot_and_bluetooth_and_ethernet);
            case TETHERING_USB_ON | TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON:
                return mContext.getString(
                        R.string.tether_settings_summary_usb_and_bluetooth_and_ethernet);
            case TETHERING_WIFI_ON | TETHERING_USB_ON | TETHERING_BLUETOOTH_ON
                    | TETHERING_ETHERNET_ON:
                return mContext.getString(R.string.tether_settings_summary_all);
            default:
                    Log.e(TAG, "None of the tether interfaces is chosen");
                Log.e(TAG, "Unknown tethering state");
                return mContext.getString(R.string.summary_placeholder);
        }
    }

        return mContext.getString(R.string.tether_settings_summary_off);
    }

    @OnLifecycleEvent(Event.ON_CREATE)
    public void onCreate() {
        if (mBluetoothAdapter != null
@@ -197,11 +207,7 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController

    @Override
    public void onTetherStateUpdated(@TetherEnabler.TetheringState int state) {
        mTetheringType = 0;
        mTetheringType |= TetherEnabler.isBluetoothTethering(state) ? TETHERING_TYPE_BLUETOOTH_ONLY
                : 0;
        mTetheringType |= TetherEnabler.isWifiTethering(state) ? TETHERING_TYPE_HOTSPOT_ONLY : 0;
        mTetheringType |= TetherEnabler.isUsbTethering(state) ? TETHERING_TYPE_USB_ONLY : 0;
        mTetheringState = state;
        updateState(mPreference);
    }
}
+10 −47
Original line number Diff line number Diff line
@@ -21,51 +21,25 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.TetheringManager;
import android.net.ConnectivityManager;
import android.text.TextUtils;
import android.util.Log;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;

import com.google.common.annotations.VisibleForTesting;

/**
 * This controller helps to manage the switch state and visibility of bluetooth tether switch
 * preference. It stores preference value when preference changed.
 * preference.
 */
public final class BluetoothTetherPreferenceController extends TetherBasePreferenceController
        implements LifecycleObserver {

    private static final String TAG = "BluetoothTetherPreferenceController";
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
public final class BluetoothTetherPreferenceController extends TetherBasePreferenceController {
    private int mBluetoothState;
    private boolean mBluetoothTethering;

    public BluetoothTetherPreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
    }

    @Override
    public boolean isChecked() {
        return mBluetoothTethering;
    }

    @Override
    public boolean setChecked(boolean isChecked) {
        if (mTetherEnabler == null) {
            return false;
        }
        if (isChecked) {
            mTetherEnabler.startTethering(TetheringManager.TETHERING_BLUETOOTH);
        } else {
            mTetherEnabler.stopTethering(TetheringManager.TETHERING_BLUETOOTH);
        }
        return true;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart() {
        mBluetoothState = BluetoothAdapter.getDefaultAdapter().getState();
@@ -79,41 +53,30 @@ public final class BluetoothTetherPreferenceController extends TetherBasePrefere
    }

    @Override
    public void updateState(Preference preference) {
        super.updateState(preference);
        if (preference == null) {
            return;
        }

    public boolean shouldEnable() {
        switch (mBluetoothState) {
            case BluetoothAdapter.STATE_ON:
            case BluetoothAdapter.STATE_OFF:
                // fall through.
            case BluetoothAdapter.ERROR:
                preference.setEnabled(true);
                break;
                return true;
            case BluetoothAdapter.STATE_TURNING_OFF:
            case BluetoothAdapter.STATE_TURNING_ON:
                // fall through.
            default:
                preference.setEnabled(false);
                return false;
        }
    }

    @Override
    public int getAvailabilityStatus() {
    public boolean shouldShow() {
        final String[] bluetoothRegexs = mCm.getTetherableBluetoothRegexs();
        if (bluetoothRegexs == null || bluetoothRegexs.length == 0) {
            return CONDITIONALLY_UNAVAILABLE;
        } else {
            return AVAILABLE;
        }
        return bluetoothRegexs != null && bluetoothRegexs.length != 0;
    }

    @Override
    public void onTetherStateUpdated(int state) {
        mBluetoothTethering = TetherEnabler.isBluetoothTethering(state);
        updateState(mPreference);
    public int getTetherType() {
        return ConnectivityManager.TETHERING_BLUETOOTH;
    }

    @VisibleForTesting
Loading