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 | |
| 46 | @Override |
| 47 | public void onCreate(Bundle savedInstanceState) { |
| 48 | super.onCreate(savedInstanceState); |
| 49 | mAdapter = new MyAdapter(); |
| 50 | CombinedBookmarkHistoryActivity.getIconListenerSet(getContentResolver()) |
| 51 | .addListener(new IconReceiver()); |
| 52 | setListAdapter(mAdapter); |
| 53 | ListView list = getListView(); |
Mihai Preda | 83817df | 2009-04-28 14:24:47 +0200 | [diff] [blame] | 54 | View v = new ViewStub(this, R.layout.empty_history); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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. |
Leon Scroggins | 9c28840 | 2009-05-21 12:37:10 -0700 | [diff] [blame] | 87 | private static final int mUrlIndex = 0; |
| 88 | private static final int mTitleIndex = 1; |
| 89 | private static final int mBookmarkIndex = 2; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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 | |