blob: ff3c8e3241ac7326289e7bab18fbdc4f51c868ad [file] [log] [blame]
Michael Kolb14612442011-06-24 13:06:29 -07001/*
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 */
16package com.android.browser;
17
18import android.content.BroadcastReceiver;
19import android.content.Context;
20import android.content.Intent;
21import android.content.SharedPreferences;
22import android.os.Bundle;
23import android.preference.PreferenceManager;
24import android.provider.Browser;
25import android.util.Log;
26
27import java.util.HashMap;
28import java.util.Iterator;
29import java.util.Map;
30
31/**
32 * Broadcast receiver for receiving browser preload requests
33 */
34public 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 Inwooddffa72f2011-08-10 12:37:43 +010042 static final String EXTRA_SEARCHBOX_CANCEL = "searchbox_cancel";
Mathew Inwood29721c22011-06-29 17:55:24 +010043 static final String EXTRA_SEARCHBOX_SETQUERY = "searchbox_query";
Michael Kolb14612442011-06-24 13:06:29 -070044
45 @Override
46 public void onReceive(Context context, Intent intent) {
47 if (LOGD_ENABLED) Log.d(LOGTAG, "received intent " + intent);
48 if (BrowserSettings.getInstance().isPreloadEnabled()
49 && intent.getAction().equals(ACTION_PRELOAD)) {
50 handlePreload(context, intent);
51 }
52 }
53
54 private void handlePreload(Context context, Intent i) {
55 String url = UrlUtils.smartUrlFilter(i.getData());
56 String id = i.getStringExtra(EXTRA_PRELOAD_ID);
57 Map<String, String> headers = null;
58 if (id == null) {
59 if (LOGD_ENABLED) Log.d(LOGTAG, "Preload request has no " + EXTRA_PRELOAD_ID);
60 return;
61 }
62 if (i.getBooleanExtra(EXTRA_PRELOAD_DISCARD, false)) {
63 if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " preload discard request");
64 Preloader.getInstance().discardPreload(id);
Mathew Inwooddffa72f2011-08-10 12:37:43 +010065 } else if (i.getBooleanExtra(EXTRA_SEARCHBOX_CANCEL, false)) {
66 if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " searchbox cancel request");
67 Preloader.getInstance().cancelSearchBoxPreload(id);
Michael Kolb14612442011-06-24 13:06:29 -070068 } else {
69 if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " preload request for " + url);
70 if (url != null && url.startsWith("http")) {
71 final Bundle pairs = i.getBundleExtra(Browser.EXTRA_HEADERS);
72 if (pairs != null && !pairs.isEmpty()) {
73 Iterator<String> iter = pairs.keySet().iterator();
74 headers = new HashMap<String, String>();
75 while (iter.hasNext()) {
76 String key = iter.next();
77 headers.put(key, pairs.getString(key));
78 }
79 }
80 }
Mathew Inwood29721c22011-06-29 17:55:24 +010081 String sbQuery = i.getStringExtra(EXTRA_SEARCHBOX_SETQUERY);
Michael Kolb14612442011-06-24 13:06:29 -070082 if (url != null) {
Mathew Inwood29721c22011-06-29 17:55:24 +010083 if (LOGD_ENABLED){
84 Log.d(LOGTAG, "Preload request(" + id + ", " + url + ", " +
85 headers + ", " + sbQuery + ")");
86 }
87 Preloader.getInstance().handlePreloadRequest(id, url, headers, sbQuery);
Michael Kolb14612442011-06-24 13:06:29 -070088 }
89 }
90 }
91
92}