blob: 3b38f1eb6fa3aa5f10ed1c10a92adffd21bb969d [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
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 Reck8af90642010-11-23 10:27:29 -080023import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.view.View;
John Reck8af90642010-11-23 10:27:29 -080025import android.view.ViewGroup;
Leon Scroggins892df312009-07-14 14:48:02 -040026import android.widget.ImageView;
John Reck23c61dd2011-09-28 15:02:24 -070027import android.widget.ImageView.ScaleType;
Leon Scroggins892df312009-07-14 14:48:02 -040028import android.widget.TextView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029
Bijan Amirzada41242f22014-03-21 12:12:18 -070030import com.android.browser.R;
31import com.android.browser.platformsupport.BrowserContract.Bookmarks;
32import com.android.browser.util.ThreadedCursorAdapter;
33import com.android.browser.view.BookmarkContainer;
John Reckf94abcf2011-10-10 15:33:48 -070034
35public class BrowserBookmarksAdapter extends
36 ThreadedCursorAdapter<BrowserBookmarksAdapterItem> {
37
John Reck8af90642010-11-23 10:27:29 -080038 LayoutInflater mInflater;
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 Reckaf991f92012-04-24 15:31:16 -070044 public BrowserBookmarksAdapter(Context context) {
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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050 }
Jeff Hamilton84029622010-08-05 14:29:28 -050051
52 @Override
John Reck9ee87d92012-05-04 13:53:25 -070053 protected long getItemId(Cursor c) {
54 return c.getLong(BookmarksLoader.COLUMN_INDEX_ID);
55 }
56
57 @Override
John Reckf94abcf2011-10-10 15:33:48 -070058 public View newView(Context context, ViewGroup parent) {
John Reckaf991f92012-04-24 15:31:16 -070059 return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false);
John Reck8af90642010-11-23 10:27:29 -080060 }
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);
John Reckaf991f92012-04-24 15:31:16 -070066 bindGridView(view, mContext, object);
John Reckf94abcf2011-10-10 15:33:48 -070067 container.setIgnoreRequestLayout(false);
68 }
69
John Reckf94abcf2011-10-10 15:33:48 -070070 CharSequence getTitle(Cursor cursor) {
John Reckd18ac4b2012-04-12 17:27:34 -070071 int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE);
72 switch (type) {
73 case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER:
John Reckf94abcf2011-10-10 15:33:48 -070074 return mContext.getText(R.string.other_bookmarks);
John Reckd18ac4b2012-04-12 17:27:34 -070075 }
76 return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
77 }
78
John Reckf94abcf2011-10-10 15:33:48 -070079 void bindGridView(View view, Context context, BrowserBookmarksAdapterItem item) {
John Reckf3828cd2011-06-14 17:21:55 -070080 // We need to set this to handle rotation and other configuration change
81 // events. If the padding didn't change, this is a no op.
82 int padding = context.getResources()
83 .getDimensionPixelSize(R.dimen.combo_horizontalSpacing);
84 view.setPadding(padding, view.getPaddingTop(),
85 padding, view.getPaddingBottom());
Jeff Hamilton84029622010-08-05 14:29:28 -050086 ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
87 TextView tv = (TextView) view.findViewById(R.id.label);
88
John Reckf94abcf2011-10-10 15:33:48 -070089 tv.setText(item.title);
90 if (item.is_folder) {
Michael Kolbfa314072010-09-17 11:33:58 -070091 // folder
John Reckb3417f02011-01-14 11:01:05 -080092 thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo);
John Reck23c61dd2011-09-28 15:02:24 -070093 thumb.setScaleType(ScaleType.FIT_END);
John Reckf94abcf2011-10-10 15:33:48 -070094 thumb.setBackground(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -080095 } else {
John Reck23c61dd2011-09-28 15:02:24 -070096 thumb.setScaleType(ScaleType.CENTER_CROP);
John Reck1df6df72012-06-05 11:05:34 -070097 if (item.thumbnail == null || !item.has_thumbnail) {
Michael Kolbfa314072010-09-17 11:33:58 -070098 thumb.setImageResource(R.drawable.browser_thumbnail);
99 } else {
John Reckf94abcf2011-10-10 15:33:48 -0700100 thumb.setImageDrawable(item.thumbnail);
Michael Kolbfa314072010-09-17 11:33:58 -0700101 }
John Reckb3417f02011-01-14 11:01:05 -0800102 thumb.setBackgroundResource(R.drawable.border_thumb_bookmarks_widget_holo);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800103 }
104 }
John Reck8af90642010-11-23 10:27:29 -0800105
John Reck608baa72010-11-24 10:32:28 -0800106 @Override
John Reckf94abcf2011-10-10 15:33:48 -0700107 public BrowserBookmarksAdapterItem getRowObject(Cursor c,
108 BrowserBookmarksAdapterItem item) {
109 if (item == null) {
110 item = new BrowserBookmarksAdapterItem();
111 }
John Reckf94abcf2011-10-10 15:33:48 -0700112 Bitmap thumbnail = item.thumbnail != null ? item.thumbnail.getBitmap() : null;
John Reckf94abcf2011-10-10 15:33:48 -0700113 thumbnail = BrowserBookmarksPage.getBitmap(c,
114 BookmarksLoader.COLUMN_INDEX_THUMBNAIL, thumbnail);
John Reck1df6df72012-06-05 11:05:34 -0700115 item.has_thumbnail = thumbnail != null;
John Reckf94abcf2011-10-10 15:33:48 -0700116 if (thumbnail != null
117 && (item.thumbnail == null || item.thumbnail.getBitmap() != thumbnail)) {
118 item.thumbnail = new BitmapDrawable(mContext.getResources(), thumbnail);
119 }
120 item.is_folder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
121 item.title = getTitle(c);
122 item.url = c.getString(BookmarksLoader.COLUMN_INDEX_URL);
123 return item;
124 }
125
126 @Override
127 public BrowserBookmarksAdapterItem getLoadingObject() {
128 BrowserBookmarksAdapterItem item = new BrowserBookmarksAdapterItem();
129 return item;
John Reck608baa72010-11-24 10:32:28 -0800130 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800131}