1 /* 2 * Copyright (C) 2011 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 package com.android.browser; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.net.ConnectivityManager; 22 import android.net.NetworkInfo; 23 import android.os.Bundle; 24 import android.provider.Browser; 25 import android.util.Log; 26 27 import java.util.HashMap; 28 import java.util.Iterator; 29 import java.util.Map; 30 31 /** 32 * Broadcast receiver for receiving browser preload requests 33 */ 34 public class PreloadRequestReceiver extends BroadcastReceiver { 35 36 private final static String LOGTAG = "browser.preloader"; 37 private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED; 38 39 private static final String ACTION_PRELOAD = "android.intent.action.PRELOAD"; 40 static final String EXTRA_PRELOAD_ID = "preload_id"; 41 static final String EXTRA_PRELOAD_DISCARD = "preload_discard"; 42 static final String EXTRA_SEARCHBOX_CANCEL = "searchbox_cancel"; 43 static final String EXTRA_SEARCHBOX_SETQUERY = "searchbox_query"; 44 45 private ConnectivityManager mConnectivityManager; 46 47 @Override 48 public void onReceive(Context context, Intent intent) { 49 if (LOGD_ENABLED) Log.d(LOGTAG, "received intent " + intent); 50 if (isPreloadEnabledOnCurrentNetwork(context) && 51 intent.getAction().equals(ACTION_PRELOAD)) { 52 handlePreload(context, intent); 53 } 54 } 55 56 private boolean isPreloadEnabledOnCurrentNetwork(Context context) { 57 String preload = BrowserSettings.getInstance().getPreloadEnabled(); 58 if (LOGD_ENABLED) Log.d(LOGTAG, "Preload setting: " + preload); 59 if (BrowserSettings.getPreloadAlwaysPreferenceString(context).equals(preload)) { 60 return true; 61 } else if (BrowserSettings.getPreloadOnWifiOnlyPreferenceString(context).equals(preload)) { 62 boolean onWifi = isOnWifi(context); 63 if (LOGD_ENABLED) Log.d(LOGTAG, "on wifi:" + onWifi); 64 return onWifi; 65 } else { 66 return false; 67 } 68 } 69 70 private boolean isOnWifi(Context context) { 71 if (mConnectivityManager == null) { 72 mConnectivityManager = (ConnectivityManager) 73 context.getSystemService(Context.CONNECTIVITY_SERVICE); 74 } 75 NetworkInfo ni = mConnectivityManager.getActiveNetworkInfo(); 76 if (ni == null) { 77 return false; 78 } 79 switch (ni.getType()) { 80 case ConnectivityManager.TYPE_MOBILE: 81 case ConnectivityManager.TYPE_MOBILE_DUN: 82 case ConnectivityManager.TYPE_MOBILE_MMS: 83 case ConnectivityManager.TYPE_MOBILE_SUPL: 84 case ConnectivityManager.TYPE_MOBILE_HIPRI: 85 case ConnectivityManager.TYPE_WIMAX: // separate case for this? 86 return false; 87 case ConnectivityManager.TYPE_WIFI: 88 case ConnectivityManager.TYPE_ETHERNET: 89 case ConnectivityManager.TYPE_BLUETOOTH: 90 return true; 91 default: 92 return false; 93 } 94 } 95 96 private void handlePreload(Context context, Intent i) { 97 String url = UrlUtils.smartUrlFilter(i.getData()); 98 String id = i.getStringExtra(EXTRA_PRELOAD_ID); 99 Map<String, String> headers = null; 100 if (id == null) { 101 if (LOGD_ENABLED) Log.d(LOGTAG, "Preload request has no " + EXTRA_PRELOAD_ID); 102 return; 103 } 104 if (i.getBooleanExtra(EXTRA_PRELOAD_DISCARD, false)) { 105 if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " preload discard request"); 106 Preloader.getInstance().discardPreload(id); 107 } else if (i.getBooleanExtra(EXTRA_SEARCHBOX_CANCEL, false)) { 108 if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " searchbox cancel request"); 109 Preloader.getInstance().cancelSearchBoxPreload(id); 110 } else { 111 if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " preload request for " + url); 112 if (url != null && url.startsWith("http")) { 113 final Bundle pairs = i.getBundleExtra(Browser.EXTRA_HEADERS); 114 if (pairs != null && !pairs.isEmpty()) { 115 Iterator<String> iter = pairs.keySet().iterator(); 116 headers = new HashMap<String, String>(); 117 while (iter.hasNext()) { 118 String key = iter.next(); 119 headers.put(key, pairs.getString(key)); 120 } 121 } 122 } 123 String sbQuery = i.getStringExtra(EXTRA_SEARCHBOX_SETQUERY); 124 if (url != null) { 125 if (LOGD_ENABLED){ 126 Log.d(LOGTAG, "Preload request(" + id + ", " + url + ", " + 127 headers + ", " + sbQuery + ")"); 128 } 129 Preloader.getInstance().handlePreloadRequest(id, url, headers, sbQuery); 130 } 131 } 132 } 133 134 } 135