The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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.AlertDialog; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 21 | import android.app.LoaderManager; |
Dianne Hackborn | 80f3262 | 2010-08-05 14:17:53 -0700 | [diff] [blame] | 22 | import android.content.ClipboardManager; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 23 | import android.content.ClippedData; |
| 24 | import android.content.ContentUris; |
| 25 | import android.content.ContentValues; |
Dianne Hackborn | 80f3262 | 2010-08-05 14:17:53 -0700 | [diff] [blame] | 26 | import android.content.Context; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 27 | import android.content.DialogInterface; |
| 28 | import android.content.Intent; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 29 | import android.content.Loader; |
| 30 | import android.database.Cursor; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 31 | import android.graphics.Bitmap; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 32 | import android.graphics.BitmapFactory; |
| 33 | import android.net.Uri; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 34 | import android.os.Bundle; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 35 | import android.provider.Browser; |
Jeff Hamilton | 69bd707 | 2010-08-17 12:38:22 -0500 | [diff] [blame^] | 36 | import android.provider.BrowserContract; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 37 | import android.util.Pair; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | import android.view.ContextMenu; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 39 | import android.view.ContextMenu.ContextMenuInfo; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 40 | import android.view.MenuInflater; |
| 41 | import android.view.MenuItem; |
| 42 | import android.view.View; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 43 | import android.view.View.OnClickListener; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 44 | import android.view.ViewGroup; |
Patrick Scott | c1cf63a | 2010-03-09 16:02:08 -0500 | [diff] [blame] | 45 | import android.webkit.WebIconDatabase.IconListener; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 46 | import android.widget.AdapterView; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 47 | import android.widget.AdapterView.OnItemClickListener; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 48 | import android.widget.Button; |
| 49 | import android.widget.GridView; |
| 50 | import android.widget.Toast; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 51 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 52 | import java.util.Stack; |
| 53 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | /** |
| 55 | * View showing the user's bookmarks in the browser. |
| 56 | */ |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 57 | public class BrowserBookmarksPage extends Activity implements View.OnCreateContextMenuListener, |
| 58 | LoaderManager.LoaderCallbacks<Cursor>, OnItemClickListener, IconListener, OnClickListener { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 59 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 60 | static final int BOOKMARKS_SAVE = 1; |
| 61 | static final String LOGTAG = "browser"; |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 62 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 63 | static final int LOADER_BOOKMARKS = 1; |
| 64 | |
| 65 | GridView mGrid; |
| 66 | BrowserBookmarksAdapter mAdapter; |
| 67 | boolean mDisableNewWindow; |
| 68 | BookmarkItem mContextHeader; |
| 69 | boolean mCanceled = false; |
| 70 | boolean mCreateShortcut; |
| 71 | View mEmptyView; |
| 72 | View mContentView; |
| 73 | Stack<Pair<String, Uri>> mFolderStack = new Stack<Pair<String, Uri>>(); |
| 74 | Button mUpButton; |
| 75 | |
| 76 | @Override |
| 77 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 78 | switch (id) { |
| 79 | case LOADER_BOOKMARKS: { |
| 80 | int rootFolder = 0; |
| 81 | if (args != null) { |
| 82 | args.getInt(BookmarksLoader.ARG_ROOT_FOLDER, 0); |
| 83 | } |
| 84 | return new BookmarksLoader(this, rootFolder); |
| 85 | } |
| 86 | } |
| 87 | throw new UnsupportedOperationException("Unknown loader id " + id); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
| 92 | // Set the visibility of the empty vs. content views |
| 93 | if (data == null || data.getCount() == 0) { |
| 94 | mEmptyView.setVisibility(View.VISIBLE); |
| 95 | mContentView.setVisibility(View.GONE); |
| 96 | } else { |
| 97 | mEmptyView.setVisibility(View.GONE); |
| 98 | mContentView.setVisibility(View.VISIBLE); |
| 99 | } |
| 100 | |
| 101 | // Fill in the "up" button if needed |
| 102 | BookmarksLoader bl = (BookmarksLoader) loader; |
| 103 | boolean rootFolder = |
| 104 | (BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER.equals(bl.getUri())); |
| 105 | if (rootFolder) { |
| 106 | mUpButton.setText(R.string.defaultBookmarksUpButton); |
| 107 | mUpButton.setEnabled(false); |
| 108 | } else { |
| 109 | mUpButton.setText(mFolderStack.peek().first); |
| 110 | mUpButton.setEnabled(true); |
| 111 | } |
| 112 | |
| 113 | // Give the new data to the adapter |
| 114 | mAdapter.changeCursor(data); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void onClick(View view) { |
| 119 | if (view == mUpButton) { |
| 120 | Pair<String, Uri> pair = mFolderStack.pop(); |
| 121 | BookmarksLoader loader = |
| 122 | (BookmarksLoader) ((Loader) getLoaderManager().getLoader(LOADER_BOOKMARKS)); |
| 123 | loader.setUri(pair.second); |
| 124 | loader.forceLoad(); |
| 125 | } |
| 126 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 127 | |
| 128 | @Override |
| 129 | public boolean onContextItemSelected(MenuItem item) { |
| 130 | // It is possible that the view has been canceled when we get to |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 131 | // this point as back has a higher priority |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 132 | if (mCanceled) { |
| 133 | return true; |
| 134 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 135 | AdapterView.AdapterContextMenuInfo i = |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 136 | (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); |
| 137 | // If we have no menu info, we can't tell which item was selected. |
| 138 | if (i == null) { |
| 139 | return true; |
| 140 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 141 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 142 | switch (item.getItemId()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 143 | case R.id.open_context_menu_id: |
| 144 | loadUrl(i.position); |
| 145 | break; |
| 146 | case R.id.edit_context_menu_id: |
| 147 | editBookmark(i.position); |
| 148 | break; |
| 149 | case R.id.shortcut_context_menu_id: |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 150 | sendBroadcast(createShortcutIntent(i.position)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 151 | break; |
| 152 | case R.id.delete_context_menu_id: |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 153 | displayRemoveBookmarkDialog(i.position); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 154 | break; |
| 155 | case R.id.new_window_context_menu_id: |
| 156 | openInNewWindow(i.position); |
| 157 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 158 | case R.id.share_link_context_menu_id: { |
| 159 | Cursor cursor = (Cursor) mAdapter.getItem(i.position); |
Leon Scroggins | 96afcb1 | 2009-12-10 12:35:56 -0500 | [diff] [blame] | 160 | BrowserActivity.sharePage(BrowserBookmarksPage.this, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 161 | cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE), |
| 162 | cursor.getString(BookmarksLoader.COLUMN_INDEX_URL), |
| 163 | getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON), |
| 164 | getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_THUMBNAIL)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 165 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 166 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 167 | case R.id.copy_url_context_menu_id: |
| 168 | copy(getUrl(i.position)); |
Leon Scroggins | feb941d | 2009-05-28 17:27:38 -0400 | [diff] [blame] | 169 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 170 | case R.id.homepage_context_menu_id: { |
Leon Scroggins | feb941d | 2009-05-28 17:27:38 -0400 | [diff] [blame] | 171 | BrowserSettings.getInstance().setHomePage(this, |
| 172 | getUrl(i.position)); |
| 173 | Toast.makeText(this, R.string.homepage_set, |
| 174 | Toast.LENGTH_LONG).show(); |
| 175 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 176 | } |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 177 | // Only for the Most visited page |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 178 | case R.id.save_to_bookmarks_menu_id: { |
| 179 | Cursor cursor = (Cursor) mAdapter.getItem(i.position); |
| 180 | String name = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
| 181 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 182 | // If the site is bookmarked, the item becomes remove from |
| 183 | // bookmarks. |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 184 | Bookmarks.removeFromBookmarks(this, getContentResolver(), url, name); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 185 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 186 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 187 | default: |
| 188 | return super.onContextItemSelected(item); |
| 189 | } |
| 190 | return true; |
| 191 | } |
| 192 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 193 | Bitmap getBitmap(Cursor cursor, int columnIndex) { |
| 194 | byte[] data = cursor.getBlob(columnIndex); |
| 195 | if (data == null) { |
| 196 | return null; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 197 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 198 | return BitmapFactory.decodeByteArray(data, 0, data.length); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
| 203 | AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
| 204 | |
| 205 | MenuInflater inflater = getMenuInflater(); |
| 206 | inflater.inflate(R.menu.bookmarkscontext, menu); |
| 207 | |
| 208 | if (mDisableNewWindow) { |
| 209 | menu.findItem(R.id.new_window_context_menu_id).setVisible(false); |
| 210 | } |
| 211 | |
| 212 | if (mContextHeader == null) { |
| 213 | mContextHeader = new BookmarkItem(BrowserBookmarksPage.this); |
| 214 | } else if (mContextHeader.getParent() != null) { |
| 215 | ((ViewGroup) mContextHeader.getParent()).removeView(mContextHeader); |
| 216 | } |
| 217 | |
| 218 | populateBookmarkItem(mAdapter, mContextHeader, info.position); |
| 219 | |
| 220 | menu.setHeaderView(mContextHeader); |
| 221 | } |
| 222 | |
| 223 | private void populateBookmarkItem(BrowserBookmarksAdapter adapter, BookmarkItem item, |
| 224 | int position) { |
| 225 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 226 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
| 227 | item.setUrl(url); |
| 228 | item.setName(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)); |
| 229 | Bitmap bitmap = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON); |
| 230 | if (bitmap == null) { |
| 231 | bitmap = CombinedBookmarkHistoryActivity.getIconListenerSet().getFavicon(url); |
| 232 | } |
| 233 | item.setFavicon(bitmap); |
| 234 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 235 | |
| 236 | /** |
| 237 | * Create a new BrowserBookmarksPage. |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 238 | */ |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 239 | @Override |
| 240 | protected void onCreate(Bundle icicle) { |
| 241 | super.onCreate(icicle); |
| 242 | |
Patrick Scott | f49ecd6 | 2010-04-09 12:43:26 -0400 | [diff] [blame] | 243 | Intent intent = getIntent(); |
| 244 | if (Intent.ACTION_CREATE_SHORTCUT.equals(intent.getAction())) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 245 | mCreateShortcut = true; |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 246 | setTitle(R.string.browser_bookmarks_page_bookmarks_text); |
| 247 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 248 | mDisableNewWindow = intent.getBooleanExtra("disable_new_window", false); |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 249 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 250 | setContentView(R.layout.bookmarks); |
| 251 | mEmptyView = findViewById(android.R.id.empty); |
| 252 | mContentView = findViewById(android.R.id.content); |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 253 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 254 | mGrid = (GridView) findViewById(R.id.grid); |
| 255 | mGrid.setOnItemClickListener(this); |
| 256 | mGrid.setColumnWidth( |
| 257 | BrowserActivity.getDesiredThumbnailWidth(this)); |
| 258 | if (!mCreateShortcut) { |
| 259 | mGrid.setOnCreateContextMenuListener(this); |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 260 | } |
Patrick Scott | f49ecd6 | 2010-04-09 12:43:26 -0400 | [diff] [blame] | 261 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 262 | mUpButton = (Button) findViewById(R.id.up); |
| 263 | mUpButton.setEnabled(false); |
| 264 | mUpButton.setOnClickListener(this); |
| 265 | |
| 266 | mAdapter = new BrowserBookmarksAdapter(this); |
| 267 | mGrid.setAdapter(mAdapter); |
| 268 | |
| 269 | // Start the loader for the bookmark data |
| 270 | getLoaderManager().initLoader(LOADER_BOOKMARKS, null, this); |
| 271 | |
| 272 | // Add our own listener in case there are favicons that have yet to be loaded. |
| 273 | CombinedBookmarkHistoryActivity.getIconListenerSet().addListener(this); |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 274 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 275 | |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 276 | @Override |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 277 | public void onReceivedIcon(String url, Bitmap icon) { |
| 278 | // A new favicon has been loaded, so let anything attached to the adapter know about it |
| 279 | // so new icons will be loaded. |
| 280 | mAdapter.notifyDataSetChanged(); |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 283 | @Override |
| 284 | public void onItemClick(AdapterView parent, View v, int position, long id) { |
| 285 | // It is possible that the view has been canceled when we get to |
| 286 | // this point as back has a higher priority |
| 287 | if (mCanceled) { |
| 288 | android.util.Log.e(LOGTAG, "item clicked when dismissing"); |
| 289 | return; |
| 290 | } |
| 291 | if (mCreateShortcut) { |
| 292 | setResultToParent(RESULT_OK, createShortcutIntent(position)); |
| 293 | finish(); |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 297 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 298 | boolean isFolder = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; |
| 299 | if (!isFolder) { |
| 300 | loadUrl(position); |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 301 | } else { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 302 | String title; |
| 303 | if (mFolderStack.size() != 0) { |
| 304 | title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
| 305 | } else { |
| 306 | // TODO localize |
| 307 | title = "Bookmarks"; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 308 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 309 | LoaderManager manager = getLoaderManager(); |
| 310 | BookmarksLoader loader = |
| 311 | (BookmarksLoader) ((Loader) manager.getLoader(LOADER_BOOKMARKS)); |
| 312 | mFolderStack.push(new Pair(title, loader.getUri())); |
| 313 | Uri uri = ContentUris.withAppendedId( |
| 314 | BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER, id); |
| 315 | loader.setUri(uri); |
| 316 | loader.forceLoad(); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 317 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 320 | private Intent createShortcutIntent(int position) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 321 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 322 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
| 323 | String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
| 324 | Bitmap touchIcon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_TOUCH_ICON); |
| 325 | Bitmap favicon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON); |
Ben Murdoch | 90b4026 | 2010-06-30 13:33:28 +0100 | [diff] [blame] | 326 | return BookmarkUtils.createAddToHomeIntent(this, url, title, touchIcon, favicon); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 327 | } |
| 328 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 329 | private void loadUrl(int position) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 330 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 331 | Intent intent = new Intent(cursor.getString(BookmarksLoader.COLUMN_INDEX_URL)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 332 | setResultToParent(RESULT_OK, intent); |
| 333 | finish(); |
| 334 | } |
| 335 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 336 | private void openInNewWindow(int position) { |
| 337 | Bundle b = new Bundle(); |
| 338 | b.putBoolean("new_window", true); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 339 | setResultToParent(RESULT_OK, (new Intent(getUrl(position))).putExtras(b)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 340 | finish(); |
| 341 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 342 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 343 | private void editBookmark(int position) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 344 | Intent intent = new Intent(this, AddBookmarkPage.class); |
| 345 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 346 | Bundle item = new Bundle(); |
| 347 | item.putString(Browser.BookmarkColumns.TITLE, |
| 348 | cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)); |
| 349 | item.putString(Browser.BookmarkColumns.URL, |
| 350 | cursor.getString(BookmarksLoader.COLUMN_INDEX_URL)); |
| 351 | byte[] data = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON); |
| 352 | if (data != null) { |
| 353 | item.putParcelable(Browser.BookmarkColumns.FAVICON, |
| 354 | BitmapFactory.decodeByteArray(data, 0, data.length)); |
| 355 | } |
| 356 | item.putInt("id", cursor.getInt(BookmarksLoader.COLUMN_INDEX_ID)); |
| 357 | intent.putExtra("bookmark", item); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 358 | startActivityForResult(intent, BOOKMARKS_SAVE); |
| 359 | } |
| 360 | |
| 361 | @Override |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 362 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 363 | switch(requestCode) { |
| 364 | case BOOKMARKS_SAVE: |
| 365 | if (resultCode == RESULT_OK) { |
| 366 | Bundle extras; |
| 367 | if (data != null && (extras = data.getExtras()) != null) { |
| 368 | // If there are extras, then we need to save |
| 369 | // the edited bookmark. This is done in updateRow() |
| 370 | String title = extras.getString("title"); |
| 371 | String url = extras.getString("url"); |
| 372 | if (title != null && url != null) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 373 | updateRow(extras); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 374 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | break; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 380 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 381 | /** |
| 382 | * Update a row in the database with new information. |
| 383 | * Requeries the database if the information has changed. |
| 384 | * @param map Bundle storing id, title and url of new information |
| 385 | */ |
| 386 | public void updateRow(Bundle map) { |
| 387 | |
| 388 | // Find the record |
| 389 | int id = map.getInt("id"); |
| 390 | int position = -1; |
| 391 | Cursor cursor = mAdapter.getCursor(); |
| 392 | for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { |
| 393 | if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_ID) == id) { |
| 394 | position = cursor.getPosition(); |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | if (position < 0) { |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | cursor.moveToPosition(position); |
| 403 | ContentValues values = new ContentValues(); |
| 404 | String title = map.getString(Browser.BookmarkColumns.TITLE); |
| 405 | if (!title.equals(cursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX))) { |
| 406 | values.put(Browser.BookmarkColumns.TITLE, title); |
| 407 | } |
| 408 | String url = map.getString(Browser.BookmarkColumns.URL); |
| 409 | if (!url.equals(cursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX))) { |
| 410 | values.put(Browser.BookmarkColumns.URL, url); |
| 411 | } |
| 412 | |
| 413 | if (map.getBoolean("invalidateThumbnail") == true) { |
| 414 | values.put(Browser.BookmarkColumns.THUMBNAIL, new byte[0]); |
| 415 | } |
| 416 | |
| 417 | if (values.size() > 0) { |
| 418 | getContentResolver().update(Browser.BOOKMARKS_URI, values, |
| 419 | "_id = ?", new String[] { Integer.toString(id) }); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | private void displayRemoveBookmarkDialog(final int position) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 424 | // Put up a dialog asking if the user really wants to |
| 425 | // delete the bookmark |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 426 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 427 | new AlertDialog.Builder(this) |
| 428 | .setTitle(R.string.delete_bookmark) |
| 429 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 430 | .setMessage(getText(R.string.delete_bookmark_warning).toString().replace( |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 431 | "%s", cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE))) |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 432 | .setPositiveButton(R.string.ok, |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 433 | new DialogInterface.OnClickListener() { |
| 434 | public void onClick(DialogInterface dialog, int whichButton) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 435 | deleteBookmark(position); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 436 | } |
| 437 | }) |
| 438 | .setNegativeButton(R.string.cancel, null) |
| 439 | .show(); |
| 440 | } |
| 441 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 442 | private String getUrl(int position) { |
| 443 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 444 | return cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 445 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 446 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 447 | private void copy(CharSequence text) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 448 | ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); |
| 449 | cm.setPrimaryClip(new ClippedData(null, null, new ClippedData.Item(text))); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Delete the currently highlighted row. |
| 454 | */ |
| 455 | public void deleteBookmark(int position) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 456 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 457 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
| 458 | String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
| 459 | Bookmarks.removeFromBookmarks(null, getContentResolver(), url, title); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 460 | } |
Grace Kloba | 5942df0 | 2009-09-18 11:48:29 -0700 | [diff] [blame] | 461 | |
| 462 | @Override |
| 463 | public void onBackPressed() { |
| 464 | setResultToParent(RESULT_CANCELED, null); |
| 465 | mCanceled = true; |
| 466 | super.onBackPressed(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Leon Scroggins | fde9746 | 2010-01-11 13:06:21 -0500 | [diff] [blame] | 469 | // This Activity is generally a sub-Activity of |
| 470 | // CombinedBookmarkHistoryActivity. In that situation, we need to pass our |
| 471 | // result code up to our parent. However, if someone calls this Activity |
| 472 | // directly, then this has no parent, and it needs to set it on itself. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 473 | private void setResultToParent(int resultCode, Intent data) { |
Leon Scroggins | fde9746 | 2010-01-11 13:06:21 -0500 | [diff] [blame] | 474 | Activity parent = getParent(); |
| 475 | if (parent == null) { |
| 476 | setResult(resultCode, data); |
| 477 | } else { |
| 478 | ((CombinedBookmarkHistoryActivity) parent).setResultFromChild( |
| 479 | resultCode, data); |
| 480 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 481 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 482 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 483 | } |