blob: 93cf5d9f89b6dfbf21c553146a62540f4fe166ed [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;
John Reckf94abcf2011-10-10 15:33:48 -070038 Context mContext;
John Reck8af90642010-11-23 10:27:29 -080039
The Android Open Source Project0c908882009-03-03 19:32:16 -080040 /**
41 * Create a new BrowserBookmarksAdapter.
The Android Open Source Project0c908882009-03-03 19:32:16 -080042 */
John Reckaf991f92012-04-24 15:31:16 -070043 public BrowserBookmarksAdapter(Context context) {
Jeff Hamilton84029622010-08-05 14:29:28 -050044 // Make sure to tell the CursorAdapter to avoid the observer and auto-requery
45 // since the Loader will do that for us.
John Reckf94abcf2011-10-10 15:33:48 -070046 super(context, null);
John Reck8af90642010-11-23 10:27:29 -080047 mInflater = LayoutInflater.from(context);
John Reckf94abcf2011-10-10 15:33:48 -070048 mContext = context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049 }
Jeff Hamilton84029622010-08-05 14:29:28 -050050
51 @Override
John Reckf94abcf2011-10-10 15:33:48 -070052 public View newView(Context context, ViewGroup parent) {
John Reckaf991f92012-04-24 15:31:16 -070053 return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false);
John Reck8af90642010-11-23 10:27:29 -080054 }
55
John Reckf94abcf2011-10-10 15:33:48 -070056 @Override
57 public void bindView(View view, BrowserBookmarksAdapterItem object) {
58 BookmarkContainer container = (BookmarkContainer) view;
59 container.setIgnoreRequestLayout(true);
John Reckaf991f92012-04-24 15:31:16 -070060 bindGridView(view, mContext, object);
John Reckf94abcf2011-10-10 15:33:48 -070061 container.setIgnoreRequestLayout(false);
62 }
63
John Reckf94abcf2011-10-10 15:33:48 -070064 CharSequence getTitle(Cursor cursor) {
John Reckd18ac4b2012-04-12 17:27:34 -070065 int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE);
66 switch (type) {
67 case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER:
John Reckf94abcf2011-10-10 15:33:48 -070068 return mContext.getText(R.string.other_bookmarks);
John Reckd18ac4b2012-04-12 17:27:34 -070069 }
70 return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
71 }
72
John Reckf94abcf2011-10-10 15:33:48 -070073 void bindGridView(View view, Context context, BrowserBookmarksAdapterItem item) {
John Reckf3828cd2011-06-14 17:21:55 -070074 // We need to set this to handle rotation and other configuration change
75 // events. If the padding didn't change, this is a no op.
76 int padding = context.getResources()
77 .getDimensionPixelSize(R.dimen.combo_horizontalSpacing);
78 view.setPadding(padding, view.getPaddingTop(),
79 padding, view.getPaddingBottom());
Jeff Hamilton84029622010-08-05 14:29:28 -050080 ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
81 TextView tv = (TextView) view.findViewById(R.id.label);
82
John Reckf94abcf2011-10-10 15:33:48 -070083 tv.setText(item.title);
84 if (item.is_folder) {
Michael Kolbfa314072010-09-17 11:33:58 -070085 // folder
John Reckb3417f02011-01-14 11:01:05 -080086 thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo);
John Reck23c61dd2011-09-28 15:02:24 -070087 thumb.setScaleType(ScaleType.FIT_END);
John Reckf94abcf2011-10-10 15:33:48 -070088 thumb.setBackground(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -080089 } else {
John Reck23c61dd2011-09-28 15:02:24 -070090 thumb.setScaleType(ScaleType.CENTER_CROP);
John Reckf94abcf2011-10-10 15:33:48 -070091 if (item.thumbnail == null) {
Michael Kolbfa314072010-09-17 11:33:58 -070092 thumb.setImageResource(R.drawable.browser_thumbnail);
93 } else {
John Reckf94abcf2011-10-10 15:33:48 -070094 thumb.setImageDrawable(item.thumbnail);
Michael Kolbfa314072010-09-17 11:33:58 -070095 }
John Reckb3417f02011-01-14 11:01:05 -080096 thumb.setBackgroundResource(R.drawable.border_thumb_bookmarks_widget_holo);
The Android Open Source Project0c908882009-03-03 19:32:16 -080097 }
98 }
John Reck8af90642010-11-23 10:27:29 -080099
John Reck608baa72010-11-24 10:32:28 -0800100 @Override
John Reckf94abcf2011-10-10 15:33:48 -0700101 public BrowserBookmarksAdapterItem getRowObject(Cursor c,
102 BrowserBookmarksAdapterItem item) {
103 if (item == null) {
104 item = new BrowserBookmarksAdapterItem();
105 }
John Reckf94abcf2011-10-10 15:33:48 -0700106 Bitmap thumbnail = item.thumbnail != null ? item.thumbnail.getBitmap() : null;
John Reckf94abcf2011-10-10 15:33:48 -0700107 thumbnail = BrowserBookmarksPage.getBitmap(c,
108 BookmarksLoader.COLUMN_INDEX_THUMBNAIL, thumbnail);
John Reckf94abcf2011-10-10 15:33:48 -0700109 if (thumbnail != null
110 && (item.thumbnail == null || item.thumbnail.getBitmap() != thumbnail)) {
111 item.thumbnail = new BitmapDrawable(mContext.getResources(), thumbnail);
112 }
113 item.is_folder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
114 item.title = getTitle(c);
115 item.url = c.getString(BookmarksLoader.COLUMN_INDEX_URL);
116 return item;
117 }
118
119 @Override
120 public BrowserBookmarksAdapterItem getLoadingObject() {
121 BrowserBookmarksAdapterItem item = new BrowserBookmarksAdapterItem();
122 return item;
John Reck608baa72010-11-24 10:32:28 -0800123 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124}