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