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