1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.wifi.tether; 18 19 import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_OPEN; 20 import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA2_PSK; 21 import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA3_SAE; 22 import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION; 23 24 import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_2GHZ; 25 import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_2GHZ_5GHZ; 26 import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_5GHZ; 27 import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_6GHZ; 28 29 import android.app.Application; 30 import android.net.wifi.SoftApConfiguration; 31 import android.net.wifi.sharedconnectivity.app.SharedConnectivitySettingsState; 32 33 import androidx.annotation.VisibleForTesting; 34 import androidx.lifecycle.AndroidViewModel; 35 import androidx.lifecycle.LiveData; 36 import androidx.lifecycle.MutableLiveData; 37 import androidx.lifecycle.Observer; 38 39 import com.android.settings.R; 40 import com.android.settings.overlay.FeatureFactory; 41 import com.android.settings.wifi.factory.WifiFeatureProvider; 42 import com.android.settings.wifi.repository.SharedConnectivityRepository; 43 import com.android.settings.wifi.repository.WifiHotspotRepository; 44 45 import org.jetbrains.annotations.NotNull; 46 47 import java.util.HashMap; 48 import java.util.Map; 49 50 /** 51 * Wi-Fi Hotspot ViewModel 52 */ 53 public class WifiTetherViewModel extends AndroidViewModel { 54 private static final String TAG = "WifiTetherViewModel"; 55 static final int RES_INSTANT_HOTSPOT_SUMMARY_ON = R.string.wifi_hotspot_instant_summary_on; 56 static final int RES_INSTANT_HOTSPOT_SUMMARY_OFF = R.string.wifi_hotspot_instant_summary_off; 57 58 static Map<Integer, Integer> sSecuritySummaryResMap = new HashMap<>(); 59 60 static { sSecuritySummaryResMap.put(SECURITY_TYPE_WPA3_SAE, R.string.wifi_security_sae)61 sSecuritySummaryResMap.put(SECURITY_TYPE_WPA3_SAE, R.string.wifi_security_sae); sSecuritySummaryResMap.put(SECURITY_TYPE_WPA3_SAE_TRANSITION, R.string.wifi_security_psk_sae)62 sSecuritySummaryResMap.put(SECURITY_TYPE_WPA3_SAE_TRANSITION, 63 R.string.wifi_security_psk_sae); sSecuritySummaryResMap.put(SECURITY_TYPE_WPA2_PSK, R.string.wifi_security_wpa2)64 sSecuritySummaryResMap.put(SECURITY_TYPE_WPA2_PSK, R.string.wifi_security_wpa2); sSecuritySummaryResMap.put(SECURITY_TYPE_OPEN, R.string.wifi_security_none)65 sSecuritySummaryResMap.put(SECURITY_TYPE_OPEN, R.string.wifi_security_none); 66 } 67 68 static Map<Integer, Integer> sSpeedSummaryResMap = new HashMap<>(); 69 70 static { sSpeedSummaryResMap.put(SPEED_2GHZ, R.string.wifi_hotspot_speed_summary_2g)71 sSpeedSummaryResMap.put(SPEED_2GHZ, R.string.wifi_hotspot_speed_summary_2g); sSpeedSummaryResMap.put(SPEED_5GHZ, R.string.wifi_hotspot_speed_summary_5g)72 sSpeedSummaryResMap.put(SPEED_5GHZ, R.string.wifi_hotspot_speed_summary_5g); sSpeedSummaryResMap.put(SPEED_6GHZ, R.string.wifi_hotspot_speed_summary_6g)73 sSpeedSummaryResMap.put(SPEED_6GHZ, R.string.wifi_hotspot_speed_summary_6g); sSpeedSummaryResMap.put(SPEED_2GHZ_5GHZ, R.string.wifi_hotspot_speed_summary_2g_and_5g)74 sSpeedSummaryResMap.put(SPEED_2GHZ_5GHZ, R.string.wifi_hotspot_speed_summary_2g_and_5g); 75 } 76 77 protected final WifiHotspotRepository mWifiHotspotRepository; 78 protected MutableLiveData<Integer> mSecuritySummary; 79 protected MutableLiveData<Integer> mSpeedSummary; 80 81 protected final Observer<Integer> mSecurityTypeObserver = st -> onSecurityTypeChanged(st); 82 protected final Observer<Integer> mSpeedTypeObserver = st -> onSpeedTypeChanged(st); 83 84 private SharedConnectivityRepository mSharedConnectivityRepository; 85 @VisibleForTesting 86 MutableLiveData<String> mInstantHotspotSummary = new MutableLiveData<>(); 87 @VisibleForTesting 88 Observer<SharedConnectivitySettingsState> mInstantHotspotStateObserver = 89 state -> onInstantHotspotStateChanged(state); 90 WifiTetherViewModel(@otNull Application application)91 public WifiTetherViewModel(@NotNull Application application) { 92 super(application); 93 WifiFeatureProvider featureProvider = FeatureFactory.getFactory(application) 94 .getWifiFeatureProvider(); 95 mWifiHotspotRepository = featureProvider.getWifiHotspotRepository(); 96 mSharedConnectivityRepository = featureProvider.getSharedConnectivityRepository(); 97 if (mSharedConnectivityRepository.isServiceAvailable()) { 98 mSharedConnectivityRepository.getSettingsState() 99 .observeForever(mInstantHotspotStateObserver); 100 } 101 } 102 103 @Override onCleared()104 protected void onCleared() { 105 if (mSecuritySummary != null) { 106 mWifiHotspotRepository.getSecurityType().removeObserver(mSecurityTypeObserver); 107 } 108 if (mSpeedSummary != null) { 109 mWifiHotspotRepository.getSpeedType().removeObserver(mSpeedTypeObserver); 110 } 111 if (mSharedConnectivityRepository.isServiceAvailable()) { 112 mSharedConnectivityRepository.getSettingsState() 113 .removeObserver(mInstantHotspotStateObserver); 114 } 115 } 116 117 /** 118 * Return whether Wi-Fi Hotspot Speed Feature is available or not. 119 * 120 * @return {@code true} if Wi-Fi Hotspot Speed Feature is available 121 */ isSpeedFeatureAvailable()122 public boolean isSpeedFeatureAvailable() { 123 return mWifiHotspotRepository.isSpeedFeatureAvailable(); 124 } 125 126 /** 127 * Gets the Wi-Fi tethered AP Configuration. 128 * 129 * @return AP details in {@link SoftApConfiguration} 130 */ getSoftApConfiguration()131 public SoftApConfiguration getSoftApConfiguration() { 132 return mWifiHotspotRepository.getSoftApConfiguration(); 133 } 134 135 /** 136 * Sets the tethered Wi-Fi AP Configuration. 137 * 138 * @param config A valid SoftApConfiguration specifying the configuration of the SAP. 139 */ setSoftApConfiguration(SoftApConfiguration config)140 public void setSoftApConfiguration(SoftApConfiguration config) { 141 mWifiHotspotRepository.setSoftApConfiguration(config); 142 } 143 144 /** 145 * Refresh data from the SoftApConfiguration. 146 */ refresh()147 public void refresh() { 148 mWifiHotspotRepository.refresh(); 149 } 150 151 /** 152 * Gets SecuritySummary LiveData 153 */ getSecuritySummary()154 public LiveData<Integer> getSecuritySummary() { 155 if (mSecuritySummary == null) { 156 mSecuritySummary = new MutableLiveData<>(); 157 mWifiHotspotRepository.getSecurityType().observeForever(mSecurityTypeObserver); 158 } 159 return mSecuritySummary; 160 } 161 onSecurityTypeChanged(int securityType)162 protected void onSecurityTypeChanged(int securityType) { 163 int resId = R.string.summary_placeholder; 164 if (sSecuritySummaryResMap.containsKey(securityType)) { 165 resId = sSecuritySummaryResMap.get(securityType); 166 } 167 mSecuritySummary.setValue(resId); 168 } 169 170 /** 171 * Gets SpeedSummary LiveData 172 */ getSpeedSummary()173 public LiveData<Integer> getSpeedSummary() { 174 if (mSpeedSummary == null) { 175 mSpeedSummary = new MutableLiveData<>(); 176 mWifiHotspotRepository.getSpeedType().observeForever(mSpeedTypeObserver); 177 } 178 return mSpeedSummary; 179 } 180 onSpeedTypeChanged(Integer speedType)181 protected void onSpeedTypeChanged(Integer speedType) { 182 int resId = R.string.summary_placeholder; 183 if (sSpeedSummaryResMap.containsKey(speedType)) { 184 resId = sSpeedSummaryResMap.get(speedType); 185 } 186 mSpeedSummary.setValue(resId); 187 } 188 189 /** 190 * Gets Restarting LiveData 191 */ getRestarting()192 public LiveData<Boolean> getRestarting() { 193 return mWifiHotspotRepository.getRestarting(); 194 } 195 196 /** 197 * Return whether Wi-Fi Instant Hotspot feature is available or not. 198 * 199 * @return {@code true} if Wi-Fi Instant Hotspot feature is available 200 */ isInstantHotspotFeatureAvailable()201 public boolean isInstantHotspotFeatureAvailable() { 202 return mSharedConnectivityRepository.isServiceAvailable(); 203 } 204 205 /** 206 * Gets InstantHotspotSummary 207 */ getInstantHotspotSummary()208 public LiveData<String> getInstantHotspotSummary() { 209 return mInstantHotspotSummary; 210 } 211 212 @VisibleForTesting onInstantHotspotStateChanged(SharedConnectivitySettingsState state)213 void onInstantHotspotStateChanged(SharedConnectivitySettingsState state) { 214 log("onInstantHotspotStateChanged(), state:" + state); 215 if (state == null) { 216 mInstantHotspotSummary.setValue(null); 217 return; 218 } 219 mInstantHotspotSummary.setValue(getInstantHotspotSummary(state.isInstantTetherEnabled())); 220 } 221 getInstantHotspotSummary(boolean enabled)222 private String getInstantHotspotSummary(boolean enabled) { 223 return getApplication().getString( 224 enabled ? RES_INSTANT_HOTSPOT_SUMMARY_ON : RES_INSTANT_HOTSPOT_SUMMARY_OFF); 225 } 226 227 /** 228 * Launch Instant Hotspot Settings 229 */ launchInstantHotspotSettings()230 public void launchInstantHotspotSettings() { 231 mSharedConnectivityRepository.launchSettings(); 232 } 233 log(String msg)234 private void log(String msg) { 235 FeatureFactory.getFactory(getApplication().getApplicationContext()).getWifiFeatureProvider() 236 .verboseLog(TAG, msg); 237 } 238 } 239