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     private static Boolean mIsRegister = false;
41 
42     /**
43      * Begin listening for connectivity changes, supplying the connectivity state to the listener
44      * until stopped.
45      */
WifiMonitor(BuiltInPrintService service, Listener listener)46     public WifiMonitor(BuiltInPrintService service, Listener listener) {
47         if (DEBUG) Log.d(TAG, "WifiMonitor()");
48         ConnectivityManager connectivityManager =
49                 (ConnectivityManager) service.getSystemService(Context.CONNECTIVITY_SERVICE);
50         if (connectivityManager == null) {
51             return;
52         }
53 
54         mListener = listener;
55         if(!mIsRegister) {
56             mBroadcasts = service.receiveBroadcasts(new BroadcastReceiver() {
57                 @Override
58                 public void onReceive(Context context, Intent intent) {
59                     if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
60                         NetworkInfo info = connectivityManager.getNetworkInfo(
61                           ConnectivityManager.TYPE_WIFI);
62                         boolean isConnected = info != null && info.isConnected();
63                         if (mListener != null && (
64                           mConnected == null || mConnected != isConnected)) {
65                             mConnected = isConnected;
66                             mListener.onConnectionStateChanged(mConnected);
67                         }
68                     }
69                 }
70             }, ConnectivityManager.CONNECTIVITY_ACTION);
71             mIsRegister = true;
72         }
73     }
74 
75     /** Cease monitoring Wi-Fi connectivity status */
close()76     public void close() {
77         if (DEBUG) Log.d(TAG, "close()");
78         if (mIsRegister && mBroadcasts != null) {
79             mBroadcasts.close();
80             mIsRegister = false;
81         }
82         mListener = null;
83     }
84 
85     /** Communicate changes to the Wi-Fi connection state */
86     public interface Listener {
87         /** Called when the Wi-Fi connection state changes */
onConnectionStateChanged(boolean isConnected)88         void onConnectionStateChanged(boolean isConnected);
89     }
90 }
91