Add Instant hotspot preference

- Add Instant hotspot preference to Wi-Fi hotspot settings

- Wait onServiceConnected callback and then getSettingsState

- Use the PendingIntent provided by SharedConnectivitySettingsState to launch Instant hotspot settings

Bug: 268550769
Test: manual test
atest -c WifiTetherSettingsTest
atest -c WifiTetherViewModelTest \
         SharedConnectivityRepositoryTest

Change-Id: I343599e6127d9b1cb4af661dcc80a8683589c7b8
This commit is contained in:
Weng Su
2023-08-15 22:22:23 +08:00
parent 4957e2d000
commit 63321a1a83
9 changed files with 581 additions and 4 deletions

View File

@@ -26,6 +26,7 @@ import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import com.android.settings.wifi.repository.SharedConnectivityRepository;
import com.android.settings.wifi.repository.WifiHotspotRepository;
import com.android.settings.wifi.tether.WifiHotspotSecurityViewModel;
import com.android.settings.wifi.tether.WifiHotspotSpeedViewModel;
@@ -44,6 +45,7 @@ public class WifiFeatureProvider {
private TetheringManager mTetheringManager;
private WifiVerboseLogging mWifiVerboseLogging;
private WifiHotspotRepository mWifiHotspotRepository;
private SharedConnectivityRepository mSharedConnectivityRepository;
public WifiFeatureProvider(@NonNull Context appContext) {
mAppContext = appContext;
@@ -92,6 +94,17 @@ public class WifiFeatureProvider {
return mWifiHotspotRepository;
}
/**
* Gets SharedConnectivityRepository
*/
public SharedConnectivityRepository getSharedConnectivityRepository() {
if (mSharedConnectivityRepository == null) {
mSharedConnectivityRepository = new SharedConnectivityRepository(mAppContext);
verboseLog(TAG, "getSharedConnectivityRepository():" + mSharedConnectivityRepository);
}
return mSharedConnectivityRepository;
}
/**
* Gets WifiTetherViewModel
*/

View File

@@ -0,0 +1,184 @@
/*
* 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.app.PendingIntent;
import android.content.Context;
import android.net.wifi.sharedconnectivity.app.HotspotNetwork;
import android.net.wifi.sharedconnectivity.app.HotspotNetworkConnectionStatus;
import android.net.wifi.sharedconnectivity.app.KnownNetwork;
import android.net.wifi.sharedconnectivity.app.KnownNetworkConnectionStatus;
import android.net.wifi.sharedconnectivity.app.SharedConnectivityClientCallback;
import android.net.wifi.sharedconnectivity.app.SharedConnectivityManager;
import android.net.wifi.sharedconnectivity.app.SharedConnectivitySettingsState;
import android.os.HandlerThread;
import android.provider.DeviceConfig;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.android.settings.overlay.FeatureFactory;
import java.util.List;
import java.util.concurrent.Executor;
/**
* Shared Connectivity Repository for {@link SharedConnectivityManager}
*/
public class SharedConnectivityRepository {
private static final String TAG = "SharedConnectivityRepository";
private static final String DEVICE_CONFIG_NAMESPACE = "wifi";
private static final String DEVICE_CONFIG_KEY = "shared_connectivity_enabled";
private Context mAppContext;
private SharedConnectivityManager mManager;
private ClientCallback mClientCallback = new ClientCallback();
private HandlerThread mWorkerThread = new HandlerThread(TAG);
private Executor mWorkerExecutor = cmd -> mWorkerThread.getThreadHandler().post(cmd);
private Runnable mLaunchSettingsRunnable = () -> handleLaunchSettings();
@VisibleForTesting
MutableLiveData<SharedConnectivitySettingsState> mSettingsState = new MutableLiveData<>();
public SharedConnectivityRepository(@NonNull Context appContext) {
this(appContext,
DeviceConfig.getBoolean(DEVICE_CONFIG_NAMESPACE, DEVICE_CONFIG_KEY, false));
}
@VisibleForTesting
SharedConnectivityRepository(@NonNull Context appContext, boolean isConfigEnabled) {
mAppContext = appContext;
if (!isConfigEnabled) {
return;
}
mManager = mAppContext.getSystemService(SharedConnectivityManager.class);
if (mManager == null) {
Log.w(TAG, "Failed to get SharedConnectivityManager");
return;
}
mWorkerThread.start();
mManager.registerCallback(mWorkerExecutor, mClientCallback);
}
/**
* Return whether Wi-Fi Shared Connectivity service is available or not.
*
* @return {@code true} if Wi-Fi Shared Connectivity service is available
*/
public boolean isServiceAvailable() {
return mManager != null;
}
/**
* Gets SharedConnectivitySettingsState LiveData
*/
public LiveData<SharedConnectivitySettingsState> getSettingsState() {
return mSettingsState;
}
/**
* Launch Instant Hotspot Settings
*/
public void launchSettings() {
mWorkerExecutor.execute(mLaunchSettingsRunnable);
}
@WorkerThread
@VisibleForTesting
void handleLaunchSettings() {
if (mManager == null) {
return;
}
SharedConnectivitySettingsState state = mManager.getSettingsState();
log("handleLaunchSettings(), state:" + state);
if (state == null) {
Log.e(TAG, "No SettingsState to launch Instant Hotspot settings");
return;
}
PendingIntent intent = state.getInstantTetherSettingsPendingIntent();
if (intent == null) {
Log.e(TAG, "No PendingIntent to launch Instant Hotspot settings");
return;
}
sendSettingsIntent(intent);
}
@WorkerThread
@VisibleForTesting
void sendSettingsIntent(@NonNull PendingIntent intent) {
try {
log("sendSettingsIntent(), sent intent:" + intent);
intent.send();
} catch (PendingIntent.CanceledException e) {
Log.e(TAG, "Failed to launch Instant Hotspot settings", e);
}
}
@WorkerThread
class ClientCallback implements SharedConnectivityClientCallback {
@Override
public void onHotspotNetworkConnectionStatusChanged(HotspotNetworkConnectionStatus status) {
log("onHotspotNetworkConnectionStatusChanged(), status:" + status);
}
@Override
public void onHotspotNetworksUpdated(List<HotspotNetwork> networks) {
log("onHotspotNetworksUpdated(), networks:" + networks);
}
@Override
public void onKnownNetworkConnectionStatusChanged(KnownNetworkConnectionStatus status) {
log("onKnownNetworkConnectionStatusChanged(), status:" + status);
}
@Override
public void onKnownNetworksUpdated(List<KnownNetwork> networks) {
log("onKnownNetworksUpdated(), networks:" + networks);
}
@Override
public void onRegisterCallbackFailed(Exception e) {
Log.e(TAG, "onRegisterCallbackFailed(), e:" + e);
}
@Override
public void onServiceConnected() {
SharedConnectivitySettingsState state = mManager.getSettingsState();
Log.d(TAG, "onServiceConnected(), Manager#getSettingsState:" + state);
mSettingsState.postValue(state);
}
@Override
public void onServiceDisconnected() {
log("onServiceDisconnected()");
}
@Override
public void onSharedConnectivitySettingsChanged(SharedConnectivitySettingsState state) {
Log.d(TAG, "onSharedConnectivitySettingsChanged(), state:" + state);
mSettingsState.postValue(state);
}
}
private void log(String msg) {
FeatureFactory.getFeatureFactory().getWifiFeatureProvider().verboseLog(TAG, msg);
}
}

View File

@@ -76,6 +76,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
static final String KEY_WIFI_HOTSPOT_SECURITY = "wifi_hotspot_security";
@VisibleForTesting
static final String KEY_WIFI_HOTSPOT_SPEED = "wifi_hotspot_speed";
@VisibleForTesting
static final String KEY_INSTANT_HOTSPOT = "wifi_hotspot_instant";
@VisibleForTesting
SettingsMainSwitchBar mMainSwitchBar;
@@ -103,6 +105,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
Preference mWifiHotspotSecurity;
@VisibleForTesting
Preference mWifiHotspotSpeed;
@VisibleForTesting
Preference mInstantHotspot;
static {
TETHER_STATE_CHANGE_FILTER = new IntentFilter(WIFI_AP_STATE_CHANGED_ACTION);
@@ -148,6 +152,7 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
.getWifiTetherViewModel(this);
if (mWifiTetherViewModel != null) {
setupSpeedFeature(mWifiTetherViewModel.isSpeedFeatureAvailable());
setupInstantHotspot(mWifiTetherViewModel.isInstantHotspotFeatureAvailable());
mWifiTetherViewModel.getRestarting().observe(this, this::onRestartingChanged);
}
}
@@ -167,6 +172,24 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
}
}
@VisibleForTesting
void setupInstantHotspot(boolean isFeatureAvailable) {
if (!isFeatureAvailable) {
return;
}
mInstantHotspot = findPreference(KEY_INSTANT_HOTSPOT);
if (mInstantHotspot == null) {
Log.e(TAG, "Failed to find Instant Hotspot preference:" + KEY_INSTANT_HOTSPOT);
return;
}
mWifiTetherViewModel.getInstantHotspotSummary()
.observe(this, this::onInstantHotspotChanged);
mInstantHotspot.setOnPreferenceClickListener(p -> {
mWifiTetherViewModel.launchInstantHotspotSettings();
return true;
});
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
@@ -279,6 +302,16 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
setLoading(restarting, false);
}
@VisibleForTesting
void onInstantHotspotChanged(String summary) {
if (summary == null) {
mInstantHotspot.setVisible(false);
return;
}
mInstantHotspot.setVisible(true);
mInstantHotspot.setSummary(summary);
}
@VisibleForTesting
SoftApConfiguration buildNewConfig() {
SoftApConfiguration currentConfig = mWifiTetherViewModel.getSoftApConfiguration();

View File

@@ -28,7 +28,9 @@ import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_6
import android.app.Application;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.sharedconnectivity.app.SharedConnectivitySettingsState;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
@@ -36,6 +38,8 @@ import androidx.lifecycle.Observer;
import com.android.settings.R;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.wifi.factory.WifiFeatureProvider;
import com.android.settings.wifi.repository.SharedConnectivityRepository;
import com.android.settings.wifi.repository.WifiHotspotRepository;
import org.jetbrains.annotations.NotNull;
@@ -48,6 +52,8 @@ import java.util.Map;
*/
public class WifiTetherViewModel extends AndroidViewModel {
private static final String TAG = "WifiTetherViewModel";
static final int RES_INSTANT_HOTSPOT_SUMMARY_ON = R.string.wifi_hotspot_instant_summary_on;
static final int RES_INSTANT_HOTSPOT_SUMMARY_OFF = R.string.wifi_hotspot_instant_summary_off;
static Map<Integer, Integer> sSecuritySummaryResMap = new HashMap<>();
@@ -78,10 +84,23 @@ public class WifiTetherViewModel extends AndroidViewModel {
protected final Observer<Integer> mSecurityTypeObserver = st -> onSecurityTypeChanged(st);
protected final Observer<Integer> mSpeedTypeObserver = st -> onSpeedTypeChanged(st);
private SharedConnectivityRepository mSharedConnectivityRepository;
@VisibleForTesting
MutableLiveData<String> mInstantHotspotSummary = new MutableLiveData<>();
@VisibleForTesting
Observer<SharedConnectivitySettingsState> mInstantHotspotStateObserver =
state -> onInstantHotspotStateChanged(state);
public WifiTetherViewModel(@NotNull Application application) {
super(application);
mWifiHotspotRepository = FeatureFactory.getFeatureFactory().getWifiFeatureProvider()
.getWifiHotspotRepository();
WifiFeatureProvider featureProvider = FeatureFactory.getFeatureFactory()
.getWifiFeatureProvider();
mWifiHotspotRepository = featureProvider.getWifiHotspotRepository();
mSharedConnectivityRepository = featureProvider.getSharedConnectivityRepository();
if (mSharedConnectivityRepository.isServiceAvailable()) {
mSharedConnectivityRepository.getSettingsState()
.observeForever(mInstantHotspotStateObserver);
}
}
@Override
@@ -92,6 +111,10 @@ public class WifiTetherViewModel extends AndroidViewModel {
if (mSpeedSummary != null) {
mWifiHotspotRepository.getSpeedType().removeObserver(mSpeedTypeObserver);
}
if (mSharedConnectivityRepository.isServiceAvailable()) {
mSharedConnectivityRepository.getSettingsState()
.removeObserver(mInstantHotspotStateObserver);
}
}
/**
@@ -172,4 +195,46 @@ public class WifiTetherViewModel extends AndroidViewModel {
public LiveData<Boolean> getRestarting() {
return mWifiHotspotRepository.getRestarting();
}
/**
* Return whether Wi-Fi Instant Hotspot feature is available or not.
*
* @return {@code true} if Wi-Fi Instant Hotspot feature is available
*/
public boolean isInstantHotspotFeatureAvailable() {
return mSharedConnectivityRepository.isServiceAvailable();
}
/**
* Gets InstantHotspotSummary
*/
public LiveData<String> getInstantHotspotSummary() {
return mInstantHotspotSummary;
}
@VisibleForTesting
void onInstantHotspotStateChanged(SharedConnectivitySettingsState state) {
log("onInstantHotspotStateChanged(), state:" + state);
if (state == null) {
mInstantHotspotSummary.setValue(null);
return;
}
mInstantHotspotSummary.setValue(getInstantHotspotSummary(state.isInstantTetherEnabled()));
}
private String getInstantHotspotSummary(boolean enabled) {
return getApplication().getString(
enabled ? RES_INSTANT_HOTSPOT_SUMMARY_ON : RES_INSTANT_HOTSPOT_SUMMARY_OFF);
}
/**
* Launch Instant Hotspot Settings
*/
public void launchInstantHotspotSettings() {
mSharedConnectivityRepository.launchSettings();
}
private void log(String msg) {
FeatureFactory.getFeatureFactory().getWifiFeatureProvider().verboseLog(TAG, msg);
}
}