blob: be3c21126018d320760d79ef31e0b1b366f52c19 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
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
17package com.android.browser;
18
Jeff Hamilton84029622010-08-05 14:29:28 -050019import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.graphics.Bitmap;
John Reckf94abcf2011-10-10 15:33:48 -070022import android.graphics.drawable.BitmapDrawable;
John Reckd18ac4b2012-04-12 17:27:34 -070023import android.provider.BrowserContract.Bookmarks;
John Reck8af90642010-11-23 10:27:29 -080024import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.view.View;
John Reck8af90642010-11-23 10:27:29 -080026import android.view.ViewGroup;
Leon Scroggins892df312009-07-14 14:48:02 -040027import android.widget.ImageView;
John Reck23c61dd2011-09-28 15:02:24 -070028import android.widget.ImageView.ScaleType;
Leon Scroggins892df312009-07-14 14:48:02 -040029import android.widget.TextView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030
John Reckf94abcf2011-10-10 15:33:48 -070031import com.android.browser.util.ThreadedCursorAdapter;
32import com.android.browser.view.BookmarkContainer;
33
34public class BrowserBookmarksAdapter extends
35 ThreadedCursorAdapter<BrowserBookmarksAdapterItem> {
36
John Reck8af90642010-11-23 10:27:29 -080037 LayoutInflater mInflater;
38 int mCurrentView;
John Reckf94abcf2011-10-10 15:33:48 -070039 Context mContext;
John Reck8af90642010-11-23 10:27:29 -080040
The Android Open Source Project0c908882009-03-03 19:32:16 -080041 /**
42 * Create a new BrowserBookmarksAdapter.
The Android Open Source Project0c908882009-03-03 19:32:16 -080043 */
John Reck8af90642010-11-23 10:27:29 -080044 public BrowserBookmarksAdapter(Context context, int defaultView) {
Jeff Hamilton84029622010-08-05 14:29:28 -050045 // Make sure to tell the CursorAdapter to avoid the observer and auto-requery
46 // since the Loader will do that for us.
John Reckf94abcf2011-10-10 15:33:48 -070047 super(context, null);
John Reck8af90642010-11-23 10:27:29 -080048 mInflater = LayoutInflater.from(context);
John Reckf94abcf2011-10-10 15:33:48 -070049 mContext = context;
John Reck8af90642010-11-23 10:27:29 -080050 selectView(defaultView);
The Android Open Source Project0c908882009-03-03 19:32:16 -080051 }
Jeff Hamilton84029622010-08-05 14:29:28 -050052
53 @Override
John Reckf94abcf2011-10-10 15:33:48 -070054 public View newView(Context context, ViewGroup parent) {
John Reck8af90642010-11-23 10:27:29 -080055 if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
John Reckf94abcf2011-10-10 15:33:48 -070056 return mInflater.inflate(R.layout.bookmark_list, parent, false);
John Reck8af90642010-11-23 10:27:29 -080057 } else {
John Reckf94abcf2011-10-10 15:33:48 -070058 return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false);
John Reck8af90642010-11-23 10:27:29 -080059 }
60 }
61
John Reckf94abcf2011-10-10 15:33:48 -070062 @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 Reckd18ac4b2012-04-12 17:27:34 -070085 int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE);
86 switch (type) {
87 case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER:
John Reckf94abcf2011-10-10 15:33:48 -070088 return mContext.getText(R.string.other_bookmarks);
John Reckd18ac4b2012-04-12 17:27:34 -070089 }
90 return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
91 }
92
John Reckf94abcf2011-10-10 15:33:48 -070093 void bindGridView(View view, Context context, BrowserBookmarksAdapterItem item) {
John Reckf3828cd2011-06-14 17:21:55 -070094 // 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 Hamilton84029622010-08-05 14:29:28 -0500100 ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
101 TextView tv = (TextView) view.findViewById(R.id.label);
102
John Reckf94abcf2011-10-10 15:33:48 -0700103 tv.setText(item.title);
104 if (item.is_folder) {
Michael Kolbfa314072010-09-17 11:33:58 -0700105 // folder
John Reckb3417f02011-01-14 11:01:05 -0800106 thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo);
John Reck23c61dd2011-09-28 15:02:24 -0700107 thumb.setScaleType(ScaleType.FIT_END);
John Reckf94abcf2011-10-10 15:33:48 -0700108 thumb.setBackground(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109 } else {
John Reck23c61dd2011-09-28 15:02:24 -0700110 thumb.setScaleType(ScaleType.CENTER_CROP);
John Reckf94abcf2011-10-10 15:33:48 -0700111 if (item.thumbnail == null) {
Michael Kolbfa314072010-09-17 11:33:58 -0700112 thumb.setImageResource(R.drawable.browser_thumbnail);
113 } else {
John Reckf94abcf2011-10-10 15:33:48 -0700114 thumb.setImageDrawable(item.thumbnail);
Michael Kolbfa314072010-09-17 11:33:58 -0700115 }
John Reckb3417f02011-01-14 11:01:05 -0800116 thumb.setBackgroundResource(R.drawable.border_thumb_bookmarks_widget_holo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117 }
118 }
John Reck8af90642010-11-23 10:27:29 -0800119
John Reckf94abcf2011-10-10 15:33:48 -0700120 void bindListView(View view, Context context, BrowserBookmarksAdapterItem item) {
John Reck8af90642010-11-23 10:27:29 -0800121 ImageView favicon = (ImageView) view.findViewById(R.id.favicon);
122 TextView tv = (TextView) view.findViewById(R.id.label);
123
John Reckf94abcf2011-10-10 15:33:48 -0700124 tv.setText(item.title);
125 if (item.is_folder) {
John Reck8af90642010-11-23 10:27:29 -0800126 // folder
Michael Kolb5a72f182011-01-13 20:35:06 -0800127 favicon.setImageResource(R.drawable.ic_folder_holo_dark);
John Reckf94abcf2011-10-10 15:33:48 -0700128 favicon.setBackground(null);
John Reck8af90642010-11-23 10:27:29 -0800129 } else {
John Reckf94abcf2011-10-10 15:33:48 -0700130 if (item.favicon == null) {
John Reck8af90642010-11-23 10:27:29 -0800131 favicon.setImageResource(R.drawable.app_web_browser_sm);
132 } else {
John Reckf94abcf2011-10-10 15:33:48 -0700133 favicon.setImageDrawable(item.favicon);
John Reck8af90642010-11-23 10:27:29 -0800134 }
John Reck18e93f72011-03-21 11:46:09 -0700135 favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg);
John Reck8af90642010-11-23 10:27:29 -0800136 }
137 }
138
John Reck8af90642010-11-23 10:27:29 -0800139 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 Reck608baa72010-11-24 10:32:28 -0800146
John Reck9db95292011-06-20 13:00:12 -0700147 public int getViewMode() {
148 return mCurrentView;
149 }
150
John Reck608baa72010-11-24 10:32:28 -0800151 @Override
John Reckf94abcf2011-10-10 15:33:48 -0700152 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 Reck608baa72010-11-24 10:32:28 -0800181 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182}