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

Commit d7bbd450 authored by Weng Su's avatar Weng Su
Browse files

Refer to the last Hotspot password when generating the password

- Query the last Hotspot password when security type is SECURITY_TYPE_OPEN and it will be used when the user changes to another security type

- If the last hotspot password is an empty string, then generate random password

Bug: 201789473
Test: manual test
atest -c WifiHotspotRepositoryTest
make RunSettingsRoboTests ROBOTEST_FILTER=WifiTetherPasswordPreferenceControllerTest

Change-Id: Id3a846099238433e7b6c8b934fa26a74df0dcb61
parent 1acdb145
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ import com.android.settings.slices.SlicesFeatureProvider;
import com.android.settings.users.UserFeatureProvider;
import com.android.settings.vpn2.AdvancedVpnFeatureProvider;
import com.android.settings.wifi.WifiTrackerLibProvider;
import com.android.settings.wifi.factory.WifiFeatureProvider;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;

/**
@@ -192,6 +193,11 @@ public abstract class FeatureFactory {
     */
    public abstract AdvancedVpnFeatureProvider getAdvancedVpnFeatureProvider();

    /**
     * Retrieves implementation for Wi-Fi feature.
     */
    public abstract WifiFeatureProvider getWifiFeatureProvider();

    public static final class FactoryNotFoundException extends RuntimeException {
        public FactoryNotFoundException(Throwable throwable) {
            super("Unable to create factory. Did you misconfigure Proguard?", throwable);
+10 −0
Original line number Diff line number Diff line
@@ -77,6 +77,7 @@ import com.android.settings.vpn2.AdvancedVpnFeatureProvider;
import com.android.settings.vpn2.AdvancedVpnFeatureProviderImpl;
import com.android.settings.wifi.WifiTrackerLibProvider;
import com.android.settings.wifi.WifiTrackerLibProviderImpl;
import com.android.settings.wifi.factory.WifiFeatureProvider;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;

/**
@@ -112,6 +113,7 @@ public class FeatureFactoryImpl extends FeatureFactory {
    private AccessibilitySearchFeatureProvider mAccessibilitySearchFeatureProvider;
    private AccessibilityMetricsFeatureProvider mAccessibilityMetricsFeatureProvider;
    private AdvancedVpnFeatureProvider mAdvancedVpnFeatureProvider;
    private WifiFeatureProvider mWifiFeatureProvider;

    @Override
    public SupportFeatureProvider getSupportFeatureProvider(Context context) {
@@ -355,4 +357,12 @@ public class FeatureFactoryImpl extends FeatureFactory {
        }
        return mAdvancedVpnFeatureProvider;
    }

    @Override
    public WifiFeatureProvider getWifiFeatureProvider() {
        if (mWifiFeatureProvider == null) {
            mWifiFeatureProvider = new WifiFeatureProvider(getAppContext());
        }
        return mWifiFeatureProvider;
    }
}
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.factory;

import android.content.Context;
import android.net.wifi.WifiManager;

import androidx.annotation.NonNull;

import com.android.settings.wifi.repository.WifiHotspotRepository;

/**
 * Wi-Fi Feature Provider
 */
public class WifiFeatureProvider {

    private final Context mAppContext;
    private WifiManager mWifiManager;
    private WifiHotspotRepository mWifiHotspotRepository;

    public WifiFeatureProvider(@NonNull Context appContext) {
        mAppContext = appContext;
    }

    /**
     * Get WifiManager
     */
    public WifiManager getWifiManager() {
        if (mWifiManager == null) {
            mWifiManager = mAppContext.getSystemService(WifiManager.class);
        }
        return mWifiManager;
    }

    /**
     * Get WifiRepository
     */
    public WifiHotspotRepository getWifiHotspotRepository() {
        if (mWifiHotspotRepository == null) {
            mWifiHotspotRepository = new WifiHotspotRepository(mAppContext, getWifiManager());
        }
        return mWifiHotspotRepository;
    }
}
+76 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.repository;

import android.content.Context;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import java.util.UUID;
import java.util.function.Consumer;

/**
 * Wi-Fi Hotspot Repository
 */
public class WifiHotspotRepository {

    protected final Context mAppContext;
    protected final WifiManager mWifiManager;

    protected String mLastPassword;
    protected LastPasswordListener mLastPasswordListener = new LastPasswordListener();

    public WifiHotspotRepository(@NonNull Context appContext, @NonNull WifiManager wifiManager) {
        mAppContext = appContext;
        mWifiManager = wifiManager;
    }

    /**
     * Query the last configured Tethered Ap Passphrase since boot.
     */
    public void queryLastPasswordIfNeeded() {
        SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
        if (config.getSecurityType() != SoftApConfiguration.SECURITY_TYPE_OPEN) {
            return;
        }
        mWifiManager.queryLastConfiguredTetheredApPassphraseSinceBoot(mAppContext.getMainExecutor(),
                mLastPasswordListener);
    }

    /**
     * Generate password.
     */
    public String generatePassword() {
        return !TextUtils.isEmpty(mLastPassword) ? mLastPassword : generateRandomPassword();
    }

    private class LastPasswordListener implements Consumer<String> {
        @Override
        public void accept(String password) {
            mLastPassword = password;
        }
    }

    private static String generateRandomPassword() {
        String randomUUID = UUID.randomUUID().toString();
        //first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
        return randomUUID.substring(0, 8) + randomUUID.substring(9, 13);
    }
}
+10 −13
Original line number Diff line number Diff line
@@ -33,10 +33,9 @@ import com.android.settings.core.FeatureFlags;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.ValidatedEditTextPreference;
import com.android.settings.wifi.WifiUtils;
import com.android.settings.wifi.repository.WifiHotspotRepository;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;

import java.util.UUID;

/**
 * Controller for logic pertaining to the password of Wi-Fi tethering.
 */
@@ -49,18 +48,22 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
    private int mSecurityType;

    private final MetricsFeatureProvider mMetricsFeatureProvider;
    private final WifiHotspotRepository mWifiHotspotRepository;

    @VisibleForTesting
    WifiTetherPasswordPreferenceController(Context context, OnTetherConfigUpdateListener listener,
            MetricsFeatureProvider provider) {
        super(context, listener);
        mMetricsFeatureProvider = provider;
        FeatureFactory featureFactory = FeatureFactory.getFactory(context);
        mMetricsFeatureProvider = (provider != null) ? provider
                : featureFactory.getMetricsFeatureProvider();
        mWifiHotspotRepository = featureFactory.getWifiFeatureProvider().getWifiHotspotRepository();
        mWifiHotspotRepository.queryLastPasswordIfNeeded();
    }

    public WifiTetherPasswordPreferenceController(Context context,
            OnTetherConfigUpdateListener listener) {
        super(context, listener);
        mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
        this(context, listener, null /* MetricsFeatureProvider */);
    }

    @Override
@@ -74,7 +77,7 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
        final SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
        if (config.getSecurityType() != SoftApConfiguration.SECURITY_TYPE_OPEN
                && TextUtils.isEmpty(config.getPassphrase())) {
            mPassword = generateRandomPassword();
            mPassword = mWifiHotspotRepository.generatePassword();
        } else {
            mPassword = config.getPassphrase();
        }
@@ -110,7 +113,7 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
        if (securityType == SoftApConfiguration.SECURITY_TYPE_OPEN) {
            return "";
        } else if (!WifiUtils.isHotspotPasswordValid(mPassword, securityType)) {
            mPassword = generateRandomPassword();
            mPassword = mWifiHotspotRepository.generatePassword();
            updatePasswordDisplay((EditTextPreference) mPreference);
        }
        return mPassword;
@@ -132,12 +135,6 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
        return WifiUtils.isHotspotPasswordValid(value, mSecurityType);
    }

    private static String generateRandomPassword() {
        String randomUUID = UUID.randomUUID().toString();
        //first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
        return randomUUID.substring(0, 8) + randomUUID.substring(9, 13);
    }

    private void updatePasswordDisplay(EditTextPreference preference) {
        ValidatedEditTextPreference pref = (ValidatedEditTextPreference) preference;
        pref.setText(mPassword);
Loading