Home | History | Annotate | Download | only in networkrecommendation
      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.networkrecommendation;
     18 
     19 import android.app.NotificationManager;
     20 import android.app.Service;
     21 import android.content.ContentResolver;
     22 import android.content.Intent;
     23 import android.content.res.Resources;
     24 import android.net.NetworkScoreManager;
     25 import android.net.wifi.WifiManager;
     26 import android.os.Handler;
     27 import android.os.HandlerThread;
     28 import android.os.IBinder;
     29 import android.os.Looper;
     30 import android.os.PowerManager;
     31 import android.os.UserManager;
     32 
     33 import com.android.networkrecommendation.notify.WifiNotificationController;
     34 import com.android.networkrecommendation.notify.WifiNotificationHelper;
     35 import com.android.networkrecommendation.util.NotificationChannelUtil;
     36 import com.android.networkrecommendation.wakeup.WifiWakeupController;
     37 import com.android.networkrecommendation.wakeup.WifiWakeupHelper;
     38 import com.android.networkrecommendation.wakeup.WifiWakeupNetworkSelector;
     39 
     40 import java.io.FileDescriptor;
     41 import java.io.PrintWriter;
     42 
     43 /**
     44  * Provides network recommendations for the platform.
     45  */
     46 public class NetworkRecommendationService extends Service {
     47 
     48     private HandlerThread mProviderHandlerThread;
     49     private Handler mProviderHandler;
     50     private HandlerThread mControllerHandlerThread;
     51     private Handler mControllerHandler;
     52     private DefaultNetworkRecommendationProvider mProvider;
     53     private WifiNotificationController mWifiNotificationController;
     54     private WifiWakeupController mWifiWakeupController;
     55 
     56     @Override
     57     public void onCreate() {
     58         mProviderHandlerThread = new HandlerThread("RecommendationProvider");
     59         mProviderHandlerThread.start();
     60         mProviderHandler = new Handler(mProviderHandlerThread.getLooper());
     61         NetworkScoreManager networkScoreManager = getSystemService(NetworkScoreManager.class);
     62         mProvider = new DefaultNetworkRecommendationProvider(this, mProviderHandler::post,
     63                 networkScoreManager, new DefaultNetworkRecommendationProvider.ScoreStorage());
     64 
     65         mControllerHandlerThread = new HandlerThread("RecommendationProvider");
     66         mControllerHandlerThread.start();
     67         mControllerHandler = new Handler(mControllerHandlerThread.getLooper());
     68         NotificationManager notificationManager = getSystemService(NotificationManager.class);
     69         NotificationChannelUtil.configureNotificationChannels(notificationManager, this);
     70 
     71         WifiManager wifiManager = getSystemService(WifiManager.class);
     72         PowerManager powerManager = getSystemService(PowerManager.class);
     73         UserManager userManager = getSystemService(UserManager.class);
     74         Resources resources = getResources();
     75         ContentResolver contentResolver = getContentResolver();
     76         mWifiNotificationController = new WifiNotificationController(
     77                 this, contentResolver, mControllerHandler, mProvider,
     78                 wifiManager, notificationManager, userManager, new WifiNotificationHelper(this));
     79         WifiWakeupNetworkSelector wifiWakeupNetworkSelector =
     80                 new WifiWakeupNetworkSelector(resources, mProvider);
     81         WifiWakeupHelper wifiWakeupHelper = new WifiWakeupHelper(this, resources, mControllerHandler,
     82                 notificationManager, wifiManager);
     83         mWifiWakeupController =
     84                 new WifiWakeupController(this, getContentResolver(), mControllerHandler, wifiManager,
     85                         powerManager, userManager, wifiWakeupNetworkSelector, wifiWakeupHelper);
     86     }
     87 
     88     @Override
     89     public IBinder onBind(Intent intent) {
     90         mWifiWakeupController.start();
     91         mWifiNotificationController.start();
     92         return mProvider.getBinder();
     93     }
     94 
     95     @Override
     96     public boolean onUnbind(Intent intent) {
     97         mWifiWakeupController.stop();
     98         mWifiNotificationController.stop();
     99         return super.onUnbind(intent);
    100     }
    101 
    102     @Override
    103     public void onDestroy() {
    104         mProviderHandlerThread.quit();
    105         super.onDestroy();
    106     }
    107 
    108     @Override
    109     protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
    110         mProvider.dump(fd, writer, args);
    111         mWifiNotificationController.dump(fd, writer, args);
    112         mWifiWakeupController.dump(fd, writer, args);
    113     }
    114 }
    115