blob: 3659041ba4aa01581876ab5e6c8b0e9d9d2c0521 [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 Scroggins25230d72010-09-28 20:09:25 -040019import com.android.browser.provider.BrowserProvider2;
Leon Scroggins504433a2011-01-13 12:26:52 -050020import com.android.browser.addbookmark.FolderSpinner;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -050021import com.android.browser.addbookmark.FolderSpinnerAdapter;
Leon Scroggins25230d72010-09-28 20:09:25 -040022
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.app.Activity;
Leon Scroggins III052ce662010-09-13 14:44:16 -040024import android.app.LoaderManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.content.ContentResolver;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070026import android.content.ContentUris;
Leon Scroggins III052ce662010-09-13 14:44:16 -040027import android.content.ContentValues;
28import android.content.Context;
29import android.content.CursorLoader;
Leon Scroggins III052ce662010-09-13 14:44:16 -040030import android.content.Loader;
Leon Scroggins25230d72010-09-28 20:09:25 -040031import android.content.SharedPreferences;
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 Scroggins25230d72010-09-28 20:09:25 -040043import android.preference.PreferenceManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040044import android.provider.BrowserContract;
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;
56import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080057import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040058import android.widget.ListView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080059import android.widget.TextView;
60import android.widget.Toast;
61
Cary Clarkf2407c62009-09-04 12:25:10 -040062import java.net.URI;
63import java.net.URISyntaxException;
Leon Scroggins74dbe012010-10-15 10:54:27 -040064import java.util.Stack;
Leon Scroggins III052ce662010-09-13 14:44:16 -040065
66public class AddBookmarkPage extends Activity
67 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040068 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
Leon Scroggins504433a2011-01-13 12:26:52 -050069 BreadCrumbView.Controller, FolderSpinner.OnSetSelectionListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080070
Michael Kolb370a4f32010-10-06 10:45:32 -070071 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040072 public static final String TOUCH_ICON_URL = "touch_icon_url";
73 // Place on an edited bookmark to remove the saved thumbnail
74 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
75 public static final String USER_AGENT = "user_agent";
Leon Scrogginsbdff8a72011-02-11 15:49:04 -050076 public static final String CHECK_FOR_DUPE = "check_for_dupe";
Michael Kolb370a4f32010-10-06 10:45:32 -070077
John Reckc8490812010-11-22 14:15:36 -080078 /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark";
79 /* package */ static final String EXTRA_IS_FOLDER = "is_folder";
80
Leon Scroggins74dbe012010-10-15 10:54:27 -040081 private static final int MAX_CRUMBS_SHOWN = 2;
82
The Android Open Source Project0c908882009-03-03 19:32:16 -080083 private final String LOGTAG = "Bookmarks";
84
Leon Scroggins III052ce662010-09-13 14:44:16 -040085 // IDs for the CursorLoaders that are used.
86 private final int LOADER_ID_FOLDER_CONTENTS = 0;
87 private final int LOADER_ID_ALL_FOLDERS = 1;
Leon Scroggins504433a2011-01-13 12:26:52 -050088 private final int LOADER_ID_FIND_ROOT = 2;
89 private final int LOADER_ID_CHECK_FOR_DUPE = 3;
Leon Scroggins2f24e992011-02-11 14:02:10 -050090 private final int LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK = 4;
91 private final int LOADER_ID_FIND_FOLDER_BY_ID = 5;
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;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400124 private static class Folder {
125 String Name;
126 long Id;
127 Folder(String name, long id) {
128 Name = name;
129 Id = id;
130 }
131 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132
Ben Murdoch1794fe22009-09-29 18:14:30 +0100133 // Message IDs
134 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400135 private static final int TOUCH_ICON_DOWNLOADED = 101;
Leon Scroggins75630672011-01-13 17:56:15 -0500136 private static final int BOOKMARK_DELETED = 102;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100137
138 private Handler mHandler;
139
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100140 private InputMethodManager getInputMethodManager() {
141 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
142 }
143
Leon Scroggins8baaa632010-12-08 19:46:53 -0500144 private Uri getUriForFolder(long folder) {
145 Uri uri;
146 if (folder == mRootFolder) {
147 uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER;
148 } else {
149 uri = BrowserContract.Bookmarks.buildFolderUri(folder);
150 }
151 String[] accountInfo = getAccountNameAndType(this);
152 if (accountInfo != null) {
153 uri = BookmarksLoader.addAccount(uri, accountInfo[1], accountInfo[0]);
154 }
155 return uri;
156 }
157
Leon Scroggins III052ce662010-09-13 14:44:16 -0400158 @Override
John Reck71efc2b2011-05-09 16:54:28 -0700159 public void onTop(BreadCrumbView view, int level, Object data) {
Leon Scroggins74dbe012010-10-15 10:54:27 -0400160 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400161 Folder folderData = (Folder) data;
162 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400163 LoaderManager manager = getLoaderManager();
164 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
165 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500166 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400167 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400168 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400169 completeOrCancelFolderNaming(true);
170 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500171 setShowBookmarkIcon(level == 1);
172 }
173
174 /**
175 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
176 * @param show True if the icon should visible, false otherwise.
177 */
178 private void setShowBookmarkIcon(boolean show) {
179 Drawable drawable = show ? mHeaderIcon: null;
180 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400181 }
182
Leon Scroggins74dbe012010-10-15 10:54:27 -0400183 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400184 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
185 if (v == mFolderNamer) {
186 if (v.getText().length() > 0) {
187 if (actionId == EditorInfo.IME_NULL) {
188 // Only want to do this once.
189 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400190 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400191 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400192 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800193 }
Michael Kolb31829b92010-10-01 11:50:21 -0700194 // Steal the key press; otherwise a newline will be added
195 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400197 return false;
198 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800199
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400200 private void switchToDefaultView(boolean changedFolder) {
201 mFolderSelector.setVisibility(View.GONE);
202 mDefaultView.setVisibility(View.VISIBLE);
203 mCrumbHolder.setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500204 mFakeTitleHolder.setVisibility(View.VISIBLE);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400205 if (changedFolder) {
206 Object data = mCrumbs.getTopData();
207 if (data != null) {
208 Folder folder = (Folder) data;
209 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500210 if (mCurrentFolder == mRootFolder) {
211 // The Spinner changed to show "Other folder ..." Change
212 // it back to "Bookmarks", which is position 0 if we are
213 // editing a folder, 1 otherwise.
Leon Scroggins504433a2011-01-13 12:26:52 -0500214 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500215 } else {
216 ((TextView) mFolder.getSelectedView()).setText(folder.Name);
217 }
218 }
219 } else {
220 // The user canceled selecting a folder. Revert back to the earlier
221 // selection.
222 if (mSaveToHomeScreen) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500223 mFolder.setSelectionIgnoringSelectionChange(0);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500224 } else {
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500225 if (mCurrentFolder == mRootFolder) {
226 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
227 } else {
228 Object data = mCrumbs.getTopData();
229 if (data != null && ((Folder) data).Id == mCurrentFolder) {
230 // We are showing the correct folder heirarchy. The
231 // folder selector will say "Other folder..." Change it
232 // to say the name of the folder once again.
233 ((TextView) mFolder.getSelectedView()).setText(((Folder) data).Name);
234 } else {
235 // We are not be showing the correct folder heirarchy.
236 // Clear the Crumbs and find the proper folder
237 mCrumbs.clear();
238 setupTopCrumb();
239 LoaderManager manager = getLoaderManager();
240 manager.restartLoader(LOADER_ID_ALL_FOLDERS, null, this);
241 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
242
243 }
244 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400245 }
246 }
247 }
248
Leon Scroggins III052ce662010-09-13 14:44:16 -0400249 @Override
250 public void onClick(View v) {
251 if (v == mButton) {
252 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400253 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400254 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400255 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700256 } else {
257 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400258 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400259 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700260 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400261 } else if (save()) {
262 finish();
263 }
264 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400265 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400266 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400267 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
268 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700269 } else {
270 finish();
271 }
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500272 } else if (v == mFolderCancel) {
273 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400274 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400275 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400276 mFolderNamer.setText(R.string.new_folder);
277 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700278 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400279 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400280 InputMethodManager imm = getInputMethodManager();
281 // Set the InputMethodManager to focus on the ListView so that it
282 // can transfer the focus to mFolderNamer.
283 imm.focusIn(mListView);
284 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
Leon Scroggins75630672011-01-13 17:56:15 -0500285 } else if (v == mRemoveLink) {
286 if (!mEditingExisting) {
287 throw new AssertionError("Remove button should not be shown for"
288 + " new bookmarks");
289 }
290 long id = mMap.getLong(BrowserContract.Bookmarks._ID);
291 createHandler();
292 Message msg = Message.obtain(mHandler, BOOKMARK_DELETED);
293 BookmarkUtils.displayRemoveBookmarkDialog(id,
294 mTitle.getText().toString(), this, msg);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800295 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400296 }
297
Leon Scroggins504433a2011-01-13 12:26:52 -0500298 // FolderSpinner.OnSetSelectionListener
299
Leon Scroggins88d08032010-10-21 15:17:10 -0400300 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500301 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500302 int intId = (int) id;
303 switch (intId) {
304 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400305 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400306 mSaveToHomeScreen = false;
307 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500308 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400309 // Create a short cut to the home screen
310 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400311 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500312 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400313 switchToFolderSelector();
314 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500315 case FolderSpinnerAdapter.RECENT_FOLDER:
316 mCurrentFolder = mFolderAdapter.recentFolderId();
317 mSaveToHomeScreen = false;
318 // In case the user decides to select OTHER_FOLDER
319 // and choose a different one, so that we will start from
320 // the correct place.
321 LoaderManager manager = getLoaderManager();
322 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
323 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins0b95ad42011-02-23 15:23:48 -0500324 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400325 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500326 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400327 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500328 }
329
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500330 /**
331 * Finish naming a folder, and close the IME
332 * @param cancel If true, the new folder is not created. If false, the new
333 * folder is created and the user is taken inside it.
334 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400335 private void completeOrCancelFolderNaming(boolean cancel) {
336 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700337 String name = mFolderNamer.getText().toString();
338 long id = addFolderToCurrent(mFolderNamer.getText().toString());
339 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700340 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400341 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400342 mAddNewFolder.setVisibility(View.VISIBLE);
343 mAddSeparator.setVisibility(View.VISIBLE);
344 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400345 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700346 }
347
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700348 private long addFolderToCurrent(String name) {
349 // Add the folder to the database
350 ContentValues values = new ContentValues();
351 values.put(BrowserContract.Bookmarks.TITLE,
352 name);
353 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500354 String[] accountInfo = getAccountNameAndType(this);
355 if (accountInfo != null) {
356 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
357 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700358 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400359 long currentFolder;
360 Object data = mCrumbs.getTopData();
361 if (data != null) {
362 currentFolder = ((Folder) data).Id;
363 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400364 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400365 }
366 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700367 Uri uri = getContentResolver().insert(
368 BrowserContract.Bookmarks.CONTENT_URI, values);
369 if (uri != null) {
370 return ContentUris.parseId(uri);
371 } else {
372 return -1;
373 }
374 }
375
Leon Scroggins III052ce662010-09-13 14:44:16 -0400376 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500377 // Set the list to the top in case it is scrolled.
378 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400379 mDefaultView.setVisibility(View.GONE);
380 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400381 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500382 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400383 mAddNewFolder.setVisibility(View.VISIBLE);
384 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400385 }
386
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700387 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700388 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400389 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400390 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700391 }
392 }
393
Leon Scroggins III052ce662010-09-13 14:44:16 -0400394 @Override
395 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
396 String[] projection;
397 switch (id) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500398 case LOADER_ID_CHECK_FOR_DUPE:
399 projection = new String[] {
400 BrowserContract.Bookmarks._ID,
401 BrowserContract.Bookmarks.PARENT,
402 BrowserContract.Bookmarks.TITLE
403 };
404 return new CursorLoader(this,
405 BookmarkUtils.getBookmarksUri(this),
406 projection,
407 BrowserContract.Bookmarks.URL + " = ?",
408 new String[] { mOriginalUrl },
409 null);
410 case LOADER_ID_FIND_ROOT:
411 String name = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME);
412 String type = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE);
413
414 projection = new String[] { BrowserContract.Bookmarks._ID };
415 String selection = BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
416 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
417 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?";
418 String[] selArgs = new String[] {
419 BrowserContract.ChromeSyncColumns.FOLDER_NAME_BOOKMARKS_BAR,
420 name,
421 type
422 };
423 return new CursorLoader(this,
424 BrowserContract.Bookmarks.CONTENT_URI,
425 projection,
426 selection,
427 selArgs,
428 null);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500429 case LOADER_ID_FIND_FOLDER_BY_ID:
430 projection = new String[] {
431 BrowserContract.Bookmarks._ID,
432 BrowserContract.Bookmarks.TITLE
433 };
434 return new CursorLoader(this,
Leon Scroggins3ba3e482011-02-23 12:08:09 -0500435 BookmarkUtils.getBookmarksUri(this),
Leon Scroggins2f24e992011-02-11 14:02:10 -0500436 projection,
437 BrowserContract.Bookmarks._ID + " = "
438 + args.getLong(BrowserContract.Bookmarks._ID),
439 null,
440 null);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400441 case LOADER_ID_ALL_FOLDERS:
442 projection = new String[] {
443 BrowserContract.Bookmarks._ID,
444 BrowserContract.Bookmarks.PARENT,
445 BrowserContract.Bookmarks.TITLE,
446 BrowserContract.Bookmarks.IS_FOLDER
447 };
448 return new CursorLoader(this,
Leon Scroggins3ba3e482011-02-23 12:08:09 -0500449 BookmarkUtils.getBookmarksUri(this),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400450 projection,
451 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
452 null,
453 null);
454 case LOADER_ID_FOLDER_CONTENTS:
455 projection = new String[] {
456 BrowserContract.Bookmarks._ID,
457 BrowserContract.Bookmarks.TITLE,
458 BrowserContract.Bookmarks.IS_FOLDER
459 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500460 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
461 if (mEditingFolder) {
462 where += " AND " + BrowserContract.Bookmarks._ID + " != "
463 + mMap.getLong(BrowserContract.Bookmarks._ID);
464 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400465 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500466 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400467 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500468 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400469 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500470 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins2f24e992011-02-11 14:02:10 -0500471 case LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK:
472 projection = new String[] {
473 BrowserContract.Bookmarks.PARENT
474 };
475 return new CursorLoader(this,
Leon Scroggins3ba3e482011-02-23 12:08:09 -0500476 BookmarkUtils.getBookmarksUri(this),
Leon Scroggins2f24e992011-02-11 14:02:10 -0500477 projection,
478 BrowserContract.Bookmarks.IS_FOLDER + " = 0",
479 null,
480 BrowserContract.Bookmarks.DATE_CREATED + " DESC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400481 default:
482 throw new AssertionError("Asking for nonexistant loader!");
483 }
484 }
485
486 @Override
487 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
488 switch (loader.getId()) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500489 case LOADER_ID_CHECK_FOR_DUPE:
490 if (cursor != null && cursor.moveToFirst()) {
491 // Site is bookmarked.
492 mEditingExisting = true;
Leon Scroggins75630672011-01-13 17:56:15 -0500493 showRemoveButton();
Leon Scroggins504433a2011-01-13 12:26:52 -0500494 mFakeTitle.setText(R.string.edit_bookmark);
495 int index = cursor.getColumnIndexOrThrow(
496 BrowserContract.Bookmarks.PARENT);
497 mCurrentFolder = cursor.getLong(index);
498 index = cursor.getColumnIndexOrThrow(
499 BrowserContract.Bookmarks.TITLE);
500 String title = cursor.getString(index);
501 mTitle.setText(title);
502 index = cursor.getColumnIndexOrThrow(
503 BrowserContract.Bookmarks._ID);
504 long id = cursor.getLong(index);
505 mMap.putLong(BrowserContract.Bookmarks._ID, id);
506 }
507 onCurrentFolderFound();
508 getLoaderManager().destroyLoader(LOADER_ID_CHECK_FOR_DUPE);
509 break;
510 case LOADER_ID_FIND_ROOT:
511 long root;
512 if (cursor != null && cursor.moveToFirst()) {
513 root = cursor.getLong(0);
514 } else {
515 root = BrowserProvider2.FIXED_ID_ROOT;
516 }
517 onRootFolderFound(root);
518 getLoaderManager().destroyLoader(LOADER_ID_FIND_ROOT);
519 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400520 case LOADER_ID_FOLDER_CONTENTS:
521 mAdapter.changeCursor(cursor);
522 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500523 case LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK:
524 LoaderManager manager = getLoaderManager();
525 if (cursor != null && cursor.moveToFirst()) {
526 // Find the parent
527 long lastUsedFolder = cursor.getLong(0);
528 if (lastUsedFolder != mRootFolder
529 && lastUsedFolder != mCurrentFolder
530 && lastUsedFolder != 0) {
531 // Find out the parent's name
532 Bundle b = new Bundle();
533 b.putLong(BrowserContract.Bookmarks._ID, lastUsedFolder);
534 manager.initLoader(LOADER_ID_FIND_FOLDER_BY_ID, b, this);
535 }
536 }
537 manager.destroyLoader(LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK);
538 break;
539 case LOADER_ID_FIND_FOLDER_BY_ID:
540 if (cursor != null && cursor.moveToFirst()) {
541 long id = cursor.getLong(cursor.getColumnIndexOrThrow(
542 BrowserContract.Bookmarks._ID));
543 String title = cursor.getString(cursor.getColumnIndexOrThrow(
544 BrowserContract.Bookmarks.TITLE));
545 mFolderAdapter.addRecentFolder(id, title);
546 }
547 getLoaderManager().destroyLoader(LOADER_ID_FIND_FOLDER_BY_ID);
548 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400549 case LOADER_ID_ALL_FOLDERS:
550 long parent = mCurrentFolder;
551 int idIndex = cursor.getColumnIndexOrThrow(
552 BrowserContract.Bookmarks._ID);
553 int titleIndex = cursor.getColumnIndexOrThrow(
554 BrowserContract.Bookmarks.TITLE);
555 int parentIndex = cursor.getColumnIndexOrThrow(
556 BrowserContract.Bookmarks.PARENT);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500557 // If the user is editing anything inside the "Other Bookmarks"
558 // folder, we need to stop searching up when we reach its parent.
559 // Find the root folder
560 moveCursorToFolder(cursor, mRootFolder, idIndex);
561 // omniparent is the folder which contains root, and therefore
562 // also the parent of the "Other Bookmarks" folder.
563 long omniparent = cursor.getLong(parentIndex);
564 Stack<Folder> folderStack = new Stack<Folder>();
565 while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400566 // First, find the folder corresponding to the current
567 // folder
Leon Scroggins8baaa632010-12-08 19:46:53 -0500568 moveCursorToFolder(cursor, parent, idIndex);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400569 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400570 if (parent == mCurrentFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500571 ((TextView) mFolder.getSelectedView()).setText(name);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400572 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400573 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400574 parent = cursor.getLong(parentIndex);
575 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400576 while (!folderStack.isEmpty()) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500577 Folder thisFolder = folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400578 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400579 }
Dianne Hackborn71e76c72010-12-20 11:44:09 -0800580 getLoaderManager().destroyLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400581 break;
582 default:
583 break;
584 }
585 }
586
Dianne Hackborn39772c82010-12-16 00:43:54 -0800587 public void onLoaderReset(Loader<Cursor> loader) {
588 switch (loader.getId()) {
589 case LOADER_ID_FOLDER_CONTENTS:
590 mAdapter.changeCursor(null);
591 break;
592 }
593 }
594
Leon Scroggins02081942010-11-01 17:52:42 -0400595 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500596 * Move cursor to the position that has folderToFind as its "_id".
597 * @param cursor Cursor containing folders in the bookmarks database
598 * @param folderToFind "_id" of the folder to move to.
599 * @param idIndex Index in cursor of "_id"
600 * @throws AssertionError if cursor is empty or there is no row with folderToFind
601 * as its "_id".
602 */
603 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
604 throws AssertionError {
605 if (!cursor.moveToFirst()) {
606 throw new AssertionError("No folders in the database!");
607 }
608 long folder;
609 do {
610 folder = cursor.getLong(idIndex);
611 } while (folder != folderToFind && cursor.moveToNext());
612 if (cursor.isAfterLast()) {
613 throw new AssertionError("Folder(id=" + folderToFind
614 + ") holding this bookmark does not exist!");
615 }
616 }
617
Leon Scroggins III052ce662010-09-13 14:44:16 -0400618 @Override
619 public void onItemClick(AdapterView<?> parent, View view, int position,
620 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400621 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400622 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400623 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400624 }
625
Leon Scroggins162f8352010-10-18 15:02:44 -0400626 private void setShowFolderNamer(boolean show) {
627 if (show != mIsFolderNamerShowing) {
628 mIsFolderNamerShowing = show;
629 if (show) {
630 // Set the selection to the folder namer so it will be in
631 // view.
632 mListView.addFooterView(mFolderNamerHolder);
633 } else {
634 mListView.removeFooterView(mFolderNamerHolder);
635 }
636 // Refresh the list.
637 mListView.setAdapter(mAdapter);
638 if (show) {
639 mListView.setSelection(mListView.getCount() - 1);
640 }
641 }
642 }
643
Leon Scroggins III052ce662010-09-13 14:44:16 -0400644 /**
645 * Shows a list of names of folders.
646 */
647 private class FolderAdapter extends CursorAdapter {
648 public FolderAdapter(Context context) {
649 super(context, null);
650 }
651
652 @Override
653 public void bindView(View view, Context context, Cursor cursor) {
654 ((TextView) view.findViewById(android.R.id.text1)).setText(
655 cursor.getString(cursor.getColumnIndexOrThrow(
656 BrowserContract.Bookmarks.TITLE)));
657 }
658
659 @Override
660 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700661 View view = LayoutInflater.from(context).inflate(
662 R.layout.folder_list_item, null);
663 view.setBackgroundDrawable(context.getResources().
664 getDrawable(android.R.drawable.list_selector_background));
665 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400666 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400667
668 @Override
669 public boolean isEmpty() {
670 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400671 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400672 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400673 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674
Leon Scrogginsc1129902010-12-08 15:28:33 -0500675 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800676 protected void onCreate(Bundle icicle) {
677 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500678 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100679
680 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100681
Leon Scroggins III052ce662010-09-13 14:44:16 -0400682 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100683
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400684 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700685
The Android Open Source Project0c908882009-03-03 19:32:16 -0800686 String title = null;
687 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100688
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400689 mFakeTitle = (TextView) findViewById(R.id.fake_title);
690
The Android Open Source Project0c908882009-03-03 19:32:16 -0800691 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800692 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800693 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800694 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800695 mMap = b;
696 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400697 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800698 if (mEditingFolder) {
699 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500700 } else {
701 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800702 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400703 } else {
704 int gravity = mMap.getInt("gravity", -1);
705 if (gravity != -1) {
706 WindowManager.LayoutParams l = window.getAttributes();
707 l.gravity = gravity;
708 window.setAttributes(l);
709 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800710 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400711 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
712 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
713 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700714 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400715 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800716
717 mTitle = (EditText) findViewById(R.id.title);
718 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100719
Leon Scroggins III052ce662010-09-13 14:44:16 -0400720 mAddress = (EditText) findViewById(R.id.address);
721 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800722
The Android Open Source Project0c908882009-03-03 19:32:16 -0800723 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400724 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800725
726 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400727 mCancelButton.setOnClickListener(this);
728
Leon Scroggins504433a2011-01-13 12:26:52 -0500729 mFolder = (FolderSpinner) findViewById(R.id.folder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500730 mFolderAdapter = new FolderSpinnerAdapter(!mEditingFolder);
731 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500732 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400733
734 mDefaultView = findViewById(R.id.default_view);
735 mFolderSelector = findViewById(R.id.folder_selector);
736
Leon Scroggins162f8352010-10-18 15:02:44 -0400737 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
738 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400739 mFolderNamer.setOnEditorActionListener(this);
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500740 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
741 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400742
743 mAddNewFolder = findViewById(R.id.add_new_folder);
744 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400745 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400746
Leon Scroggins74dbe012010-10-15 10:54:27 -0400747 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
748 mCrumbs.setUseBackButton(true);
749 mCrumbs.setController(this);
Michael Kolb5a72f182011-01-13 20:35:06 -0800750 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400751 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800752 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400753
Leon Scroggins III052ce662010-09-13 14:44:16 -0400754 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400755 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400756 View empty = findViewById(R.id.empty);
757 mListView.setEmptyView(empty);
758 mListView.setAdapter(mAdapter);
759 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400760 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500761
Leon Scroggins75630672011-01-13 17:56:15 -0500762 mFakeTitleHolder = findViewById(R.id.title_holder);
763
Leon Scroggins504433a2011-01-13 12:26:52 -0500764 if (!window.getDecorView().isInTouchMode()) {
765 mButton.requestFocus();
766 }
767
768 String[] accountInfo = getAccountNameAndType(this);
769 if (accountInfo == null) {
770 onRootFolderFound(BrowserProvider2.FIXED_ID_ROOT);
771 } else {
772 Bundle args = new Bundle();
773 args.putString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, accountInfo[0]);
774 args.putString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, accountInfo[1]);
775 getLoaderManager().initLoader(LOADER_ID_FIND_ROOT, args, this);
776 }
777
778 }
779
Leon Scroggins75630672011-01-13 17:56:15 -0500780 private void showRemoveButton() {
781 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
782 mRemoveLink = findViewById(R.id.remove);
783 mRemoveLink.setVisibility(View.VISIBLE);
784 mRemoveLink.setOnClickListener(this);
785 }
786
Leon Scroggins504433a2011-01-13 12:26:52 -0500787 // Called once we have determined which folder is the root folder
788 private void onRootFolderFound(long root) {
789 mRootFolder = root;
790 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
791 mCurrentFolder = mRootFolder;
792 }
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500793 setupTopCrumb();
Leon Scrogginsbdff8a72011-02-11 15:49:04 -0500794 if (mEditingExisting || TextUtils.isEmpty(mOriginalUrl)
795 || !(mMap != null && mMap.getBoolean(CHECK_FOR_DUPE))) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500796 onCurrentFolderFound();
797 } else {
798 // User is attempting to bookmark a site, rather than deliberately
799 // editing a bookmark. Rather than let them create a duplicate
800 // bookmark, see if the bookmark already exists.
801 getLoaderManager().initLoader(LOADER_ID_CHECK_FOR_DUPE, null, this);
802 }
803 }
804
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500805 private void setupTopCrumb() {
806 String name = getString(R.string.bookmarks);
807 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
808 new Folder(name, mRootFolder));
809 // To better match the other folders.
810 mTopLevelLabel.setCompoundDrawablePadding(6);
811 }
812
Leon Scroggins504433a2011-01-13 12:26:52 -0500813 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400814 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500815 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400816 // Find all the folders
817 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500818 // Since we're not in the root folder, change the selection to other
819 // folder now. The text will get changed once we select the correct
820 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500821 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500822 } else {
823 setShowBookmarkIcon(true);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500824 if (!mEditingExisting) {
825 // Find the most recently saved bookmark, so that we can include it in
826 // the list of options to save the current bookmark.
827 manager.initLoader(LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK, null, this);
828 }
Leon Scroggins504433a2011-01-13 12:26:52 -0500829 if (!mEditingFolder) {
830 // Initially the "Bookmarks" folder should be showing, rather than
831 // the home screen. In the editing folder case, home screen is not
832 // an option, so "Bookmarks" folder is already at the top.
833 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
834 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400835 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400836 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400837 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500838}
Leon Scroggins8baaa632010-12-08 19:46:53 -0500839 /**
840 * Get the account name and type of the currently synced account.
841 * @param context Context to access preferences.
842 * @return null if no account name or type. Otherwise, the result will be
843 * an array of two Strings, the accountName and accountType, respectively.
844 */
845 private String[] getAccountNameAndType(Context context) {
846 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
847 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
848 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
849 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
850 return null;
851 }
852 return new String[] { accountName, accountType };
853 }
854
Leon Scroggins02065b02010-01-04 14:30:13 -0500855 /**
856 * Runnable to save a bookmark, so it can be performed in its own thread.
857 */
858 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400859 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500860 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200861 private Context mContext;
862 public SaveBookmarkRunnable(Context ctx, Message msg) {
863 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500864 mMessage = msg;
865 }
866 public void run() {
867 // Unbundle bookmark data.
868 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400869 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
870 String url = bundle.getString(BrowserContract.Bookmarks.URL);
871 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500872 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400873 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
874 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500875
876 // Save to the bookmarks DB.
877 try {
878 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400879 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
880 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500881 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200882 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500883 }
884 mMessage.arg1 = 1;
885 } catch (IllegalStateException e) {
886 mMessage.arg1 = 0;
887 }
888 mMessage.sendToTarget();
889 }
890 }
891
John Reckc8490812010-11-22 14:15:36 -0800892 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
893 Context mContext;
894 Long mId;
895
896 public UpdateBookmarkTask(Context context, long id) {
897 mContext = context;
898 mId = id;
899 }
900
901 @Override
902 protected Void doInBackground(ContentValues... params) {
903 if (params.length != 1) {
904 throw new IllegalArgumentException("No ContentValues provided!");
905 }
906 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
907 mContext.getContentResolver().update(
908 uri,
909 params[0], null, null);
910 return null;
911 }
912 }
913
Ben Murdoch1794fe22009-09-29 18:14:30 +0100914 private void createHandler() {
915 if (mHandler == null) {
916 mHandler = new Handler() {
917 @Override
918 public void handleMessage(Message msg) {
919 switch (msg.what) {
920 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500921 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100922 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
923 Toast.LENGTH_LONG).show();
924 } else {
925 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
926 Toast.LENGTH_LONG).show();
927 }
928 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400929 case TOUCH_ICON_DOWNLOADED:
930 Bundle b = msg.getData();
931 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400932 AddBookmarkPage.this,
933 b.getString(BrowserContract.Bookmarks.URL),
934 b.getString(BrowserContract.Bookmarks.TITLE),
935 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
936 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400937 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500938 case BOOKMARK_DELETED:
939 finish();
940 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100941 }
942 }
943 };
944 }
945 }
946
The Android Open Source Project0c908882009-03-03 19:32:16 -0800947 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100948 * 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 -0800949 */
950 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100951 createHandler();
952
The Android Open Source Project0c908882009-03-03 19:32:16 -0800953 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100954 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700955 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100956
The Android Open Source Project0c908882009-03-03 19:32:16 -0800957 boolean emptyTitle = title.length() == 0;
958 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
959 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800960 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800961 if (emptyTitle) {
962 mTitle.setError(r.getText(R.string.bookmark_needs_title));
963 }
964 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400965 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800966 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400967 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100968
The Android Open Source Project0c908882009-03-03 19:32:16 -0800969 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000970 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800971 if (!mEditingFolder) {
972 try {
973 // We allow bookmarks with a javascript: scheme, but these will in most cases
974 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000975
John Reckc8490812010-11-22 14:15:36 -0800976 if (!url.toLowerCase().startsWith("javascript:")) {
977 URI uriObj = new URI(url);
978 String scheme = uriObj.getScheme();
979 if (!Bookmarks.urlHasAcceptableScheme(url)) {
980 // If the scheme was non-null, let the user know that we
981 // can't save their bookmark. If it was null, we'll assume
982 // they meant http when we parse it in the WebAddress class.
983 if (scheme != null) {
984 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
985 return false;
986 }
987 WebAddress address;
988 try {
989 address = new WebAddress(unfilteredUrl);
990 } catch (ParseException e) {
991 throw new URISyntaxException("", "");
992 }
993 if (address.getHost().length() == 0) {
994 throw new URISyntaxException("", "");
995 }
996 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000997 }
Ben Murdochde353622009-10-12 10:29:00 +0100998 }
John Reckc8490812010-11-22 14:15:36 -0800999 } catch (URISyntaxException e) {
1000 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1001 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001002 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001003 }
Ben Murdochaac7aa62009-09-17 16:57:40 +01001004
Leon Scroggins88d08032010-10-21 15:17:10 -04001005 if (mSaveToHomeScreen) {
1006 mEditingExisting = false;
1007 }
1008
1009 boolean urlUnmodified = url.equals(mOriginalUrl);
1010
Ben Murdoch1794fe22009-09-29 18:14:30 +01001011 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -08001012 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
1013 ContentValues values = new ContentValues();
1014 values.put(BrowserContract.Bookmarks.TITLE, title);
1015 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
1016 if (!mEditingFolder) {
1017 values.put(BrowserContract.Bookmarks.URL, url);
1018 if (!urlUnmodified) {
1019 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
1020 }
1021 }
1022 if (values.size() > 0) {
1023 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
1024 }
1025 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +01001026 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -04001027 Bitmap thumbnail;
1028 Bitmap favicon;
1029 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001030 thumbnail = (Bitmap) mMap.getParcelable(
1031 BrowserContract.Bookmarks.THUMBNAIL);
1032 favicon = (Bitmap) mMap.getParcelable(
1033 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -04001034 } else {
1035 thumbnail = null;
1036 favicon = null;
1037 }
1038
Ben Murdoch1794fe22009-09-29 18:14:30 +01001039 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001040 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
1041 bundle.putString(BrowserContract.Bookmarks.URL, url);
1042 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -04001043
1044 if (mSaveToHomeScreen) {
1045 if (mTouchIconUrl != null && urlUnmodified) {
1046 Message msg = Message.obtain(mHandler,
1047 TOUCH_ICON_DOWNLOADED);
1048 msg.setData(bundle);
1049 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -04001050 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -04001051 icon.execute(mTouchIconUrl);
1052 } else {
1053 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
1054 title, null /*touchIcon*/, favicon));
1055 }
1056 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001057 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
1058 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
1059 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -04001060 // Post a message to write to the DB.
1061 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
1062 msg.setData(bundle);
1063 // Start a new thread so as to not slow down the UI
1064 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
1065 t.start();
1066 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001067 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001068 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001069 }
1070 return true;
1071 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001072
1073 /*
1074 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1075 */
1076 public static class CustomListView extends ListView {
1077 private EditText mEditText;
1078
1079 public void addEditText(EditText editText) {
1080 mEditText = editText;
1081 }
1082
1083 public CustomListView(Context context) {
1084 super(context);
1085 }
1086
1087 public CustomListView(Context context, AttributeSet attrs) {
1088 super(context, attrs);
1089 }
1090
1091 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1092 super(context, attrs, defStyle);
1093 }
1094
1095 @Override
1096 public boolean checkInputConnectionProxy(View view) {
1097 return view == mEditText;
1098 }
1099 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001100}