Merge "Add Instant hotspot preference" into udc-qpr-dev am: 67d5cb42e9
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/24483137 Change-Id: I345ec2843e47be6ba23b64807eb1b23ad479738a Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -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
|
||||
*/
|
||||
|
@@ -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.getFactory(mAppContext).getWifiFeatureProvider().verboseLog(TAG, msg);
|
||||
}
|
||||
}
|
@@ -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);
|
||||
@@ -278,6 +301,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();
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user