1 /*
2  * Copyright (C) 2010 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.app.NotificationChannel;
20 import android.app.NotificationManager;
21 import android.content.Context;
22 import android.util.Log;
23 
24 import com.android.server.SystemService;
25 import com.android.server.wifi.util.WifiAsyncChannel;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 /**
31  * Service implementing Wi-Fi functionality. Delegates actual interface
32  * implementation to WifiServiceImpl.
33  */
34 public final class WifiService extends SystemService {
35 
36     private static final String TAG = "WifiService";
37     // Notification channels used by the wifi service.
38     public static final String NOTIFICATION_NETWORK_STATUS = "NETWORK_STATUS";
39     public static final String NOTIFICATION_NETWORK_ALERTS = "NETWORK_ALERTS";
40     public static final String NOTIFICATION_NETWORK_AVAILABLE = "NETWORK_AVAILABLE";
41 
42     private final WifiServiceImpl mImpl;
43     private final WifiContext mWifiContext;
44 
WifiService(Context contextBase)45     public WifiService(Context contextBase) {
46         super(contextBase);
47         mWifiContext = new WifiContext(contextBase);
48         WifiInjector injector = new WifiInjector(mWifiContext);
49         WifiAsyncChannel channel =  new WifiAsyncChannel(TAG);
50         mImpl = new WifiServiceImpl(mWifiContext, injector, channel);
51     }
52 
53     @Override
onStart()54     public void onStart() {
55         Log.i(TAG, "Registering " + Context.WIFI_SERVICE);
56         publishBinderService(Context.WIFI_SERVICE, mImpl);
57     }
58 
59     @Override
onBootPhase(int phase)60     public void onBootPhase(int phase) {
61         if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
62             createNotificationChannels(mWifiContext);
63             mImpl.checkAndStartWifi();
64         } else if (phase == SystemService.PHASE_BOOT_COMPLETED) {
65             mImpl.handleBootCompleted();
66         }
67     }
68 
69     @Override
onUserSwitching(TargetUser from, TargetUser to)70     public void onUserSwitching(TargetUser from, TargetUser to) {
71         mImpl.handleUserSwitch(to.getUserHandle().getIdentifier());
72     }
73 
74     @Override
onUserUnlocking(TargetUser user)75     public void onUserUnlocking(TargetUser user) {
76         mImpl.handleUserUnlock(user.getUserHandle().getIdentifier());
77     }
78 
79     @Override
onUserStopping(TargetUser user)80     public void onUserStopping(TargetUser user) {
81         mImpl.handleUserStop(user.getUserHandle().getIdentifier());
82     }
83 
84     // Create notification channels used by wifi.
createNotificationChannels(Context ctx)85     private static void createNotificationChannels(Context ctx) {
86         final NotificationManager nm = ctx.getSystemService(NotificationManager.class);
87         List<NotificationChannel> channelsList = new ArrayList<>();
88         final NotificationChannel networkStatusChannel = new NotificationChannel(
89                 NOTIFICATION_NETWORK_STATUS,
90                 ctx.getResources().getString(
91                         com.android.wifi.resources.R.string.notification_channel_network_status),
92                 NotificationManager.IMPORTANCE_LOW);
93         channelsList.add(networkStatusChannel);
94 
95         final NotificationChannel networkAlertsChannel = new NotificationChannel(
96                 NOTIFICATION_NETWORK_ALERTS,
97                 ctx.getResources().getString(
98                         com.android.wifi.resources.R.string.notification_channel_network_alerts),
99                 NotificationManager.IMPORTANCE_HIGH);
100         networkAlertsChannel.setBlockable(true);
101         channelsList.add(networkAlertsChannel);
102 
103         final NotificationChannel networkAvailable = new NotificationChannel(
104                 NOTIFICATION_NETWORK_AVAILABLE,
105                 ctx.getResources().getString(
106                         com.android.wifi.resources.R.string.notification_channel_network_available),
107                 NotificationManager.IMPORTANCE_LOW);
108         networkAvailable.setBlockable(true);
109         channelsList.add(networkAvailable);
110 
111         nm.createNotificationChannels(channelsList);
112     }
113 }
114