The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.content.ContentResolver; |
| 20 | import android.content.ContentUris; |
| 21 | import android.content.ContentValues; |
| 22 | import android.database.ContentObserver; |
| 23 | import android.database.Cursor; |
| 24 | import android.database.DataSetObserver; |
| 25 | import android.graphics.Bitmap; |
| 26 | import android.graphics.BitmapFactory; |
| 27 | import android.net.Uri; |
| 28 | import android.os.Bundle; |
| 29 | import android.os.Handler; |
| 30 | import android.provider.Browser; |
| 31 | import android.provider.Browser.BookmarkColumns; |
| 32 | import android.view.KeyEvent; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 34 | import android.view.View; |
| 35 | import android.view.ViewGroup; |
| 36 | import android.webkit.WebIconDatabase; |
| 37 | import android.webkit.WebIconDatabase.IconListener; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 38 | import android.webkit.WebView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | import android.widget.BaseAdapter; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 40 | import android.widget.ImageView; |
| 41 | import android.widget.TextView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 42 | |
| 43 | import java.io.ByteArrayOutputStream; |
| 44 | |
| 45 | class BrowserBookmarksAdapter extends BaseAdapter { |
| 46 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 47 | private String mCurrentPage; |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 48 | private String mCurrentTitle; |
Ben Murdoch | dcc2b6f | 2009-09-21 14:29:20 +0100 | [diff] [blame] | 49 | private Bitmap mCurrentThumbnail; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 50 | private Cursor mCursor; |
| 51 | private int mCount; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 52 | private BrowserBookmarksPage mBookmarksPage; |
| 53 | private ContentResolver mContentResolver; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | private boolean mDataValid; |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 55 | private BookmarkViewMode mViewMode; |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 56 | private boolean mMostVisited; |
| 57 | private boolean mNeedsOffset; |
| 58 | private int mExtraOffset; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 59 | |
| 60 | // Implementation of WebIconDatabase.IconListener |
| 61 | private class IconReceiver implements IconListener { |
| 62 | public void onReceivedIcon(String url, Bitmap icon) { |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 63 | updateBookmarkFavicon(mContentResolver, null, url, icon); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | // Instance of IconReceiver |
| 68 | private final IconReceiver mIconReceiver = new IconReceiver(); |
| 69 | |
| 70 | /** |
| 71 | * Create a new BrowserBookmarksAdapter. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 72 | * @param b BrowserBookmarksPage that instantiated this. |
| 73 | * Necessary so it will adjust its focus |
| 74 | * appropriately after a search. |
| 75 | */ |
| 76 | public BrowserBookmarksAdapter(BrowserBookmarksPage b, String curPage, |
Ben Murdoch | dcc2b6f | 2009-09-21 14:29:20 +0100 | [diff] [blame] | 77 | String curTitle, Bitmap curThumbnail, boolean createShortcut, |
| 78 | boolean mostVisited) { |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 79 | mNeedsOffset = !(createShortcut || mostVisited); |
| 80 | mMostVisited = mostVisited; |
| 81 | mExtraOffset = mNeedsOffset ? 1 : 0; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 82 | mBookmarksPage = b; |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 83 | mCurrentPage = b.getResources().getString(R.string.current_page) |
| 84 | + curPage; |
| 85 | mCurrentTitle = curTitle; |
Ben Murdoch | dcc2b6f | 2009-09-21 14:29:20 +0100 | [diff] [blame] | 86 | mCurrentThumbnail = curThumbnail; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 87 | mContentResolver = b.getContentResolver(); |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 88 | mViewMode = BookmarkViewMode.LIST; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 89 | |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 90 | String whereClause; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 91 | // FIXME: Should have a default sort order that the user selects. |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 92 | String orderBy = Browser.BookmarkColumns.VISITS + " DESC"; |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 93 | if (mostVisited) { |
| 94 | whereClause = Browser.BookmarkColumns.VISITS + " != 0"; |
| 95 | } else { |
| 96 | whereClause = Browser.BookmarkColumns.BOOKMARK + " != 0"; |
| 97 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 98 | mCursor = b.managedQuery(Browser.BOOKMARKS_URI, |
| 99 | Browser.HISTORY_PROJECTION, whereClause, null, orderBy); |
| 100 | mCursor.registerContentObserver(new ChangeObserver()); |
| 101 | mCursor.registerDataSetObserver(new MyDataSetObserver()); |
| 102 | |
| 103 | mDataValid = true; |
| 104 | notifyDataSetChanged(); |
| 105 | |
| 106 | mCount = mCursor.getCount() + mExtraOffset; |
| 107 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 108 | // FIXME: This requires another query of the database after the |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 109 | // managedQuery. Can we optimize this? |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 110 | Browser.requestAllIcons(mContentResolver, |
| 111 | Browser.BookmarkColumns.FAVICON + " is NULL AND " + |
| 112 | Browser.BookmarkColumns.BOOKMARK + " == 1", mIconReceiver); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Return a hashmap with one row's Title, Url, and favicon. |
| 117 | * @param position Position in the list. |
| 118 | * @return Bundle Stores title, url of row position, favicon, and id |
| 119 | * for the url. Return a blank map if position is out of |
| 120 | * range. |
| 121 | */ |
| 122 | public Bundle getRow(int position) { |
| 123 | Bundle map = new Bundle(); |
| 124 | if (position < mExtraOffset || position >= mCount) { |
| 125 | return map; |
| 126 | } |
| 127 | mCursor.moveToPosition(position- mExtraOffset); |
| 128 | String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX); |
| 129 | map.putString(Browser.BookmarkColumns.TITLE, |
| 130 | mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX)); |
| 131 | map.putString(Browser.BookmarkColumns.URL, url); |
| 132 | byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX); |
| 133 | if (data != null) { |
| 134 | map.putParcelable(Browser.BookmarkColumns.FAVICON, |
| 135 | BitmapFactory.decodeByteArray(data, 0, data.length)); |
| 136 | } |
| 137 | map.putInt("id", mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX)); |
| 138 | return map; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Update a row in the database with new information. |
| 143 | * Requeries the database if the information has changed. |
| 144 | * @param map Bundle storing id, title and url of new information |
| 145 | */ |
| 146 | public void updateRow(Bundle map) { |
| 147 | |
| 148 | // Find the record |
| 149 | int id = map.getInt("id"); |
| 150 | int position = -1; |
| 151 | for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) { |
| 152 | if (mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX) == id) { |
| 153 | position = mCursor.getPosition(); |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | if (position < 0) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | mCursor.moveToPosition(position); |
| 162 | ContentValues values = new ContentValues(); |
| 163 | String title = map.getString(Browser.BookmarkColumns.TITLE); |
| 164 | if (!title.equals(mCursor |
| 165 | .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX))) { |
| 166 | values.put(Browser.BookmarkColumns.TITLE, title); |
| 167 | } |
| 168 | String url = map.getString(Browser.BookmarkColumns.URL); |
| 169 | if (!url.equals(mCursor. |
| 170 | getString(Browser.HISTORY_PROJECTION_URL_INDEX))) { |
| 171 | values.put(Browser.BookmarkColumns.URL, url); |
| 172 | } |
| 173 | if (values.size() > 0 |
| 174 | && mContentResolver.update(Browser.BOOKMARKS_URI, values, |
| 175 | "_id = " + id, null) != -1) { |
| 176 | refreshList(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Delete a row from the database. Requeries the database. |
| 182 | * Does nothing if the provided position is out of range. |
| 183 | * @param position Position in the list. |
| 184 | */ |
| 185 | public void deleteRow(int position) { |
| 186 | if (position < mExtraOffset || position >= getCount()) { |
| 187 | return; |
| 188 | } |
| 189 | mCursor.moveToPosition(position- mExtraOffset); |
| 190 | String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX); |
Andrei Popescu | c952619 | 2009-09-23 15:52:16 +0100 | [diff] [blame^] | 191 | String title = mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX); |
| 192 | Bookmarks.removeFromBookmarks(null, mContentResolver, url, title); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 193 | refreshList(); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Delete all bookmarks from the db. Requeries the database. |
| 198 | * All bookmarks with become visited URLs or if never visited |
| 199 | * are removed |
| 200 | */ |
| 201 | public void deleteAllRows() { |
| 202 | StringBuilder deleteIds = null; |
| 203 | StringBuilder convertIds = null; |
| 204 | |
| 205 | for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) { |
| 206 | String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX); |
| 207 | WebIconDatabase.getInstance().releaseIconForPageUrl(url); |
| 208 | int id = mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX); |
| 209 | int numVisits = mCursor.getInt(Browser.HISTORY_PROJECTION_VISITS_INDEX); |
| 210 | if (0 == numVisits) { |
| 211 | if (deleteIds == null) { |
| 212 | deleteIds = new StringBuilder(); |
| 213 | deleteIds.append("( "); |
| 214 | } else { |
| 215 | deleteIds.append(" OR ( "); |
| 216 | } |
| 217 | deleteIds.append(BookmarkColumns._ID); |
| 218 | deleteIds.append(" = "); |
| 219 | deleteIds.append(id); |
| 220 | deleteIds.append(" )"); |
| 221 | } else { |
| 222 | // It is no longer a bookmark, but it is still a visited site. |
| 223 | if (convertIds == null) { |
| 224 | convertIds = new StringBuilder(); |
| 225 | convertIds.append("( "); |
| 226 | } else { |
| 227 | convertIds.append(" OR ( "); |
| 228 | } |
| 229 | convertIds.append(BookmarkColumns._ID); |
| 230 | convertIds.append(" = "); |
| 231 | convertIds.append(id); |
| 232 | convertIds.append(" )"); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if (deleteIds != null) { |
| 237 | mContentResolver.delete(Browser.BOOKMARKS_URI, deleteIds.toString(), |
| 238 | null); |
| 239 | } |
| 240 | if (convertIds != null) { |
| 241 | ContentValues values = new ContentValues(); |
| 242 | values.put(Browser.BookmarkColumns.BOOKMARK, 0); |
| 243 | mContentResolver.update(Browser.BOOKMARKS_URI, values, |
| 244 | convertIds.toString(), null); |
| 245 | } |
| 246 | refreshList(); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Refresh list to recognize a change in the database. |
| 251 | */ |
| 252 | public void refreshList() { |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 253 | mCursor.requery(); |
| 254 | mCount = mCursor.getCount() + mExtraOffset; |
| 255 | notifyDataSetChanged(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 259 | * Update the bookmark's favicon. This is a convenience method for updating |
| 260 | * a bookmark favicon for the originalUrl and url of the passed in WebView. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 261 | * @param cr The ContentResolver to use. |
Patrick Scott | 15525d4 | 2009-09-21 13:39:37 -0400 | [diff] [blame] | 262 | * @param originalUrl The original url before any redirects. |
| 263 | * @param url The current url. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 264 | * @param favicon The favicon bitmap to write to the db. |
| 265 | */ |
| 266 | /* package */ static void updateBookmarkFavicon(ContentResolver cr, |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 267 | String originalUrl, String url, Bitmap favicon) { |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 268 | final Cursor c = queryBookmarksForUrl(cr, originalUrl, url, true); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 269 | if (c == null) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 270 | return; |
| 271 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 272 | boolean succeed = c.moveToFirst(); |
| 273 | ContentValues values = null; |
| 274 | while (succeed) { |
| 275 | if (values == null) { |
| 276 | final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 277 | favicon.compress(Bitmap.CompressFormat.PNG, 100, os); |
| 278 | values = new ContentValues(); |
| 279 | values.put(Browser.BookmarkColumns.FAVICON, os.toByteArray()); |
| 280 | } |
| 281 | cr.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI, c |
| 282 | .getInt(0)), values, null, null); |
| 283 | succeed = c.moveToNext(); |
| 284 | } |
| 285 | c.close(); |
| 286 | } |
| 287 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 288 | /* package */ static Cursor queryBookmarksForUrl(ContentResolver cr, |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 289 | String originalUrl, String url, boolean onlyBookmarks) { |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 290 | if (cr == null || url == null) { |
| 291 | return null; |
| 292 | } |
| 293 | |
| 294 | // If originalUrl is null, just set it to url. |
| 295 | if (originalUrl == null) { |
| 296 | originalUrl = url; |
| 297 | } |
| 298 | |
| 299 | // Look for both the original url and the actual url. This takes in to |
| 300 | // account redirects. |
| 301 | String originalUrlNoQuery = removeQuery(originalUrl); |
| 302 | String urlNoQuery = removeQuery(url); |
| 303 | originalUrl = originalUrlNoQuery + '?'; |
| 304 | url = urlNoQuery + '?'; |
| 305 | |
| 306 | // Use NoQuery to search for the base url (i.e. if the url is |
| 307 | // http://www.yahoo.com/?rs=1, search for http://www.yahoo.com) |
| 308 | // Use url to match the base url with other queries (i.e. if the url is |
| 309 | // http://www.google.com/m, search for |
| 310 | // http://www.google.com/m?some_query) |
| 311 | final String[] selArgs = new String[] { |
| 312 | originalUrlNoQuery, urlNoQuery, originalUrl, url }; |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 313 | String where = BookmarkColumns.URL + " == ? OR " |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 314 | + BookmarkColumns.URL + " == ? OR " |
| 315 | + BookmarkColumns.URL + " GLOB ? || '*' OR " |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 316 | + BookmarkColumns.URL + " GLOB ? || '*'"; |
| 317 | if (onlyBookmarks) { |
| 318 | where = "(" + where + ") AND " + BookmarkColumns.BOOKMARK + " == 1"; |
| 319 | } |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 320 | final String[] projection = |
| 321 | new String[] { Browser.BookmarkColumns._ID }; |
| 322 | return cr.query(Browser.BOOKMARKS_URI, projection, where, selArgs, |
| 323 | null); |
| 324 | } |
| 325 | |
| 326 | // Strip the query from the given url. |
| 327 | private static String removeQuery(String url) { |
| 328 | if (url == null) { |
| 329 | return null; |
| 330 | } |
| 331 | int query = url.indexOf('?'); |
| 332 | String noQuery = url; |
| 333 | if (query != -1) { |
| 334 | noQuery = url.substring(0, query); |
| 335 | } |
| 336 | return noQuery; |
| 337 | } |
| 338 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 339 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 340 | * How many items should be displayed in the list. |
| 341 | * @return Count of items. |
| 342 | */ |
| 343 | public int getCount() { |
| 344 | if (mDataValid) { |
| 345 | return mCount; |
| 346 | } else { |
| 347 | return 0; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | public boolean areAllItemsEnabled() { |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | public boolean isEnabled(int position) { |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Get the data associated with the specified position in the list. |
| 361 | * @param position Index of the item whose data we want. |
| 362 | * @return The data at the specified position. |
| 363 | */ |
| 364 | public Object getItem(int position) { |
| 365 | return null; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Get the row id associated with the specified position in the list. |
| 370 | * @param position Index of the item whose row id we want. |
| 371 | * @return The id of the item at the specified position. |
| 372 | */ |
| 373 | public long getItemId(int position) { |
| 374 | return position; |
| 375 | } |
| 376 | |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 377 | /* package */ void switchViewMode(BookmarkViewMode viewMode) { |
| 378 | mViewMode = viewMode; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* package */ void populateBookmarkItem(BookmarkItem b, int position) { |
| 382 | mCursor.moveToPosition(position - mExtraOffset); |
Patrick Scott | 8f0076b | 2009-09-17 13:51:30 -0400 | [diff] [blame] | 383 | String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX); |
| 384 | b.setUrl(url); |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 385 | b.setName(mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX)); |
| 386 | byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX); |
Patrick Scott | 8f0076b | 2009-09-17 13:51:30 -0400 | [diff] [blame] | 387 | Bitmap bitmap = null; |
| 388 | if (data == null) { |
| 389 | bitmap = CombinedBookmarkHistoryActivity.getIconListenerSet() |
| 390 | .getFavicon(url); |
| 391 | } else { |
| 392 | bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); |
| 393 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 394 | b.setFavicon(bitmap); |
| 395 | } |
| 396 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 397 | /** |
| 398 | * Get a View that displays the data at the specified position |
| 399 | * in the list. |
| 400 | * @param position Index of the item whose view we want. |
| 401 | * @return A View corresponding to the data at the specified position. |
| 402 | */ |
| 403 | public View getView(int position, View convertView, ViewGroup parent) { |
| 404 | if (!mDataValid) { |
| 405 | throw new IllegalStateException( |
| 406 | "this should only be called when the cursor is valid"); |
| 407 | } |
| 408 | if (position < 0 || position > mCount) { |
| 409 | throw new AssertionError( |
| 410 | "BrowserBookmarksAdapter tried to get a view out of range"); |
| 411 | } |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 412 | if (mViewMode == BookmarkViewMode.GRID) { |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 413 | if (convertView == null || convertView instanceof AddNewBookmark |
| 414 | || convertView instanceof BookmarkItem) { |
| 415 | LayoutInflater factory = LayoutInflater.from(mBookmarksPage); |
| 416 | convertView |
| 417 | = factory.inflate(R.layout.bookmark_thumbnail, null); |
| 418 | } |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 419 | View holder = convertView.findViewById(R.id.holder); |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 420 | ImageView thumb = (ImageView) convertView.findViewById(R.id.thumb); |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 421 | TextView tv = (TextView) convertView.findViewById(R.id.label); |
| 422 | |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 423 | if (0 == position && mNeedsOffset) { |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 424 | // This is to create a bookmark for the current page. |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 425 | holder.setVisibility(View.VISIBLE); |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 426 | tv.setText(mCurrentTitle); |
Ben Murdoch | dcc2b6f | 2009-09-21 14:29:20 +0100 | [diff] [blame] | 427 | |
| 428 | if (mCurrentThumbnail != null) { |
| 429 | thumb.setImageBitmap(mCurrentThumbnail); |
| 430 | } else { |
| 431 | thumb.setImageResource( |
| 432 | R.drawable.ic_launcher_shortcut_browser_bookmark); |
| 433 | } |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 434 | return convertView; |
| 435 | } |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 436 | holder.setVisibility(View.GONE); |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 437 | mCursor.moveToPosition(position - mExtraOffset); |
| 438 | tv.setText(mCursor.getString( |
| 439 | Browser.HISTORY_PROJECTION_TITLE_INDEX)); |
| 440 | byte[] data = mCursor.getBlob( |
| 441 | Browser.HISTORY_PROJECTION_THUMBNAIL_INDEX); |
| 442 | if (data == null) { |
Leon Scroggins | b40bf27 | 2009-09-14 18:43:27 -0400 | [diff] [blame] | 443 | thumb.setImageResource(R.drawable.ic_launcher_shortcut_browser_bookmark); |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 444 | } else { |
| 445 | thumb.setImageBitmap( |
| 446 | BitmapFactory.decodeByteArray(data, 0, data.length)); |
| 447 | } |
Leon Scroggins | 89c6d36 | 2009-07-15 16:54:37 -0400 | [diff] [blame] | 448 | |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 449 | return convertView; |
| 450 | |
| 451 | } |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 452 | if (position == 0 && mNeedsOffset) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 453 | AddNewBookmark b; |
| 454 | if (convertView instanceof AddNewBookmark) { |
| 455 | b = (AddNewBookmark) convertView; |
| 456 | } else { |
| 457 | b = new AddNewBookmark(mBookmarksPage); |
| 458 | } |
| 459 | b.setUrl(mCurrentPage); |
| 460 | return b; |
| 461 | } |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 462 | if (mMostVisited) { |
| 463 | if (convertView == null || !(convertView instanceof HistoryItem)) { |
| 464 | convertView = new HistoryItem(mBookmarksPage); |
| 465 | } |
| 466 | } else { |
| 467 | if (convertView == null || !(convertView instanceof BookmarkItem)) { |
| 468 | convertView = new BookmarkItem(mBookmarksPage); |
| 469 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 470 | } |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 471 | bind((BookmarkItem) convertView, position); |
| 472 | if (mMostVisited) { |
| 473 | ((HistoryItem) convertView).setIsBookmark( |
| 474 | getIsBookmark(position)); |
| 475 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 476 | return convertView; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Return the title for this item in the list. |
| 481 | */ |
| 482 | public String getTitle(int position) { |
| 483 | return getString(Browser.HISTORY_PROJECTION_TITLE_INDEX, position); |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Return the Url for this item in the list. |
| 488 | */ |
| 489 | public String getUrl(int position) { |
| 490 | return getString(Browser.HISTORY_PROJECTION_URL_INDEX, position); |
| 491 | } |
| 492 | |
| 493 | /** |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 494 | * Return the favicon for this item in the list. |
| 495 | */ |
| 496 | public Bitmap getFavicon(int position) { |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 497 | return getBitmap(Browser.HISTORY_PROJECTION_FAVICON_INDEX, position); |
| 498 | } |
| 499 | |
| 500 | public Bitmap getTouchIcon(int position) { |
| 501 | return getBitmap(Browser.HISTORY_PROJECTION_TOUCH_ICON_INDEX, position); |
| 502 | } |
| 503 | |
| 504 | private Bitmap getBitmap(int cursorIndex, int position) { |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 505 | if (position < mExtraOffset || position > mCount) { |
| 506 | return null; |
| 507 | } |
| 508 | mCursor.moveToPosition(position - mExtraOffset); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 509 | byte[] data = mCursor.getBlob(cursorIndex); |
Patrick Scott | e09761e | 2009-03-24 20:43:37 -0700 | [diff] [blame] | 510 | if (data == null) { |
| 511 | return null; |
| 512 | } |
| 513 | return BitmapFactory.decodeByteArray(data, 0, data.length); |
| 514 | } |
| 515 | |
| 516 | /** |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 517 | * Return whether or not this item represents a bookmarked site. |
| 518 | */ |
| 519 | public boolean getIsBookmark(int position) { |
| 520 | if (position < mExtraOffset || position > mCount) { |
| 521 | return false; |
| 522 | } |
| 523 | mCursor.moveToPosition(position - mExtraOffset); |
| 524 | return (1 == mCursor.getInt(Browser.HISTORY_PROJECTION_BOOKMARK_INDEX)); |
| 525 | } |
| 526 | |
| 527 | /** |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 528 | * Private helper function to return the title or url. |
| 529 | */ |
| 530 | private String getString(int cursorIndex, int position) { |
| 531 | if (position < mExtraOffset || position > mCount) { |
| 532 | return ""; |
| 533 | } |
| 534 | mCursor.moveToPosition(position- mExtraOffset); |
| 535 | return mCursor.getString(cursorIndex); |
| 536 | } |
| 537 | |
| 538 | private void bind(BookmarkItem b, int position) { |
| 539 | mCursor.moveToPosition(position- mExtraOffset); |
| 540 | |
| 541 | String title = mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX); |
| 542 | if (title.length() > BrowserSettings.MAX_TEXTVIEW_LEN) { |
| 543 | title = title.substring(0, BrowserSettings.MAX_TEXTVIEW_LEN); |
| 544 | } |
| 545 | b.setName(title); |
| 546 | String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX); |
| 547 | if (url.length() > BrowserSettings.MAX_TEXTVIEW_LEN) { |
| 548 | url = url.substring(0, BrowserSettings.MAX_TEXTVIEW_LEN); |
| 549 | } |
| 550 | b.setUrl(url); |
| 551 | byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX); |
| 552 | if (data != null) { |
| 553 | b.setFavicon(BitmapFactory.decodeByteArray(data, 0, data.length)); |
| 554 | } else { |
Patrick Scott | 8f0076b | 2009-09-17 13:51:30 -0400 | [diff] [blame] | 555 | b.setFavicon(CombinedBookmarkHistoryActivity.getIconListenerSet() |
| 556 | .getFavicon(url)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
| 560 | private class ChangeObserver extends ContentObserver { |
| 561 | public ChangeObserver() { |
| 562 | super(new Handler()); |
| 563 | } |
| 564 | |
| 565 | @Override |
| 566 | public boolean deliverSelfNotifications() { |
| 567 | return true; |
| 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public void onChange(boolean selfChange) { |
| 572 | refreshList(); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | private class MyDataSetObserver extends DataSetObserver { |
| 577 | @Override |
| 578 | public void onChanged() { |
| 579 | mDataValid = true; |
| 580 | notifyDataSetChanged(); |
| 581 | } |
| 582 | |
| 583 | @Override |
| 584 | public void onInvalidated() { |
| 585 | mDataValid = false; |
| 586 | notifyDataSetInvalidated(); |
| 587 | } |
| 588 | } |
| 589 | } |