blob: 704ee279f4e9d9607e904293c01359392ed13635 [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;
26import android.os.Bundle;
27import android.os.Handler;
28import android.provider.Browser;
29import android.webkit.WebIconDatabase.IconListener;
30import android.widget.ListAdapter;
31import android.widget.ListView;
32import android.widget.SimpleCursorAdapter;
33import android.widget.TextView;
34import android.view.LayoutInflater;
35import android.view.View;
36import android.view.ViewGroup;
37import android.view.ViewGroup.LayoutParams;
38
39import java.util.Vector;
40
41public class MostVisitedActivity extends ListActivity {
42
43 private MyAdapter mAdapter;
44
45 @Override
46 public void onCreate(Bundle savedInstanceState) {
47 super.onCreate(savedInstanceState);
48 mAdapter = new MyAdapter();
49 CombinedBookmarkHistoryActivity.getIconListenerSet(getContentResolver())
50 .addListener(new IconReceiver());
51 setListAdapter(mAdapter);
52 ListView list = getListView();
53 LayoutInflater factory = LayoutInflater.from(this);
54 View v = factory.inflate(R.layout.empty_history, null);
55 addContentView(v, new LayoutParams(LayoutParams.FILL_PARENT,
56 LayoutParams.FILL_PARENT));
57 list.setEmptyView(v);
58 }
59
60 private class IconReceiver implements IconListener {
61 public void onReceivedIcon(String url, Bitmap icon) {
62 setListAdapter(mAdapter);
63 }
64 }
65
66 protected void onListItemClick(ListView l, View v, int position, long id) {
67 TextView tv = (TextView) v.findViewById(R.id.url);
68 String url = tv.getText().toString();
69 loadUrl(url, false);
70 }
71
72 private void loadUrl(String url, boolean newWindow) {
73 Intent intent = new Intent().setAction(url);
74 if (newWindow) {
75 Bundle b = new Bundle();
76 b.putBoolean("new_window", true);
77 intent.putExtras(b);
78 }
79 setResultToParent(RESULT_OK, intent);
80 finish();
81 }
82
83 private class MyAdapter implements ListAdapter {
84 private Vector<DataSetObserver> mObservers;
85 private Cursor mCursor;
86 // These correspond with projection below.
87 private final int mUrlIndex = 0;
88 private final int mTitleIndex = 1;
89 private final int mBookmarkIndex = 2;
90
91 MyAdapter() {
92 mObservers = new Vector<DataSetObserver>();
93 String[] projection = new String[] {
94 Browser.BookmarkColumns.URL,
95 Browser.BookmarkColumns.TITLE,
96 Browser.BookmarkColumns.BOOKMARK };
97 String whereClause = Browser.BookmarkColumns.VISITS + " != 0";
98 String orderBy = Browser.BookmarkColumns.VISITS + " DESC";
99 mCursor = managedQuery(Browser.BOOKMARKS_URI, projection,
100 whereClause, null, orderBy);
101 mCursor.registerContentObserver(new ChangeObserver());
102 }
103
104 private class ChangeObserver extends ContentObserver {
105 public ChangeObserver() {
106 super(new Handler());
107 }
108
109 @Override
110 public boolean deliverSelfNotifications() {
111 return true;
112 }
113
114 @Override
115 public void onChange(boolean selfChange) {
116 MyAdapter.this.refreshData();
117 }
118 }
119
120 void refreshData() {
121 mCursor.requery();
122 for (DataSetObserver o : mObservers) {
123 o.onChanged();
124 }
125 }
126
127 public View getView(int position, View convertView, ViewGroup parent) {
128 HistoryItem item;
129 if (null == convertView) {
130 item = new HistoryItem(MostVisitedActivity.this);
131 } else {
132 item = (HistoryItem) convertView;
133 }
134 mCursor.moveToPosition(position);
135 item.setName(mCursor.getString(mTitleIndex));
136 String url = mCursor.getString(mUrlIndex);
137 item.setUrl(url);
138 item.setFavicon(CombinedBookmarkHistoryActivity.getIconListenerSet(
139 getContentResolver()).getFavicon(url));
140 item.setIsBookmark(1 == mCursor.getInt(mBookmarkIndex));
141 return item;
142 }
143
144 public boolean areAllItemsEnabled() {
145 return true;
146 }
147
148 public boolean isEnabled(int position) {
149 return true;
150 }
151
152 public int getCount() {
153 return mCursor.getCount();
154 }
155
156 public Object getItem(int position) {
157 return null;
158 }
159
160 public long getItemId(int position) {
161 return position;
162 }
163
164 // Always a HistoryItem
165 public int getItemViewType(int position) {
166 return 0;
167 }
168
169 public int getViewTypeCount() {
170 return 1;
171 }
172
173 public boolean hasStableIds() {
174 return true;
175 }
176
177 public void registerDataSetObserver(DataSetObserver observer) {
178 mObservers.add(observer);
179 }
180
181 public void unregisterDataSetObserver(DataSetObserver observer) {
182 mObservers.remove(observer);
183 }
184
185 public boolean isEmpty() {
186 return getCount() == 0;
187 }
188 }
189
190 // This Activity is generally a sub-Activity of CombinedHistoryActivity. In
191 // that situation, we need to pass our result code up to our parent.
192 // However, if someone calls this Activity directly, then this has no
193 // parent, and it needs to set it on itself.
194 private void setResultToParent(int resultCode, Intent data) {
195 Activity a = getParent() == null ? this : getParent();
196 a.setResult(resultCode, data);
197 }
198}
199