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