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 com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_2GHZ;
20 import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_2GHZ_5GHZ;
21 import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_5GHZ;
22 import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_6GHZ;
23 
24 import android.app.settings.SettingsEnums;
25 import android.os.Bundle;
26 
27 import androidx.annotation.VisibleForTesting;
28 
29 import com.android.settings.R;
30 import com.android.settings.dashboard.DashboardFragment;
31 import com.android.settings.overlay.FeatureFactory;
32 import com.android.settingslib.widget.SelectorWithWidgetPreference;
33 
34 import java.util.HashMap;
35 import java.util.Map;
36 
37 /**
38  * Wi-Fi Hotspot Speed & compatibility Settings
39  */
40 public class WifiHotspotSpeedSettings extends DashboardFragment implements
41         SelectorWithWidgetPreference.OnClickListener {
42 
43     private static final String TAG = "WifiHotspotSpeedSettings";
44 
45     protected static final String KEY_SPEED_2GHZ = "wifi_hotspot_speed_2g";
46     protected static final String KEY_SPEED_5GHZ = "wifi_hotspot_speed_5g";
47     protected static final String KEY_SPEED_2GHZ_5GHZ = "wifi_hotspot_speed_2g_5g";
48     protected static final String KEY_SPEED_6GHZ = "wifi_hotspot_speed_6g";
49     protected static Map<String, Integer> sSpeedKeyMap = new HashMap<>();
50 
51     static {
sSpeedKeyMap.put(KEY_SPEED_2GHZ, SPEED_2GHZ)52         sSpeedKeyMap.put(KEY_SPEED_2GHZ, SPEED_2GHZ);
sSpeedKeyMap.put(KEY_SPEED_5GHZ, SPEED_5GHZ)53         sSpeedKeyMap.put(KEY_SPEED_5GHZ, SPEED_5GHZ);
sSpeedKeyMap.put(KEY_SPEED_2GHZ_5GHZ, SPEED_2GHZ_5GHZ)54         sSpeedKeyMap.put(KEY_SPEED_2GHZ_5GHZ, SPEED_2GHZ_5GHZ);
sSpeedKeyMap.put(KEY_SPEED_6GHZ, SPEED_6GHZ)55         sSpeedKeyMap.put(KEY_SPEED_6GHZ, SPEED_6GHZ);
56     }
57 
58     protected WifiHotspotSpeedViewModel mWifiHotspotSpeedViewModel;
59     protected Map<Integer, SelectorWithWidgetPreference> mSpeedPreferenceMap = new HashMap<>();
60 
61     @Override
getPreferenceScreenResId()62     protected int getPreferenceScreenResId() {
63         return R.xml.wifi_hotspot_speed;
64     }
65 
66     @Override
getLogTag()67     protected String getLogTag() {
68         return TAG;
69     }
70 
71     @Override
getMetricsCategory()72     public int getMetricsCategory() {
73         return SettingsEnums.WIFI_TETHER_SETTINGS;
74     }
75 
76     @Override
onCreate(Bundle icicle)77     public void onCreate(Bundle icicle) {
78         super.onCreate(icicle);
79         loadPreferences();
80         mWifiHotspotSpeedViewModel = FeatureFactory.getFeatureFactory()
81                 .getWifiFeatureProvider().getWifiHotspotSpeedViewModel(this);
82         onSpeedInfoMapDataChanged(mWifiHotspotSpeedViewModel.getSpeedInfoMapData().getValue());
83         mWifiHotspotSpeedViewModel.getSpeedInfoMapData()
84                 .observe(this, this::onSpeedInfoMapDataChanged);
85         mWifiHotspotSpeedViewModel.getRestarting().observe(this, this::onRestartingChanged);
86     }
87 
loadPreferences()88     protected void loadPreferences() {
89         for (Map.Entry<String, Integer> entry : sSpeedKeyMap.entrySet()) {
90             SelectorWithWidgetPreference preference = findPreference(entry.getKey());
91             if (preference != null) {
92                 preference.setOnClickListener(this);
93                 mSpeedPreferenceMap.put(entry.getValue(), preference);
94             }
95         }
96     }
97 
onSpeedInfoMapDataChanged( Map<Integer, WifiHotspotSpeedViewModel.SpeedInfo> speedInfoMap)98     protected void onSpeedInfoMapDataChanged(
99             Map<Integer, WifiHotspotSpeedViewModel.SpeedInfo> speedInfoMap) {
100         log("onSpeedViewDataChanged(), speedInfoMap:" + speedInfoMap);
101         for (Map.Entry<Integer, SelectorWithWidgetPreference> entry :
102                 mSpeedPreferenceMap.entrySet()) {
103             WifiHotspotSpeedViewModel.SpeedInfo speedInfo = speedInfoMap.get(entry.getKey());
104             if (speedInfo == null) {
105                 continue;
106             }
107             SelectorWithWidgetPreference radioButton = entry.getValue();
108             if (radioButton == null) {
109                 continue;
110             }
111             if (!speedInfo.mIsVisible) {
112                 radioButton.setVisible(false);
113                 continue;
114             }
115             radioButton.setEnabled(speedInfo.mIsEnabled);
116             radioButton.setChecked(speedInfo.mIsChecked);
117             if (speedInfo.mSummary != null) {
118                 radioButton.setSummary(speedInfo.mSummary);
119             }
120             // setVisible at the end to avoid UI flickering
121             radioButton.setVisible(true);
122         }
123     }
124 
125     @VisibleForTesting
onRestartingChanged(Boolean restarting)126     void onRestartingChanged(Boolean restarting) {
127         log("onRestartingChanged(), restarting:" + restarting);
128         setLoading(restarting, false);
129     }
130 
131     @Override
onRadioButtonClicked(SelectorWithWidgetPreference emiter)132     public void onRadioButtonClicked(SelectorWithWidgetPreference emiter) {
133         String key = emiter.getKey();
134         log("onRadioButtonClicked(), key:" + key);
135         if (sSpeedKeyMap.containsKey(key)) {
136             mWifiHotspotSpeedViewModel.setSpeedType(sSpeedKeyMap.get(key));
137         }
138     }
139 
log(String msg)140     private void log(String msg) {
141         FeatureFactory.getFeatureFactory().getWifiFeatureProvider().verboseLog(TAG, msg);
142     }
143 }
144