1 /*
2  * Copyright (C) 2016 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.server.wifi;
18 
19 import android.annotation.NonNull;
20 import android.app.ActivityManager;
21 import android.app.AlarmManager;
22 import android.app.AppOpsManager;
23 import android.app.NotificationManager;
24 import android.content.Context;
25 import android.content.pm.PackageManager;
26 import android.hardware.SystemSensorManager;
27 import android.net.IpMemoryStore;
28 import android.net.NetworkCapabilities;
29 import android.net.NetworkKey;
30 import android.net.NetworkScoreManager;
31 import android.net.wifi.IWifiScanner;
32 import android.net.wifi.IWificond;
33 import android.net.wifi.WifiManager;
34 import android.net.wifi.WifiNetworkScoreCache;
35 import android.net.wifi.WifiScanner;
36 import android.os.BatteryStats;
37 import android.os.Handler;
38 import android.os.HandlerThread;
39 import android.os.IBinder;
40 import android.os.INetworkManagementService;
41 import android.os.Looper;
42 import android.os.ServiceManager;
43 import android.os.SystemProperties;
44 import android.os.UserManager;
45 import android.provider.Settings.Secure;
46 import android.security.KeyStore;
47 import android.telephony.SubscriptionManager;
48 import android.telephony.TelephonyManager;
49 import android.util.LocalLog;
50 
51 import com.android.internal.R;
52 import com.android.internal.app.IBatteryStats;
53 import com.android.internal.os.PowerProfile;
54 import com.android.server.am.ActivityManagerService;
55 import com.android.server.am.BatteryStatsService;
56 import com.android.server.wifi.aware.WifiAwareMetrics;
57 import com.android.server.wifi.hotspot2.PasspointManager;
58 import com.android.server.wifi.hotspot2.PasspointNetworkEvaluator;
59 import com.android.server.wifi.hotspot2.PasspointObjectFactory;
60 import com.android.server.wifi.p2p.SupplicantP2pIfaceHal;
61 import com.android.server.wifi.p2p.WifiP2pMetrics;
62 import com.android.server.wifi.p2p.WifiP2pMonitor;
63 import com.android.server.wifi.p2p.WifiP2pNative;
64 import com.android.server.wifi.rtt.RttMetrics;
65 import com.android.server.wifi.util.WifiPermissionsUtil;
66 import com.android.server.wifi.util.WifiPermissionsWrapper;
67 
68 import java.util.Random;
69 
70 /**
71  *  WiFi dependency injector. To be used for accessing various WiFi class instances and as a
72  *  handle for mock injection.
73  *
74  *  Some WiFi class instances currently depend on having a Looper from a HandlerThread that has
75  *  been started. To accommodate this, we have a two-phased approach to initialize and retrieve
76  *  an instance of the WifiInjector.
77  */
78 public class WifiInjector {
79     private static final String BOOT_DEFAULT_WIFI_COUNTRY_CODE = "ro.boot.wificountrycode";
80     private static final String WIFICOND_SERVICE_NAME = "wificond";
81 
82     static WifiInjector sWifiInjector = null;
83 
84     private final Context mContext;
85     private final FrameworkFacade mFrameworkFacade = new FrameworkFacade();
86     private final DeviceConfigFacade mDeviceConfigFacade;
87     private final HandlerThread mWifiServiceHandlerThread;
88     private final HandlerThread mWifiCoreHandlerThread;
89     private final HandlerThread mWifiP2pServiceHandlerThread;
90     private final WifiTrafficPoller mWifiTrafficPoller;
91     private final WifiCountryCode mCountryCode;
92     private final BackupManagerProxy mBackupManagerProxy = new BackupManagerProxy();
93     private final WifiApConfigStore mWifiApConfigStore;
94     private final WifiNative mWifiNative;
95     private final WifiMonitor mWifiMonitor;
96     private final WifiP2pNative mWifiP2pNative;
97     private final WifiP2pMonitor mWifiP2pMonitor;
98     private final SupplicantStaIfaceHal mSupplicantStaIfaceHal;
99     private final SupplicantP2pIfaceHal mSupplicantP2pIfaceHal;
100     private final HostapdHal mHostapdHal;
101     private final WifiVendorHal mWifiVendorHal;
102     private final ScoringParams mScoringParams;
103     private final ClientModeImpl mClientModeImpl;
104     private final ActiveModeWarden mActiveModeWarden;
105     private final WifiSettingsStore mSettingsStore;
106     private OpenNetworkNotifier mOpenNetworkNotifier;
107     private CarrierNetworkNotifier mCarrierNetworkNotifier;
108     private final CarrierNetworkConfig mCarrierNetworkConfig;
109     private final WifiLockManager mLockManager;
110     private final WifiController mWifiController;
111     private final WificondControl mWificondControl;
112     private final Clock mClock = new Clock();
113     private final WifiMetrics mWifiMetrics;
114     private final WifiP2pMetrics mWifiP2pMetrics;
115     private WifiLastResortWatchdog mWifiLastResortWatchdog;
116     private final PropertyService mPropertyService = new SystemPropertyService();
117     private final BuildProperties mBuildProperties = new SystemBuildProperties();
118     private final KeyStore mKeyStore = KeyStore.getInstance();
119     private final WifiBackupRestore mWifiBackupRestore;
120     private final WifiMulticastLockManager mWifiMulticastLockManager;
121     private final WifiConfigStore mWifiConfigStore;
122     private final WifiKeyStore mWifiKeyStore;
123     private final WifiConfigManager mWifiConfigManager;
124     private final WifiConnectivityHelper mWifiConnectivityHelper;
125     private final LocalLog mConnectivityLocalLog;
126     private final WifiNetworkSelector mWifiNetworkSelector;
127     private final SavedNetworkEvaluator mSavedNetworkEvaluator;
128     private final NetworkSuggestionEvaluator mNetworkSuggestionEvaluator;
129     private final PasspointNetworkEvaluator mPasspointNetworkEvaluator;
130     private final ScoredNetworkEvaluator mScoredNetworkEvaluator;
131     private final CarrierNetworkEvaluator mCarrierNetworkEvaluator;
132     private final WifiNetworkScoreCache mWifiNetworkScoreCache;
133     private final NetworkScoreManager mNetworkScoreManager;
134     private WifiScanner mWifiScanner;
135     private final WifiPermissionsWrapper mWifiPermissionsWrapper;
136     private final WifiPermissionsUtil mWifiPermissionsUtil;
137     private final PasspointManager mPasspointManager;
138     private final SIMAccessor mSimAccessor;
139     private HandlerThread mWifiAwareHandlerThread;
140     private HandlerThread mRttHandlerThread;
141     private HalDeviceManager mHalDeviceManager;
142     private final IBatteryStats mBatteryStats;
143     private final WifiStateTracker mWifiStateTracker;
144     private final SelfRecovery mSelfRecovery;
145     private final WakeupController mWakeupController;
146     private final INetworkManagementService mNwManagementService;
147     private final ScanRequestProxy mScanRequestProxy;
148     private final SarManager mSarManager;
149     private final BaseWifiDiagnostics mWifiDiagnostics;
150     private final WifiDataStall mWifiDataStall;
151     private final WifiScoreCard mWifiScoreCard;
152     private final WifiNetworkSuggestionsManager mWifiNetworkSuggestionsManager;
153     private final DppMetrics mDppMetrics;
154     private final DppManager mDppManager;
155     private final LinkProbeManager mLinkProbeManager;
156     private final IpMemoryStore mIpMemoryStore;
157     private final CellularLinkLayerStatsCollector mCellularLinkLayerStatsCollector;
158     private final MacAddressUtil mMacAddressUtil;
159     private final ConnectionFailureNotificationBuilder mConnectionFailureNotificationBuilder;
160 
WifiInjector(Context context)161     public WifiInjector(Context context) {
162         if (context == null) {
163             throw new IllegalStateException(
164                     "WifiInjector should not be initialized with a null Context.");
165         }
166 
167         if (sWifiInjector != null) {
168             throw new IllegalStateException(
169                     "WifiInjector was already created, use getInstance instead.");
170         }
171 
172         sWifiInjector = this;
173 
174         mMacAddressUtil = new MacAddressUtil();
175         mContext = context;
176         mDeviceConfigFacade = new DeviceConfigFacade();
177         mConnectionFailureNotificationBuilder = new ConnectionFailureNotificationBuilder(
178                 mContext, getWifiStackPackageName(), mFrameworkFacade);
179         mWifiScoreCard = new WifiScoreCard(mClock,
180                 Secure.getString(mContext.getContentResolver(), Secure.ANDROID_ID));
181         mSettingsStore = new WifiSettingsStore(mContext);
182         mWifiPermissionsWrapper = new WifiPermissionsWrapper(mContext);
183         mNetworkScoreManager = mContext.getSystemService(NetworkScoreManager.class);
184         mWifiNetworkScoreCache = new WifiNetworkScoreCache(mContext);
185         mNetworkScoreManager.registerNetworkScoreCache(NetworkKey.TYPE_WIFI,
186                 mWifiNetworkScoreCache, NetworkScoreManager.SCORE_FILTER_NONE);
187         mWifiPermissionsUtil = new WifiPermissionsUtil(mWifiPermissionsWrapper, mContext,
188                 UserManager.get(mContext), this);
189         mWifiBackupRestore = new WifiBackupRestore(mWifiPermissionsUtil);
190         mBatteryStats = IBatteryStats.Stub.asInterface(mFrameworkFacade.getService(
191                 BatteryStats.SERVICE_NAME));
192         mWifiStateTracker = new WifiStateTracker(mBatteryStats);
193         // Now create and start handler threads
194         mWifiServiceHandlerThread = new HandlerThread("WifiService");
195         mWifiServiceHandlerThread.start();
196         mWifiCoreHandlerThread = new HandlerThread("ClientModeImpl");
197         mWifiCoreHandlerThread.start();
198         mWifiP2pServiceHandlerThread = new HandlerThread("WifiP2pService");
199         mWifiP2pServiceHandlerThread.start();
200         Looper clientModeImplLooper = mWifiCoreHandlerThread.getLooper();
201         mCarrierNetworkConfig = new CarrierNetworkConfig(mContext,
202                 clientModeImplLooper, mFrameworkFacade);
203         WifiAwareMetrics awareMetrics = new WifiAwareMetrics(mClock);
204         RttMetrics rttMetrics = new RttMetrics(mClock);
205         mWifiP2pMetrics = new WifiP2pMetrics(mClock);
206         mDppMetrics = new DppMetrics();
207         mCellularLinkLayerStatsCollector = new CellularLinkLayerStatsCollector(mContext);
208         mWifiMetrics = new WifiMetrics(mContext, mFrameworkFacade, mClock, clientModeImplLooper,
209                 awareMetrics, rttMetrics, new WifiPowerMetrics(), mWifiP2pMetrics, mDppMetrics,
210                 mCellularLinkLayerStatsCollector);
211         // Modules interacting with Native.
212         mWifiMonitor = new WifiMonitor(this);
213         mHalDeviceManager = new HalDeviceManager(mClock, clientModeImplLooper);
214         mWifiVendorHal =
215                 new WifiVendorHal(mHalDeviceManager, mWifiCoreHandlerThread.getLooper());
216         mSupplicantStaIfaceHal =
217                 new SupplicantStaIfaceHal(mContext, mWifiMonitor, mPropertyService,
218                         clientModeImplLooper);
219         mHostapdHal = new HostapdHal(mContext, clientModeImplLooper);
220         mWificondControl = new WificondControl(this, mWifiMonitor, mCarrierNetworkConfig,
221                 (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE),
222                 clientModeImplLooper, mClock);
223         mNwManagementService = INetworkManagementService.Stub.asInterface(
224                 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
225         mWifiNative = new WifiNative(
226                 mWifiVendorHal, mSupplicantStaIfaceHal, mHostapdHal, mWificondControl,
227                 mWifiMonitor, mNwManagementService, mPropertyService, mWifiMetrics,
228                 new Handler(mWifiCoreHandlerThread.getLooper()), new Random());
229         mWifiP2pMonitor = new WifiP2pMonitor(this);
230         mSupplicantP2pIfaceHal = new SupplicantP2pIfaceHal(mWifiP2pMonitor);
231         mWifiP2pNative = new WifiP2pNative(
232                 mWifiVendorHal, mSupplicantP2pIfaceHal, mHalDeviceManager,
233                 mPropertyService);
234 
235         // Now get instances of all the objects that depend on the HandlerThreads
236         mWifiTrafficPoller = new WifiTrafficPoller(clientModeImplLooper);
237         mCountryCode = new WifiCountryCode(mWifiNative,
238                 SystemProperties.get(BOOT_DEFAULT_WIFI_COUNTRY_CODE),
239                 mContext.getResources()
240                         .getBoolean(R.bool.config_wifi_revert_country_code_on_cellular_loss));
241         mWifiApConfigStore = new WifiApConfigStore(
242                 mContext, mWifiCoreHandlerThread.getLooper(), mBackupManagerProxy,
243                 mFrameworkFacade);
244 
245         // WifiConfigManager/Store objects and their dependencies.
246         // New config store
247         mWifiKeyStore = new WifiKeyStore(mKeyStore);
248         mWifiConfigStore = new WifiConfigStore(
249                 mContext, clientModeImplLooper, mClock, mWifiMetrics,
250                 WifiConfigStore.createSharedFile(mFrameworkFacade.isNiapModeOn(mContext)));
251         SubscriptionManager subscriptionManager =
252                 mContext.getSystemService(SubscriptionManager.class);
253         // Config Manager
254         mWifiConfigManager = new WifiConfigManager(mContext, mClock,
255                 UserManager.get(mContext), makeTelephonyManager(),
256                 mWifiKeyStore, mWifiConfigStore, mWifiPermissionsUtil,
257                 mWifiPermissionsWrapper, this, new NetworkListSharedStoreData(mContext),
258                 new NetworkListUserStoreData(mContext),
259                 new DeletedEphemeralSsidsStoreData(mClock), new RandomizedMacStoreData(),
260                 mFrameworkFacade, mWifiCoreHandlerThread.getLooper(), mDeviceConfigFacade);
261         mWifiMetrics.setWifiConfigManager(mWifiConfigManager);
262         mWifiConnectivityHelper = new WifiConnectivityHelper(mWifiNative);
263         mConnectivityLocalLog = new LocalLog(ActivityManager.isLowRamDeviceStatic() ? 256 : 512);
264         mScoringParams = new ScoringParams(mContext, mFrameworkFacade,
265                 new Handler(clientModeImplLooper));
266         mWifiMetrics.setScoringParams(mScoringParams);
267         mWifiNetworkSelector = new WifiNetworkSelector(mContext, mWifiScoreCard, mScoringParams,
268                 mWifiConfigManager, mClock, mConnectivityLocalLog, mWifiMetrics, mWifiNative);
269         CompatibilityScorer compatibilityScorer = new CompatibilityScorer(mScoringParams);
270         mWifiNetworkSelector.registerCandidateScorer(compatibilityScorer);
271         ScoreCardBasedScorer scoreCardBasedScorer = new ScoreCardBasedScorer(mScoringParams);
272         mWifiNetworkSelector.registerCandidateScorer(scoreCardBasedScorer);
273         BubbleFunScorer bubbleFunScorer = new BubbleFunScorer(mScoringParams);
274         mWifiNetworkSelector.registerCandidateScorer(bubbleFunScorer);
275         mWifiMetrics.setWifiNetworkSelector(mWifiNetworkSelector);
276         mSavedNetworkEvaluator = new SavedNetworkEvaluator(mContext, mScoringParams,
277                 mWifiConfigManager, mClock, mConnectivityLocalLog, mWifiConnectivityHelper,
278                 subscriptionManager);
279         mWifiNetworkSuggestionsManager = new WifiNetworkSuggestionsManager(mContext,
280                 new Handler(mWifiCoreHandlerThread.getLooper()), this, mWifiPermissionsUtil,
281                 mWifiConfigManager, mWifiConfigStore, mWifiMetrics, mWifiKeyStore);
282         mNetworkSuggestionEvaluator = new NetworkSuggestionEvaluator(mWifiNetworkSuggestionsManager,
283                 mWifiConfigManager, mConnectivityLocalLog);
284         mScoredNetworkEvaluator = new ScoredNetworkEvaluator(context, clientModeImplLooper,
285                 mFrameworkFacade, mNetworkScoreManager, mWifiConfigManager, mConnectivityLocalLog,
286                 mWifiNetworkScoreCache, mWifiPermissionsUtil);
287         mCarrierNetworkEvaluator = new CarrierNetworkEvaluator(mWifiConfigManager,
288                 mCarrierNetworkConfig, mConnectivityLocalLog, this);
289         mSimAccessor = new SIMAccessor(mContext);
290         mPasspointManager = new PasspointManager(mContext, this,
291                 new Handler(mWifiCoreHandlerThread.getLooper()), mWifiNative, mWifiKeyStore, mClock,
292                 mSimAccessor, new PasspointObjectFactory(), mWifiConfigManager, mWifiConfigStore,
293                 mWifiMetrics, makeTelephonyManager(), subscriptionManager);
294         mPasspointNetworkEvaluator = new PasspointNetworkEvaluator(
295                 mPasspointManager, mWifiConfigManager, mConnectivityLocalLog,
296                 mCarrierNetworkConfig, this, subscriptionManager);
297         mWifiMetrics.setPasspointManager(mPasspointManager);
298         mScanRequestProxy = new ScanRequestProxy(mContext,
299                 (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE),
300                 (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE),
301                 this, mWifiConfigManager,
302                 mWifiPermissionsUtil, mWifiMetrics, mClock, mFrameworkFacade,
303                 new Handler(clientModeImplLooper));
304         mSarManager = new SarManager(mContext, makeTelephonyManager(), clientModeImplLooper,
305                 mWifiNative, new SystemSensorManager(mContext, clientModeImplLooper),
306                 mWifiMetrics);
307         mWifiDiagnostics = new WifiDiagnostics(
308                 mContext, this, mWifiNative, mBuildProperties,
309                 new LastMileLogger(this), mClock);
310         mWifiDataStall = new WifiDataStall(mContext, mFrameworkFacade, mWifiMetrics,
311                 mDeviceConfigFacade, clientModeImplLooper, mClock);
312         mWifiMetrics.setWifiDataStall(mWifiDataStall);
313         mLinkProbeManager = new LinkProbeManager(mClock, mWifiNative, mWifiMetrics,
314                 mFrameworkFacade, mWifiCoreHandlerThread.getLooper(), mContext);
315         mClientModeImpl = new ClientModeImpl(mContext, mFrameworkFacade,
316                 clientModeImplLooper, UserManager.get(mContext),
317                 this, mBackupManagerProxy, mCountryCode, mWifiNative,
318                 new WrongPasswordNotifier(mContext, mFrameworkFacade),
319                 mSarManager, mWifiTrafficPoller, mLinkProbeManager);
320         mActiveModeWarden = new ActiveModeWarden(this, mContext, clientModeImplLooper,
321                 mWifiNative, new DefaultModeManager(mContext, clientModeImplLooper),
322                 mBatteryStats);
323 
324         WakeupNotificationFactory wakeupNotificationFactory =
325                 new WakeupNotificationFactory(mContext, mFrameworkFacade);
326         WakeupOnboarding wakeupOnboarding = new WakeupOnboarding(mContext, mWifiConfigManager,
327                 mWifiCoreHandlerThread.getLooper(), mFrameworkFacade,
328                 wakeupNotificationFactory);
329         mWakeupController = new WakeupController(mContext,
330                 mWifiCoreHandlerThread.getLooper(),
331                 new WakeupLock(mWifiConfigManager, mWifiMetrics.getWakeupMetrics(), mClock),
332                 new WakeupEvaluator(mScoringParams), wakeupOnboarding, mWifiConfigManager,
333                 mWifiConfigStore, mWifiNetworkSuggestionsManager, mWifiMetrics.getWakeupMetrics(),
334                 this, mFrameworkFacade, mClock);
335         mLockManager = new WifiLockManager(mContext, BatteryStatsService.getService(),
336                 mClientModeImpl, mFrameworkFacade, new Handler(clientModeImplLooper), mWifiNative,
337                 mClock, mWifiMetrics);
338         mWifiController = new WifiController(mContext, mClientModeImpl, clientModeImplLooper,
339                 mSettingsStore, mWifiServiceHandlerThread.getLooper(), mFrameworkFacade,
340                 mActiveModeWarden, mWifiPermissionsUtil);
341         mSelfRecovery = new SelfRecovery(mWifiController, mClock);
342         mWifiMulticastLockManager = new WifiMulticastLockManager(
343                 mClientModeImpl.getMcastLockManagerFilterController(),
344                 BatteryStatsService.getService());
345         mDppManager = new DppManager(mWifiCoreHandlerThread.getLooper(), mWifiNative,
346                 mWifiConfigManager, mContext, mDppMetrics);
347         mIpMemoryStore = IpMemoryStore.getMemoryStore(mContext);
348 
349         // Register the various network evaluators with the network selector.
350         mWifiNetworkSelector.registerNetworkEvaluator(mSavedNetworkEvaluator);
351         mWifiNetworkSelector.registerNetworkEvaluator(mNetworkSuggestionEvaluator);
352         if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_PASSPOINT)) {
353             mWifiNetworkSelector.registerNetworkEvaluator(mPasspointNetworkEvaluator);
354         }
355         mWifiNetworkSelector.registerNetworkEvaluator(mCarrierNetworkEvaluator);
356         mWifiNetworkSelector.registerNetworkEvaluator(mScoredNetworkEvaluator);
357 
358         mClientModeImpl.start();
359     }
360 
361     /**
362      *  Obtain an instance of the WifiInjector class.
363      *
364      *  This is the generic method to get an instance of the class. The first instance should be
365      *  retrieved using the getInstanceWithContext method.
366      */
getInstance()367     public static WifiInjector getInstance() {
368         if (sWifiInjector == null) {
369             throw new IllegalStateException(
370                     "Attempted to retrieve a WifiInjector instance before constructor was called.");
371         }
372         return sWifiInjector;
373     }
374 
375     /**
376      * Enable verbose logging in Injector objects. Called from the WifiServiceImpl (based on
377      * binder call).
378      */
enableVerboseLogging(int verbose)379     public void enableVerboseLogging(int verbose) {
380         mWifiLastResortWatchdog.enableVerboseLogging(verbose);
381         mWifiBackupRestore.enableVerboseLogging(verbose);
382         mHalDeviceManager.enableVerboseLogging(verbose);
383         mScanRequestProxy.enableVerboseLogging(verbose);
384         mWakeupController.enableVerboseLogging(verbose);
385         mCarrierNetworkConfig.enableVerboseLogging(verbose);
386         mWifiNetworkSuggestionsManager.enableVerboseLogging(verbose);
387         LogcatLog.enableVerboseLogging(verbose);
388         mDppManager.enableVerboseLogging(verbose);
389     }
390 
getUserManager()391     public UserManager getUserManager() {
392         return UserManager.get(mContext);
393     }
394 
getWifiMetrics()395     public WifiMetrics getWifiMetrics() {
396         return mWifiMetrics;
397     }
398 
getWifiP2pMetrics()399     public WifiP2pMetrics getWifiP2pMetrics() {
400         return mWifiP2pMetrics;
401     }
402 
getSupplicantStaIfaceHal()403     public SupplicantStaIfaceHal getSupplicantStaIfaceHal() {
404         return mSupplicantStaIfaceHal;
405     }
406 
getBackupManagerProxy()407     public BackupManagerProxy getBackupManagerProxy() {
408         return mBackupManagerProxy;
409     }
410 
getFrameworkFacade()411     public FrameworkFacade getFrameworkFacade() {
412         return mFrameworkFacade;
413     }
414 
getWifiServiceHandlerThread()415     public HandlerThread getWifiServiceHandlerThread() {
416         return mWifiServiceHandlerThread;
417     }
418 
getWifiP2pServiceHandlerThread()419     public HandlerThread getWifiP2pServiceHandlerThread() {
420         return mWifiP2pServiceHandlerThread;
421     }
422 
getWifiCoreHandlerThread()423     public HandlerThread getWifiCoreHandlerThread() {
424         return mWifiCoreHandlerThread;
425     }
426 
getWifiTrafficPoller()427     public WifiTrafficPoller getWifiTrafficPoller() {
428         return mWifiTrafficPoller;
429     }
430 
getWifiCountryCode()431     public WifiCountryCode getWifiCountryCode() {
432         return mCountryCode;
433     }
434 
getWifiApConfigStore()435     public WifiApConfigStore getWifiApConfigStore() {
436         return mWifiApConfigStore;
437     }
438 
getSarManager()439     public SarManager getSarManager() {
440         return mSarManager;
441     }
442 
getClientModeImpl()443     public ClientModeImpl getClientModeImpl() {
444         return mClientModeImpl;
445     }
446 
getClientModeImplHandler()447     public Handler getClientModeImplHandler() {
448         return mClientModeImpl.getHandler();
449     }
450 
getActiveModeWarden()451     public ActiveModeWarden getActiveModeWarden() {
452         return mActiveModeWarden;
453     }
454 
getWifiSettingsStore()455     public WifiSettingsStore getWifiSettingsStore() {
456         return mSettingsStore;
457     }
458 
getWifiLockManager()459     public WifiLockManager getWifiLockManager() {
460         return mLockManager;
461     }
462 
getWifiController()463     public WifiController getWifiController() {
464         return mWifiController;
465     }
466 
getWifiLastResortWatchdog()467     public WifiLastResortWatchdog getWifiLastResortWatchdog() {
468         return mWifiLastResortWatchdog;
469     }
470 
getClock()471     public Clock getClock() {
472         return mClock;
473     }
474 
getPropertyService()475     public PropertyService getPropertyService() {
476         return mPropertyService;
477     }
478 
getBuildProperties()479     public BuildProperties getBuildProperties() {
480         return mBuildProperties;
481     }
482 
getKeyStore()483     public KeyStore getKeyStore() {
484         return mKeyStore;
485     }
486 
getWifiBackupRestore()487     public WifiBackupRestore getWifiBackupRestore() {
488         return mWifiBackupRestore;
489     }
490 
getWifiMulticastLockManager()491     public WifiMulticastLockManager getWifiMulticastLockManager() {
492         return mWifiMulticastLockManager;
493     }
494 
getWifiConfigManager()495     public WifiConfigManager getWifiConfigManager() {
496         return mWifiConfigManager;
497     }
498 
getPasspointManager()499     public PasspointManager getPasspointManager() {
500         return mPasspointManager;
501     }
502 
getCarrierNetworkConfig()503     public CarrierNetworkConfig getCarrierNetworkConfig() {
504         return mCarrierNetworkConfig;
505     }
506 
getWakeupController()507     public WakeupController getWakeupController() {
508         return mWakeupController;
509     }
510 
getScoringParams()511     public ScoringParams getScoringParams() {
512         return mScoringParams;
513     }
514 
getWifiScoreCard()515     public WifiScoreCard getWifiScoreCard() {
516         return mWifiScoreCard;
517     }
518 
makeTelephonyManager()519     public TelephonyManager makeTelephonyManager() {
520         return (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
521     }
522 
getWifiStateTracker()523     public WifiStateTracker getWifiStateTracker() {
524         return mWifiStateTracker;
525     }
526 
getDppManager()527     public DppManager getDppManager() {
528         return mDppManager;
529     }
530 
531     /** Gets IWificond without caching. */
makeWificond()532     public IWificond makeWificond() {
533         // We depend on being able to refresh our binder in ClientModeImpl, so don't cache it.
534         IBinder binder = ServiceManager.getService(WIFICOND_SERVICE_NAME);
535         return IWificond.Stub.asInterface(binder);
536     }
537 
538     /**
539      * Create a SoftApManager.
540      * @param config SoftApModeConfiguration object holding the config and mode
541      * @return an instance of SoftApManager
542      */
makeSoftApManager(@onNull WifiManager.SoftApCallback callback, @NonNull SoftApModeConfiguration config)543     public SoftApManager makeSoftApManager(@NonNull WifiManager.SoftApCallback callback,
544                                            @NonNull SoftApModeConfiguration config) {
545         return new SoftApManager(mContext, mWifiCoreHandlerThread.getLooper(),
546                 mFrameworkFacade, mWifiNative, mCountryCode.getCountryCode(), callback,
547                 mWifiApConfigStore, config, mWifiMetrics, mSarManager, mWifiDiagnostics);
548     }
549 
550     /**
551      * Create a ScanOnlyModeManager
552      *
553      * @param listener listener for ScanOnlyModeManager state changes
554      * @return a new instance of ScanOnlyModeManager
555      */
makeScanOnlyModeManager( @onNull ScanOnlyModeManager.Listener listener)556     public ScanOnlyModeManager makeScanOnlyModeManager(
557             @NonNull ScanOnlyModeManager.Listener listener) {
558         return new ScanOnlyModeManager(mContext, mWifiCoreHandlerThread.getLooper(),
559                 mWifiNative, listener, mWifiMetrics, mWakeupController,
560                 mSarManager);
561     }
562 
563     /**
564      * Create a ClientModeManager
565      *
566      * @param listener listener for ClientModeManager state changes
567      * @return a new instance of ClientModeManager
568      */
makeClientModeManager(ClientModeManager.Listener listener)569     public ClientModeManager makeClientModeManager(ClientModeManager.Listener listener) {
570         return new ClientModeManager(mContext, mWifiCoreHandlerThread.getLooper(),
571                 mWifiNative, listener, mWifiMetrics, mClientModeImpl);
572     }
573 
574     /**
575      * Create a WifiLog instance.
576      * @param tag module name to include in all log messages
577      */
makeLog(String tag)578     public WifiLog makeLog(String tag) {
579         return new LogcatLog(tag);
580     }
581 
getWifiDiagnostics()582     public BaseWifiDiagnostics getWifiDiagnostics() {
583         return mWifiDiagnostics;
584     }
585 
586     /**
587      * Obtain an instance of WifiScanner.
588      * If it was not already created, then obtain an instance.  Note, this must be done lazily since
589      * WifiScannerService is separate and created later.
590      */
getWifiScanner()591     public synchronized WifiScanner getWifiScanner() {
592         if (mWifiScanner == null) {
593             mWifiScanner = new WifiScanner(mContext,
594                     IWifiScanner.Stub.asInterface(ServiceManager.getService(
595                             Context.WIFI_SCANNING_SERVICE)),
596                     mWifiCoreHandlerThread.getLooper());
597         }
598         return mWifiScanner;
599     }
600 
601     /**
602      * Construct a new instance of WifiConnectivityManager & its dependencies.
603      *
604      * Create and return a new WifiConnectivityManager.
605      * @param clientModeImpl Instance of client mode impl.
606      * TODO(b/116233964): Remove cyclic dependency between WifiConnectivityManager & ClientModeImpl.
607      */
makeWifiConnectivityManager(ClientModeImpl clientModeImpl)608     public WifiConnectivityManager makeWifiConnectivityManager(ClientModeImpl clientModeImpl) {
609         mOpenNetworkNotifier = new OpenNetworkNotifier(mContext,
610                 mWifiCoreHandlerThread.getLooper(), mFrameworkFacade, mClock, mWifiMetrics,
611                 mWifiConfigManager, mWifiConfigStore, clientModeImpl,
612                 new ConnectToNetworkNotificationBuilder(mContext, mFrameworkFacade));
613         mCarrierNetworkNotifier = new CarrierNetworkNotifier(mContext,
614                 mWifiCoreHandlerThread.getLooper(), mFrameworkFacade, mClock, mWifiMetrics,
615                 mWifiConfigManager, mWifiConfigStore, clientModeImpl,
616                 new ConnectToNetworkNotificationBuilder(mContext, mFrameworkFacade));
617         mWifiLastResortWatchdog = new WifiLastResortWatchdog(this, mContext, mClock,
618                 mWifiMetrics, clientModeImpl, clientModeImpl.getHandler().getLooper(),
619                 mDeviceConfigFacade);
620         return new WifiConnectivityManager(mContext, getScoringParams(),
621                 clientModeImpl, this,
622                 mWifiConfigManager, clientModeImpl.getWifiInfo(),
623                 mWifiNetworkSelector, mWifiConnectivityHelper,
624                 mWifiLastResortWatchdog, mOpenNetworkNotifier, mCarrierNetworkNotifier,
625                 mCarrierNetworkConfig, mWifiMetrics, mWifiCoreHandlerThread.getLooper(),
626                 mClock, mConnectivityLocalLog);
627     }
628 
629     /**
630      * Construct a new instance of ConnectionFailureNotifier.
631      * @param wifiConnectivityManager
632      * @return the created instance
633      */
makeConnectionFailureNotifier( WifiConnectivityManager wifiConnectivityManager)634     public ConnectionFailureNotifier makeConnectionFailureNotifier(
635             WifiConnectivityManager wifiConnectivityManager) {
636         return new ConnectionFailureNotifier(mContext, this, mFrameworkFacade, mWifiConfigManager,
637                 wifiConnectivityManager, new Handler(mWifiCoreHandlerThread.getLooper()));
638     }
639 
640     /**
641      * Construct a new instance of {@link WifiNetworkFactory}.
642      * TODO(b/116233964): Remove cyclic dependency between WifiConnectivityManager & ClientModeImpl.
643      */
makeWifiNetworkFactory( NetworkCapabilities nc, WifiConnectivityManager wifiConnectivityManager)644     public WifiNetworkFactory makeWifiNetworkFactory(
645             NetworkCapabilities nc, WifiConnectivityManager wifiConnectivityManager) {
646         return new WifiNetworkFactory(
647                 mWifiCoreHandlerThread.getLooper(), mContext, nc,
648                 (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE),
649                 (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE),
650                 (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE),
651                 mClock, this, wifiConnectivityManager, mWifiConfigManager,
652                 mWifiConfigStore, mWifiPermissionsUtil, mWifiMetrics);
653     }
654 
655     /**
656      * Construct an instance of {@link NetworkRequestStoreData}.
657      */
makeNetworkRequestStoreData( NetworkRequestStoreData.DataSource dataSource)658     public NetworkRequestStoreData makeNetworkRequestStoreData(
659             NetworkRequestStoreData.DataSource dataSource) {
660         return new NetworkRequestStoreData(dataSource);
661     }
662 
663     /**
664      * Construct a new instance of {@link UntrustedWifiNetworkFactory}.
665      * TODO(b/116233964): Remove cyclic dependency between WifiConnectivityManager & ClientModeImpl.
666      */
makeUntrustedWifiNetworkFactory( NetworkCapabilities nc, WifiConnectivityManager wifiConnectivityManager)667     public UntrustedWifiNetworkFactory makeUntrustedWifiNetworkFactory(
668             NetworkCapabilities nc, WifiConnectivityManager wifiConnectivityManager) {
669         return new UntrustedWifiNetworkFactory(
670                 mWifiCoreHandlerThread.getLooper(), mContext, nc, wifiConnectivityManager);
671     }
672 
673     /**
674      * Construct an instance of {@link NetworkSuggestionStoreData}.
675      */
makeNetworkSuggestionStoreData( NetworkSuggestionStoreData.DataSource dataSource)676     public NetworkSuggestionStoreData makeNetworkSuggestionStoreData(
677             NetworkSuggestionStoreData.DataSource dataSource) {
678         return new NetworkSuggestionStoreData(dataSource);
679     }
680 
getWifiPermissionsUtil()681     public WifiPermissionsUtil getWifiPermissionsUtil() {
682         return mWifiPermissionsUtil;
683     }
684 
getWifiPermissionsWrapper()685     public WifiPermissionsWrapper getWifiPermissionsWrapper() {
686         return mWifiPermissionsWrapper;
687     }
688 
689     /**
690      * Returns a singleton instance of a HandlerThread for injection. Uses lazy initialization.
691      *
692      * TODO: share worker thread with other Wi-Fi handlers (b/27924886)
693      */
getWifiAwareHandlerThread()694     public HandlerThread getWifiAwareHandlerThread() {
695         if (mWifiAwareHandlerThread == null) { // lazy initialization
696             mWifiAwareHandlerThread = new HandlerThread("wifiAwareService");
697             mWifiAwareHandlerThread.start();
698         }
699         return mWifiAwareHandlerThread;
700     }
701 
702     /**
703      * Returns a singleton instance of a HandlerThread for injection. Uses lazy initialization.
704      *
705      * TODO: share worker thread with other Wi-Fi handlers (b/27924886)
706      */
getRttHandlerThread()707     public HandlerThread getRttHandlerThread() {
708         if (mRttHandlerThread == null) { // lazy initialization
709             mRttHandlerThread = new HandlerThread("wifiRttService");
710             mRttHandlerThread.start();
711         }
712         return mRttHandlerThread;
713     }
714 
getMacAddressUtil()715     public MacAddressUtil getMacAddressUtil() {
716         return mMacAddressUtil;
717     }
718 
getNotificationManager()719     public NotificationManager getNotificationManager() {
720         return (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
721     }
722 
getConnectionFailureNotificationBuilder()723     public ConnectionFailureNotificationBuilder getConnectionFailureNotificationBuilder() {
724         return mConnectionFailureNotificationBuilder;
725     }
726 
727     /**
728      * Returns a single instance of HalDeviceManager for injection.
729      */
getHalDeviceManager()730     public HalDeviceManager getHalDeviceManager() {
731         return mHalDeviceManager;
732     }
733 
getWifiNative()734     public WifiNative getWifiNative() {
735         return mWifiNative;
736     }
737 
getWifiMonitor()738     public WifiMonitor getWifiMonitor() {
739         return mWifiMonitor;
740     }
741 
getWifiP2pNative()742     public WifiP2pNative getWifiP2pNative() {
743         return mWifiP2pNative;
744     }
745 
getWifiP2pMonitor()746     public WifiP2pMonitor getWifiP2pMonitor() {
747         return mWifiP2pMonitor;
748     }
749 
getSelfRecovery()750     public SelfRecovery getSelfRecovery() {
751         return mSelfRecovery;
752     }
753 
getPowerProfile()754     public PowerProfile getPowerProfile() {
755         return new PowerProfile(mContext, false);
756     }
757 
getScanRequestProxy()758     public ScanRequestProxy getScanRequestProxy() {
759         return mScanRequestProxy;
760     }
761 
getJavaRuntime()762     public Runtime getJavaRuntime() {
763         return Runtime.getRuntime();
764     }
765 
getActivityManagerService()766     public ActivityManagerService getActivityManagerService() {
767         return (ActivityManagerService) ActivityManager.getService();
768     }
769 
getWifiDataStall()770     public WifiDataStall getWifiDataStall() {
771         return mWifiDataStall;
772     }
773 
getWifiNetworkSuggestionsManager()774     public WifiNetworkSuggestionsManager getWifiNetworkSuggestionsManager() {
775         return mWifiNetworkSuggestionsManager;
776     }
777 
getIpMemoryStore()778     public IpMemoryStore getIpMemoryStore() {
779         return mIpMemoryStore;
780     }
781 
getHostapdHal()782     public HostapdHal getHostapdHal() {
783         return mHostapdHal;
784     }
785 
getWifiStackPackageName()786     public String getWifiStackPackageName() {
787         return mContext.getPackageName();
788     }
789 }
790