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