blob: 936ef9c9297a186926a15353aed3aa12aedf2ca0 [file] [log] [blame]
John Reck0ebd3ac2010-12-09 11:14:04 -08001/*
2 * Copyright (C) 2010 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
17
Bijan Amirzada41242f22014-03-21 12:12:18 -070018package com.android.browser;
John Reck0ebd3ac2010-12-09 11:14:04 -080019
20import android.content.ContentResolver;
21import android.content.ContentUris;
22import android.content.ContentValues;
23import android.content.Context;
24import android.database.Cursor;
John Recke969cc52010-12-21 17:24:43 -080025import android.database.sqlite.SQLiteException;
John Reck4eadc342011-10-31 14:04:10 -070026import android.graphics.Bitmap;
27import android.net.Uri;
John Reck0ebd3ac2010-12-09 11:14:04 -080028import android.os.Handler;
John Reck0ebd3ac2010-12-09 11:14:04 -080029import android.os.Message;
John Recke969cc52010-12-21 17:24:43 -080030import android.util.Log;
31
Bijan Amirzada3f04dc72014-06-25 11:48:36 -070032import com.android.browser.platformsupport.Browser;
Bijan Amirzada41242f22014-03-21 12:12:18 -070033import com.android.browser.platformsupport.BrowserContract;
34import com.android.browser.platformsupport.BrowserContract.History;
35import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck4eadc342011-10-31 14:04:10 -070036
37import java.nio.ByteBuffer;
John Recke969cc52010-12-21 17:24:43 -080038import java.util.concurrent.BlockingQueue;
39import java.util.concurrent.LinkedBlockingQueue;
John Reck0ebd3ac2010-12-09 11:14:04 -080040
41public class DataController {
John Recke969cc52010-12-21 17:24:43 -080042 private static final String LOGTAG = "DataController";
John Reck0ebd3ac2010-12-09 11:14:04 -080043 // Message IDs
44 private static final int HISTORY_UPDATE_VISITED = 100;
45 private static final int HISTORY_UPDATE_TITLE = 101;
John Reck4eadc342011-10-31 14:04:10 -070046 private static final int QUERY_URL_IS_BOOKMARK = 200;
47 private static final int TAB_LOAD_THUMBNAIL = 201;
48 private static final int TAB_SAVE_THUMBNAIL = 202;
49 private static final int TAB_DELETE_THUMBNAIL = 203;
John Reck0ebd3ac2010-12-09 11:14:04 -080050 private static DataController sInstance;
51
52 private Context mContext;
John Recke969cc52010-12-21 17:24:43 -080053 private DataControllerHandler mDataHandler;
54 private Handler mCbHandler; // To respond on the UI thread
John Reck4eadc342011-10-31 14:04:10 -070055 private ByteBuffer mBuffer; // to capture thumbnails
John Recke969cc52010-12-21 17:24:43 -080056
57 /* package */ static interface OnQueryUrlIsBookmark {
58 void onQueryUrlIsBookmark(String url, boolean isBookmark);
59 }
60 private static class CallbackContainer {
61 Object replyTo;
62 Object[] args;
63 }
64
65 private static class DCMessage {
66 int what;
67 Object obj;
68 Object replyTo;
69 DCMessage(int w, Object o) {
70 what = w;
71 obj = o;
72 }
73 }
John Reck0ebd3ac2010-12-09 11:14:04 -080074
75 /* package */ static DataController getInstance(Context c) {
76 if (sInstance == null) {
77 sInstance = new DataController(c);
78 }
79 return sInstance;
80 }
81
82 private DataController(Context c) {
83 mContext = c.getApplicationContext();
John Recke969cc52010-12-21 17:24:43 -080084 mDataHandler = new DataControllerHandler();
John Recke969cc52010-12-21 17:24:43 -080085 mDataHandler.start();
86 mCbHandler = new Handler() {
87 @Override
88 public void handleMessage(Message msg) {
89 CallbackContainer cc = (CallbackContainer) msg.obj;
90 switch (msg.what) {
91 case QUERY_URL_IS_BOOKMARK: {
92 OnQueryUrlIsBookmark cb = (OnQueryUrlIsBookmark) cc.replyTo;
93 String url = (String) cc.args[0];
94 boolean isBookmark = (Boolean) cc.args[1];
95 cb.onQueryUrlIsBookmark(url, isBookmark);
96 break;
97 }
98 }
99 }
100 };
John Reck0ebd3ac2010-12-09 11:14:04 -0800101 }
102
103 public void updateVisitedHistory(String url) {
John Recke969cc52010-12-21 17:24:43 -0800104 mDataHandler.sendMessage(HISTORY_UPDATE_VISITED, url);
John Reck0ebd3ac2010-12-09 11:14:04 -0800105 }
106
107 public void updateHistoryTitle(String url, String title) {
John Recke969cc52010-12-21 17:24:43 -0800108 mDataHandler.sendMessage(HISTORY_UPDATE_TITLE, new String[] { url, title });
John Reck0ebd3ac2010-12-09 11:14:04 -0800109 }
110
John Recke969cc52010-12-21 17:24:43 -0800111 public void queryBookmarkStatus(String url, OnQueryUrlIsBookmark replyTo) {
John Reck6c702ee2011-01-07 09:41:53 -0800112 if (url == null || url.trim().length() == 0) {
113 // null or empty url is never a bookmark
114 replyTo.onQueryUrlIsBookmark(url, false);
115 return;
116 }
117 mDataHandler.sendMessage(QUERY_URL_IS_BOOKMARK, url.trim(), replyTo);
John Recke969cc52010-12-21 17:24:43 -0800118 }
119
John Reck4eadc342011-10-31 14:04:10 -0700120 public void loadThumbnail(Tab tab) {
121 mDataHandler.sendMessage(TAB_LOAD_THUMBNAIL, tab);
122 }
123
124 public void deleteThumbnail(Tab tab) {
125 mDataHandler.sendMessage(TAB_DELETE_THUMBNAIL, tab.getId());
126 }
127
128 public void saveThumbnail(Tab tab) {
129 mDataHandler.sendMessage(TAB_SAVE_THUMBNAIL, tab);
130 }
131
John Recke969cc52010-12-21 17:24:43 -0800132 // The standard Handler and Message classes don't allow the queue manipulation
133 // we want (such as peeking). So we use our own queue.
134 class DataControllerHandler extends Thread {
135 private BlockingQueue<DCMessage> mMessageQueue
136 = new LinkedBlockingQueue<DCMessage>();
John Reck0ebd3ac2010-12-09 11:14:04 -0800137
John Reck4eadc342011-10-31 14:04:10 -0700138 public DataControllerHandler() {
139 super("DataControllerHandler");
140 }
141
John Reck0ebd3ac2010-12-09 11:14:04 -0800142 @Override
John Recke969cc52010-12-21 17:24:43 -0800143 public void run() {
John Reck4eadc342011-10-31 14:04:10 -0700144 setPriority(Thread.MIN_PRIORITY);
John Recke969cc52010-12-21 17:24:43 -0800145 while (true) {
146 try {
147 handleMessage(mMessageQueue.take());
148 } catch (InterruptedException ex) {
149 break;
150 }
151 }
152 }
153
154 void sendMessage(int what, Object obj) {
155 DCMessage m = new DCMessage(what, obj);
156 mMessageQueue.add(m);
157 }
158
159 void sendMessage(int what, Object obj, Object replyTo) {
160 DCMessage m = new DCMessage(what, obj);
161 m.replyTo = replyTo;
162 mMessageQueue.add(m);
163 }
164
165 private void handleMessage(DCMessage msg) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800166 switch (msg.what) {
167 case HISTORY_UPDATE_VISITED:
168 doUpdateVisitedHistory((String) msg.obj);
169 break;
170 case HISTORY_UPDATE_TITLE:
171 String[] args = (String[]) msg.obj;
172 doUpdateHistoryTitle(args[0], args[1]);
173 break;
John Recke969cc52010-12-21 17:24:43 -0800174 case QUERY_URL_IS_BOOKMARK:
175 // TODO: Look for identical messages in the queue and remove them
176 // TODO: Also, look for partial matches and merge them (such as
177 // multiple callbacks querying the same URL)
178 doQueryBookmarkStatus((String) msg.obj, msg.replyTo);
179 break;
John Reck4eadc342011-10-31 14:04:10 -0700180 case TAB_LOAD_THUMBNAIL:
181 doLoadThumbnail((Tab) msg.obj);
182 break;
183 case TAB_DELETE_THUMBNAIL:
184 ContentResolver cr = mContext.getContentResolver();
185 try {
186 cr.delete(ContentUris.withAppendedId(
187 Thumbnails.CONTENT_URI, (Long)msg.obj),
188 null, null);
189 } catch (Throwable t) {}
190 break;
191 case TAB_SAVE_THUMBNAIL:
192 doSaveThumbnail((Tab)msg.obj);
193 break;
194 }
195 }
196
197 private byte[] getCaptureBlob(Tab tab) {
198 synchronized (tab) {
199 Bitmap capture = tab.getScreenshot();
200 if (capture == null) {
201 return null;
202 }
203 if (mBuffer == null || mBuffer.limit() < capture.getByteCount()) {
204 mBuffer = ByteBuffer.allocate(capture.getByteCount());
205 }
206 capture.copyPixelsToBuffer(mBuffer);
207 mBuffer.rewind();
208 return mBuffer.array();
209 }
210 }
211
212 private void doSaveThumbnail(Tab tab) {
213 byte[] blob = getCaptureBlob(tab);
214 if (blob == null) {
215 return;
216 }
217 ContentResolver cr = mContext.getContentResolver();
218 ContentValues values = new ContentValues();
219 values.put(Thumbnails._ID, tab.getId());
220 values.put(Thumbnails.THUMBNAIL, blob);
221 cr.insert(Thumbnails.CONTENT_URI, values);
222 }
223
224 private void doLoadThumbnail(Tab tab) {
225 ContentResolver cr = mContext.getContentResolver();
226 Cursor c = null;
227 try {
228 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, tab.getId());
229 c = cr.query(uri, new String[] {Thumbnails._ID,
230 Thumbnails.THUMBNAIL}, null, null, null);
231 if (c.moveToFirst()) {
232 byte[] data = c.getBlob(1);
233 if (data != null && data.length > 0) {
234 tab.updateCaptureFromBlob(data);
235 }
236 }
237 } finally {
238 if (c != null) {
239 c.close();
240 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800241 }
242 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800243
John Recke969cc52010-12-21 17:24:43 -0800244 private void doUpdateVisitedHistory(String url) {
245 ContentResolver cr = mContext.getContentResolver();
246 Cursor c = null;
247 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800248 c = cr.query(History.CONTENT_URI, new String[] {History._ID, History.VISITS},
John Recke969cc52010-12-21 17:24:43 -0800249 History.URL + "=?", new String[] { url }, null);
250 if (c.moveToFirst()) {
251 ContentValues values = new ContentValues();
252 values.put(History.VISITS, c.getInt(1) + 1);
253 values.put(History.DATE_LAST_VISITED, System.currentTimeMillis());
254 cr.update(ContentUris.withAppendedId(History.CONTENT_URI, c.getLong(0)),
255 values, null, null);
256 } else {
Bijan Amirzada3f04dc72014-06-25 11:48:36 -0700257 Browser.truncateHistory(cr);
John Recke969cc52010-12-21 17:24:43 -0800258 ContentValues values = new ContentValues();
259 values.put(History.URL, url);
260 values.put(History.VISITS, 1);
261 values.put(History.DATE_LAST_VISITED, System.currentTimeMillis());
262 values.put(History.TITLE, url);
263 values.put(History.DATE_CREATED, 0);
264 values.put(History.USER_ENTERED, 0);
265 cr.insert(History.CONTENT_URI, values);
266 }
267 } finally {
268 if (c != null) c.close();
John Reck0ebd3ac2010-12-09 11:14:04 -0800269 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800270 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800271
John Recke969cc52010-12-21 17:24:43 -0800272 private void doQueryBookmarkStatus(String url, Object replyTo) {
John Recke969cc52010-12-21 17:24:43 -0800273 // Check to see if the site is bookmarked
274 Cursor cursor = null;
275 boolean isBookmark = false;
276 try {
277 cursor = mContext.getContentResolver().query(
278 BookmarkUtils.getBookmarksUri(mContext),
279 new String[] { BrowserContract.Bookmarks.URL },
280 BrowserContract.Bookmarks.URL + " == ?",
281 new String[] { url },
282 null);
283 isBookmark = cursor.moveToFirst();
284 } catch (SQLiteException e) {
285 Log.e(LOGTAG, "Error checking for bookmark: " + e);
286 } finally {
287 if (cursor != null) cursor.close();
288 }
289 CallbackContainer cc = new CallbackContainer();
290 cc.replyTo = replyTo;
291 cc.args = new Object[] { url, isBookmark };
292 mCbHandler.obtainMessage(QUERY_URL_IS_BOOKMARK, cc).sendToTarget();
293 }
294
295 private void doUpdateHistoryTitle(String url, String title) {
296 ContentResolver cr = mContext.getContentResolver();
297 ContentValues values = new ContentValues();
298 values.put(History.TITLE, title);
299 cr.update(History.CONTENT_URI, values, History.URL + "=?",
300 new String[] { url });
301 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800302 }
303}