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