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