blob: a88e8e717e8c18d5a60976b81489f81af3f2642f [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Leon Scroggins25230d72010-09-28 20:09:25 -040019import com.android.browser.provider.BrowserProvider2;
Leon Scroggins504433a2011-01-13 12:26:52 -050020import com.android.browser.addbookmark.FolderSpinner;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -050021import com.android.browser.addbookmark.FolderSpinnerAdapter;
Leon Scroggins25230d72010-09-28 20:09:25 -040022
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.app.Activity;
Leon Scroggins III052ce662010-09-13 14:44:16 -040024import android.app.LoaderManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.content.ContentResolver;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070026import android.content.ContentUris;
Leon Scroggins III052ce662010-09-13 14:44:16 -040027import android.content.ContentValues;
28import android.content.Context;
29import android.content.CursorLoader;
Leon Scroggins III052ce662010-09-13 14:44:16 -040030import android.content.Loader;
Leon Scroggins25230d72010-09-28 20:09:25 -040031import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040033import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010034import android.graphics.Bitmap;
Leon Scroggins02081942010-11-01 17:52:42 -040035import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.net.ParseException;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070037import android.net.Uri;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.net.WebAddress;
John Reckc8490812010-11-22 14:15:36 -080039import android.os.AsyncTask;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010041import android.os.Handler;
42import android.os.Message;
Leon Scroggins25230d72010-09-28 20:09:25 -040043import android.preference.PreferenceManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040044import android.provider.BrowserContract;
Leon Scroggins25230d72010-09-28 20:09:25 -040045import android.text.TextUtils;
Leon Scroggins162f8352010-10-18 15:02:44 -040046import android.util.AttributeSet;
Leon Scroggins III052ce662010-09-13 14:44:16 -040047import android.view.KeyEvent;
48import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040050import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040052import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040053import android.view.inputmethod.EditorInfo;
54import android.view.inputmethod.InputMethodManager;
55import android.widget.AdapterView;
56import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080057import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040058import android.widget.ListView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080059import android.widget.TextView;
60import android.widget.Toast;
61
Cary Clarkf2407c62009-09-04 12:25:10 -040062import java.net.URI;
63import java.net.URISyntaxException;
Leon Scroggins74dbe012010-10-15 10:54:27 -040064import java.util.Stack;
Leon Scroggins III052ce662010-09-13 14:44:16 -040065
66public class AddBookmarkPage extends Activity
67 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040068 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
Leon Scroggins504433a2011-01-13 12:26:52 -050069 BreadCrumbView.Controller, FolderSpinner.OnSetSelectionListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080070
Michael Kolb370a4f32010-10-06 10:45:32 -070071 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040072 public static final String TOUCH_ICON_URL = "touch_icon_url";
73 // Place on an edited bookmark to remove the saved thumbnail
74 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
75 public static final String USER_AGENT = "user_agent";
Leon Scrogginsbdff8a72011-02-11 15:49:04 -050076 public static final String CHECK_FOR_DUPE = "check_for_dupe";
Michael Kolb370a4f32010-10-06 10:45:32 -070077
John Reckc8490812010-11-22 14:15:36 -080078 /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark";
79 /* package */ static final String EXTRA_IS_FOLDER = "is_folder";
80
Leon Scroggins74dbe012010-10-15 10:54:27 -040081 private static final int MAX_CRUMBS_SHOWN = 2;
82
The Android Open Source Project0c908882009-03-03 19:32:16 -080083 private final String LOGTAG = "Bookmarks";
84
Leon Scroggins III052ce662010-09-13 14:44:16 -040085 // IDs for the CursorLoaders that are used.
86 private final int LOADER_ID_FOLDER_CONTENTS = 0;
87 private final int LOADER_ID_ALL_FOLDERS = 1;
Leon Scroggins504433a2011-01-13 12:26:52 -050088 private final int LOADER_ID_FIND_ROOT = 2;
89 private final int LOADER_ID_CHECK_FOR_DUPE = 3;
Leon Scroggins2f24e992011-02-11 14:02:10 -050090 private final int LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK = 4;
91 private final int LOADER_ID_FIND_FOLDER_BY_ID = 5;
Leon Scroggins III052ce662010-09-13 14:44:16 -040092
The Android Open Source Project0c908882009-03-03 19:32:16 -080093 private EditText mTitle;
94 private EditText mAddress;
95 private TextView mButton;
96 private View mCancelButton;
97 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -080098 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -080099 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -0400100 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +0100101 private String mOriginalUrl;
Leon Scroggins504433a2011-01-13 12:26:52 -0500102 private FolderSpinner mFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400103 private View mDefaultView;
104 private View mFolderSelector;
105 private EditText mFolderNamer;
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500106 private View mFolderCancel;
Leon Scroggins162f8352010-10-18 15:02:44 -0400107 private boolean mIsFolderNamerShowing;
108 private View mFolderNamerHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400109 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400110 private View mAddSeparator;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500111 private long mCurrentFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400112 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400113 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400114 private TextView mFakeTitle;
115 private View mCrumbHolder;
Leon Scroggins162f8352010-10-18 15:02:44 -0400116 private CustomListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400117 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400118 private long mRootFolder;
Leon Scroggins905250c2010-12-17 15:25:33 -0500119 private TextView mTopLevelLabel;
120 private Drawable mHeaderIcon;
Leon Scroggins75630672011-01-13 17:56:15 -0500121 private View mRemoveLink;
122 private View mFakeTitleHolder;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500123 private FolderSpinnerAdapter mFolderAdapter;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400124 private static class Folder {
125 String Name;
126 long Id;
127 Folder(String name, long id) {
128 Name = name;
129 Id = id;
130 }
131 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132
Ben Murdoch1794fe22009-09-29 18:14:30 +0100133 // Message IDs
134 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400135 private static final int TOUCH_ICON_DOWNLOADED = 101;
Leon Scroggins75630672011-01-13 17:56:15 -0500136 private static final int BOOKMARK_DELETED = 102;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100137
138 private Handler mHandler;
139
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100140 private InputMethodManager getInputMethodManager() {
141 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
142 }
143
Leon Scroggins8baaa632010-12-08 19:46:53 -0500144 private Uri getUriForFolder(long folder) {
145 Uri uri;
146 if (folder == mRootFolder) {
147 uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER;
148 } else {
149 uri = BrowserContract.Bookmarks.buildFolderUri(folder);
150 }
151 String[] accountInfo = getAccountNameAndType(this);
152 if (accountInfo != null) {
153 uri = BookmarksLoader.addAccount(uri, accountInfo[1], accountInfo[0]);
154 }
155 return uri;
156 }
157
Leon Scroggins III052ce662010-09-13 14:44:16 -0400158 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400159 public void onTop(int level, Object data) {
160 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400161 Folder folderData = (Folder) data;
162 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400163 LoaderManager manager = getLoaderManager();
164 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
165 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500166 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400167 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400168 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400169 completeOrCancelFolderNaming(true);
170 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500171 setShowBookmarkIcon(level == 1);
172 }
173
174 /**
175 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
176 * @param show True if the icon should visible, false otherwise.
177 */
178 private void setShowBookmarkIcon(boolean show) {
179 Drawable drawable = show ? mHeaderIcon: null;
180 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400181 }
182
Leon Scroggins74dbe012010-10-15 10:54:27 -0400183 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400184 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
185 if (v == mFolderNamer) {
186 if (v.getText().length() > 0) {
187 if (actionId == EditorInfo.IME_NULL) {
188 // Only want to do this once.
189 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400190 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400191 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400192 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800193 }
Michael Kolb31829b92010-10-01 11:50:21 -0700194 // Steal the key press; otherwise a newline will be added
195 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400197 return false;
198 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800199
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400200 private void switchToDefaultView(boolean changedFolder) {
201 mFolderSelector.setVisibility(View.GONE);
202 mDefaultView.setVisibility(View.VISIBLE);
203 mCrumbHolder.setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500204 mFakeTitleHolder.setVisibility(View.VISIBLE);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400205 if (changedFolder) {
206 Object data = mCrumbs.getTopData();
207 if (data != null) {
208 Folder folder = (Folder) data;
209 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500210 if (mCurrentFolder == mRootFolder) {
211 // The Spinner changed to show "Other folder ..." Change
212 // it back to "Bookmarks", which is position 0 if we are
213 // editing a folder, 1 otherwise.
Leon Scroggins504433a2011-01-13 12:26:52 -0500214 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500215 } else {
216 ((TextView) mFolder.getSelectedView()).setText(folder.Name);
217 }
218 }
219 } else {
220 // The user canceled selecting a folder. Revert back to the earlier
221 // selection.
222 if (mSaveToHomeScreen) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500223 mFolder.setSelectionIgnoringSelectionChange(0);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500224 } else {
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500225 if (mCurrentFolder == mRootFolder) {
226 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
227 } else {
228 Object data = mCrumbs.getTopData();
229 if (data != null && ((Folder) data).Id == mCurrentFolder) {
230 // We are showing the correct folder heirarchy. The
231 // folder selector will say "Other folder..." Change it
232 // to say the name of the folder once again.
233 ((TextView) mFolder.getSelectedView()).setText(((Folder) data).Name);
234 } else {
235 // We are not be showing the correct folder heirarchy.
236 // Clear the Crumbs and find the proper folder
237 mCrumbs.clear();
238 setupTopCrumb();
239 LoaderManager manager = getLoaderManager();
240 manager.restartLoader(LOADER_ID_ALL_FOLDERS, null, this);
241 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
242
243 }
244 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400245 }
246 }
247 }
248
Leon Scroggins III052ce662010-09-13 14:44:16 -0400249 @Override
250 public void onClick(View v) {
251 if (v == mButton) {
252 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400253 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400254 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400255 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700256 } else {
257 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400258 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400259 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700260 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400261 } else if (save()) {
262 finish();
263 }
264 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400265 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400266 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400267 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
268 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700269 } else {
270 finish();
271 }
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500272 } else if (v == mFolderCancel) {
273 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400274 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400275 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400276 mFolderNamer.setText(R.string.new_folder);
277 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700278 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400279 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400280 InputMethodManager imm = getInputMethodManager();
281 // Set the InputMethodManager to focus on the ListView so that it
282 // can transfer the focus to mFolderNamer.
283 imm.focusIn(mListView);
284 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
Leon Scroggins75630672011-01-13 17:56:15 -0500285 } else if (v == mRemoveLink) {
286 if (!mEditingExisting) {
287 throw new AssertionError("Remove button should not be shown for"
288 + " new bookmarks");
289 }
290 long id = mMap.getLong(BrowserContract.Bookmarks._ID);
291 createHandler();
292 Message msg = Message.obtain(mHandler, BOOKMARK_DELETED);
293 BookmarkUtils.displayRemoveBookmarkDialog(id,
294 mTitle.getText().toString(), this, msg);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800295 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400296 }
297
Leon Scroggins504433a2011-01-13 12:26:52 -0500298 // FolderSpinner.OnSetSelectionListener
299
Leon Scroggins88d08032010-10-21 15:17:10 -0400300 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500301 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500302 int intId = (int) id;
303 switch (intId) {
304 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400305 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400306 mSaveToHomeScreen = false;
307 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500308 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400309 // Create a short cut to the home screen
310 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400311 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500312 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400313 switchToFolderSelector();
314 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500315 case FolderSpinnerAdapter.RECENT_FOLDER:
316 mCurrentFolder = mFolderAdapter.recentFolderId();
317 mSaveToHomeScreen = false;
318 // In case the user decides to select OTHER_FOLDER
319 // and choose a different one, so that we will start from
320 // the correct place.
321 LoaderManager manager = getLoaderManager();
322 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
323 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins88d08032010-10-21 15:17:10 -0400324 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500325 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400326 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500327 }
328
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500329 /**
330 * Finish naming a folder, and close the IME
331 * @param cancel If true, the new folder is not created. If false, the new
332 * folder is created and the user is taken inside it.
333 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400334 private void completeOrCancelFolderNaming(boolean cancel) {
335 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700336 String name = mFolderNamer.getText().toString();
337 long id = addFolderToCurrent(mFolderNamer.getText().toString());
338 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700339 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400340 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400341 mAddNewFolder.setVisibility(View.VISIBLE);
342 mAddSeparator.setVisibility(View.VISIBLE);
343 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400344 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700345 }
346
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700347 private long addFolderToCurrent(String name) {
348 // Add the folder to the database
349 ContentValues values = new ContentValues();
350 values.put(BrowserContract.Bookmarks.TITLE,
351 name);
352 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500353 String[] accountInfo = getAccountNameAndType(this);
354 if (accountInfo != null) {
355 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
356 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700357 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400358 long currentFolder;
359 Object data = mCrumbs.getTopData();
360 if (data != null) {
361 currentFolder = ((Folder) data).Id;
362 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400363 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400364 }
365 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700366 Uri uri = getContentResolver().insert(
367 BrowserContract.Bookmarks.CONTENT_URI, values);
368 if (uri != null) {
369 return ContentUris.parseId(uri);
370 } else {
371 return -1;
372 }
373 }
374
Leon Scroggins III052ce662010-09-13 14:44:16 -0400375 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500376 // Set the list to the top in case it is scrolled.
377 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400378 mDefaultView.setVisibility(View.GONE);
379 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400380 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500381 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400382 mAddNewFolder.setVisibility(View.VISIBLE);
383 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400384 }
385
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700386 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700387 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400388 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400389 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700390 }
391 }
392
Leon Scroggins III052ce662010-09-13 14:44:16 -0400393 @Override
394 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
395 String[] projection;
396 switch (id) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500397 case LOADER_ID_CHECK_FOR_DUPE:
398 projection = new String[] {
399 BrowserContract.Bookmarks._ID,
400 BrowserContract.Bookmarks.PARENT,
401 BrowserContract.Bookmarks.TITLE
402 };
403 return new CursorLoader(this,
404 BookmarkUtils.getBookmarksUri(this),
405 projection,
406 BrowserContract.Bookmarks.URL + " = ?",
407 new String[] { mOriginalUrl },
408 null);
409 case LOADER_ID_FIND_ROOT:
410 String name = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME);
411 String type = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE);
412
413 projection = new String[] { BrowserContract.Bookmarks._ID };
414 String selection = BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
415 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
416 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?";
417 String[] selArgs = new String[] {
418 BrowserContract.ChromeSyncColumns.FOLDER_NAME_BOOKMARKS_BAR,
419 name,
420 type
421 };
422 return new CursorLoader(this,
423 BrowserContract.Bookmarks.CONTENT_URI,
424 projection,
425 selection,
426 selArgs,
427 null);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500428 case LOADER_ID_FIND_FOLDER_BY_ID:
429 projection = new String[] {
430 BrowserContract.Bookmarks._ID,
431 BrowserContract.Bookmarks.TITLE
432 };
433 return new CursorLoader(this,
Leon Scroggins3ba3e482011-02-23 12:08:09 -0500434 BookmarkUtils.getBookmarksUri(this),
Leon Scroggins2f24e992011-02-11 14:02:10 -0500435 projection,
436 BrowserContract.Bookmarks._ID + " = "
437 + args.getLong(BrowserContract.Bookmarks._ID),
438 null,
439 null);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400440 case LOADER_ID_ALL_FOLDERS:
441 projection = new String[] {
442 BrowserContract.Bookmarks._ID,
443 BrowserContract.Bookmarks.PARENT,
444 BrowserContract.Bookmarks.TITLE,
445 BrowserContract.Bookmarks.IS_FOLDER
446 };
447 return new CursorLoader(this,
Leon Scroggins3ba3e482011-02-23 12:08:09 -0500448 BookmarkUtils.getBookmarksUri(this),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400449 projection,
450 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
451 null,
452 null);
453 case LOADER_ID_FOLDER_CONTENTS:
454 projection = new String[] {
455 BrowserContract.Bookmarks._ID,
456 BrowserContract.Bookmarks.TITLE,
457 BrowserContract.Bookmarks.IS_FOLDER
458 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500459 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
460 if (mEditingFolder) {
461 where += " AND " + BrowserContract.Bookmarks._ID + " != "
462 + mMap.getLong(BrowserContract.Bookmarks._ID);
463 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400464 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500465 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400466 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500467 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400468 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500469 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins2f24e992011-02-11 14:02:10 -0500470 case LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK:
471 projection = new String[] {
472 BrowserContract.Bookmarks.PARENT
473 };
474 return new CursorLoader(this,
Leon Scroggins3ba3e482011-02-23 12:08:09 -0500475 BookmarkUtils.getBookmarksUri(this),
Leon Scroggins2f24e992011-02-11 14:02:10 -0500476 projection,
477 BrowserContract.Bookmarks.IS_FOLDER + " = 0",
478 null,
479 BrowserContract.Bookmarks.DATE_CREATED + " DESC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400480 default:
481 throw new AssertionError("Asking for nonexistant loader!");
482 }
483 }
484
485 @Override
486 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
487 switch (loader.getId()) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500488 case LOADER_ID_CHECK_FOR_DUPE:
489 if (cursor != null && cursor.moveToFirst()) {
490 // Site is bookmarked.
491 mEditingExisting = true;
Leon Scroggins75630672011-01-13 17:56:15 -0500492 showRemoveButton();
Leon Scroggins504433a2011-01-13 12:26:52 -0500493 mFakeTitle.setText(R.string.edit_bookmark);
494 int index = cursor.getColumnIndexOrThrow(
495 BrowserContract.Bookmarks.PARENT);
496 mCurrentFolder = cursor.getLong(index);
497 index = cursor.getColumnIndexOrThrow(
498 BrowserContract.Bookmarks.TITLE);
499 String title = cursor.getString(index);
500 mTitle.setText(title);
501 index = cursor.getColumnIndexOrThrow(
502 BrowserContract.Bookmarks._ID);
503 long id = cursor.getLong(index);
504 mMap.putLong(BrowserContract.Bookmarks._ID, id);
505 }
506 onCurrentFolderFound();
507 getLoaderManager().destroyLoader(LOADER_ID_CHECK_FOR_DUPE);
508 break;
509 case LOADER_ID_FIND_ROOT:
510 long root;
511 if (cursor != null && cursor.moveToFirst()) {
512 root = cursor.getLong(0);
513 } else {
514 root = BrowserProvider2.FIXED_ID_ROOT;
515 }
516 onRootFolderFound(root);
517 getLoaderManager().destroyLoader(LOADER_ID_FIND_ROOT);
518 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400519 case LOADER_ID_FOLDER_CONTENTS:
520 mAdapter.changeCursor(cursor);
521 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500522 case LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK:
523 LoaderManager manager = getLoaderManager();
524 if (cursor != null && cursor.moveToFirst()) {
525 // Find the parent
526 long lastUsedFolder = cursor.getLong(0);
527 if (lastUsedFolder != mRootFolder
528 && lastUsedFolder != mCurrentFolder
529 && lastUsedFolder != 0) {
530 // Find out the parent's name
531 Bundle b = new Bundle();
532 b.putLong(BrowserContract.Bookmarks._ID, lastUsedFolder);
533 manager.initLoader(LOADER_ID_FIND_FOLDER_BY_ID, b, this);
534 }
535 }
536 manager.destroyLoader(LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK);
537 break;
538 case LOADER_ID_FIND_FOLDER_BY_ID:
539 if (cursor != null && cursor.moveToFirst()) {
540 long id = cursor.getLong(cursor.getColumnIndexOrThrow(
541 BrowserContract.Bookmarks._ID));
542 String title = cursor.getString(cursor.getColumnIndexOrThrow(
543 BrowserContract.Bookmarks.TITLE));
544 mFolderAdapter.addRecentFolder(id, title);
545 }
546 getLoaderManager().destroyLoader(LOADER_ID_FIND_FOLDER_BY_ID);
547 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400548 case LOADER_ID_ALL_FOLDERS:
549 long parent = mCurrentFolder;
550 int idIndex = cursor.getColumnIndexOrThrow(
551 BrowserContract.Bookmarks._ID);
552 int titleIndex = cursor.getColumnIndexOrThrow(
553 BrowserContract.Bookmarks.TITLE);
554 int parentIndex = cursor.getColumnIndexOrThrow(
555 BrowserContract.Bookmarks.PARENT);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500556 // If the user is editing anything inside the "Other Bookmarks"
557 // folder, we need to stop searching up when we reach its parent.
558 // Find the root folder
559 moveCursorToFolder(cursor, mRootFolder, idIndex);
560 // omniparent is the folder which contains root, and therefore
561 // also the parent of the "Other Bookmarks" folder.
562 long omniparent = cursor.getLong(parentIndex);
563 Stack<Folder> folderStack = new Stack<Folder>();
564 while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400565 // First, find the folder corresponding to the current
566 // folder
Leon Scroggins8baaa632010-12-08 19:46:53 -0500567 moveCursorToFolder(cursor, parent, idIndex);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400568 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400569 if (parent == mCurrentFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500570 ((TextView) mFolder.getSelectedView()).setText(name);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400571 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400572 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400573 parent = cursor.getLong(parentIndex);
574 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400575 while (!folderStack.isEmpty()) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500576 Folder thisFolder = folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400577 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400578 }
Dianne Hackborn71e76c72010-12-20 11:44:09 -0800579 getLoaderManager().destroyLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400580 break;
581 default:
582 break;
583 }
584 }
585
Dianne Hackborn39772c82010-12-16 00:43:54 -0800586 public void onLoaderReset(Loader<Cursor> loader) {
587 switch (loader.getId()) {
588 case LOADER_ID_FOLDER_CONTENTS:
589 mAdapter.changeCursor(null);
590 break;
591 }
592 }
593
Leon Scroggins02081942010-11-01 17:52:42 -0400594 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500595 * Move cursor to the position that has folderToFind as its "_id".
596 * @param cursor Cursor containing folders in the bookmarks database
597 * @param folderToFind "_id" of the folder to move to.
598 * @param idIndex Index in cursor of "_id"
599 * @throws AssertionError if cursor is empty or there is no row with folderToFind
600 * as its "_id".
601 */
602 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
603 throws AssertionError {
604 if (!cursor.moveToFirst()) {
605 throw new AssertionError("No folders in the database!");
606 }
607 long folder;
608 do {
609 folder = cursor.getLong(idIndex);
610 } while (folder != folderToFind && cursor.moveToNext());
611 if (cursor.isAfterLast()) {
612 throw new AssertionError("Folder(id=" + folderToFind
613 + ") holding this bookmark does not exist!");
614 }
615 }
616
Leon Scroggins III052ce662010-09-13 14:44:16 -0400617 @Override
618 public void onItemClick(AdapterView<?> parent, View view, int position,
619 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400620 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400621 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400622 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400623 }
624
Leon Scroggins162f8352010-10-18 15:02:44 -0400625 private void setShowFolderNamer(boolean show) {
626 if (show != mIsFolderNamerShowing) {
627 mIsFolderNamerShowing = show;
628 if (show) {
629 // Set the selection to the folder namer so it will be in
630 // view.
631 mListView.addFooterView(mFolderNamerHolder);
632 } else {
633 mListView.removeFooterView(mFolderNamerHolder);
634 }
635 // Refresh the list.
636 mListView.setAdapter(mAdapter);
637 if (show) {
638 mListView.setSelection(mListView.getCount() - 1);
639 }
640 }
641 }
642
Leon Scroggins III052ce662010-09-13 14:44:16 -0400643 /**
644 * Shows a list of names of folders.
645 */
646 private class FolderAdapter extends CursorAdapter {
647 public FolderAdapter(Context context) {
648 super(context, null);
649 }
650
651 @Override
652 public void bindView(View view, Context context, Cursor cursor) {
653 ((TextView) view.findViewById(android.R.id.text1)).setText(
654 cursor.getString(cursor.getColumnIndexOrThrow(
655 BrowserContract.Bookmarks.TITLE)));
656 }
657
658 @Override
659 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700660 View view = LayoutInflater.from(context).inflate(
661 R.layout.folder_list_item, null);
662 view.setBackgroundDrawable(context.getResources().
663 getDrawable(android.R.drawable.list_selector_background));
664 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400665 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400666
667 @Override
668 public boolean isEmpty() {
669 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400670 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400671 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400672 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800673
Leon Scrogginsc1129902010-12-08 15:28:33 -0500674 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800675 protected void onCreate(Bundle icicle) {
676 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500677 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100678
679 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100680
Leon Scroggins III052ce662010-09-13 14:44:16 -0400681 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100682
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400683 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700684
The Android Open Source Project0c908882009-03-03 19:32:16 -0800685 String title = null;
686 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100687
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400688 mFakeTitle = (TextView) findViewById(R.id.fake_title);
689
The Android Open Source Project0c908882009-03-03 19:32:16 -0800690 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800691 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800692 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800693 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800694 mMap = b;
695 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400696 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800697 if (mEditingFolder) {
698 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500699 } else {
700 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800701 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400702 } else {
703 int gravity = mMap.getInt("gravity", -1);
704 if (gravity != -1) {
705 WindowManager.LayoutParams l = window.getAttributes();
706 l.gravity = gravity;
707 window.setAttributes(l);
708 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800709 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400710 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
711 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
712 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700713 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400714 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800715
716 mTitle = (EditText) findViewById(R.id.title);
717 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100718
Leon Scroggins III052ce662010-09-13 14:44:16 -0400719 mAddress = (EditText) findViewById(R.id.address);
720 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800721
The Android Open Source Project0c908882009-03-03 19:32:16 -0800722 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400723 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800724
725 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400726 mCancelButton.setOnClickListener(this);
727
Leon Scroggins504433a2011-01-13 12:26:52 -0500728 mFolder = (FolderSpinner) findViewById(R.id.folder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500729 mFolderAdapter = new FolderSpinnerAdapter(!mEditingFolder);
730 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500731 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400732
733 mDefaultView = findViewById(R.id.default_view);
734 mFolderSelector = findViewById(R.id.folder_selector);
735
Leon Scroggins162f8352010-10-18 15:02:44 -0400736 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
737 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400738 mFolderNamer.setOnEditorActionListener(this);
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500739 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
740 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400741
742 mAddNewFolder = findViewById(R.id.add_new_folder);
743 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400744 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400745
Leon Scroggins74dbe012010-10-15 10:54:27 -0400746 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
747 mCrumbs.setUseBackButton(true);
748 mCrumbs.setController(this);
Michael Kolb5a72f182011-01-13 20:35:06 -0800749 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400750 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800751 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400752
Leon Scroggins III052ce662010-09-13 14:44:16 -0400753 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400754 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400755 View empty = findViewById(R.id.empty);
756 mListView.setEmptyView(empty);
757 mListView.setAdapter(mAdapter);
758 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400759 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500760
Leon Scroggins75630672011-01-13 17:56:15 -0500761 mFakeTitleHolder = findViewById(R.id.title_holder);
762
Leon Scroggins504433a2011-01-13 12:26:52 -0500763 if (!window.getDecorView().isInTouchMode()) {
764 mButton.requestFocus();
765 }
766
767 String[] accountInfo = getAccountNameAndType(this);
768 if (accountInfo == null) {
769 onRootFolderFound(BrowserProvider2.FIXED_ID_ROOT);
770 } else {
771 Bundle args = new Bundle();
772 args.putString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, accountInfo[0]);
773 args.putString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, accountInfo[1]);
774 getLoaderManager().initLoader(LOADER_ID_FIND_ROOT, args, this);
775 }
776
777 }
778
Leon Scroggins75630672011-01-13 17:56:15 -0500779 private void showRemoveButton() {
780 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
781 mRemoveLink = findViewById(R.id.remove);
782 mRemoveLink.setVisibility(View.VISIBLE);
783 mRemoveLink.setOnClickListener(this);
784 }
785
Leon Scroggins504433a2011-01-13 12:26:52 -0500786 // Called once we have determined which folder is the root folder
787 private void onRootFolderFound(long root) {
788 mRootFolder = root;
789 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
790 mCurrentFolder = mRootFolder;
791 }
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500792 setupTopCrumb();
Leon Scrogginsbdff8a72011-02-11 15:49:04 -0500793 if (mEditingExisting || TextUtils.isEmpty(mOriginalUrl)
794 || !(mMap != null && mMap.getBoolean(CHECK_FOR_DUPE))) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500795 onCurrentFolderFound();
796 } else {
797 // User is attempting to bookmark a site, rather than deliberately
798 // editing a bookmark. Rather than let them create a duplicate
799 // bookmark, see if the bookmark already exists.
800 getLoaderManager().initLoader(LOADER_ID_CHECK_FOR_DUPE, null, this);
801 }
802 }
803
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500804 private void setupTopCrumb() {
805 String name = getString(R.string.bookmarks);
806 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
807 new Folder(name, mRootFolder));
808 // To better match the other folders.
809 mTopLevelLabel.setCompoundDrawablePadding(6);
810 }
811
Leon Scroggins504433a2011-01-13 12:26:52 -0500812 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400813 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500814 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400815 // Find all the folders
816 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500817 // Since we're not in the root folder, change the selection to other
818 // folder now. The text will get changed once we select the correct
819 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500820 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500821 } else {
822 setShowBookmarkIcon(true);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500823 if (!mEditingExisting) {
824 // Find the most recently saved bookmark, so that we can include it in
825 // the list of options to save the current bookmark.
826 manager.initLoader(LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK, null, this);
827 }
Leon Scroggins504433a2011-01-13 12:26:52 -0500828 if (!mEditingFolder) {
829 // Initially the "Bookmarks" folder should be showing, rather than
830 // the home screen. In the editing folder case, home screen is not
831 // an option, so "Bookmarks" folder is already at the top.
832 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
833 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400834 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400835 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400836 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500837}
Leon Scroggins8baaa632010-12-08 19:46:53 -0500838 /**
839 * Get the account name and type of the currently synced account.
840 * @param context Context to access preferences.
841 * @return null if no account name or type. Otherwise, the result will be
842 * an array of two Strings, the accountName and accountType, respectively.
843 */
844 private String[] getAccountNameAndType(Context context) {
845 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
846 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
847 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
848 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
849 return null;
850 }
851 return new String[] { accountName, accountType };
852 }
853
Leon Scroggins02065b02010-01-04 14:30:13 -0500854 /**
855 * Runnable to save a bookmark, so it can be performed in its own thread.
856 */
857 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400858 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500859 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200860 private Context mContext;
861 public SaveBookmarkRunnable(Context ctx, Message msg) {
862 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500863 mMessage = msg;
864 }
865 public void run() {
866 // Unbundle bookmark data.
867 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400868 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
869 String url = bundle.getString(BrowserContract.Bookmarks.URL);
870 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500871 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400872 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
873 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500874
875 // Save to the bookmarks DB.
876 try {
877 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400878 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
879 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500880 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200881 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500882 }
883 mMessage.arg1 = 1;
884 } catch (IllegalStateException e) {
885 mMessage.arg1 = 0;
886 }
887 mMessage.sendToTarget();
888 }
889 }
890
John Reckc8490812010-11-22 14:15:36 -0800891 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
892 Context mContext;
893 Long mId;
894
895 public UpdateBookmarkTask(Context context, long id) {
896 mContext = context;
897 mId = id;
898 }
899
900 @Override
901 protected Void doInBackground(ContentValues... params) {
902 if (params.length != 1) {
903 throw new IllegalArgumentException("No ContentValues provided!");
904 }
905 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
906 mContext.getContentResolver().update(
907 uri,
908 params[0], null, null);
909 return null;
910 }
911 }
912
Ben Murdoch1794fe22009-09-29 18:14:30 +0100913 private void createHandler() {
914 if (mHandler == null) {
915 mHandler = new Handler() {
916 @Override
917 public void handleMessage(Message msg) {
918 switch (msg.what) {
919 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500920 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100921 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
922 Toast.LENGTH_LONG).show();
923 } else {
924 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
925 Toast.LENGTH_LONG).show();
926 }
927 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400928 case TOUCH_ICON_DOWNLOADED:
929 Bundle b = msg.getData();
930 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400931 AddBookmarkPage.this,
932 b.getString(BrowserContract.Bookmarks.URL),
933 b.getString(BrowserContract.Bookmarks.TITLE),
934 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
935 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400936 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500937 case BOOKMARK_DELETED:
938 finish();
939 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100940 }
941 }
942 };
943 }
944 }
945
The Android Open Source Project0c908882009-03-03 19:32:16 -0800946 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100947 * 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 -0800948 */
949 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100950 createHandler();
951
The Android Open Source Project0c908882009-03-03 19:32:16 -0800952 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100953 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700954 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100955
The Android Open Source Project0c908882009-03-03 19:32:16 -0800956 boolean emptyTitle = title.length() == 0;
957 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
958 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800959 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800960 if (emptyTitle) {
961 mTitle.setError(r.getText(R.string.bookmark_needs_title));
962 }
963 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400964 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800965 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400966 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100967
The Android Open Source Project0c908882009-03-03 19:32:16 -0800968 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000969 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800970 if (!mEditingFolder) {
971 try {
972 // We allow bookmarks with a javascript: scheme, but these will in most cases
973 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000974
John Reckc8490812010-11-22 14:15:36 -0800975 if (!url.toLowerCase().startsWith("javascript:")) {
976 URI uriObj = new URI(url);
977 String scheme = uriObj.getScheme();
978 if (!Bookmarks.urlHasAcceptableScheme(url)) {
979 // If the scheme was non-null, let the user know that we
980 // can't save their bookmark. If it was null, we'll assume
981 // they meant http when we parse it in the WebAddress class.
982 if (scheme != null) {
983 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
984 return false;
985 }
986 WebAddress address;
987 try {
988 address = new WebAddress(unfilteredUrl);
989 } catch (ParseException e) {
990 throw new URISyntaxException("", "");
991 }
992 if (address.getHost().length() == 0) {
993 throw new URISyntaxException("", "");
994 }
995 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000996 }
Ben Murdochde353622009-10-12 10:29:00 +0100997 }
John Reckc8490812010-11-22 14:15:36 -0800998 } catch (URISyntaxException e) {
999 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1000 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001001 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001002 }
Ben Murdochaac7aa62009-09-17 16:57:40 +01001003
Leon Scroggins88d08032010-10-21 15:17:10 -04001004 if (mSaveToHomeScreen) {
1005 mEditingExisting = false;
1006 }
1007
1008 boolean urlUnmodified = url.equals(mOriginalUrl);
1009
Ben Murdoch1794fe22009-09-29 18:14:30 +01001010 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -08001011 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
1012 ContentValues values = new ContentValues();
1013 values.put(BrowserContract.Bookmarks.TITLE, title);
1014 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
1015 if (!mEditingFolder) {
1016 values.put(BrowserContract.Bookmarks.URL, url);
1017 if (!urlUnmodified) {
1018 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
1019 }
1020 }
1021 if (values.size() > 0) {
1022 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
1023 }
1024 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +01001025 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -04001026 Bitmap thumbnail;
1027 Bitmap favicon;
1028 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001029 thumbnail = (Bitmap) mMap.getParcelable(
1030 BrowserContract.Bookmarks.THUMBNAIL);
1031 favicon = (Bitmap) mMap.getParcelable(
1032 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -04001033 } else {
1034 thumbnail = null;
1035 favicon = null;
1036 }
1037
Ben Murdoch1794fe22009-09-29 18:14:30 +01001038 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001039 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
1040 bundle.putString(BrowserContract.Bookmarks.URL, url);
1041 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -04001042
1043 if (mSaveToHomeScreen) {
1044 if (mTouchIconUrl != null && urlUnmodified) {
1045 Message msg = Message.obtain(mHandler,
1046 TOUCH_ICON_DOWNLOADED);
1047 msg.setData(bundle);
1048 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -04001049 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -04001050 icon.execute(mTouchIconUrl);
1051 } else {
1052 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
1053 title, null /*touchIcon*/, favicon));
1054 }
1055 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001056 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
1057 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
1058 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -04001059 // Post a message to write to the DB.
1060 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
1061 msg.setData(bundle);
1062 // Start a new thread so as to not slow down the UI
1063 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
1064 t.start();
1065 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001066 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001067 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001068 }
1069 return true;
1070 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001071
1072 /*
1073 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1074 */
1075 public static class CustomListView extends ListView {
1076 private EditText mEditText;
1077
1078 public void addEditText(EditText editText) {
1079 mEditText = editText;
1080 }
1081
1082 public CustomListView(Context context) {
1083 super(context);
1084 }
1085
1086 public CustomListView(Context context, AttributeSet attrs) {
1087 super(context, attrs);
1088 }
1089
1090 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1091 super(context, attrs, defStyle);
1092 }
1093
1094 @Override
1095 public boolean checkInputConnectionProxy(View view) {
1096 return view == mEditText;
1097 }
1098 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001099}