blob: c86d660b7d3205bf6dea324e890383a9450ceda7 [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";
42
43 @Override
44 public void onReceive(Context context, Intent intent) {
45 if (LOGD_ENABLED) Log.d(LOGTAG, "received intent " + intent);
46 if (BrowserSettings.getInstance().isPreloadEnabled()
47 && intent.getAction().equals(ACTION_PRELOAD)) {
48 handlePreload(context, intent);
49 }
50 }
51
52 private void handlePreload(Context context, Intent i) {
53 String url = UrlUtils.smartUrlFilter(i.getData());
54 String id = i.getStringExtra(EXTRA_PRELOAD_ID);
55 Map<String, String> headers = null;
56 if (id == null) {
57 if (LOGD_ENABLED) Log.d(LOGTAG, "Preload request has no " + EXTRA_PRELOAD_ID);
58 return;
59 }
60 if (i.getBooleanExtra(EXTRA_PRELOAD_DISCARD, false)) {
61 if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " preload discard request");
62 Preloader.getInstance().discardPreload(id);
63 } else {
64 if (LOGD_ENABLED) Log.d(LOGTAG, "Got " + id + " preload request for " + url);
65 if (url != null && url.startsWith("http")) {
66 final Bundle pairs = i.getBundleExtra(Browser.EXTRA_HEADERS);
67 if (pairs != null && !pairs.isEmpty()) {
68 Iterator<String> iter = pairs.keySet().iterator();
69 headers = new HashMap<String, String>();
70 while (iter.hasNext()) {
71 String key = iter.next();
72 headers.put(key, pairs.getString(key));
73 }
74 }
75 }
76 if (url != null) {
77 Preloader.getInstance().handlePreloadRequest(id, url, headers);
78 }
79 }
80 }
81
82}