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 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 19 | import android.content.Context; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 20 | import android.database.Cursor; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
| 22 | import android.graphics.BitmapFactory; |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame^] | 23 | import android.graphics.drawable.Drawable; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 25 | import android.view.View; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 26 | import android.view.ViewGroup; |
| 27 | import android.widget.CursorAdapter; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 28 | import android.widget.ImageView; |
| 29 | import android.widget.TextView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 30 | |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 31 | class BrowserBookmarksAdapter extends CursorAdapter { |
| 32 | LayoutInflater mInflater; |
| 33 | int mCurrentView; |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame^] | 34 | Drawable mFaviconBackground; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 35 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 36 | /** |
| 37 | * Create a new BrowserBookmarksAdapter. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | */ |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 39 | public BrowserBookmarksAdapter(Context context, int defaultView) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 40 | // Make sure to tell the CursorAdapter to avoid the observer and auto-requery |
| 41 | // since the Loader will do that for us. |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 42 | super(context, null, 0); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 43 | mInflater = LayoutInflater.from(context); |
| 44 | selectView(defaultView); |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 45 | float density = context.getResources().getDisplayMetrics().density; |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame^] | 46 | mFaviconBackground = BookmarkUtils.createListFaviconBackground(context); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 47 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 48 | |
| 49 | @Override |
| 50 | public void bindView(View view, Context context, Cursor cursor) { |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 51 | if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) { |
| 52 | bindListView(view, context, cursor); |
| 53 | } else { |
| 54 | bindGridView(view, context, cursor); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void bindGridView(View view, Context context, Cursor cursor) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 59 | ImageView thumb = (ImageView) view.findViewById(R.id.thumb); |
| 60 | TextView tv = (TextView) view.findViewById(R.id.label); |
| 61 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 62 | tv.setText(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)); |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 63 | if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) { |
| 64 | // folder |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 65 | thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo); |
| 66 | thumb.setBackgroundDrawable(null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 67 | } else { |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 68 | byte[] thumbData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_THUMBNAIL); |
| 69 | Bitmap thumbBitmap = null; |
| 70 | if (thumbData != null) { |
| 71 | thumbBitmap = BitmapFactory.decodeByteArray(thumbData, 0, thumbData.length); |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 72 | } |
| 73 | |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 74 | if (thumbBitmap == null) { |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 75 | thumb.setImageResource(R.drawable.browser_thumbnail); |
| 76 | } else { |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 77 | thumb.setImageBitmap(thumbBitmap); |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 78 | } |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 79 | thumb.setBackgroundResource(R.drawable.border_thumb_bookmarks_widget_holo); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 82 | |
| 83 | void bindListView(View view, Context context, Cursor cursor) { |
| 84 | ImageView favicon = (ImageView) view.findViewById(R.id.favicon); |
| 85 | TextView tv = (TextView) view.findViewById(R.id.label); |
| 86 | |
| 87 | tv.setText(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)); |
| 88 | if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) { |
| 89 | // folder |
John Reck | 439c9a5 | 2010-12-14 10:04:39 -0800 | [diff] [blame] | 90 | favicon.setImageResource(R.drawable.ic_folder_bookmark_widget_holo_dark); |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 91 | favicon.setBackgroundDrawable(null); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 92 | } else { |
| 93 | byte[] faviconData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON); |
| 94 | Bitmap faviconBitmap = null; |
| 95 | if (faviconData != null) { |
| 96 | faviconBitmap = BitmapFactory.decodeByteArray(faviconData, 0, faviconData.length); |
| 97 | } |
| 98 | |
| 99 | if (faviconBitmap == null) { |
| 100 | favicon.setImageResource(R.drawable.app_web_browser_sm); |
| 101 | } else { |
| 102 | favicon.setImageBitmap(faviconBitmap); |
| 103 | } |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 104 | //favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg); |
| 105 | // TODO: Switch to above instead of below once b/3353813 is fixed |
| 106 | favicon.setBackgroundDrawable(mFaviconBackground); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
| 112 | if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) { |
| 113 | return mInflater.inflate(R.layout.bookmark_list, parent, false); |
| 114 | } else { |
| 115 | return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public void selectView(int view) { |
| 120 | if (view != BrowserBookmarksPage.VIEW_LIST |
| 121 | && view != BrowserBookmarksPage.VIEW_THUMBNAILS) { |
| 122 | throw new IllegalArgumentException("Unknown view specified: " + view); |
| 123 | } |
| 124 | mCurrentView = view; |
| 125 | } |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 126 | |
| 127 | @Override |
| 128 | public Cursor getItem(int position) { |
| 129 | return (Cursor) super.getItem(position); |
| 130 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 131 | } |