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