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