1 /*
2  * Copyright 2014, 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.managedprovisioning;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.net.ConnectivityManager;
24 import android.net.NetworkInfo;
25 import android.net.NetworkInfo.DetailedState;
26 
27 /**
28  * Monitor the state of the data network and the checkin service. Invoke a callback when the network
29  * is connected and checkin has succeeded. Callbacks are made on the thread that created this
30  * object.
31  */
32 public class NetworkMonitor {
33     /** State notification callback. Expect some duplicate notifications. */
34     public interface Callback {
35 
onNetworkConnected()36         void onNetworkConnected();
37 
onNetworkDisconnected()38         void onNetworkDisconnected();
39     }
40 
41     private Context mContext = null;
42     private Callback mCallback = null;
43 
44     private boolean mNetworkConnected = false;
45 
46     private boolean mReceiverRegistered;
47 
48     /**
49      * Start watching the network and monitoring the checkin service. Immediately invokes one of the
50      * callback methods to report the current state, and then invokes callback methods over time as
51      * the state changes.
52      *
53      * @param context to use for intent observers and such
54      * @param callback to invoke when the network status changes
55      */
NetworkMonitor(Context context, Callback callback)56     public NetworkMonitor(Context context, Callback callback) {
57         mContext = context;
58         mCallback = callback;
59 
60         IntentFilter filter = new IntentFilter();
61         filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
62 
63         // Listen to immediate connectivity changes which are 3 seconds
64         // earlier than CONNECTIVITY_ACTION and may not have IPv6 routes
65         // setup. However, this may allow us to start up services like
66         // the CheckinService a bit earlier.
67         filter.addAction(ConnectivityManager.INET_CONDITION_ACTION);
68 
69         context.registerReceiver(mBroadcastReceiver, filter);
70         mReceiverRegistered = true;
71 
72     }
73 
74     /**
75      * Stop watching the network and checkin service.
76      */
close()77     public synchronized void close() {
78         if (mCallback == null) {
79             return;
80         }
81         mCallback = null;
82 
83         if (mReceiverRegistered) {
84             mContext.unregisterReceiver(mBroadcastReceiver);
85             mReceiverRegistered = false;
86         }
87     }
88 
89     public final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
90         @Override
91         public void onReceive(Context context, Intent intent) {
92             ProvisionLogger.logd("onReceive " + intent.toString());
93 
94             mNetworkConnected = isConnectedToWifi(context);
95 
96             if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION) ||
97                     intent.getAction().equals(ConnectivityManager.INET_CONDITION_ACTION)) {
98                 if (mNetworkConnected) {
99                     mCallback.onNetworkConnected();
100                 } else {
101                     mCallback.onNetworkDisconnected();
102                 }
103             }
104         }
105     };
106 
isConnectedToWifi(Context context)107     public static boolean isConnectedToWifi(Context context) {
108         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
109                 Context.CONNECTIVITY_SERVICE);
110         if (cm != null) {
111             NetworkInfo ni = cm.getActiveNetworkInfo();
112             if (ni != null) {
113                 DetailedState detailedState = ni.getDetailedState();
114                 return detailedState.equals(DetailedState.CONNECTED);
115             } else {
116                 return false;
117             }
118         } else {
119             return false;
120         }
121     }
122 
isNetworkConnected()123     public boolean isNetworkConnected() {
124         return mNetworkConnected;
125     }
126 }
127