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 | 0b95ad4 | 2011-02-23 15:23:48 -0500 | [diff] [blame^] | 324 | break; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 325 | default: |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 326 | break; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 327 | } |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 328 | } |
| 329 | |
Leon Scroggins | 0e3a7b8 | 2011-01-11 19:08:03 -0500 | [diff] [blame] | 330 | /** |
| 331 | * Finish naming a folder, and close the IME |
| 332 | * @param cancel If true, the new folder is not created. If false, the new |
| 333 | * folder is created and the user is taken inside it. |
| 334 | */ |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 335 | private void completeOrCancelFolderNaming(boolean cancel) { |
| 336 | if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) { |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 337 | String name = mFolderNamer.getText().toString(); |
| 338 | long id = addFolderToCurrent(mFolderNamer.getText().toString()); |
| 339 | descendInto(name, id); |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 340 | } |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 341 | setShowFolderNamer(false); |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 342 | mAddNewFolder.setVisibility(View.VISIBLE); |
| 343 | mAddSeparator.setVisibility(View.VISIBLE); |
| 344 | getInputMethodManager().hideSoftInputFromWindow( |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 345 | mListView.getWindowToken(), 0); |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 348 | private long addFolderToCurrent(String name) { |
| 349 | // Add the folder to the database |
| 350 | ContentValues values = new ContentValues(); |
| 351 | values.put(BrowserContract.Bookmarks.TITLE, |
| 352 | name); |
| 353 | values.put(BrowserContract.Bookmarks.IS_FOLDER, 1); |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 354 | String[] accountInfo = getAccountNameAndType(this); |
| 355 | if (accountInfo != null) { |
| 356 | values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]); |
| 357 | values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]); |
John Reck | e89daa9 | 2010-11-05 14:39:39 -0700 | [diff] [blame] | 358 | } |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 359 | long currentFolder; |
| 360 | Object data = mCrumbs.getTopData(); |
| 361 | if (data != null) { |
| 362 | currentFolder = ((Folder) data).Id; |
| 363 | } else { |
Leon Scroggins | 0208194 | 2010-11-01 17:52:42 -0400 | [diff] [blame] | 364 | currentFolder = mRootFolder; |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 365 | } |
| 366 | values.put(BrowserContract.Bookmarks.PARENT, currentFolder); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 367 | Uri uri = getContentResolver().insert( |
| 368 | BrowserContract.Bookmarks.CONTENT_URI, values); |
| 369 | if (uri != null) { |
| 370 | return ContentUris.parseId(uri); |
| 371 | } else { |
| 372 | return -1; |
| 373 | } |
| 374 | } |
| 375 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 376 | private void switchToFolderSelector() { |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 377 | // Set the list to the top in case it is scrolled. |
| 378 | mListView.setSelection(0); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 379 | mDefaultView.setVisibility(View.GONE); |
| 380 | mFolderSelector.setVisibility(View.VISIBLE); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 381 | mCrumbHolder.setVisibility(View.VISIBLE); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 382 | mFakeTitleHolder.setVisibility(View.GONE); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 383 | mAddNewFolder.setVisibility(View.VISIBLE); |
| 384 | mAddSeparator.setVisibility(View.VISIBLE); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 385 | } |
| 386 | |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 387 | private void descendInto(String foldername, long id) { |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 388 | if (id != DEFAULT_FOLDER_ID) { |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 389 | mCrumbs.pushView(foldername, new Folder(foldername, id)); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 390 | mCrumbs.notifyController(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 394 | @Override |
| 395 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 396 | String[] projection; |
| 397 | switch (id) { |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 398 | case LOADER_ID_CHECK_FOR_DUPE: |
| 399 | projection = new String[] { |
| 400 | BrowserContract.Bookmarks._ID, |
| 401 | BrowserContract.Bookmarks.PARENT, |
| 402 | BrowserContract.Bookmarks.TITLE |
| 403 | }; |
| 404 | return new CursorLoader(this, |
| 405 | BookmarkUtils.getBookmarksUri(this), |
| 406 | projection, |
| 407 | BrowserContract.Bookmarks.URL + " = ?", |
| 408 | new String[] { mOriginalUrl }, |
| 409 | null); |
| 410 | case LOADER_ID_FIND_ROOT: |
| 411 | String name = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME); |
| 412 | String type = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE); |
| 413 | |
| 414 | projection = new String[] { BrowserContract.Bookmarks._ID }; |
| 415 | String selection = BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND " |
| 416 | + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND " |
| 417 | + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?"; |
| 418 | String[] selArgs = new String[] { |
| 419 | BrowserContract.ChromeSyncColumns.FOLDER_NAME_BOOKMARKS_BAR, |
| 420 | name, |
| 421 | type |
| 422 | }; |
| 423 | return new CursorLoader(this, |
| 424 | BrowserContract.Bookmarks.CONTENT_URI, |
| 425 | projection, |
| 426 | selection, |
| 427 | selArgs, |
| 428 | null); |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 429 | case LOADER_ID_FIND_FOLDER_BY_ID: |
| 430 | projection = new String[] { |
| 431 | BrowserContract.Bookmarks._ID, |
| 432 | BrowserContract.Bookmarks.TITLE |
| 433 | }; |
| 434 | return new CursorLoader(this, |
Leon Scroggins | 3ba3e48 | 2011-02-23 12:08:09 -0500 | [diff] [blame] | 435 | BookmarkUtils.getBookmarksUri(this), |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 436 | projection, |
| 437 | BrowserContract.Bookmarks._ID + " = " |
| 438 | + args.getLong(BrowserContract.Bookmarks._ID), |
| 439 | null, |
| 440 | null); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 441 | case LOADER_ID_ALL_FOLDERS: |
| 442 | projection = new String[] { |
| 443 | BrowserContract.Bookmarks._ID, |
| 444 | BrowserContract.Bookmarks.PARENT, |
| 445 | BrowserContract.Bookmarks.TITLE, |
| 446 | BrowserContract.Bookmarks.IS_FOLDER |
| 447 | }; |
| 448 | return new CursorLoader(this, |
Leon Scroggins | 3ba3e48 | 2011-02-23 12:08:09 -0500 | [diff] [blame] | 449 | BookmarkUtils.getBookmarksUri(this), |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 450 | projection, |
| 451 | BrowserContract.Bookmarks.IS_FOLDER + " != 0", |
| 452 | null, |
| 453 | null); |
| 454 | case LOADER_ID_FOLDER_CONTENTS: |
| 455 | projection = new String[] { |
| 456 | BrowserContract.Bookmarks._ID, |
| 457 | BrowserContract.Bookmarks.TITLE, |
| 458 | BrowserContract.Bookmarks.IS_FOLDER |
| 459 | }; |
Leon Scroggins | c112990 | 2010-12-08 15:28:33 -0500 | [diff] [blame] | 460 | String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0"; |
| 461 | if (mEditingFolder) { |
| 462 | where += " AND " + BrowserContract.Bookmarks._ID + " != " |
| 463 | + mMap.getLong(BrowserContract.Bookmarks._ID); |
| 464 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 465 | return new CursorLoader(this, |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 466 | getUriForFolder(mCurrentFolder), |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 467 | projection, |
Leon Scroggins | c112990 | 2010-12-08 15:28:33 -0500 | [diff] [blame] | 468 | where, |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 469 | null, |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 470 | BrowserContract.Bookmarks._ID + " ASC"); |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 471 | case LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK: |
| 472 | projection = new String[] { |
| 473 | BrowserContract.Bookmarks.PARENT |
| 474 | }; |
| 475 | return new CursorLoader(this, |
Leon Scroggins | 3ba3e48 | 2011-02-23 12:08:09 -0500 | [diff] [blame] | 476 | BookmarkUtils.getBookmarksUri(this), |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 477 | projection, |
| 478 | BrowserContract.Bookmarks.IS_FOLDER + " = 0", |
| 479 | null, |
| 480 | BrowserContract.Bookmarks.DATE_CREATED + " DESC"); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 481 | default: |
| 482 | throw new AssertionError("Asking for nonexistant loader!"); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { |
| 488 | switch (loader.getId()) { |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 489 | case LOADER_ID_CHECK_FOR_DUPE: |
| 490 | if (cursor != null && cursor.moveToFirst()) { |
| 491 | // Site is bookmarked. |
| 492 | mEditingExisting = true; |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 493 | showRemoveButton(); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 494 | mFakeTitle.setText(R.string.edit_bookmark); |
| 495 | int index = cursor.getColumnIndexOrThrow( |
| 496 | BrowserContract.Bookmarks.PARENT); |
| 497 | mCurrentFolder = cursor.getLong(index); |
| 498 | index = cursor.getColumnIndexOrThrow( |
| 499 | BrowserContract.Bookmarks.TITLE); |
| 500 | String title = cursor.getString(index); |
| 501 | mTitle.setText(title); |
| 502 | index = cursor.getColumnIndexOrThrow( |
| 503 | BrowserContract.Bookmarks._ID); |
| 504 | long id = cursor.getLong(index); |
| 505 | mMap.putLong(BrowserContract.Bookmarks._ID, id); |
| 506 | } |
| 507 | onCurrentFolderFound(); |
| 508 | getLoaderManager().destroyLoader(LOADER_ID_CHECK_FOR_DUPE); |
| 509 | break; |
| 510 | case LOADER_ID_FIND_ROOT: |
| 511 | long root; |
| 512 | if (cursor != null && cursor.moveToFirst()) { |
| 513 | root = cursor.getLong(0); |
| 514 | } else { |
| 515 | root = BrowserProvider2.FIXED_ID_ROOT; |
| 516 | } |
| 517 | onRootFolderFound(root); |
| 518 | getLoaderManager().destroyLoader(LOADER_ID_FIND_ROOT); |
| 519 | break; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 520 | case LOADER_ID_FOLDER_CONTENTS: |
| 521 | mAdapter.changeCursor(cursor); |
| 522 | break; |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 523 | case LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK: |
| 524 | LoaderManager manager = getLoaderManager(); |
| 525 | if (cursor != null && cursor.moveToFirst()) { |
| 526 | // Find the parent |
| 527 | long lastUsedFolder = cursor.getLong(0); |
| 528 | if (lastUsedFolder != mRootFolder |
| 529 | && lastUsedFolder != mCurrentFolder |
| 530 | && lastUsedFolder != 0) { |
| 531 | // Find out the parent's name |
| 532 | Bundle b = new Bundle(); |
| 533 | b.putLong(BrowserContract.Bookmarks._ID, lastUsedFolder); |
| 534 | manager.initLoader(LOADER_ID_FIND_FOLDER_BY_ID, b, this); |
| 535 | } |
| 536 | } |
| 537 | manager.destroyLoader(LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK); |
| 538 | break; |
| 539 | case LOADER_ID_FIND_FOLDER_BY_ID: |
| 540 | if (cursor != null && cursor.moveToFirst()) { |
| 541 | long id = cursor.getLong(cursor.getColumnIndexOrThrow( |
| 542 | BrowserContract.Bookmarks._ID)); |
| 543 | String title = cursor.getString(cursor.getColumnIndexOrThrow( |
| 544 | BrowserContract.Bookmarks.TITLE)); |
| 545 | mFolderAdapter.addRecentFolder(id, title); |
| 546 | } |
| 547 | getLoaderManager().destroyLoader(LOADER_ID_FIND_FOLDER_BY_ID); |
| 548 | break; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 549 | case LOADER_ID_ALL_FOLDERS: |
| 550 | long parent = mCurrentFolder; |
| 551 | int idIndex = cursor.getColumnIndexOrThrow( |
| 552 | BrowserContract.Bookmarks._ID); |
| 553 | int titleIndex = cursor.getColumnIndexOrThrow( |
| 554 | BrowserContract.Bookmarks.TITLE); |
| 555 | int parentIndex = cursor.getColumnIndexOrThrow( |
| 556 | BrowserContract.Bookmarks.PARENT); |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 557 | // If the user is editing anything inside the "Other Bookmarks" |
| 558 | // folder, we need to stop searching up when we reach its parent. |
| 559 | // Find the root folder |
| 560 | moveCursorToFolder(cursor, mRootFolder, idIndex); |
| 561 | // omniparent is the folder which contains root, and therefore |
| 562 | // also the parent of the "Other Bookmarks" folder. |
| 563 | long omniparent = cursor.getLong(parentIndex); |
| 564 | Stack<Folder> folderStack = new Stack<Folder>(); |
| 565 | while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 566 | // First, find the folder corresponding to the current |
| 567 | // folder |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 568 | moveCursorToFolder(cursor, parent, idIndex); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 569 | String name = cursor.getString(titleIndex); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 570 | if (parent == mCurrentFolder) { |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 571 | ((TextView) mFolder.getSelectedView()).setText(name); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 572 | } |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 573 | folderStack.push(new Folder(name, parent)); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 574 | parent = cursor.getLong(parentIndex); |
| 575 | } |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 576 | while (!folderStack.isEmpty()) { |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 577 | Folder thisFolder = folderStack.pop(); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 578 | mCrumbs.pushView(thisFolder.Name, thisFolder); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 579 | } |
Dianne Hackborn | 71e76c7 | 2010-12-20 11:44:09 -0800 | [diff] [blame] | 580 | getLoaderManager().destroyLoader(LOADER_ID_ALL_FOLDERS); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 581 | break; |
| 582 | default: |
| 583 | break; |
| 584 | } |
| 585 | } |
| 586 | |
Dianne Hackborn | 39772c8 | 2010-12-16 00:43:54 -0800 | [diff] [blame] | 587 | public void onLoaderReset(Loader<Cursor> loader) { |
| 588 | switch (loader.getId()) { |
| 589 | case LOADER_ID_FOLDER_CONTENTS: |
| 590 | mAdapter.changeCursor(null); |
| 591 | break; |
| 592 | } |
| 593 | } |
| 594 | |
Leon Scroggins | 0208194 | 2010-11-01 17:52:42 -0400 | [diff] [blame] | 595 | /** |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 596 | * Move cursor to the position that has folderToFind as its "_id". |
| 597 | * @param cursor Cursor containing folders in the bookmarks database |
| 598 | * @param folderToFind "_id" of the folder to move to. |
| 599 | * @param idIndex Index in cursor of "_id" |
| 600 | * @throws AssertionError if cursor is empty or there is no row with folderToFind |
| 601 | * as its "_id". |
| 602 | */ |
| 603 | void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex) |
| 604 | throws AssertionError { |
| 605 | if (!cursor.moveToFirst()) { |
| 606 | throw new AssertionError("No folders in the database!"); |
| 607 | } |
| 608 | long folder; |
| 609 | do { |
| 610 | folder = cursor.getLong(idIndex); |
| 611 | } while (folder != folderToFind && cursor.moveToNext()); |
| 612 | if (cursor.isAfterLast()) { |
| 613 | throw new AssertionError("Folder(id=" + folderToFind |
| 614 | + ") holding this bookmark does not exist!"); |
| 615 | } |
| 616 | } |
| 617 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 618 | @Override |
| 619 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 620 | long id) { |
Leon Scroggins | d14cb12 | 2010-09-29 16:01:48 -0400 | [diff] [blame] | 621 | TextView tv = (TextView) view.findViewById(android.R.id.text1); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 622 | // Switch to the folder that was clicked on. |
Leon Scroggins | d14cb12 | 2010-09-29 16:01:48 -0400 | [diff] [blame] | 623 | descendInto(tv.getText().toString(), id); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 624 | } |
| 625 | |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 626 | private void setShowFolderNamer(boolean show) { |
| 627 | if (show != mIsFolderNamerShowing) { |
| 628 | mIsFolderNamerShowing = show; |
| 629 | if (show) { |
| 630 | // Set the selection to the folder namer so it will be in |
| 631 | // view. |
| 632 | mListView.addFooterView(mFolderNamerHolder); |
| 633 | } else { |
| 634 | mListView.removeFooterView(mFolderNamerHolder); |
| 635 | } |
| 636 | // Refresh the list. |
| 637 | mListView.setAdapter(mAdapter); |
| 638 | if (show) { |
| 639 | mListView.setSelection(mListView.getCount() - 1); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 644 | /** |
| 645 | * Shows a list of names of folders. |
| 646 | */ |
| 647 | private class FolderAdapter extends CursorAdapter { |
| 648 | public FolderAdapter(Context context) { |
| 649 | super(context, null); |
| 650 | } |
| 651 | |
| 652 | @Override |
| 653 | public void bindView(View view, Context context, Cursor cursor) { |
| 654 | ((TextView) view.findViewById(android.R.id.text1)).setText( |
| 655 | cursor.getString(cursor.getColumnIndexOrThrow( |
| 656 | BrowserContract.Bookmarks.TITLE))); |
| 657 | } |
| 658 | |
| 659 | @Override |
| 660 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 661 | View view = LayoutInflater.from(context).inflate( |
| 662 | R.layout.folder_list_item, null); |
| 663 | view.setBackgroundDrawable(context.getResources(). |
| 664 | getDrawable(android.R.drawable.list_selector_background)); |
| 665 | return view; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 666 | } |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 667 | |
| 668 | @Override |
| 669 | public boolean isEmpty() { |
| 670 | // 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] | 671 | return super.isEmpty() && !mIsFolderNamerShowing; |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 672 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 673 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 674 | |
Leon Scroggins | c112990 | 2010-12-08 15:28:33 -0500 | [diff] [blame] | 675 | @Override |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 676 | protected void onCreate(Bundle icicle) { |
| 677 | super.onCreate(icicle); |
Leon Scroggins | 7453ff2 | 2010-12-15 16:03:59 -0500 | [diff] [blame] | 678 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 679 | |
| 680 | mMap = getIntent().getExtras(); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 681 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 682 | setContentView(R.layout.browser_add_bookmark); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 683 | |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 684 | Window window = getWindow(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 685 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 686 | String title = null; |
| 687 | String url = null; |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 688 | |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 689 | mFakeTitle = (TextView) findViewById(R.id.fake_title); |
| 690 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 691 | if (mMap != null) { |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 692 | Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 693 | if (b != null) { |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 694 | mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 695 | mMap = b; |
| 696 | mEditingExisting = true; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 697 | mFakeTitle.setText(R.string.edit_bookmark); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 698 | if (mEditingFolder) { |
| 699 | findViewById(R.id.row_address).setVisibility(View.GONE); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 700 | } else { |
| 701 | showRemoveButton(); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 702 | } |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 703 | } else { |
| 704 | int gravity = mMap.getInt("gravity", -1); |
| 705 | if (gravity != -1) { |
| 706 | WindowManager.LayoutParams l = window.getAttributes(); |
| 707 | l.gravity = gravity; |
| 708 | window.setAttributes(l); |
| 709 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 710 | } |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 711 | title = mMap.getString(BrowserContract.Bookmarks.TITLE); |
| 712 | url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL); |
| 713 | mTouchIconUrl = mMap.getString(TOUCH_ICON_URL); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 714 | mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID); |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 715 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 716 | |
| 717 | mTitle = (EditText) findViewById(R.id.title); |
| 718 | mTitle.setText(title); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 719 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 720 | mAddress = (EditText) findViewById(R.id.address); |
| 721 | mAddress.setText(url); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 722 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 723 | mButton = (TextView) findViewById(R.id.OK); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 724 | mButton.setOnClickListener(this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 725 | |
| 726 | mCancelButton = findViewById(R.id.cancel); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 727 | mCancelButton.setOnClickListener(this); |
| 728 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 729 | mFolder = (FolderSpinner) findViewById(R.id.folder); |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 730 | mFolderAdapter = new FolderSpinnerAdapter(!mEditingFolder); |
| 731 | mFolder.setAdapter(mFolderAdapter); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 732 | mFolder.setOnSetSelectionListener(this); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 733 | |
| 734 | mDefaultView = findViewById(R.id.default_view); |
| 735 | mFolderSelector = findViewById(R.id.folder_selector); |
| 736 | |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 737 | mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null); |
| 738 | mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 739 | mFolderNamer.setOnEditorActionListener(this); |
Leon Scroggins | 0e3a7b8 | 2011-01-11 19:08:03 -0500 | [diff] [blame] | 740 | mFolderCancel = mFolderNamerHolder.findViewById(R.id.close); |
| 741 | mFolderCancel.setOnClickListener(this); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 742 | |
| 743 | mAddNewFolder = findViewById(R.id.add_new_folder); |
| 744 | mAddNewFolder.setOnClickListener(this); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 745 | mAddSeparator = findViewById(R.id.add_divider); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 746 | |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 747 | mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs); |
| 748 | mCrumbs.setUseBackButton(true); |
| 749 | mCrumbs.setController(this); |
Michael Kolb | 5a72f18 | 2011-01-13 20:35:06 -0800 | [diff] [blame] | 750 | mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_holo_dark); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 751 | mCrumbHolder = findViewById(R.id.crumb_holder); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 752 | mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 753 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 754 | mAdapter = new FolderAdapter(this); |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 755 | mListView = (CustomListView) findViewById(R.id.list); |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 756 | View empty = findViewById(R.id.empty); |
| 757 | mListView.setEmptyView(empty); |
| 758 | mListView.setAdapter(mAdapter); |
| 759 | mListView.setOnItemClickListener(this); |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 760 | mListView.addEditText(mFolderNamer); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 761 | |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 762 | mFakeTitleHolder = findViewById(R.id.title_holder); |
| 763 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 764 | if (!window.getDecorView().isInTouchMode()) { |
| 765 | mButton.requestFocus(); |
| 766 | } |
| 767 | |
| 768 | String[] accountInfo = getAccountNameAndType(this); |
| 769 | if (accountInfo == null) { |
| 770 | onRootFolderFound(BrowserProvider2.FIXED_ID_ROOT); |
| 771 | } else { |
| 772 | Bundle args = new Bundle(); |
| 773 | args.putString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, accountInfo[0]); |
| 774 | args.putString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, accountInfo[1]); |
| 775 | getLoaderManager().initLoader(LOADER_ID_FIND_ROOT, args, this); |
| 776 | } |
| 777 | |
| 778 | } |
| 779 | |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 780 | private void showRemoveButton() { |
| 781 | findViewById(R.id.remove_divider).setVisibility(View.VISIBLE); |
| 782 | mRemoveLink = findViewById(R.id.remove); |
| 783 | mRemoveLink.setVisibility(View.VISIBLE); |
| 784 | mRemoveLink.setOnClickListener(this); |
| 785 | } |
| 786 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 787 | // Called once we have determined which folder is the root folder |
| 788 | private void onRootFolderFound(long root) { |
| 789 | mRootFolder = root; |
| 790 | if (mCurrentFolder == DEFAULT_FOLDER_ID) { |
| 791 | mCurrentFolder = mRootFolder; |
| 792 | } |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 793 | setupTopCrumb(); |
Leon Scroggins | bdff8a7 | 2011-02-11 15:49:04 -0500 | [diff] [blame] | 794 | if (mEditingExisting || TextUtils.isEmpty(mOriginalUrl) |
| 795 | || !(mMap != null && mMap.getBoolean(CHECK_FOR_DUPE))) { |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 796 | onCurrentFolderFound(); |
| 797 | } else { |
| 798 | // User is attempting to bookmark a site, rather than deliberately |
| 799 | // editing a bookmark. Rather than let them create a duplicate |
| 800 | // bookmark, see if the bookmark already exists. |
| 801 | getLoaderManager().initLoader(LOADER_ID_CHECK_FOR_DUPE, null, this); |
| 802 | } |
| 803 | } |
| 804 | |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 805 | private void setupTopCrumb() { |
| 806 | String name = getString(R.string.bookmarks); |
| 807 | mTopLevelLabel = (TextView) mCrumbs.pushView(name, false, |
| 808 | new Folder(name, mRootFolder)); |
| 809 | // To better match the other folders. |
| 810 | mTopLevelLabel.setCompoundDrawablePadding(6); |
| 811 | } |
| 812 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 813 | private void onCurrentFolderFound() { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 814 | LoaderManager manager = getLoaderManager(); |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 815 | if (mCurrentFolder != mRootFolder) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 816 | // Find all the folders |
| 817 | manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this); |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 818 | // Since we're not in the root folder, change the selection to other |
| 819 | // folder now. The text will get changed once we select the correct |
| 820 | // folder. |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 821 | mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2); |
Leon Scroggins | 905250c | 2010-12-17 15:25:33 -0500 | [diff] [blame] | 822 | } else { |
| 823 | setShowBookmarkIcon(true); |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 824 | if (!mEditingExisting) { |
| 825 | // Find the most recently saved bookmark, so that we can include it in |
| 826 | // the list of options to save the current bookmark. |
| 827 | manager.initLoader(LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK, null, this); |
| 828 | } |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 829 | if (!mEditingFolder) { |
| 830 | // Initially the "Bookmarks" folder should be showing, rather than |
| 831 | // the home screen. In the editing folder case, home screen is not |
| 832 | // an option, so "Bookmarks" folder is already at the top. |
| 833 | mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER); |
| 834 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 835 | } |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 836 | // Find the contents of the current folder |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 837 | manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 838 | } |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 839 | /** |
| 840 | * Get the account name and type of the currently synced account. |
| 841 | * @param context Context to access preferences. |
| 842 | * @return null if no account name or type. Otherwise, the result will be |
| 843 | * an array of two Strings, the accountName and accountType, respectively. |
| 844 | */ |
| 845 | private String[] getAccountNameAndType(Context context) { |
| 846 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); |
| 847 | String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); |
| 848 | String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); |
| 849 | if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) { |
| 850 | return null; |
| 851 | } |
| 852 | return new String[] { accountName, accountType }; |
| 853 | } |
| 854 | |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 855 | /** |
| 856 | * Runnable to save a bookmark, so it can be performed in its own thread. |
| 857 | */ |
| 858 | private class SaveBookmarkRunnable implements Runnable { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 859 | // FIXME: This should be an async task. |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 860 | private Message mMessage; |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 861 | private Context mContext; |
| 862 | public SaveBookmarkRunnable(Context ctx, Message msg) { |
| 863 | mContext = ctx; |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 864 | mMessage = msg; |
| 865 | } |
| 866 | public void run() { |
| 867 | // Unbundle bookmark data. |
| 868 | Bundle bundle = mMessage.getData(); |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 869 | String title = bundle.getString(BrowserContract.Bookmarks.TITLE); |
| 870 | String url = bundle.getString(BrowserContract.Bookmarks.URL); |
| 871 | boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 872 | Bitmap thumbnail = invalidateThumbnail ? null |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 873 | : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL); |
| 874 | String touchIconUrl = bundle.getString(TOUCH_ICON_URL); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 875 | |
| 876 | // Save to the bookmarks DB. |
| 877 | try { |
| 878 | final ContentResolver cr = getContentResolver(); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 879 | Bookmarks.addBookmark(AddBookmarkPage.this, false, url, |
| 880 | title, thumbnail, true, mCurrentFolder); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 881 | if (touchIconUrl != null) { |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 882 | new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 883 | } |
| 884 | mMessage.arg1 = 1; |
| 885 | } catch (IllegalStateException e) { |
| 886 | mMessage.arg1 = 0; |
| 887 | } |
| 888 | mMessage.sendToTarget(); |
| 889 | } |
| 890 | } |
| 891 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 892 | private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> { |
| 893 | Context mContext; |
| 894 | Long mId; |
| 895 | |
| 896 | public UpdateBookmarkTask(Context context, long id) { |
| 897 | mContext = context; |
| 898 | mId = id; |
| 899 | } |
| 900 | |
| 901 | @Override |
| 902 | protected Void doInBackground(ContentValues... params) { |
| 903 | if (params.length != 1) { |
| 904 | throw new IllegalArgumentException("No ContentValues provided!"); |
| 905 | } |
| 906 | Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId); |
| 907 | mContext.getContentResolver().update( |
| 908 | uri, |
| 909 | params[0], null, null); |
| 910 | return null; |
| 911 | } |
| 912 | } |
| 913 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 914 | private void createHandler() { |
| 915 | if (mHandler == null) { |
| 916 | mHandler = new Handler() { |
| 917 | @Override |
| 918 | public void handleMessage(Message msg) { |
| 919 | switch (msg.what) { |
| 920 | case SAVE_BOOKMARK: |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 921 | if (1 == msg.arg1) { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 922 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved, |
| 923 | Toast.LENGTH_LONG).show(); |
| 924 | } else { |
| 925 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved, |
| 926 | Toast.LENGTH_LONG).show(); |
| 927 | } |
| 928 | break; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 929 | case TOUCH_ICON_DOWNLOADED: |
| 930 | Bundle b = msg.getData(); |
| 931 | sendBroadcast(BookmarkUtils.createAddToHomeIntent( |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 932 | AddBookmarkPage.this, |
| 933 | b.getString(BrowserContract.Bookmarks.URL), |
| 934 | b.getString(BrowserContract.Bookmarks.TITLE), |
| 935 | (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON), |
| 936 | (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON))); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 937 | break; |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 938 | case BOOKMARK_DELETED: |
| 939 | finish(); |
| 940 | break; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | }; |
| 944 | } |
| 945 | } |
| 946 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 947 | /** |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 948 | * 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] | 949 | */ |
| 950 | boolean save() { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 951 | createHandler(); |
| 952 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 953 | String title = mTitle.getText().toString().trim(); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 954 | String unfilteredUrl; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 955 | unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString()); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 956 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 957 | boolean emptyTitle = title.length() == 0; |
| 958 | boolean emptyUrl = unfilteredUrl.trim().length() == 0; |
| 959 | Resources r = getResources(); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 960 | if (emptyTitle || (emptyUrl && !mEditingFolder)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 961 | if (emptyTitle) { |
| 962 | mTitle.setError(r.getText(R.string.bookmark_needs_title)); |
| 963 | } |
| 964 | if (emptyUrl) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 965 | mAddress.setError(r.getText(R.string.bookmark_needs_url)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 966 | } |
Leon Scroggins III | 6e3faea | 2010-09-07 15:22:14 -0400 | [diff] [blame] | 967 | return false; |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 968 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 969 | } |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 970 | String url = unfilteredUrl.trim(); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 971 | if (!mEditingFolder) { |
| 972 | try { |
| 973 | // We allow bookmarks with a javascript: scheme, but these will in most cases |
| 974 | // 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] | 975 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 976 | if (!url.toLowerCase().startsWith("javascript:")) { |
| 977 | URI uriObj = new URI(url); |
| 978 | String scheme = uriObj.getScheme(); |
| 979 | if (!Bookmarks.urlHasAcceptableScheme(url)) { |
| 980 | // If the scheme was non-null, let the user know that we |
| 981 | // can't save their bookmark. If it was null, we'll assume |
| 982 | // they meant http when we parse it in the WebAddress class. |
| 983 | if (scheme != null) { |
| 984 | mAddress.setError(r.getText(R.string.bookmark_cannot_save_url)); |
| 985 | return false; |
| 986 | } |
| 987 | WebAddress address; |
| 988 | try { |
| 989 | address = new WebAddress(unfilteredUrl); |
| 990 | } catch (ParseException e) { |
| 991 | throw new URISyntaxException("", ""); |
| 992 | } |
| 993 | if (address.getHost().length() == 0) { |
| 994 | throw new URISyntaxException("", ""); |
| 995 | } |
| 996 | url = address.toString(); |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 997 | } |
Ben Murdoch | de35362 | 2009-10-12 10:29:00 +0100 | [diff] [blame] | 998 | } |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 999 | } catch (URISyntaxException e) { |
| 1000 | mAddress.setError(r.getText(R.string.bookmark_url_not_valid)); |
| 1001 | return false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1002 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1003 | } |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 1004 | |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1005 | if (mSaveToHomeScreen) { |
| 1006 | mEditingExisting = false; |
| 1007 | } |
| 1008 | |
| 1009 | boolean urlUnmodified = url.equals(mOriginalUrl); |
| 1010 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 1011 | if (mEditingExisting) { |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 1012 | Long id = mMap.getLong(BrowserContract.Bookmarks._ID); |
| 1013 | ContentValues values = new ContentValues(); |
| 1014 | values.put(BrowserContract.Bookmarks.TITLE, title); |
| 1015 | values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder); |
| 1016 | if (!mEditingFolder) { |
| 1017 | values.put(BrowserContract.Bookmarks.URL, url); |
| 1018 | if (!urlUnmodified) { |
| 1019 | values.putNull(BrowserContract.Bookmarks.THUMBNAIL); |
| 1020 | } |
| 1021 | } |
| 1022 | if (values.size() > 0) { |
| 1023 | new UpdateBookmarkTask(getApplicationContext(), id).execute(values); |
| 1024 | } |
| 1025 | setResult(RESULT_OK); |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 1026 | } else { |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1027 | Bitmap thumbnail; |
| 1028 | Bitmap favicon; |
| 1029 | if (urlUnmodified) { |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 1030 | thumbnail = (Bitmap) mMap.getParcelable( |
| 1031 | BrowserContract.Bookmarks.THUMBNAIL); |
| 1032 | favicon = (Bitmap) mMap.getParcelable( |
| 1033 | BrowserContract.Bookmarks.FAVICON); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1034 | } else { |
| 1035 | thumbnail = null; |
| 1036 | favicon = null; |
| 1037 | } |
| 1038 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 1039 | Bundle bundle = new Bundle(); |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 1040 | bundle.putString(BrowserContract.Bookmarks.TITLE, title); |
| 1041 | bundle.putString(BrowserContract.Bookmarks.URL, url); |
| 1042 | bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1043 | |
| 1044 | if (mSaveToHomeScreen) { |
| 1045 | if (mTouchIconUrl != null && urlUnmodified) { |
| 1046 | Message msg = Message.obtain(mHandler, |
| 1047 | TOUCH_ICON_DOWNLOADED); |
| 1048 | msg.setData(bundle); |
| 1049 | DownloadTouchIcon icon = new DownloadTouchIcon(this, msg, |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 1050 | mMap.getString(USER_AGENT)); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1051 | icon.execute(mTouchIconUrl); |
| 1052 | } else { |
| 1053 | sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url, |
| 1054 | title, null /*touchIcon*/, favicon)); |
| 1055 | } |
| 1056 | } else { |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 1057 | bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail); |
| 1058 | bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified); |
| 1059 | bundle.putString(TOUCH_ICON_URL, mTouchIconUrl); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1060 | // Post a message to write to the DB. |
| 1061 | Message msg = Message.obtain(mHandler, SAVE_BOOKMARK); |
| 1062 | msg.setData(bundle); |
| 1063 | // Start a new thread so as to not slow down the UI |
| 1064 | Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg)); |
| 1065 | t.start(); |
| 1066 | } |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 1067 | setResult(RESULT_OK); |
Kristian Monsen | 0a1d838 | 2010-02-01 18:41:07 +0000 | [diff] [blame] | 1068 | LogTag.logBookmarkAdded(url, "bookmarkview"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1069 | } |
| 1070 | return true; |
| 1071 | } |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 1072 | |
| 1073 | /* |
| 1074 | * Class used as a proxy for the InputMethodManager to get to mFolderNamer |
| 1075 | */ |
| 1076 | public static class CustomListView extends ListView { |
| 1077 | private EditText mEditText; |
| 1078 | |
| 1079 | public void addEditText(EditText editText) { |
| 1080 | mEditText = editText; |
| 1081 | } |
| 1082 | |
| 1083 | public CustomListView(Context context) { |
| 1084 | super(context); |
| 1085 | } |
| 1086 | |
| 1087 | public CustomListView(Context context, AttributeSet attrs) { |
| 1088 | super(context, attrs); |
| 1089 | } |
| 1090 | |
| 1091 | public CustomListView(Context context, AttributeSet attrs, int defStyle) { |
| 1092 | super(context, attrs, defStyle); |
| 1093 | } |
| 1094 | |
| 1095 | @Override |
| 1096 | public boolean checkInputConnectionProxy(View view) { |
| 1097 | return view == mEditText; |
| 1098 | } |
| 1099 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1100 | } |