blob: c680a88e1bec827752230f20cfbc3203486439a9 [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 Reck8af90642010-11-23 10:27:29 -080030class BrowserBookmarksAdapter extends CursorAdapter {
31 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 Reck8af90642010-11-23 10:27:29 -080040 super(context, null);
41 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) {
Jeff Hamilton84029622010-08-05 14:29:28 -050055 ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
56 TextView tv = (TextView) view.findViewById(R.id.label);
57
Jeff Hamilton84029622010-08-05 14:29:28 -050058 tv.setText(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
Michael Kolbfa314072010-09-17 11:33:58 -070059 if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) {
60 // folder
61 thumb.setImageResource(R.drawable.ic_folder);
The Android Open Source Project0c908882009-03-03 19:32:16 -080062 } else {
John Reck8af90642010-11-23 10:27:29 -080063 byte[] thumbData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_THUMBNAIL);
64 Bitmap thumbBitmap = null;
65 if (thumbData != null) {
66 thumbBitmap = BitmapFactory.decodeByteArray(thumbData, 0, thumbData.length);
Michael Kolbfa314072010-09-17 11:33:58 -070067 }
68
John Reck8af90642010-11-23 10:27:29 -080069 if (thumbBitmap == null) {
Michael Kolbfa314072010-09-17 11:33:58 -070070 thumb.setImageResource(R.drawable.browser_thumbnail);
71 } else {
John Reck8af90642010-11-23 10:27:29 -080072 thumb.setImageBitmap(thumbBitmap);
Michael Kolbfa314072010-09-17 11:33:58 -070073 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080074 }
75 }
John Reck8af90642010-11-23 10:27:29 -080076
77 void bindListView(View view, Context context, Cursor cursor) {
78 ImageView favicon = (ImageView) view.findViewById(R.id.favicon);
79 TextView tv = (TextView) view.findViewById(R.id.label);
80
81 tv.setText(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE));
82 if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) {
83 // folder
John Reck439c9a52010-12-14 10:04:39 -080084 favicon.setImageResource(R.drawable.ic_folder_bookmark_widget_holo_dark);
John Reck8af90642010-11-23 10:27:29 -080085 } else {
86 byte[] faviconData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON);
87 Bitmap faviconBitmap = null;
88 if (faviconData != null) {
89 faviconBitmap = BitmapFactory.decodeByteArray(faviconData, 0, faviconData.length);
90 }
91
92 if (faviconBitmap == null) {
93 favicon.setImageResource(R.drawable.app_web_browser_sm);
94 } else {
95 favicon.setImageBitmap(faviconBitmap);
96 }
97 }
98 }
99
100 @Override
101 public View newView(Context context, Cursor cursor, ViewGroup parent) {
102 if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
103 return mInflater.inflate(R.layout.bookmark_list, parent, false);
104 } else {
105 return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false);
106 }
107 }
108
109 public void selectView(int view) {
110 if (view != BrowserBookmarksPage.VIEW_LIST
111 && view != BrowserBookmarksPage.VIEW_THUMBNAILS) {
112 throw new IllegalArgumentException("Unknown view specified: " + view);
113 }
114 mCurrentView = view;
115 }
John Reck608baa72010-11-24 10:32:28 -0800116
117 @Override
118 public Cursor getItem(int position) {
119 return (Cursor) super.getItem(position);
120 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121}