blob: 2fd880c097f5840c6707bd234241bffa44767f58 [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;
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 Reck71efc2b2011-05-09 16:54:28 -070031public class BrowserBookmarksAdapter extends CursorAdapter {
John Reck8af90642010-11-23 10:27:29 -080032 LayoutInflater mInflater;
33 int mCurrentView;
34
The Android Open Source Project0c908882009-03-03 19:32:16 -080035 /**
36 * Create a new BrowserBookmarksAdapter.
The Android Open Source Project0c908882009-03-03 19:32:16 -080037 */
John Reck8af90642010-11-23 10:27:29 -080038 public BrowserBookmarksAdapter(Context context, int defaultView) {
Jeff Hamilton84029622010-08-05 14:29:28 -050039 // Make sure to tell the CursorAdapter to avoid the observer and auto-requery
40 // since the Loader will do that for us.
John Reckb3417f02011-01-14 11:01:05 -080041 super(context, null, 0);
John Reck8af90642010-11-23 10:27:29 -080042 mInflater = LayoutInflater.from(context);
43 selectView(defaultView);
The Android Open Source Project0c908882009-03-03 19:32:16 -080044 }
Jeff Hamilton84029622010-08-05 14:29:28 -050045
46 @Override
47 public void bindView(View view, Context context, Cursor cursor) {
John Reck8af90642010-11-23 10:27:29 -080048 if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
49 bindListView(view, context, cursor);
50 } else {
51 bindGridView(view, context, cursor);
52 }
53 }
54
55 void bindGridView(View view, Context context, Cursor cursor) {
John Reckf3828cd2011-06-14 17:21:55 -070056 // We need to set this to handle rotation and other configuration change
57 // events. If the padding didn't change, this is a no op.
58 int padding = context.getResources()
59 .getDimensionPixelSize(R.dimen.combo_horizontalSpacing);
60 view.setPadding(padding, view.getPaddingTop(),
61 padding, view.getPaddingBottom());
Jeff Hamilton84029622010-08-05 14:29:28 -050062 ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
63 TextView tv = (TextView) view.findViewById(R.id.label);
64
Jeff Hamilton84029622010-08-05 14:29:28 -050065 tv.setText(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
Michael Kolbfa314072010-09-17 11:33:58 -070066 if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) {
67 // folder
John Reckb3417f02011-01-14 11:01:05 -080068 thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo);
John Reck23c61dd2011-09-28 15:02:24 -070069 thumb.setScaleType(ScaleType.FIT_END);
John Reckb3417f02011-01-14 11:01:05 -080070 thumb.setBackgroundDrawable(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -080071 } else {
John Reck8af90642010-11-23 10:27:29 -080072 byte[] thumbData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_THUMBNAIL);
73 Bitmap thumbBitmap = null;
74 if (thumbData != null) {
75 thumbBitmap = BitmapFactory.decodeByteArray(thumbData, 0, thumbData.length);
Michael Kolbfa314072010-09-17 11:33:58 -070076 }
77
John Reck23c61dd2011-09-28 15:02:24 -070078 thumb.setScaleType(ScaleType.CENTER_CROP);
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
Michael Kolb5a72f182011-01-13 20:35:06 -080095 favicon.setImageResource(R.drawable.ic_folder_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 Reck18e93f72011-03-21 11:46:09 -0700109 favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg);
John Reck8af90642010-11-23 10:27:29 -0800110 }
111 }
112
113 @Override
114 public View newView(Context context, Cursor cursor, ViewGroup parent) {
115 if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
116 return mInflater.inflate(R.layout.bookmark_list, parent, false);
117 } else {
118 return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false);
119 }
120 }
121
122 public void selectView(int view) {
123 if (view != BrowserBookmarksPage.VIEW_LIST
124 && view != BrowserBookmarksPage.VIEW_THUMBNAILS) {
125 throw new IllegalArgumentException("Unknown view specified: " + view);
126 }
127 mCurrentView = view;
128 }
John Reck608baa72010-11-24 10:32:28 -0800129
John Reck9db95292011-06-20 13:00:12 -0700130 public int getViewMode() {
131 return mCurrentView;
132 }
133
John Reck608baa72010-11-24 10:32:28 -0800134 @Override
135 public Cursor getItem(int position) {
136 return (Cursor) super.getItem(position);
137 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800138}