Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [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.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.database.Cursor; |
| 23 | import android.database.ContentObserver; |
| 24 | import android.database.DataSetObserver; |
| 25 | import android.graphics.BitmapFactory; |
| 26 | import android.os.Bundle; |
| 27 | import android.os.Handler; |
| 28 | import android.provider.Browser; |
| 29 | import android.provider.Browser.BookmarkColumns; |
| 30 | import android.view.LayoutInflater; |
| 31 | import android.view.View; |
| 32 | import android.view.ViewGroup; |
| 33 | import android.widget.AdapterView; |
| 34 | import android.widget.GridView; |
| 35 | import android.widget.ImageView; |
| 36 | import android.widget.ListAdapter; |
| 37 | import android.widget.TextView; |
| 38 | |
| 39 | import java.util.ArrayList; |
| 40 | |
| 41 | public class BookmarkGridPage extends Activity { |
| 42 | private final static int SPACING = 10; |
Leon Scroggins | 71bd366 | 2009-07-06 16:40:00 -0400 | [diff] [blame] | 43 | private static final int BOOKMARKS_SAVE = 1; |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 44 | private BookmarkGrid mGridView; |
| 45 | private BookmarkGridAdapter mAdapter; |
| 46 | |
| 47 | @Override |
| 48 | public void onCreate(Bundle savedInstanceState) { |
| 49 | super.onCreate(savedInstanceState); |
| 50 | |
| 51 | mGridView = new BookmarkGrid(this); |
| 52 | mGridView.setNumColumns(3); |
| 53 | mAdapter = new BookmarkGridAdapter(this); |
| 54 | mGridView.setAdapter(mAdapter); |
| 55 | mGridView.setFocusable(true); |
| 56 | mGridView.setFocusableInTouchMode(true); |
| 57 | mGridView.setSelector(android.R.drawable.gallery_thumb); |
| 58 | mGridView.setVerticalSpacing(SPACING); |
| 59 | mGridView.setHorizontalSpacing(SPACING); |
| 60 | setContentView(mGridView); |
| 61 | mGridView.requestFocus(); |
| 62 | } |
| 63 | |
Leon Scroggins | 71bd366 | 2009-07-06 16:40:00 -0400 | [diff] [blame] | 64 | @Override |
| 65 | protected void onActivityResult(int requestCode, int resultCode, |
| 66 | Intent data) { |
| 67 | switch(requestCode) { |
| 68 | case BOOKMARKS_SAVE: |
| 69 | if (resultCode == RESULT_OK) { |
| 70 | mAdapter.refreshData(); |
| 71 | } |
| 72 | break; |
| 73 | default: |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 78 | private class BookmarkGrid extends GridView { |
| 79 | public BookmarkGrid(Context context) { |
| 80 | super(context); |
| 81 | } |
| 82 | @Override |
| 83 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 84 | int thumbHeight = (h - 2 * (SPACING + getListPaddingTop() |
| 85 | + getListPaddingBottom())) / 3; |
| 86 | mAdapter.heightChanged(thumbHeight); |
| 87 | super.onSizeChanged(w, h, oldw, oldh); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private class BookmarkGridAdapter implements ListAdapter { |
| 92 | private ArrayList<DataSetObserver> mDataObservers; |
| 93 | private Context mContext; // Context to use to inflate views |
| 94 | private Cursor mCursor; |
| 95 | private int mThumbHeight; |
| 96 | |
| 97 | public BookmarkGridAdapter(Context context) { |
| 98 | mContext = context; |
| 99 | mDataObservers = new ArrayList<DataSetObserver>(); |
| 100 | String whereClause = Browser.BookmarkColumns.BOOKMARK + " != 0"; |
| 101 | String orderBy = Browser.BookmarkColumns.VISITS + " DESC"; |
| 102 | mCursor = managedQuery(Browser.BOOKMARKS_URI, |
| 103 | Browser.HISTORY_PROJECTION, whereClause, null, orderBy); |
| 104 | mCursor.registerContentObserver(new ChangeObserver()); |
| 105 | mGridView.setOnItemClickListener( |
| 106 | new AdapterView.OnItemClickListener() { |
| 107 | public void onItemClick(AdapterView parent, View v, |
| 108 | int position, long id) { |
Leon Scroggins | 71bd366 | 2009-07-06 16:40:00 -0400 | [diff] [blame] | 109 | if (0 == position) { |
| 110 | // Launch the add bookmark activity |
| 111 | Intent i = new Intent(BookmarkGridPage.this, |
| 112 | AddBookmarkPage.class); |
| 113 | i.putExtras(getIntent()); |
| 114 | startActivityForResult(i, BOOKMARKS_SAVE); |
| 115 | return; |
| 116 | } |
| 117 | position--; |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 118 | mCursor.moveToPosition(position); |
| 119 | String url = mCursor.getString( |
| 120 | Browser.HISTORY_PROJECTION_URL_INDEX); |
| 121 | Intent intent = (new Intent()).setAction(url); |
| 122 | getParent().setResult(RESULT_OK, intent); |
| 123 | finish(); |
| 124 | }}); |
| 125 | } |
| 126 | |
| 127 | void heightChanged(int newHeight) { |
| 128 | mThumbHeight = newHeight; |
| 129 | } |
| 130 | |
| 131 | private class ChangeObserver extends ContentObserver { |
| 132 | public ChangeObserver() { |
| 133 | super(new Handler()); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public boolean deliverSelfNotifications() { |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void onChange(boolean selfChange) { |
| 143 | BookmarkGridAdapter.this.refreshData(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void refreshData() { |
| 148 | mCursor.requery(); |
| 149 | for (DataSetObserver o : mDataObservers) { |
| 150 | o.onChanged(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* (non-Javadoc) |
| 155 | * @see android.widget.ListAdapter#areAllItemsSelectable() |
| 156 | */ |
| 157 | public boolean areAllItemsEnabled() { |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | /* (non-Javadoc) |
| 162 | * @see android.widget.ListAdapter#isSelectable(int) |
| 163 | */ |
| 164 | public boolean isEnabled(int position) { |
| 165 | if (position >= 0 && position < mCursor.getCount()) { |
| 166 | return true; |
| 167 | } |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | /* (non-Javadoc) |
| 172 | * @see android.widget.Adapter#getCount() |
| 173 | */ |
| 174 | public int getCount() { |
| 175 | return mCursor.getCount(); |
| 176 | } |
| 177 | |
| 178 | /* (non-Javadoc) |
| 179 | * @see android.widget.Adapter#getItem(int) |
| 180 | */ |
| 181 | public Object getItem(int position) { |
| 182 | return null; |
| 183 | } |
| 184 | |
| 185 | /* (non-Javadoc) |
| 186 | * @see android.widget.Adapter#getItemId(int) |
| 187 | */ |
| 188 | public long getItemId(int position) { |
| 189 | return position; |
| 190 | } |
| 191 | |
| 192 | /* (non-Javadoc) |
| 193 | * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup) |
| 194 | */ |
| 195 | public View getView(int position, View convertView, ViewGroup parent) { |
| 196 | View v = null; |
| 197 | if (convertView != null) { |
| 198 | v = convertView; |
| 199 | } else { |
| 200 | LayoutInflater factory = LayoutInflater.from(mContext); |
| 201 | v = factory.inflate(R.layout.bookmark_thumbnail, null); |
| 202 | } |
| 203 | ImageView thumb = (ImageView) v.findViewById(R.id.thumb); |
Leon Scroggins | 266ef43 | 2009-07-07 11:25:28 -0400 | [diff] [blame] | 204 | // Favicon disabled for now. |
| 205 | //ImageView fav = (ImageView) v.findViewById(R.id.fav); |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 206 | TextView tv = (TextView) v.findViewById(R.id.label); |
| 207 | |
Leon Scroggins | 71bd366 | 2009-07-06 16:40:00 -0400 | [diff] [blame] | 208 | ViewGroup.LayoutParams lp = thumb.getLayoutParams(); |
| 209 | if (lp.height != mThumbHeight) { |
| 210 | lp.height = mThumbHeight; |
| 211 | thumb.requestLayout(); |
| 212 | } |
| 213 | |
| 214 | if (0 == position) { |
| 215 | // This is to create a bookmark for the current page. |
| 216 | tv.setText(R.string.add_new_bookmark); |
| 217 | thumb.setImageResource( |
| 218 | R.drawable.ic_tab_browser_bookmark_selected); |
| 219 | return v; |
| 220 | } |
| 221 | position--; |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 222 | mCursor.moveToPosition(position); |
| 223 | tv.setText(mCursor.getString( |
| 224 | Browser.HISTORY_PROJECTION_TITLE_INDEX)); |
| 225 | byte[] data = mCursor.getBlob( |
| 226 | Browser.HISTORY_PROJECTION_THUMBNAIL_INDEX); |
| 227 | if (data == null) { |
Leon Scroggins | 266ef43 | 2009-07-07 11:25:28 -0400 | [diff] [blame] | 228 | // Backup is to just show white |
| 229 | thumb.setImageResource(R.drawable.blank); |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 230 | } else { |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 231 | thumb.setImageBitmap( |
| 232 | BitmapFactory.decodeByteArray(data, 0, data.length)); |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 233 | } |
Leon Scroggins | 266ef43 | 2009-07-07 11:25:28 -0400 | [diff] [blame] | 234 | /* |
| 235 | // Now show the favicon |
| 236 | data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX); |
| 237 | if (data == null) { |
| 238 | fav.setVisibility(View.GONE); |
| 239 | } else { |
| 240 | fav.setVisibility(View.VISIBLE); |
| 241 | fav.setImageBitmap( |
| 242 | BitmapFactory.decodeByteArray(data, 0, data.length)); |
| 243 | } |
| 244 | */ |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 245 | return v; |
| 246 | } |
| 247 | |
| 248 | /* (non-Javadoc) |
| 249 | * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver) |
| 250 | */ |
| 251 | public void registerDataSetObserver(DataSetObserver observer) { |
| 252 | mDataObservers.add(observer); |
| 253 | } |
| 254 | |
| 255 | /* (non-Javadoc) |
| 256 | * @see android.widget.Adapter#hasStableIds() |
| 257 | */ |
| 258 | public boolean hasStableIds() { |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | /* (non-Javadoc) |
| 263 | * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver) |
| 264 | */ |
| 265 | public void unregisterDataSetObserver(DataSetObserver observer) { |
| 266 | mDataObservers.remove(observer); |
| 267 | } |
| 268 | |
| 269 | public int getItemViewType(int position) { |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | public int getViewTypeCount() { |
| 274 | return 1; |
| 275 | } |
| 276 | |
| 277 | public boolean isEmpty() { |
| 278 | return getCount() == 0; |
| 279 | } |
| 280 | } |
| 281 | } |