blob: 3b76e757c468458046fbeac43a137b44d7311f1b [file] [log] [blame]
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2006 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
17package com.android.browser;
18
19import android.content.ContentResolver;
20import android.content.ContentUris;
21import android.content.ContentValues;
22import android.database.ContentObserver;
23import android.database.Cursor;
24import android.database.DataSetObserver;
25import android.graphics.Bitmap;
26import android.graphics.BitmapFactory;
27import android.net.Uri;
28import android.os.Bundle;
29import android.os.Handler;
30import android.provider.Browser;
31import android.provider.Browser.BookmarkColumns;
32import android.view.KeyEvent;
33import android.view.View;
34import android.view.ViewGroup;
35import android.webkit.WebIconDatabase;
36import android.webkit.WebIconDatabase.IconListener;
37import android.widget.BaseAdapter;
38
39import java.io.ByteArrayOutputStream;
40
41class BrowserBookmarksAdapter extends BaseAdapter {
42
43 private final String LOGTAG = "Bookmarks";
44
45 private String mCurrentPage;
46 private Cursor mCursor;
47 private int mCount;
48 private String mLastWhereClause;
49 private String[] mLastSelectionArgs;
50 private String mLastOrderBy;
51 private BrowserBookmarksPage mBookmarksPage;
52 private ContentResolver mContentResolver;
53 private ChangeObserver mChangeObserver;
54 private DataSetObserver mDataSetObserver;
55 private boolean mDataValid;
56
57 // When true, this adapter is used to pick a bookmark to create a shortcut
58 private boolean mCreateShortcut;
59 private int mExtraOffset;
60
61 // Implementation of WebIconDatabase.IconListener
62 private class IconReceiver implements IconListener {
63 public void onReceivedIcon(String url, Bitmap icon) {
64 updateBookmarkFavicon(mContentResolver, url, icon);
65 }
66 }
67
68 // Instance of IconReceiver
69 private final IconReceiver mIconReceiver = new IconReceiver();
70
71 /**
72 * Create a new BrowserBookmarksAdapter.
73 * @param b BrowserBookmarksPage that instantiated this.
74 * Necessary so it will adjust its focus
75 * appropriately after a search.
76 */
77 public BrowserBookmarksAdapter(BrowserBookmarksPage b, String curPage) {
78 this(b, curPage, false);
79 }
80
81 /**
82 * Create a new BrowserBookmarksAdapter.
83 * @param b BrowserBookmarksPage that instantiated this.
84 * Necessary so it will adjust its focus
85 * appropriately after a search.
86 */
87 public BrowserBookmarksAdapter(BrowserBookmarksPage b, String curPage,
88 boolean createShortcut) {
89 mDataValid = false;
90 mCreateShortcut = createShortcut;
91 mExtraOffset = createShortcut ? 0 : 1;
92 mBookmarksPage = b;
93 mCurrentPage = b.getResources().getString(R.string.current_page) +
94 curPage;
95 mContentResolver = b.getContentResolver();
96 mLastOrderBy = Browser.BookmarkColumns.CREATED + " DESC";
97 mChangeObserver = new ChangeObserver();
98 mDataSetObserver = new MyDataSetObserver();
99 // FIXME: Should have a default sort order that the user selects.
100 search(null);
101 // FIXME: This requires another query of the database after the
102 // initial search(null). Can we optimize this?
103 Browser.requestAllIcons(mContentResolver,
104 Browser.BookmarkColumns.FAVICON + " is NULL AND " +
105 Browser.BookmarkColumns.BOOKMARK + " == 1", mIconReceiver);
106 }
107
108 /**
109 * Return a hashmap with one row's Title, Url, and favicon.
110 * @param position Position in the list.
111 * @return Bundle Stores title, url of row position, favicon, and id
112 * for the url. Return a blank map if position is out of
113 * range.
114 */
115 public Bundle getRow(int position) {
116 Bundle map = new Bundle();
117 if (position < mExtraOffset || position >= mCount) {
118 return map;
119 }
120 mCursor.moveToPosition(position- mExtraOffset);
121 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
122 map.putString(Browser.BookmarkColumns.TITLE,
123 mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
124 map.putString(Browser.BookmarkColumns.URL, url);
125 byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
126 if (data != null) {
127 map.putParcelable(Browser.BookmarkColumns.FAVICON,
128 BitmapFactory.decodeByteArray(data, 0, data.length));
129 }
130 map.putInt("id", mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX));
131 return map;
132 }
133
134 /**
135 * Update a row in the database with new information.
136 * Requeries the database if the information has changed.
137 * @param map Bundle storing id, title and url of new information
138 */
139 public void updateRow(Bundle map) {
140
141 // Find the record
142 int id = map.getInt("id");
143 int position = -1;
144 for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
145 if (mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX) == id) {
146 position = mCursor.getPosition();
147 break;
148 }
149 }
150 if (position < 0) {
151 return;
152 }
153
154 mCursor.moveToPosition(position);
155 ContentValues values = new ContentValues();
156 String title = map.getString(Browser.BookmarkColumns.TITLE);
157 if (!title.equals(mCursor
158 .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX))) {
159 values.put(Browser.BookmarkColumns.TITLE, title);
160 }
161 String url = map.getString(Browser.BookmarkColumns.URL);
162 if (!url.equals(mCursor.
163 getString(Browser.HISTORY_PROJECTION_URL_INDEX))) {
164 values.put(Browser.BookmarkColumns.URL, url);
165 }
166 if (values.size() > 0
167 && mContentResolver.update(Browser.BOOKMARKS_URI, values,
168 "_id = " + id, null) != -1) {
169 refreshList();
170 }
171 }
172
173 /**
174 * Delete a row from the database. Requeries the database.
175 * Does nothing if the provided position is out of range.
176 * @param position Position in the list.
177 */
178 public void deleteRow(int position) {
179 if (position < mExtraOffset || position >= getCount()) {
180 return;
181 }
182 mCursor.moveToPosition(position- mExtraOffset);
183 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
184 WebIconDatabase.getInstance().releaseIconForPageUrl(url);
185 Uri uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI, mCursor
186 .getInt(Browser.HISTORY_PROJECTION_ID_INDEX));
187 int numVisits = mCursor.getInt(Browser.HISTORY_PROJECTION_VISITS_INDEX);
188 if (0 == numVisits) {
189 mContentResolver.delete(uri, null, null);
190 } else {
191 // It is no longer a bookmark, but it is still a visited site.
192 ContentValues values = new ContentValues();
193 values.put(Browser.BookmarkColumns.BOOKMARK, 0);
194 mContentResolver.update(uri, values, null, null);
195 }
196 refreshList();
197 }
198
199 /**
200 * Delete all bookmarks from the db. Requeries the database.
201 * All bookmarks with become visited URLs or if never visited
202 * are removed
203 */
204 public void deleteAllRows() {
205 StringBuilder deleteIds = null;
206 StringBuilder convertIds = null;
207
208 for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
209 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
210 WebIconDatabase.getInstance().releaseIconForPageUrl(url);
211 int id = mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX);
212 int numVisits = mCursor.getInt(Browser.HISTORY_PROJECTION_VISITS_INDEX);
213 if (0 == numVisits) {
214 if (deleteIds == null) {
215 deleteIds = new StringBuilder();
216 deleteIds.append("( ");
217 } else {
218 deleteIds.append(" OR ( ");
219 }
220 deleteIds.append(BookmarkColumns._ID);
221 deleteIds.append(" = ");
222 deleteIds.append(id);
223 deleteIds.append(" )");
224 } else {
225 // It is no longer a bookmark, but it is still a visited site.
226 if (convertIds == null) {
227 convertIds = new StringBuilder();
228 convertIds.append("( ");
229 } else {
230 convertIds.append(" OR ( ");
231 }
232 convertIds.append(BookmarkColumns._ID);
233 convertIds.append(" = ");
234 convertIds.append(id);
235 convertIds.append(" )");
236 }
237 }
238
239 if (deleteIds != null) {
240 mContentResolver.delete(Browser.BOOKMARKS_URI, deleteIds.toString(),
241 null);
242 }
243 if (convertIds != null) {
244 ContentValues values = new ContentValues();
245 values.put(Browser.BookmarkColumns.BOOKMARK, 0);
246 mContentResolver.update(Browser.BOOKMARKS_URI, values,
247 convertIds.toString(), null);
248 }
249 refreshList();
250 }
251
252 /**
253 * Refresh list to recognize a change in the database.
254 */
255 public void refreshList() {
256 // FIXME: consider using requery().
257 // Need to do more work to get it to function though.
258 searchInternal(mLastWhereClause, mLastSelectionArgs, mLastOrderBy);
259 }
260
261 /**
262 * Search the database for bookmarks that match the input string.
263 * @param like String to use to search the database. Strings with spaces
264 * are treated as having multiple search terms using the
265 * OR operator. Search both the title and url.
266 */
267 public void search(String like) {
268 String whereClause = Browser.BookmarkColumns.BOOKMARK + " == 1";
269 String[] selectionArgs = null;
270 if (like != null) {
271 String[] likes = like.split(" ");
272 int count = 0;
273 boolean firstTerm = true;
274 StringBuilder andClause = new StringBuilder(256);
275 for (int j = 0; j < likes.length; j++) {
276 if (likes[j].length() > 0) {
277 if (firstTerm) {
278 firstTerm = false;
279 } else {
280 andClause.append(" OR ");
281 }
282 andClause.append(Browser.BookmarkColumns.TITLE
283 + " LIKE ? OR " + Browser.BookmarkColumns.URL
284 + " LIKE ? ");
285 count += 2;
286 }
287 }
288 if (count > 0) {
289 selectionArgs = new String[count];
290 count = 0;
291 for (int j = 0; j < likes.length; j++) {
292 if (likes[j].length() > 0) {
293 like = "%" + likes[j] + "%";
294 selectionArgs[count++] = like;
295 selectionArgs[count++] = like;
296 }
297 }
298 whereClause += " AND (" + andClause + ")";
299 }
300 }
301 searchInternal(whereClause, selectionArgs, mLastOrderBy);
302 }
303
304 /**
305 * Update the bookmark's favicon.
306 * @param cr The ContentResolver to use.
307 * @param url The url of the bookmark to update.
308 * @param favicon The favicon bitmap to write to the db.
309 */
310 /* package */ static void updateBookmarkFavicon(ContentResolver cr,
311 String url, Bitmap favicon) {
312 if (url == null || favicon == null) {
313 return;
314 }
315 final String[] selArgs = new String[] { url };
316 final String where = Browser.BookmarkColumns.URL + " == ? AND "
317 + Browser.BookmarkColumns.BOOKMARK + " == 1";
318 final String[] projection = new String[] { Browser.BookmarkColumns._ID };
319 final Cursor c = cr.query(Browser.BOOKMARKS_URI, projection, where,
320 selArgs, null);
321 boolean succeed = c.moveToFirst();
322 ContentValues values = null;
323 while (succeed) {
324 if (values == null) {
325 final ByteArrayOutputStream os = new ByteArrayOutputStream();
326 favicon.compress(Bitmap.CompressFormat.PNG, 100, os);
327 values = new ContentValues();
328 values.put(Browser.BookmarkColumns.FAVICON, os.toByteArray());
329 }
330 cr.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI, c
331 .getInt(0)), values, null, null);
332 succeed = c.moveToNext();
333 }
334 c.close();
335 }
336
337 /**
338 * This sorts alphabetically, with non-capitalized titles before
339 * capitalized.
340 */
341 public void sortAlphabetical() {
342 searchInternal(mLastWhereClause, mLastSelectionArgs,
343 Browser.BookmarkColumns.TITLE + " COLLATE UNICODE ASC");
344 }
345
346 /**
347 * Internal function used in search, sort, and refreshList.
348 */
349 private void searchInternal(String whereClause, String[] selectionArgs,
350 String orderBy) {
351 if (mCursor != null) {
352 mCursor.unregisterContentObserver(mChangeObserver);
353 mCursor.unregisterDataSetObserver(mDataSetObserver);
354 mBookmarksPage.stopManagingCursor(mCursor);
355 mCursor.deactivate();
356 }
357
358 mLastWhereClause = whereClause;
359 mLastSelectionArgs = selectionArgs;
360 mLastOrderBy = orderBy;
361 mCursor = mContentResolver.query(
362 Browser.BOOKMARKS_URI,
363 Browser.HISTORY_PROJECTION,
364 whereClause,
365 selectionArgs,
366 orderBy);
367 mCursor.registerContentObserver(mChangeObserver);
368 mCursor.registerDataSetObserver(mDataSetObserver);
369 mBookmarksPage.startManagingCursor(mCursor);
370
371 mDataValid = true;
372 notifyDataSetChanged();
373
374 mCount = mCursor.getCount() + mExtraOffset;
375 }
376
377 /**
378 * How many items should be displayed in the list.
379 * @return Count of items.
380 */
381 public int getCount() {
382 if (mDataValid) {
383 return mCount;
384 } else {
385 return 0;
386 }
387 }
388
389 public boolean areAllItemsEnabled() {
390 return true;
391 }
392
393 public boolean isEnabled(int position) {
394 return true;
395 }
396
397 /**
398 * Get the data associated with the specified position in the list.
399 * @param position Index of the item whose data we want.
400 * @return The data at the specified position.
401 */
402 public Object getItem(int position) {
403 return null;
404 }
405
406 /**
407 * Get the row id associated with the specified position in the list.
408 * @param position Index of the item whose row id we want.
409 * @return The id of the item at the specified position.
410 */
411 public long getItemId(int position) {
412 return position;
413 }
414
415 /**
416 * Get a View that displays the data at the specified position
417 * in the list.
418 * @param position Index of the item whose view we want.
419 * @return A View corresponding to the data at the specified position.
420 */
421 public View getView(int position, View convertView, ViewGroup parent) {
422 if (!mDataValid) {
423 throw new IllegalStateException(
424 "this should only be called when the cursor is valid");
425 }
426 if (position < 0 || position > mCount) {
427 throw new AssertionError(
428 "BrowserBookmarksAdapter tried to get a view out of range");
429 }
430 if (position == 0 && !mCreateShortcut) {
431 AddNewBookmark b;
432 if (convertView instanceof AddNewBookmark) {
433 b = (AddNewBookmark) convertView;
434 } else {
435 b = new AddNewBookmark(mBookmarksPage);
436 }
437 b.setUrl(mCurrentPage);
438 return b;
439 }
440 if (convertView == null || convertView instanceof AddNewBookmark) {
441 convertView = new BookmarkItem(mBookmarksPage);
442 }
443 bind((BookmarkItem)convertView, position);
444 return convertView;
445 }
446
447 /**
448 * Return the title for this item in the list.
449 */
450 public String getTitle(int position) {
451 return getString(Browser.HISTORY_PROJECTION_TITLE_INDEX, position);
452 }
453
454 /**
455 * Return the Url for this item in the list.
456 */
457 public String getUrl(int position) {
458 return getString(Browser.HISTORY_PROJECTION_URL_INDEX, position);
459 }
460
461 /**
462 * Private helper function to return the title or url.
463 */
464 private String getString(int cursorIndex, int position) {
465 if (position < mExtraOffset || position > mCount) {
466 return "";
467 }
468 mCursor.moveToPosition(position- mExtraOffset);
469 return mCursor.getString(cursorIndex);
470 }
471
472 private void bind(BookmarkItem b, int position) {
473 mCursor.moveToPosition(position- mExtraOffset);
474
475 String title = mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX);
476 if (title.length() > BrowserSettings.MAX_TEXTVIEW_LEN) {
477 title = title.substring(0, BrowserSettings.MAX_TEXTVIEW_LEN);
478 }
479 b.setName(title);
480 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
481 if (url.length() > BrowserSettings.MAX_TEXTVIEW_LEN) {
482 url = url.substring(0, BrowserSettings.MAX_TEXTVIEW_LEN);
483 }
484 b.setUrl(url);
485 byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
486 if (data != null) {
487 b.setFavicon(BitmapFactory.decodeByteArray(data, 0, data.length));
488 } else {
489 b.setFavicon(null);
490 }
491 }
492
493 private class ChangeObserver extends ContentObserver {
494 public ChangeObserver() {
495 super(new Handler());
496 }
497
498 @Override
499 public boolean deliverSelfNotifications() {
500 return true;
501 }
502
503 @Override
504 public void onChange(boolean selfChange) {
505 refreshList();
506 }
507 }
508
509 private class MyDataSetObserver extends DataSetObserver {
510 @Override
511 public void onChanged() {
512 mDataValid = true;
513 notifyDataSetChanged();
514 }
515
516 @Override
517 public void onInvalidated() {
518 mDataValid = false;
519 notifyDataSetInvalidated();
520 }
521 }
522}