blob: fdb34c48ed837bc4604da2d4ef8cc1d6f66cae83 [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 Reck903a0722011-11-09 17:53:10 -0800148 BookmarkAccount account =
149 (BookmarkAccount) mAccountSpinner.getSelectedItem();
150 if (folder == mRootFolder && account != null) {
151 return BookmarksLoader.addAccount(
152 BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER,
153 account.accountType, account.accountName);
154 }
John Reck2eec4c32011-05-11 15:55:32 -0700155 return BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500156 }
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();
John Reck2eec4c32011-05-11 15:55:32 -0700164 CursorLoader loader = (CursorLoader) ((Loader<?>) manager.getLoader(
Leon Scroggins74dbe012010-10-15 10:54:27 -0400165 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 {
John Reck2eec4c32011-05-11 15:55:32 -0700216 mFolderAdapter.setOtherFolderDisplayText(folder.Name);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500217 }
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) {
John Reck2eec4c32011-05-11 15:55:32 -0700230 // We are showing the correct folder hierarchy. The
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500231 // folder selector will say "Other folder..." Change it
232 // to say the name of the folder once again.
John Reck2eec4c32011-05-11 15:55:32 -0700233 mFolderAdapter.setOtherFolderDisplayText(((Folder) data).Name);
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500234 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700235 // We are not showing the correct folder hierarchy.
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500236 // Clear the Crumbs and find the proper folder
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500237 setupTopCrumb();
238 LoaderManager manager = getLoaderManager();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500239 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
240
241 }
242 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400243 }
244 }
245 }
246
Leon Scroggins III052ce662010-09-13 14:44:16 -0400247 @Override
248 public void onClick(View v) {
249 if (v == mButton) {
250 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400251 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400252 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400253 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700254 } else {
255 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400256 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400257 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700258 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400259 } else if (save()) {
260 finish();
261 }
262 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400263 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400264 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400265 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
266 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700267 } else {
268 finish();
269 }
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500270 } else if (v == mFolderCancel) {
271 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400272 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400273 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400274 mFolderNamer.setText(R.string.new_folder);
275 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700276 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400277 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400278 InputMethodManager imm = getInputMethodManager();
279 // Set the InputMethodManager to focus on the ListView so that it
280 // can transfer the focus to mFolderNamer.
281 imm.focusIn(mListView);
282 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
Leon Scroggins75630672011-01-13 17:56:15 -0500283 } else if (v == mRemoveLink) {
284 if (!mEditingExisting) {
285 throw new AssertionError("Remove button should not be shown for"
286 + " new bookmarks");
287 }
288 long id = mMap.getLong(BrowserContract.Bookmarks._ID);
289 createHandler();
290 Message msg = Message.obtain(mHandler, BOOKMARK_DELETED);
291 BookmarkUtils.displayRemoveBookmarkDialog(id,
292 mTitle.getText().toString(), this, msg);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800293 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400294 }
295
Leon Scroggins504433a2011-01-13 12:26:52 -0500296 // FolderSpinner.OnSetSelectionListener
297
Leon Scroggins88d08032010-10-21 15:17:10 -0400298 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500299 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500300 int intId = (int) id;
301 switch (intId) {
302 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400303 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400304 mSaveToHomeScreen = false;
305 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500306 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400307 // Create a short cut to the home screen
308 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400309 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500310 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400311 switchToFolderSelector();
312 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500313 case FolderSpinnerAdapter.RECENT_FOLDER:
314 mCurrentFolder = mFolderAdapter.recentFolderId();
315 mSaveToHomeScreen = false;
316 // In case the user decides to select OTHER_FOLDER
317 // and choose a different one, so that we will start from
318 // the correct place.
319 LoaderManager manager = getLoaderManager();
Leon Scroggins2f24e992011-02-11 14:02:10 -0500320 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins0b95ad42011-02-23 15:23:48 -0500321 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400322 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500323 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400324 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500325 }
326
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500327 /**
328 * Finish naming a folder, and close the IME
329 * @param cancel If true, the new folder is not created. If false, the new
330 * folder is created and the user is taken inside it.
331 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400332 private void completeOrCancelFolderNaming(boolean cancel) {
333 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700334 String name = mFolderNamer.getText().toString();
335 long id = addFolderToCurrent(mFolderNamer.getText().toString());
336 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700337 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400338 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400339 mAddNewFolder.setVisibility(View.VISIBLE);
340 mAddSeparator.setVisibility(View.VISIBLE);
341 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400342 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700343 }
344
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700345 private long addFolderToCurrent(String name) {
346 // Add the folder to the database
347 ContentValues values = new ContentValues();
348 values.put(BrowserContract.Bookmarks.TITLE,
349 name);
350 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400351 long currentFolder;
352 Object data = mCrumbs.getTopData();
353 if (data != null) {
354 currentFolder = ((Folder) data).Id;
355 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400356 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400357 }
358 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700359 Uri uri = getContentResolver().insert(
360 BrowserContract.Bookmarks.CONTENT_URI, values);
361 if (uri != null) {
362 return ContentUris.parseId(uri);
363 } else {
364 return -1;
365 }
366 }
367
Leon Scroggins III052ce662010-09-13 14:44:16 -0400368 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500369 // Set the list to the top in case it is scrolled.
370 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400371 mDefaultView.setVisibility(View.GONE);
372 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400373 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500374 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400375 mAddNewFolder.setVisibility(View.VISIBLE);
376 mAddSeparator.setVisibility(View.VISIBLE);
John Reck95f88e42011-09-26 09:25:43 -0700377 getInputMethodManager().hideSoftInputFromWindow(
378 mListView.getWindowToken(), 0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400379 }
380
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700381 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700382 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400383 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400384 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700385 }
386 }
387
John Reck2eec4c32011-05-11 15:55:32 -0700388 private LoaderCallbacks<EditBookmarkInfo> mEditInfoLoaderCallbacks =
389 new LoaderCallbacks<EditBookmarkInfo>() {
390
391 @Override
392 public void onLoaderReset(Loader<EditBookmarkInfo> loader) {
393 // Don't care
394 }
395
396 @Override
397 public void onLoadFinished(Loader<EditBookmarkInfo> loader,
398 EditBookmarkInfo info) {
399 boolean setAccount = false;
400 if (info.id != -1) {
401 mEditingExisting = true;
402 showRemoveButton();
403 mFakeTitle.setText(R.string.edit_bookmark);
404 mTitle.setText(info.title);
405 mFolderAdapter.setOtherFolderDisplayText(info.parentTitle);
406 mMap.putLong(BrowserContract.Bookmarks._ID, info.id);
407 setAccount = true;
408 setAccount(info.accountName, info.accountType);
409 mCurrentFolder = info.parentId;
410 onCurrentFolderFound();
411 }
John Reck37894a92011-05-13 12:47:53 -0700412 // TODO: Detect if lastUsedId is a subfolder of info.id in the
413 // editing folder case. For now, just don't show the last used
414 // folder at all to prevent any chance of the user adding a parent
415 // folder to a child folder
416 if (info.lastUsedId != -1 && info.lastUsedId != info.id
417 && !mEditingFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700418 if (setAccount && info.lastUsedId != mRootFolder
419 && TextUtils.equals(info.lastUsedAccountName, info.accountName)
420 && TextUtils.equals(info.lastUsedAccountType, info.accountType)) {
421 mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle);
422 } else if (!setAccount) {
423 setAccount = true;
424 setAccount(info.lastUsedAccountName, info.lastUsedAccountType);
425 if (info.lastUsedId != mRootFolder) {
426 mFolderAdapter.addRecentFolder(info.lastUsedId,
427 info.lastUsedTitle);
428 }
429 }
430 }
431 if (!setAccount) {
432 mAccountSpinner.setSelection(0);
433 }
434 }
435
436 @Override
437 public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) {
438 return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap);
439 }
440 };
441
442 void setAccount(String accountName, String accountType) {
443 for (int i = 0; i < mAccountAdapter.getCount(); i++) {
444 BookmarkAccount account = mAccountAdapter.getItem(i);
445 if (TextUtils.equals(account.accountName, accountName)
446 && TextUtils.equals(account.accountType, accountType)) {
John Reck2eec4c32011-05-11 15:55:32 -0700447 mAccountSpinner.setSelection(i);
John Reck903a0722011-11-09 17:53:10 -0800448 onRootFolderFound(account.rootFolderId);
John Reck2eec4c32011-05-11 15:55:32 -0700449 return;
450 }
451 }
452 }
453
Leon Scroggins III052ce662010-09-13 14:44:16 -0400454 @Override
455 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
456 String[] projection;
457 switch (id) {
John Reck2eec4c32011-05-11 15:55:32 -0700458 case LOADER_ID_ACCOUNTS:
459 return new AccountsLoader(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400460 case LOADER_ID_FOLDER_CONTENTS:
461 projection = new String[] {
462 BrowserContract.Bookmarks._ID,
463 BrowserContract.Bookmarks.TITLE,
464 BrowserContract.Bookmarks.IS_FOLDER
465 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500466 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
John Reck1dd8cd42011-05-13 11:22:09 -0700467 String whereArgs[] = null;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500468 if (mEditingFolder) {
John Reck1dd8cd42011-05-13 11:22:09 -0700469 where += " AND " + BrowserContract.Bookmarks._ID + " != ?";
470 whereArgs = new String[] { Long.toString(mMap.getLong(
471 BrowserContract.Bookmarks._ID)) };
472 }
473 long currentFolder;
474 Object data = mCrumbs.getTopData();
475 if (data != null) {
476 currentFolder = ((Folder) data).Id;
477 } else {
478 currentFolder = mRootFolder;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500479 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400480 return new CursorLoader(this,
John Reck1dd8cd42011-05-13 11:22:09 -0700481 getUriForFolder(currentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400482 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500483 where,
John Reck1dd8cd42011-05-13 11:22:09 -0700484 whereArgs,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500485 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400486 default:
487 throw new AssertionError("Asking for nonexistant loader!");
488 }
489 }
490
491 @Override
492 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
493 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700494 case LOADER_ID_ACCOUNTS:
495 mAccountAdapter.clear();
496 while (cursor.moveToNext()) {
497 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500498 }
John Reck2eec4c32011-05-11 15:55:32 -0700499 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
500 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
501 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500502 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400503 case LOADER_ID_FOLDER_CONTENTS:
504 mAdapter.changeCursor(cursor);
505 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400506 }
507 }
508
Dianne Hackborn39772c82010-12-16 00:43:54 -0800509 public void onLoaderReset(Loader<Cursor> loader) {
510 switch (loader.getId()) {
511 case LOADER_ID_FOLDER_CONTENTS:
512 mAdapter.changeCursor(null);
513 break;
514 }
515 }
516
Leon Scroggins02081942010-11-01 17:52:42 -0400517 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500518 * Move cursor to the position that has folderToFind as its "_id".
519 * @param cursor Cursor containing folders in the bookmarks database
520 * @param folderToFind "_id" of the folder to move to.
521 * @param idIndex Index in cursor of "_id"
522 * @throws AssertionError if cursor is empty or there is no row with folderToFind
523 * as its "_id".
524 */
525 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
526 throws AssertionError {
527 if (!cursor.moveToFirst()) {
528 throw new AssertionError("No folders in the database!");
529 }
530 long folder;
531 do {
532 folder = cursor.getLong(idIndex);
533 } while (folder != folderToFind && cursor.moveToNext());
534 if (cursor.isAfterLast()) {
535 throw new AssertionError("Folder(id=" + folderToFind
536 + ") holding this bookmark does not exist!");
537 }
538 }
539
Leon Scroggins III052ce662010-09-13 14:44:16 -0400540 @Override
541 public void onItemClick(AdapterView<?> parent, View view, int position,
542 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400543 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400544 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400545 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400546 }
547
Leon Scroggins162f8352010-10-18 15:02:44 -0400548 private void setShowFolderNamer(boolean show) {
549 if (show != mIsFolderNamerShowing) {
550 mIsFolderNamerShowing = show;
551 if (show) {
552 // Set the selection to the folder namer so it will be in
553 // view.
554 mListView.addFooterView(mFolderNamerHolder);
555 } else {
556 mListView.removeFooterView(mFolderNamerHolder);
557 }
558 // Refresh the list.
559 mListView.setAdapter(mAdapter);
560 if (show) {
561 mListView.setSelection(mListView.getCount() - 1);
562 }
563 }
564 }
565
Leon Scroggins III052ce662010-09-13 14:44:16 -0400566 /**
567 * Shows a list of names of folders.
568 */
569 private class FolderAdapter extends CursorAdapter {
570 public FolderAdapter(Context context) {
571 super(context, null);
572 }
573
574 @Override
575 public void bindView(View view, Context context, Cursor cursor) {
576 ((TextView) view.findViewById(android.R.id.text1)).setText(
577 cursor.getString(cursor.getColumnIndexOrThrow(
578 BrowserContract.Bookmarks.TITLE)));
579 }
580
581 @Override
582 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700583 View view = LayoutInflater.from(context).inflate(
584 R.layout.folder_list_item, null);
585 view.setBackgroundDrawable(context.getResources().
586 getDrawable(android.R.drawable.list_selector_background));
587 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400588 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400589
590 @Override
591 public boolean isEmpty() {
592 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400593 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400594 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400595 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800596
Leon Scrogginsc1129902010-12-08 15:28:33 -0500597 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800598 protected void onCreate(Bundle icicle) {
599 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500600 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100601
602 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100603
Leon Scroggins III052ce662010-09-13 14:44:16 -0400604 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100605
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400606 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700607
The Android Open Source Project0c908882009-03-03 19:32:16 -0800608 String title = null;
609 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100610
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400611 mFakeTitle = (TextView) findViewById(R.id.fake_title);
612
The Android Open Source Project0c908882009-03-03 19:32:16 -0800613 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800614 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800615 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800616 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800617 mMap = b;
618 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400619 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800620 if (mEditingFolder) {
621 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500622 } else {
623 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800624 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400625 } else {
626 int gravity = mMap.getInt("gravity", -1);
627 if (gravity != -1) {
628 WindowManager.LayoutParams l = window.getAttributes();
629 l.gravity = gravity;
630 window.setAttributes(l);
631 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800632 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400633 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
634 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
635 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700636 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400637 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800638
639 mTitle = (EditText) findViewById(R.id.title);
640 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100641
Leon Scroggins III052ce662010-09-13 14:44:16 -0400642 mAddress = (EditText) findViewById(R.id.address);
643 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800644
The Android Open Source Project0c908882009-03-03 19:32:16 -0800645 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400646 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800647
648 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400649 mCancelButton.setOnClickListener(this);
650
Leon Scroggins504433a2011-01-13 12:26:52 -0500651 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700652 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500653 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500654 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400655
656 mDefaultView = findViewById(R.id.default_view);
657 mFolderSelector = findViewById(R.id.folder_selector);
658
Leon Scroggins162f8352010-10-18 15:02:44 -0400659 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
660 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400661 mFolderNamer.setOnEditorActionListener(this);
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500662 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
663 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400664
665 mAddNewFolder = findViewById(R.id.add_new_folder);
666 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400667 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400668
Leon Scroggins74dbe012010-10-15 10:54:27 -0400669 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
670 mCrumbs.setUseBackButton(true);
671 mCrumbs.setController(this);
Michael Kolb5a72f182011-01-13 20:35:06 -0800672 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400673 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800674 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400675
Leon Scroggins III052ce662010-09-13 14:44:16 -0400676 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400677 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400678 View empty = findViewById(R.id.empty);
679 mListView.setEmptyView(empty);
680 mListView.setAdapter(mAdapter);
681 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400682 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500683
John Reck2eec4c32011-05-11 15:55:32 -0700684 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
685 android.R.layout.simple_spinner_item);
686 mAccountAdapter.setDropDownViewResource(
687 android.R.layout.simple_spinner_dropdown_item);
688 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
689 mAccountSpinner.setAdapter(mAccountAdapter);
690 mAccountSpinner.setOnItemSelectedListener(this);
691
692
Leon Scroggins75630672011-01-13 17:56:15 -0500693 mFakeTitleHolder = findViewById(R.id.title_holder);
694
Leon Scroggins504433a2011-01-13 12:26:52 -0500695 if (!window.getDecorView().isInTouchMode()) {
696 mButton.requestFocus();
697 }
698
John Reck2eec4c32011-05-11 15:55:32 -0700699 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500700 }
701
Leon Scroggins75630672011-01-13 17:56:15 -0500702 private void showRemoveButton() {
703 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
704 mRemoveLink = findViewById(R.id.remove);
705 mRemoveLink.setVisibility(View.VISIBLE);
706 mRemoveLink.setOnClickListener(this);
707 }
708
Leon Scroggins504433a2011-01-13 12:26:52 -0500709 // Called once we have determined which folder is the root folder
710 private void onRootFolderFound(long root) {
711 mRootFolder = root;
John Reck2eec4c32011-05-11 15:55:32 -0700712 mCurrentFolder = mRootFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500713 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700714 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500715 }
716
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500717 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700718 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500719 String name = getString(R.string.bookmarks);
720 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
721 new Folder(name, mRootFolder));
722 // To better match the other folders.
723 mTopLevelLabel.setCompoundDrawablePadding(6);
724 }
725
Leon Scroggins504433a2011-01-13 12:26:52 -0500726 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400727 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500728 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500729 // Since we're not in the root folder, change the selection to other
730 // folder now. The text will get changed once we select the correct
731 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500732 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500733 } else {
734 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500735 if (!mEditingFolder) {
736 // Initially the "Bookmarks" folder should be showing, rather than
737 // the home screen. In the editing folder case, home screen is not
738 // an option, so "Bookmarks" folder is already at the top.
739 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
740 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400741 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400742 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700743 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500744 }
745
Leon Scroggins02065b02010-01-04 14:30:13 -0500746 /**
747 * Runnable to save a bookmark, so it can be performed in its own thread.
748 */
749 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400750 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500751 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200752 private Context mContext;
753 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100754 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500755 mMessage = msg;
756 }
757 public void run() {
758 // Unbundle bookmark data.
759 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400760 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
761 String url = bundle.getString(BrowserContract.Bookmarks.URL);
762 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500763 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400764 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
765 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500766
767 // Save to the bookmarks DB.
768 try {
769 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400770 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700771 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500772 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200773 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500774 }
775 mMessage.arg1 = 1;
776 } catch (IllegalStateException e) {
777 mMessage.arg1 = 0;
778 }
779 mMessage.sendToTarget();
780 }
781 }
782
John Reckc8490812010-11-22 14:15:36 -0800783 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
784 Context mContext;
785 Long mId;
786
787 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100788 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800789 mId = id;
790 }
791
792 @Override
793 protected Void doInBackground(ContentValues... params) {
794 if (params.length != 1) {
795 throw new IllegalArgumentException("No ContentValues provided!");
796 }
797 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
798 mContext.getContentResolver().update(
799 uri,
800 params[0], null, null);
801 return null;
802 }
803 }
804
Ben Murdoch1794fe22009-09-29 18:14:30 +0100805 private void createHandler() {
806 if (mHandler == null) {
807 mHandler = new Handler() {
808 @Override
809 public void handleMessage(Message msg) {
810 switch (msg.what) {
811 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500812 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100813 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
814 Toast.LENGTH_LONG).show();
815 } else {
816 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
817 Toast.LENGTH_LONG).show();
818 }
819 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400820 case TOUCH_ICON_DOWNLOADED:
821 Bundle b = msg.getData();
822 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400823 AddBookmarkPage.this,
824 b.getString(BrowserContract.Bookmarks.URL),
825 b.getString(BrowserContract.Bookmarks.TITLE),
826 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
827 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400828 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500829 case BOOKMARK_DELETED:
830 finish();
831 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100832 }
833 }
834 };
835 }
836 }
837
The Android Open Source Project0c908882009-03-03 19:32:16 -0800838 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100839 * 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 -0800840 */
841 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100842 createHandler();
843
The Android Open Source Project0c908882009-03-03 19:32:16 -0800844 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100845 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700846 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100847
The Android Open Source Project0c908882009-03-03 19:32:16 -0800848 boolean emptyTitle = title.length() == 0;
849 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
850 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800851 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800852 if (emptyTitle) {
853 mTitle.setError(r.getText(R.string.bookmark_needs_title));
854 }
855 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400856 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800857 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400858 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100859
The Android Open Source Project0c908882009-03-03 19:32:16 -0800860 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000861 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800862 if (!mEditingFolder) {
863 try {
864 // We allow bookmarks with a javascript: scheme, but these will in most cases
865 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000866
John Reckc8490812010-11-22 14:15:36 -0800867 if (!url.toLowerCase().startsWith("javascript:")) {
868 URI uriObj = new URI(url);
869 String scheme = uriObj.getScheme();
870 if (!Bookmarks.urlHasAcceptableScheme(url)) {
871 // If the scheme was non-null, let the user know that we
872 // can't save their bookmark. If it was null, we'll assume
873 // they meant http when we parse it in the WebAddress class.
874 if (scheme != null) {
875 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
876 return false;
877 }
878 WebAddress address;
879 try {
880 address = new WebAddress(unfilteredUrl);
881 } catch (ParseException e) {
882 throw new URISyntaxException("", "");
883 }
884 if (address.getHost().length() == 0) {
885 throw new URISyntaxException("", "");
886 }
887 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000888 }
Ben Murdochde353622009-10-12 10:29:00 +0100889 }
John Reckc8490812010-11-22 14:15:36 -0800890 } catch (URISyntaxException e) {
891 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
892 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800893 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800894 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100895
Leon Scroggins88d08032010-10-21 15:17:10 -0400896 if (mSaveToHomeScreen) {
897 mEditingExisting = false;
898 }
899
900 boolean urlUnmodified = url.equals(mOriginalUrl);
901
Ben Murdoch1794fe22009-09-29 18:14:30 +0100902 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800903 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
904 ContentValues values = new ContentValues();
905 values.put(BrowserContract.Bookmarks.TITLE, title);
906 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
907 if (!mEditingFolder) {
908 values.put(BrowserContract.Bookmarks.URL, url);
909 if (!urlUnmodified) {
910 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
911 }
912 }
913 if (values.size() > 0) {
914 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
915 }
916 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100917 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400918 Bitmap thumbnail;
919 Bitmap favicon;
920 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400921 thumbnail = (Bitmap) mMap.getParcelable(
922 BrowserContract.Bookmarks.THUMBNAIL);
923 favicon = (Bitmap) mMap.getParcelable(
924 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400925 } else {
926 thumbnail = null;
927 favicon = null;
928 }
929
Ben Murdoch1794fe22009-09-29 18:14:30 +0100930 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400931 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
932 bundle.putString(BrowserContract.Bookmarks.URL, url);
933 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400934
935 if (mSaveToHomeScreen) {
936 if (mTouchIconUrl != null && urlUnmodified) {
937 Message msg = Message.obtain(mHandler,
938 TOUCH_ICON_DOWNLOADED);
939 msg.setData(bundle);
940 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400941 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400942 icon.execute(mTouchIconUrl);
943 } else {
944 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
945 title, null /*touchIcon*/, favicon));
946 }
947 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400948 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
949 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
950 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400951 // Post a message to write to the DB.
952 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
953 msg.setData(bundle);
954 // Start a new thread so as to not slow down the UI
955 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
956 t.start();
957 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100958 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000959 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800960 }
961 return true;
962 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400963
John Reck2eec4c32011-05-11 15:55:32 -0700964 @Override
965 public void onItemSelected(AdapterView<?> parent, View view, int position,
966 long id) {
967 if (mAccountSpinner == parent) {
968 long root = mAccountAdapter.getItem(position).rootFolderId;
969 if (root != mRootFolder) {
970 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -0700971 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -0700972 }
973 }
974 }
975
976 @Override
977 public void onNothingSelected(AdapterView<?> parent) {
978 // Don't care
979 }
980
Leon Scroggins162f8352010-10-18 15:02:44 -0400981 /*
982 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
983 */
984 public static class CustomListView extends ListView {
985 private EditText mEditText;
986
987 public void addEditText(EditText editText) {
988 mEditText = editText;
989 }
990
991 public CustomListView(Context context) {
992 super(context);
993 }
994
995 public CustomListView(Context context, AttributeSet attrs) {
996 super(context, attrs);
997 }
998
999 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1000 super(context, attrs, defStyle);
1001 }
1002
1003 @Override
1004 public boolean checkInputConnectionProxy(View view) {
1005 return view == mEditText;
1006 }
1007 }
John Reck2eec4c32011-05-11 15:55:32 -07001008
John Reck3bf3cfb2011-10-17 18:00:05 -07001009 static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001010
1011 static final String[] PROJECTION = new String[] {
1012 Accounts.ACCOUNT_NAME,
1013 Accounts.ACCOUNT_TYPE,
1014 Accounts.ROOT_ID,
1015 };
1016
1017 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1018 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1019 static final int COLUMN_INDEX_ROOT_ID = 2;
1020
1021 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001022 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001023 }
1024
1025 }
1026
John Reck9b8cd1e2011-05-25 18:14:01 -07001027 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001028
1029 private String mLabel;
1030 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001031 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001032
1033 public BookmarkAccount(Context context, Cursor cursor) {
1034 accountName = cursor.getString(
1035 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1036 accountType = cursor.getString(
1037 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1038 rootFolderId = cursor.getLong(
1039 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1040 mLabel = accountName;
1041 if (TextUtils.isEmpty(mLabel)) {
1042 mLabel = context.getString(R.string.local_bookmarks);
1043 }
1044 }
1045
1046 @Override
1047 public String toString() {
1048 return mLabel;
1049 }
1050 }
1051
1052 static class EditBookmarkInfo {
1053 long id = -1;
1054 long parentId = -1;
1055 String parentTitle;
1056 String title;
1057 String accountName;
1058 String accountType;
1059
1060 long lastUsedId = -1;
1061 String lastUsedTitle;
1062 String lastUsedAccountName;
1063 String lastUsedAccountType;
1064 }
1065
1066 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1067
1068 private Context mContext;
1069 private Bundle mMap;
1070
1071 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1072 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001073 mContext = context.getApplicationContext();
John Reck2eec4c32011-05-11 15:55:32 -07001074 mMap = bundle;
1075 }
1076
1077 @Override
1078 public EditBookmarkInfo loadInBackground() {
1079 final ContentResolver cr = mContext.getContentResolver();
1080 EditBookmarkInfo info = new EditBookmarkInfo();
1081 Cursor c = null;
1082
1083 try {
1084 // First, let's lookup the bookmark (check for dupes, get needed info)
1085 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1086 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1087 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1088 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1089 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1090 new String[] { BrowserContract.Bookmarks._ID},
1091 BrowserContract.Bookmarks.URL + "=?",
1092 new String[] { url }, null);
1093 if (c.getCount() == 1 && c.moveToFirst()) {
1094 info.id = c.getLong(0);
1095 }
1096 c.close();
1097 }
1098 if (info.id != -1) {
1099 c = cr.query(ContentUris.withAppendedId(
1100 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1101 new String[] {
1102 BrowserContract.Bookmarks.PARENT,
1103 BrowserContract.Bookmarks.ACCOUNT_NAME,
1104 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1105 BrowserContract.Bookmarks.TITLE},
1106 null, null, null);
1107 if (c.moveToFirst()) {
1108 info.parentId = c.getLong(0);
1109 info.accountName = c.getString(1);
1110 info.accountType = c.getString(2);
1111 info.title = c.getString(3);
1112 }
1113 c.close();
1114 c = cr.query(ContentUris.withAppendedId(
1115 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1116 new String[] {
1117 BrowserContract.Bookmarks.TITLE,},
1118 null, null, null);
1119 if (c.moveToFirst()) {
1120 info.parentTitle = c.getString(0);
1121 }
1122 c.close();
1123 }
1124
1125 // Figure out the last used folder/account
1126 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1127 new String[] {
1128 BrowserContract.Bookmarks.PARENT,
1129 }, null, null,
1130 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1131 if (c.moveToFirst()) {
1132 long parent = c.getLong(0);
1133 c.close();
1134 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1135 new String[] {
1136 BrowserContract.Bookmarks.TITLE,
1137 BrowserContract.Bookmarks.ACCOUNT_NAME,
1138 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1139 BrowserContract.Bookmarks._ID + "=?", new String[] {
1140 Long.toString(parent)}, null);
1141 if (c.moveToFirst()) {
1142 info.lastUsedId = parent;
1143 info.lastUsedTitle = c.getString(0);
1144 info.lastUsedAccountName = c.getString(1);
1145 info.lastUsedAccountType = c.getString(2);
1146 }
1147 c.close();
1148 }
1149 } finally {
1150 if (c != null) {
1151 c.close();
1152 }
1153 }
1154
1155 return info;
1156 }
1157
1158 @Override
1159 protected void onStartLoading() {
1160 forceLoad();
1161 }
1162
1163 }
1164
The Android Open Source Project0c908882009-03-03 19:32:16 -08001165}