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 | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 25 | import android.content.ContentResolver; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 26 | import android.content.ContentUris; |
| 27 | import android.content.ContentValues; |
Dianne Hackborn | 80f3262 | 2010-08-05 14:17:53 -0700 | [diff] [blame] | 28 | import android.content.Context; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 29 | import android.content.CursorLoader; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 30 | import android.content.DialogInterface; |
| 31 | import android.content.Intent; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 32 | import android.content.Loader; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 33 | import android.content.SharedPreferences; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 34 | import android.database.Cursor; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 35 | import android.graphics.Bitmap; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 36 | import android.graphics.BitmapFactory; |
| 37 | import android.net.Uri; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | import android.os.Bundle; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 39 | import android.preference.PreferenceManager; |
Jeff Hamilton | 69bd707 | 2010-08-17 12:38:22 -0500 | [diff] [blame] | 40 | import android.provider.BrowserContract; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 41 | import android.provider.BrowserContract.Accounts; |
| 42 | import android.text.TextUtils; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 43 | import android.util.Pair; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 44 | import android.view.ContextMenu; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 45 | import android.view.ContextMenu.ContextMenuInfo; |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 46 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 47 | import android.view.MenuInflater; |
| 48 | import android.view.MenuItem; |
| 49 | import android.view.View; |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 50 | import android.view.View.OnClickListener; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 51 | import android.view.ViewGroup; |
Patrick Scott | c1cf63a | 2010-03-09 16:02:08 -0500 | [diff] [blame] | 52 | import android.webkit.WebIconDatabase.IconListener; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 53 | import android.widget.Adapter; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | import android.widget.AdapterView; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 55 | import android.widget.AdapterView.OnItemClickListener; |
| 56 | import android.widget.AdapterView.OnItemSelectedListener; |
| 57 | import android.widget.ArrayAdapter; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 58 | import android.widget.Button; |
| 59 | import android.widget.GridView; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 60 | import android.widget.Spinner; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 61 | import android.widget.Toast; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 62 | |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 63 | import java.util.ArrayList; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 64 | import java.util.Stack; |
| 65 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 66 | /** |
| 67 | * View showing the user's bookmarks in the browser. |
| 68 | */ |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 69 | public class BrowserBookmarksPage extends Fragment implements View.OnCreateContextMenuListener, |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 70 | LoaderManager.LoaderCallbacks<Cursor>, OnItemClickListener, IconListener, OnClickListener, |
| 71 | OnItemSelectedListener { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 72 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 73 | static final int BOOKMARKS_SAVE = 1; |
| 74 | static final String LOGTAG = "browser"; |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 75 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 76 | static final int LOADER_BOOKMARKS = 1; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 77 | static final int LOADER_ACCOUNTS = 2; |
| 78 | static final int LOADER_ACCOUNTS_THEN_BOOKMARKS = 3; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 79 | |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 80 | static final String EXTRA_SHORTCUT = "create_shortcut"; |
| 81 | static final String EXTRA_DISABLE_WINDOW = "disable_new_window"; |
| 82 | |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 83 | static final String PREF_ACCOUNT_TYPE = "acct_type"; |
| 84 | static final String PREF_ACCOUNT_NAME = "acct_name"; |
| 85 | |
| 86 | static final String DEFAULT_ACCOUNT = "local"; |
| 87 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 88 | BookmarksHistoryCallbacks mCallbacks; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 89 | GridView mGrid; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 90 | Spinner mAccountSelector; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 91 | BrowserBookmarksAdapter mAdapter; |
| 92 | boolean mDisableNewWindow; |
| 93 | BookmarkItem mContextHeader; |
| 94 | boolean mCanceled = false; |
| 95 | boolean mCreateShortcut; |
| 96 | View mEmptyView; |
| 97 | View mContentView; |
| 98 | Stack<Pair<String, Uri>> mFolderStack = new Stack<Pair<String, Uri>>(); |
| 99 | Button mUpButton; |
| 100 | |
| 101 | @Override |
| 102 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 103 | switch (id) { |
| 104 | case LOADER_BOOKMARKS: { |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 105 | String accountType = null; |
| 106 | String accountName = null; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 107 | if (args != null) { |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 108 | accountType = args.getString(BookmarksLoader.ARG_ACCOUNT_TYPE); |
| 109 | accountName = args.getString(BookmarksLoader.ARG_ACCOUNT_NAME); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 110 | } |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 111 | return new BookmarksLoader(getActivity(), accountType, accountName); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 112 | } |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 113 | |
| 114 | case LOADER_ACCOUNTS: |
| 115 | case LOADER_ACCOUNTS_THEN_BOOKMARKS: { |
| 116 | return new CursorLoader(getActivity(), Accounts.CONTENT_URI, |
| 117 | new String[] { Accounts.ACCOUNT_TYPE, Accounts.ACCOUNT_NAME }, null, null, |
| 118 | null); |
| 119 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 120 | } |
| 121 | throw new UnsupportedOperationException("Unknown loader id " + id); |
| 122 | } |
| 123 | |
| 124 | @Override |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 125 | public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { |
| 126 | switch (loader.getId()) { |
| 127 | case LOADER_BOOKMARKS: { |
| 128 | // Set the visibility of the empty vs. content views |
| 129 | if (cursor == null || cursor.getCount() == 0) { |
| 130 | mEmptyView.setVisibility(View.VISIBLE); |
| 131 | mGrid.setVisibility(View.GONE); |
| 132 | } else { |
| 133 | mEmptyView.setVisibility(View.GONE); |
| 134 | mGrid.setVisibility(View.VISIBLE); |
| 135 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 136 | |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 137 | // Fill in the "up" button if needed |
| 138 | BookmarksLoader bl = (BookmarksLoader) loader; |
| 139 | String path = bl.getUri().getPath(); |
| 140 | boolean rootFolder = |
| 141 | BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER.getPath().equals(path); |
| 142 | if (rootFolder) { |
| 143 | mUpButton.setText(R.string.defaultBookmarksUpButton); |
| 144 | mUpButton.setEnabled(false); |
| 145 | } else { |
| 146 | mUpButton.setText(mFolderStack.peek().first); |
| 147 | mUpButton.setEnabled(true); |
| 148 | } |
| 149 | mUpButton.setVisibility(View.VISIBLE); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 150 | |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 151 | // Give the new data to the adapter |
| 152 | mAdapter.changeCursor(cursor); |
| 153 | |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | case LOADER_ACCOUNTS: |
| 158 | case LOADER_ACCOUNTS_THEN_BOOKMARKS: { |
| 159 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( |
| 160 | getActivity()); |
| 161 | String storedAccountType = prefs.getString(PREF_ACCOUNT_TYPE, null); |
| 162 | String storedAccountName = prefs.getString(PREF_ACCOUNT_NAME, null); |
| 163 | String accountType = |
| 164 | TextUtils.isEmpty(storedAccountType) ? DEFAULT_ACCOUNT : storedAccountType; |
| 165 | String accountName = |
| 166 | TextUtils.isEmpty(storedAccountName) ? DEFAULT_ACCOUNT : storedAccountName; |
| 167 | |
| 168 | Bundle args = null; |
| 169 | if (cursor == null || !cursor.moveToFirst()) { |
| 170 | // No accounts, set the prefs to the default |
| 171 | accountType = DEFAULT_ACCOUNT; |
| 172 | accountName = DEFAULT_ACCOUNT; |
| 173 | mAccountSelector.setVisibility(View.GONE); |
| 174 | } else { |
| 175 | int accountPosition = -1; |
| 176 | |
| 177 | if (!DEFAULT_ACCOUNT.equals(accountType) && |
| 178 | !DEFAULT_ACCOUNT.equals(accountName)) { |
| 179 | // Check to see if the account in prefs still exists |
| 180 | cursor.moveToFirst(); |
| 181 | do { |
| 182 | if (accountType.equals(cursor.getString(0)) |
| 183 | && accountName.equals(cursor.getString(1))) { |
| 184 | accountPosition = cursor.getPosition(); |
| 185 | break; |
| 186 | } |
| 187 | } while (cursor.moveToNext()); |
| 188 | } |
| 189 | |
| 190 | if (accountPosition == -1) { |
| 191 | // No account is set in prefs and there is at least one, |
| 192 | // so pick the first one as the default |
| 193 | cursor.moveToFirst(); |
| 194 | accountType = cursor.getString(0); |
| 195 | accountName = cursor.getString(1); |
| 196 | accountPosition = 0; |
| 197 | } |
| 198 | |
| 199 | args = new Bundle(); |
| 200 | args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType); |
| 201 | args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName); |
| 202 | |
| 203 | // Setup the account selector if there is more than 1 account |
| 204 | if (cursor.getCount() > 1) { |
| 205 | ArrayList<String> accounts = new ArrayList<String>(); |
| 206 | cursor.moveToFirst(); |
| 207 | do { |
| 208 | accounts.add(cursor.getString(1)); |
| 209 | } while (cursor.moveToNext()); |
| 210 | |
| 211 | mAccountSelector.setAdapter(new ArrayAdapter<String>(getActivity(), |
| 212 | android.R.layout.simple_list_item_1, android.R.id.text1, accounts)); |
| 213 | mAccountSelector.setVisibility(View.VISIBLE); |
| 214 | mAccountSelector.setSelection(accountPosition); |
| 215 | } |
| 216 | } |
| 217 | if (!accountType.equals(storedAccountType) |
| 218 | || !accountName.equals(storedAccountName)) { |
| 219 | prefs.edit() |
| 220 | .putString(PREF_ACCOUNT_TYPE, accountType) |
| 221 | .putString(PREF_ACCOUNT_NAME, accountName) |
| 222 | .apply(); |
| 223 | } |
| 224 | if (loader.getId() == LOADER_ACCOUNTS_THEN_BOOKMARKS) { |
| 225 | getLoaderManager().initLoader(LOADER_BOOKMARKS, args, this); |
| 226 | } |
| 227 | |
| 228 | break; |
| 229 | } |
| 230 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void onClick(View view) { |
| 235 | if (view == mUpButton) { |
| 236 | Pair<String, Uri> pair = mFolderStack.pop(); |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 237 | BookmarksLoader loader = |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 238 | (BookmarksLoader) ((Loader) getLoaderManager().getLoader(LOADER_BOOKMARKS)); |
| 239 | loader.setUri(pair.second); |
| 240 | loader.forceLoad(); |
| 241 | } |
| 242 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 243 | |
| 244 | @Override |
| 245 | public boolean onContextItemSelected(MenuItem item) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 246 | final Activity activity = getActivity(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 247 | // 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] | 248 | // this point as back has a higher priority |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 249 | if (mCanceled) { |
| 250 | return true; |
| 251 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 252 | AdapterView.AdapterContextMenuInfo i = |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 253 | (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); |
| 254 | // If we have no menu info, we can't tell which item was selected. |
| 255 | if (i == null) { |
| 256 | return true; |
| 257 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 258 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 259 | switch (item.getItemId()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 260 | case R.id.open_context_menu_id: |
| 261 | loadUrl(i.position); |
| 262 | break; |
| 263 | case R.id.edit_context_menu_id: |
| 264 | editBookmark(i.position); |
| 265 | break; |
| 266 | case R.id.shortcut_context_menu_id: |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 267 | activity.sendBroadcast(createShortcutIntent(i.position)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 268 | break; |
| 269 | case R.id.delete_context_menu_id: |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 270 | displayRemoveBookmarkDialog(i.position); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 271 | break; |
| 272 | case R.id.new_window_context_menu_id: |
| 273 | openInNewWindow(i.position); |
| 274 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 275 | case R.id.share_link_context_menu_id: { |
| 276 | Cursor cursor = (Cursor) mAdapter.getItem(i.position); |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 277 | BrowserActivity.sharePage(activity, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 278 | cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE), |
| 279 | cursor.getString(BookmarksLoader.COLUMN_INDEX_URL), |
| 280 | getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON), |
| 281 | getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_THUMBNAIL)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 282 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 283 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 284 | case R.id.copy_url_context_menu_id: |
| 285 | copy(getUrl(i.position)); |
Leon Scroggins | feb941d | 2009-05-28 17:27:38 -0400 | [diff] [blame] | 286 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 287 | case R.id.homepage_context_menu_id: { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 288 | BrowserSettings.getInstance().setHomePage(activity, getUrl(i.position)); |
| 289 | Toast.makeText(activity, R.string.homepage_set, Toast.LENGTH_LONG).show(); |
Leon Scroggins | feb941d | 2009-05-28 17:27:38 -0400 | [diff] [blame] | 290 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 291 | } |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 292 | // Only for the Most visited page |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 293 | case R.id.save_to_bookmarks_menu_id: { |
| 294 | Cursor cursor = (Cursor) mAdapter.getItem(i.position); |
| 295 | String name = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
| 296 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 297 | // If the site is bookmarked, the item becomes remove from |
| 298 | // bookmarks. |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 299 | Bookmarks.removeFromBookmarks(activity, activity.getContentResolver(), url, name); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 300 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 301 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 302 | default: |
| 303 | return super.onContextItemSelected(item); |
| 304 | } |
| 305 | return true; |
| 306 | } |
| 307 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 308 | Bitmap getBitmap(Cursor cursor, int columnIndex) { |
| 309 | byte[] data = cursor.getBlob(columnIndex); |
| 310 | if (data == null) { |
| 311 | return null; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 312 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 313 | return BitmapFactory.decodeByteArray(data, 0, data.length); |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
| 318 | AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 319 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 320 | final Activity activity = getActivity(); |
| 321 | MenuInflater inflater = activity.getMenuInflater(); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 322 | inflater.inflate(R.menu.bookmarkscontext, menu); |
| 323 | |
| 324 | if (mDisableNewWindow) { |
| 325 | menu.findItem(R.id.new_window_context_menu_id).setVisible(false); |
| 326 | } |
| 327 | |
| 328 | if (mContextHeader == null) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 329 | mContextHeader = new BookmarkItem(activity); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 330 | } else if (mContextHeader.getParent() != null) { |
| 331 | ((ViewGroup) mContextHeader.getParent()).removeView(mContextHeader); |
| 332 | } |
| 333 | |
| 334 | populateBookmarkItem(mAdapter, mContextHeader, info.position); |
| 335 | |
| 336 | menu.setHeaderView(mContextHeader); |
| 337 | } |
| 338 | |
| 339 | private void populateBookmarkItem(BrowserBookmarksAdapter adapter, BookmarkItem item, |
| 340 | int position) { |
| 341 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 342 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
| 343 | item.setUrl(url); |
| 344 | item.setName(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)); |
| 345 | Bitmap bitmap = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON); |
| 346 | if (bitmap == null) { |
| 347 | bitmap = CombinedBookmarkHistoryActivity.getIconListenerSet().getFavicon(url); |
| 348 | } |
| 349 | item.setFavicon(bitmap); |
| 350 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 351 | |
| 352 | /** |
| 353 | * Create a new BrowserBookmarksPage. |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 354 | */ |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 355 | @Override |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 356 | public void onCreate(Bundle icicle) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 357 | super.onCreate(icicle); |
| 358 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 359 | Bundle args = getArguments(); |
| 360 | mCreateShortcut = args == null ? false : args.getBoolean("create_shortcut", false); |
| 361 | mDisableNewWindow = args == null ? false : args.getBoolean("disable_new_window", false); |
| 362 | } |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 363 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 364 | @Override |
| 365 | public void onAttach(Activity activity) { |
| 366 | super.onAttach(activity); |
| 367 | mCallbacks = (BookmarksHistoryCallbacks) activity; |
| 368 | } |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 369 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 370 | @Override |
| 371 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 372 | Bundle savedInstanceState) { |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 373 | Context context = getActivity(); |
| 374 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 375 | View root = inflater.inflate(R.layout.bookmarks, container, false); |
| 376 | mEmptyView = root.findViewById(android.R.id.empty); |
| 377 | mContentView = root.findViewById(android.R.id.content); |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 378 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 379 | mGrid = (GridView) root.findViewById(R.id.grid); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 380 | mGrid.setOnItemClickListener(this); |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 381 | mGrid.setColumnWidth(BrowserActivity.getDesiredThumbnailWidth(getActivity())); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 382 | if (!mCreateShortcut) { |
| 383 | mGrid.setOnCreateContextMenuListener(this); |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 384 | } |
Patrick Scott | f49ecd6 | 2010-04-09 12:43:26 -0400 | [diff] [blame] | 385 | |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 386 | mAccountSelector = (Spinner) root.findViewById(R.id.accounts); |
| 387 | mAccountSelector.setOnItemSelectedListener(this); |
| 388 | mAccountSelector.setVisibility(View.INVISIBLE); |
| 389 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 390 | mUpButton = (Button) root.findViewById(R.id.up); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 391 | mUpButton.setEnabled(false); |
| 392 | mUpButton.setOnClickListener(this); |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 393 | mUpButton.setVisibility(View.GONE); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 394 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 395 | mAdapter = new BrowserBookmarksAdapter(getActivity()); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 396 | mGrid.setAdapter(mAdapter); |
| 397 | |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 398 | // Start the loaders |
| 399 | LoaderManager lm = getLoaderManager(); |
| 400 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); |
| 401 | String accountType = prefs.getString(PREF_ACCOUNT_TYPE, null); |
| 402 | String accountName = prefs.getString(PREF_ACCOUNT_NAME, null); |
| 403 | if (!TextUtils.isEmpty(accountType) && !TextUtils.isEmpty(accountName)) { |
| 404 | // There is an account set, load up that one |
| 405 | Bundle args = null; |
| 406 | if (!DEFAULT_ACCOUNT.equals(accountType) && !DEFAULT_ACCOUNT.equals(accountName)) { |
| 407 | args = new Bundle(); |
| 408 | args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType); |
| 409 | args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName); |
| 410 | } |
| 411 | lm.initLoader(LOADER_BOOKMARKS, args, this); |
| 412 | lm.initLoader(LOADER_ACCOUNTS, null, this); |
| 413 | } else { |
| 414 | // No account set, load them first |
| 415 | lm.initLoader(LOADER_ACCOUNTS_THEN_BOOKMARKS, null, this); |
| 416 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 417 | |
| 418 | // Add our own listener in case there are favicons that have yet to be loaded. |
| 419 | CombinedBookmarkHistoryActivity.getIconListenerSet().addListener(this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 420 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 421 | return root; |
| 422 | } |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 423 | |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 424 | @Override |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 425 | public void onReceivedIcon(String url, Bitmap icon) { |
| 426 | // A new favicon has been loaded, so let anything attached to the adapter know about it |
| 427 | // so new icons will be loaded. |
| 428 | mAdapter.notifyDataSetChanged(); |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 431 | @Override |
| 432 | public void onItemClick(AdapterView parent, View v, int position, long id) { |
| 433 | // It is possible that the view has been canceled when we get to |
| 434 | // this point as back has a higher priority |
| 435 | if (mCanceled) { |
| 436 | android.util.Log.e(LOGTAG, "item clicked when dismissing"); |
| 437 | return; |
| 438 | } |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 439 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 440 | if (mCreateShortcut) { |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 441 | Intent intent = createShortcutIntent(position); |
| 442 | // the activity handles the intent in startActivityFromFragment |
| 443 | startActivity(intent); |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 444 | return; |
| 445 | } |
| 446 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 447 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 448 | boolean isFolder = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; |
| 449 | if (!isFolder) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 450 | mCallbacks.onUrlSelected(getUrl(position), false); |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 451 | } else { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 452 | String title; |
| 453 | if (mFolderStack.size() != 0) { |
| 454 | title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
| 455 | } else { |
| 456 | // TODO localize |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 457 | title = "Bookmarks"; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 458 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 459 | LoaderManager manager = getLoaderManager(); |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 460 | BookmarksLoader loader = |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 461 | (BookmarksLoader) ((Loader) manager.getLoader(LOADER_BOOKMARKS)); |
| 462 | mFolderStack.push(new Pair(title, loader.getUri())); |
| 463 | Uri uri = ContentUris.withAppendedId( |
| 464 | BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER, id); |
| 465 | loader.setUri(uri); |
| 466 | loader.forceLoad(); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 467 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame^] | 470 | @Override |
| 471 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
| 472 | Adapter adapter = parent.getAdapter(); |
| 473 | String accountType = "com.google"; |
| 474 | String accountName = adapter.getItem(position).toString(); |
| 475 | |
| 476 | // Remember the selection for later |
| 477 | PreferenceManager.getDefaultSharedPreferences(getActivity()).edit() |
| 478 | .putString(PREF_ACCOUNT_TYPE, accountType) |
| 479 | .putString(PREF_ACCOUNT_NAME, accountName) |
| 480 | .apply(); |
| 481 | |
| 482 | Bundle args = new Bundle(); |
| 483 | args.putString(BookmarksLoader.ARG_ACCOUNT_TYPE, accountType); |
| 484 | args.putString(BookmarksLoader.ARG_ACCOUNT_NAME, accountName); |
| 485 | getLoaderManager().restartLoader(LOADER_BOOKMARKS, args, this); |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public void onNothingSelected(AdapterView<?> parent) { |
| 490 | // Do nothing |
| 491 | } |
| 492 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 493 | private Intent createShortcutIntent(int position) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 494 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 495 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
| 496 | String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
| 497 | Bitmap touchIcon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_TOUCH_ICON); |
| 498 | Bitmap favicon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON); |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 499 | return BookmarkUtils.createAddToHomeIntent(getActivity(), url, title, touchIcon, favicon); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 500 | } |
| 501 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 502 | private void loadUrl(int position) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 503 | mCallbacks.onUrlSelected(getUrl(position), false); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 504 | } |
| 505 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 506 | private void openInNewWindow(int position) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 507 | mCallbacks.onUrlSelected(getUrl(position), true); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 508 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 509 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 510 | private void editBookmark(int position) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 511 | Intent intent = new Intent(getActivity(), AddBookmarkPage.class); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 512 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 513 | Bundle item = new Bundle(); |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 514 | item.putString(BrowserContract.Bookmarks.TITLE, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 515 | cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)); |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 516 | item.putString(BrowserContract.Bookmarks.URL, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 517 | cursor.getString(BookmarksLoader.COLUMN_INDEX_URL)); |
| 518 | byte[] data = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON); |
| 519 | if (data != null) { |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 520 | item.putParcelable(BrowserContract.Bookmarks.FAVICON, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 521 | BitmapFactory.decodeByteArray(data, 0, data.length)); |
| 522 | } |
| 523 | item.putInt("id", cursor.getInt(BookmarksLoader.COLUMN_INDEX_ID)); |
| 524 | intent.putExtra("bookmark", item); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 525 | startActivityForResult(intent, BOOKMARKS_SAVE); |
| 526 | } |
| 527 | |
| 528 | @Override |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 529 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 530 | switch(requestCode) { |
| 531 | case BOOKMARKS_SAVE: |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 532 | if (resultCode == Activity.RESULT_OK) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 533 | Bundle extras; |
| 534 | if (data != null && (extras = data.getExtras()) != null) { |
| 535 | // If there are extras, then we need to save |
| 536 | // the edited bookmark. This is done in updateRow() |
| 537 | String title = extras.getString("title"); |
| 538 | String url = extras.getString("url"); |
| 539 | if (title != null && url != null) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 540 | updateRow(extras); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 541 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | break; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 545 | } |
| 546 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 547 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 548 | /** |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 549 | * Update a row in the database with new information. |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 550 | * @param map Bundle storing id, title and url of new information |
| 551 | */ |
| 552 | public void updateRow(Bundle map) { |
| 553 | |
| 554 | // Find the record |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 555 | int id = map.getInt("id"); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 556 | int position = -1; |
| 557 | Cursor cursor = mAdapter.getCursor(); |
| 558 | for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { |
| 559 | if (cursor.getInt(BookmarksLoader.COLUMN_INDEX_ID) == id) { |
| 560 | position = cursor.getPosition(); |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | if (position < 0) { |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | cursor.moveToPosition(position); |
| 569 | ContentValues values = new ContentValues(); |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 570 | String title = map.getString(BrowserContract.Bookmarks.TITLE); |
| 571 | if (!title.equals(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE))) { |
| 572 | values.put(BrowserContract.Bookmarks.TITLE, title); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 573 | } |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 574 | String url = map.getString(BrowserContract.Bookmarks.URL); |
| 575 | if (!url.equals(cursor.getString(BookmarksLoader.COLUMN_INDEX_URL))) { |
| 576 | values.put(BrowserContract.Bookmarks.URL, url); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | if (map.getBoolean("invalidateThumbnail") == true) { |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 580 | values.putNull(BrowserContract.Bookmarks.THUMBNAIL); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | if (values.size() > 0) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 584 | getActivity().getContentResolver().update( |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 585 | ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id), |
| 586 | values, null, null); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | private void displayRemoveBookmarkDialog(final int position) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 591 | // Put up a dialog asking if the user really wants to |
| 592 | // delete the bookmark |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 593 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 594 | Context context = getActivity(); |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 595 | final ContentResolver resolver = context.getContentResolver(); |
| 596 | final Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, |
| 597 | cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID)); |
| 598 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 599 | new AlertDialog.Builder(context) |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 600 | .setTitle(R.string.delete_bookmark) |
| 601 | .setIcon(android.R.drawable.ic_dialog_alert) |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 602 | .setMessage(context.getString(R.string.delete_bookmark_warning, |
| 603 | cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE))) |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 604 | .setPositiveButton(R.string.ok, |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 605 | new DialogInterface.OnClickListener() { |
| 606 | public void onClick(DialogInterface dialog, int whichButton) { |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 607 | resolver.delete(uri, null, null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 608 | } |
| 609 | }) |
| 610 | .setNegativeButton(R.string.cancel, null) |
| 611 | .show(); |
| 612 | } |
| 613 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 614 | private String getUrl(int position) { |
| 615 | Cursor cursor = (Cursor) mAdapter.getItem(position); |
| 616 | return cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 617 | } |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 618 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 619 | private void copy(CharSequence text) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 620 | ClipboardManager cm = (ClipboardManager) getActivity().getSystemService( |
| 621 | Context.CLIPBOARD_SERVICE); |
Dianne Hackborn | eb07b17 | 2010-08-26 22:18:14 -0700 | [diff] [blame] | 622 | 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] | 623 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 624 | } |