blob: 51761769b9484384de0dd17cb5f19a454502049c [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 Inwood29721c22011-06-29 17:55:24 +010042 static final String EXTRA_SEARCHBOX_SETQUERY = "searchbox_query";
Michael Kolb14612442011-06-24 13:06:29 -070043
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 Inwood29721c22011-06-29 17:55:24 +010077 String sbQuery = i.getStringExtra(EXTRA_SEARCHBOX_SETQUERY);
Michael Kolb14612442011-06-24 13:06:29 -070078 if (url != null) {
Mathew Inwood29721c22011-06-29 17:55:24 +010079 if (LOGD_ENABLED){
80 Log.d(LOGTAG, "Preload request(" + id + ", " + url + ", " +
81 headers + ", " + sbQuery + ")");
82 }
83 Preloader.getInstance().handlePreloadRequest(id, url, headers, sbQuery);
Michael Kolb14612442011-06-24 13:06:29 -070084 }
85 }
86 }
87
88}