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; |
| 38 | int mCurrentView; |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 39 | Context mContext; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 40 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 41 | /** |
| 42 | * Create a new BrowserBookmarksAdapter. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 43 | */ |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 44 | public BrowserBookmarksAdapter(Context context, int defaultView) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 45 | // Make sure to tell the CursorAdapter to avoid the observer and auto-requery |
| 46 | // since the Loader will do that for us. |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 47 | super(context, null); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 48 | mInflater = LayoutInflater.from(context); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 49 | mContext = context; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 50 | selectView(defaultView); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 51 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 52 | |
| 53 | @Override |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 54 | public View newView(Context context, ViewGroup parent) { |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 55 | if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) { |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 56 | return mInflater.inflate(R.layout.bookmark_list, parent, false); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 57 | } else { |
John Reck | f94abcf | 2011-10-10 15:33:48 -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 | } |
| 61 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 62 | @Override |
| 63 | public void bindView(View view, BrowserBookmarksAdapterItem object) { |
| 64 | BookmarkContainer container = (BookmarkContainer) view; |
| 65 | container.setIgnoreRequestLayout(true); |
| 66 | if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) { |
| 67 | bindListView(view, mContext, object); |
| 68 | } else { |
| 69 | bindGridView(view, mContext, object); |
| 70 | } |
| 71 | container.setIgnoreRequestLayout(false); |
| 72 | } |
| 73 | |
| 74 | void clearView(View view) { |
| 75 | if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) { |
| 76 | ImageView favicon = (ImageView) view.findViewById(R.id.favicon); |
| 77 | favicon.setImageBitmap(null); |
| 78 | } else { |
| 79 | ImageView thumb = (ImageView) view.findViewById(R.id.thumb); |
| 80 | thumb.setImageBitmap(null); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | CharSequence getTitle(Cursor cursor) { |
John Reck | d18ac4b | 2012-04-12 17:27:34 -0700 | [diff] [blame] | 85 | int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE); |
| 86 | switch (type) { |
| 87 | case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER: |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 88 | return mContext.getText(R.string.other_bookmarks); |
John Reck | d18ac4b | 2012-04-12 17:27:34 -0700 | [diff] [blame] | 89 | } |
| 90 | return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
| 91 | } |
| 92 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 93 | void bindGridView(View view, Context context, BrowserBookmarksAdapterItem item) { |
John Reck | f3828cd | 2011-06-14 17:21:55 -0700 | [diff] [blame] | 94 | // We need to set this to handle rotation and other configuration change |
| 95 | // events. If the padding didn't change, this is a no op. |
| 96 | int padding = context.getResources() |
| 97 | .getDimensionPixelSize(R.dimen.combo_horizontalSpacing); |
| 98 | view.setPadding(padding, view.getPaddingTop(), |
| 99 | padding, view.getPaddingBottom()); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 100 | ImageView thumb = (ImageView) view.findViewById(R.id.thumb); |
| 101 | TextView tv = (TextView) view.findViewById(R.id.label); |
| 102 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 103 | tv.setText(item.title); |
| 104 | if (item.is_folder) { |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 105 | // folder |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 106 | thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo); |
John Reck | 23c61dd | 2011-09-28 15:02:24 -0700 | [diff] [blame] | 107 | thumb.setScaleType(ScaleType.FIT_END); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 108 | thumb.setBackground(null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 109 | } else { |
John Reck | 23c61dd | 2011-09-28 15:02:24 -0700 | [diff] [blame] | 110 | thumb.setScaleType(ScaleType.CENTER_CROP); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 111 | if (item.thumbnail == null) { |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 112 | thumb.setImageResource(R.drawable.browser_thumbnail); |
| 113 | } else { |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 114 | thumb.setImageDrawable(item.thumbnail); |
Michael Kolb | fa31407 | 2010-09-17 11:33:58 -0700 | [diff] [blame] | 115 | } |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 116 | thumb.setBackgroundResource(R.drawable.border_thumb_bookmarks_widget_holo); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 119 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 120 | void bindListView(View view, Context context, BrowserBookmarksAdapterItem item) { |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 121 | ImageView favicon = (ImageView) view.findViewById(R.id.favicon); |
| 122 | TextView tv = (TextView) view.findViewById(R.id.label); |
| 123 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 124 | tv.setText(item.title); |
| 125 | if (item.is_folder) { |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 126 | // folder |
Michael Kolb | 5a72f18 | 2011-01-13 20:35:06 -0800 | [diff] [blame] | 127 | favicon.setImageResource(R.drawable.ic_folder_holo_dark); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 128 | favicon.setBackground(null); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 129 | } else { |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 130 | if (item.favicon == null) { |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 131 | favicon.setImageResource(R.drawable.app_web_browser_sm); |
| 132 | } else { |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 133 | favicon.setImageDrawable(item.favicon); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 134 | } |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 135 | favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 139 | public void selectView(int view) { |
| 140 | if (view != BrowserBookmarksPage.VIEW_LIST |
| 141 | && view != BrowserBookmarksPage.VIEW_THUMBNAILS) { |
| 142 | throw new IllegalArgumentException("Unknown view specified: " + view); |
| 143 | } |
| 144 | mCurrentView = view; |
| 145 | } |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 146 | |
John Reck | 9db9529 | 2011-06-20 13:00:12 -0700 | [diff] [blame] | 147 | public int getViewMode() { |
| 148 | return mCurrentView; |
| 149 | } |
| 150 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 151 | @Override |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 152 | public BrowserBookmarksAdapterItem getRowObject(Cursor c, |
| 153 | BrowserBookmarksAdapterItem item) { |
| 154 | if (item == null) { |
| 155 | item = new BrowserBookmarksAdapterItem(); |
| 156 | } |
| 157 | Bitmap favicon = item.favicon != null ? item.favicon.getBitmap() : null; |
| 158 | Bitmap thumbnail = item.thumbnail != null ? item.thumbnail.getBitmap() : null; |
| 159 | favicon = BrowserBookmarksPage.getBitmap(c, |
| 160 | BookmarksLoader.COLUMN_INDEX_FAVICON, favicon); |
| 161 | thumbnail = BrowserBookmarksPage.getBitmap(c, |
| 162 | BookmarksLoader.COLUMN_INDEX_THUMBNAIL, thumbnail); |
| 163 | if (favicon != null |
| 164 | && (item.favicon == null || item.favicon.getBitmap() != favicon)) { |
| 165 | item.favicon = new BitmapDrawable(mContext.getResources(), favicon); |
| 166 | } |
| 167 | if (thumbnail != null |
| 168 | && (item.thumbnail == null || item.thumbnail.getBitmap() != thumbnail)) { |
| 169 | item.thumbnail = new BitmapDrawable(mContext.getResources(), thumbnail); |
| 170 | } |
| 171 | item.is_folder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; |
| 172 | item.title = getTitle(c); |
| 173 | item.url = c.getString(BookmarksLoader.COLUMN_INDEX_URL); |
| 174 | return item; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public BrowserBookmarksAdapterItem getLoadingObject() { |
| 179 | BrowserBookmarksAdapterItem item = new BrowserBookmarksAdapterItem(); |
| 180 | return item; |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 181 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 182 | } |