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