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