blob: 3a98846b506df4baebe7bbc1cc6f8a44f5203881 [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);
John Reck2eec4c32011-05-11 15:55:32 -0700344 String[] accountInfo = getAccountNameAndType();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500345 if (accountInfo != null) {
346 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
347 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700348 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400349 long currentFolder;
350 Object data = mCrumbs.getTopData();
351 if (data != null) {
352 currentFolder = ((Folder) data).Id;
353 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400354 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400355 }
356 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700357 Uri uri = getContentResolver().insert(
358 BrowserContract.Bookmarks.CONTENT_URI, values);
359 if (uri != null) {
360 return ContentUris.parseId(uri);
361 } else {
362 return -1;
363 }
364 }
365
Leon Scroggins III052ce662010-09-13 14:44:16 -0400366 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500367 // Set the list to the top in case it is scrolled.
368 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400369 mDefaultView.setVisibility(View.GONE);
370 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400371 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500372 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400373 mAddNewFolder.setVisibility(View.VISIBLE);
374 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400375 }
376
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700377 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700378 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400379 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400380 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700381 }
382 }
383
John Reck2eec4c32011-05-11 15:55:32 -0700384 private LoaderCallbacks<EditBookmarkInfo> mEditInfoLoaderCallbacks =
385 new LoaderCallbacks<EditBookmarkInfo>() {
386
387 @Override
388 public void onLoaderReset(Loader<EditBookmarkInfo> loader) {
389 // Don't care
390 }
391
392 @Override
393 public void onLoadFinished(Loader<EditBookmarkInfo> loader,
394 EditBookmarkInfo info) {
395 boolean setAccount = false;
396 if (info.id != -1) {
397 mEditingExisting = true;
398 showRemoveButton();
399 mFakeTitle.setText(R.string.edit_bookmark);
400 mTitle.setText(info.title);
401 mFolderAdapter.setOtherFolderDisplayText(info.parentTitle);
402 mMap.putLong(BrowserContract.Bookmarks._ID, info.id);
403 setAccount = true;
404 setAccount(info.accountName, info.accountType);
405 mCurrentFolder = info.parentId;
406 onCurrentFolderFound();
407 }
John Reckddf4a472011-05-13 11:25:33 -0700408 if (info.lastUsedId != -1 && info.lastUsedId != info.id) {
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";
458 if (mEditingFolder) {
459 where += " AND " + BrowserContract.Bookmarks._ID + " != "
460 + mMap.getLong(BrowserContract.Bookmarks._ID);
461 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400462 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500463 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400464 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500465 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400466 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500467 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400468 default:
469 throw new AssertionError("Asking for nonexistant loader!");
470 }
471 }
472
473 @Override
474 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
475 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700476 case LOADER_ID_ACCOUNTS:
477 mAccountAdapter.clear();
478 while (cursor.moveToNext()) {
479 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500480 }
John Reck2eec4c32011-05-11 15:55:32 -0700481 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
482 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
483 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500484 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400485 case LOADER_ID_FOLDER_CONTENTS:
486 mAdapter.changeCursor(cursor);
487 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400488 }
489 }
490
Dianne Hackborn39772c82010-12-16 00:43:54 -0800491 public void onLoaderReset(Loader<Cursor> loader) {
492 switch (loader.getId()) {
493 case LOADER_ID_FOLDER_CONTENTS:
494 mAdapter.changeCursor(null);
495 break;
496 }
497 }
498
Leon Scroggins02081942010-11-01 17:52:42 -0400499 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500500 * Move cursor to the position that has folderToFind as its "_id".
501 * @param cursor Cursor containing folders in the bookmarks database
502 * @param folderToFind "_id" of the folder to move to.
503 * @param idIndex Index in cursor of "_id"
504 * @throws AssertionError if cursor is empty or there is no row with folderToFind
505 * as its "_id".
506 */
507 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
508 throws AssertionError {
509 if (!cursor.moveToFirst()) {
510 throw new AssertionError("No folders in the database!");
511 }
512 long folder;
513 do {
514 folder = cursor.getLong(idIndex);
515 } while (folder != folderToFind && cursor.moveToNext());
516 if (cursor.isAfterLast()) {
517 throw new AssertionError("Folder(id=" + folderToFind
518 + ") holding this bookmark does not exist!");
519 }
520 }
521
Leon Scroggins III052ce662010-09-13 14:44:16 -0400522 @Override
523 public void onItemClick(AdapterView<?> parent, View view, int position,
524 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400525 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400526 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400527 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400528 }
529
Leon Scroggins162f8352010-10-18 15:02:44 -0400530 private void setShowFolderNamer(boolean show) {
531 if (show != mIsFolderNamerShowing) {
532 mIsFolderNamerShowing = show;
533 if (show) {
534 // Set the selection to the folder namer so it will be in
535 // view.
536 mListView.addFooterView(mFolderNamerHolder);
537 } else {
538 mListView.removeFooterView(mFolderNamerHolder);
539 }
540 // Refresh the list.
541 mListView.setAdapter(mAdapter);
542 if (show) {
543 mListView.setSelection(mListView.getCount() - 1);
544 }
545 }
546 }
547
Leon Scroggins III052ce662010-09-13 14:44:16 -0400548 /**
549 * Shows a list of names of folders.
550 */
551 private class FolderAdapter extends CursorAdapter {
552 public FolderAdapter(Context context) {
553 super(context, null);
554 }
555
556 @Override
557 public void bindView(View view, Context context, Cursor cursor) {
558 ((TextView) view.findViewById(android.R.id.text1)).setText(
559 cursor.getString(cursor.getColumnIndexOrThrow(
560 BrowserContract.Bookmarks.TITLE)));
561 }
562
563 @Override
564 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700565 View view = LayoutInflater.from(context).inflate(
566 R.layout.folder_list_item, null);
567 view.setBackgroundDrawable(context.getResources().
568 getDrawable(android.R.drawable.list_selector_background));
569 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400570 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400571
572 @Override
573 public boolean isEmpty() {
574 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400575 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400576 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400577 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800578
Leon Scrogginsc1129902010-12-08 15:28:33 -0500579 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800580 protected void onCreate(Bundle icicle) {
581 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500582 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100583
584 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100585
Leon Scroggins III052ce662010-09-13 14:44:16 -0400586 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100587
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400588 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700589
The Android Open Source Project0c908882009-03-03 19:32:16 -0800590 String title = null;
591 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100592
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400593 mFakeTitle = (TextView) findViewById(R.id.fake_title);
594
The Android Open Source Project0c908882009-03-03 19:32:16 -0800595 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800596 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800597 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800598 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800599 mMap = b;
600 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400601 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800602 if (mEditingFolder) {
603 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500604 } else {
605 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800606 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400607 } else {
608 int gravity = mMap.getInt("gravity", -1);
609 if (gravity != -1) {
610 WindowManager.LayoutParams l = window.getAttributes();
611 l.gravity = gravity;
612 window.setAttributes(l);
613 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800614 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400615 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
616 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
617 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700618 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400619 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800620
621 mTitle = (EditText) findViewById(R.id.title);
622 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100623
Leon Scroggins III052ce662010-09-13 14:44:16 -0400624 mAddress = (EditText) findViewById(R.id.address);
625 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800626
The Android Open Source Project0c908882009-03-03 19:32:16 -0800627 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400628 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629
630 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400631 mCancelButton.setOnClickListener(this);
632
Leon Scroggins504433a2011-01-13 12:26:52 -0500633 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700634 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500635 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500636 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400637
638 mDefaultView = findViewById(R.id.default_view);
639 mFolderSelector = findViewById(R.id.folder_selector);
640
Leon Scroggins162f8352010-10-18 15:02:44 -0400641 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
642 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400643 mFolderNamer.setOnEditorActionListener(this);
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500644 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
645 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400646
647 mAddNewFolder = findViewById(R.id.add_new_folder);
648 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400649 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400650
Leon Scroggins74dbe012010-10-15 10:54:27 -0400651 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
652 mCrumbs.setUseBackButton(true);
653 mCrumbs.setController(this);
Michael Kolb5a72f182011-01-13 20:35:06 -0800654 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400655 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800656 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400657
Leon Scroggins III052ce662010-09-13 14:44:16 -0400658 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400659 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400660 View empty = findViewById(R.id.empty);
661 mListView.setEmptyView(empty);
662 mListView.setAdapter(mAdapter);
663 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400664 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500665
John Reck2eec4c32011-05-11 15:55:32 -0700666 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
667 android.R.layout.simple_spinner_item);
668 mAccountAdapter.setDropDownViewResource(
669 android.R.layout.simple_spinner_dropdown_item);
670 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
671 mAccountSpinner.setAdapter(mAccountAdapter);
672 mAccountSpinner.setOnItemSelectedListener(this);
673
674
Leon Scroggins75630672011-01-13 17:56:15 -0500675 mFakeTitleHolder = findViewById(R.id.title_holder);
676
Leon Scroggins504433a2011-01-13 12:26:52 -0500677 if (!window.getDecorView().isInTouchMode()) {
678 mButton.requestFocus();
679 }
680
John Reck2eec4c32011-05-11 15:55:32 -0700681 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500682 }
683
Leon Scroggins75630672011-01-13 17:56:15 -0500684 private void showRemoveButton() {
685 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
686 mRemoveLink = findViewById(R.id.remove);
687 mRemoveLink.setVisibility(View.VISIBLE);
688 mRemoveLink.setOnClickListener(this);
689 }
690
Leon Scroggins504433a2011-01-13 12:26:52 -0500691 // Called once we have determined which folder is the root folder
692 private void onRootFolderFound(long root) {
693 mRootFolder = root;
John Reck2eec4c32011-05-11 15:55:32 -0700694 mCurrentFolder = mRootFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500695 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700696 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500697 }
698
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500699 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700700 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500701 String name = getString(R.string.bookmarks);
702 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
703 new Folder(name, mRootFolder));
704 // To better match the other folders.
705 mTopLevelLabel.setCompoundDrawablePadding(6);
706 }
707
Leon Scroggins504433a2011-01-13 12:26:52 -0500708 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400709 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500710 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500711 // Since we're not in the root folder, change the selection to other
712 // folder now. The text will get changed once we select the correct
713 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500714 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500715 } else {
716 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500717 if (!mEditingFolder) {
718 // Initially the "Bookmarks" folder should be showing, rather than
719 // the home screen. In the editing folder case, home screen is not
720 // an option, so "Bookmarks" folder is already at the top.
721 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
722 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400723 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400724 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700725 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500726}
Leon Scroggins8baaa632010-12-08 19:46:53 -0500727 /**
728 * Get the account name and type of the currently synced account.
Leon Scroggins8baaa632010-12-08 19:46:53 -0500729 * @return null if no account name or type. Otherwise, the result will be
730 * an array of two Strings, the accountName and accountType, respectively.
731 */
John Reck2eec4c32011-05-11 15:55:32 -0700732 private String[] getAccountNameAndType() {
733 BookmarkAccount account = (BookmarkAccount) mAccountSpinner.getSelectedItem();
734 if (account == null) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500735 return null;
736 }
John Reck2eec4c32011-05-11 15:55:32 -0700737 return new String[] { account.accountName, account.accountType };
Leon Scroggins8baaa632010-12-08 19:46:53 -0500738 }
739
Leon Scroggins02065b02010-01-04 14:30:13 -0500740 /**
741 * Runnable to save a bookmark, so it can be performed in its own thread.
742 */
743 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400744 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500745 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200746 private Context mContext;
747 public SaveBookmarkRunnable(Context ctx, Message msg) {
748 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500749 mMessage = msg;
750 }
751 public void run() {
752 // Unbundle bookmark data.
753 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400754 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
755 String url = bundle.getString(BrowserContract.Bookmarks.URL);
756 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500757 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400758 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
759 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500760
761 // Save to the bookmarks DB.
762 try {
763 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400764 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
765 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500766 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200767 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500768 }
769 mMessage.arg1 = 1;
770 } catch (IllegalStateException e) {
771 mMessage.arg1 = 0;
772 }
773 mMessage.sendToTarget();
774 }
775 }
776
John Reckc8490812010-11-22 14:15:36 -0800777 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
778 Context mContext;
779 Long mId;
780
781 public UpdateBookmarkTask(Context context, long id) {
782 mContext = context;
783 mId = id;
784 }
785
786 @Override
787 protected Void doInBackground(ContentValues... params) {
788 if (params.length != 1) {
789 throw new IllegalArgumentException("No ContentValues provided!");
790 }
791 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
792 mContext.getContentResolver().update(
793 uri,
794 params[0], null, null);
795 return null;
796 }
797 }
798
Ben Murdoch1794fe22009-09-29 18:14:30 +0100799 private void createHandler() {
800 if (mHandler == null) {
801 mHandler = new Handler() {
802 @Override
803 public void handleMessage(Message msg) {
804 switch (msg.what) {
805 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500806 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100807 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
808 Toast.LENGTH_LONG).show();
809 } else {
810 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
811 Toast.LENGTH_LONG).show();
812 }
813 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400814 case TOUCH_ICON_DOWNLOADED:
815 Bundle b = msg.getData();
816 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400817 AddBookmarkPage.this,
818 b.getString(BrowserContract.Bookmarks.URL),
819 b.getString(BrowserContract.Bookmarks.TITLE),
820 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
821 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400822 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500823 case BOOKMARK_DELETED:
824 finish();
825 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100826 }
827 }
828 };
829 }
830 }
831
The Android Open Source Project0c908882009-03-03 19:32:16 -0800832 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100833 * 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 -0800834 */
835 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100836 createHandler();
837
The Android Open Source Project0c908882009-03-03 19:32:16 -0800838 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100839 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100841
The Android Open Source Project0c908882009-03-03 19:32:16 -0800842 boolean emptyTitle = title.length() == 0;
843 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
844 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800845 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800846 if (emptyTitle) {
847 mTitle.setError(r.getText(R.string.bookmark_needs_title));
848 }
849 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400850 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800851 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400852 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100853
The Android Open Source Project0c908882009-03-03 19:32:16 -0800854 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000855 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800856 if (!mEditingFolder) {
857 try {
858 // We allow bookmarks with a javascript: scheme, but these will in most cases
859 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000860
John Reckc8490812010-11-22 14:15:36 -0800861 if (!url.toLowerCase().startsWith("javascript:")) {
862 URI uriObj = new URI(url);
863 String scheme = uriObj.getScheme();
864 if (!Bookmarks.urlHasAcceptableScheme(url)) {
865 // If the scheme was non-null, let the user know that we
866 // can't save their bookmark. If it was null, we'll assume
867 // they meant http when we parse it in the WebAddress class.
868 if (scheme != null) {
869 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
870 return false;
871 }
872 WebAddress address;
873 try {
874 address = new WebAddress(unfilteredUrl);
875 } catch (ParseException e) {
876 throw new URISyntaxException("", "");
877 }
878 if (address.getHost().length() == 0) {
879 throw new URISyntaxException("", "");
880 }
881 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000882 }
Ben Murdochde353622009-10-12 10:29:00 +0100883 }
John Reckc8490812010-11-22 14:15:36 -0800884 } catch (URISyntaxException e) {
885 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
886 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800887 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800888 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100889
Leon Scroggins88d08032010-10-21 15:17:10 -0400890 if (mSaveToHomeScreen) {
891 mEditingExisting = false;
892 }
893
894 boolean urlUnmodified = url.equals(mOriginalUrl);
895
Ben Murdoch1794fe22009-09-29 18:14:30 +0100896 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800897 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
898 ContentValues values = new ContentValues();
899 values.put(BrowserContract.Bookmarks.TITLE, title);
900 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
901 if (!mEditingFolder) {
902 values.put(BrowserContract.Bookmarks.URL, url);
903 if (!urlUnmodified) {
904 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
905 }
906 }
907 if (values.size() > 0) {
908 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
909 }
910 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100911 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400912 Bitmap thumbnail;
913 Bitmap favicon;
914 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400915 thumbnail = (Bitmap) mMap.getParcelable(
916 BrowserContract.Bookmarks.THUMBNAIL);
917 favicon = (Bitmap) mMap.getParcelable(
918 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400919 } else {
920 thumbnail = null;
921 favicon = null;
922 }
923
Ben Murdoch1794fe22009-09-29 18:14:30 +0100924 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400925 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
926 bundle.putString(BrowserContract.Bookmarks.URL, url);
927 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400928
929 if (mSaveToHomeScreen) {
930 if (mTouchIconUrl != null && urlUnmodified) {
931 Message msg = Message.obtain(mHandler,
932 TOUCH_ICON_DOWNLOADED);
933 msg.setData(bundle);
934 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400935 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400936 icon.execute(mTouchIconUrl);
937 } else {
938 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
939 title, null /*touchIcon*/, favicon));
940 }
941 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400942 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
943 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
944 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400945 // Post a message to write to the DB.
946 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
947 msg.setData(bundle);
948 // Start a new thread so as to not slow down the UI
949 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
950 t.start();
951 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100952 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000953 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800954 }
955 return true;
956 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400957
John Reck2eec4c32011-05-11 15:55:32 -0700958 @Override
959 public void onItemSelected(AdapterView<?> parent, View view, int position,
960 long id) {
961 if (mAccountSpinner == parent) {
962 long root = mAccountAdapter.getItem(position).rootFolderId;
963 if (root != mRootFolder) {
964 onRootFolderFound(root);
965 }
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
1002 static class AccountsLoader extends CursorLoader {
1003
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) {
1015 super(context, Accounts.CONTENT_URI, PROJECTION, null, null,
1016 Accounts.ACCOUNT_NAME + " ASC");
1017 }
1018
1019 }
1020
1021 static class BookmarkAccount {
1022
1023 private String mLabel;
1024 String accountName, accountType;
1025 long rootFolderId;
1026
1027 public BookmarkAccount(Context context, Cursor cursor) {
1028 accountName = cursor.getString(
1029 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1030 accountType = cursor.getString(
1031 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1032 rootFolderId = cursor.getLong(
1033 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1034 mLabel = accountName;
1035 if (TextUtils.isEmpty(mLabel)) {
1036 mLabel = context.getString(R.string.local_bookmarks);
1037 }
1038 }
1039
1040 @Override
1041 public String toString() {
1042 return mLabel;
1043 }
1044 }
1045
1046 static class EditBookmarkInfo {
1047 long id = -1;
1048 long parentId = -1;
1049 String parentTitle;
1050 String title;
1051 String accountName;
1052 String accountType;
1053
1054 long lastUsedId = -1;
1055 String lastUsedTitle;
1056 String lastUsedAccountName;
1057 String lastUsedAccountType;
1058 }
1059
1060 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1061
1062 private Context mContext;
1063 private Bundle mMap;
1064
1065 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1066 super(context);
1067 mContext = context;
1068 mMap = bundle;
1069 }
1070
1071 @Override
1072 public EditBookmarkInfo loadInBackground() {
1073 final ContentResolver cr = mContext.getContentResolver();
1074 EditBookmarkInfo info = new EditBookmarkInfo();
1075 Cursor c = null;
1076
1077 try {
1078 // First, let's lookup the bookmark (check for dupes, get needed info)
1079 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1080 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1081 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1082 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1083 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1084 new String[] { BrowserContract.Bookmarks._ID},
1085 BrowserContract.Bookmarks.URL + "=?",
1086 new String[] { url }, null);
1087 if (c.getCount() == 1 && c.moveToFirst()) {
1088 info.id = c.getLong(0);
1089 }
1090 c.close();
1091 }
1092 if (info.id != -1) {
1093 c = cr.query(ContentUris.withAppendedId(
1094 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1095 new String[] {
1096 BrowserContract.Bookmarks.PARENT,
1097 BrowserContract.Bookmarks.ACCOUNT_NAME,
1098 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1099 BrowserContract.Bookmarks.TITLE},
1100 null, null, null);
1101 if (c.moveToFirst()) {
1102 info.parentId = c.getLong(0);
1103 info.accountName = c.getString(1);
1104 info.accountType = c.getString(2);
1105 info.title = c.getString(3);
1106 }
1107 c.close();
1108 c = cr.query(ContentUris.withAppendedId(
1109 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1110 new String[] {
1111 BrowserContract.Bookmarks.TITLE,},
1112 null, null, null);
1113 if (c.moveToFirst()) {
1114 info.parentTitle = c.getString(0);
1115 }
1116 c.close();
1117 }
1118
1119 // Figure out the last used folder/account
1120 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1121 new String[] {
1122 BrowserContract.Bookmarks.PARENT,
1123 }, null, null,
1124 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1125 if (c.moveToFirst()) {
1126 long parent = c.getLong(0);
1127 c.close();
1128 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1129 new String[] {
1130 BrowserContract.Bookmarks.TITLE,
1131 BrowserContract.Bookmarks.ACCOUNT_NAME,
1132 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1133 BrowserContract.Bookmarks._ID + "=?", new String[] {
1134 Long.toString(parent)}, null);
1135 if (c.moveToFirst()) {
1136 info.lastUsedId = parent;
1137 info.lastUsedTitle = c.getString(0);
1138 info.lastUsedAccountName = c.getString(1);
1139 info.lastUsedAccountType = c.getString(2);
1140 }
1141 c.close();
1142 }
1143 } finally {
1144 if (c != null) {
1145 c.close();
1146 }
1147 }
1148
1149 return info;
1150 }
1151
1152 @Override
1153 protected void onStartLoading() {
1154 forceLoad();
1155 }
1156
1157 }
1158
The Android Open Source Project0c908882009-03-03 19:32:16 -08001159}