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