blob: fcc3f276003fa7e8fba43d236fecd35c6b3d4a10 [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 Reckd18ac4b2012-04-12 17:27:34 -070023import android.provider.BrowserContract.Bookmarks;
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;
John Reck23c61dd2011-09-28 15:02:24 -070029import android.widget.ImageView.ScaleType;
Leon Scroggins892df312009-07-14 14:48:02 -040030import android.widget.TextView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031
John Reck71efc2b2011-05-09 16:54:28 -070032public class BrowserBookmarksAdapter extends CursorAdapter {
John Reck8af90642010-11-23 10:27:29 -080033 LayoutInflater mInflater;
34 int mCurrentView;
35
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);
The Android Open Source Project0c908882009-03-03 19:32:16 -080045 }
Jeff Hamilton84029622010-08-05 14:29:28 -050046
47 @Override
48 public void bindView(View view, Context context, Cursor cursor) {
John Reck8af90642010-11-23 10:27:29 -080049 if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
50 bindListView(view, context, cursor);
51 } else {
52 bindGridView(view, context, cursor);
53 }
54 }
55
John Reckd18ac4b2012-04-12 17:27:34 -070056 CharSequence getTitle(Cursor cursor, Context context) {
57 int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE);
58 switch (type) {
59 case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER:
60 return context.getText(R.string.other_bookmarks);
61 }
62 return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
63 }
64
John Reck8af90642010-11-23 10:27:29 -080065 void bindGridView(View view, Context context, Cursor cursor) {
John Reckf3828cd2011-06-14 17:21:55 -070066 // We need to set this to handle rotation and other configuration change
67 // events. If the padding didn't change, this is a no op.
68 int padding = context.getResources()
69 .getDimensionPixelSize(R.dimen.combo_horizontalSpacing);
70 view.setPadding(padding, view.getPaddingTop(),
71 padding, view.getPaddingBottom());
Jeff Hamilton84029622010-08-05 14:29:28 -050072 ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
73 TextView tv = (TextView) view.findViewById(R.id.label);
74
John Reckd18ac4b2012-04-12 17:27:34 -070075 tv.setText(getTitle(cursor, context));
Michael Kolbfa314072010-09-17 11:33:58 -070076 if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) {
77 // folder
John Reckb3417f02011-01-14 11:01:05 -080078 thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo);
John Reck23c61dd2011-09-28 15:02:24 -070079 thumb.setScaleType(ScaleType.FIT_END);
John Reckb3417f02011-01-14 11:01:05 -080080 thumb.setBackgroundDrawable(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -080081 } else {
John Reck8af90642010-11-23 10:27:29 -080082 byte[] thumbData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_THUMBNAIL);
83 Bitmap thumbBitmap = null;
84 if (thumbData != null) {
85 thumbBitmap = BitmapFactory.decodeByteArray(thumbData, 0, thumbData.length);
Michael Kolbfa314072010-09-17 11:33:58 -070086 }
87
John Reck23c61dd2011-09-28 15:02:24 -070088 thumb.setScaleType(ScaleType.CENTER_CROP);
John Reck8af90642010-11-23 10:27:29 -080089 if (thumbBitmap == null) {
Michael Kolbfa314072010-09-17 11:33:58 -070090 thumb.setImageResource(R.drawable.browser_thumbnail);
91 } else {
John Reck8af90642010-11-23 10:27:29 -080092 thumb.setImageBitmap(thumbBitmap);
Michael Kolbfa314072010-09-17 11:33:58 -070093 }
John Reckb3417f02011-01-14 11:01:05 -080094 thumb.setBackgroundResource(R.drawable.border_thumb_bookmarks_widget_holo);
The Android Open Source Project0c908882009-03-03 19:32:16 -080095 }
96 }
John Reck8af90642010-11-23 10:27:29 -080097
98 void bindListView(View view, Context context, Cursor cursor) {
99 ImageView favicon = (ImageView) view.findViewById(R.id.favicon);
100 TextView tv = (TextView) view.findViewById(R.id.label);
101
John Reckd18ac4b2012-04-12 17:27:34 -0700102 tv.setText(getTitle(cursor, context));
John Reck8af90642010-11-23 10:27:29 -0800103 if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0) {
104 // folder
Michael Kolb5a72f182011-01-13 20:35:06 -0800105 favicon.setImageResource(R.drawable.ic_folder_holo_dark);
John Reckb3417f02011-01-14 11:01:05 -0800106 favicon.setBackgroundDrawable(null);
John Reck8af90642010-11-23 10:27:29 -0800107 } else {
108 byte[] faviconData = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON);
109 Bitmap faviconBitmap = null;
110 if (faviconData != null) {
111 faviconBitmap = BitmapFactory.decodeByteArray(faviconData, 0, faviconData.length);
112 }
113
114 if (faviconBitmap == null) {
115 favicon.setImageResource(R.drawable.app_web_browser_sm);
116 } else {
117 favicon.setImageBitmap(faviconBitmap);
118 }
John Reck18e93f72011-03-21 11:46:09 -0700119 favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg);
John Reck8af90642010-11-23 10:27:29 -0800120 }
121 }
122
123 @Override
124 public View newView(Context context, Cursor cursor, ViewGroup parent) {
125 if (mCurrentView == BrowserBookmarksPage.VIEW_LIST) {
126 return mInflater.inflate(R.layout.bookmark_list, parent, false);
127 } else {
128 return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false);
129 }
130 }
131
132 public void selectView(int view) {
133 if (view != BrowserBookmarksPage.VIEW_LIST
134 && view != BrowserBookmarksPage.VIEW_THUMBNAILS) {
135 throw new IllegalArgumentException("Unknown view specified: " + view);
136 }
137 mCurrentView = view;
138 }
John Reck608baa72010-11-24 10:32:28 -0800139
John Reck9db95292011-06-20 13:00:12 -0700140 public int getViewMode() {
141 return mCurrentView;
142 }
143
John Reck608baa72010-11-24 10:32:28 -0800144 @Override
145 public Cursor getItem(int position) {
146 return (Cursor) super.getItem(position);
147 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148}