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.Context; |
| 19 | import android.os.Handler; |
| 20 | import android.os.Looper; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.util.Log; |
| 23 | import android.webkit.WebView; |
| 24 | |
| 25 | import java.util.HashMap; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | /** |
| 29 | * Singleton class for handling preload requests. |
| 30 | */ |
| 31 | public class Preloader { |
| 32 | |
| 33 | private final static String LOGTAG = "browser.preloader"; |
| 34 | private final static boolean LOGD_ENABLED = true;//com.android.browser.Browser.LOGD_ENABLED; |
| 35 | |
| 36 | private static final int PRERENDER_TIMEOUT_MILLIS = 30 * 1000; // 30s |
| 37 | |
| 38 | private static Preloader sInstance; |
| 39 | |
| 40 | private final Context mContext; |
| 41 | private final Handler mHandler; |
| 42 | private final BrowserWebViewFactory mFactory; |
| 43 | private final HashMap<String, PreloaderSession> mSessions; |
| 44 | |
| 45 | public static void initialize(Context context) { |
| 46 | sInstance = new Preloader(context); |
| 47 | } |
| 48 | |
| 49 | public static Preloader getInstance() { |
| 50 | return sInstance; |
| 51 | } |
| 52 | |
| 53 | private Preloader(Context context) { |
| 54 | mContext = context; |
| 55 | mHandler = new Handler(Looper.getMainLooper()); |
| 56 | mSessions = new HashMap<String, PreloaderSession>(); |
| 57 | mFactory = new BrowserWebViewFactory(context); |
| 58 | |
| 59 | } |
| 60 | |
| 61 | private PreloaderSession getSession(String id) { |
| 62 | PreloaderSession s = mSessions.get(id); |
| 63 | if (s == null) { |
| 64 | if (LOGD_ENABLED) Log.d(LOGTAG, "Create new preload session " + id); |
| 65 | s = new PreloaderSession(id); |
| 66 | mSessions.put(id, s); |
| 67 | } |
| 68 | return s; |
| 69 | } |
| 70 | |
| 71 | private PreloaderSession takeSession(String id) { |
| 72 | PreloaderSession s = mSessions.remove(id); |
| 73 | if (s != null) { |
| 74 | s.cancelTimeout(); |
| 75 | } |
| 76 | return s; |
| 77 | } |
| 78 | |
| 79 | public void handlePreloadRequest(String id, String url, Map<String, String> headers) { |
| 80 | PreloaderSession s = getSession(id); |
| 81 | s.touch(); // reset timer |
| 82 | if (LOGD_ENABLED) Log.d(LOGTAG, "Preloading " + url); |
| 83 | s.getTab().loadUrl(url, headers); |
| 84 | } |
| 85 | |
| 86 | public void discardPreload(String id) { |
| 87 | PreloaderSession s = takeSession(id); |
| 88 | if (s != null) { |
| 89 | if (LOGD_ENABLED) Log.d(LOGTAG, "Discard preload session " + id); |
| 90 | Tab t = s.getTab(); |
| 91 | t.destroy(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Return a preloaded tab, and remove it from the preloader. This is used when the |
| 97 | * view is about to be displayed. |
| 98 | */ |
| 99 | public Tab getPreloadedTab(String id) { |
| 100 | PreloaderSession s = takeSession(id); |
| 101 | if (LOGD_ENABLED) Log.d(LOGTAG, "Showing preload session " + id + "=" + s); |
| 102 | return s == null ? null : s.getTab(); |
| 103 | } |
| 104 | |
| 105 | private class PreloaderSession { |
| 106 | private final String mId; |
| 107 | private final Tab mTab; |
| 108 | |
| 109 | private final Runnable mTimeoutTask = new Runnable(){ |
| 110 | @Override |
| 111 | public void run() { |
| 112 | if (LOGD_ENABLED) Log.d(LOGTAG, "Preload session timeout " + mId); |
| 113 | discardPreload(mId); |
| 114 | }}; |
| 115 | |
| 116 | public PreloaderSession(String id) { |
| 117 | mId = id; |
| 118 | mTab = new Tab(new PreloadController(mContext), mFactory.createWebView(false)); |
| 119 | touch(); |
| 120 | } |
| 121 | |
| 122 | public void cancelTimeout() { |
| 123 | mHandler.removeCallbacks(mTimeoutTask); |
| 124 | } |
| 125 | |
| 126 | public void touch() { |
| 127 | cancelTimeout(); |
| 128 | mHandler.postDelayed(mTimeoutTask, PRERENDER_TIMEOUT_MILLIS); |
| 129 | } |
| 130 | |
| 131 | public Tab getTab() { |
| 132 | return mTab; |
| 133 | } |
| 134 | |
| 135 | } |
| 136 | |
| 137 | } |