blob: a9db7ac06272ae4f10e6c83aecd5effaf6b92958 [file] [log] [blame]
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -04001/*
2 * Copyright (C) 2009 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
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.database.Cursor;
23import android.database.ContentObserver;
24import android.database.DataSetObserver;
25import android.graphics.BitmapFactory;
26import android.os.Bundle;
27import android.os.Handler;
28import android.provider.Browser;
29import android.provider.Browser.BookmarkColumns;
30import android.view.LayoutInflater;
31import android.view.View;
32import android.view.ViewGroup;
33import android.widget.AdapterView;
34import android.widget.GridView;
35import android.widget.ImageView;
36import android.widget.ListAdapter;
37import android.widget.TextView;
38
39import java.util.ArrayList;
40
41public class BookmarkGridPage extends Activity {
42 private final static int SPACING = 10;
43 private BookmarkGrid mGridView;
44 private BookmarkGridAdapter mAdapter;
45
46 @Override
47 public void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
49
50 mGridView = new BookmarkGrid(this);
51 mGridView.setNumColumns(3);
52 mAdapter = new BookmarkGridAdapter(this);
53 mGridView.setAdapter(mAdapter);
54 mGridView.setFocusable(true);
55 mGridView.setFocusableInTouchMode(true);
56 mGridView.setSelector(android.R.drawable.gallery_thumb);
57 mGridView.setVerticalSpacing(SPACING);
58 mGridView.setHorizontalSpacing(SPACING);
59 setContentView(mGridView);
60 mGridView.requestFocus();
61 }
62
63 private class BookmarkGrid extends GridView {
64 public BookmarkGrid(Context context) {
65 super(context);
66 }
67 @Override
68 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
69 int thumbHeight = (h - 2 * (SPACING + getListPaddingTop()
70 + getListPaddingBottom())) / 3;
71 mAdapter.heightChanged(thumbHeight);
72 super.onSizeChanged(w, h, oldw, oldh);
73 }
74 }
75
76 private class BookmarkGridAdapter implements ListAdapter {
77 private ArrayList<DataSetObserver> mDataObservers;
78 private Context mContext; // Context to use to inflate views
79 private Cursor mCursor;
80 private int mThumbHeight;
81
82 public BookmarkGridAdapter(Context context) {
83 mContext = context;
84 mDataObservers = new ArrayList<DataSetObserver>();
85 String whereClause = Browser.BookmarkColumns.BOOKMARK + " != 0";
86 String orderBy = Browser.BookmarkColumns.VISITS + " DESC";
87 mCursor = managedQuery(Browser.BOOKMARKS_URI,
88 Browser.HISTORY_PROJECTION, whereClause, null, orderBy);
89 mCursor.registerContentObserver(new ChangeObserver());
90 mGridView.setOnItemClickListener(
91 new AdapterView.OnItemClickListener() {
92 public void onItemClick(AdapterView parent, View v,
93 int position, long id) {
94 mCursor.moveToPosition(position);
95 String url = mCursor.getString(
96 Browser.HISTORY_PROJECTION_URL_INDEX);
97 Intent intent = (new Intent()).setAction(url);
98 getParent().setResult(RESULT_OK, intent);
99 finish();
100 }});
101 }
102
103 void heightChanged(int newHeight) {
104 mThumbHeight = newHeight;
105 }
106
107 private class ChangeObserver extends ContentObserver {
108 public ChangeObserver() {
109 super(new Handler());
110 }
111
112 @Override
113 public boolean deliverSelfNotifications() {
114 return true;
115 }
116
117 @Override
118 public void onChange(boolean selfChange) {
119 BookmarkGridAdapter.this.refreshData();
120 }
121 }
122
123 void refreshData() {
124 mCursor.requery();
125 for (DataSetObserver o : mDataObservers) {
126 o.onChanged();
127 }
128 }
129
130 /* (non-Javadoc)
131 * @see android.widget.ListAdapter#areAllItemsSelectable()
132 */
133 public boolean areAllItemsEnabled() {
134 return true;
135 }
136
137 /* (non-Javadoc)
138 * @see android.widget.ListAdapter#isSelectable(int)
139 */
140 public boolean isEnabled(int position) {
141 if (position >= 0 && position < mCursor.getCount()) {
142 return true;
143 }
144 return false;
145 }
146
147 /* (non-Javadoc)
148 * @see android.widget.Adapter#getCount()
149 */
150 public int getCount() {
151 return mCursor.getCount();
152 }
153
154 /* (non-Javadoc)
155 * @see android.widget.Adapter#getItem(int)
156 */
157 public Object getItem(int position) {
158 return null;
159 }
160
161 /* (non-Javadoc)
162 * @see android.widget.Adapter#getItemId(int)
163 */
164 public long getItemId(int position) {
165 return position;
166 }
167
168 /* (non-Javadoc)
169 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
170 */
171 public View getView(int position, View convertView, ViewGroup parent) {
172 View v = null;
173 if (convertView != null) {
174 v = convertView;
175 } else {
176 LayoutInflater factory = LayoutInflater.from(mContext);
177 v = factory.inflate(R.layout.bookmark_thumbnail, null);
178 }
179 ImageView thumb = (ImageView) v.findViewById(R.id.thumb);
180 TextView tv = (TextView) v.findViewById(R.id.label);
181
182 mCursor.moveToPosition(position);
183 tv.setText(mCursor.getString(
184 Browser.HISTORY_PROJECTION_TITLE_INDEX));
185 byte[] data = mCursor.getBlob(
186 Browser.HISTORY_PROJECTION_THUMBNAIL_INDEX);
187 if (data == null) {
188 // Backup is to show the favicon
189 data = mCursor.getBlob(
190 Browser.HISTORY_PROJECTION_FAVICON_INDEX);
191 thumb.setScaleType(ImageView.ScaleType.CENTER);
192 } else {
193 thumb.setScaleType(ImageView.ScaleType.FIT_XY);
194 }
195 if (data != null) {
196 thumb.setImageBitmap(
197 BitmapFactory.decodeByteArray(data, 0, data.length));
198 } else {
199 thumb.setImageResource(R.drawable.app_web_browser_sm);
200 thumb.setScaleType(ImageView.ScaleType.CENTER);
201 }
202 ViewGroup.LayoutParams lp = thumb.getLayoutParams();
203 if (lp.height != mThumbHeight) {
204 lp.height = mThumbHeight;
205 thumb.requestLayout();
206 }
207 return v;
208 }
209
210 /* (non-Javadoc)
211 * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver)
212 */
213 public void registerDataSetObserver(DataSetObserver observer) {
214 mDataObservers.add(observer);
215 }
216
217 /* (non-Javadoc)
218 * @see android.widget.Adapter#hasStableIds()
219 */
220 public boolean hasStableIds() {
221 return true;
222 }
223
224 /* (non-Javadoc)
225 * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver)
226 */
227 public void unregisterDataSetObserver(DataSetObserver observer) {
228 mDataObservers.remove(observer);
229 }
230
231 public int getItemViewType(int position) {
232 return 0;
233 }
234
235 public int getViewTypeCount() {
236 return 1;
237 }
238
239 public boolean isEmpty() {
240 return getCount() == 0;
241 }
242 }
243}