1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * Copyright (C) 2016 Mopria Alliance, Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.bips.util;
19 
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.net.ConnectivityManager;
24 import android.net.NetworkInfo;
25 import android.util.Log;
26 
27 import com.android.bips.BuiltInPrintService;
28 
29 /** Reliably reports on changes to Wi-Fi connectivity state */
30 public class WifiMonitor {
31     private static final String TAG = WifiMonitor.class.getSimpleName();
32     private static final boolean DEBUG = false;
33 
34     private BroadcastMonitor mBroadcasts;
35     private Listener mListener;
36 
37     /** Current connectivity state or null if not known yet */
38     private Boolean mConnected;
39 
40     /**
41      * Begin listening for connectivity changes, supplying the connectivity state to the listener
42      * until stopped.
43      */
WifiMonitor(BuiltInPrintService service, Listener listener)44     public WifiMonitor(BuiltInPrintService service, Listener listener) {
45         if (DEBUG) Log.d(TAG, "WifiMonitor()");
46         ConnectivityManager connectivityManager =
47                 (ConnectivityManager) service.getSystemService(Context.CONNECTIVITY_SERVICE);
48         if (connectivityManager == null) {
49             return;
50         }
51 
52         mListener = listener;
53         mBroadcasts = service.receiveBroadcasts(new BroadcastReceiver() {
54             @Override
55             public void onReceive(Context context, Intent intent) {
56                 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
57                     NetworkInfo info = connectivityManager.getActiveNetworkInfo();
58                     boolean isConnected = info != null && info.isConnected();
59                     if (mListener != null && (mConnected == null || mConnected != isConnected)) {
60                         mConnected = isConnected;
61                         mListener.onConnectionStateChanged(mConnected);
62                     }
63                 }
64             }
65         }, ConnectivityManager.CONNECTIVITY_ACTION);
66     }
67 
68     /** Cease monitoring Wi-Fi connectivity status */
close()69     public void close() {
70         if (DEBUG) Log.d(TAG, "close()");
71         if (mBroadcasts != null) {
72             mBroadcasts.close();
73         }
74         mListener = null;
75     }
76 
77     /** Communicate changes to the Wi-Fi connection state */
78     public interface Listener {
79         /** Called when the Wi-Fi connection state changes */
onConnectionStateChanged(boolean isConnected)80         void onConnectionStateChanged(boolean isConnected);
81     }
82 }
83