• 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.annotation.NonNull;
20 import android.content.Context;
21 import android.net.ConnectivityManager;
22 import android.net.wifi.SoftApConfiguration;
23 import android.net.wifi.WifiClient;
24 import android.net.wifi.WifiManager;
25 import android.text.BidiFormatter;
26 
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.settings.R;
32 import com.android.settings.Utils;
33 import com.android.settings.core.PreferenceControllerMixin;
34 import com.android.settingslib.core.AbstractPreferenceController;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 import com.android.settingslib.core.lifecycle.LifecycleObserver;
37 import com.android.settingslib.core.lifecycle.events.OnStart;
38 import com.android.settingslib.core.lifecycle.events.OnStop;
39 
40 import java.util.List;
41 
42 public class WifiTetherPreferenceController extends AbstractPreferenceController
43         implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop {
44 
45     private static final String WIFI_TETHER_SETTINGS = "wifi_tether";
46 
47     private final ConnectivityManager mConnectivityManager;
48     private final String[] mWifiRegexs;
49     private final WifiManager mWifiManager;
50     private final Lifecycle mLifecycle;
51     private int mSoftApState;
52     @VisibleForTesting
53     Preference mPreference;
54     @VisibleForTesting
55     WifiTetherSoftApManager mWifiTetherSoftApManager;
56 
WifiTetherPreferenceController(Context context, Lifecycle lifecycle)57     public WifiTetherPreferenceController(Context context, Lifecycle lifecycle) {
58         this(context, lifecycle, true /* initSoftApManager */);
59     }
60 
61     @VisibleForTesting
WifiTetherPreferenceController(Context context, Lifecycle lifecycle, boolean initSoftApManager)62     WifiTetherPreferenceController(Context context, Lifecycle lifecycle,
63             boolean initSoftApManager) {
64         super(context);
65         mConnectivityManager =
66                 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
67         mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
68         mWifiRegexs = mConnectivityManager.getTetherableWifiRegexs();
69         mLifecycle = lifecycle;
70         if (lifecycle != null) {
71             lifecycle.addObserver(this);
72         }
73         if (initSoftApManager) {
74             initWifiTetherSoftApManager();
75         }
76     }
77 
78     @Override
isAvailable()79     public boolean isAvailable() {
80         return mWifiRegexs != null
81                 && mWifiRegexs.length != 0
82                 && !Utils.isMonkeyRunning();
83     }
84 
85     @Override
displayPreference(PreferenceScreen screen)86     public void displayPreference(PreferenceScreen screen) {
87         super.displayPreference(screen);
88         mPreference = screen.findPreference(WIFI_TETHER_SETTINGS);
89         if (mPreference == null) {
90             // unavailable
91             return;
92         }
93     }
94 
95     @Override
getPreferenceKey()96     public String getPreferenceKey() {
97         return WIFI_TETHER_SETTINGS;
98     }
99 
100     @Override
onStart()101     public void onStart() {
102         if (mPreference != null) {
103             if (mWifiTetherSoftApManager != null) {
104                 mWifiTetherSoftApManager.registerSoftApCallback();
105             }
106         }
107     }
108 
109     @Override
onStop()110     public void onStop() {
111         if (mPreference != null) {
112             if (mWifiTetherSoftApManager != null) {
113                 mWifiTetherSoftApManager.unRegisterSoftApCallback();
114             }
115         }
116     }
117 
118     @VisibleForTesting
initWifiTetherSoftApManager()119     void initWifiTetherSoftApManager() {
120         // This manager only handles the number of connected devices, other parts are handled by
121         // normal BroadcastReceiver in this controller
122         mWifiTetherSoftApManager = new WifiTetherSoftApManager(mWifiManager,
123                 new WifiTetherSoftApManager.WifiTetherSoftApCallback() {
124                     @Override
125                     public void onStateChanged(int state, int failureReason) {
126                         mSoftApState = state;
127                         handleWifiApStateChanged(state, failureReason);
128                     }
129 
130                     @Override
131                     public void onConnectedClientsChanged(List<WifiClient> clients) {
132                         if (mPreference != null
133                                 && mSoftApState == WifiManager.WIFI_AP_STATE_ENABLED) {
134                             // Only show the number of clients when state is on
135                             mPreference.setSummary(mContext.getResources().getQuantityString(
136                                     R.plurals.wifi_tether_connected_summary, clients.size(),
137                                     clients.size()));
138                         }
139                     }
140                 });
141     }
142 
143     @VisibleForTesting
handleWifiApStateChanged(int state, int reason)144     void handleWifiApStateChanged(int state, int reason) {
145         switch (state) {
146             case WifiManager.WIFI_AP_STATE_ENABLING:
147                 mPreference.setSummary(R.string.wifi_tether_starting);
148                 break;
149             case WifiManager.WIFI_AP_STATE_ENABLED:
150                 final SoftApConfiguration softApConfig = mWifiManager.getSoftApConfiguration();
151                 updateConfigSummary(softApConfig);
152                 break;
153             case WifiManager.WIFI_AP_STATE_DISABLING:
154                 mPreference.setSummary(R.string.wifi_tether_stopping);
155                 break;
156             case WifiManager.WIFI_AP_STATE_DISABLED:
157                 mPreference.setSummary(R.string.wifi_hotspot_off_subtext);
158                 break;
159             default:
160                 if (reason == WifiManager.SAP_START_FAILURE_NO_CHANNEL) {
161                     mPreference.setSummary(R.string.wifi_sap_no_channel_error);
162                 } else {
163                     mPreference.setSummary(R.string.wifi_error);
164                 }
165         }
166     }
167 
updateConfigSummary(@onNull SoftApConfiguration softApConfig)168     private void updateConfigSummary(@NonNull SoftApConfiguration softApConfig) {
169         if (softApConfig == null) {
170             // Should never happen.
171             return;
172         }
173         mPreference.setSummary(mContext.getString(R.string.wifi_tether_enabled_subtext,
174                 BidiFormatter.getInstance().unicodeWrap(softApConfig.getSsid())));
175     }
176 }
177