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