Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 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.content.SharedPreferences; |
| 22 | import android.os.Bundle; |
| 23 | import android.preference.PreferenceManager; |
| 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"; |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame^] | 42 | static final String EXTRA_SEARCHBOX_SETQUERY = "searchbox_query"; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 43 | |
| 44 | @Override |
| 45 | public void onReceive(Context context, Intent intent) { |
| 46 | if (LOGD_ENABLED) Log.d(LOGTAG, "received intent " + intent); |
| 47 | if (BrowserSettings.getInstance().isPreloadEnabled() |
| 48 | && intent.getAction().equals(ACTION_PRELOAD)) { |
| 49 | handlePreload(context, intent); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private void handlePreload(Context context, Intent i) { |
| 54 | String url = UrlUtils.smartUrlFilter(i.getData()); |
| 55 | String id = i.getStringExtra(EXTRA_PRELOAD_ID); |
| 56 | Map<String, String> headers = null; |
| 57 | if (id == null) { |
| 58 | if (LOGD_ENABLED) Log.d(LOGTAG, "Preload request has no " + EXTRA_PRELOAD_ID); |
| 59 | return; |
| 60 | } |
| 61 | if (i.getBooleanExtra(EXTRA_PRELOAD_DISCARD, false)) { |
| 62 | if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " preload discard request"); |
| 63 | Preloader.getInstance().discardPreload(id); |
| 64 | } else { |
| 65 | if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " preload request for " + url); |
| 66 | if (url != null && url.startsWith("http")) { |
| 67 | final Bundle pairs = i.getBundleExtra(Browser.EXTRA_HEADERS); |
| 68 | if (pairs != null && !pairs.isEmpty()) { |
| 69 | Iterator<String> iter = pairs.keySet().iterator(); |
| 70 | headers = new HashMap<String, String>(); |
| 71 | while (iter.hasNext()) { |
| 72 | String key = iter.next(); |
| 73 | headers.put(key, pairs.getString(key)); |
| 74 | } |
| 75 | } |
| 76 | } |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame^] | 77 | String sbQuery = i.getStringExtra(EXTRA_SEARCHBOX_SETQUERY); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 78 | if (url != null) { |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame^] | 79 | if (LOGD_ENABLED){ |
| 80 | Log.d(LOGTAG, "Preload request(" + id + ", " + url + ", " + |
| 81 | headers + ", " + sbQuery + ")"); |
| 82 | } |
| 83 | Preloader.getInstance().handlePreloadRequest(id, url, headers, sbQuery); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | } |