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.aware;
18 
19 import android.content.Context;
20 import android.os.HandlerThread;
21 import android.util.Log;
22 
23 import com.android.server.SystemService;
24 import com.android.server.wifi.HalDeviceManager;
25 import com.android.server.wifi.WifiContext;
26 import com.android.server.wifi.WifiInjector;
27 
28 /**
29  * Service implementing Wi-Fi Aware functionality. Delegates actual interface
30  * implementation to WifiAwareServiceImpl.
31  */
32 public final class WifiAwareService extends SystemService {
33     private static final String TAG = "WifiAwareService";
34     final WifiAwareServiceImpl mImpl;
35 
WifiAwareService(Context contextBase)36     public WifiAwareService(Context contextBase) {
37         super(new WifiContext(contextBase));
38         mImpl = new WifiAwareServiceImpl(getContext());
39     }
40 
41     @Override
onStart()42     public void onStart() {
43         Log.i(TAG, "Registering " + Context.WIFI_AWARE_SERVICE);
44         publishBinderService(Context.WIFI_AWARE_SERVICE, mImpl);
45     }
46 
47     @Override
onBootPhase(int phase)48     public void onBootPhase(int phase) {
49         if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
50             WifiInjector wifiInjector = WifiInjector.getInstance();
51             if (wifiInjector == null) {
52                 Log.e(TAG, "onBootPhase(PHASE_SYSTEM_SERVICES_READY): NULL injector!");
53                 return;
54             }
55 
56             HalDeviceManager halDeviceManager = wifiInjector.getHalDeviceManager();
57 
58             WifiAwareStateManager wifiAwareStateManager = new WifiAwareStateManager();
59             WifiAwareNativeCallback wifiAwareNativeCallback = new WifiAwareNativeCallback(
60                     wifiAwareStateManager);
61             WifiAwareNativeManager wifiAwareNativeManager = new WifiAwareNativeManager(
62                     wifiAwareStateManager, halDeviceManager, wifiAwareNativeCallback);
63             WifiAwareNativeApi wifiAwareNativeApi = new WifiAwareNativeApi(wifiAwareNativeManager);
64             wifiAwareStateManager.setNative(wifiAwareNativeManager, wifiAwareNativeApi);
65             WifiAwareShellCommand wifiAwareShellCommand = new WifiAwareShellCommand();
66             wifiAwareShellCommand.register("native_api", wifiAwareNativeApi);
67             wifiAwareShellCommand.register("native_cb", wifiAwareNativeCallback);
68             wifiAwareShellCommand.register("state_mgr", wifiAwareStateManager);
69 
70             HandlerThread awareHandlerThread = wifiInjector.getWifiAwareHandlerThread();
71             mImpl.start(awareHandlerThread, wifiAwareStateManager, wifiAwareShellCommand,
72                     wifiInjector.getWifiMetrics().getWifiAwareMetrics(),
73                     wifiInjector.getWifiPermissionsUtil(),
74                     wifiInjector.getWifiPermissionsWrapper(), wifiInjector.getSettingsConfigStore(),
75                     wifiAwareNativeManager, wifiAwareNativeApi, wifiAwareNativeCallback,
76                     wifiInjector.makeNetdWrapper());
77         } else if (phase == SystemService.PHASE_BOOT_COMPLETED) {
78             mImpl.startLate();
79         }
80     }
81 }
82 
83