blob: 26ba62cc0b34be7131be4f33fdf91f172699b1cb [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;
22import android.graphics.BitmapFactory;
John Reckb3417f02011-01-14 11:01:05 -080023import android.graphics.drawable.PaintDrawable;
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;
27import android.widget.CursorAdapter;
Leon Scroggins892df312009-07-14 14:48:02 -040028import android.widget.ImageView;
29import android.widget.TextView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030
John Reck8af90642010-11-23 10:27:29 -080031class BrowserBookmarksAdapter extends CursorAdapter {
32 LayoutInflater mInflater;
33 int mCurrentView;
John Reckb3417f02011-01-14 11:01:05 -080034 PaintDrawable mFaviconBackground;
John Reck8af90642010-11-23 10:27:29 -080035
The Android Open Source Project0c908882009-03-03 19:32:16 -080036 /**
37 * Create a new BrowserBookmarksAdapter.
The Android Open Source Project0c908882009-03-03 19:32:16 -080038 */
John Reck8af90642010-11-23 10:27:29 -080039 public BrowserBookmarksAdapter(Context context, int defaultView) {
Jeff Hamilton84029622010-08-05 14:29:28 -050040 // Make sure to tell the CursorAdapter to avoid the observer and auto-requery
41 // since the Loader will do that for us.
John Reckb3417f02011-01-14 11:01:05 -080042 super(context, null, 0);
John Reck8af90642010-11-23 10:27:29 -080043 mInflater = LayoutInflater.from(context);
44 selectView(defaultView);
John Reckb3417f02011-01-14 11:01:05 -080045 float density = context.getResources().getDisplayMetrics().density;
46 mFaviconBackground = new PaintDrawable();
47 int padding = (int) (5 * density);
48 mFaviconBackground.setPadding(padding, padding, padding, padding);
49 mFaviconBackground.getPaint().setColor(context.getResources()
50 .getColor(R.color.bookmarkListFaviconBackground));
51 mFaviconBackground.setCornerRadius(3 * density);
The Android Open Source Project0c908882009-03-03 19:32:16 -080052 }
Jeff Hamilton84029622010-08-05 14:29:28 -050053
54 @Override
55 public void bindView(View view, Context context, Cursor cursor) {
John Reck8af90642010-11-23 10:27:29 -080056 if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
57 bindListView(view, context, cursor);
58 } else {
59 bindGridView(view, context, cursor);
60 }
61 }
62
63 void bindGridView(View view, Context context, Cursor cursor) {
Jeff Hamilton84029622010-08-05 14:29:28 -050064 ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
65 TextView tv = (TextView) view.findViewById(R.id.label);
66
Jeff Hamilton84029622010-08-05 14:29:28 -050067 tv.setText(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
Michael Kolbfa314072010-09-17 11:33:58 -070068 if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) {
69 // folder
John Reckb3417f02011-01-14 11:01:05 -080070 thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo);
71 thumb.setBackgroundDrawable(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -080072 } else {
John Reck8af90642010-11-23 10:27:29 -080073 byte[] thumbData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_THUMBNAIL);
74 Bitmap thumbBitmap = null;
75 if (thumbData != null) {
76 thumbBitmap = BitmapFactory.decodeByteArray(thumbData, 0, thumbData.length);
Michael Kolbfa314072010-09-17 11:33:58 -070077 }
78
John Reck8af90642010-11-23 10:27:29 -080079 if (thumbBitmap == null) {
Michael Kolbfa314072010-09-17 11:33:58 -070080 thumb.setImageResource(R.drawable.browser_thumbnail);
81 } else {
John Reck8af90642010-11-23 10:27:29 -080082 thumb.setImageBitmap(thumbBitmap);
Michael Kolbfa314072010-09-17 11:33:58 -070083 }
John Reckb3417f02011-01-14 11:01:05 -080084 thumb.setBackgroundResource(R.drawable.border_thumb_bookmarks_widget_holo);
The Android Open Source Project0c908882009-03-03 19:32:16 -080085 }
86 }
John Reck8af90642010-11-23 10:27:29 -080087
88 void bindListView(View view, Context context, Cursor cursor) {
89 ImageView favicon = (ImageView) view.findViewById(R.id.favicon);
90 TextView tv = (TextView) view.findViewById(R.id.label);
91
92 tv.setText(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
93 if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) {
94 // folder
John Reck439c9a52010-12-14 10:04:39 -080095 favicon.setImageResource(R.drawable.ic_folder_bookmark_widget_holo_dark);
John Reckb3417f02011-01-14 11:01:05 -080096 favicon.setBackgroundDrawable(null);
John Reck8af90642010-11-23 10:27:29 -080097 } else {
98 byte[] faviconData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON);
99 Bitmap faviconBitmap = null;
100 if (faviconData != null) {
101 faviconBitmap = BitmapFactory.decodeByteArray(faviconData, 0, faviconData.length);
102 }
103
104 if (faviconBitmap == null) {
105 favicon.setImageResource(R.drawable.app_web_browser_sm);
106 } else {
107 favicon.setImageBitmap(faviconBitmap);
108 }
John Reckb3417f02011-01-14 11:01:05 -0800109 //favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg);
110 // TODO: Switch to above instead of below once b/3353813 is fixed
111 favicon.setBackgroundDrawable(mFaviconBackground);
John Reck8af90642010-11-23 10:27:29 -0800112 }
113 }
114
115 @Override
116 public View newView(Context context, Cursor cursor, ViewGroup parent) {
117 if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
118 return mInflater.inflate(R.layout.bookmark_list, parent, false);
119 } else {
120 return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false);
121 }
122 }
123
124 public void selectView(int view) {
125 if (view != BrowserBookmarksPage.VIEW_LIST
126 && view != BrowserBookmarksPage.VIEW_THUMBNAILS) {
127 throw new IllegalArgumentException("Unknown view specified: " + view);
128 }
129 mCurrentView = view;
130 }
John Reck608baa72010-11-24 10:32:28 -0800131
132 @Override
133 public Cursor getItem(int position) {
134 return (Cursor) super.getItem(position);
135 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800136}