1 /* 2 * Copyright (C) 2010 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.browser; 18 19 import android.app.Activity; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.net.ConnectivityManager; 25 import android.net.NetworkInfo; 26 import android.webkit.WebView; 27 28 /** 29 * Handle network state changes 30 */ 31 public class NetworkStateHandler { 32 33 Activity mActivity; 34 Controller mController; 35 36 // monitor platform changes 37 private IntentFilter mNetworkStateChangedFilter; 38 private BroadcastReceiver mNetworkStateIntentReceiver; 39 private boolean mIsNetworkUp; 40 41 public NetworkStateHandler(Activity activity, Controller controller) { 42 mActivity = activity; 43 mController = controller; 44 // Find out if the network is currently up. 45 ConnectivityManager cm = (ConnectivityManager) mActivity 46 .getSystemService(Context.CONNECTIVITY_SERVICE); 47 NetworkInfo info = cm.getActiveNetworkInfo(); 48 if (info != null) { 49 mIsNetworkUp = info.isAvailable(); 50 } 51 52 /* 53 * enables registration for changes in network status from http stack 54 */ 55 mNetworkStateChangedFilter = new IntentFilter(); 56 mNetworkStateChangedFilter.addAction( 57 ConnectivityManager.CONNECTIVITY_ACTION); 58 mNetworkStateIntentReceiver = new BroadcastReceiver() { 59 @Override 60 public void onReceive(Context context, Intent intent) { 61 if (intent.getAction().equals( 62 ConnectivityManager.CONNECTIVITY_ACTION)) { 63 64 NetworkInfo info = intent.getParcelableExtra( 65 ConnectivityManager.EXTRA_NETWORK_INFO); 66 String typeName = info.getTypeName(); 67 String subtypeName = info.getSubtypeName(); 68 sendNetworkType(typeName.toLowerCase(), 69 (subtypeName != null ? subtypeName.toLowerCase() : "")); 70 71 boolean noConnection = intent.getBooleanExtra( 72 ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); 73 74 onNetworkToggle(!noConnection); 75 } 76 } 77 }; 78 79 } 80 81 void onPause() { 82 // unregister network state listener 83 mActivity.unregisterReceiver(mNetworkStateIntentReceiver); 84 } 85 86 void onResume() { 87 mActivity.registerReceiver(mNetworkStateIntentReceiver, 88 mNetworkStateChangedFilter); 89 } 90 91 /** 92 * connectivity manager says net has come or gone... inform the user 93 * @param up true if net has come up, false if net has gone down 94 */ 95 void onNetworkToggle(boolean up) { 96 if (up == mIsNetworkUp) { 97 return; 98 } 99 mIsNetworkUp = up; 100 WebView w = mController.getCurrentWebView(); 101 if (w != null) { 102 w.setNetworkAvailable(up); 103 } 104 } 105 106 boolean isNetworkUp() { 107 return mIsNetworkUp; 108 } 109 110 private void sendNetworkType(String type, String subtype) { 111 WebView w = mController.getCurrentWebView(); 112 if (w != null) { 113 w.setNetworkType(type, subtype); 114 } 115 } 116 117 } 118