blob: b3f8d985841f378195899ededb0e334ef3641d19 [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 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;
26import android.widget.CursorAdapter;
Leon Scroggins892df312009-07-14 14:48:02 -040027import android.widget.ImageView;
28import android.widget.TextView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029
John Reck71efc2b2011-05-09 16:54:28 -070030public class BrowserBookmarksAdapter extends CursorAdapter {
John Reck8af90642010-11-23 10:27:29 -080031 LayoutInflater mInflater;
32 int mCurrentView;
33
The Android Open Source Project0c908882009-03-03 19:32:16 -080034 /**
35 * Create a new BrowserBookmarksAdapter.
The Android Open Source Project0c908882009-03-03 19:32:16 -080036 */
John Reck8af90642010-11-23 10:27:29 -080037 public BrowserBookmarksAdapter(Context context, int defaultView) {
Jeff Hamilton84029622010-08-05 14:29:28 -050038 // Make sure to tell the CursorAdapter to avoid the observer and auto-requery
39 // since the Loader will do that for us.
John Reckb3417f02011-01-14 11:01:05 -080040 super(context, null, 0);
John Reck8af90642010-11-23 10:27:29 -080041 mInflater = LayoutInflater.from(context);
42 selectView(defaultView);
The Android Open Source Project0c908882009-03-03 19:32:16 -080043 }
Jeff Hamilton84029622010-08-05 14:29:28 -050044
45 @Override
46 public void bindView(View view, Context context, Cursor cursor) {
John Reck8af90642010-11-23 10:27:29 -080047 if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
48 bindListView(view, context, cursor);
49 } else {
50 bindGridView(view, context, cursor);
51 }
52 }
53
54 void bindGridView(View view, Context context, Cursor cursor) {
John Reckf3828cd2011-06-14 17:21:55 -070055 // We need to set this to handle rotation and other configuration change
56 // events. If the padding didn't change, this is a no op.
57 int padding = context.getResources()
58 .getDimensionPixelSize(R.dimen.combo_horizontalSpacing);
59 view.setPadding(padding, view.getPaddingTop(),
60 padding, view.getPaddingBottom());
Jeff Hamilton84029622010-08-05 14:29:28 -050061 ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
62 TextView tv = (TextView) view.findViewById(R.id.label);
63
Jeff Hamilton84029622010-08-05 14:29:28 -050064 tv.setText(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
Michael Kolbfa314072010-09-17 11:33:58 -070065 if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) {
66 // folder
John Reckb3417f02011-01-14 11:01:05 -080067 thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo);
68 thumb.setBackgroundDrawable(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -080069 } else {
John Reck8af90642010-11-23 10:27:29 -080070 byte[] thumbData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_THUMBNAIL);
71 Bitmap thumbBitmap = null;
72 if (thumbData != null) {
73 thumbBitmap = BitmapFactory.decodeByteArray(thumbData, 0, thumbData.length);
Michael Kolbfa314072010-09-17 11:33:58 -070074 }
75
John Reck8af90642010-11-23 10:27:29 -080076 if (thumbBitmap == null) {
Michael Kolbfa314072010-09-17 11:33:58 -070077 thumb.setImageResource(R.drawable.browser_thumbnail);
78 } else {
John Reck8af90642010-11-23 10:27:29 -080079 thumb.setImageBitmap(thumbBitmap);
Michael Kolbfa314072010-09-17 11:33:58 -070080 }
John Reckb3417f02011-01-14 11:01:05 -080081 thumb.setBackgroundResource(R.drawable.border_thumb_bookmarks_widget_holo);
The Android Open Source Project0c908882009-03-03 19:32:16 -080082 }
83 }
John Reck8af90642010-11-23 10:27:29 -080084
85 void bindListView(View view, Context context, Cursor cursor) {
86 ImageView favicon = (ImageView) view.findViewById(R.id.favicon);
87 TextView tv = (TextView) view.findViewById(R.id.label);
88
89 tv.setText(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
90 if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) {
91 // folder
Michael Kolb5a72f182011-01-13 20:35:06 -080092 favicon.setImageResource(R.drawable.ic_folder_holo_dark);
John Reckb3417f02011-01-14 11:01:05 -080093 favicon.setBackgroundDrawable(null);
John Reck8af90642010-11-23 10:27:29 -080094 } else {
95 byte[] faviconData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON);
96 Bitmap faviconBitmap = null;
97 if (faviconData != null) {
98 faviconBitmap = BitmapFactory.decodeByteArray(faviconData, 0, faviconData.length);
99 }
100
101 if (faviconBitmap == null) {
102 favicon.setImageResource(R.drawable.app_web_browser_sm);
103 } else {
104 favicon.setImageBitmap(faviconBitmap);
105 }
John Reck18e93f72011-03-21 11:46:09 -0700106 favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg);
John Reck8af90642010-11-23 10:27:29 -0800107 }
108 }
109
110 @Override
111 public View newView(Context context, Cursor cursor, ViewGroup parent) {
112 if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
113 return mInflater.inflate(R.layout.bookmark_list, parent, false);
114 } else {
115 return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false);
116 }
117 }
118
119 public void selectView(int view) {
120 if (view != BrowserBookmarksPage.VIEW_LIST
121 && view != BrowserBookmarksPage.VIEW_THUMBNAILS) {
122 throw new IllegalArgumentException("Unknown view specified: " + view);
123 }
124 mCurrentView = view;
125 }
John Reck608baa72010-11-24 10:32:28 -0800126
John Reck9db95292011-06-20 13:00:12 -0700127 public int getViewMode() {
128 return mCurrentView;
129 }
130
John Reck608baa72010-11-24 10:32:28 -0800131 @Override
132 public Cursor getItem(int position) {
133 return (Cursor) super.getItem(position);
134 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800135}