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