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