11461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb/*
21461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb * Copyright (C) 2011 The Android Open Source Project
31461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb *
41461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb * Licensed under the Apache License, Version 2.0 (the "License");
51461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb * you may not use this file except in compliance with the License.
61461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb * You may obtain a copy of the License at
71461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb *
81461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb *      http://www.apache.org/licenses/LICENSE-2.0
91461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb *
101461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb * Unless required by applicable law or agreed to in writing, software
111461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb * distributed under the License is distributed on an "AS IS" BASIS,
121461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb * See the License for the specific language governing permissions and
141461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb * limitations under the License.
151461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb */
161461244018a225006a8d4c203f9dfe294ffe94faMichael Kolbpackage com.android.browser;
171461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb
181461244018a225006a8d4c203f9dfe294ffe94faMichael Kolbimport android.content.BroadcastReceiver;
191461244018a225006a8d4c203f9dfe294ffe94faMichael Kolbimport android.content.Context;
201461244018a225006a8d4c203f9dfe294ffe94faMichael Kolbimport android.content.Intent;
21467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwoodimport android.net.ConnectivityManager;
22467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwoodimport android.net.NetworkInfo;
231461244018a225006a8d4c203f9dfe294ffe94faMichael Kolbimport android.os.Bundle;
241461244018a225006a8d4c203f9dfe294ffe94faMichael Kolbimport android.provider.Browser;
251461244018a225006a8d4c203f9dfe294ffe94faMichael Kolbimport android.util.Log;
261461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb
271461244018a225006a8d4c203f9dfe294ffe94faMichael Kolbimport java.util.HashMap;
281461244018a225006a8d4c203f9dfe294ffe94faMichael Kolbimport java.util.Iterator;
291461244018a225006a8d4c203f9dfe294ffe94faMichael Kolbimport java.util.Map;
301461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb
311461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb/**
321461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb * Broadcast receiver for receiving browser preload requests
331461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb */
341461244018a225006a8d4c203f9dfe294ffe94faMichael Kolbpublic class PreloadRequestReceiver extends BroadcastReceiver {
351461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb
361461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb    private final static String LOGTAG = "browser.preloader";
371461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb    private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
381461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb
391461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb    private static final String ACTION_PRELOAD = "android.intent.action.PRELOAD";
401461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb    static final String EXTRA_PRELOAD_ID = "preload_id";
411461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb    static final String EXTRA_PRELOAD_DISCARD = "preload_discard";
42dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood    static final String EXTRA_SEARCHBOX_CANCEL = "searchbox_cancel";
4329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood    static final String EXTRA_SEARCHBOX_SETQUERY = "searchbox_query";
441461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb
45467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood    private ConnectivityManager mConnectivityManager;
46467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood
471461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb    @Override
481461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb    public void onReceive(Context context, Intent intent) {
491461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb        if (LOGD_ENABLED) Log.d(LOGTAG, "received intent " + intent);
50467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        if (isPreloadEnabledOnCurrentNetwork(context) &&
51467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood                intent.getAction().equals(ACTION_PRELOAD)) {
521461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb            handlePreload(context, intent);
531461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb        }
541461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb    }
551461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb
56467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood    private boolean isPreloadEnabledOnCurrentNetwork(Context context) {
57467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        String preload = BrowserSettings.getInstance().getPreloadEnabled();
58467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        if (LOGD_ENABLED) Log.d(LOGTAG, "Preload setting: " + preload);
59467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        if (BrowserSettings.getPreloadAlwaysPreferenceString(context).equals(preload)) {
60467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            return true;
61467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        } else if (BrowserSettings.getPreloadOnWifiOnlyPreferenceString(context).equals(preload)) {
62467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            boolean onWifi = isOnWifi(context);
63467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            if (LOGD_ENABLED) Log.d(LOGTAG, "on wifi:" + onWifi);
64467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            return onWifi;
65467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        } else {
66467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            return false;
67467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        }
68467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood    }
69467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood
70467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood    private boolean isOnWifi(Context context) {
71467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        if (mConnectivityManager == null) {
72467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            mConnectivityManager = (ConnectivityManager)
73467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
74467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        }
75467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        NetworkInfo ni = mConnectivityManager.getActiveNetworkInfo();
76467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        if (ni == null) {
77467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            return false;
78467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        }
79467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        switch (ni.getType()) {
80467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            case ConnectivityManager.TYPE_MOBILE:
81467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            case ConnectivityManager.TYPE_MOBILE_DUN:
82467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            case ConnectivityManager.TYPE_MOBILE_MMS:
83467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            case ConnectivityManager.TYPE_MOBILE_SUPL:
84467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            case ConnectivityManager.TYPE_MOBILE_HIPRI:
85467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            case ConnectivityManager.TYPE_WIMAX: // separate case for this?
86467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood                return false;
87467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            case ConnectivityManager.TYPE_WIFI:
88467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            case ConnectivityManager.TYPE_ETHERNET:
89467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            case ConnectivityManager.TYPE_BLUETOOTH:
90467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood                return true;
91467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood            default:
92467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood                return false;
93467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood        }
94467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood    }
95467813fba07c4dcc4ce350a3d4576f5739c5c940Mathew Inwood
961461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb    private void handlePreload(Context context, Intent i) {
971461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb        String url = UrlUtils.smartUrlFilter(i.getData());
981461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb        String id = i.getStringExtra(EXTRA_PRELOAD_ID);
991461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb        Map<String, String> headers = null;
1001461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb        if (id == null) {
1011461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb            if (LOGD_ENABLED) Log.d(LOGTAG, "Preload request has no " + EXTRA_PRELOAD_ID);
1021461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb            return;
1031461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb        }
1041461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb        if (i.getBooleanExtra(EXTRA_PRELOAD_DISCARD, false)) {
1051461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb            if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " preload discard request");
1061461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb            Preloader.getInstance().discardPreload(id);
107dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood        } else if (i.getBooleanExtra(EXTRA_SEARCHBOX_CANCEL, false)) {
108dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood            if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " searchbox cancel request");
109dffa72f926d051c089981adf52a44d2c933a0de4Mathew Inwood            Preloader.getInstance().cancelSearchBoxPreload(id);
1101461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb        } else {
1111461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb            if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " preload request for " + url);
1121461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb            if (url != null && url.startsWith("http")) {
1131461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb                final Bundle pairs = i.getBundleExtra(Browser.EXTRA_HEADERS);
1141461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb                if (pairs != null && !pairs.isEmpty()) {
1151461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb                    Iterator<String> iter = pairs.keySet().iterator();
1161461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb                    headers = new HashMap<String, String>();
1171461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb                    while (iter.hasNext()) {
1181461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb                        String key = iter.next();
1191461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb                        headers.put(key, pairs.getString(key));
1201461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb                    }
1211461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb                }
1221461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb            }
12329721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood            String sbQuery = i.getStringExtra(EXTRA_SEARCHBOX_SETQUERY);
1241461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb            if (url != null) {
12529721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood                if (LOGD_ENABLED){
12629721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood                    Log.d(LOGTAG, "Preload request(" + id + ", " + url + ", " +
12729721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood                            headers + ", " + sbQuery + ")");
12829721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood                }
12929721c2c6cc7f79a52962556c4431b71bb3ce46eMathew Inwood                Preloader.getInstance().handlePreloadRequest(id, url, headers, sbQuery);
1301461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb            }
1311461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb        }
1321461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb    }
1331461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb
1341461244018a225006a8d4c203f9dfe294ffe94faMichael Kolb}
135