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