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 | 66302e5 | 2011-05-18 17:12:33 -0700 | [diff] [blame^] | 19 | import com.android.browser.BookmarkDragHandler.BookmarkDragController; |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 20 | import com.android.browser.view.BookmarkExpandableGridView; |
| 21 | import com.android.browser.view.BookmarkExpandableGridView.BookmarkContextMenuInfo; |
John Reck | d4893b0 | 2010-12-07 17:38:34 -0800 | [diff] [blame] | 22 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 23 | import android.app.Activity; |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 24 | import android.app.Fragment; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 25 | import android.app.LoaderManager; |
Dianne Hackborn | eb07b17 | 2010-08-26 22:18:14 -0700 | [diff] [blame] | 26 | import android.content.ClipData; |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 27 | import android.content.ClipboardManager; |
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; |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [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.Intent; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 32 | import android.content.Loader; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 33 | import android.content.SharedPreferences; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 34 | import android.content.SharedPreferences.Editor; |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 35 | import android.content.res.Configuration; |
| 36 | import android.content.res.Resources; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 37 | import android.database.Cursor; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | import android.graphics.Bitmap; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 39 | import android.graphics.BitmapFactory; |
| 40 | import android.net.Uri; |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 41 | import android.os.AsyncTask; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 42 | import android.os.Bundle; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 43 | import android.preference.PreferenceManager; |
Jeff Hamilton | 69bd707 | 2010-08-17 12:38:22 -0500 | [diff] [blame] | 44 | import android.provider.BrowserContract; |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 45 | import android.provider.BrowserContract.Accounts; |
John Reck | 1cebb44 | 2010-12-10 12:02:16 -0800 | [diff] [blame] | 46 | import android.provider.BrowserContract.ChromeSyncColumns; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 47 | import android.view.ContextMenu; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 48 | import android.view.ContextMenu.ContextMenuInfo; |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 49 | import android.view.LayoutInflater; |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 50 | import android.view.Menu; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 51 | import android.view.MenuInflater; |
| 52 | import android.view.MenuItem; |
| 53 | import android.view.View; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 54 | import android.view.ViewGroup; |
Patrick Scott | c1cf63a | 2010-03-09 16:02:08 -0500 | [diff] [blame] | 55 | import android.webkit.WebIconDatabase.IconListener; |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 56 | import android.widget.ExpandableListView; |
| 57 | import android.widget.ExpandableListView.OnChildClickListener; |
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; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 60 | import android.widget.Toast; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 61 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 62 | import java.util.HashMap; |
| 63 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 64 | interface BookmarksPageCallbacks { |
| 65 | // Return true if handled |
| 66 | boolean onBookmarkSelected(Cursor c, boolean isFolder); |
| 67 | // Return true if handled |
| 68 | boolean onOpenInNewWindow(Cursor c); |
| 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, |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 75 | LoaderManager.LoaderCallbacks<Cursor>, IconListener, |
| 76 | BreadCrumbView.Controller, OnMenuItemClickListener, OnChildClickListener { |
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 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 80 | static final int LOADER_ACCOUNTS = 1; |
| 81 | static final int LOADER_BOOKMARKS = 100; |
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 | 7f6cf3e | 2010-09-17 17:22:21 -0500 | [diff] [blame] | 85 | public static final String PREF_ACCOUNT_TYPE = "acct_type"; |
| 86 | public static final String PREF_ACCOUNT_NAME = "acct_name"; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 87 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 88 | static final String ACCOUNT_TYPE = "account_type"; |
| 89 | static final String ACCOUNT_NAME = "account_name"; |
| 90 | |
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; |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 96 | View mRoot; |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 97 | BookmarkExpandableGridView mGrid; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 98 | ListView mList; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 99 | boolean mDisableNewWindow; |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 100 | boolean mEnableContextMenu = true; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 101 | View mEmptyView; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 102 | int mCurrentView; |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 103 | View mHeader; |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 104 | HashMap<Integer, BrowserBookmarksAdapter> mBookmarkAdapters = new HashMap<Integer, BrowserBookmarksAdapter>(); |
John Reck | 66302e5 | 2011-05-18 17:12:33 -0700 | [diff] [blame^] | 105 | BookmarkDragHandler mDragHandler; |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 106 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 107 | static BrowserBookmarksPage newInstance(BookmarksPageCallbacks cb, |
| 108 | Bundle args, ViewGroup headerContainer) { |
Michael Kolb | e421c24 | 2010-10-04 19:29:01 -0700 | [diff] [blame] | 109 | BrowserBookmarksPage bbp = new BrowserBookmarksPage(); |
| 110 | bbp.mCallbacks = cb; |
| 111 | bbp.setArguments(args); |
| 112 | return bbp; |
| 113 | } |
| 114 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 115 | @Override |
| 116 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 117 | if (id == LOADER_ACCOUNTS) { |
| 118 | return new AccountsLoader(getActivity()); |
| 119 | } else if (id >= LOADER_BOOKMARKS) { |
| 120 | String accountType = args.getString(ACCOUNT_TYPE); |
| 121 | String accountName = args.getString(ACCOUNT_NAME); |
| 122 | BookmarksLoader bl = new BookmarksLoader(getActivity(), |
| 123 | accountType, accountName); |
| 124 | return bl; |
| 125 | } else { |
| 126 | throw new UnsupportedOperationException("Unknown loader id " + id); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 127 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 131 | public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 132 | if (loader.getId() == LOADER_ACCOUNTS) { |
| 133 | LoaderManager lm = getLoaderManager(); |
| 134 | int id = LOADER_BOOKMARKS; |
| 135 | while (cursor.moveToNext()) { |
| 136 | String accountName = cursor.getString(0); |
| 137 | String accountType = cursor.getString(1); |
| 138 | Bundle args = new Bundle(); |
| 139 | args.putString(ACCOUNT_NAME, accountName); |
| 140 | args.putString(ACCOUNT_TYPE, accountType); |
| 141 | BrowserBookmarksAdapter adapter = new BrowserBookmarksAdapter( |
| 142 | getActivity(), mCurrentView); |
| 143 | mBookmarkAdapters.put(id, adapter); |
| 144 | mGrid.addAccount(accountName, adapter); |
| 145 | lm.restartLoader(id, args, this); |
| 146 | id++; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 147 | } |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 148 | // TODO: Figure out what a reload of these means |
| 149 | // Currently, a reload is triggered whenever bookmarks change |
| 150 | // This is less than ideal |
| 151 | // It also causes UI flickering as a new adapter is created |
| 152 | // instead of re-using an existing one when the account_name is the |
| 153 | // same. |
| 154 | // For now, this is a one-shot load |
| 155 | getLoaderManager().destroyLoader(LOADER_ACCOUNTS); |
| 156 | } else if (loader.getId() >= LOADER_BOOKMARKS) { |
| 157 | BrowserBookmarksAdapter adapter = mBookmarkAdapters.get(loader.getId()); |
| 158 | adapter.changeCursor(cursor); |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 159 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 160 | } |
| 161 | |
John Reck | 522a438 | 2011-01-26 13:23:47 -0800 | [diff] [blame] | 162 | @Override |
Dianne Hackborn | 39772c8 | 2010-12-16 00:43:54 -0800 | [diff] [blame] | 163 | public void onLoaderReset(Loader<Cursor> loader) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 164 | // TODO: Figure out what to do here (if anything?) |
Dianne Hackborn | 39772c8 | 2010-12-16 00:43:54 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 167 | long getFolderId() { |
| 168 | LoaderManager manager = getLoaderManager(); |
| 169 | BookmarksLoader loader = |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 170 | (BookmarksLoader) ((Loader<?>)manager.getLoader(LOADER_BOOKMARKS)); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 171 | |
| 172 | Uri uri = loader.getUri(); |
| 173 | if (uri != null) { |
| 174 | try { |
| 175 | return ContentUris.parseId(uri); |
| 176 | } catch (NumberFormatException nfx) { |
| 177 | return -1; |
| 178 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 179 | } |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 180 | return -1; |
| 181 | } |
| 182 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 183 | @Override |
| 184 | public boolean onContextItemSelected(MenuItem item) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 185 | final Activity activity = getActivity(); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 186 | BookmarkContextMenuInfo i = (BookmarkContextMenuInfo)item.getMenuInfo(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 187 | // If we have no menu info, we can't tell which item was selected. |
| 188 | if (i == null) { |
Leon Scroggins | c8cae08 | 2010-11-03 18:51:46 -0400 | [diff] [blame] | 189 | return false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 190 | } |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 191 | BrowserBookmarksAdapter adapter = getChildAdapter(i.groupPosition); |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 192 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 193 | switch (item.getItemId()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 194 | case R.id.open_context_menu_id: |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 195 | loadUrl(adapter, i.childPosition); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 196 | break; |
| 197 | case R.id.edit_context_menu_id: |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 198 | editBookmark(adapter, i.childPosition); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 199 | break; |
| 200 | case R.id.shortcut_context_menu_id: |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 201 | Cursor c = adapter.getItem(i.childPosition); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 202 | activity.sendBroadcast(createShortcutIntent(getActivity(), c)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 203 | break; |
| 204 | case R.id.delete_context_menu_id: |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 205 | displayRemoveBookmarkDialog(adapter, i.childPosition); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 206 | break; |
| 207 | case R.id.new_window_context_menu_id: |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 208 | openInNewWindow(adapter, i.childPosition); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 209 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 210 | case R.id.share_link_context_menu_id: { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 211 | Cursor cursor = adapter.getItem(i.childPosition); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 212 | Controller.sharePage(activity, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 213 | cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE), |
| 214 | cursor.getString(BookmarksLoader.COLUMN_INDEX_URL), |
| 215 | getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON), |
| 216 | getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_THUMBNAIL)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 217 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 218 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 219 | case R.id.copy_url_context_menu_id: |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 220 | copy(getUrl(adapter, i.childPosition)); |
Leon Scroggins | feb941d | 2009-05-28 17:27:38 -0400 | [diff] [blame] | 221 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 222 | case R.id.homepage_context_menu_id: { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 223 | BrowserSettings.getInstance().setHomePage(getUrl(adapter, i.childPosition)); |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 224 | Toast.makeText(activity, R.string.homepage_set, Toast.LENGTH_LONG).show(); |
Leon Scroggins | feb941d | 2009-05-28 17:27:38 -0400 | [diff] [blame] | 225 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 226 | } |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 227 | // Only for the Most visited page |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 228 | case R.id.save_to_bookmarks_menu_id: { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 229 | Cursor cursor = adapter.getItem(i.childPosition); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 230 | String name = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
| 231 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 232 | // If the site is bookmarked, the item becomes remove from |
| 233 | // bookmarks. |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 234 | Bookmarks.removeFromBookmarks(activity, activity.getContentResolver(), url, name); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 235 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 236 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 237 | default: |
| 238 | return super.onContextItemSelected(item); |
| 239 | } |
| 240 | return true; |
| 241 | } |
| 242 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 243 | static Bitmap getBitmap(Cursor cursor, int columnIndex) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 244 | byte[] data = cursor.getBlob(columnIndex); |
| 245 | if (data == null) { |
| 246 | return null; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 247 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 248 | return BitmapFactory.decodeByteArray(data, 0, data.length); |
| 249 | } |
| 250 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 251 | private MenuItem.OnMenuItemClickListener mContextItemClickListener = |
| 252 | new MenuItem.OnMenuItemClickListener() { |
| 253 | @Override |
| 254 | public boolean onMenuItemClick(MenuItem item) { |
| 255 | return onContextItemSelected(item); |
| 256 | } |
| 257 | }; |
| 258 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 259 | @Override |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 260 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 261 | inflater.inflate(R.menu.bookmark, menu); |
John Reck | a5176f3 | 2011-05-17 12:35:37 -0700 | [diff] [blame] | 262 | if (!BrowserActivity.isTablet(getActivity())) { |
John Reck | 9d27ff5 | 2011-02-11 17:47:54 -0800 | [diff] [blame] | 263 | MenuItem item = menu.findItem(R.id.add_bookmark); |
| 264 | item.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); |
| 265 | } |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 269 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 270 | BookmarkContextMenuInfo info = (BookmarkContextMenuInfo) menuInfo; |
| 271 | BrowserBookmarksAdapter adapter = getChildAdapter(info.groupPosition); |
| 272 | Cursor cursor = adapter.getItem(info.childPosition); |
John Reck | 1cebb44 | 2010-12-10 12:02:16 -0800 | [diff] [blame] | 273 | if (!canEdit(cursor)) { |
| 274 | return; |
| 275 | } |
Leon Scroggins | 5e8a40f | 2010-09-27 17:13:35 -0400 | [diff] [blame] | 276 | boolean isFolder |
| 277 | = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 278 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 279 | final Activity activity = getActivity(); |
| 280 | MenuInflater inflater = activity.getMenuInflater(); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 281 | inflater.inflate(R.menu.bookmarkscontext, menu); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 282 | if (isFolder) { |
| 283 | menu.setGroupVisible(R.id.FOLDER_CONTEXT_MENU, true); |
| 284 | } else { |
| 285 | menu.setGroupVisible(R.id.BOOKMARK_CONTEXT_MENU, true); |
| 286 | if (mDisableNewWindow) { |
| 287 | menu.findItem(R.id.new_window_context_menu_id).setVisible(false); |
| 288 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 289 | } |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 290 | BookmarkItem header = new BookmarkItem(activity); |
| 291 | populateBookmarkItem(cursor, header, isFolder); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 292 | menu.setHeaderView(header); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 293 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 294 | int count = menu.size(); |
| 295 | for (int i = 0; i < count; i++) { |
| 296 | menu.getItem(i).setOnMenuItemClickListener(mContextItemClickListener); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 297 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 298 | } |
| 299 | |
John Reck | 1cebb44 | 2010-12-10 12:02:16 -0800 | [diff] [blame] | 300 | boolean canEdit(Cursor c) { |
| 301 | String unique = c.getString(BookmarksLoader.COLUMN_INDEX_SERVER_UNIQUE); |
| 302 | return !ChromeSyncColumns.FOLDER_NAME_OTHER_BOOKMARKS.equals(unique); |
| 303 | } |
| 304 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 305 | private void populateBookmarkItem(Cursor cursor, BookmarkItem item, boolean isFolder) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 306 | item.setName(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 307 | if (isFolder) { |
| 308 | item.setUrl(null); |
| 309 | Bitmap bitmap = |
Michael Kolb | 5a72f18 | 2011-01-13 20:35:06 -0800 | [diff] [blame] | 310 | BitmapFactory.decodeResource(getResources(), R.drawable.ic_folder_holo_dark); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 311 | item.setFavicon(bitmap); |
John Reck | 60e3336 | 2010-12-02 17:52:38 -0800 | [diff] [blame] | 312 | new LookupBookmarkCount(getActivity(), item) |
| 313 | .execute(cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID)); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 314 | } else { |
| 315 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
| 316 | item.setUrl(url); |
| 317 | Bitmap bitmap = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON); |
| 318 | if (bitmap == null) { |
| 319 | bitmap = CombinedBookmarkHistoryView.getIconListenerSet().getFavicon(url); |
| 320 | } |
| 321 | item.setFavicon(bitmap); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 322 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 323 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 324 | |
| 325 | /** |
| 326 | * Create a new BrowserBookmarksPage. |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 327 | */ |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 328 | @Override |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 329 | public void onCreate(Bundle icicle) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 330 | super.onCreate(icicle); |
John Reck | 8f295d1 | 2011-03-18 15:25:18 -0700 | [diff] [blame] | 331 | SharedPreferences prefs = PreferenceManager |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 332 | .getDefaultSharedPreferences(getActivity()); |
John Reck | 8f295d1 | 2011-03-18 15:25:18 -0700 | [diff] [blame] | 333 | mCurrentView = prefs.getInt(PREF_SELECTED_VIEW, getDefaultView()); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 334 | // TODO: Support list view |
| 335 | mCurrentView = VIEW_THUMBNAILS; |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 336 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 337 | Bundle args = getArguments(); |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 338 | mDisableNewWindow = args == null ? false : args.getBoolean(EXTRA_DISABLE_WINDOW, false); |
John Reck | 8f295d1 | 2011-03-18 15:25:18 -0700 | [diff] [blame] | 339 | |
| 340 | setHasOptionsMenu(true); |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 341 | } |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 342 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 343 | @Override |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 344 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 345 | Bundle savedInstanceState) { |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 346 | mRoot = inflater.inflate(R.layout.bookmarks, container, false); |
| 347 | mEmptyView = mRoot.findViewById(android.R.id.empty); |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 348 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 349 | mGrid = (BookmarkExpandableGridView) mRoot.findViewById(R.id.grid); |
| 350 | mGrid.setOnChildClickListener(this); |
| 351 | mGrid.setColumnWidthFromLayout(R.layout.bookmark_thumbnail); |
| 352 | mGrid.setBreadcrumbController(this); |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 353 | mList = (ListView) mRoot.findViewById(R.id.list); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 354 | // TODO: mList.setOnItemClickListener(this); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 355 | setEnableContextMenu(mEnableContextMenu); |
John Reck | 66302e5 | 2011-05-18 17:12:33 -0700 | [diff] [blame^] | 356 | mDragHandler = new BookmarkDragHandler(getActivity(), mDragController, |
| 357 | mGrid.getDragAdapter()); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 358 | |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 359 | // Start the loaders |
| 360 | LoaderManager lm = getLoaderManager(); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 361 | lm.restartLoader(LOADER_ACCOUNTS, null, this); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 362 | |
| 363 | // 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] | 364 | CombinedBookmarkHistoryView.getIconListenerSet().addListener(this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 365 | |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 366 | return mRoot; |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 367 | } |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 368 | |
John Reck | 9d27ff5 | 2011-02-11 17:47:54 -0800 | [diff] [blame] | 369 | private int getDefaultView() { |
John Reck | a5176f3 | 2011-05-17 12:35:37 -0700 | [diff] [blame] | 370 | if (BrowserActivity.isTablet(getActivity())) { |
John Reck | 9d27ff5 | 2011-02-11 17:47:54 -0800 | [diff] [blame] | 371 | return VIEW_THUMBNAILS; |
| 372 | } |
| 373 | return VIEW_LIST; |
| 374 | } |
| 375 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 376 | @Override |
| 377 | public void onDestroyView() { |
| 378 | super.onDestroyView(); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 379 | mGrid.setBreadcrumbController(null); |
| 380 | LoaderManager lm = getLoaderManager(); |
| 381 | lm.destroyLoader(LOADER_ACCOUNTS); |
| 382 | for (int id : mBookmarkAdapters.keySet()) { |
| 383 | lm.destroyLoader(id); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 384 | } |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 385 | mBookmarkAdapters.clear(); |
| 386 | |
John Reck | e3063ae | 2011-01-27 14:57:40 -0800 | [diff] [blame] | 387 | CombinedBookmarkHistoryView.getIconListenerSet().removeListener(this); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 390 | @Override |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 391 | public void onReceivedIcon(String url, Bitmap icon) { |
| 392 | // A new favicon has been loaded, so let anything attached to the adapter know about it |
| 393 | // so new icons will be loaded. |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 394 | // TODO: Notify all of data set changed |
| 395 | // TODO: Wait, is this even needed? Won't this trigger a DB change anyway? |
| 396 | } |
| 397 | |
| 398 | private BrowserBookmarksAdapter getChildAdapter(int groupPosition) { |
| 399 | if (mCurrentView == VIEW_THUMBNAILS) { |
| 400 | return mGrid.getChildAdapter(groupPosition); |
| 401 | } else { |
| 402 | // TODO: Support expandable list |
| 403 | return null; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | private BreadCrumbView getBreadCrumbs(int groupPosition) { |
| 408 | if (mCurrentView == VIEW_THUMBNAILS) { |
| 409 | return mGrid.getBreadCrumbs(groupPosition); |
| 410 | } else { |
| 411 | // TODO: Support expandable list |
| 412 | return null; |
| 413 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 416 | @Override |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 417 | public boolean onChildClick(ExpandableListView parent, View v, |
| 418 | int groupPosition, int childPosition, long id) { |
| 419 | BrowserBookmarksAdapter adapter = getChildAdapter(groupPosition); |
| 420 | Cursor cursor = adapter.getItem(childPosition); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 421 | boolean isFolder = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; |
| 422 | if (mCallbacks != null && |
| 423 | mCallbacks.onBookmarkSelected(cursor, isFolder)) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 424 | return true; |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 425 | } |
| 426 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 427 | // TODO: Folder stuff |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 428 | if (isFolder) { |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 429 | String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 430 | Uri uri = ContentUris.withAppendedId( |
| 431 | BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER, id); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 432 | BreadCrumbView crumbs = getBreadCrumbs(groupPosition); |
| 433 | if (crumbs != null) { |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 434 | // update crumbs |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 435 | crumbs.pushView(title, uri); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 436 | } |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 437 | loadFolder(groupPosition, uri); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 438 | } |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 439 | return true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 440 | } |
| 441 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 442 | /* package */ static Intent createShortcutIntent(Context context, Cursor cursor) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 443 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
Michael Kolb | ccaa896 | 2010-10-20 17:03:10 -0700 | [diff] [blame] | 444 | String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 445 | Bitmap touchIcon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_TOUCH_ICON); |
| 446 | Bitmap favicon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 447 | return BookmarkUtils.createAddToHomeIntent(context, url, title, touchIcon, favicon); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 448 | } |
| 449 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 450 | private void loadUrl(BrowserBookmarksAdapter adapter, int position) { |
| 451 | if (mCallbacks != null && adapter != null) { |
| 452 | mCallbacks.onBookmarkSelected(adapter.getItem(position), false); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 453 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 454 | } |
| 455 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 456 | private void openInNewWindow(BrowserBookmarksAdapter adapter, int position) { |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 457 | if (mCallbacks != null) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 458 | Cursor c = adapter.getItem(position); |
John Reck | 1b3c66e | 2011-01-11 13:20:32 -0800 | [diff] [blame] | 459 | boolean isFolder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) == 1; |
| 460 | if (isFolder) { |
| 461 | long id = c.getLong(BookmarksLoader.COLUMN_INDEX_ID); |
| 462 | new OpenAllInTabsTask(id).execute(); |
| 463 | } else { |
| 464 | mCallbacks.onOpenInNewWindow(c); |
| 465 | } |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 466 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 467 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 468 | |
John Reck | 1b3c66e | 2011-01-11 13:20:32 -0800 | [diff] [blame] | 469 | class OpenAllInTabsTask extends AsyncTask<Void, Void, Cursor> { |
| 470 | long mFolderId; |
| 471 | public OpenAllInTabsTask(long id) { |
| 472 | mFolderId = id; |
| 473 | } |
| 474 | |
| 475 | @Override |
| 476 | protected Cursor doInBackground(Void... params) { |
| 477 | Context c = getActivity(); |
| 478 | if (c == null) return null; |
| 479 | return c.getContentResolver().query(BookmarkUtils.getBookmarksUri(c), |
| 480 | BookmarksLoader.PROJECTION, BrowserContract.Bookmarks.PARENT + "=?", |
| 481 | new String[] { Long.toString(mFolderId) }, null); |
| 482 | } |
| 483 | |
| 484 | @Override |
| 485 | protected void onPostExecute(Cursor result) { |
| 486 | if (mCallbacks != null) { |
| 487 | while (result.moveToNext()) { |
| 488 | mCallbacks.onOpenInNewWindow(result); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | } |
| 494 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 495 | private void editBookmark(BrowserBookmarksAdapter adapter, int position) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 496 | Intent intent = new Intent(getActivity(), AddBookmarkPage.class); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 497 | Cursor cursor = adapter.getItem(position); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 498 | Bundle item = new Bundle(); |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 499 | item.putString(BrowserContract.Bookmarks.TITLE, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 500 | cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)); |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 501 | item.putString(BrowserContract.Bookmarks.URL, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 502 | cursor.getString(BookmarksLoader.COLUMN_INDEX_URL)); |
| 503 | byte[] data = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON); |
| 504 | if (data != null) { |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 505 | item.putParcelable(BrowserContract.Bookmarks.FAVICON, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 506 | BitmapFactory.decodeByteArray(data, 0, data.length)); |
| 507 | } |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 508 | item.putLong(BrowserContract.Bookmarks._ID, |
| 509 | cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID)); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 510 | item.putLong(BrowserContract.Bookmarks.PARENT, |
| 511 | cursor.getLong(BookmarksLoader.COLUMN_INDEX_PARENT)); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 512 | intent.putExtra(AddBookmarkPage.EXTRA_EDIT_BOOKMARK, item); |
| 513 | intent.putExtra(AddBookmarkPage.EXTRA_IS_FOLDER, |
| 514 | cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) == 1); |
| 515 | startActivity(intent); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 516 | } |
| 517 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 518 | private void displayRemoveBookmarkDialog(BrowserBookmarksAdapter adapter, |
| 519 | int position) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 520 | // Put up a dialog asking if the user really wants to |
| 521 | // delete the bookmark |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 522 | Cursor cursor = adapter.getItem(position); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 523 | long id = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID); |
| 524 | String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 525 | Context context = getActivity(); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 526 | BookmarkUtils.displayRemoveBookmarkDialog(id, title, context, null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 527 | } |
| 528 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 529 | private String getUrl(BrowserBookmarksAdapter adapter, int position) { |
| 530 | return getUrl(adapter.getItem(position)); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* package */ static String getUrl(Cursor c) { |
| 534 | return c.getString(BookmarksLoader.COLUMN_INDEX_URL); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 535 | } |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 536 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 537 | private void copy(CharSequence text) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 538 | ClipboardManager cm = (ClipboardManager) getActivity().getSystemService( |
| 539 | Context.CLIPBOARD_SERVICE); |
Dianne Hackborn | bf1d10a | 2011-01-17 12:29:51 -0800 | [diff] [blame] | 540 | cm.setPrimaryClip(ClipData.newRawUri(null, Uri.parse(text.toString()))); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 541 | } |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 542 | |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 543 | @Override |
| 544 | public boolean onOptionsItemSelected(MenuItem item) { |
| 545 | switch (item.getItemId()) { |
| 546 | case R.id.thumbnail_view: |
| 547 | selectView(VIEW_THUMBNAILS); |
| 548 | return true; |
| 549 | case R.id.list_view: |
| 550 | selectView(VIEW_LIST); |
| 551 | return true; |
| 552 | } |
| 553 | return super.onOptionsItemSelected(item); |
| 554 | } |
| 555 | |
| 556 | @Override |
| 557 | public void onConfigurationChanged(Configuration newConfig) { |
| 558 | super.onConfigurationChanged(newConfig); |
| 559 | Resources res = getActivity().getResources(); |
| 560 | int horizontalSpacing = (int) res.getDimension(R.dimen.combo_horizontalSpacing); |
| 561 | mGrid.setHorizontalSpacing(horizontalSpacing); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 562 | mGrid.setColumnWidthFromLayout(R.layout.bookmark_thumbnail); |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 563 | int paddingLeftRight = (int) res.getDimension(R.dimen.combo_paddingLeftRight); |
| 564 | int paddingTop = (int) res.getDimension(R.dimen.combo_paddingTop); |
| 565 | mRoot.setPadding(paddingLeftRight, paddingTop, |
| 566 | paddingLeftRight, 0); |
John Reck | 9fdd981 | 2011-05-17 14:16:38 -0700 | [diff] [blame] | 567 | getActivity().invalidateOptionsMenu(); |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public void onPrepareOptionsMenu(Menu menu) { |
| 572 | super.onPrepareOptionsMenu(menu); |
| 573 | menu.findItem(R.id.list_view).setVisible(mCurrentView != VIEW_LIST); |
| 574 | menu.findItem(R.id.thumbnail_view).setVisible(mCurrentView != VIEW_THUMBNAILS); |
| 575 | } |
| 576 | |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 577 | void selectView(int view) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 578 | // TODO: Support list view |
| 579 | view = mCurrentView; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 580 | if (view == mCurrentView) { |
| 581 | return; |
| 582 | } |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 583 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); |
| 584 | Editor edit = prefs.edit(); |
| 585 | edit.putInt(PREF_SELECTED_VIEW, mCurrentView); |
| 586 | edit.apply(); |
| 587 | if (mEmptyView.getVisibility() == View.VISIBLE) { |
| 588 | return; |
| 589 | } |
| 590 | setupBookmarkView(); |
| 591 | } |
| 592 | |
| 593 | private void setupBookmarkView() { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 594 | // TODO: Support list view |
| 595 | // mAdapter.selectView(mCurrentView); |
| 596 | // switch (mCurrentView) { |
| 597 | // case VIEW_THUMBNAILS: |
| 598 | // mList.setAdapter(null); |
| 599 | // SharedPreferences prefs = PreferenceManager |
| 600 | // .getDefaultSharedPreferences(getActivity()); |
| 601 | // String accountName = prefs.getString(PREF_ACCOUNT_NAME, null); |
| 602 | // mGrid.addAccount(accountName, mAdapter); |
| 603 | // mGrid.setVisibility(View.VISIBLE); |
| 604 | // mList.setVisibility(View.GONE); |
| 605 | // break; |
| 606 | // case VIEW_LIST: |
| 607 | // mGrid.clearAccounts(); |
| 608 | // if (mList.getAdapter() != mAdapter) { |
| 609 | // mList.setAdapter(mAdapter); |
| 610 | // } |
| 611 | // mGrid.setVisibility(View.GONE); |
| 612 | // mList.setVisibility(View.VISIBLE); |
| 613 | // break; |
| 614 | // } |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 615 | } |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 616 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 617 | /** |
| 618 | * BreadCrumb controller callback |
| 619 | */ |
| 620 | @Override |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 621 | public void onTop(BreadCrumbView view, int level, Object data) { |
| 622 | int groupPosition = (Integer) view.getTag(R.id.group_position); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 623 | Uri uri = (Uri) data; |
| 624 | if (uri == null) { |
| 625 | // top level |
| 626 | uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER; |
| 627 | } |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 628 | loadFolder(groupPosition, uri); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /** |
| 632 | * @param uri |
| 633 | */ |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 634 | private void loadFolder(int groupPosition, Uri uri) { |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 635 | LoaderManager manager = getLoaderManager(); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 636 | // This assumes groups are ordered the same as loaders |
| 637 | BookmarksLoader loader = (BookmarksLoader) ((Loader<?>) |
| 638 | manager.getLoader(LOADER_BOOKMARKS + groupPosition)); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 639 | loader.setUri(uri); |
| 640 | loader.forceLoad(); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | @Override |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 644 | public boolean onMenuItemClick(MenuItem item) { |
| 645 | switch (item.getItemId()) { |
| 646 | case R.id.list_view: |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 647 | selectView(BrowserBookmarksPage.VIEW_LIST); |
| 648 | return true; |
| 649 | case R.id.thumbnail_view: |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 650 | selectView(BrowserBookmarksPage.VIEW_THUMBNAILS); |
| 651 | return true; |
| 652 | } |
| 653 | return false; |
| 654 | } |
| 655 | |
| 656 | public boolean onBackPressed() { |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 657 | return false; |
| 658 | } |
| 659 | |
| 660 | public void setCallbackListener(BookmarksPageCallbacks callbackListener) { |
| 661 | mCallbacks = callbackListener; |
| 662 | } |
| 663 | |
| 664 | public void setEnableContextMenu(boolean enable) { |
| 665 | mEnableContextMenu = enable; |
| 666 | if (mGrid != null) { |
| 667 | if (mEnableContextMenu) { |
| 668 | registerForContextMenu(mGrid); |
| 669 | } else { |
| 670 | unregisterForContextMenu(mGrid); |
| 671 | mGrid.setLongClickable(false); |
| 672 | } |
| 673 | } |
| 674 | if (mList != null) { |
| 675 | if (mEnableContextMenu) { |
| 676 | registerForContextMenu(mList); |
| 677 | } else { |
| 678 | unregisterForContextMenu(mList); |
| 679 | mList.setLongClickable(false); |
| 680 | } |
| 681 | } |
| 682 | } |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 683 | |
John Reck | 66302e5 | 2011-05-18 17:12:33 -0700 | [diff] [blame^] | 684 | private BookmarkDragController mDragController = new BookmarkDragController() { |
| 685 | |
| 686 | @Override |
| 687 | public boolean startDrag(Cursor item) { |
| 688 | return canEdit(item); |
| 689 | } |
| 690 | }; |
| 691 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 692 | private static class LookupBookmarkCount extends AsyncTask<Long, Void, Integer> { |
| 693 | Context mContext; |
| 694 | BookmarkItem mHeader; |
| 695 | |
| 696 | public LookupBookmarkCount(Context context, BookmarkItem header) { |
| 697 | mContext = context; |
| 698 | mHeader = header; |
| 699 | } |
| 700 | |
| 701 | @Override |
| 702 | protected Integer doInBackground(Long... params) { |
| 703 | if (params.length != 1) { |
| 704 | throw new IllegalArgumentException("Missing folder id!"); |
| 705 | } |
| 706 | Uri uri = BookmarkUtils.getBookmarksUri(mContext); |
| 707 | Cursor c = mContext.getContentResolver().query(uri, |
| 708 | null, BrowserContract.Bookmarks.PARENT + "=?", |
| 709 | new String[] {params[0].toString()}, null); |
| 710 | return c.getCount(); |
| 711 | } |
| 712 | |
| 713 | @Override |
| 714 | protected void onPostExecute(Integer result) { |
| 715 | if (result > 0) { |
| 716 | mHeader.setUrl(mContext.getString(R.string.contextheader_folder_bookmarkcount, |
| 717 | result)); |
| 718 | } else if (result == 0) { |
| 719 | mHeader.setUrl(mContext.getString(R.string.contextheader_folder_empty)); |
| 720 | } |
| 721 | } |
| 722 | } |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 723 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 724 | static class AccountsLoader extends CursorLoader { |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 725 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 726 | static String[] ACCOUNTS_PROJECTION = new String[] { |
| 727 | Accounts.ACCOUNT_NAME, |
| 728 | Accounts.ACCOUNT_TYPE |
| 729 | }; |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 730 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 731 | public AccountsLoader(Context context) { |
| 732 | super(context, Accounts.CONTENT_URI, ACCOUNTS_PROJECTION, null, null, |
| 733 | Accounts.ACCOUNT_NAME + " ASC"); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 734 | } |
John Reck | 522a438 | 2011-01-26 13:23:47 -0800 | [diff] [blame] | 735 | |
John Reck | 522a438 | 2011-01-26 13:23:47 -0800 | [diff] [blame] | 736 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 737 | } |