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