blob: ae5ae695026563f8f1fefdb5269b81a170aaea93 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
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
17package com.android.browser;
18
Leon Scroggins504433a2011-01-13 12:26:52 -050019import com.android.browser.addbookmark.FolderSpinner;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -050020import com.android.browser.addbookmark.FolderSpinnerAdapter;
Leon Scroggins25230d72010-09-28 20:09:25 -040021
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.app.Activity;
Leon Scroggins III052ce662010-09-13 14:44:16 -040023import android.app.LoaderManager;
John Reck2eec4c32011-05-11 15:55:32 -070024import android.app.LoaderManager.LoaderCallbacks;
25import android.content.AsyncTaskLoader;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import android.content.ContentResolver;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070027import android.content.ContentUris;
Leon Scroggins III052ce662010-09-13 14:44:16 -040028import android.content.ContentValues;
29import android.content.Context;
30import android.content.CursorLoader;
Leon Scroggins III052ce662010-09-13 14:44:16 -040031import android.content.Loader;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040033import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010034import android.graphics.Bitmap;
Leon Scroggins02081942010-11-01 17:52:42 -040035import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.net.ParseException;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070037import android.net.Uri;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.net.WebAddress;
John Reckc8490812010-11-22 14:15:36 -080039import android.os.AsyncTask;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010041import android.os.Handler;
42import android.os.Message;
Leon Scroggins III052ce662010-09-13 14:44:16 -040043import android.provider.BrowserContract;
John Reck2eec4c32011-05-11 15:55:32 -070044import android.provider.BrowserContract.Accounts;
Leon Scroggins25230d72010-09-28 20:09:25 -040045import android.text.TextUtils;
Leon Scroggins162f8352010-10-18 15:02:44 -040046import android.util.AttributeSet;
Leon Scroggins III052ce662010-09-13 14:44:16 -040047import android.view.KeyEvent;
48import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040050import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040052import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040053import android.view.inputmethod.EditorInfo;
54import android.view.inputmethod.InputMethodManager;
55import android.widget.AdapterView;
John Reck2eec4c32011-05-11 15:55:32 -070056import android.widget.AdapterView.OnItemSelectedListener;
57import android.widget.ArrayAdapter;
Leon Scroggins III052ce662010-09-13 14:44:16 -040058import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080059import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040060import android.widget.ListView;
John Reck2eec4c32011-05-11 15:55:32 -070061import android.widget.Spinner;
The Android Open Source Project0c908882009-03-03 19:32:16 -080062import android.widget.TextView;
63import android.widget.Toast;
64
Cary Clarkf2407c62009-09-04 12:25:10 -040065import java.net.URI;
66import java.net.URISyntaxException;
Leon Scroggins III052ce662010-09-13 14:44:16 -040067
68public class AddBookmarkPage extends Activity
69 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040070 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
John Reck2eec4c32011-05-11 15:55:32 -070071 BreadCrumbView.Controller, FolderSpinner.OnSetSelectionListener,
72 OnItemSelectedListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080073
Michael Kolb370a4f32010-10-06 10:45:32 -070074 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040075 public static final String TOUCH_ICON_URL = "touch_icon_url";
76 // Place on an edited bookmark to remove the saved thumbnail
77 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
78 public static final String USER_AGENT = "user_agent";
Leon Scrogginsbdff8a72011-02-11 15:49:04 -050079 public static final String CHECK_FOR_DUPE = "check_for_dupe";
Michael Kolb370a4f32010-10-06 10:45:32 -070080
John Reckc8490812010-11-22 14:15:36 -080081 /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark";
82 /* package */ static final String EXTRA_IS_FOLDER = "is_folder";
83
Leon Scroggins74dbe012010-10-15 10:54:27 -040084 private static final int MAX_CRUMBS_SHOWN = 2;
85
The Android Open Source Project0c908882009-03-03 19:32:16 -080086 private final String LOGTAG = "Bookmarks";
87
Leon Scroggins III052ce662010-09-13 14:44:16 -040088 // IDs for the CursorLoaders that are used.
John Reck2eec4c32011-05-11 15:55:32 -070089 private final int LOADER_ID_ACCOUNTS = 0;
90 private final int LOADER_ID_FOLDER_CONTENTS = 1;
91 private final int LOADER_ID_EDIT_INFO = 2;
Leon Scroggins III052ce662010-09-13 14:44:16 -040092
The Android Open Source Project0c908882009-03-03 19:32:16 -080093 private EditText mTitle;
94 private EditText mAddress;
95 private TextView mButton;
96 private View mCancelButton;
97 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -080098 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -080099 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -0400100 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +0100101 private String mOriginalUrl;
Leon Scroggins504433a2011-01-13 12:26:52 -0500102 private FolderSpinner mFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400103 private View mDefaultView;
104 private View mFolderSelector;
105 private EditText mFolderNamer;
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500106 private View mFolderCancel;
Leon Scroggins162f8352010-10-18 15:02:44 -0400107 private boolean mIsFolderNamerShowing;
108 private View mFolderNamerHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400109 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400110 private View mAddSeparator;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500111 private long mCurrentFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400112 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400113 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400114 private TextView mFakeTitle;
115 private View mCrumbHolder;
Leon Scroggins162f8352010-10-18 15:02:44 -0400116 private CustomListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400117 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400118 private long mRootFolder;
Leon Scroggins905250c2010-12-17 15:25:33 -0500119 private TextView mTopLevelLabel;
120 private Drawable mHeaderIcon;
Leon Scroggins75630672011-01-13 17:56:15 -0500121 private View mRemoveLink;
122 private View mFakeTitleHolder;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500123 private FolderSpinnerAdapter mFolderAdapter;
John Reck2eec4c32011-05-11 15:55:32 -0700124 private Spinner mAccountSpinner;
125 private ArrayAdapter<BookmarkAccount> mAccountAdapter;
126
Leon Scroggins III052ce662010-09-13 14:44:16 -0400127 private static class Folder {
128 String Name;
129 long Id;
130 Folder(String name, long id) {
131 Name = name;
132 Id = id;
133 }
134 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800135
Ben Murdoch1794fe22009-09-29 18:14:30 +0100136 // Message IDs
137 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400138 private static final int TOUCH_ICON_DOWNLOADED = 101;
Leon Scroggins75630672011-01-13 17:56:15 -0500139 private static final int BOOKMARK_DELETED = 102;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100140
141 private Handler mHandler;
142
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100143 private InputMethodManager getInputMethodManager() {
144 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
145 }
146
Leon Scroggins8baaa632010-12-08 19:46:53 -0500147 private Uri getUriForFolder(long folder) {
John Reck2eec4c32011-05-11 15:55:32 -0700148 return BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500149 }
150
Leon Scroggins III052ce662010-09-13 14:44:16 -0400151 @Override
John Reck71efc2b2011-05-09 16:54:28 -0700152 public void onTop(BreadCrumbView view, int level, Object data) {
Leon Scroggins74dbe012010-10-15 10:54:27 -0400153 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400154 Folder folderData = (Folder) data;
155 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400156 LoaderManager manager = getLoaderManager();
John Reck2eec4c32011-05-11 15:55:32 -0700157 CursorLoader loader = (CursorLoader) ((Loader<?>) manager.getLoader(
Leon Scroggins74dbe012010-10-15 10:54:27 -0400158 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500159 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400160 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400161 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400162 completeOrCancelFolderNaming(true);
163 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500164 setShowBookmarkIcon(level == 1);
165 }
166
167 /**
168 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
169 * @param show True if the icon should visible, false otherwise.
170 */
171 private void setShowBookmarkIcon(boolean show) {
172 Drawable drawable = show ? mHeaderIcon: null;
173 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400174 }
175
Leon Scroggins74dbe012010-10-15 10:54:27 -0400176 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400177 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
178 if (v == mFolderNamer) {
179 if (v.getText().length() > 0) {
180 if (actionId == EditorInfo.IME_NULL) {
181 // Only want to do this once.
182 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400183 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400184 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400185 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 }
Michael Kolb31829b92010-10-01 11:50:21 -0700187 // Steal the key press; otherwise a newline will be added
188 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400190 return false;
191 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400193 private void switchToDefaultView(boolean changedFolder) {
194 mFolderSelector.setVisibility(View.GONE);
195 mDefaultView.setVisibility(View.VISIBLE);
196 mCrumbHolder.setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500197 mFakeTitleHolder.setVisibility(View.VISIBLE);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400198 if (changedFolder) {
199 Object data = mCrumbs.getTopData();
200 if (data != null) {
201 Folder folder = (Folder) data;
202 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500203 if (mCurrentFolder == mRootFolder) {
204 // The Spinner changed to show "Other folder ..." Change
205 // it back to "Bookmarks", which is position 0 if we are
206 // editing a folder, 1 otherwise.
Leon Scroggins504433a2011-01-13 12:26:52 -0500207 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500208 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700209 mFolderAdapter.setOtherFolderDisplayText(folder.Name);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500210 }
211 }
212 } else {
213 // The user canceled selecting a folder. Revert back to the earlier
214 // selection.
215 if (mSaveToHomeScreen) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500216 mFolder.setSelectionIgnoringSelectionChange(0);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500217 } else {
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500218 if (mCurrentFolder == mRootFolder) {
219 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
220 } else {
221 Object data = mCrumbs.getTopData();
222 if (data != null && ((Folder) data).Id == mCurrentFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700223 // We are showing the correct folder hierarchy. The
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500224 // folder selector will say "Other folder..." Change it
225 // to say the name of the folder once again.
John Reck2eec4c32011-05-11 15:55:32 -0700226 mFolderAdapter.setOtherFolderDisplayText(((Folder) data).Name);
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500227 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700228 // We are not showing the correct folder hierarchy.
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500229 // Clear the Crumbs and find the proper folder
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500230 setupTopCrumb();
231 LoaderManager manager = getLoaderManager();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500232 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
233
234 }
235 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400236 }
237 }
238 }
239
Leon Scroggins III052ce662010-09-13 14:44:16 -0400240 @Override
241 public void onClick(View v) {
242 if (v == mButton) {
243 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400244 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400245 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400246 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700247 } else {
248 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400249 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400250 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700251 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400252 } else if (save()) {
253 finish();
254 }
255 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400256 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400257 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400258 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
259 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700260 } else {
261 finish();
262 }
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500263 } else if (v == mFolderCancel) {
264 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400265 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400266 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400267 mFolderNamer.setText(R.string.new_folder);
268 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700269 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400270 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400271 InputMethodManager imm = getInputMethodManager();
272 // Set the InputMethodManager to focus on the ListView so that it
273 // can transfer the focus to mFolderNamer.
274 imm.focusIn(mListView);
275 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
Leon Scroggins75630672011-01-13 17:56:15 -0500276 } else if (v == mRemoveLink) {
277 if (!mEditingExisting) {
278 throw new AssertionError("Remove button should not be shown for"
279 + " new bookmarks");
280 }
281 long id = mMap.getLong(BrowserContract.Bookmarks._ID);
282 createHandler();
283 Message msg = Message.obtain(mHandler, BOOKMARK_DELETED);
284 BookmarkUtils.displayRemoveBookmarkDialog(id,
285 mTitle.getText().toString(), this, msg);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800286 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400287 }
288
Leon Scroggins504433a2011-01-13 12:26:52 -0500289 // FolderSpinner.OnSetSelectionListener
290
Leon Scroggins88d08032010-10-21 15:17:10 -0400291 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500292 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500293 int intId = (int) id;
294 switch (intId) {
295 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400296 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400297 mSaveToHomeScreen = false;
298 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500299 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400300 // Create a short cut to the home screen
301 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400302 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500303 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400304 switchToFolderSelector();
305 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500306 case FolderSpinnerAdapter.RECENT_FOLDER:
307 mCurrentFolder = mFolderAdapter.recentFolderId();
308 mSaveToHomeScreen = false;
309 // In case the user decides to select OTHER_FOLDER
310 // and choose a different one, so that we will start from
311 // the correct place.
312 LoaderManager manager = getLoaderManager();
Leon Scroggins2f24e992011-02-11 14:02:10 -0500313 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins0b95ad42011-02-23 15:23:48 -0500314 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400315 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500316 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400317 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500318 }
319
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500320 /**
321 * Finish naming a folder, and close the IME
322 * @param cancel If true, the new folder is not created. If false, the new
323 * folder is created and the user is taken inside it.
324 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400325 private void completeOrCancelFolderNaming(boolean cancel) {
326 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700327 String name = mFolderNamer.getText().toString();
328 long id = addFolderToCurrent(mFolderNamer.getText().toString());
329 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700330 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400331 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400332 mAddNewFolder.setVisibility(View.VISIBLE);
333 mAddSeparator.setVisibility(View.VISIBLE);
334 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400335 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700336 }
337
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700338 private long addFolderToCurrent(String name) {
339 // Add the folder to the database
340 ContentValues values = new ContentValues();
341 values.put(BrowserContract.Bookmarks.TITLE,
342 name);
343 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400344 long currentFolder;
345 Object data = mCrumbs.getTopData();
346 if (data != null) {
347 currentFolder = ((Folder) data).Id;
348 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400349 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400350 }
351 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700352 Uri uri = getContentResolver().insert(
353 BrowserContract.Bookmarks.CONTENT_URI, values);
354 if (uri != null) {
355 return ContentUris.parseId(uri);
356 } else {
357 return -1;
358 }
359 }
360
Leon Scroggins III052ce662010-09-13 14:44:16 -0400361 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500362 // Set the list to the top in case it is scrolled.
363 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400364 mDefaultView.setVisibility(View.GONE);
365 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400366 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500367 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400368 mAddNewFolder.setVisibility(View.VISIBLE);
369 mAddSeparator.setVisibility(View.VISIBLE);
John Reck95f88e42011-09-26 09:25:43 -0700370 getInputMethodManager().hideSoftInputFromWindow(
371 mListView.getWindowToken(), 0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400372 }
373
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700374 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700375 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400376 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400377 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700378 }
379 }
380
John Reck2eec4c32011-05-11 15:55:32 -0700381 private LoaderCallbacks<EditBookmarkInfo> mEditInfoLoaderCallbacks =
382 new LoaderCallbacks<EditBookmarkInfo>() {
383
384 @Override
385 public void onLoaderReset(Loader<EditBookmarkInfo> loader) {
386 // Don't care
387 }
388
389 @Override
390 public void onLoadFinished(Loader<EditBookmarkInfo> loader,
391 EditBookmarkInfo info) {
392 boolean setAccount = false;
393 if (info.id != -1) {
394 mEditingExisting = true;
395 showRemoveButton();
396 mFakeTitle.setText(R.string.edit_bookmark);
397 mTitle.setText(info.title);
398 mFolderAdapter.setOtherFolderDisplayText(info.parentTitle);
399 mMap.putLong(BrowserContract.Bookmarks._ID, info.id);
400 setAccount = true;
401 setAccount(info.accountName, info.accountType);
402 mCurrentFolder = info.parentId;
403 onCurrentFolderFound();
404 }
John Reck37894a92011-05-13 12:47:53 -0700405 // TODO: Detect if lastUsedId is a subfolder of info.id in the
406 // editing folder case. For now, just don't show the last used
407 // folder at all to prevent any chance of the user adding a parent
408 // folder to a child folder
409 if (info.lastUsedId != -1 && info.lastUsedId != info.id
410 && !mEditingFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700411 if (setAccount && info.lastUsedId != mRootFolder
412 && TextUtils.equals(info.lastUsedAccountName, info.accountName)
413 && TextUtils.equals(info.lastUsedAccountType, info.accountType)) {
414 mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle);
415 } else if (!setAccount) {
416 setAccount = true;
417 setAccount(info.lastUsedAccountName, info.lastUsedAccountType);
418 if (info.lastUsedId != mRootFolder) {
419 mFolderAdapter.addRecentFolder(info.lastUsedId,
420 info.lastUsedTitle);
421 }
422 }
423 }
424 if (!setAccount) {
425 mAccountSpinner.setSelection(0);
426 }
427 }
428
429 @Override
430 public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) {
431 return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap);
432 }
433 };
434
435 void setAccount(String accountName, String accountType) {
436 for (int i = 0; i < mAccountAdapter.getCount(); i++) {
437 BookmarkAccount account = mAccountAdapter.getItem(i);
438 if (TextUtils.equals(account.accountName, accountName)
439 && TextUtils.equals(account.accountType, accountType)) {
440 onRootFolderFound(account.rootFolderId);
441 mAccountSpinner.setSelection(i);
442 return;
443 }
444 }
445 }
446
Leon Scroggins III052ce662010-09-13 14:44:16 -0400447 @Override
448 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
449 String[] projection;
450 switch (id) {
John Reck2eec4c32011-05-11 15:55:32 -0700451 case LOADER_ID_ACCOUNTS:
452 return new AccountsLoader(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400453 case LOADER_ID_FOLDER_CONTENTS:
454 projection = new String[] {
455 BrowserContract.Bookmarks._ID,
456 BrowserContract.Bookmarks.TITLE,
457 BrowserContract.Bookmarks.IS_FOLDER
458 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500459 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
John Reck1dd8cd42011-05-13 11:22:09 -0700460 String whereArgs[] = null;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500461 if (mEditingFolder) {
John Reck1dd8cd42011-05-13 11:22:09 -0700462 where += " AND " + BrowserContract.Bookmarks._ID + " != ?";
463 whereArgs = new String[] { Long.toString(mMap.getLong(
464 BrowserContract.Bookmarks._ID)) };
465 }
466 long currentFolder;
467 Object data = mCrumbs.getTopData();
468 if (data != null) {
469 currentFolder = ((Folder) data).Id;
470 } else {
471 currentFolder = mRootFolder;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500472 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400473 return new CursorLoader(this,
John Reck1dd8cd42011-05-13 11:22:09 -0700474 getUriForFolder(currentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400475 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500476 where,
John Reck1dd8cd42011-05-13 11:22:09 -0700477 whereArgs,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500478 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400479 default:
480 throw new AssertionError("Asking for nonexistant loader!");
481 }
482 }
483
484 @Override
485 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
486 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700487 case LOADER_ID_ACCOUNTS:
488 mAccountAdapter.clear();
489 while (cursor.moveToNext()) {
490 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500491 }
John Reck2eec4c32011-05-11 15:55:32 -0700492 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
493 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
494 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500495 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400496 case LOADER_ID_FOLDER_CONTENTS:
497 mAdapter.changeCursor(cursor);
498 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400499 }
500 }
501
Dianne Hackborn39772c82010-12-16 00:43:54 -0800502 public void onLoaderReset(Loader<Cursor> loader) {
503 switch (loader.getId()) {
504 case LOADER_ID_FOLDER_CONTENTS:
505 mAdapter.changeCursor(null);
506 break;
507 }
508 }
509
Leon Scroggins02081942010-11-01 17:52:42 -0400510 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500511 * Move cursor to the position that has folderToFind as its "_id".
512 * @param cursor Cursor containing folders in the bookmarks database
513 * @param folderToFind "_id" of the folder to move to.
514 * @param idIndex Index in cursor of "_id"
515 * @throws AssertionError if cursor is empty or there is no row with folderToFind
516 * as its "_id".
517 */
518 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
519 throws AssertionError {
520 if (!cursor.moveToFirst()) {
521 throw new AssertionError("No folders in the database!");
522 }
523 long folder;
524 do {
525 folder = cursor.getLong(idIndex);
526 } while (folder != folderToFind && cursor.moveToNext());
527 if (cursor.isAfterLast()) {
528 throw new AssertionError("Folder(id=" + folderToFind
529 + ") holding this bookmark does not exist!");
530 }
531 }
532
Leon Scroggins III052ce662010-09-13 14:44:16 -0400533 @Override
534 public void onItemClick(AdapterView<?> parent, View view, int position,
535 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400536 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400537 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400538 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400539 }
540
Leon Scroggins162f8352010-10-18 15:02:44 -0400541 private void setShowFolderNamer(boolean show) {
542 if (show != mIsFolderNamerShowing) {
543 mIsFolderNamerShowing = show;
544 if (show) {
545 // Set the selection to the folder namer so it will be in
546 // view.
547 mListView.addFooterView(mFolderNamerHolder);
548 } else {
549 mListView.removeFooterView(mFolderNamerHolder);
550 }
551 // Refresh the list.
552 mListView.setAdapter(mAdapter);
553 if (show) {
554 mListView.setSelection(mListView.getCount() - 1);
555 }
556 }
557 }
558
Leon Scroggins III052ce662010-09-13 14:44:16 -0400559 /**
560 * Shows a list of names of folders.
561 */
562 private class FolderAdapter extends CursorAdapter {
563 public FolderAdapter(Context context) {
564 super(context, null);
565 }
566
567 @Override
568 public void bindView(View view, Context context, Cursor cursor) {
569 ((TextView) view.findViewById(android.R.id.text1)).setText(
570 cursor.getString(cursor.getColumnIndexOrThrow(
571 BrowserContract.Bookmarks.TITLE)));
572 }
573
574 @Override
575 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700576 View view = LayoutInflater.from(context).inflate(
577 R.layout.folder_list_item, null);
578 view.setBackgroundDrawable(context.getResources().
579 getDrawable(android.R.drawable.list_selector_background));
580 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400581 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400582
583 @Override
584 public boolean isEmpty() {
585 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400586 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400587 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400588 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800589
Leon Scrogginsc1129902010-12-08 15:28:33 -0500590 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800591 protected void onCreate(Bundle icicle) {
592 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500593 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100594
595 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100596
Leon Scroggins III052ce662010-09-13 14:44:16 -0400597 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100598
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400599 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700600
The Android Open Source Project0c908882009-03-03 19:32:16 -0800601 String title = null;
602 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100603
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400604 mFakeTitle = (TextView) findViewById(R.id.fake_title);
605
The Android Open Source Project0c908882009-03-03 19:32:16 -0800606 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800607 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800608 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800609 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800610 mMap = b;
611 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400612 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800613 if (mEditingFolder) {
614 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500615 } else {
616 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800617 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400618 } else {
619 int gravity = mMap.getInt("gravity", -1);
620 if (gravity != -1) {
621 WindowManager.LayoutParams l = window.getAttributes();
622 l.gravity = gravity;
623 window.setAttributes(l);
624 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800625 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400626 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
627 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
628 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700629 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400630 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800631
632 mTitle = (EditText) findViewById(R.id.title);
633 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100634
Leon Scroggins III052ce662010-09-13 14:44:16 -0400635 mAddress = (EditText) findViewById(R.id.address);
636 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800637
The Android Open Source Project0c908882009-03-03 19:32:16 -0800638 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400639 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800640
641 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400642 mCancelButton.setOnClickListener(this);
643
Leon Scroggins504433a2011-01-13 12:26:52 -0500644 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700645 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500646 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500647 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400648
649 mDefaultView = findViewById(R.id.default_view);
650 mFolderSelector = findViewById(R.id.folder_selector);
651
Leon Scroggins162f8352010-10-18 15:02:44 -0400652 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
653 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400654 mFolderNamer.setOnEditorActionListener(this);
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500655 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
656 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400657
658 mAddNewFolder = findViewById(R.id.add_new_folder);
659 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400660 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400661
Leon Scroggins74dbe012010-10-15 10:54:27 -0400662 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
663 mCrumbs.setUseBackButton(true);
664 mCrumbs.setController(this);
Michael Kolb5a72f182011-01-13 20:35:06 -0800665 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400666 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800667 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400668
Leon Scroggins III052ce662010-09-13 14:44:16 -0400669 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400670 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400671 View empty = findViewById(R.id.empty);
672 mListView.setEmptyView(empty);
673 mListView.setAdapter(mAdapter);
674 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400675 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500676
John Reck2eec4c32011-05-11 15:55:32 -0700677 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
678 android.R.layout.simple_spinner_item);
679 mAccountAdapter.setDropDownViewResource(
680 android.R.layout.simple_spinner_dropdown_item);
681 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
682 mAccountSpinner.setAdapter(mAccountAdapter);
683 mAccountSpinner.setOnItemSelectedListener(this);
684
685
Leon Scroggins75630672011-01-13 17:56:15 -0500686 mFakeTitleHolder = findViewById(R.id.title_holder);
687
Leon Scroggins504433a2011-01-13 12:26:52 -0500688 if (!window.getDecorView().isInTouchMode()) {
689 mButton.requestFocus();
690 }
691
John Reck2eec4c32011-05-11 15:55:32 -0700692 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500693 }
694
Leon Scroggins75630672011-01-13 17:56:15 -0500695 private void showRemoveButton() {
696 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
697 mRemoveLink = findViewById(R.id.remove);
698 mRemoveLink.setVisibility(View.VISIBLE);
699 mRemoveLink.setOnClickListener(this);
700 }
701
Leon Scroggins504433a2011-01-13 12:26:52 -0500702 // Called once we have determined which folder is the root folder
703 private void onRootFolderFound(long root) {
704 mRootFolder = root;
John Reck2eec4c32011-05-11 15:55:32 -0700705 mCurrentFolder = mRootFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500706 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700707 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500708 }
709
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500710 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700711 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500712 String name = getString(R.string.bookmarks);
713 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
714 new Folder(name, mRootFolder));
715 // To better match the other folders.
716 mTopLevelLabel.setCompoundDrawablePadding(6);
717 }
718
Leon Scroggins504433a2011-01-13 12:26:52 -0500719 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400720 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500721 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500722 // Since we're not in the root folder, change the selection to other
723 // folder now. The text will get changed once we select the correct
724 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500725 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500726 } else {
727 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500728 if (!mEditingFolder) {
729 // Initially the "Bookmarks" folder should be showing, rather than
730 // the home screen. In the editing folder case, home screen is not
731 // an option, so "Bookmarks" folder is already at the top.
732 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
733 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400734 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400735 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700736 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500737 }
738
Leon Scroggins02065b02010-01-04 14:30:13 -0500739 /**
740 * Runnable to save a bookmark, so it can be performed in its own thread.
741 */
742 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400743 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500744 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200745 private Context mContext;
746 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100747 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500748 mMessage = msg;
749 }
750 public void run() {
751 // Unbundle bookmark data.
752 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400753 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
754 String url = bundle.getString(BrowserContract.Bookmarks.URL);
755 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500756 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400757 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
758 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500759
760 // Save to the bookmarks DB.
761 try {
762 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400763 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700764 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500765 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200766 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500767 }
768 mMessage.arg1 = 1;
769 } catch (IllegalStateException e) {
770 mMessage.arg1 = 0;
771 }
772 mMessage.sendToTarget();
773 }
774 }
775
John Reckc8490812010-11-22 14:15:36 -0800776 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
777 Context mContext;
778 Long mId;
779
780 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100781 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800782 mId = id;
783 }
784
785 @Override
786 protected Void doInBackground(ContentValues... params) {
787 if (params.length != 1) {
788 throw new IllegalArgumentException("No ContentValues provided!");
789 }
790 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
791 mContext.getContentResolver().update(
792 uri,
793 params[0], null, null);
794 return null;
795 }
796 }
797
Ben Murdoch1794fe22009-09-29 18:14:30 +0100798 private void createHandler() {
799 if (mHandler == null) {
800 mHandler = new Handler() {
801 @Override
802 public void handleMessage(Message msg) {
803 switch (msg.what) {
804 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500805 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100806 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
807 Toast.LENGTH_LONG).show();
808 } else {
809 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
810 Toast.LENGTH_LONG).show();
811 }
812 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400813 case TOUCH_ICON_DOWNLOADED:
814 Bundle b = msg.getData();
815 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400816 AddBookmarkPage.this,
817 b.getString(BrowserContract.Bookmarks.URL),
818 b.getString(BrowserContract.Bookmarks.TITLE),
819 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
820 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400821 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500822 case BOOKMARK_DELETED:
823 finish();
824 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100825 }
826 }
827 };
828 }
829 }
830
The Android Open Source Project0c908882009-03-03 19:32:16 -0800831 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100832 * Parse the data entered in the dialog and post a message to update the bookmarks database.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800833 */
834 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100835 createHandler();
836
The Android Open Source Project0c908882009-03-03 19:32:16 -0800837 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100838 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100840
The Android Open Source Project0c908882009-03-03 19:32:16 -0800841 boolean emptyTitle = title.length() == 0;
842 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
843 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800844 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800845 if (emptyTitle) {
846 mTitle.setError(r.getText(R.string.bookmark_needs_title));
847 }
848 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400849 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800850 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400851 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100852
The Android Open Source Project0c908882009-03-03 19:32:16 -0800853 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000854 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800855 if (!mEditingFolder) {
856 try {
857 // We allow bookmarks with a javascript: scheme, but these will in most cases
858 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000859
John Reckc8490812010-11-22 14:15:36 -0800860 if (!url.toLowerCase().startsWith("javascript:")) {
861 URI uriObj = new URI(url);
862 String scheme = uriObj.getScheme();
863 if (!Bookmarks.urlHasAcceptableScheme(url)) {
864 // If the scheme was non-null, let the user know that we
865 // can't save their bookmark. If it was null, we'll assume
866 // they meant http when we parse it in the WebAddress class.
867 if (scheme != null) {
868 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
869 return false;
870 }
871 WebAddress address;
872 try {
873 address = new WebAddress(unfilteredUrl);
874 } catch (ParseException e) {
875 throw new URISyntaxException("", "");
876 }
877 if (address.getHost().length() == 0) {
878 throw new URISyntaxException("", "");
879 }
880 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000881 }
Ben Murdochde353622009-10-12 10:29:00 +0100882 }
John Reckc8490812010-11-22 14:15:36 -0800883 } catch (URISyntaxException e) {
884 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
885 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800886 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800887 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100888
Leon Scroggins88d08032010-10-21 15:17:10 -0400889 if (mSaveToHomeScreen) {
890 mEditingExisting = false;
891 }
892
893 boolean urlUnmodified = url.equals(mOriginalUrl);
894
Ben Murdoch1794fe22009-09-29 18:14:30 +0100895 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800896 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
897 ContentValues values = new ContentValues();
898 values.put(BrowserContract.Bookmarks.TITLE, title);
899 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
900 if (!mEditingFolder) {
901 values.put(BrowserContract.Bookmarks.URL, url);
902 if (!urlUnmodified) {
903 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
904 }
905 }
906 if (values.size() > 0) {
907 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
908 }
909 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100910 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400911 Bitmap thumbnail;
912 Bitmap favicon;
913 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400914 thumbnail = (Bitmap) mMap.getParcelable(
915 BrowserContract.Bookmarks.THUMBNAIL);
916 favicon = (Bitmap) mMap.getParcelable(
917 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400918 } else {
919 thumbnail = null;
920 favicon = null;
921 }
922
Ben Murdoch1794fe22009-09-29 18:14:30 +0100923 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400924 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
925 bundle.putString(BrowserContract.Bookmarks.URL, url);
926 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400927
928 if (mSaveToHomeScreen) {
929 if (mTouchIconUrl != null && urlUnmodified) {
930 Message msg = Message.obtain(mHandler,
931 TOUCH_ICON_DOWNLOADED);
932 msg.setData(bundle);
933 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400934 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400935 icon.execute(mTouchIconUrl);
936 } else {
937 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
938 title, null /*touchIcon*/, favicon));
939 }
940 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400941 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
942 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
943 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400944 // Post a message to write to the DB.
945 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
946 msg.setData(bundle);
947 // Start a new thread so as to not slow down the UI
948 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
949 t.start();
950 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100951 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000952 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800953 }
954 return true;
955 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400956
John Reck2eec4c32011-05-11 15:55:32 -0700957 @Override
958 public void onItemSelected(AdapterView<?> parent, View view, int position,
959 long id) {
960 if (mAccountSpinner == parent) {
961 long root = mAccountAdapter.getItem(position).rootFolderId;
962 if (root != mRootFolder) {
963 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -0700964 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -0700965 }
966 }
967 }
968
969 @Override
970 public void onNothingSelected(AdapterView<?> parent) {
971 // Don't care
972 }
973
Leon Scroggins162f8352010-10-18 15:02:44 -0400974 /*
975 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
976 */
977 public static class CustomListView extends ListView {
978 private EditText mEditText;
979
980 public void addEditText(EditText editText) {
981 mEditText = editText;
982 }
983
984 public CustomListView(Context context) {
985 super(context);
986 }
987
988 public CustomListView(Context context, AttributeSet attrs) {
989 super(context, attrs);
990 }
991
992 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
993 super(context, attrs, defStyle);
994 }
995
996 @Override
997 public boolean checkInputConnectionProxy(View view) {
998 return view == mEditText;
999 }
1000 }
John Reck2eec4c32011-05-11 15:55:32 -07001001
John Reck3bf3cfb2011-10-17 18:00:05 -07001002 static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001003
1004 static final String[] PROJECTION = new String[] {
1005 Accounts.ACCOUNT_NAME,
1006 Accounts.ACCOUNT_TYPE,
1007 Accounts.ROOT_ID,
1008 };
1009
1010 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1011 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1012 static final int COLUMN_INDEX_ROOT_ID = 2;
1013
1014 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001015 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001016 }
1017
1018 }
1019
John Reck9b8cd1e2011-05-25 18:14:01 -07001020 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001021
1022 private String mLabel;
1023 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001024 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001025
1026 public BookmarkAccount(Context context, Cursor cursor) {
1027 accountName = cursor.getString(
1028 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1029 accountType = cursor.getString(
1030 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1031 rootFolderId = cursor.getLong(
1032 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1033 mLabel = accountName;
1034 if (TextUtils.isEmpty(mLabel)) {
1035 mLabel = context.getString(R.string.local_bookmarks);
1036 }
1037 }
1038
1039 @Override
1040 public String toString() {
1041 return mLabel;
1042 }
1043 }
1044
1045 static class EditBookmarkInfo {
1046 long id = -1;
1047 long parentId = -1;
1048 String parentTitle;
1049 String title;
1050 String accountName;
1051 String accountType;
1052
1053 long lastUsedId = -1;
1054 String lastUsedTitle;
1055 String lastUsedAccountName;
1056 String lastUsedAccountType;
1057 }
1058
1059 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1060
1061 private Context mContext;
1062 private Bundle mMap;
1063
1064 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1065 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001066 mContext = context.getApplicationContext();
John Reck2eec4c32011-05-11 15:55:32 -07001067 mMap = bundle;
1068 }
1069
1070 @Override
1071 public EditBookmarkInfo loadInBackground() {
1072 final ContentResolver cr = mContext.getContentResolver();
1073 EditBookmarkInfo info = new EditBookmarkInfo();
1074 Cursor c = null;
1075
1076 try {
1077 // First, let's lookup the bookmark (check for dupes, get needed info)
1078 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1079 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1080 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1081 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1082 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1083 new String[] { BrowserContract.Bookmarks._ID},
1084 BrowserContract.Bookmarks.URL + "=?",
1085 new String[] { url }, null);
1086 if (c.getCount() == 1 && c.moveToFirst()) {
1087 info.id = c.getLong(0);
1088 }
1089 c.close();
1090 }
1091 if (info.id != -1) {
1092 c = cr.query(ContentUris.withAppendedId(
1093 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1094 new String[] {
1095 BrowserContract.Bookmarks.PARENT,
1096 BrowserContract.Bookmarks.ACCOUNT_NAME,
1097 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1098 BrowserContract.Bookmarks.TITLE},
1099 null, null, null);
1100 if (c.moveToFirst()) {
1101 info.parentId = c.getLong(0);
1102 info.accountName = c.getString(1);
1103 info.accountType = c.getString(2);
1104 info.title = c.getString(3);
1105 }
1106 c.close();
1107 c = cr.query(ContentUris.withAppendedId(
1108 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1109 new String[] {
1110 BrowserContract.Bookmarks.TITLE,},
1111 null, null, null);
1112 if (c.moveToFirst()) {
1113 info.parentTitle = c.getString(0);
1114 }
1115 c.close();
1116 }
1117
1118 // Figure out the last used folder/account
1119 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1120 new String[] {
1121 BrowserContract.Bookmarks.PARENT,
1122 }, null, null,
1123 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1124 if (c.moveToFirst()) {
1125 long parent = c.getLong(0);
1126 c.close();
1127 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1128 new String[] {
1129 BrowserContract.Bookmarks.TITLE,
1130 BrowserContract.Bookmarks.ACCOUNT_NAME,
1131 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1132 BrowserContract.Bookmarks._ID + "=?", new String[] {
1133 Long.toString(parent)}, null);
1134 if (c.moveToFirst()) {
1135 info.lastUsedId = parent;
1136 info.lastUsedTitle = c.getString(0);
1137 info.lastUsedAccountName = c.getString(1);
1138 info.lastUsedAccountType = c.getString(2);
1139 }
1140 c.close();
1141 }
1142 } finally {
1143 if (c != null) {
1144 c.close();
1145 }
1146 }
1147
1148 return info;
1149 }
1150
1151 @Override
1152 protected void onStartLoading() {
1153 forceLoad();
1154 }
1155
1156 }
1157
The Android Open Source Project0c908882009-03-03 19:32:16 -08001158}