The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.Activity; |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 20 | import android.app.Fragment; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 21 | import android.app.LoaderManager; |
Dianne Hackborn | eb07b17 | 2010-08-26 22:18:14 -0700 | [diff] [blame] | 22 | import android.content.ClipData; |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 23 | import android.content.ClipboardManager; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 24 | import android.content.ContentUris; |
Dianne Hackborn | 80f3262 | 2010-08-05 14:17:53 -0700 | [diff] [blame] | 25 | import android.content.Context; |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 26 | import android.content.CursorLoader; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 27 | import android.content.Intent; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 28 | import android.content.Loader; |
John Reck | 4f61ee4 | 2011-08-22 16:31:14 -0700 | [diff] [blame] | 29 | import android.content.SharedPreferences; |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 30 | import android.content.res.Configuration; |
| 31 | import android.content.res.Resources; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 32 | import android.database.Cursor; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 33 | import android.graphics.Bitmap; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 34 | import android.graphics.BitmapFactory; |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 35 | import android.graphics.BitmapFactory.Options; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 36 | import android.net.Uri; |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 37 | import android.os.AsyncTask; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | import android.os.Bundle; |
Jeff Hamilton | 69bd707 | 2010-08-17 12:38:22 -0500 | [diff] [blame] | 39 | import android.provider.BrowserContract; |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 40 | import android.provider.BrowserContract.Accounts; |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 41 | import android.view.ActionMode; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 42 | import android.view.ContextMenu; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 43 | import android.view.ContextMenu.ContextMenuInfo; |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 44 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 45 | import android.view.MenuInflater; |
| 46 | import android.view.MenuItem; |
| 47 | import android.view.View; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 48 | import android.view.ViewGroup; |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 49 | import android.widget.ExpandableListView; |
| 50 | import android.widget.ExpandableListView.OnChildClickListener; |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 51 | import android.widget.ImageView; |
| 52 | import android.widget.LinearLayout; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 53 | import android.widget.Toast; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 55 | import com.android.browser.BookmarkDragHandler.BookmarkDragController; |
| 56 | import com.android.browser.BookmarkDragHandler.BookmarkDragState; |
John Reck | 1e9815d | 2011-08-08 17:45:05 -0700 | [diff] [blame] | 57 | import com.android.browser.provider.BrowserProvider2; |
John Reck | 9db9529 | 2011-06-20 13:00:12 -0700 | [diff] [blame] | 58 | import com.android.browser.view.BookmarkExpandableView; |
| 59 | import com.android.browser.view.BookmarkExpandableView.BookmarkContextMenuInfo; |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 60 | |
John Reck | 4f61ee4 | 2011-08-22 16:31:14 -0700 | [diff] [blame] | 61 | import org.json.JSONException; |
| 62 | import org.json.JSONObject; |
| 63 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 64 | import java.util.HashMap; |
| 65 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 66 | interface BookmarksPageCallbacks { |
| 67 | // Return true if handled |
| 68 | boolean onBookmarkSelected(Cursor c, boolean isFolder); |
| 69 | // Return true if handled |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 70 | boolean onOpenInNewWindow(String... urls); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 71 | } |
| 72 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 73 | /** |
| 74 | * View showing the user's bookmarks in the browser. |
| 75 | */ |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 76 | public class BrowserBookmarksPage extends Fragment implements View.OnCreateContextMenuListener, |
John Reck | 9db9529 | 2011-06-20 13:00:12 -0700 | [diff] [blame] | 77 | LoaderManager.LoaderCallbacks<Cursor>, BreadCrumbView.Controller, |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 78 | OnChildClickListener { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 79 | |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 80 | public static class ExtraDragState { |
| 81 | public int childPosition; |
| 82 | public int groupPosition; |
| 83 | } |
| 84 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 85 | static final String LOGTAG = "browser"; |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 86 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 87 | static final int LOADER_ACCOUNTS = 1; |
| 88 | static final int LOADER_BOOKMARKS = 100; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 89 | |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 90 | static final String EXTRA_DISABLE_WINDOW = "disable_new_window"; |
John Reck | 4f61ee4 | 2011-08-22 16:31:14 -0700 | [diff] [blame] | 91 | static final String PREF_GROUP_STATE = "bbp_group_state"; |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 92 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 93 | static final String ACCOUNT_TYPE = "account_type"; |
| 94 | static final String ACCOUNT_NAME = "account_name"; |
| 95 | |
John Reck | 9db9529 | 2011-06-20 13:00:12 -0700 | [diff] [blame] | 96 | public static final int VIEW_THUMBNAILS = 1; |
| 97 | public static final int VIEW_LIST = 2; |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 98 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 99 | BookmarksPageCallbacks mCallbacks; |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 100 | View mRoot; |
John Reck | 9db9529 | 2011-06-20 13:00:12 -0700 | [diff] [blame] | 101 | BookmarkExpandableView mGrid; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 102 | boolean mDisableNewWindow; |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 103 | boolean mEnableContextMenu = true; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 104 | View mEmptyView; |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 105 | View mHeader; |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 106 | HashMap<Integer, BrowserBookmarksAdapter> mBookmarkAdapters = new HashMap<Integer, BrowserBookmarksAdapter>(); |
John Reck | 66302e5 | 2011-05-18 17:12:33 -0700 | [diff] [blame] | 107 | BookmarkDragHandler mDragHandler; |
John Reck | 4f61ee4 | 2011-08-22 16:31:14 -0700 | [diff] [blame] | 108 | JSONObject mState; |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 109 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 110 | @Override |
| 111 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 112 | if (id == LOADER_ACCOUNTS) { |
| 113 | return new AccountsLoader(getActivity()); |
| 114 | } else if (id >= LOADER_BOOKMARKS) { |
| 115 | String accountType = args.getString(ACCOUNT_TYPE); |
| 116 | String accountName = args.getString(ACCOUNT_NAME); |
| 117 | BookmarksLoader bl = new BookmarksLoader(getActivity(), |
| 118 | accountType, accountName); |
| 119 | return bl; |
| 120 | } else { |
| 121 | throw new UnsupportedOperationException("Unknown loader id " + id); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 122 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 126 | public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 127 | if (loader.getId() == LOADER_ACCOUNTS) { |
| 128 | LoaderManager lm = getLoaderManager(); |
| 129 | int id = LOADER_BOOKMARKS; |
| 130 | while (cursor.moveToNext()) { |
| 131 | String accountName = cursor.getString(0); |
| 132 | String accountType = cursor.getString(1); |
| 133 | Bundle args = new Bundle(); |
| 134 | args.putString(ACCOUNT_NAME, accountName); |
| 135 | args.putString(ACCOUNT_TYPE, accountType); |
| 136 | BrowserBookmarksAdapter adapter = new BrowserBookmarksAdapter( |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 137 | getActivity(), VIEW_THUMBNAILS); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 138 | mBookmarkAdapters.put(id, adapter); |
John Reck | 4f61ee4 | 2011-08-22 16:31:14 -0700 | [diff] [blame] | 139 | boolean expand = true; |
| 140 | try { |
| 141 | expand = mState.getBoolean(accountName != null ? accountName |
| 142 | : BookmarkExpandableView.LOCAL_ACCOUNT_NAME); |
| 143 | } catch (JSONException e) {} // no state for accountName |
| 144 | mGrid.addAccount(accountName, adapter, expand); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 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) { |
Dianne Hackborn | 39772c8 | 2010-12-16 00:43:54 -0800 | [diff] [blame] | 164 | } |
| 165 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 166 | @Override |
| 167 | public boolean onContextItemSelected(MenuItem item) { |
John Reck | 54ed3aa | 2011-08-19 15:16:05 -0700 | [diff] [blame] | 168 | if (!(item.getMenuInfo() instanceof BookmarkContextMenuInfo)) { |
| 169 | return false; |
| 170 | } |
| 171 | BookmarkContextMenuInfo i = (BookmarkContextMenuInfo) item.getMenuInfo(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 172 | // If we have no menu info, we can't tell which item was selected. |
| 173 | if (i == null) { |
Leon Scroggins | c8cae08 | 2010-11-03 18:51:46 -0400 | [diff] [blame] | 174 | return false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 175 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 176 | |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 177 | if (handleContextItem(item.getItemId(), i.groupPosition, i.childPosition)) { |
| 178 | return true; |
| 179 | } |
| 180 | return super.onContextItemSelected(item); |
| 181 | } |
| 182 | |
| 183 | public boolean handleContextItem(int itemId, int groupPosition, |
| 184 | int childPosition) { |
| 185 | final Activity activity = getActivity(); |
| 186 | BrowserBookmarksAdapter adapter = getChildAdapter(groupPosition); |
| 187 | |
| 188 | switch (itemId) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 189 | case R.id.open_context_menu_id: |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 190 | loadUrl(adapter, childPosition); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 191 | break; |
| 192 | case R.id.edit_context_menu_id: |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 193 | editBookmark(adapter, childPosition); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 194 | break; |
| 195 | case R.id.shortcut_context_menu_id: |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 196 | Cursor c = adapter.getItem(childPosition); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 197 | activity.sendBroadcast(createShortcutIntent(getActivity(), c)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 198 | break; |
| 199 | case R.id.delete_context_menu_id: |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 200 | displayRemoveBookmarkDialog(adapter, childPosition); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 201 | break; |
| 202 | case R.id.new_window_context_menu_id: |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 203 | openInNewWindow(adapter, childPosition); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 204 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 205 | case R.id.share_link_context_menu_id: { |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 206 | Cursor cursor = adapter.getItem(childPosition); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 207 | Controller.sharePage(activity, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 208 | cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE), |
| 209 | cursor.getString(BookmarksLoader.COLUMN_INDEX_URL), |
| 210 | getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON), |
| 211 | getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_THUMBNAIL)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 212 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 213 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 214 | case R.id.copy_url_context_menu_id: |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 215 | copy(getUrl(adapter, childPosition)); |
Leon Scroggins | feb941d | 2009-05-28 17:27:38 -0400 | [diff] [blame] | 216 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 217 | case R.id.homepage_context_menu_id: { |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 218 | BrowserSettings.getInstance().setHomePage(getUrl(adapter, childPosition)); |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 219 | Toast.makeText(activity, R.string.homepage_set, Toast.LENGTH_LONG).show(); |
Leon Scroggins | feb941d | 2009-05-28 17:27:38 -0400 | [diff] [blame] | 220 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 221 | } |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 222 | // Only for the Most visited page |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 223 | case R.id.save_to_bookmarks_menu_id: { |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 224 | Cursor cursor = adapter.getItem(childPosition); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 225 | String name = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
| 226 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 227 | // If the site is bookmarked, the item becomes remove from |
| 228 | // bookmarks. |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 229 | Bookmarks.removeFromBookmarks(activity, activity.getContentResolver(), url, name); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 230 | break; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 231 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 232 | default: |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 233 | return false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 234 | } |
| 235 | return true; |
| 236 | } |
| 237 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 238 | static Bitmap getBitmap(Cursor cursor, int columnIndex) { |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 239 | return getBitmap(cursor, columnIndex, null); |
| 240 | } |
| 241 | |
| 242 | static ThreadLocal<Options> sOptions = new ThreadLocal<Options>() { |
| 243 | @Override |
| 244 | protected Options initialValue() { |
| 245 | return new Options(); |
| 246 | }; |
| 247 | }; |
| 248 | static Bitmap getBitmap(Cursor cursor, int columnIndex, Bitmap inBitmap) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 249 | byte[] data = cursor.getBlob(columnIndex); |
| 250 | if (data == null) { |
| 251 | return null; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 252 | } |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 253 | Options opts = sOptions.get(); |
| 254 | opts.inBitmap = inBitmap; |
| 255 | opts.inSampleSize = 1; |
| 256 | opts.inScaled = false; |
| 257 | try { |
| 258 | return BitmapFactory.decodeByteArray(data, 0, data.length, opts); |
| 259 | } catch (IllegalArgumentException ex) { |
| 260 | // Failed to re-use bitmap, create a new one |
| 261 | return BitmapFactory.decodeByteArray(data, 0, data.length); |
| 262 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 263 | } |
| 264 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 265 | private MenuItem.OnMenuItemClickListener mContextItemClickListener = |
| 266 | new MenuItem.OnMenuItemClickListener() { |
| 267 | @Override |
| 268 | public boolean onMenuItemClick(MenuItem item) { |
| 269 | return onContextItemSelected(item); |
| 270 | } |
| 271 | }; |
| 272 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 273 | @Override |
| 274 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 275 | BookmarkContextMenuInfo info = (BookmarkContextMenuInfo) menuInfo; |
| 276 | BrowserBookmarksAdapter adapter = getChildAdapter(info.groupPosition); |
| 277 | Cursor cursor = adapter.getItem(info.childPosition); |
John Reck | 1cebb44 | 2010-12-10 12:02:16 -0800 | [diff] [blame] | 278 | if (!canEdit(cursor)) { |
| 279 | return; |
| 280 | } |
Leon Scroggins | 5e8a40f | 2010-09-27 17:13:35 -0400 | [diff] [blame] | 281 | boolean isFolder |
| 282 | = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 283 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 284 | final Activity activity = getActivity(); |
| 285 | MenuInflater inflater = activity.getMenuInflater(); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 286 | inflater.inflate(R.menu.bookmarkscontext, menu); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 287 | if (isFolder) { |
| 288 | menu.setGroupVisible(R.id.FOLDER_CONTEXT_MENU, true); |
| 289 | } else { |
| 290 | menu.setGroupVisible(R.id.BOOKMARK_CONTEXT_MENU, true); |
| 291 | if (mDisableNewWindow) { |
| 292 | menu.findItem(R.id.new_window_context_menu_id).setVisible(false); |
| 293 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 294 | } |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 295 | BookmarkItem header = new BookmarkItem(activity); |
John Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 296 | header.setEnableScrolling(true); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 297 | populateBookmarkItem(cursor, header, isFolder); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 298 | menu.setHeaderView(header); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 299 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 300 | int count = menu.size(); |
| 301 | for (int i = 0; i < count; i++) { |
| 302 | menu.getItem(i).setOnMenuItemClickListener(mContextItemClickListener); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 303 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 304 | } |
| 305 | |
John Reck | 1cebb44 | 2010-12-10 12:02:16 -0800 | [diff] [blame] | 306 | boolean canEdit(Cursor c) { |
John Reck | d18ac4b | 2012-04-12 17:27:34 -0700 | [diff] [blame] | 307 | int type = c.getInt(BookmarksLoader.COLUMN_INDEX_TYPE); |
| 308 | return type == BrowserContract.Bookmarks.BOOKMARK_TYPE_BOOKMARK |
| 309 | || type == BrowserContract.Bookmarks.BOOKMARK_TYPE_FOLDER; |
John Reck | 1cebb44 | 2010-12-10 12:02:16 -0800 | [diff] [blame] | 310 | } |
| 311 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 312 | private void populateBookmarkItem(Cursor cursor, BookmarkItem item, boolean isFolder) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 313 | item.setName(cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 314 | if (isFolder) { |
| 315 | item.setUrl(null); |
| 316 | Bitmap bitmap = |
Michael Kolb | 5a72f18 | 2011-01-13 20:35:06 -0800 | [diff] [blame] | 317 | BitmapFactory.decodeResource(getResources(), R.drawable.ic_folder_holo_dark); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 318 | item.setFavicon(bitmap); |
John Reck | 60e3336 | 2010-12-02 17:52:38 -0800 | [diff] [blame] | 319 | new LookupBookmarkCount(getActivity(), item) |
| 320 | .execute(cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID)); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 321 | } else { |
| 322 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
| 323 | item.setUrl(url); |
| 324 | Bitmap bitmap = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 325 | item.setFavicon(bitmap); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 326 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 327 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 328 | |
| 329 | /** |
| 330 | * Create a new BrowserBookmarksPage. |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 331 | */ |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 332 | @Override |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 333 | public void onCreate(Bundle icicle) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 334 | super.onCreate(icicle); |
John Reck | 4f61ee4 | 2011-08-22 16:31:14 -0700 | [diff] [blame] | 335 | SharedPreferences prefs = BrowserSettings.getInstance().getPreferences(); |
| 336 | try { |
| 337 | mState = new JSONObject(prefs.getString(PREF_GROUP_STATE, "{}")); |
| 338 | } catch (JSONException e) { |
| 339 | // Parse failed, clear preference and start with empty state |
| 340 | prefs.edit().remove(PREF_GROUP_STATE).apply(); |
| 341 | mState = new JSONObject(); |
| 342 | } |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 343 | Bundle args = getArguments(); |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 344 | mDisableNewWindow = args == null ? false : args.getBoolean(EXTRA_DISABLE_WINDOW, false); |
John Reck | 8f295d1 | 2011-03-18 15:25:18 -0700 | [diff] [blame] | 345 | setHasOptionsMenu(true); |
John Reck | 2d963a2 | 2011-08-10 15:53:07 -0700 | [diff] [blame] | 346 | if (mCallbacks == null && getActivity() instanceof CombinedBookmarksCallbacks) { |
| 347 | mCallbacks = new CombinedBookmarksCallbackWrapper( |
| 348 | (CombinedBookmarksCallbacks) getActivity()); |
| 349 | } |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 350 | } |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 351 | |
John Reck | 4f61ee4 | 2011-08-22 16:31:14 -0700 | [diff] [blame] | 352 | @Override |
| 353 | public void onPause() { |
| 354 | super.onPause(); |
| 355 | try { |
| 356 | mState = mGrid.saveGroupState(); |
| 357 | // Save state |
| 358 | SharedPreferences prefs = BrowserSettings.getInstance().getPreferences(); |
| 359 | prefs.edit() |
| 360 | .putString(PREF_GROUP_STATE, mState.toString()) |
| 361 | .apply(); |
| 362 | } catch (JSONException e) { |
| 363 | // Not critical, ignore |
| 364 | } |
| 365 | } |
| 366 | |
John Reck | 2d963a2 | 2011-08-10 15:53:07 -0700 | [diff] [blame] | 367 | private static class CombinedBookmarksCallbackWrapper |
| 368 | implements BookmarksPageCallbacks { |
| 369 | |
| 370 | private CombinedBookmarksCallbacks mCombinedCallback; |
| 371 | |
| 372 | private CombinedBookmarksCallbackWrapper(CombinedBookmarksCallbacks cb) { |
| 373 | mCombinedCallback = cb; |
| 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public boolean onOpenInNewWindow(String... urls) { |
| 378 | mCombinedCallback.openInNewTab(urls); |
| 379 | return true; |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public boolean onBookmarkSelected(Cursor c, boolean isFolder) { |
| 384 | if (isFolder) { |
| 385 | return false; |
| 386 | } |
| 387 | mCombinedCallback.openUrl(BrowserBookmarksPage.getUrl(c)); |
| 388 | return true; |
| 389 | } |
| 390 | }; |
| 391 | |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 392 | @Override |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 393 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 394 | Bundle savedInstanceState) { |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 395 | mRoot = inflater.inflate(R.layout.bookmarks, container, false); |
| 396 | mEmptyView = mRoot.findViewById(android.R.id.empty); |
Leon Scroggins | d87f85e | 2009-08-18 14:13:31 -0400 | [diff] [blame] | 397 | |
John Reck | 9db9529 | 2011-06-20 13:00:12 -0700 | [diff] [blame] | 398 | mGrid = (BookmarkExpandableView) mRoot.findViewById(R.id.grid); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 399 | mGrid.setOnChildClickListener(this); |
| 400 | mGrid.setColumnWidthFromLayout(R.layout.bookmark_thumbnail); |
| 401 | mGrid.setBreadcrumbController(this); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 402 | setEnableContextMenu(mEnableContextMenu); |
John Reck | 66302e5 | 2011-05-18 17:12:33 -0700 | [diff] [blame] | 403 | mDragHandler = new BookmarkDragHandler(getActivity(), mDragController, |
| 404 | mGrid.getDragAdapter()); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 405 | |
Jeff Hamilton | db90aa8 | 2010-09-16 03:38:04 -0500 | [diff] [blame] | 406 | // Start the loaders |
| 407 | LoaderManager lm = getLoaderManager(); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 408 | lm.restartLoader(LOADER_ACCOUNTS, null, this); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 409 | |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 410 | return mRoot; |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 411 | } |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 412 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 413 | @Override |
| 414 | public void onDestroyView() { |
| 415 | super.onDestroyView(); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 416 | mGrid.setBreadcrumbController(null); |
John Reck | c3c46d7 | 2011-09-06 16:17:34 -0700 | [diff] [blame] | 417 | mGrid.clearAccounts(); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 418 | LoaderManager lm = getLoaderManager(); |
| 419 | lm.destroyLoader(LOADER_ACCOUNTS); |
| 420 | for (int id : mBookmarkAdapters.keySet()) { |
| 421 | lm.destroyLoader(id); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 422 | } |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 423 | mBookmarkAdapters.clear(); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | private BrowserBookmarksAdapter getChildAdapter(int groupPosition) { |
John Reck | 9db9529 | 2011-06-20 13:00:12 -0700 | [diff] [blame] | 427 | return mGrid.getChildAdapter(groupPosition); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | private BreadCrumbView getBreadCrumbs(int groupPosition) { |
John Reck | 9db9529 | 2011-06-20 13:00:12 -0700 | [diff] [blame] | 431 | return mGrid.getBreadCrumbs(groupPosition); |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 434 | @Override |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 435 | public boolean onChildClick(ExpandableListView parent, View v, |
| 436 | int groupPosition, int childPosition, long id) { |
| 437 | BrowserBookmarksAdapter adapter = getChildAdapter(groupPosition); |
| 438 | Cursor cursor = adapter.getItem(childPosition); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 439 | boolean isFolder = cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; |
| 440 | if (mCallbacks != null && |
| 441 | mCallbacks.onBookmarkSelected(cursor, isFolder)) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 442 | return true; |
Ben Murdoch | 328ea87 | 2009-09-16 13:33:29 +0100 | [diff] [blame] | 443 | } |
| 444 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 445 | if (isFolder) { |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 446 | String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 447 | Uri uri = ContentUris.withAppendedId( |
| 448 | BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER, id); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 449 | BreadCrumbView crumbs = getBreadCrumbs(groupPosition); |
| 450 | if (crumbs != null) { |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 451 | // update crumbs |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 452 | crumbs.pushView(title, uri); |
John Reck | cece1fe | 2011-09-13 08:47:17 -0700 | [diff] [blame] | 453 | crumbs.setVisibility(View.VISIBLE); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 454 | } |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 455 | loadFolder(groupPosition, uri); |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 456 | } |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 457 | return true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 458 | } |
| 459 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 460 | /* package */ static Intent createShortcutIntent(Context context, Cursor cursor) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 461 | String url = cursor.getString(BookmarksLoader.COLUMN_INDEX_URL); |
Michael Kolb | ccaa896 | 2010-10-20 17:03:10 -0700 | [diff] [blame] | 462 | String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 463 | Bitmap touchIcon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_TOUCH_ICON); |
| 464 | Bitmap favicon = getBitmap(cursor, BookmarksLoader.COLUMN_INDEX_FAVICON); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 465 | return BookmarkUtils.createAddToHomeIntent(context, url, title, touchIcon, favicon); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 466 | } |
| 467 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 468 | private void loadUrl(BrowserBookmarksAdapter adapter, int position) { |
| 469 | if (mCallbacks != null && adapter != null) { |
| 470 | mCallbacks.onBookmarkSelected(adapter.getItem(position), false); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 471 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 472 | } |
| 473 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 474 | private void openInNewWindow(BrowserBookmarksAdapter adapter, int position) { |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 475 | if (mCallbacks != null) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 476 | Cursor c = adapter.getItem(position); |
John Reck | 1b3c66e | 2011-01-11 13:20:32 -0800 | [diff] [blame] | 477 | boolean isFolder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) == 1; |
| 478 | if (isFolder) { |
| 479 | long id = c.getLong(BookmarksLoader.COLUMN_INDEX_ID); |
| 480 | new OpenAllInTabsTask(id).execute(); |
| 481 | } else { |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 482 | mCallbacks.onOpenInNewWindow(BrowserBookmarksPage.getUrl(c)); |
John Reck | 1b3c66e | 2011-01-11 13:20:32 -0800 | [diff] [blame] | 483 | } |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 484 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 485 | } |
Nicolas Catania | 095292f | 2010-03-15 09:00:14 -0700 | [diff] [blame] | 486 | |
John Reck | 1b3c66e | 2011-01-11 13:20:32 -0800 | [diff] [blame] | 487 | class OpenAllInTabsTask extends AsyncTask<Void, Void, Cursor> { |
| 488 | long mFolderId; |
| 489 | public OpenAllInTabsTask(long id) { |
| 490 | mFolderId = id; |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | protected Cursor doInBackground(Void... params) { |
| 495 | Context c = getActivity(); |
| 496 | if (c == null) return null; |
| 497 | return c.getContentResolver().query(BookmarkUtils.getBookmarksUri(c), |
| 498 | BookmarksLoader.PROJECTION, BrowserContract.Bookmarks.PARENT + "=?", |
| 499 | new String[] { Long.toString(mFolderId) }, null); |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | protected void onPostExecute(Cursor result) { |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 504 | if (mCallbacks != null && result.getCount() > 0) { |
| 505 | String[] urls = new String[result.getCount()]; |
| 506 | int i = 0; |
John Reck | 1b3c66e | 2011-01-11 13:20:32 -0800 | [diff] [blame] | 507 | while (result.moveToNext()) { |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 508 | urls[i++] = BrowserBookmarksPage.getUrl(result); |
John Reck | 1b3c66e | 2011-01-11 13:20:32 -0800 | [diff] [blame] | 509 | } |
John Reck | d3e4d5b | 2011-07-13 15:48:43 -0700 | [diff] [blame] | 510 | mCallbacks.onOpenInNewWindow(urls); |
John Reck | 1b3c66e | 2011-01-11 13:20:32 -0800 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | } |
| 515 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 516 | private void editBookmark(BrowserBookmarksAdapter adapter, int position) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 517 | Intent intent = new Intent(getActivity(), AddBookmarkPage.class); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 518 | Cursor cursor = adapter.getItem(position); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 519 | Bundle item = new Bundle(); |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 520 | item.putString(BrowserContract.Bookmarks.TITLE, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 521 | cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE)); |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 522 | item.putString(BrowserContract.Bookmarks.URL, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 523 | cursor.getString(BookmarksLoader.COLUMN_INDEX_URL)); |
| 524 | byte[] data = cursor.getBlob(BookmarksLoader.COLUMN_INDEX_FAVICON); |
| 525 | if (data != null) { |
Michael Kolb | bef7a75 | 2010-08-25 17:11:35 -0700 | [diff] [blame] | 526 | item.putParcelable(BrowserContract.Bookmarks.FAVICON, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 527 | BitmapFactory.decodeByteArray(data, 0, data.length)); |
| 528 | } |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 529 | item.putLong(BrowserContract.Bookmarks._ID, |
| 530 | cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID)); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 531 | item.putLong(BrowserContract.Bookmarks.PARENT, |
| 532 | cursor.getLong(BookmarksLoader.COLUMN_INDEX_PARENT)); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 533 | intent.putExtra(AddBookmarkPage.EXTRA_EDIT_BOOKMARK, item); |
| 534 | intent.putExtra(AddBookmarkPage.EXTRA_IS_FOLDER, |
| 535 | cursor.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) == 1); |
| 536 | startActivity(intent); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 537 | } |
| 538 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 539 | private void displayRemoveBookmarkDialog(BrowserBookmarksAdapter adapter, |
| 540 | int position) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 541 | // Put up a dialog asking if the user really wants to |
| 542 | // delete the bookmark |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 543 | Cursor cursor = adapter.getItem(position); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 544 | long id = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID); |
| 545 | String title = cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 546 | Context context = getActivity(); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 547 | BookmarkUtils.displayRemoveBookmarkDialog(id, title, context, null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 548 | } |
| 549 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 550 | private String getUrl(BrowserBookmarksAdapter adapter, int position) { |
| 551 | return getUrl(adapter.getItem(position)); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /* package */ static String getUrl(Cursor c) { |
| 555 | return c.getString(BookmarksLoader.COLUMN_INDEX_URL); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 556 | } |
Michael Kolb | 801ecb7 | 2010-08-25 12:57:38 -0700 | [diff] [blame] | 557 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 558 | private void copy(CharSequence text) { |
Jeff Hamilton | 64144e4 | 2010-08-18 16:31:59 -0500 | [diff] [blame] | 559 | ClipboardManager cm = (ClipboardManager) getActivity().getSystemService( |
| 560 | Context.CLIPBOARD_SERVICE); |
Dianne Hackborn | bf1d10a | 2011-01-17 12:29:51 -0800 | [diff] [blame] | 561 | cm.setPrimaryClip(ClipData.newRawUri(null, Uri.parse(text.toString()))); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 562 | } |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 563 | |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 564 | @Override |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 565 | public void onConfigurationChanged(Configuration newConfig) { |
| 566 | super.onConfigurationChanged(newConfig); |
| 567 | Resources res = getActivity().getResources(); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 568 | mGrid.setColumnWidthFromLayout(R.layout.bookmark_thumbnail); |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 569 | int paddingTop = (int) res.getDimension(R.dimen.combo_paddingTop); |
John Reck | f3828cd | 2011-06-14 17:21:55 -0700 | [diff] [blame] | 570 | mRoot.setPadding(0, paddingTop, 0, 0); |
John Reck | 9fdd981 | 2011-05-17 14:16:38 -0700 | [diff] [blame] | 571 | getActivity().invalidateOptionsMenu(); |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 572 | } |
| 573 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 574 | /** |
| 575 | * BreadCrumb controller callback |
| 576 | */ |
| 577 | @Override |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 578 | public void onTop(BreadCrumbView view, int level, Object data) { |
| 579 | int groupPosition = (Integer) view.getTag(R.id.group_position); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 580 | Uri uri = (Uri) data; |
| 581 | if (uri == null) { |
| 582 | // top level |
| 583 | uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER; |
| 584 | } |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 585 | loadFolder(groupPosition, uri); |
John Reck | cece1fe | 2011-09-13 08:47:17 -0700 | [diff] [blame] | 586 | if (level <= 1) { |
| 587 | view.setVisibility(View.GONE); |
| 588 | } else { |
| 589 | view.setVisibility(View.VISIBLE); |
| 590 | } |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /** |
| 594 | * @param uri |
| 595 | */ |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 596 | private void loadFolder(int groupPosition, Uri uri) { |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 597 | LoaderManager manager = getLoaderManager(); |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 598 | // This assumes groups are ordered the same as loaders |
| 599 | BookmarksLoader loader = (BookmarksLoader) ((Loader<?>) |
| 600 | manager.getLoader(LOADER_BOOKMARKS + groupPosition)); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 601 | loader.setUri(uri); |
| 602 | loader.forceLoad(); |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 603 | } |
| 604 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 605 | public void setCallbackListener(BookmarksPageCallbacks callbackListener) { |
| 606 | mCallbacks = callbackListener; |
| 607 | } |
| 608 | |
| 609 | public void setEnableContextMenu(boolean enable) { |
| 610 | mEnableContextMenu = enable; |
| 611 | if (mGrid != null) { |
| 612 | if (mEnableContextMenu) { |
| 613 | registerForContextMenu(mGrid); |
| 614 | } else { |
| 615 | unregisterForContextMenu(mGrid); |
| 616 | mGrid.setLongClickable(false); |
| 617 | } |
| 618 | } |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 619 | } |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 620 | |
John Reck | 66302e5 | 2011-05-18 17:12:33 -0700 | [diff] [blame] | 621 | private BookmarkDragController mDragController = new BookmarkDragController() { |
| 622 | |
| 623 | @Override |
| 624 | public boolean startDrag(Cursor item) { |
| 625 | return canEdit(item); |
| 626 | } |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 627 | |
| 628 | @Override |
| 629 | public ViewGroup getActionModeView(ActionMode mode, |
| 630 | BookmarkDragState state) { |
| 631 | LayoutInflater inflater = LayoutInflater.from(getActivity()); |
| 632 | LinearLayout view = (LinearLayout) inflater.inflate( |
| 633 | R.layout.bookmarks_drag_actionmode, null); |
| 634 | view.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE); |
| 635 | ExtraDragState extraState = (ExtraDragState) state.extraState; |
| 636 | BrowserBookmarksAdapter adapter = getChildAdapter(extraState.groupPosition); |
| 637 | Cursor c = adapter.getItem(extraState.childPosition); |
| 638 | boolean isFolder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; |
| 639 | if (isFolder) { |
| 640 | view.findViewById(R.id.open_context_menu_id).setVisibility(View.GONE); |
| 641 | ImageView iv = (ImageView) view.findViewById( |
| 642 | R.id.new_window_context_menu_id); |
| 643 | iv.setImageResource(R.drawable.ic_windows_holo_dark); |
| 644 | } |
| 645 | return view; |
| 646 | } |
| 647 | |
| 648 | @Override |
| 649 | public void actionItemClicked(View v, BookmarkDragState state) { |
| 650 | if (v.getId() == R.id.info) { |
John Reck | 9db9529 | 2011-06-20 13:00:12 -0700 | [diff] [blame] | 651 | mGrid.showContextMenuForState(state); |
John Reck | e7c97de | 2011-05-27 14:42:43 -0700 | [diff] [blame] | 652 | } else { |
| 653 | ExtraDragState extraState = (ExtraDragState) state.extraState; |
| 654 | handleContextItem(v.getId(), extraState.groupPosition, |
| 655 | extraState.childPosition); |
| 656 | } |
| 657 | } |
John Reck | 66302e5 | 2011-05-18 17:12:33 -0700 | [diff] [blame] | 658 | }; |
| 659 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 660 | private static class LookupBookmarkCount extends AsyncTask<Long, Void, Integer> { |
| 661 | Context mContext; |
| 662 | BookmarkItem mHeader; |
| 663 | |
| 664 | public LookupBookmarkCount(Context context, BookmarkItem header) { |
Ben Murdoch | 914c559 | 2011-08-01 13:58:47 +0100 | [diff] [blame] | 665 | mContext = context.getApplicationContext(); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 666 | mHeader = header; |
| 667 | } |
| 668 | |
| 669 | @Override |
| 670 | protected Integer doInBackground(Long... params) { |
| 671 | if (params.length != 1) { |
| 672 | throw new IllegalArgumentException("Missing folder id!"); |
| 673 | } |
| 674 | Uri uri = BookmarkUtils.getBookmarksUri(mContext); |
| 675 | Cursor c = mContext.getContentResolver().query(uri, |
| 676 | null, BrowserContract.Bookmarks.PARENT + "=?", |
| 677 | new String[] {params[0].toString()}, null); |
| 678 | return c.getCount(); |
| 679 | } |
| 680 | |
| 681 | @Override |
| 682 | protected void onPostExecute(Integer result) { |
| 683 | if (result > 0) { |
| 684 | mHeader.setUrl(mContext.getString(R.string.contextheader_folder_bookmarkcount, |
| 685 | result)); |
| 686 | } else if (result == 0) { |
| 687 | mHeader.setUrl(mContext.getString(R.string.contextheader_folder_empty)); |
| 688 | } |
| 689 | } |
| 690 | } |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 691 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 692 | static class AccountsLoader extends CursorLoader { |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 693 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 694 | static String[] ACCOUNTS_PROJECTION = new String[] { |
| 695 | Accounts.ACCOUNT_NAME, |
| 696 | Accounts.ACCOUNT_TYPE |
| 697 | }; |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 698 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 699 | public AccountsLoader(Context context) { |
John Reck | 1e9815d | 2011-08-08 17:45:05 -0700 | [diff] [blame] | 700 | super(context, Accounts.CONTENT_URI |
| 701 | .buildUpon() |
| 702 | .appendQueryParameter(BrowserProvider2.PARAM_ALLOW_EMPTY_ACCOUNTS, "false") |
| 703 | .build(), |
| 704 | ACCOUNTS_PROJECTION, null, null, null); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 705 | } |
John Reck | 522a438 | 2011-01-26 13:23:47 -0800 | [diff] [blame] | 706 | |
John Reck | 522a438 | 2011-01-26 13:23:47 -0800 | [diff] [blame] | 707 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 708 | } |