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; |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 22 | import android.graphics.drawable.BitmapDrawable; |
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; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 27 | import android.widget.ImageView; |
John Reck | 23c61dd | 2011-09-28 15:02:24 -0700 | [diff] [blame] | 28 | import android.widget.ImageView.ScaleType; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 29 | import android.widget.TextView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 30 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 31 | import com.android.browser.util.ThreadedCursorAdapter; |
| 32 | import com.android.browser.view.BookmarkContainer; |
| 33 | |
| 34 | public class BrowserBookmarksAdapter extends |
| 35 | ThreadedCursorAdapter<BrowserBookmarksAdapterItem> { |
| 36 | |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 37 | LayoutInflater mInflater; |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 38 | Context mContext; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 39 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Create a new BrowserBookmarksAdapter. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 42 | */ |
John Reck | af991f9 | 2012-04-24 15:31:16 -0700 | [diff] [blame] | 43 | public BrowserBookmarksAdapter(Context context) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 44 | // Make sure to tell the CursorAdapter to avoid the observer and auto-requery |
| 45 | // since the Loader will do that for us. |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 46 | super(context, null); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 47 | mInflater = LayoutInflater.from(context); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 48 | mContext = context; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 49 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 50 | |
| 51 | @Override |
John Reck | 9ee87d9 | 2012-05-04 13:53:25 -0700 | [diff] [blame] | 52 | protected long getItemId(Cursor c) { |
| 53 | return c.getLong(BookmarksLoader.COLUMN_INDEX_ID); |
| 54 | } |
| 55 | |
| 56 | @Override |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 57 | public View newView(Context context, ViewGroup parent) { |
John Reck | af991f9 | 2012-04-24 15:31:16 -0700 | [diff] [blame] | 58 | return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 59 | } |
| 60 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 61 | @Override |
| 62 | public void bindView(View view, BrowserBookmarksAdapterItem object) { |
| 63 | BookmarkContainer container = (BookmarkContainer) view; |
| 64 | container.setIgnoreRequestLayout(true); |
John Reck | af991f9 | 2012-04-24 15:31:16 -0700 | [diff] [blame] | 65 | bindGridView(view, mContext, object); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 66 | container.setIgnoreRequestLayout(false); |
| 67 | } |
| 68 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 69 | CharSequence getTitle(Cursor cursor) { |
John Reck | d18ac4b | 2012-04-12 17:27:34 -0700 | [diff] [blame] | 70 | int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE); |
| 71 | switch (type) { |
| 72 | case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER: |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 73 | return mContext.getText(R.string.other_bookmarks); |
John Reck | d18ac4b | 2012-04-12 17:27:34 -0700 | [diff] [blame] | 74 | } |
| 75 | return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
| 76 | } |
| 77 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 78 | void bindGridView(View view, Context context, BrowserBookmarksAdapterItem item) { |
John Reck | f3828cd | 2011-06-14 17:21:55 -0700 | [diff] [blame] | 79 | // We need to set this to handle rotation and other configuration change |
| 80 | // events. If the padding didn't change, this is a no op. |
| 81 | int padding = context.getResources() |
| 82 | .getDimensionPixelSize(R.dimen.combo_horizontalSpacing); |
| 83 | view.setPadding(padding, view.getPaddingTop(), |
| 84 | padding, view.getPaddingBottom()); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 85 | ImageView thumb = (ImageView) view.findViewById(R.id.thumb); |
| 86 | TextView tv = (TextView) view.findViewById(R.id.label); |
| 87 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 88 | tv.setText(item.title); |
| 89 | if (item.is_folder) { |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 90 | // folder |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 91 | thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo); |
John Reck | 23c61dd | 2011-09-28 15:02:24 -0700 | [diff] [blame] | 92 | thumb.setScaleType(ScaleType.FIT_END); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 93 | thumb.setBackground(null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 94 | } else { |
John Reck | 23c61dd | 2011-09-28 15:02:24 -0700 | [diff] [blame] | 95 | thumb.setScaleType(ScaleType.CENTER_CROP); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 96 | if (item.thumbnail == null) { |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 97 | thumb.setImageResource(R.drawable.browser_thumbnail); |
| 98 | } else { |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 99 | thumb.setImageDrawable(item.thumbnail); |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 100 | } |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 101 | thumb.setBackgroundResource(R.drawable.border_thumb_bookmarks_widget_holo); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 104 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 105 | @Override |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 106 | public BrowserBookmarksAdapterItem getRowObject(Cursor c, |
| 107 | BrowserBookmarksAdapterItem item) { |
| 108 | if (item == null) { |
| 109 | item = new BrowserBookmarksAdapterItem(); |
| 110 | } |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 111 | Bitmap thumbnail = item.thumbnail != null ? item.thumbnail.getBitmap() : null; |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 112 | thumbnail = BrowserBookmarksPage.getBitmap(c, |
| 113 | BookmarksLoader.COLUMN_INDEX_THUMBNAIL, thumbnail); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 114 | if (thumbnail != null |
| 115 | && (item.thumbnail == null || item.thumbnail.getBitmap() != thumbnail)) { |
| 116 | item.thumbnail = new BitmapDrawable(mContext.getResources(), thumbnail); |
| 117 | } |
| 118 | item.is_folder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; |
| 119 | item.title = getTitle(c); |
| 120 | item.url = c.getString(BookmarksLoader.COLUMN_INDEX_URL); |
| 121 | return item; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public BrowserBookmarksAdapterItem getLoadingObject() { |
| 126 | BrowserBookmarksAdapterItem item = new BrowserBookmarksAdapterItem(); |
| 127 | return item; |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 128 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 129 | } |