1 /* 2 * Copyright (C) 2017 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 android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.net.wifi.SoftApConfiguration; 23 import android.text.TextUtils; 24 import android.util.Log; 25 26 import androidx.annotation.VisibleForTesting; 27 import androidx.preference.EditTextPreference; 28 import androidx.preference.Preference; 29 30 import com.android.settings.overlay.FeatureFactory; 31 import com.android.settings.widget.ValidatedEditTextPreference; 32 import com.android.settings.wifi.dpp.WifiDppUtils; 33 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 34 35 public class WifiTetherSSIDPreferenceController extends WifiTetherBasePreferenceController 36 implements ValidatedEditTextPreference.Validator { 37 38 private static final String TAG = "WifiTetherSsidPref"; 39 private static final String PREF_KEY = "wifi_tether_network_name"; 40 @VisibleForTesting 41 static final String DEFAULT_SSID = "AndroidAP"; 42 43 private String mSSID; 44 private WifiDeviceNameTextValidator mWifiDeviceNameTextValidator; 45 46 private final MetricsFeatureProvider mMetricsFeatureProvider; 47 48 // This constructor is used for testing. 49 @VisibleForTesting WifiTetherSSIDPreferenceController(Context context, OnTetherConfigUpdateListener listener, MetricsFeatureProvider provider)50 WifiTetherSSIDPreferenceController(Context context, OnTetherConfigUpdateListener listener, 51 MetricsFeatureProvider provider) { 52 super(context, listener); 53 mWifiDeviceNameTextValidator = new WifiDeviceNameTextValidator(); 54 mMetricsFeatureProvider = provider; 55 } 56 WifiTetherSSIDPreferenceController(Context context, OnTetherConfigUpdateListener listener)57 public WifiTetherSSIDPreferenceController(Context context, 58 OnTetherConfigUpdateListener listener) { 59 super(context, listener); 60 61 mWifiDeviceNameTextValidator = new WifiDeviceNameTextValidator(); 62 mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider(); 63 } 64 65 @Override getPreferenceKey()66 public String getPreferenceKey() { 67 return PREF_KEY; 68 } 69 70 @Override updateDisplay()71 public void updateDisplay() { 72 final SoftApConfiguration config = mWifiManager.getSoftApConfiguration(); 73 if (config != null) { 74 mSSID = config.getSsid(); 75 } else { 76 mSSID = DEFAULT_SSID; 77 } 78 ((ValidatedEditTextPreference) mPreference).setValidator(this); 79 80 if (mWifiManager.isWifiApEnabled() && config != null) { 81 final Intent intent = WifiDppUtils.getHotspotConfiguratorIntentOrNull(mContext, 82 mWifiManager, config); 83 84 if (intent == null) { 85 Log.e(TAG, "Invalid security to share hotspot"); 86 ((WifiTetherSsidPreference) mPreference).setButtonVisible(false); 87 } else { 88 ((WifiTetherSsidPreference) mPreference).setButtonOnClickListener( 89 view -> shareHotspotNetwork(intent)); 90 ((WifiTetherSsidPreference) mPreference).setButtonVisible(true); 91 } 92 } else { 93 ((WifiTetherSsidPreference) mPreference).setButtonVisible(false); 94 } 95 96 updateSsidDisplay((EditTextPreference) mPreference); 97 } 98 99 @Override onPreferenceChange(Preference preference, Object newValue)100 public boolean onPreferenceChange(Preference preference, Object newValue) { 101 if (!TextUtils.equals(mSSID, (String) newValue)) { 102 mMetricsFeatureProvider.action(mContext, 103 SettingsEnums.ACTION_SETTINGS_CHANGE_WIFI_HOTSPOT_NAME); 104 } 105 mSSID = (String) newValue; 106 updateSsidDisplay((EditTextPreference) preference); 107 mListener.onTetherConfigUpdated(this); 108 return true; 109 } 110 111 @Override isTextValid(String value)112 public boolean isTextValid(String value) { 113 return mWifiDeviceNameTextValidator.isTextValid(value); 114 } 115 getSSID()116 public String getSSID() { 117 return mSSID; 118 } 119 updateSsidDisplay(EditTextPreference preference)120 private void updateSsidDisplay(EditTextPreference preference) { 121 preference.setText(mSSID); 122 preference.setSummary(mSSID); 123 } 124 shareHotspotNetwork(Intent intent)125 private void shareHotspotNetwork(Intent intent) { 126 WifiDppUtils.showLockScreen(mContext, () -> { 127 mMetricsFeatureProvider.action(SettingsEnums.PAGE_UNKNOWN, 128 SettingsEnums.ACTION_SETTINGS_SHARE_WIFI_HOTSPOT_QR_CODE, 129 SettingsEnums.SETTINGS_WIFI_DPP_CONFIGURATOR, 130 /* key */ null, 131 /* value */ Integer.MIN_VALUE); 132 133 mContext.startActivity(intent); 134 }); 135 } 136 137 @VisibleForTesting isQrCodeButtonAvailable()138 boolean isQrCodeButtonAvailable() { 139 return ((WifiTetherSsidPreference) mPreference).isQrCodeButtonAvailable(); 140 } 141 } 142