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 | |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 19 | import com.android.browser.provider.BrowserProvider2; |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 20 | import com.android.browser.addbookmark.FolderSpinner; |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 21 | import com.android.browser.addbookmark.FolderSpinnerAdapter; |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 22 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 23 | import android.app.Activity; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 24 | import android.app.LoaderManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 25 | import android.content.ContentResolver; |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 26 | import android.content.ContentUris; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 27 | import android.content.ContentValues; |
| 28 | import android.content.Context; |
| 29 | import android.content.CursorLoader; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 30 | import android.content.Loader; |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 31 | import android.content.SharedPreferences; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 32 | import android.content.res.Resources; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 33 | import android.database.Cursor; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 34 | import android.graphics.Bitmap; |
Leon Scroggins | 0208194 | 2010-11-01 17:52:42 -0400 | [diff] [blame] | 35 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 36 | import android.net.ParseException; |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 37 | import android.net.Uri; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | import android.net.WebAddress; |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 39 | import android.os.AsyncTask; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 40 | import android.os.Bundle; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 41 | import android.os.Handler; |
| 42 | import android.os.Message; |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 43 | import android.preference.PreferenceManager; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 44 | import android.provider.BrowserContract; |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 45 | import android.text.TextUtils; |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 46 | import android.util.AttributeSet; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 47 | import android.view.KeyEvent; |
| 48 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 49 | import android.view.View; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 50 | import android.view.ViewGroup; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 51 | import android.view.Window; |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 52 | import android.view.WindowManager; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 53 | import android.view.inputmethod.EditorInfo; |
| 54 | import android.view.inputmethod.InputMethodManager; |
| 55 | import android.widget.AdapterView; |
| 56 | import android.widget.CursorAdapter; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 57 | import android.widget.EditText; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 58 | import android.widget.ListView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 59 | import android.widget.TextView; |
| 60 | import android.widget.Toast; |
| 61 | |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 62 | import java.net.URI; |
| 63 | import java.net.URISyntaxException; |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 64 | import java.util.Stack; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 65 | |
| 66 | public class AddBookmarkPage extends Activity |
| 67 | implements View.OnClickListener, TextView.OnEditorActionListener, |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 68 | AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>, |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 69 | BreadCrumbView.Controller, FolderSpinner.OnSetSelectionListener { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 70 | |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 71 | public static final long DEFAULT_FOLDER_ID = -1; |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 72 | public static final String TOUCH_ICON_URL = "touch_icon_url"; |
| 73 | // Place on an edited bookmark to remove the saved thumbnail |
| 74 | public static final String REMOVE_THUMBNAIL = "remove_thumbnail"; |
| 75 | public static final String USER_AGENT = "user_agent"; |
Leon Scroggins | bdff8a7 | 2011-02-11 15:49:04 -0500 | [diff] [blame] | 76 | public static final String CHECK_FOR_DUPE = "check_for_dupe"; |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 77 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 78 | /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark"; |
| 79 | /* package */ static final String EXTRA_IS_FOLDER = "is_folder"; |
| 80 | |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 81 | private static final int MAX_CRUMBS_SHOWN = 2; |
| 82 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 83 | private final String LOGTAG = "Bookmarks"; |
| 84 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 85 | // IDs for the CursorLoaders that are used. |
| 86 | private final int LOADER_ID_FOLDER_CONTENTS = 0; |
| 87 | private final int LOADER_ID_ALL_FOLDERS = 1; |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 88 | private final int LOADER_ID_FIND_ROOT = 2; |
| 89 | private final int LOADER_ID_CHECK_FOR_DUPE = 3; |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 90 | private final int LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK = 4; |
| 91 | private final int LOADER_ID_FIND_FOLDER_BY_ID = 5; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 92 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 93 | private EditText mTitle; |
| 94 | private EditText mAddress; |
| 95 | private TextView mButton; |
| 96 | private View mCancelButton; |
| 97 | private boolean mEditingExisting; |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 98 | private boolean mEditingFolder; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 99 | private Bundle mMap; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 100 | private String mTouchIconUrl; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 101 | private String mOriginalUrl; |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 102 | private FolderSpinner mFolder; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 103 | private View mDefaultView; |
| 104 | private View mFolderSelector; |
| 105 | private EditText mFolderNamer; |
Leon Scroggins | 0e3a7b8 | 2011-01-11 19:08:03 -0500 | [diff] [blame] | 106 | private View mFolderCancel; |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 107 | private boolean mIsFolderNamerShowing; |
| 108 | private View mFolderNamerHolder; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 109 | private View mAddNewFolder; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 110 | private View mAddSeparator; |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 111 | private long mCurrentFolder; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 112 | private FolderAdapter mAdapter; |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 113 | private BreadCrumbView mCrumbs; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 114 | private TextView mFakeTitle; |
| 115 | private View mCrumbHolder; |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 116 | private CustomListView mListView; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 117 | private boolean mSaveToHomeScreen; |
Leon Scroggins | 0208194 | 2010-11-01 17:52:42 -0400 | [diff] [blame] | 118 | private long mRootFolder; |
Leon Scroggins | 905250c | 2010-12-17 15:25:33 -0500 | [diff] [blame] | 119 | private TextView mTopLevelLabel; |
| 120 | private Drawable mHeaderIcon; |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 121 | private View mRemoveLink; |
| 122 | private View mFakeTitleHolder; |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 123 | private FolderSpinnerAdapter mFolderAdapter; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 124 | private static class Folder { |
| 125 | String Name; |
| 126 | long Id; |
| 127 | Folder(String name, long id) { |
| 128 | Name = name; |
| 129 | Id = id; |
| 130 | } |
| 131 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 132 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 133 | // Message IDs |
| 134 | private static final int SAVE_BOOKMARK = 100; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 135 | private static final int TOUCH_ICON_DOWNLOADED = 101; |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 136 | private static final int BOOKMARK_DELETED = 102; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 137 | |
| 138 | private Handler mHandler; |
| 139 | |
Bjorn Bringert | b1402a5 | 2010-10-12 10:53:12 +0100 | [diff] [blame] | 140 | private InputMethodManager getInputMethodManager() { |
| 141 | return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); |
| 142 | } |
| 143 | |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 144 | private Uri getUriForFolder(long folder) { |
| 145 | Uri uri; |
| 146 | if (folder == mRootFolder) { |
| 147 | uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER; |
| 148 | } else { |
| 149 | uri = BrowserContract.Bookmarks.buildFolderUri(folder); |
| 150 | } |
| 151 | String[] accountInfo = getAccountNameAndType(this); |
| 152 | if (accountInfo != null) { |
| 153 | uri = BookmarksLoader.addAccount(uri, accountInfo[1], accountInfo[0]); |
| 154 | } |
| 155 | return uri; |
| 156 | } |
| 157 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 158 | @Override |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 159 | public void onTop(int level, Object data) { |
| 160 | if (null == data) return; |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 161 | Folder folderData = (Folder) data; |
| 162 | long folder = folderData.Id; |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 163 | LoaderManager manager = getLoaderManager(); |
| 164 | CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader( |
| 165 | LOADER_ID_FOLDER_CONTENTS)); |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 166 | loader.setUri(getUriForFolder(folder)); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 167 | loader.forceLoad(); |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 168 | if (mIsFolderNamerShowing) { |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 169 | completeOrCancelFolderNaming(true); |
| 170 | } |
Leon Scroggins | 905250c | 2010-12-17 15:25:33 -0500 | [diff] [blame] | 171 | setShowBookmarkIcon(level == 1); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view. |
| 176 | * @param show True if the icon should visible, false otherwise. |
| 177 | */ |
| 178 | private void setShowBookmarkIcon(boolean show) { |
| 179 | Drawable drawable = show ? mHeaderIcon: null; |
| 180 | mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 183 | @Override |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 184 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 185 | if (v == mFolderNamer) { |
| 186 | if (v.getText().length() > 0) { |
| 187 | if (actionId == EditorInfo.IME_NULL) { |
| 188 | // Only want to do this once. |
| 189 | if (event.getAction() == KeyEvent.ACTION_UP) { |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 190 | completeOrCancelFolderNaming(false); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 191 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 192 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 193 | } |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 194 | // Steal the key press; otherwise a newline will be added |
| 195 | return true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 196 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 197 | return false; |
| 198 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 199 | |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 200 | private void switchToDefaultView(boolean changedFolder) { |
| 201 | mFolderSelector.setVisibility(View.GONE); |
| 202 | mDefaultView.setVisibility(View.VISIBLE); |
| 203 | mCrumbHolder.setVisibility(View.GONE); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 204 | mFakeTitleHolder.setVisibility(View.VISIBLE); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 205 | if (changedFolder) { |
| 206 | Object data = mCrumbs.getTopData(); |
| 207 | if (data != null) { |
| 208 | Folder folder = (Folder) data; |
| 209 | mCurrentFolder = folder.Id; |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 210 | if (mCurrentFolder == mRootFolder) { |
| 211 | // The Spinner changed to show "Other folder ..." Change |
| 212 | // it back to "Bookmarks", which is position 0 if we are |
| 213 | // editing a folder, 1 otherwise. |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 214 | mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1); |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 215 | } else { |
| 216 | ((TextView) mFolder.getSelectedView()).setText(folder.Name); |
| 217 | } |
| 218 | } |
| 219 | } else { |
| 220 | // The user canceled selecting a folder. Revert back to the earlier |
| 221 | // selection. |
| 222 | if (mSaveToHomeScreen) { |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 223 | mFolder.setSelectionIgnoringSelectionChange(0); |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 224 | } else { |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 225 | if (mCurrentFolder == mRootFolder) { |
| 226 | mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1); |
| 227 | } else { |
| 228 | Object data = mCrumbs.getTopData(); |
| 229 | if (data != null && ((Folder) data).Id == mCurrentFolder) { |
| 230 | // We are showing the correct folder heirarchy. The |
| 231 | // folder selector will say "Other folder..." Change it |
| 232 | // to say the name of the folder once again. |
| 233 | ((TextView) mFolder.getSelectedView()).setText(((Folder) data).Name); |
| 234 | } else { |
| 235 | // We are not be showing the correct folder heirarchy. |
| 236 | // Clear the Crumbs and find the proper folder |
| 237 | mCrumbs.clear(); |
| 238 | setupTopCrumb(); |
| 239 | LoaderManager manager = getLoaderManager(); |
| 240 | manager.restartLoader(LOADER_ID_ALL_FOLDERS, null, this); |
| 241 | manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this); |
| 242 | |
| 243 | } |
| 244 | } |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 249 | @Override |
| 250 | public void onClick(View v) { |
| 251 | if (v == mButton) { |
| 252 | if (mFolderSelector.getVisibility() == View.VISIBLE) { |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 253 | // We are showing the folder selector. |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 254 | if (mIsFolderNamerShowing) { |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 255 | completeOrCancelFolderNaming(false); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 256 | } else { |
| 257 | // User has selected a folder. Go back to the opening page |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 258 | mSaveToHomeScreen = false; |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 259 | switchToDefaultView(true); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 260 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 261 | } else if (save()) { |
| 262 | finish(); |
| 263 | } |
| 264 | } else if (v == mCancelButton) { |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 265 | if (mIsFolderNamerShowing) { |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 266 | completeOrCancelFolderNaming(true); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 267 | } else if (mFolderSelector.getVisibility() == View.VISIBLE) { |
| 268 | switchToDefaultView(false); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 269 | } else { |
| 270 | finish(); |
| 271 | } |
Leon Scroggins | 0e3a7b8 | 2011-01-11 19:08:03 -0500 | [diff] [blame] | 272 | } else if (v == mFolderCancel) { |
| 273 | completeOrCancelFolderNaming(true); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 274 | } else if (v == mAddNewFolder) { |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 275 | setShowFolderNamer(true); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 276 | mFolderNamer.setText(R.string.new_folder); |
| 277 | mFolderNamer.requestFocus(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 278 | mAddNewFolder.setVisibility(View.GONE); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 279 | mAddSeparator.setVisibility(View.GONE); |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 280 | InputMethodManager imm = getInputMethodManager(); |
| 281 | // Set the InputMethodManager to focus on the ListView so that it |
| 282 | // can transfer the focus to mFolderNamer. |
| 283 | imm.focusIn(mListView); |
| 284 | imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 285 | } else if (v == mRemoveLink) { |
| 286 | if (!mEditingExisting) { |
| 287 | throw new AssertionError("Remove button should not be shown for" |
| 288 | + " new bookmarks"); |
| 289 | } |
| 290 | long id = mMap.getLong(BrowserContract.Bookmarks._ID); |
| 291 | createHandler(); |
| 292 | Message msg = Message.obtain(mHandler, BOOKMARK_DELETED); |
| 293 | BookmarkUtils.displayRemoveBookmarkDialog(id, |
| 294 | mTitle.getText().toString(), this, msg); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 295 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 298 | // FolderSpinner.OnSetSelectionListener |
| 299 | |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 300 | @Override |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 301 | public void onSetSelection(long id) { |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 302 | int intId = (int) id; |
| 303 | switch (intId) { |
| 304 | case FolderSpinnerAdapter.ROOT_FOLDER: |
Leon Scroggins | 0208194 | 2010-11-01 17:52:42 -0400 | [diff] [blame] | 305 | mCurrentFolder = mRootFolder; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 306 | mSaveToHomeScreen = false; |
| 307 | break; |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 308 | case FolderSpinnerAdapter.HOME_SCREEN: |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 309 | // Create a short cut to the home screen |
| 310 | mSaveToHomeScreen = true; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 311 | break; |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 312 | case FolderSpinnerAdapter.OTHER_FOLDER: |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 313 | switchToFolderSelector(); |
| 314 | break; |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 315 | case FolderSpinnerAdapter.RECENT_FOLDER: |
| 316 | mCurrentFolder = mFolderAdapter.recentFolderId(); |
| 317 | mSaveToHomeScreen = false; |
| 318 | // In case the user decides to select OTHER_FOLDER |
| 319 | // and choose a different one, so that we will start from |
| 320 | // the correct place. |
| 321 | LoaderManager manager = getLoaderManager(); |
| 322 | manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this); |
| 323 | manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 324 | default: |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 325 | break; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 326 | } |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 327 | } |
| 328 | |
Leon Scroggins | 0e3a7b8 | 2011-01-11 19:08:03 -0500 | [diff] [blame] | 329 | /** |
| 330 | * Finish naming a folder, and close the IME |
| 331 | * @param cancel If true, the new folder is not created. If false, the new |
| 332 | * folder is created and the user is taken inside it. |
| 333 | */ |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 334 | private void completeOrCancelFolderNaming(boolean cancel) { |
| 335 | if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) { |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 336 | String name = mFolderNamer.getText().toString(); |
| 337 | long id = addFolderToCurrent(mFolderNamer.getText().toString()); |
| 338 | descendInto(name, id); |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 339 | } |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 340 | setShowFolderNamer(false); |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 341 | mAddNewFolder.setVisibility(View.VISIBLE); |
| 342 | mAddSeparator.setVisibility(View.VISIBLE); |
| 343 | getInputMethodManager().hideSoftInputFromWindow( |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 344 | mListView.getWindowToken(), 0); |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 347 | private long addFolderToCurrent(String name) { |
| 348 | // Add the folder to the database |
| 349 | ContentValues values = new ContentValues(); |
| 350 | values.put(BrowserContract.Bookmarks.TITLE, |
| 351 | name); |
| 352 | values.put(BrowserContract.Bookmarks.IS_FOLDER, 1); |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 353 | String[] accountInfo = getAccountNameAndType(this); |
| 354 | if (accountInfo != null) { |
| 355 | values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]); |
| 356 | values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]); |
John Reck | e89daa9 | 2010-11-05 14:39:39 -0700 | [diff] [blame] | 357 | } |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 358 | long currentFolder; |
| 359 | Object data = mCrumbs.getTopData(); |
| 360 | if (data != null) { |
| 361 | currentFolder = ((Folder) data).Id; |
| 362 | } else { |
Leon Scroggins | 0208194 | 2010-11-01 17:52:42 -0400 | [diff] [blame] | 363 | currentFolder = mRootFolder; |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 364 | } |
| 365 | values.put(BrowserContract.Bookmarks.PARENT, currentFolder); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 366 | Uri uri = getContentResolver().insert( |
| 367 | BrowserContract.Bookmarks.CONTENT_URI, values); |
| 368 | if (uri != null) { |
| 369 | return ContentUris.parseId(uri); |
| 370 | } else { |
| 371 | return -1; |
| 372 | } |
| 373 | } |
| 374 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 375 | private void switchToFolderSelector() { |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 376 | // Set the list to the top in case it is scrolled. |
| 377 | mListView.setSelection(0); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 378 | mDefaultView.setVisibility(View.GONE); |
| 379 | mFolderSelector.setVisibility(View.VISIBLE); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 380 | mCrumbHolder.setVisibility(View.VISIBLE); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 381 | mFakeTitleHolder.setVisibility(View.GONE); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 382 | mAddNewFolder.setVisibility(View.VISIBLE); |
| 383 | mAddSeparator.setVisibility(View.VISIBLE); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 384 | } |
| 385 | |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 386 | private void descendInto(String foldername, long id) { |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 387 | if (id != DEFAULT_FOLDER_ID) { |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 388 | mCrumbs.pushView(foldername, new Folder(foldername, id)); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 389 | mCrumbs.notifyController(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 393 | @Override |
| 394 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 395 | String[] projection; |
| 396 | switch (id) { |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 397 | case LOADER_ID_CHECK_FOR_DUPE: |
| 398 | projection = new String[] { |
| 399 | BrowserContract.Bookmarks._ID, |
| 400 | BrowserContract.Bookmarks.PARENT, |
| 401 | BrowserContract.Bookmarks.TITLE |
| 402 | }; |
| 403 | return new CursorLoader(this, |
| 404 | BookmarkUtils.getBookmarksUri(this), |
| 405 | projection, |
| 406 | BrowserContract.Bookmarks.URL + " = ?", |
| 407 | new String[] { mOriginalUrl }, |
| 408 | null); |
| 409 | case LOADER_ID_FIND_ROOT: |
| 410 | String name = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME); |
| 411 | String type = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE); |
| 412 | |
| 413 | projection = new String[] { BrowserContract.Bookmarks._ID }; |
| 414 | String selection = BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND " |
| 415 | + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND " |
| 416 | + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?"; |
| 417 | String[] selArgs = new String[] { |
| 418 | BrowserContract.ChromeSyncColumns.FOLDER_NAME_BOOKMARKS_BAR, |
| 419 | name, |
| 420 | type |
| 421 | }; |
| 422 | return new CursorLoader(this, |
| 423 | BrowserContract.Bookmarks.CONTENT_URI, |
| 424 | projection, |
| 425 | selection, |
| 426 | selArgs, |
| 427 | null); |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 428 | case LOADER_ID_FIND_FOLDER_BY_ID: |
| 429 | projection = new String[] { |
| 430 | BrowserContract.Bookmarks._ID, |
| 431 | BrowserContract.Bookmarks.TITLE |
| 432 | }; |
| 433 | return new CursorLoader(this, |
| 434 | BrowserContract.Bookmarks.CONTENT_URI, |
| 435 | projection, |
| 436 | BrowserContract.Bookmarks._ID + " = " |
| 437 | + args.getLong(BrowserContract.Bookmarks._ID), |
| 438 | null, |
| 439 | null); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 440 | case LOADER_ID_ALL_FOLDERS: |
| 441 | projection = new String[] { |
| 442 | BrowserContract.Bookmarks._ID, |
| 443 | BrowserContract.Bookmarks.PARENT, |
| 444 | BrowserContract.Bookmarks.TITLE, |
| 445 | BrowserContract.Bookmarks.IS_FOLDER |
| 446 | }; |
| 447 | return new CursorLoader(this, |
| 448 | BrowserContract.Bookmarks.CONTENT_URI, |
| 449 | projection, |
| 450 | BrowserContract.Bookmarks.IS_FOLDER + " != 0", |
| 451 | null, |
| 452 | null); |
| 453 | case LOADER_ID_FOLDER_CONTENTS: |
| 454 | projection = new String[] { |
| 455 | BrowserContract.Bookmarks._ID, |
| 456 | BrowserContract.Bookmarks.TITLE, |
| 457 | BrowserContract.Bookmarks.IS_FOLDER |
| 458 | }; |
Leon Scroggins | c112990 | 2010-12-08 15:28:33 -0500 | [diff] [blame] | 459 | String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0"; |
| 460 | if (mEditingFolder) { |
| 461 | where += " AND " + BrowserContract.Bookmarks._ID + " != " |
| 462 | + mMap.getLong(BrowserContract.Bookmarks._ID); |
| 463 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 464 | return new CursorLoader(this, |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 465 | getUriForFolder(mCurrentFolder), |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 466 | projection, |
Leon Scroggins | c112990 | 2010-12-08 15:28:33 -0500 | [diff] [blame] | 467 | where, |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 468 | null, |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 469 | BrowserContract.Bookmarks._ID + " ASC"); |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 470 | case LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK: |
| 471 | projection = new String[] { |
| 472 | BrowserContract.Bookmarks.PARENT |
| 473 | }; |
| 474 | return new CursorLoader(this, |
| 475 | BrowserContract.Bookmarks.CONTENT_URI, |
| 476 | projection, |
| 477 | BrowserContract.Bookmarks.IS_FOLDER + " = 0", |
| 478 | null, |
| 479 | BrowserContract.Bookmarks.DATE_CREATED + " DESC"); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 480 | default: |
| 481 | throw new AssertionError("Asking for nonexistant loader!"); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | @Override |
| 486 | public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { |
| 487 | switch (loader.getId()) { |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 488 | case LOADER_ID_CHECK_FOR_DUPE: |
| 489 | if (cursor != null && cursor.moveToFirst()) { |
| 490 | // Site is bookmarked. |
| 491 | mEditingExisting = true; |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 492 | showRemoveButton(); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 493 | mFakeTitle.setText(R.string.edit_bookmark); |
| 494 | int index = cursor.getColumnIndexOrThrow( |
| 495 | BrowserContract.Bookmarks.PARENT); |
| 496 | mCurrentFolder = cursor.getLong(index); |
| 497 | index = cursor.getColumnIndexOrThrow( |
| 498 | BrowserContract.Bookmarks.TITLE); |
| 499 | String title = cursor.getString(index); |
| 500 | mTitle.setText(title); |
| 501 | index = cursor.getColumnIndexOrThrow( |
| 502 | BrowserContract.Bookmarks._ID); |
| 503 | long id = cursor.getLong(index); |
| 504 | mMap.putLong(BrowserContract.Bookmarks._ID, id); |
| 505 | } |
| 506 | onCurrentFolderFound(); |
| 507 | getLoaderManager().destroyLoader(LOADER_ID_CHECK_FOR_DUPE); |
| 508 | break; |
| 509 | case LOADER_ID_FIND_ROOT: |
| 510 | long root; |
| 511 | if (cursor != null && cursor.moveToFirst()) { |
| 512 | root = cursor.getLong(0); |
| 513 | } else { |
| 514 | root = BrowserProvider2.FIXED_ID_ROOT; |
| 515 | } |
| 516 | onRootFolderFound(root); |
| 517 | getLoaderManager().destroyLoader(LOADER_ID_FIND_ROOT); |
| 518 | break; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 519 | case LOADER_ID_FOLDER_CONTENTS: |
| 520 | mAdapter.changeCursor(cursor); |
| 521 | break; |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 522 | case LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK: |
| 523 | LoaderManager manager = getLoaderManager(); |
| 524 | if (cursor != null && cursor.moveToFirst()) { |
| 525 | // Find the parent |
| 526 | long lastUsedFolder = cursor.getLong(0); |
| 527 | if (lastUsedFolder != mRootFolder |
| 528 | && lastUsedFolder != mCurrentFolder |
| 529 | && lastUsedFolder != 0) { |
| 530 | // Find out the parent's name |
| 531 | Bundle b = new Bundle(); |
| 532 | b.putLong(BrowserContract.Bookmarks._ID, lastUsedFolder); |
| 533 | manager.initLoader(LOADER_ID_FIND_FOLDER_BY_ID, b, this); |
| 534 | } |
| 535 | } |
| 536 | manager.destroyLoader(LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK); |
| 537 | break; |
| 538 | case LOADER_ID_FIND_FOLDER_BY_ID: |
| 539 | if (cursor != null && cursor.moveToFirst()) { |
| 540 | long id = cursor.getLong(cursor.getColumnIndexOrThrow( |
| 541 | BrowserContract.Bookmarks._ID)); |
| 542 | String title = cursor.getString(cursor.getColumnIndexOrThrow( |
| 543 | BrowserContract.Bookmarks.TITLE)); |
| 544 | mFolderAdapter.addRecentFolder(id, title); |
| 545 | } |
| 546 | getLoaderManager().destroyLoader(LOADER_ID_FIND_FOLDER_BY_ID); |
| 547 | break; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 548 | case LOADER_ID_ALL_FOLDERS: |
| 549 | long parent = mCurrentFolder; |
| 550 | int idIndex = cursor.getColumnIndexOrThrow( |
| 551 | BrowserContract.Bookmarks._ID); |
| 552 | int titleIndex = cursor.getColumnIndexOrThrow( |
| 553 | BrowserContract.Bookmarks.TITLE); |
| 554 | int parentIndex = cursor.getColumnIndexOrThrow( |
| 555 | BrowserContract.Bookmarks.PARENT); |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 556 | // If the user is editing anything inside the "Other Bookmarks" |
| 557 | // folder, we need to stop searching up when we reach its parent. |
| 558 | // Find the root folder |
| 559 | moveCursorToFolder(cursor, mRootFolder, idIndex); |
| 560 | // omniparent is the folder which contains root, and therefore |
| 561 | // also the parent of the "Other Bookmarks" folder. |
| 562 | long omniparent = cursor.getLong(parentIndex); |
| 563 | Stack<Folder> folderStack = new Stack<Folder>(); |
| 564 | while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 565 | // First, find the folder corresponding to the current |
| 566 | // folder |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 567 | moveCursorToFolder(cursor, parent, idIndex); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 568 | String name = cursor.getString(titleIndex); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 569 | if (parent == mCurrentFolder) { |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 570 | ((TextView) mFolder.getSelectedView()).setText(name); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 571 | } |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 572 | folderStack.push(new Folder(name, parent)); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 573 | parent = cursor.getLong(parentIndex); |
| 574 | } |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 575 | while (!folderStack.isEmpty()) { |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 576 | Folder thisFolder = folderStack.pop(); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 577 | mCrumbs.pushView(thisFolder.Name, thisFolder); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 578 | } |
Dianne Hackborn | 71e76c7 | 2010-12-20 11:44:09 -0800 | [diff] [blame] | 579 | getLoaderManager().destroyLoader(LOADER_ID_ALL_FOLDERS); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 580 | break; |
| 581 | default: |
| 582 | break; |
| 583 | } |
| 584 | } |
| 585 | |
Dianne Hackborn | 39772c8 | 2010-12-16 00:43:54 -0800 | [diff] [blame] | 586 | public void onLoaderReset(Loader<Cursor> loader) { |
| 587 | switch (loader.getId()) { |
| 588 | case LOADER_ID_FOLDER_CONTENTS: |
| 589 | mAdapter.changeCursor(null); |
| 590 | break; |
| 591 | } |
| 592 | } |
| 593 | |
Leon Scroggins | 0208194 | 2010-11-01 17:52:42 -0400 | [diff] [blame] | 594 | /** |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 595 | * Move cursor to the position that has folderToFind as its "_id". |
| 596 | * @param cursor Cursor containing folders in the bookmarks database |
| 597 | * @param folderToFind "_id" of the folder to move to. |
| 598 | * @param idIndex Index in cursor of "_id" |
| 599 | * @throws AssertionError if cursor is empty or there is no row with folderToFind |
| 600 | * as its "_id". |
| 601 | */ |
| 602 | void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex) |
| 603 | throws AssertionError { |
| 604 | if (!cursor.moveToFirst()) { |
| 605 | throw new AssertionError("No folders in the database!"); |
| 606 | } |
| 607 | long folder; |
| 608 | do { |
| 609 | folder = cursor.getLong(idIndex); |
| 610 | } while (folder != folderToFind && cursor.moveToNext()); |
| 611 | if (cursor.isAfterLast()) { |
| 612 | throw new AssertionError("Folder(id=" + folderToFind |
| 613 | + ") holding this bookmark does not exist!"); |
| 614 | } |
| 615 | } |
| 616 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 617 | @Override |
| 618 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 619 | long id) { |
Leon Scroggins | d14cb12 | 2010-09-29 16:01:48 -0400 | [diff] [blame] | 620 | TextView tv = (TextView) view.findViewById(android.R.id.text1); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 621 | // Switch to the folder that was clicked on. |
Leon Scroggins | d14cb12 | 2010-09-29 16:01:48 -0400 | [diff] [blame] | 622 | descendInto(tv.getText().toString(), id); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 623 | } |
| 624 | |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 625 | private void setShowFolderNamer(boolean show) { |
| 626 | if (show != mIsFolderNamerShowing) { |
| 627 | mIsFolderNamerShowing = show; |
| 628 | if (show) { |
| 629 | // Set the selection to the folder namer so it will be in |
| 630 | // view. |
| 631 | mListView.addFooterView(mFolderNamerHolder); |
| 632 | } else { |
| 633 | mListView.removeFooterView(mFolderNamerHolder); |
| 634 | } |
| 635 | // Refresh the list. |
| 636 | mListView.setAdapter(mAdapter); |
| 637 | if (show) { |
| 638 | mListView.setSelection(mListView.getCount() - 1); |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 643 | /** |
| 644 | * Shows a list of names of folders. |
| 645 | */ |
| 646 | private class FolderAdapter extends CursorAdapter { |
| 647 | public FolderAdapter(Context context) { |
| 648 | super(context, null); |
| 649 | } |
| 650 | |
| 651 | @Override |
| 652 | public void bindView(View view, Context context, Cursor cursor) { |
| 653 | ((TextView) view.findViewById(android.R.id.text1)).setText( |
| 654 | cursor.getString(cursor.getColumnIndexOrThrow( |
| 655 | BrowserContract.Bookmarks.TITLE))); |
| 656 | } |
| 657 | |
| 658 | @Override |
| 659 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 660 | View view = LayoutInflater.from(context).inflate( |
| 661 | R.layout.folder_list_item, null); |
| 662 | view.setBackgroundDrawable(context.getResources(). |
| 663 | getDrawable(android.R.drawable.list_selector_background)); |
| 664 | return view; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 665 | } |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 666 | |
| 667 | @Override |
| 668 | public boolean isEmpty() { |
| 669 | // Do not show the empty view if the user is creating a new folder. |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 670 | return super.isEmpty() && !mIsFolderNamerShowing; |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 671 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 672 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 673 | |
Leon Scroggins | c112990 | 2010-12-08 15:28:33 -0500 | [diff] [blame] | 674 | @Override |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 675 | protected void onCreate(Bundle icicle) { |
| 676 | super.onCreate(icicle); |
Leon Scroggins | 7453ff2 | 2010-12-15 16:03:59 -0500 | [diff] [blame] | 677 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 678 | |
| 679 | mMap = getIntent().getExtras(); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 680 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 681 | setContentView(R.layout.browser_add_bookmark); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 682 | |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 683 | Window window = getWindow(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 684 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 685 | String title = null; |
| 686 | String url = null; |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 687 | |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 688 | mFakeTitle = (TextView) findViewById(R.id.fake_title); |
| 689 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 690 | if (mMap != null) { |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 691 | Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 692 | if (b != null) { |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 693 | mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 694 | mMap = b; |
| 695 | mEditingExisting = true; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 696 | mFakeTitle.setText(R.string.edit_bookmark); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 697 | if (mEditingFolder) { |
| 698 | findViewById(R.id.row_address).setVisibility(View.GONE); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 699 | } else { |
| 700 | showRemoveButton(); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 701 | } |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 702 | } else { |
| 703 | int gravity = mMap.getInt("gravity", -1); |
| 704 | if (gravity != -1) { |
| 705 | WindowManager.LayoutParams l = window.getAttributes(); |
| 706 | l.gravity = gravity; |
| 707 | window.setAttributes(l); |
| 708 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 709 | } |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 710 | title = mMap.getString(BrowserContract.Bookmarks.TITLE); |
| 711 | url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL); |
| 712 | mTouchIconUrl = mMap.getString(TOUCH_ICON_URL); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 713 | mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID); |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 714 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 715 | |
| 716 | mTitle = (EditText) findViewById(R.id.title); |
| 717 | mTitle.setText(title); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 718 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 719 | mAddress = (EditText) findViewById(R.id.address); |
| 720 | mAddress.setText(url); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 721 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 722 | mButton = (TextView) findViewById(R.id.OK); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 723 | mButton.setOnClickListener(this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 724 | |
| 725 | mCancelButton = findViewById(R.id.cancel); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 726 | mCancelButton.setOnClickListener(this); |
| 727 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 728 | mFolder = (FolderSpinner) findViewById(R.id.folder); |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 729 | mFolderAdapter = new FolderSpinnerAdapter(!mEditingFolder); |
| 730 | mFolder.setAdapter(mFolderAdapter); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 731 | mFolder.setOnSetSelectionListener(this); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 732 | |
| 733 | mDefaultView = findViewById(R.id.default_view); |
| 734 | mFolderSelector = findViewById(R.id.folder_selector); |
| 735 | |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 736 | mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null); |
| 737 | mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 738 | mFolderNamer.setOnEditorActionListener(this); |
Leon Scroggins | 0e3a7b8 | 2011-01-11 19:08:03 -0500 | [diff] [blame] | 739 | mFolderCancel = mFolderNamerHolder.findViewById(R.id.close); |
| 740 | mFolderCancel.setOnClickListener(this); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 741 | |
| 742 | mAddNewFolder = findViewById(R.id.add_new_folder); |
| 743 | mAddNewFolder.setOnClickListener(this); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 744 | mAddSeparator = findViewById(R.id.add_divider); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 745 | |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 746 | mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs); |
| 747 | mCrumbs.setUseBackButton(true); |
| 748 | mCrumbs.setController(this); |
Michael Kolb | 5a72f18 | 2011-01-13 20:35:06 -0800 | [diff] [blame] | 749 | mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_holo_dark); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 750 | mCrumbHolder = findViewById(R.id.crumb_holder); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 751 | mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 752 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 753 | mAdapter = new FolderAdapter(this); |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 754 | mListView = (CustomListView) findViewById(R.id.list); |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 755 | View empty = findViewById(R.id.empty); |
| 756 | mListView.setEmptyView(empty); |
| 757 | mListView.setAdapter(mAdapter); |
| 758 | mListView.setOnItemClickListener(this); |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 759 | mListView.addEditText(mFolderNamer); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 760 | |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 761 | mFakeTitleHolder = findViewById(R.id.title_holder); |
| 762 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 763 | if (!window.getDecorView().isInTouchMode()) { |
| 764 | mButton.requestFocus(); |
| 765 | } |
| 766 | |
| 767 | String[] accountInfo = getAccountNameAndType(this); |
| 768 | if (accountInfo == null) { |
| 769 | onRootFolderFound(BrowserProvider2.FIXED_ID_ROOT); |
| 770 | } else { |
| 771 | Bundle args = new Bundle(); |
| 772 | args.putString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, accountInfo[0]); |
| 773 | args.putString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, accountInfo[1]); |
| 774 | getLoaderManager().initLoader(LOADER_ID_FIND_ROOT, args, this); |
| 775 | } |
| 776 | |
| 777 | } |
| 778 | |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 779 | private void showRemoveButton() { |
| 780 | findViewById(R.id.remove_divider).setVisibility(View.VISIBLE); |
| 781 | mRemoveLink = findViewById(R.id.remove); |
| 782 | mRemoveLink.setVisibility(View.VISIBLE); |
| 783 | mRemoveLink.setOnClickListener(this); |
| 784 | } |
| 785 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 786 | // Called once we have determined which folder is the root folder |
| 787 | private void onRootFolderFound(long root) { |
| 788 | mRootFolder = root; |
| 789 | if (mCurrentFolder == DEFAULT_FOLDER_ID) { |
| 790 | mCurrentFolder = mRootFolder; |
| 791 | } |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 792 | setupTopCrumb(); |
Leon Scroggins | bdff8a7 | 2011-02-11 15:49:04 -0500 | [diff] [blame] | 793 | if (mEditingExisting || TextUtils.isEmpty(mOriginalUrl) |
| 794 | || !(mMap != null && mMap.getBoolean(CHECK_FOR_DUPE))) { |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 795 | onCurrentFolderFound(); |
| 796 | } else { |
| 797 | // User is attempting to bookmark a site, rather than deliberately |
| 798 | // editing a bookmark. Rather than let them create a duplicate |
| 799 | // bookmark, see if the bookmark already exists. |
| 800 | getLoaderManager().initLoader(LOADER_ID_CHECK_FOR_DUPE, null, this); |
| 801 | } |
| 802 | } |
| 803 | |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 804 | private void setupTopCrumb() { |
| 805 | String name = getString(R.string.bookmarks); |
| 806 | mTopLevelLabel = (TextView) mCrumbs.pushView(name, false, |
| 807 | new Folder(name, mRootFolder)); |
| 808 | // To better match the other folders. |
| 809 | mTopLevelLabel.setCompoundDrawablePadding(6); |
| 810 | } |
| 811 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 812 | private void onCurrentFolderFound() { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 813 | LoaderManager manager = getLoaderManager(); |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 814 | if (mCurrentFolder != mRootFolder) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 815 | // Find all the folders |
| 816 | manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this); |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 817 | // Since we're not in the root folder, change the selection to other |
| 818 | // folder now. The text will get changed once we select the correct |
| 819 | // folder. |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 820 | mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2); |
Leon Scroggins | 905250c | 2010-12-17 15:25:33 -0500 | [diff] [blame] | 821 | } else { |
| 822 | setShowBookmarkIcon(true); |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 823 | if (!mEditingExisting) { |
| 824 | // Find the most recently saved bookmark, so that we can include it in |
| 825 | // the list of options to save the current bookmark. |
| 826 | manager.initLoader(LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK, null, this); |
| 827 | } |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 828 | if (!mEditingFolder) { |
| 829 | // Initially the "Bookmarks" folder should be showing, rather than |
| 830 | // the home screen. In the editing folder case, home screen is not |
| 831 | // an option, so "Bookmarks" folder is already at the top. |
| 832 | mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER); |
| 833 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 834 | } |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 835 | // Find the contents of the current folder |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 836 | manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 837 | } |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 838 | /** |
| 839 | * Get the account name and type of the currently synced account. |
| 840 | * @param context Context to access preferences. |
| 841 | * @return null if no account name or type. Otherwise, the result will be |
| 842 | * an array of two Strings, the accountName and accountType, respectively. |
| 843 | */ |
| 844 | private String[] getAccountNameAndType(Context context) { |
| 845 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); |
| 846 | String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); |
| 847 | String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); |
| 848 | if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) { |
| 849 | return null; |
| 850 | } |
| 851 | return new String[] { accountName, accountType }; |
| 852 | } |
| 853 | |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 854 | /** |
| 855 | * Runnable to save a bookmark, so it can be performed in its own thread. |
| 856 | */ |
| 857 | private class SaveBookmarkRunnable implements Runnable { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 858 | // FIXME: This should be an async task. |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 859 | private Message mMessage; |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 860 | private Context mContext; |
| 861 | public SaveBookmarkRunnable(Context ctx, Message msg) { |
| 862 | mContext = ctx; |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 863 | mMessage = msg; |
| 864 | } |
| 865 | public void run() { |
| 866 | // Unbundle bookmark data. |
| 867 | Bundle bundle = mMessage.getData(); |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 868 | String title = bundle.getString(BrowserContract.Bookmarks.TITLE); |
| 869 | String url = bundle.getString(BrowserContract.Bookmarks.URL); |
| 870 | boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 871 | Bitmap thumbnail = invalidateThumbnail ? null |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 872 | : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL); |
| 873 | String touchIconUrl = bundle.getString(TOUCH_ICON_URL); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 874 | |
| 875 | // Save to the bookmarks DB. |
| 876 | try { |
| 877 | final ContentResolver cr = getContentResolver(); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 878 | Bookmarks.addBookmark(AddBookmarkPage.this, false, url, |
| 879 | title, thumbnail, true, mCurrentFolder); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 880 | if (touchIconUrl != null) { |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 881 | new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 882 | } |
| 883 | mMessage.arg1 = 1; |
| 884 | } catch (IllegalStateException e) { |
| 885 | mMessage.arg1 = 0; |
| 886 | } |
| 887 | mMessage.sendToTarget(); |
| 888 | } |
| 889 | } |
| 890 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 891 | private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> { |
| 892 | Context mContext; |
| 893 | Long mId; |
| 894 | |
| 895 | public UpdateBookmarkTask(Context context, long id) { |
| 896 | mContext = context; |
| 897 | mId = id; |
| 898 | } |
| 899 | |
| 900 | @Override |
| 901 | protected Void doInBackground(ContentValues... params) { |
| 902 | if (params.length != 1) { |
| 903 | throw new IllegalArgumentException("No ContentValues provided!"); |
| 904 | } |
| 905 | Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId); |
| 906 | mContext.getContentResolver().update( |
| 907 | uri, |
| 908 | params[0], null, null); |
| 909 | return null; |
| 910 | } |
| 911 | } |
| 912 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 913 | private void createHandler() { |
| 914 | if (mHandler == null) { |
| 915 | mHandler = new Handler() { |
| 916 | @Override |
| 917 | public void handleMessage(Message msg) { |
| 918 | switch (msg.what) { |
| 919 | case SAVE_BOOKMARK: |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 920 | if (1 == msg.arg1) { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 921 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved, |
| 922 | Toast.LENGTH_LONG).show(); |
| 923 | } else { |
| 924 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved, |
| 925 | Toast.LENGTH_LONG).show(); |
| 926 | } |
| 927 | break; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 928 | case TOUCH_ICON_DOWNLOADED: |
| 929 | Bundle b = msg.getData(); |
| 930 | sendBroadcast(BookmarkUtils.createAddToHomeIntent( |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 931 | AddBookmarkPage.this, |
| 932 | b.getString(BrowserContract.Bookmarks.URL), |
| 933 | b.getString(BrowserContract.Bookmarks.TITLE), |
| 934 | (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON), |
| 935 | (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON))); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 936 | break; |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 937 | case BOOKMARK_DELETED: |
| 938 | finish(); |
| 939 | break; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | }; |
| 943 | } |
| 944 | } |
| 945 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 946 | /** |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 947 | * Parse the data entered in the dialog and post a message to update the bookmarks database. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 948 | */ |
| 949 | boolean save() { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 950 | createHandler(); |
| 951 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 952 | String title = mTitle.getText().toString().trim(); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 953 | String unfilteredUrl; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 954 | unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString()); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 955 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 956 | boolean emptyTitle = title.length() == 0; |
| 957 | boolean emptyUrl = unfilteredUrl.trim().length() == 0; |
| 958 | Resources r = getResources(); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 959 | if (emptyTitle || (emptyUrl && !mEditingFolder)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 960 | if (emptyTitle) { |
| 961 | mTitle.setError(r.getText(R.string.bookmark_needs_title)); |
| 962 | } |
| 963 | if (emptyUrl) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 964 | mAddress.setError(r.getText(R.string.bookmark_needs_url)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 965 | } |
Leon Scroggins III | 6e3faea | 2010-09-07 15:22:14 -0400 | [diff] [blame] | 966 | return false; |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 967 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 968 | } |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 969 | String url = unfilteredUrl.trim(); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 970 | if (!mEditingFolder) { |
| 971 | try { |
| 972 | // We allow bookmarks with a javascript: scheme, but these will in most cases |
| 973 | // fail URI parsing, so don't try it if that's the kind of bookmark we have. |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 974 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 975 | if (!url.toLowerCase().startsWith("javascript:")) { |
| 976 | URI uriObj = new URI(url); |
| 977 | String scheme = uriObj.getScheme(); |
| 978 | if (!Bookmarks.urlHasAcceptableScheme(url)) { |
| 979 | // If the scheme was non-null, let the user know that we |
| 980 | // can't save their bookmark. If it was null, we'll assume |
| 981 | // they meant http when we parse it in the WebAddress class. |
| 982 | if (scheme != null) { |
| 983 | mAddress.setError(r.getText(R.string.bookmark_cannot_save_url)); |
| 984 | return false; |
| 985 | } |
| 986 | WebAddress address; |
| 987 | try { |
| 988 | address = new WebAddress(unfilteredUrl); |
| 989 | } catch (ParseException e) { |
| 990 | throw new URISyntaxException("", ""); |
| 991 | } |
| 992 | if (address.getHost().length() == 0) { |
| 993 | throw new URISyntaxException("", ""); |
| 994 | } |
| 995 | url = address.toString(); |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 996 | } |
Ben Murdoch | de35362 | 2009-10-12 10:29:00 +0100 | [diff] [blame] | 997 | } |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 998 | } catch (URISyntaxException e) { |
| 999 | mAddress.setError(r.getText(R.string.bookmark_url_not_valid)); |
| 1000 | return false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1001 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1002 | } |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 1003 | |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1004 | if (mSaveToHomeScreen) { |
| 1005 | mEditingExisting = false; |
| 1006 | } |
| 1007 | |
| 1008 | boolean urlUnmodified = url.equals(mOriginalUrl); |
| 1009 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 1010 | if (mEditingExisting) { |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 1011 | Long id = mMap.getLong(BrowserContract.Bookmarks._ID); |
| 1012 | ContentValues values = new ContentValues(); |
| 1013 | values.put(BrowserContract.Bookmarks.TITLE, title); |
| 1014 | values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder); |
| 1015 | if (!mEditingFolder) { |
| 1016 | values.put(BrowserContract.Bookmarks.URL, url); |
| 1017 | if (!urlUnmodified) { |
| 1018 | values.putNull(BrowserContract.Bookmarks.THUMBNAIL); |
| 1019 | } |
| 1020 | } |
| 1021 | if (values.size() > 0) { |
| 1022 | new UpdateBookmarkTask(getApplicationContext(), id).execute(values); |
| 1023 | } |
| 1024 | setResult(RESULT_OK); |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 1025 | } else { |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1026 | Bitmap thumbnail; |
| 1027 | Bitmap favicon; |
| 1028 | if (urlUnmodified) { |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 1029 | thumbnail = (Bitmap) mMap.getParcelable( |
| 1030 | BrowserContract.Bookmarks.THUMBNAIL); |
| 1031 | favicon = (Bitmap) mMap.getParcelable( |
| 1032 | BrowserContract.Bookmarks.FAVICON); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1033 | } else { |
| 1034 | thumbnail = null; |
| 1035 | favicon = null; |
| 1036 | } |
| 1037 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 1038 | Bundle bundle = new Bundle(); |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 1039 | bundle.putString(BrowserContract.Bookmarks.TITLE, title); |
| 1040 | bundle.putString(BrowserContract.Bookmarks.URL, url); |
| 1041 | bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1042 | |
| 1043 | if (mSaveToHomeScreen) { |
| 1044 | if (mTouchIconUrl != null && urlUnmodified) { |
| 1045 | Message msg = Message.obtain(mHandler, |
| 1046 | TOUCH_ICON_DOWNLOADED); |
| 1047 | msg.setData(bundle); |
| 1048 | DownloadTouchIcon icon = new DownloadTouchIcon(this, msg, |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 1049 | mMap.getString(USER_AGENT)); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1050 | icon.execute(mTouchIconUrl); |
| 1051 | } else { |
| 1052 | sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url, |
| 1053 | title, null /*touchIcon*/, favicon)); |
| 1054 | } |
| 1055 | } else { |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 1056 | bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail); |
| 1057 | bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified); |
| 1058 | bundle.putString(TOUCH_ICON_URL, mTouchIconUrl); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1059 | // Post a message to write to the DB. |
| 1060 | Message msg = Message.obtain(mHandler, SAVE_BOOKMARK); |
| 1061 | msg.setData(bundle); |
| 1062 | // Start a new thread so as to not slow down the UI |
| 1063 | Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg)); |
| 1064 | t.start(); |
| 1065 | } |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 1066 | setResult(RESULT_OK); |
Kristian Monsen | 0a1d838 | 2010-02-01 18:41:07 +0000 | [diff] [blame] | 1067 | LogTag.logBookmarkAdded(url, "bookmarkview"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1068 | } |
| 1069 | return true; |
| 1070 | } |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 1071 | |
| 1072 | /* |
| 1073 | * Class used as a proxy for the InputMethodManager to get to mFolderNamer |
| 1074 | */ |
| 1075 | public static class CustomListView extends ListView { |
| 1076 | private EditText mEditText; |
| 1077 | |
| 1078 | public void addEditText(EditText editText) { |
| 1079 | mEditText = editText; |
| 1080 | } |
| 1081 | |
| 1082 | public CustomListView(Context context) { |
| 1083 | super(context); |
| 1084 | } |
| 1085 | |
| 1086 | public CustomListView(Context context, AttributeSet attrs) { |
| 1087 | super(context, attrs); |
| 1088 | } |
| 1089 | |
| 1090 | public CustomListView(Context context, AttributeSet attrs, int defStyle) { |
| 1091 | super(context, attrs, defStyle); |
| 1092 | } |
| 1093 | |
| 1094 | @Override |
| 1095 | public boolean checkInputConnectionProxy(View view) { |
| 1096 | return view == mEditText; |
| 1097 | } |
| 1098 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1099 | } |