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