PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/WebVox/src/com/marvin/webvox/MostVisitedActivity.java

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