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