1 /*
2  * Copyright (C) 2021 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.network;
18 
19 import android.content.Context;
20 
21 import androidx.lifecycle.Lifecycle;
22 import androidx.preference.Preference;
23 import androidx.preference.PreferenceScreen;
24 
25 import com.android.settings.R;
26 import com.android.settings.core.TogglePreferenceController;
27 import com.android.settings.wifi.WifiPickerTrackerHelper;
28 import com.android.wifitrackerlib.WifiPickerTracker;
29 
30 /**
31  * Preference controller for "Carrier Wi-Fi network"
32  */
33 public class CarrierWifiTogglePreferenceController extends TogglePreferenceController implements
34         WifiPickerTracker.WifiPickerTrackerCallback {
35 
36     private static final String TAG = "CarrierWifiTogglePreferenceController";
37     protected static final String CARRIER_WIFI_TOGGLE_PREF_KEY = "carrier_wifi_toggle";
38     protected static final String CARRIER_WIFI_NETWORK_PREF_KEY = "carrier_wifi_network";
39 
40     protected final Context mContext;
41     protected int mSubId;
42     protected WifiPickerTrackerHelper mWifiPickerTrackerHelper;
43     protected boolean mIsCarrierProvisionWifiEnabled;
44     protected Preference mCarrierNetworkPreference;
45 
CarrierWifiTogglePreferenceController(Context context, String preferenceKey)46     public CarrierWifiTogglePreferenceController(Context context,
47             String preferenceKey) {
48         super(context, preferenceKey);
49         mContext = context;
50     }
51 
52     /** Initialize related properties */
init(Lifecycle lifecycle, int subId)53     public void init(Lifecycle lifecycle, int subId) {
54         mSubId = subId;
55         mWifiPickerTrackerHelper = new WifiPickerTrackerHelper(lifecycle, mContext, this);
56         mIsCarrierProvisionWifiEnabled =
57                 mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(mSubId);
58     }
59 
60     @Override
getAvailabilityStatus()61     public int getAvailabilityStatus() {
62         return mIsCarrierProvisionWifiEnabled ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
63     }
64 
65     @Override
isChecked()66     public boolean isChecked() {
67         return mWifiPickerTrackerHelper.isCarrierNetworkEnabled();
68     }
69 
70     @Override
setChecked(boolean isChecked)71     public boolean setChecked(boolean isChecked) {
72         if (mWifiPickerTrackerHelper == null) {
73             return false;
74         }
75         mWifiPickerTrackerHelper.setCarrierNetworkEnabled(isChecked);
76         return true;
77     }
78 
79     @Override
displayPreference(PreferenceScreen screen)80     public void displayPreference(PreferenceScreen screen) {
81         super.displayPreference(screen);
82         mCarrierNetworkPreference = screen.findPreference(CARRIER_WIFI_NETWORK_PREF_KEY);
83         updateCarrierNetworkPreference();
84     }
85 
86     @Override
getSliceHighlightMenuRes()87     public int getSliceHighlightMenuRes() {
88         return R.string.menu_key_network;
89     }
90 
91     @Override
onWifiStateChanged()92     public void onWifiStateChanged() {
93         updateCarrierNetworkPreference();
94     }
95 
96     @Override
onWifiEntriesChanged()97     public void onWifiEntriesChanged() {
98         updateCarrierNetworkPreference();
99     }
100 
101     @Override
onNumSavedNetworksChanged()102     public void onNumSavedNetworksChanged() {
103         // Do nothing
104     }
105 
106     @Override
onNumSavedSubscriptionsChanged()107     public void onNumSavedSubscriptionsChanged() {
108         // Do nothing
109     }
110 
updateCarrierNetworkPreference()111     protected void updateCarrierNetworkPreference() {
112         if (mCarrierNetworkPreference == null) {
113             return;
114         }
115         if (getAvailabilityStatus() != AVAILABLE || !isCarrierNetworkActive()) {
116             mCarrierNetworkPreference.setVisible(false);
117             return;
118         }
119         mCarrierNetworkPreference.setVisible(true);
120         mCarrierNetworkPreference.setSummary(getCarrierNetworkSsid());
121     }
122 
isCarrierNetworkActive()123     protected boolean isCarrierNetworkActive() {
124         if (mWifiPickerTrackerHelper == null) {
125             return false;
126         }
127         return mWifiPickerTrackerHelper.isCarrierNetworkActive();
128     }
129 
getCarrierNetworkSsid()130     protected String getCarrierNetworkSsid() {
131         if (mWifiPickerTrackerHelper == null) {
132             return null;
133         }
134         return mWifiPickerTrackerHelper.getCarrierNetworkSsid();
135     }
136 }
137