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