blob: 903c363bf7c1357ddd1987456bd5cd17cc3fc5d2 [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);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400370 }
371
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700372 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700373 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400374 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400375 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700376 }
377 }
378
John Reck2eec4c32011-05-11 15:55:32 -0700379 private LoaderCallbacks<EditBookmarkInfo> mEditInfoLoaderCallbacks =
380 new LoaderCallbacks<EditBookmarkInfo>() {
381
382 @Override
383 public void onLoaderReset(Loader<EditBookmarkInfo> loader) {
384 // Don't care
385 }
386
387 @Override
388 public void onLoadFinished(Loader<EditBookmarkInfo> loader,
389 EditBookmarkInfo info) {
390 boolean setAccount = false;
391 if (info.id != -1) {
392 mEditingExisting = true;
393 showRemoveButton();
394 mFakeTitle.setText(R.string.edit_bookmark);
395 mTitle.setText(info.title);
396 mFolderAdapter.setOtherFolderDisplayText(info.parentTitle);
397 mMap.putLong(BrowserContract.Bookmarks._ID, info.id);
398 setAccount = true;
399 setAccount(info.accountName, info.accountType);
400 mCurrentFolder = info.parentId;
401 onCurrentFolderFound();
402 }
John Reck37894a92011-05-13 12:47:53 -0700403 // TODO: Detect if lastUsedId is a subfolder of info.id in the
404 // editing folder case. For now, just don't show the last used
405 // folder at all to prevent any chance of the user adding a parent
406 // folder to a child folder
407 if (info.lastUsedId != -1 && info.lastUsedId != info.id
408 && !mEditingFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700409 if (setAccount && info.lastUsedId != mRootFolder
410 && TextUtils.equals(info.lastUsedAccountName, info.accountName)
411 && TextUtils.equals(info.lastUsedAccountType, info.accountType)) {
412 mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle);
413 } else if (!setAccount) {
414 setAccount = true;
415 setAccount(info.lastUsedAccountName, info.lastUsedAccountType);
416 if (info.lastUsedId != mRootFolder) {
417 mFolderAdapter.addRecentFolder(info.lastUsedId,
418 info.lastUsedTitle);
419 }
420 }
421 }
422 if (!setAccount) {
423 mAccountSpinner.setSelection(0);
424 }
425 }
426
427 @Override
428 public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) {
429 return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap);
430 }
431 };
432
433 void setAccount(String accountName, String accountType) {
434 for (int i = 0; i < mAccountAdapter.getCount(); i++) {
435 BookmarkAccount account = mAccountAdapter.getItem(i);
436 if (TextUtils.equals(account.accountName, accountName)
437 && TextUtils.equals(account.accountType, accountType)) {
438 onRootFolderFound(account.rootFolderId);
439 mAccountSpinner.setSelection(i);
440 return;
441 }
442 }
443 }
444
Leon Scroggins III052ce662010-09-13 14:44:16 -0400445 @Override
446 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
447 String[] projection;
448 switch (id) {
John Reck2eec4c32011-05-11 15:55:32 -0700449 case LOADER_ID_ACCOUNTS:
450 return new AccountsLoader(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400451 case LOADER_ID_FOLDER_CONTENTS:
452 projection = new String[] {
453 BrowserContract.Bookmarks._ID,
454 BrowserContract.Bookmarks.TITLE,
455 BrowserContract.Bookmarks.IS_FOLDER
456 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500457 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
John Reck1dd8cd42011-05-13 11:22:09 -0700458 String whereArgs[] = null;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500459 if (mEditingFolder) {
John Reck1dd8cd42011-05-13 11:22:09 -0700460 where += " AND " + BrowserContract.Bookmarks._ID + " != ?";
461 whereArgs = new String[] { Long.toString(mMap.getLong(
462 BrowserContract.Bookmarks._ID)) };
463 }
464 long currentFolder;
465 Object data = mCrumbs.getTopData();
466 if (data != null) {
467 currentFolder = ((Folder) data).Id;
468 } else {
469 currentFolder = mRootFolder;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500470 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400471 return new CursorLoader(this,
John Reck1dd8cd42011-05-13 11:22:09 -0700472 getUriForFolder(currentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400473 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500474 where,
John Reck1dd8cd42011-05-13 11:22:09 -0700475 whereArgs,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500476 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400477 default:
478 throw new AssertionError("Asking for nonexistant loader!");
479 }
480 }
481
482 @Override
483 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
484 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700485 case LOADER_ID_ACCOUNTS:
486 mAccountAdapter.clear();
487 while (cursor.moveToNext()) {
488 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500489 }
John Reck2eec4c32011-05-11 15:55:32 -0700490 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
491 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
492 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500493 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400494 case LOADER_ID_FOLDER_CONTENTS:
495 mAdapter.changeCursor(cursor);
496 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400497 }
498 }
499
Dianne Hackborn39772c82010-12-16 00:43:54 -0800500 public void onLoaderReset(Loader<Cursor> loader) {
501 switch (loader.getId()) {
502 case LOADER_ID_FOLDER_CONTENTS:
503 mAdapter.changeCursor(null);
504 break;
505 }
506 }
507
Leon Scroggins02081942010-11-01 17:52:42 -0400508 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500509 * Move cursor to the position that has folderToFind as its "_id".
510 * @param cursor Cursor containing folders in the bookmarks database
511 * @param folderToFind "_id" of the folder to move to.
512 * @param idIndex Index in cursor of "_id"
513 * @throws AssertionError if cursor is empty or there is no row with folderToFind
514 * as its "_id".
515 */
516 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
517 throws AssertionError {
518 if (!cursor.moveToFirst()) {
519 throw new AssertionError("No folders in the database!");
520 }
521 long folder;
522 do {
523 folder = cursor.getLong(idIndex);
524 } while (folder != folderToFind && cursor.moveToNext());
525 if (cursor.isAfterLast()) {
526 throw new AssertionError("Folder(id=" + folderToFind
527 + ") holding this bookmark does not exist!");
528 }
529 }
530
Leon Scroggins III052ce662010-09-13 14:44:16 -0400531 @Override
532 public void onItemClick(AdapterView<?> parent, View view, int position,
533 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400534 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400535 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400536 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400537 }
538
Leon Scroggins162f8352010-10-18 15:02:44 -0400539 private void setShowFolderNamer(boolean show) {
540 if (show != mIsFolderNamerShowing) {
541 mIsFolderNamerShowing = show;
542 if (show) {
543 // Set the selection to the folder namer so it will be in
544 // view.
545 mListView.addFooterView(mFolderNamerHolder);
546 } else {
547 mListView.removeFooterView(mFolderNamerHolder);
548 }
549 // Refresh the list.
550 mListView.setAdapter(mAdapter);
551 if (show) {
552 mListView.setSelection(mListView.getCount() - 1);
553 }
554 }
555 }
556
Leon Scroggins III052ce662010-09-13 14:44:16 -0400557 /**
558 * Shows a list of names of folders.
559 */
560 private class FolderAdapter extends CursorAdapter {
561 public FolderAdapter(Context context) {
562 super(context, null);
563 }
564
565 @Override
566 public void bindView(View view, Context context, Cursor cursor) {
567 ((TextView) view.findViewById(android.R.id.text1)).setText(
568 cursor.getString(cursor.getColumnIndexOrThrow(
569 BrowserContract.Bookmarks.TITLE)));
570 }
571
572 @Override
573 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700574 View view = LayoutInflater.from(context).inflate(
575 R.layout.folder_list_item, null);
576 view.setBackgroundDrawable(context.getResources().
577 getDrawable(android.R.drawable.list_selector_background));
578 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400579 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400580
581 @Override
582 public boolean isEmpty() {
583 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400584 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400585 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400586 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800587
Leon Scrogginsc1129902010-12-08 15:28:33 -0500588 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800589 protected void onCreate(Bundle icicle) {
590 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500591 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100592
593 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100594
Leon Scroggins III052ce662010-09-13 14:44:16 -0400595 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100596
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400597 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700598
The Android Open Source Project0c908882009-03-03 19:32:16 -0800599 String title = null;
600 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100601
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400602 mFakeTitle = (TextView) findViewById(R.id.fake_title);
603
The Android Open Source Project0c908882009-03-03 19:32:16 -0800604 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800605 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800606 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800607 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800608 mMap = b;
609 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400610 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800611 if (mEditingFolder) {
612 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500613 } else {
614 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800615 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400616 } else {
617 int gravity = mMap.getInt("gravity", -1);
618 if (gravity != -1) {
619 WindowManager.LayoutParams l = window.getAttributes();
620 l.gravity = gravity;
621 window.setAttributes(l);
622 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800623 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400624 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
625 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
626 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700627 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400628 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629
630 mTitle = (EditText) findViewById(R.id.title);
631 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100632
Leon Scroggins III052ce662010-09-13 14:44:16 -0400633 mAddress = (EditText) findViewById(R.id.address);
634 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800635
The Android Open Source Project0c908882009-03-03 19:32:16 -0800636 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400637 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800638
639 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400640 mCancelButton.setOnClickListener(this);
641
Leon Scroggins504433a2011-01-13 12:26:52 -0500642 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700643 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500644 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500645 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400646
647 mDefaultView = findViewById(R.id.default_view);
648 mFolderSelector = findViewById(R.id.folder_selector);
649
Leon Scroggins162f8352010-10-18 15:02:44 -0400650 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
651 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400652 mFolderNamer.setOnEditorActionListener(this);
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500653 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
654 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400655
656 mAddNewFolder = findViewById(R.id.add_new_folder);
657 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400658 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400659
Leon Scroggins74dbe012010-10-15 10:54:27 -0400660 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
661 mCrumbs.setUseBackButton(true);
662 mCrumbs.setController(this);
Michael Kolb5a72f182011-01-13 20:35:06 -0800663 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400664 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800665 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400666
Leon Scroggins III052ce662010-09-13 14:44:16 -0400667 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400668 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400669 View empty = findViewById(R.id.empty);
670 mListView.setEmptyView(empty);
671 mListView.setAdapter(mAdapter);
672 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400673 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500674
John Reck2eec4c32011-05-11 15:55:32 -0700675 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
676 android.R.layout.simple_spinner_item);
677 mAccountAdapter.setDropDownViewResource(
678 android.R.layout.simple_spinner_dropdown_item);
679 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
680 mAccountSpinner.setAdapter(mAccountAdapter);
681 mAccountSpinner.setOnItemSelectedListener(this);
682
683
Leon Scroggins75630672011-01-13 17:56:15 -0500684 mFakeTitleHolder = findViewById(R.id.title_holder);
685
Leon Scroggins504433a2011-01-13 12:26:52 -0500686 if (!window.getDecorView().isInTouchMode()) {
687 mButton.requestFocus();
688 }
689
John Reck2eec4c32011-05-11 15:55:32 -0700690 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500691 }
692
Leon Scroggins75630672011-01-13 17:56:15 -0500693 private void showRemoveButton() {
694 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
695 mRemoveLink = findViewById(R.id.remove);
696 mRemoveLink.setVisibility(View.VISIBLE);
697 mRemoveLink.setOnClickListener(this);
698 }
699
Leon Scroggins504433a2011-01-13 12:26:52 -0500700 // Called once we have determined which folder is the root folder
701 private void onRootFolderFound(long root) {
702 mRootFolder = root;
John Reck2eec4c32011-05-11 15:55:32 -0700703 mCurrentFolder = mRootFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500704 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700705 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500706 }
707
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500708 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700709 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500710 String name = getString(R.string.bookmarks);
711 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
712 new Folder(name, mRootFolder));
713 // To better match the other folders.
714 mTopLevelLabel.setCompoundDrawablePadding(6);
715 }
716
Leon Scroggins504433a2011-01-13 12:26:52 -0500717 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400718 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500719 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500720 // Since we're not in the root folder, change the selection to other
721 // folder now. The text will get changed once we select the correct
722 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500723 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500724 } else {
725 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500726 if (!mEditingFolder) {
727 // Initially the "Bookmarks" folder should be showing, rather than
728 // the home screen. In the editing folder case, home screen is not
729 // an option, so "Bookmarks" folder is already at the top.
730 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
731 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400732 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400733 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700734 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500735 }
736
Leon Scroggins02065b02010-01-04 14:30:13 -0500737 /**
738 * Runnable to save a bookmark, so it can be performed in its own thread.
739 */
740 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400741 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500742 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200743 private Context mContext;
744 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100745 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500746 mMessage = msg;
747 }
748 public void run() {
749 // Unbundle bookmark data.
750 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400751 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
752 String url = bundle.getString(BrowserContract.Bookmarks.URL);
753 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500754 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400755 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
756 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500757
758 // Save to the bookmarks DB.
759 try {
760 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400761 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700762 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500763 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200764 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500765 }
766 mMessage.arg1 = 1;
767 } catch (IllegalStateException e) {
768 mMessage.arg1 = 0;
769 }
770 mMessage.sendToTarget();
771 }
772 }
773
John Reckc8490812010-11-22 14:15:36 -0800774 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
775 Context mContext;
776 Long mId;
777
778 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100779 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800780 mId = id;
781 }
782
783 @Override
784 protected Void doInBackground(ContentValues... params) {
785 if (params.length != 1) {
786 throw new IllegalArgumentException("No ContentValues provided!");
787 }
788 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
789 mContext.getContentResolver().update(
790 uri,
791 params[0], null, null);
792 return null;
793 }
794 }
795
Ben Murdoch1794fe22009-09-29 18:14:30 +0100796 private void createHandler() {
797 if (mHandler == null) {
798 mHandler = new Handler() {
799 @Override
800 public void handleMessage(Message msg) {
801 switch (msg.what) {
802 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500803 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100804 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
805 Toast.LENGTH_LONG).show();
806 } else {
807 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
808 Toast.LENGTH_LONG).show();
809 }
810 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400811 case TOUCH_ICON_DOWNLOADED:
812 Bundle b = msg.getData();
813 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400814 AddBookmarkPage.this,
815 b.getString(BrowserContract.Bookmarks.URL),
816 b.getString(BrowserContract.Bookmarks.TITLE),
817 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
818 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400819 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500820 case BOOKMARK_DELETED:
821 finish();
822 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100823 }
824 }
825 };
826 }
827 }
828
The Android Open Source Project0c908882009-03-03 19:32:16 -0800829 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100830 * 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 -0800831 */
832 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100833 createHandler();
834
The Android Open Source Project0c908882009-03-03 19:32:16 -0800835 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100836 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100838
The Android Open Source Project0c908882009-03-03 19:32:16 -0800839 boolean emptyTitle = title.length() == 0;
840 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
841 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800842 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800843 if (emptyTitle) {
844 mTitle.setError(r.getText(R.string.bookmark_needs_title));
845 }
846 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400847 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800848 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400849 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100850
The Android Open Source Project0c908882009-03-03 19:32:16 -0800851 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000852 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800853 if (!mEditingFolder) {
854 try {
855 // We allow bookmarks with a javascript: scheme, but these will in most cases
856 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000857
John Reckc8490812010-11-22 14:15:36 -0800858 if (!url.toLowerCase().startsWith("javascript:")) {
859 URI uriObj = new URI(url);
860 String scheme = uriObj.getScheme();
861 if (!Bookmarks.urlHasAcceptableScheme(url)) {
862 // If the scheme was non-null, let the user know that we
863 // can't save their bookmark. If it was null, we'll assume
864 // they meant http when we parse it in the WebAddress class.
865 if (scheme != null) {
866 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
867 return false;
868 }
869 WebAddress address;
870 try {
871 address = new WebAddress(unfilteredUrl);
872 } catch (ParseException e) {
873 throw new URISyntaxException("", "");
874 }
875 if (address.getHost().length() == 0) {
876 throw new URISyntaxException("", "");
877 }
878 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000879 }
Ben Murdochde353622009-10-12 10:29:00 +0100880 }
John Reckc8490812010-11-22 14:15:36 -0800881 } catch (URISyntaxException e) {
882 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
883 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800884 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800885 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100886
Leon Scroggins88d08032010-10-21 15:17:10 -0400887 if (mSaveToHomeScreen) {
888 mEditingExisting = false;
889 }
890
891 boolean urlUnmodified = url.equals(mOriginalUrl);
892
Ben Murdoch1794fe22009-09-29 18:14:30 +0100893 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800894 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
895 ContentValues values = new ContentValues();
896 values.put(BrowserContract.Bookmarks.TITLE, title);
897 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
898 if (!mEditingFolder) {
899 values.put(BrowserContract.Bookmarks.URL, url);
900 if (!urlUnmodified) {
901 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
902 }
903 }
904 if (values.size() > 0) {
905 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
906 }
907 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100908 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400909 Bitmap thumbnail;
910 Bitmap favicon;
911 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400912 thumbnail = (Bitmap) mMap.getParcelable(
913 BrowserContract.Bookmarks.THUMBNAIL);
914 favicon = (Bitmap) mMap.getParcelable(
915 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400916 } else {
917 thumbnail = null;
918 favicon = null;
919 }
920
Ben Murdoch1794fe22009-09-29 18:14:30 +0100921 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400922 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
923 bundle.putString(BrowserContract.Bookmarks.URL, url);
924 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400925
926 if (mSaveToHomeScreen) {
927 if (mTouchIconUrl != null && urlUnmodified) {
928 Message msg = Message.obtain(mHandler,
929 TOUCH_ICON_DOWNLOADED);
930 msg.setData(bundle);
931 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400932 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400933 icon.execute(mTouchIconUrl);
934 } else {
935 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
936 title, null /*touchIcon*/, favicon));
937 }
938 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400939 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
940 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
941 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400942 // Post a message to write to the DB.
943 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
944 msg.setData(bundle);
945 // Start a new thread so as to not slow down the UI
946 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
947 t.start();
948 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100949 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000950 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800951 }
952 return true;
953 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400954
John Reck2eec4c32011-05-11 15:55:32 -0700955 @Override
956 public void onItemSelected(AdapterView<?> parent, View view, int position,
957 long id) {
958 if (mAccountSpinner == parent) {
959 long root = mAccountAdapter.getItem(position).rootFolderId;
960 if (root != mRootFolder) {
961 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -0700962 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -0700963 }
964 }
965 }
966
967 @Override
968 public void onNothingSelected(AdapterView<?> parent) {
969 // Don't care
970 }
971
Leon Scroggins162f8352010-10-18 15:02:44 -0400972 /*
973 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
974 */
975 public static class CustomListView extends ListView {
976 private EditText mEditText;
977
978 public void addEditText(EditText editText) {
979 mEditText = editText;
980 }
981
982 public CustomListView(Context context) {
983 super(context);
984 }
985
986 public CustomListView(Context context, AttributeSet attrs) {
987 super(context, attrs);
988 }
989
990 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
991 super(context, attrs, defStyle);
992 }
993
994 @Override
995 public boolean checkInputConnectionProxy(View view) {
996 return view == mEditText;
997 }
998 }
John Reck2eec4c32011-05-11 15:55:32 -0700999
John Reck9b8cd1e2011-05-25 18:14:01 -07001000 public static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001001
1002 static final String[] PROJECTION = new String[] {
1003 Accounts.ACCOUNT_NAME,
1004 Accounts.ACCOUNT_TYPE,
1005 Accounts.ROOT_ID,
1006 };
1007
1008 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1009 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1010 static final int COLUMN_INDEX_ROOT_ID = 2;
1011
1012 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001013 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001014 }
1015
1016 }
1017
John Reck9b8cd1e2011-05-25 18:14:01 -07001018 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001019
1020 private String mLabel;
1021 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001022 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001023
1024 public BookmarkAccount(Context context, Cursor cursor) {
1025 accountName = cursor.getString(
1026 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1027 accountType = cursor.getString(
1028 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1029 rootFolderId = cursor.getLong(
1030 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1031 mLabel = accountName;
1032 if (TextUtils.isEmpty(mLabel)) {
1033 mLabel = context.getString(R.string.local_bookmarks);
1034 }
1035 }
1036
1037 @Override
1038 public String toString() {
1039 return mLabel;
1040 }
1041 }
1042
1043 static class EditBookmarkInfo {
1044 long id = -1;
1045 long parentId = -1;
1046 String parentTitle;
1047 String title;
1048 String accountName;
1049 String accountType;
1050
1051 long lastUsedId = -1;
1052 String lastUsedTitle;
1053 String lastUsedAccountName;
1054 String lastUsedAccountType;
1055 }
1056
1057 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1058
1059 private Context mContext;
1060 private Bundle mMap;
1061
1062 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1063 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001064 mContext = context.getApplicationContext();
John Reck2eec4c32011-05-11 15:55:32 -07001065 mMap = bundle;
1066 }
1067
1068 @Override
1069 public EditBookmarkInfo loadInBackground() {
1070 final ContentResolver cr = mContext.getContentResolver();
1071 EditBookmarkInfo info = new EditBookmarkInfo();
1072 Cursor c = null;
1073
1074 try {
1075 // First, let's lookup the bookmark (check for dupes, get needed info)
1076 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1077 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1078 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1079 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1080 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1081 new String[] { BrowserContract.Bookmarks._ID},
1082 BrowserContract.Bookmarks.URL + "=?",
1083 new String[] { url }, null);
1084 if (c.getCount() == 1 && c.moveToFirst()) {
1085 info.id = c.getLong(0);
1086 }
1087 c.close();
1088 }
1089 if (info.id != -1) {
1090 c = cr.query(ContentUris.withAppendedId(
1091 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1092 new String[] {
1093 BrowserContract.Bookmarks.PARENT,
1094 BrowserContract.Bookmarks.ACCOUNT_NAME,
1095 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1096 BrowserContract.Bookmarks.TITLE},
1097 null, null, null);
1098 if (c.moveToFirst()) {
1099 info.parentId = c.getLong(0);
1100 info.accountName = c.getString(1);
1101 info.accountType = c.getString(2);
1102 info.title = c.getString(3);
1103 }
1104 c.close();
1105 c = cr.query(ContentUris.withAppendedId(
1106 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1107 new String[] {
1108 BrowserContract.Bookmarks.TITLE,},
1109 null, null, null);
1110 if (c.moveToFirst()) {
1111 info.parentTitle = c.getString(0);
1112 }
1113 c.close();
1114 }
1115
1116 // Figure out the last used folder/account
1117 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1118 new String[] {
1119 BrowserContract.Bookmarks.PARENT,
1120 }, null, null,
1121 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1122 if (c.moveToFirst()) {
1123 long parent = c.getLong(0);
1124 c.close();
1125 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1126 new String[] {
1127 BrowserContract.Bookmarks.TITLE,
1128 BrowserContract.Bookmarks.ACCOUNT_NAME,
1129 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1130 BrowserContract.Bookmarks._ID + "=?", new String[] {
1131 Long.toString(parent)}, null);
1132 if (c.moveToFirst()) {
1133 info.lastUsedId = parent;
1134 info.lastUsedTitle = c.getString(0);
1135 info.lastUsedAccountName = c.getString(1);
1136 info.lastUsedAccountType = c.getString(2);
1137 }
1138 c.close();
1139 }
1140 } finally {
1141 if (c != null) {
1142 c.close();
1143 }
1144 }
1145
1146 return info;
1147 }
1148
1149 @Override
1150 protected void onStartLoading() {
1151 forceLoad();
1152 }
1153
1154 }
1155
The Android Open Source Project0c908882009-03-03 19:32:16 -08001156}