blob: b8fe45e3366921f7ea71b3bcee8210ac5c3084c8 [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 Reck37894a92011-05-13 12:47:53 -0700408 // TODO: Detect if lastUsedId is a subfolder of info.id in the
409 // editing folder case. For now, just don't show the last used
410 // folder at all to prevent any chance of the user adding a parent
411 // folder to a child folder
412 if (info.lastUsedId != -1 && info.lastUsedId != info.id
413 && !mEditingFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700414 if (setAccount && info.lastUsedId != mRootFolder
415 && TextUtils.equals(info.lastUsedAccountName, info.accountName)
416 && TextUtils.equals(info.lastUsedAccountType, info.accountType)) {
417 mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle);
418 } else if (!setAccount) {
419 setAccount = true;
420 setAccount(info.lastUsedAccountName, info.lastUsedAccountType);
421 if (info.lastUsedId != mRootFolder) {
422 mFolderAdapter.addRecentFolder(info.lastUsedId,
423 info.lastUsedTitle);
424 }
425 }
426 }
427 if (!setAccount) {
428 mAccountSpinner.setSelection(0);
429 }
430 }
431
432 @Override
433 public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) {
434 return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap);
435 }
436 };
437
438 void setAccount(String accountName, String accountType) {
439 for (int i = 0; i < mAccountAdapter.getCount(); i++) {
440 BookmarkAccount account = mAccountAdapter.getItem(i);
441 if (TextUtils.equals(account.accountName, accountName)
442 && TextUtils.equals(account.accountType, accountType)) {
443 onRootFolderFound(account.rootFolderId);
444 mAccountSpinner.setSelection(i);
445 return;
446 }
447 }
448 }
449
Leon Scroggins III052ce662010-09-13 14:44:16 -0400450 @Override
451 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
452 String[] projection;
453 switch (id) {
John Reck2eec4c32011-05-11 15:55:32 -0700454 case LOADER_ID_ACCOUNTS:
455 return new AccountsLoader(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400456 case LOADER_ID_FOLDER_CONTENTS:
457 projection = new String[] {
458 BrowserContract.Bookmarks._ID,
459 BrowserContract.Bookmarks.TITLE,
460 BrowserContract.Bookmarks.IS_FOLDER
461 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500462 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
John Reck1dd8cd42011-05-13 11:22:09 -0700463 String whereArgs[] = null;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500464 if (mEditingFolder) {
John Reck1dd8cd42011-05-13 11:22:09 -0700465 where += " AND " + BrowserContract.Bookmarks._ID + " != ?";
466 whereArgs = new String[] { Long.toString(mMap.getLong(
467 BrowserContract.Bookmarks._ID)) };
468 }
469 long currentFolder;
470 Object data = mCrumbs.getTopData();
471 if (data != null) {
472 currentFolder = ((Folder) data).Id;
473 } else {
474 currentFolder = mRootFolder;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500475 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400476 return new CursorLoader(this,
John Reck1dd8cd42011-05-13 11:22:09 -0700477 getUriForFolder(currentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400478 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500479 where,
John Reck1dd8cd42011-05-13 11:22:09 -0700480 whereArgs,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500481 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400482 default:
483 throw new AssertionError("Asking for nonexistant loader!");
484 }
485 }
486
487 @Override
488 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
489 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700490 case LOADER_ID_ACCOUNTS:
491 mAccountAdapter.clear();
492 while (cursor.moveToNext()) {
493 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500494 }
John Reck2eec4c32011-05-11 15:55:32 -0700495 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
496 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
497 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500498 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400499 case LOADER_ID_FOLDER_CONTENTS:
500 mAdapter.changeCursor(cursor);
501 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400502 }
503 }
504
Dianne Hackborn39772c82010-12-16 00:43:54 -0800505 public void onLoaderReset(Loader<Cursor> loader) {
506 switch (loader.getId()) {
507 case LOADER_ID_FOLDER_CONTENTS:
508 mAdapter.changeCursor(null);
509 break;
510 }
511 }
512
Leon Scroggins02081942010-11-01 17:52:42 -0400513 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500514 * Move cursor to the position that has folderToFind as its "_id".
515 * @param cursor Cursor containing folders in the bookmarks database
516 * @param folderToFind "_id" of the folder to move to.
517 * @param idIndex Index in cursor of "_id"
518 * @throws AssertionError if cursor is empty or there is no row with folderToFind
519 * as its "_id".
520 */
521 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
522 throws AssertionError {
523 if (!cursor.moveToFirst()) {
524 throw new AssertionError("No folders in the database!");
525 }
526 long folder;
527 do {
528 folder = cursor.getLong(idIndex);
529 } while (folder != folderToFind && cursor.moveToNext());
530 if (cursor.isAfterLast()) {
531 throw new AssertionError("Folder(id=" + folderToFind
532 + ") holding this bookmark does not exist!");
533 }
534 }
535
Leon Scroggins III052ce662010-09-13 14:44:16 -0400536 @Override
537 public void onItemClick(AdapterView<?> parent, View view, int position,
538 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400539 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400540 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400541 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400542 }
543
Leon Scroggins162f8352010-10-18 15:02:44 -0400544 private void setShowFolderNamer(boolean show) {
545 if (show != mIsFolderNamerShowing) {
546 mIsFolderNamerShowing = show;
547 if (show) {
548 // Set the selection to the folder namer so it will be in
549 // view.
550 mListView.addFooterView(mFolderNamerHolder);
551 } else {
552 mListView.removeFooterView(mFolderNamerHolder);
553 }
554 // Refresh the list.
555 mListView.setAdapter(mAdapter);
556 if (show) {
557 mListView.setSelection(mListView.getCount() - 1);
558 }
559 }
560 }
561
Leon Scroggins III052ce662010-09-13 14:44:16 -0400562 /**
563 * Shows a list of names of folders.
564 */
565 private class FolderAdapter extends CursorAdapter {
566 public FolderAdapter(Context context) {
567 super(context, null);
568 }
569
570 @Override
571 public void bindView(View view, Context context, Cursor cursor) {
572 ((TextView) view.findViewById(android.R.id.text1)).setText(
573 cursor.getString(cursor.getColumnIndexOrThrow(
574 BrowserContract.Bookmarks.TITLE)));
575 }
576
577 @Override
578 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700579 View view = LayoutInflater.from(context).inflate(
580 R.layout.folder_list_item, null);
581 view.setBackgroundDrawable(context.getResources().
582 getDrawable(android.R.drawable.list_selector_background));
583 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400584 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400585
586 @Override
587 public boolean isEmpty() {
588 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400589 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400590 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400591 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800592
Leon Scrogginsc1129902010-12-08 15:28:33 -0500593 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800594 protected void onCreate(Bundle icicle) {
595 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500596 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100597
598 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100599
Leon Scroggins III052ce662010-09-13 14:44:16 -0400600 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100601
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400602 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700603
The Android Open Source Project0c908882009-03-03 19:32:16 -0800604 String title = null;
605 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100606
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400607 mFakeTitle = (TextView) findViewById(R.id.fake_title);
608
The Android Open Source Project0c908882009-03-03 19:32:16 -0800609 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800610 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800611 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800612 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800613 mMap = b;
614 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400615 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800616 if (mEditingFolder) {
617 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500618 } else {
619 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800620 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400621 } else {
622 int gravity = mMap.getInt("gravity", -1);
623 if (gravity != -1) {
624 WindowManager.LayoutParams l = window.getAttributes();
625 l.gravity = gravity;
626 window.setAttributes(l);
627 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800628 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400629 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
630 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
631 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700632 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400633 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800634
635 mTitle = (EditText) findViewById(R.id.title);
636 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100637
Leon Scroggins III052ce662010-09-13 14:44:16 -0400638 mAddress = (EditText) findViewById(R.id.address);
639 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800640
The Android Open Source Project0c908882009-03-03 19:32:16 -0800641 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400642 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643
644 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400645 mCancelButton.setOnClickListener(this);
646
Leon Scroggins504433a2011-01-13 12:26:52 -0500647 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700648 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500649 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500650 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400651
652 mDefaultView = findViewById(R.id.default_view);
653 mFolderSelector = findViewById(R.id.folder_selector);
654
Leon Scroggins162f8352010-10-18 15:02:44 -0400655 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
656 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400657 mFolderNamer.setOnEditorActionListener(this);
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500658 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
659 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400660
661 mAddNewFolder = findViewById(R.id.add_new_folder);
662 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400663 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400664
Leon Scroggins74dbe012010-10-15 10:54:27 -0400665 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
666 mCrumbs.setUseBackButton(true);
667 mCrumbs.setController(this);
Michael Kolb5a72f182011-01-13 20:35:06 -0800668 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400669 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800670 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400671
Leon Scroggins III052ce662010-09-13 14:44:16 -0400672 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400673 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400674 View empty = findViewById(R.id.empty);
675 mListView.setEmptyView(empty);
676 mListView.setAdapter(mAdapter);
677 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400678 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500679
John Reck2eec4c32011-05-11 15:55:32 -0700680 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
681 android.R.layout.simple_spinner_item);
682 mAccountAdapter.setDropDownViewResource(
683 android.R.layout.simple_spinner_dropdown_item);
684 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
685 mAccountSpinner.setAdapter(mAccountAdapter);
686 mAccountSpinner.setOnItemSelectedListener(this);
687
688
Leon Scroggins75630672011-01-13 17:56:15 -0500689 mFakeTitleHolder = findViewById(R.id.title_holder);
690
Leon Scroggins504433a2011-01-13 12:26:52 -0500691 if (!window.getDecorView().isInTouchMode()) {
692 mButton.requestFocus();
693 }
694
John Reck2eec4c32011-05-11 15:55:32 -0700695 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500696 }
697
Leon Scroggins75630672011-01-13 17:56:15 -0500698 private void showRemoveButton() {
699 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
700 mRemoveLink = findViewById(R.id.remove);
701 mRemoveLink.setVisibility(View.VISIBLE);
702 mRemoveLink.setOnClickListener(this);
703 }
704
Leon Scroggins504433a2011-01-13 12:26:52 -0500705 // Called once we have determined which folder is the root folder
706 private void onRootFolderFound(long root) {
707 mRootFolder = root;
John Reck2eec4c32011-05-11 15:55:32 -0700708 mCurrentFolder = mRootFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500709 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700710 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500711 }
712
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500713 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700714 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500715 String name = getString(R.string.bookmarks);
716 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
717 new Folder(name, mRootFolder));
718 // To better match the other folders.
719 mTopLevelLabel.setCompoundDrawablePadding(6);
720 }
721
Leon Scroggins504433a2011-01-13 12:26:52 -0500722 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400723 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500724 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500725 // Since we're not in the root folder, change the selection to other
726 // folder now. The text will get changed once we select the correct
727 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500728 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500729 } else {
730 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500731 if (!mEditingFolder) {
732 // Initially the "Bookmarks" folder should be showing, rather than
733 // the home screen. In the editing folder case, home screen is not
734 // an option, so "Bookmarks" folder is already at the top.
735 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
736 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400737 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400738 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700739 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500740}
Leon Scroggins8baaa632010-12-08 19:46:53 -0500741 /**
742 * Get the account name and type of the currently synced account.
Leon Scroggins8baaa632010-12-08 19:46:53 -0500743 * @return null if no account name or type. Otherwise, the result will be
744 * an array of two Strings, the accountName and accountType, respectively.
745 */
John Reck2eec4c32011-05-11 15:55:32 -0700746 private String[] getAccountNameAndType() {
747 BookmarkAccount account = (BookmarkAccount) mAccountSpinner.getSelectedItem();
748 if (account == null) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500749 return null;
750 }
John Reck2eec4c32011-05-11 15:55:32 -0700751 return new String[] { account.accountName, account.accountType };
Leon Scroggins8baaa632010-12-08 19:46:53 -0500752 }
753
Leon Scroggins02065b02010-01-04 14:30:13 -0500754 /**
755 * Runnable to save a bookmark, so it can be performed in its own thread.
756 */
757 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400758 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500759 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200760 private Context mContext;
761 public SaveBookmarkRunnable(Context ctx, Message msg) {
762 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500763 mMessage = msg;
764 }
765 public void run() {
766 // Unbundle bookmark data.
767 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400768 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
769 String url = bundle.getString(BrowserContract.Bookmarks.URL);
770 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500771 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400772 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
773 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500774
775 // Save to the bookmarks DB.
776 try {
777 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400778 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
779 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500780 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200781 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500782 }
783 mMessage.arg1 = 1;
784 } catch (IllegalStateException e) {
785 mMessage.arg1 = 0;
786 }
787 mMessage.sendToTarget();
788 }
789 }
790
John Reckc8490812010-11-22 14:15:36 -0800791 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
792 Context mContext;
793 Long mId;
794
795 public UpdateBookmarkTask(Context context, long id) {
796 mContext = context;
797 mId = id;
798 }
799
800 @Override
801 protected Void doInBackground(ContentValues... params) {
802 if (params.length != 1) {
803 throw new IllegalArgumentException("No ContentValues provided!");
804 }
805 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
806 mContext.getContentResolver().update(
807 uri,
808 params[0], null, null);
809 return null;
810 }
811 }
812
Ben Murdoch1794fe22009-09-29 18:14:30 +0100813 private void createHandler() {
814 if (mHandler == null) {
815 mHandler = new Handler() {
816 @Override
817 public void handleMessage(Message msg) {
818 switch (msg.what) {
819 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500820 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100821 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
822 Toast.LENGTH_LONG).show();
823 } else {
824 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
825 Toast.LENGTH_LONG).show();
826 }
827 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400828 case TOUCH_ICON_DOWNLOADED:
829 Bundle b = msg.getData();
830 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400831 AddBookmarkPage.this,
832 b.getString(BrowserContract.Bookmarks.URL),
833 b.getString(BrowserContract.Bookmarks.TITLE),
834 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
835 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400836 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500837 case BOOKMARK_DELETED:
838 finish();
839 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100840 }
841 }
842 };
843 }
844 }
845
The Android Open Source Project0c908882009-03-03 19:32:16 -0800846 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100847 * 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 -0800848 */
849 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100850 createHandler();
851
The Android Open Source Project0c908882009-03-03 19:32:16 -0800852 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100853 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700854 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100855
The Android Open Source Project0c908882009-03-03 19:32:16 -0800856 boolean emptyTitle = title.length() == 0;
857 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
858 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800859 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800860 if (emptyTitle) {
861 mTitle.setError(r.getText(R.string.bookmark_needs_title));
862 }
863 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400864 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800865 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400866 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100867
The Android Open Source Project0c908882009-03-03 19:32:16 -0800868 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000869 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800870 if (!mEditingFolder) {
871 try {
872 // We allow bookmarks with a javascript: scheme, but these will in most cases
873 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000874
John Reckc8490812010-11-22 14:15:36 -0800875 if (!url.toLowerCase().startsWith("javascript:")) {
876 URI uriObj = new URI(url);
877 String scheme = uriObj.getScheme();
878 if (!Bookmarks.urlHasAcceptableScheme(url)) {
879 // If the scheme was non-null, let the user know that we
880 // can't save their bookmark. If it was null, we'll assume
881 // they meant http when we parse it in the WebAddress class.
882 if (scheme != null) {
883 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
884 return false;
885 }
886 WebAddress address;
887 try {
888 address = new WebAddress(unfilteredUrl);
889 } catch (ParseException e) {
890 throw new URISyntaxException("", "");
891 }
892 if (address.getHost().length() == 0) {
893 throw new URISyntaxException("", "");
894 }
895 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000896 }
Ben Murdochde353622009-10-12 10:29:00 +0100897 }
John Reckc8490812010-11-22 14:15:36 -0800898 } catch (URISyntaxException e) {
899 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
900 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800901 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800902 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100903
Leon Scroggins88d08032010-10-21 15:17:10 -0400904 if (mSaveToHomeScreen) {
905 mEditingExisting = false;
906 }
907
908 boolean urlUnmodified = url.equals(mOriginalUrl);
909
Ben Murdoch1794fe22009-09-29 18:14:30 +0100910 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800911 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
912 ContentValues values = new ContentValues();
913 values.put(BrowserContract.Bookmarks.TITLE, title);
914 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
915 if (!mEditingFolder) {
916 values.put(BrowserContract.Bookmarks.URL, url);
917 if (!urlUnmodified) {
918 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
919 }
920 }
921 if (values.size() > 0) {
922 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
923 }
924 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100925 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400926 Bitmap thumbnail;
927 Bitmap favicon;
928 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400929 thumbnail = (Bitmap) mMap.getParcelable(
930 BrowserContract.Bookmarks.THUMBNAIL);
931 favicon = (Bitmap) mMap.getParcelable(
932 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400933 } else {
934 thumbnail = null;
935 favicon = null;
936 }
937
Ben Murdoch1794fe22009-09-29 18:14:30 +0100938 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400939 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
940 bundle.putString(BrowserContract.Bookmarks.URL, url);
941 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400942
943 if (mSaveToHomeScreen) {
944 if (mTouchIconUrl != null && urlUnmodified) {
945 Message msg = Message.obtain(mHandler,
946 TOUCH_ICON_DOWNLOADED);
947 msg.setData(bundle);
948 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400949 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400950 icon.execute(mTouchIconUrl);
951 } else {
952 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
953 title, null /*touchIcon*/, favicon));
954 }
955 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400956 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
957 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
958 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400959 // Post a message to write to the DB.
960 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
961 msg.setData(bundle);
962 // Start a new thread so as to not slow down the UI
963 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
964 t.start();
965 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100966 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000967 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800968 }
969 return true;
970 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400971
John Reck2eec4c32011-05-11 15:55:32 -0700972 @Override
973 public void onItemSelected(AdapterView<?> parent, View view, int position,
974 long id) {
975 if (mAccountSpinner == parent) {
976 long root = mAccountAdapter.getItem(position).rootFolderId;
977 if (root != mRootFolder) {
978 onRootFolderFound(root);
979 }
980 }
981 }
982
983 @Override
984 public void onNothingSelected(AdapterView<?> parent) {
985 // Don't care
986 }
987
Leon Scroggins162f8352010-10-18 15:02:44 -0400988 /*
989 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
990 */
991 public static class CustomListView extends ListView {
992 private EditText mEditText;
993
994 public void addEditText(EditText editText) {
995 mEditText = editText;
996 }
997
998 public CustomListView(Context context) {
999 super(context);
1000 }
1001
1002 public CustomListView(Context context, AttributeSet attrs) {
1003 super(context, attrs);
1004 }
1005
1006 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1007 super(context, attrs, defStyle);
1008 }
1009
1010 @Override
1011 public boolean checkInputConnectionProxy(View view) {
1012 return view == mEditText;
1013 }
1014 }
John Reck2eec4c32011-05-11 15:55:32 -07001015
1016 static class AccountsLoader extends CursorLoader {
1017
1018 static final String[] PROJECTION = new String[] {
1019 Accounts.ACCOUNT_NAME,
1020 Accounts.ACCOUNT_TYPE,
1021 Accounts.ROOT_ID,
1022 };
1023
1024 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1025 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1026 static final int COLUMN_INDEX_ROOT_ID = 2;
1027
1028 public AccountsLoader(Context context) {
1029 super(context, Accounts.CONTENT_URI, PROJECTION, null, null,
1030 Accounts.ACCOUNT_NAME + " ASC");
1031 }
1032
1033 }
1034
1035 static class BookmarkAccount {
1036
1037 private String mLabel;
1038 String accountName, accountType;
1039 long rootFolderId;
1040
1041 public BookmarkAccount(Context context, Cursor cursor) {
1042 accountName = cursor.getString(
1043 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1044 accountType = cursor.getString(
1045 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1046 rootFolderId = cursor.getLong(
1047 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1048 mLabel = accountName;
1049 if (TextUtils.isEmpty(mLabel)) {
1050 mLabel = context.getString(R.string.local_bookmarks);
1051 }
1052 }
1053
1054 @Override
1055 public String toString() {
1056 return mLabel;
1057 }
1058 }
1059
1060 static class EditBookmarkInfo {
1061 long id = -1;
1062 long parentId = -1;
1063 String parentTitle;
1064 String title;
1065 String accountName;
1066 String accountType;
1067
1068 long lastUsedId = -1;
1069 String lastUsedTitle;
1070 String lastUsedAccountName;
1071 String lastUsedAccountType;
1072 }
1073
1074 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1075
1076 private Context mContext;
1077 private Bundle mMap;
1078
1079 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1080 super(context);
1081 mContext = context;
1082 mMap = bundle;
1083 }
1084
1085 @Override
1086 public EditBookmarkInfo loadInBackground() {
1087 final ContentResolver cr = mContext.getContentResolver();
1088 EditBookmarkInfo info = new EditBookmarkInfo();
1089 Cursor c = null;
1090
1091 try {
1092 // First, let's lookup the bookmark (check for dupes, get needed info)
1093 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1094 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1095 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1096 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1097 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1098 new String[] { BrowserContract.Bookmarks._ID},
1099 BrowserContract.Bookmarks.URL + "=?",
1100 new String[] { url }, null);
1101 if (c.getCount() == 1 && c.moveToFirst()) {
1102 info.id = c.getLong(0);
1103 }
1104 c.close();
1105 }
1106 if (info.id != -1) {
1107 c = cr.query(ContentUris.withAppendedId(
1108 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1109 new String[] {
1110 BrowserContract.Bookmarks.PARENT,
1111 BrowserContract.Bookmarks.ACCOUNT_NAME,
1112 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1113 BrowserContract.Bookmarks.TITLE},
1114 null, null, null);
1115 if (c.moveToFirst()) {
1116 info.parentId = c.getLong(0);
1117 info.accountName = c.getString(1);
1118 info.accountType = c.getString(2);
1119 info.title = c.getString(3);
1120 }
1121 c.close();
1122 c = cr.query(ContentUris.withAppendedId(
1123 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1124 new String[] {
1125 BrowserContract.Bookmarks.TITLE,},
1126 null, null, null);
1127 if (c.moveToFirst()) {
1128 info.parentTitle = c.getString(0);
1129 }
1130 c.close();
1131 }
1132
1133 // Figure out the last used folder/account
1134 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1135 new String[] {
1136 BrowserContract.Bookmarks.PARENT,
1137 }, null, null,
1138 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1139 if (c.moveToFirst()) {
1140 long parent = c.getLong(0);
1141 c.close();
1142 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1143 new String[] {
1144 BrowserContract.Bookmarks.TITLE,
1145 BrowserContract.Bookmarks.ACCOUNT_NAME,
1146 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1147 BrowserContract.Bookmarks._ID + "=?", new String[] {
1148 Long.toString(parent)}, null);
1149 if (c.moveToFirst()) {
1150 info.lastUsedId = parent;
1151 info.lastUsedTitle = c.getString(0);
1152 info.lastUsedAccountName = c.getString(1);
1153 info.lastUsedAccountType = c.getString(2);
1154 }
1155 c.close();
1156 }
1157 } finally {
1158 if (c != null) {
1159 c.close();
1160 }
1161 }
1162
1163 return info;
1164 }
1165
1166 @Override
1167 protected void onStartLoading() {
1168 forceLoad();
1169 }
1170
1171 }
1172
The Android Open Source Project0c908882009-03-03 19:32:16 -08001173}