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

Merged-In: I343599e6127d9b1cb4af661dcc80a8683589c7b8
Change-Id: I343599e6127d9b1cb4af661dcc80a8683589c7b8
This commit is contained in:
Weng Su
2023-08-15 22:22:23 +08:00
parent a928e9202f
commit 27b3821313
9 changed files with 582 additions and 4 deletions

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<>();
@@ -75,10 +81,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.getFactory(application).getWifiFeatureProvider()
.getWifiHotspotRepository();
WifiFeatureProvider featureProvider = FeatureFactory.getFactory(application)
.getWifiFeatureProvider();
mWifiHotspotRepository = featureProvider.getWifiHotspotRepository();
mSharedConnectivityRepository = featureProvider.getSharedConnectivityRepository();
if (mSharedConnectivityRepository.isServiceAvailable()) {
mSharedConnectivityRepository.getSettingsState()
.observeForever(mInstantHotspotStateObserver);
}
}
@Override
@@ -89,6 +108,10 @@ public class WifiTetherViewModel extends AndroidViewModel {
if (mSpeedSummary != null) {
mWifiHotspotRepository.getSpeedType().removeObserver(mSpeedTypeObserver);
}
if (mSharedConnectivityRepository.isServiceAvailable()) {
mSharedConnectivityRepository.getSettingsState()
.removeObserver(mInstantHotspotStateObserver);
}
}
/**
@@ -169,4 +192,47 @@ 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.getFactory(getApplication().getApplicationContext()).getWifiFeatureProvider()
.verboseLog(TAG, msg);
}
}