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 | d18ac4b | 2012-04-12 17:27:34 -0700 | [diff] [blame] | 23 | import android.provider.BrowserContract.Bookmarks; |
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; |
John Reck | 23c61dd | 2011-09-28 15:02:24 -0700 | [diff] [blame] | 29 | import android.widget.ImageView.ScaleType; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 30 | import android.widget.TextView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 31 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 32 | public class BrowserBookmarksAdapter extends CursorAdapter { |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 33 | LayoutInflater mInflater; |
| 34 | int mCurrentView; |
| 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); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 45 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 46 | |
| 47 | @Override |
| 48 | public void bindView(View view, Context context, Cursor cursor) { |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 49 | if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) { |
| 50 | bindListView(view, context, cursor); |
| 51 | } else { |
| 52 | bindGridView(view, context, cursor); |
| 53 | } |
| 54 | } |
| 55 | |
John Reck | d18ac4b | 2012-04-12 17:27:34 -0700 | [diff] [blame] | 56 | CharSequence getTitle(Cursor cursor, Context context) { |
| 57 | int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE); |
| 58 | switch (type) { |
| 59 | case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER: |
| 60 | return context.getText(R.string.other_bookmarks); |
| 61 | } |
| 62 | return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
| 63 | } |
| 64 | |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 65 | void bindGridView(View view, Context context, Cursor cursor) { |
John Reck | f3828cd | 2011-06-14 17:21:55 -0700 | [diff] [blame] | 66 | // We need to set this to handle rotation and other configuration change |
| 67 | // events. If the padding didn't change, this is a no op. |
| 68 | int padding = context.getResources() |
| 69 | .getDimensionPixelSize(R.dimen.combo_horizontalSpacing); |
| 70 | view.setPadding(padding, view.getPaddingTop(), |
| 71 | padding, view.getPaddingBottom()); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 72 | ImageView thumb = (ImageView) view.findViewById(R.id.thumb); |
| 73 | TextView tv = (TextView) view.findViewById(R.id.label); |
| 74 | |
John Reck | d18ac4b | 2012-04-12 17:27:34 -0700 | [diff] [blame] | 75 | tv.setText(getTitle(cursor, context)); |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 76 | if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) { |
| 77 | // folder |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 78 | thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo); |
John Reck | 23c61dd | 2011-09-28 15:02:24 -0700 | [diff] [blame] | 79 | thumb.setScaleType(ScaleType.FIT_END); |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 80 | thumb.setBackgroundDrawable(null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 81 | } else { |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 82 | byte[] thumbData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_THUMBNAIL); |
| 83 | Bitmap thumbBitmap = null; |
| 84 | if (thumbData != null) { |
| 85 | thumbBitmap = BitmapFactory.decodeByteArray(thumbData, 0, thumbData.length); |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 86 | } |
| 87 | |
John Reck | 23c61dd | 2011-09-28 15:02:24 -0700 | [diff] [blame] | 88 | thumb.setScaleType(ScaleType.CENTER_CROP); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 89 | if (thumbBitmap == null) { |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 90 | thumb.setImageResource(R.drawable.browser_thumbnail); |
| 91 | } else { |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 92 | thumb.setImageBitmap(thumbBitmap); |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 93 | } |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 94 | thumb.setBackgroundResource(R.drawable.border_thumb_bookmarks_widget_holo); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 97 | |
| 98 | void bindListView(View view, Context context, Cursor cursor) { |
| 99 | ImageView favicon = (ImageView) view.findViewById(R.id.favicon); |
| 100 | TextView tv = (TextView) view.findViewById(R.id.label); |
| 101 | |
John Reck | d18ac4b | 2012-04-12 17:27:34 -0700 | [diff] [blame] | 102 | tv.setText(getTitle(cursor, context)); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 103 | if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) { |
| 104 | // folder |
Michael Kolb | 5a72f18 | 2011-01-13 20:35:06 -0800 | [diff] [blame] | 105 | favicon.setImageResource(R.drawable.ic_folder_holo_dark); |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 106 | favicon.setBackgroundDrawable(null); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 107 | } else { |
| 108 | byte[] faviconData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON); |
| 109 | Bitmap faviconBitmap = null; |
| 110 | if (faviconData != null) { |
| 111 | faviconBitmap = BitmapFactory.decodeByteArray(faviconData, 0, faviconData.length); |
| 112 | } |
| 113 | |
| 114 | if (faviconBitmap == null) { |
| 115 | favicon.setImageResource(R.drawable.app_web_browser_sm); |
| 116 | } else { |
| 117 | favicon.setImageBitmap(faviconBitmap); |
| 118 | } |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 119 | favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
| 125 | if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) { |
| 126 | return mInflater.inflate(R.layout.bookmark_list, parent, false); |
| 127 | } else { |
| 128 | return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public void selectView(int view) { |
| 133 | if (view != BrowserBookmarksPage.VIEW_LIST |
| 134 | && view != BrowserBookmarksPage.VIEW_THUMBNAILS) { |
| 135 | throw new IllegalArgumentException("Unknown view specified: " + view); |
| 136 | } |
| 137 | mCurrentView = view; |
| 138 | } |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 139 | |
John Reck | 9db9529 | 2011-06-20 13:00:12 -0700 | [diff] [blame] | 140 | public int getViewMode() { |
| 141 | return mCurrentView; |
| 142 | } |
| 143 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 144 | @Override |
| 145 | public Cursor getItem(int position) { |
| 146 | return (Cursor) super.getItem(position); |
| 147 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 148 | } |