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 android.app.settings.SettingsEnums;
20 import android.os.Bundle;
21 
22 import androidx.annotation.VisibleForTesting;
23 import androidx.lifecycle.LiveData;
24 
25 import com.android.settings.R;
26 import com.android.settings.dashboard.DashboardFragment;
27 import com.android.settings.overlay.FeatureFactory;
28 import com.android.settingslib.widget.SelectorWithWidgetPreference;
29 
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 
34 /**
35  * Wi-Fi Hotspot Security Settings
36  */
37 public class WifiHotspotSecuritySettings extends DashboardFragment implements
38         SelectorWithWidgetPreference.OnClickListener {
39     private static final String TAG = "WifiHotspotSecuritySettings";
40 
41     protected WifiHotspotSecurityViewModel mWifiHotspotSecurityViewModel;
42     protected Map<Integer, SelectorWithWidgetPreference> mPreferenceMap = new HashMap<>();
43 
44     @Override
getMetricsCategory()45     public int getMetricsCategory() {
46         return SettingsEnums.WIFI_TETHER_SETTINGS;
47     }
48 
49     @Override
getPreferenceScreenResId()50     protected int getPreferenceScreenResId() {
51         return R.xml.wifi_hotspot_security;
52     }
53 
54     @Override
getLogTag()55     protected String getLogTag() {
56         return TAG;
57     }
58 
59     @Override
onCreate(Bundle icicle)60     public void onCreate(Bundle icicle) {
61         super.onCreate(icicle);
62         loadViewModel();
63     }
64 
loadViewModel()65     protected void loadViewModel() {
66         mWifiHotspotSecurityViewModel = FeatureFactory.getFeatureFactory()
67                 .getWifiFeatureProvider().getWifiHotspotSecurityViewModel(this);
68         LiveData<List<WifiHotspotSecurityViewModel.ViewItem>> viewItemListData =
69                 mWifiHotspotSecurityViewModel.getViewItemListData();
70         viewItemListData.observe(this, this::onViewItemListDataChanged);
71         // set the onRadioButtonClicked callback to related preference
72         for (WifiHotspotSecurityViewModel.ViewItem viewItem : viewItemListData.getValue()) {
73             SelectorWithWidgetPreference preference = findPreference(viewItem.mKey);
74             preference.setOnClickListener(this);
75         }
76         mWifiHotspotSecurityViewModel.getRestarting().observe(this, this::onRestartingChanged);
77     }
78 
onViewItemListDataChanged( List<WifiHotspotSecurityViewModel.ViewItem> viewItems)79     protected void onViewItemListDataChanged(
80             List<WifiHotspotSecurityViewModel.ViewItem> viewItems) {
81         log("onViewItemListDataChanged(), viewItems:" + viewItems);
82         for (WifiHotspotSecurityViewModel.ViewItem viewItem : viewItems) {
83             SelectorWithWidgetPreference preference = findPreference(viewItem.mKey);
84             if (preference == null) {
85                 continue;
86             }
87             if (preference.isChecked() != viewItem.mIsChecked) {
88                 preference.setChecked(viewItem.mIsChecked);
89             }
90             if (preference.isEnabled() != viewItem.mIsEnabled) {
91                 preference.setEnabled(viewItem.mIsEnabled);
92                 if (viewItem.mIsEnabled) {
93                     preference.setSummary(null);
94                 } else {
95                     preference.setSummary(R.string.wifi_hotspot_security_summary_unavailable);
96                 }
97             }
98         }
99     }
100 
101     @VisibleForTesting
onRestartingChanged(Boolean restarting)102     void onRestartingChanged(Boolean restarting) {
103         log("onRestartingChanged(), restarting:" + restarting);
104         setLoading(restarting, false);
105     }
106 
107     @Override
onRadioButtonClicked(SelectorWithWidgetPreference emiter)108     public void onRadioButtonClicked(SelectorWithWidgetPreference emiter) {
109         String key = emiter.getKey();
110         log("onRadioButtonClicked(), key:" + key);
111         if (key.isEmpty()) {
112             return;
113         }
114         mWifiHotspotSecurityViewModel.handleRadioButtonClicked(key);
115     }
116 
log(String msg)117     private void log(String msg) {
118         FeatureFactory.getFeatureFactory().getWifiFeatureProvider().verboseLog(TAG, msg);
119     }
120 }
121