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