blob: d03c7a3ad95a4a51f39eba39bf7dba6235031fb4 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
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.app.ListActivity;
21import android.content.Intent;
22import android.database.ContentObserver;
23import android.database.Cursor;
24import android.database.DataSetObserver;
25import android.graphics.Bitmap;
Patrick Scott8f0076b2009-09-17 13:51:30 -040026import android.graphics.BitmapFactory;
The Android Open Source Project0c908882009-03-03 19:32:16 -080027import android.os.Bundle;
28import android.os.Handler;
29import android.provider.Browser;
30import android.webkit.WebIconDatabase.IconListener;
31import android.widget.ListAdapter;
32import android.widget.ListView;
33import android.widget.SimpleCursorAdapter;
34import android.widget.TextView;
35import android.view.LayoutInflater;
36import android.view.View;
37import android.view.ViewGroup;
38import android.view.ViewGroup.LayoutParams;
Mihai Preda83817df2009-04-28 14:24:47 +020039import android.view.ViewStub;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040
41import java.util.Vector;
42
43public class MostVisitedActivity extends ListActivity {
44
45 private MyAdapter mAdapter;
46
Grace Klobab8a9cb02009-08-27 11:53:36 -070047 // Instance of IconReceiver
48 private final IconReceiver mIconReceiver = new IconReceiver();
49
The Android Open Source Project0c908882009-03-03 19:32:16 -080050 @Override
51 public void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
53 mAdapter = new MyAdapter();
Patrick Scott8f0076b2009-09-17 13:51:30 -040054 CombinedBookmarkHistoryActivity.getIconListenerSet()
Grace Klobab8a9cb02009-08-27 11:53:36 -070055 .addListener(mIconReceiver);
The Android Open Source Project0c908882009-03-03 19:32:16 -080056 setListAdapter(mAdapter);
57 ListView list = getListView();
Mihai Preda83817df2009-04-28 14:24:47 +020058 View v = new ViewStub(this, R.layout.empty_history);
The Android Open Source Project0c908882009-03-03 19:32:16 -080059 addContentView(v, new LayoutParams(LayoutParams.FILL_PARENT,
60 LayoutParams.FILL_PARENT));
61 list.setEmptyView(v);
62 }
63
Grace Klobab8a9cb02009-08-27 11:53:36 -070064 @Override
65 protected void onDestroy() {
66 super.onDestroy();
Patrick Scott8f0076b2009-09-17 13:51:30 -040067 CombinedBookmarkHistoryActivity.getIconListenerSet()
Grace Klobab8a9cb02009-08-27 11:53:36 -070068 .removeListener(mIconReceiver);
69 }
70
The Android Open Source Project0c908882009-03-03 19:32:16 -080071 private class IconReceiver implements IconListener {
72 public void onReceivedIcon(String url, Bitmap icon) {
73 setListAdapter(mAdapter);
74 }
75 }
76
77 protected void onListItemClick(ListView l, View v, int position, long id) {
78 TextView tv = (TextView) v.findViewById(R.id.url);
79 String url = tv.getText().toString();
80 loadUrl(url, false);
81 }
82
83 private void loadUrl(String url, boolean newWindow) {
84 Intent intent = new Intent().setAction(url);
85 if (newWindow) {
86 Bundle b = new Bundle();
87 b.putBoolean("new_window", true);
88 intent.putExtras(b);
89 }
90 setResultToParent(RESULT_OK, intent);
91 finish();
92 }
93
94 private class MyAdapter implements ListAdapter {
95 private Vector<DataSetObserver> mObservers;
96 private Cursor mCursor;
97 // These correspond with projection below.
Leon Scroggins9c288402009-05-21 12:37:10 -070098 private static final int mUrlIndex = 0;
99 private static final int mTitleIndex = 1;
100 private static final int mBookmarkIndex = 2;
Patrick Scott8f0076b2009-09-17 13:51:30 -0400101 private static final int mFaviconIndex = 3;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800102
103 MyAdapter() {
104 mObservers = new Vector<DataSetObserver>();
105 String[] projection = new String[] {
106 Browser.BookmarkColumns.URL,
107 Browser.BookmarkColumns.TITLE,
Patrick Scott8f0076b2009-09-17 13:51:30 -0400108 Browser.BookmarkColumns.BOOKMARK,
109 Browser.BookmarkColumns.FAVICON };
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 String whereClause = Browser.BookmarkColumns.VISITS + " != 0";
111 String orderBy = Browser.BookmarkColumns.VISITS + " DESC";
112 mCursor = managedQuery(Browser.BOOKMARKS_URI, projection,
113 whereClause, null, orderBy);
114 mCursor.registerContentObserver(new ChangeObserver());
115 }
116
117 private class ChangeObserver extends ContentObserver {
118 public ChangeObserver() {
119 super(new Handler());
120 }
121
122 @Override
123 public boolean deliverSelfNotifications() {
124 return true;
125 }
126
127 @Override
128 public void onChange(boolean selfChange) {
129 MyAdapter.this.refreshData();
130 }
131 }
132
133 void refreshData() {
134 mCursor.requery();
135 for (DataSetObserver o : mObservers) {
136 o.onChanged();
137 }
138 }
139
140 public View getView(int position, View convertView, ViewGroup parent) {
141 HistoryItem item;
142 if (null == convertView) {
143 item = new HistoryItem(MostVisitedActivity.this);
144 } else {
145 item = (HistoryItem) convertView;
146 }
147 mCursor.moveToPosition(position);
148 item.setName(mCursor.getString(mTitleIndex));
149 String url = mCursor.getString(mUrlIndex);
150 item.setUrl(url);
Patrick Scott8f0076b2009-09-17 13:51:30 -0400151 byte[] data = mCursor.getBlob(mFaviconIndex);
152 if (data != null) {
153 item.setFavicon(BitmapFactory.decodeByteArray(data, 0,
154 data.length));
155 } else {
156 item.setFavicon(CombinedBookmarkHistoryActivity
157 .getIconListenerSet().getFavicon(url));
158 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800159 item.setIsBookmark(1 == mCursor.getInt(mBookmarkIndex));
160 return item;
161 }
162
163 public boolean areAllItemsEnabled() {
164 return true;
165 }
166
167 public boolean isEnabled(int position) {
168 return true;
169 }
170
171 public int getCount() {
172 return mCursor.getCount();
173 }
174
175 public Object getItem(int position) {
176 return null;
177 }
178
179 public long getItemId(int position) {
180 return position;
181 }
182
183 // Always a HistoryItem
184 public int getItemViewType(int position) {
185 return 0;
186 }
187
188 public int getViewTypeCount() {
189 return 1;
190 }
191
192 public boolean hasStableIds() {
193 return true;
194 }
195
196 public void registerDataSetObserver(DataSetObserver observer) {
197 mObservers.add(observer);
198 }
199
200 public void unregisterDataSetObserver(DataSetObserver observer) {
201 mObservers.remove(observer);
202 }
203
204 public boolean isEmpty() {
205 return getCount() == 0;
206 }
207 }
208
209 // This Activity is generally a sub-Activity of CombinedHistoryActivity. In
210 // that situation, we need to pass our result code up to our parent.
211 // However, if someone calls this Activity directly, then this has no
212 // parent, and it needs to set it on itself.
213 private void setResultToParent(int resultCode, Intent data) {
214 Activity a = getParent() == null ? this : getParent();
215 a.setResult(resultCode, data);
216 }
217}
218