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